blob: 282ec5052e4f72b7e746763683378d45e4a3e862 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Jorim Jaggife762342016-10-13 14:33:27 +020019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
21import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
22import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
23import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
24import static android.app.ActivityManager.RESIZE_MODE_FORCED;
25import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
26import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
36import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
41
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
67import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
69import static com.android.server.am.ActivityManagerService.ANIMATE;
70import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
71import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
72import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
73import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
74import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
75import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
76import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
77import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
78import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
79import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
80import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
81import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
82import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
83import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
84import static com.android.server.am.ActivityStack.STACK_VISIBLE;
85import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
86import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
90import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
91
Svetoslav7008b512015-06-24 18:47:07 -070092import android.Manifest;
Tony Mak853304c2016-04-18 15:17:41 +010093import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070094import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080096import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070097import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080098import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.app.ActivityOptions;
100import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -0700101import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700103import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700104import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400105import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700106import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400107import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700109import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700111import android.content.Intent;
112import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700114import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.pm.PackageManager;
116import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100117import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700119import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800120import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121import android.hardware.display.DisplayManager;
122import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800123import android.hardware.display.DisplayManagerGlobal;
124import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800125import android.hardware.input.InputManager;
126import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700127import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100128import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700129import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700130import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700132import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700133import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700135import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700136import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700137import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400138import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700140import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700141import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700142import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100143import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700144import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700145import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400146import android.provider.Settings;
147import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700148import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700150import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700151import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700152import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700153import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800154import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800156import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800157import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800158import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800159
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800160import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700161import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400162import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700163import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400164import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800165import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700166import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700168
Craig Mautner8d341ef2013-03-26 09:03:27 -0700169import java.io.FileDescriptor;
170import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700171import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700172import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700173import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700174import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800176import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700177import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700178
Jorim Jaggife762342016-10-13 14:33:27 +0200179public class ActivityStackSupervisor extends ConfigurationContainer
Andrii Kulian1779e612016-10-12 21:58:25 -0700180 implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800181 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700183 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700185 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800192 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194
Craig Mautnerf3333272013-04-22 10:55:53 -0700195 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700197
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203
Craig Mautner05d29032013-05-03 13:40:13 -0700204 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
205 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
206 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
210 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
211 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700212 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400213 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
214 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700215 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700216 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700217 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800218 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
219 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800220
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700222
Jason Monk62515be2014-05-21 16:06:19 -0400223 private static final String LOCK_TASK_TAG = "Lock-to-App";
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. stack) that we are trying to get
226 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800227 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700229 // Used to indicate that windows of activities should be preserved during the resize.
230 static final boolean PRESERVE_WINDOWS = true;
231
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232 // Used to indicate if an object (e.g. task) should be moved/created
233 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700234 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235
236 // Used to indicate that an objects (e.g. task) removal from its container
237 // (e.g. stack) is due to it moving to another container.
238 static final boolean MOVING = true;
239
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700240 // Force the focus to change to the stack we are moving a task to..
241 static final boolean FORCE_FOCUS = true;
242
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700243 // Restore task from the saved recents if it can't be found in any live stack.
244 static final boolean RESTORE_FROM_RECENTS = true;
245
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700246 // Don't execute any calls to resume.
247 static final boolean DEFER_RESUME = true;
248
Svetoslav7008b512015-06-24 18:47:07 -0700249 // Activity actions an app cannot start if it uses a permission which is not granted.
250 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
251 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700252
Svetoslav7008b512015-06-24 18:47:07 -0700253 static {
254 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
255 Manifest.permission.CAMERA);
256 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
257 Manifest.permission.CAMERA);
258 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
259 Manifest.permission.CALL_PHONE);
260 }
261
Svet Ganov99b60432015-06-27 13:15:22 -0700262 /** Action restriction: launching the activity is not restricted. */
263 private static final int ACTIVITY_RESTRICTION_NONE = 0;
264 /** Action restriction: launching the activity is restricted by a permission. */
265 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
266 /** Action restriction: launching the activity is restricted by an app op. */
267 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700268
justinzhang5286d3f2014-05-12 17:06:01 -0400269 /** Status Bar Service **/
270 private IBinder mToken = new Binder();
271 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400272 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400273
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700274 // For debugging to make sure the caller when acquiring/releasing our
275 // wake lock is the system process.
276 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800277 /** The number of distinct task ids that can be assigned to the tasks of a single user */
278 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700279
Craig Mautner27084302013-03-25 08:05:25 -0700280 final ActivityManagerService mService;
281
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800282 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800283
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700284 final ActivityStackSupervisorHandler mHandler;
285
286 /** Short cut */
287 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800288 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700289
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700290 /** Counter for next free stack ID to use for dynamic activity stacks. */
291 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800293 /**
294 * Maps the task identifier that activities are currently being started in to the userId of the
295 * task. Each time a new task is created, the entry for the userId of the task is incremented
296 */
297 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Craig Mautner2420ead2013-04-01 17:13:20 -0700299 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800300 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700301
Craig Mautnere0a38842013-12-16 16:14:02 -0800302 /** The stack containing the launcher app. Assumed to always be attached to
303 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800304 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700305
Craig Mautnere0a38842013-12-16 16:14:02 -0800306 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800307 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700308
Craig Mautner4a1cb222013-12-04 16:14:06 -0800309 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
310 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800311 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800312 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700313
314 /** List of activities that are waiting for a new activity to become visible before completing
315 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700319 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320
321 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700322 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700323
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324 /** List of activities that are ready to be stopped, but waiting for the next activity to
325 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700327
Craig Mautnerf3333272013-04-22 10:55:53 -0700328 /** List of activities that are ready to be finished, but waiting for the previous activity to
329 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700331
Craig Mautner0eea92c2013-05-16 13:35:39 -0700332 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800333 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700334
Wale Ogunwale22e25262016-02-01 10:32:02 -0800335 /** List of activities whose multi-window mode changed that we need to report to the
336 * application */
337 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
338
339 /** List of activities whose picture-in-picture mode changed that we need to report to the
340 * application */
341 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
342
Craig Mautnerf3333272013-04-22 10:55:53 -0700343 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700344 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700345
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
347 * is being brought in front of us. */
348 boolean mUserLeaving = false;
349
Craig Mautner0eea92c2013-05-16 13:35:39 -0700350 /** Set when we have taken too long waiting to go to sleep. */
351 boolean mSleepTimeout = false;
352
353 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700354 * We don't want to allow the device to go to sleep while in the process
355 * of launching an activity. This is primarily to allow alarm intent
356 * receivers to launch an activity and get that to run before the device
357 * goes back to sleep.
358 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700359 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360
361 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700362 * Set when the system is going to sleep, until we have
363 * successfully paused the current activity and released our wake lock.
364 * At that point the system is allowed to actually sleep.
365 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700366 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700368 /** Stack id of the front stack when user switched, indexed by userId. */
369 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700370
Craig Mautner4504de52013-12-20 09:06:56 -0800371 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800372 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700373 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374
375 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700376 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377
Jeff Brownca9bc702014-02-11 14:32:56 -0800378 InputManagerInternal mInputManagerInternal;
379
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
381 * may be finished until there is only one entry left. If this is empty the system is not
382 * in lockTask mode. */
383 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000384 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700385 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
386 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000387 */
388 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400389 /**
390 * Notifies the user when entering/exiting lock-task.
391 */
392 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800393
Wale Ogunwaled046a012015-12-24 13:05:59 -0800394 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800395 boolean inResumeTopActivity;
396
Andrii Kulian1e32e022016-09-16 15:29:34 -0700397 /**
398 * Temporary rect used during docked stack resize calculation so we don't need to create a new
399 * object each time.
400 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700401 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700402
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700403 // The default minimal size that will be used if the activity doesn't specify its minimal size.
404 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700405 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700406
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700407 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
408 private boolean mTaskLayersChanged = true;
409
Jorim Jaggi275561a2016-02-23 10:11:02 -0500410 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800411
Jorim Jaggidc249c42015-12-15 14:57:31 -0800412 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
413
Andrii Kulian1779e612016-10-12 21:58:25 -0700414 @Override
415 protected int getChildCount() {
416 return mActivityDisplays.size();
417 }
418
419 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700420 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700421 return mActivityDisplays.valueAt(index);
422 }
423
424 @Override
425 protected ConfigurationContainer getParent() {
426 return null;
427 }
428
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700429 Configuration getDisplayOverrideConfiguration(int displayId) {
430 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
431 if (activityDisplay == null) {
432 throw new IllegalArgumentException("No display found with id: " + displayId);
433 }
434
435 return activityDisplay.getOverrideConfiguration();
436 }
437
438 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
439 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
440 if (activityDisplay == null) {
441 throw new IllegalArgumentException("No display found with id: " + displayId);
442 }
443
444 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
445 }
446
Wale Ogunwale39381972015-12-17 17:15:29 -0800447 static class FindTaskResult {
448 ActivityRecord r;
449 boolean matchedByRootAffinity;
450 }
451 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
452
Craig Mautneree36c772014-07-16 14:56:05 -0700453 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100454 * Used to keep track whether app visibilities got changed since the last pause. Useful to
455 * determine whether to invoke the task stack change listener after pausing.
456 */
457 boolean mAppVisibilitiesChangedSinceLastPause;
458
459 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100460 * Set of tasks that are in resizing mode during an app transition to fill the "void".
461 */
462 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
463
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700464
465 /**
466 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
467 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
468 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
469 * like the docked stack going empty.
470 */
471 private boolean mAllowDockedStackResize = true;
472
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100473 /**
Tony Mak853304c2016-04-18 15:17:41 +0100474 * Is dock currently minimized.
475 */
476 boolean mIsDockMinimized;
477
Jorim Jaggife762342016-10-13 14:33:27 +0200478 final KeyguardController mKeyguardController;
479
Tony Mak853304c2016-04-18 15:17:41 +0100480 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700481 * Description of a request to start a new activity, which has been held
482 * due to app switches being disabled.
483 */
484 static class PendingActivityLaunch {
485 final ActivityRecord r;
486 final ActivityRecord sourceRecord;
487 final int startFlags;
488 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700489 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700490
491 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700492 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700493 r = _r;
494 sourceRecord = _sourceRecord;
495 startFlags = _startFlags;
496 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700497 callerApp = _callerApp;
498 }
499
500 void sendErrorResult(String message) {
501 try {
502 if (callerApp.thread != null) {
503 callerApp.thread.scheduleCrash(message);
504 }
505 } catch (RemoteException e) {
506 Slog.e(TAG, "Exception scheduling crash of failed "
507 + "activity launcher sourceRecord=" + sourceRecord, e);
508 }
Craig Mautneree36c772014-07-16 14:56:05 -0700509 }
510 }
511
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800512 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700513 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700514 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800515 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800516 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200517 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700518 }
519
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800520 void setRecentTasks(RecentTasks recentTasks) {
521 mRecentTasks = recentTasks;
522 }
523
Jeff Brown2c43c332014-06-12 22:38:59 -0700524 /**
525 * At the time when the constructor runs, the power manager has not yet been
526 * initialized. So we initialize our wakelocks afterwards.
527 */
528 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700530 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700531 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700532 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700533 }
534
justinzhang5286d3f2014-05-12 17:06:01 -0400535 // This function returns a IStatusBarService. The value is from ServiceManager.
536 // getService and is cached.
537 private IStatusBarService getStatusBarService() {
538 synchronized (mService) {
539 if (mStatusBarService == null) {
540 mStatusBarService = IStatusBarService.Stub.asInterface(
541 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
542 if (mStatusBarService == null) {
543 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
544 }
545 }
546 return mStatusBarService;
547 }
548 }
549
Jason Monk35c62a42014-06-17 10:24:47 -0400550 private IDevicePolicyManager getDevicePolicyManager() {
551 synchronized (mService) {
552 if (mDevicePolicyManager == null) {
553 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
554 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
555 if (mDevicePolicyManager == null) {
556 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
557 }
558 }
559 return mDevicePolicyManager;
560 }
561 }
562
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700563 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 synchronized (mService) {
565 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200566 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567
568 mDisplayManager =
569 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
570 mDisplayManager.registerDisplayListener(this, null);
571
572 Display[] displays = mDisplayManager.getDisplays();
573 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
574 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800575 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700576 if (activityDisplay.mDisplay == null) {
577 throw new IllegalStateException("Default Display does not exist");
578 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800579 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700580 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 }
582
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800583 mHomeStack = mFocusedStack = mLastFocusedStack =
584 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800585
586 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 }
Craig Mautner27084302013-03-25 08:05:25 -0700588 }
589
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700590 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700592 }
593
Craig Mautnerde4ef022013-04-07 19:01:33 -0700594 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700596 }
597
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700598 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700599 if (stack == null) {
600 return false;
601 }
602
Craig Mautnerdf88d732014-01-27 09:21:32 -0800603 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
604 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700605 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800606 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800607 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700608 }
609
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700610 /** The top most stack. */
611 boolean isFrontStack(ActivityStack stack) {
612 if (stack == null) {
613 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700615
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700616 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
617 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700618 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800619 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700620 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
621 }
622
Wale Ogunwaled046a012015-12-24 13:05:59 -0800623 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800624 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
625 if (!focusCandidate.isFocusable()) {
626 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
627 focusCandidate = focusCandidate.getNextFocusableStackLocked();
628 }
629
630 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800631 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800632 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800633
Wale Ogunwaled046a012015-12-24 13:05:59 -0800634 EventLogTags.writeAmFocusedStack(
635 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
636 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
637 }
638
639 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800640 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800641 if (r != null && r.idle) {
642 checkFinishBootingLocked();
643 }
644 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700645 }
646
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700647 void moveHomeStackToFront(String reason) {
648 mHomeStack.moveToFront(reason);
649 }
650
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700651 /** Returns true if the focus activity was adjusted to the home stack top activity. */
652 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700653 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700654 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700655 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700656 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700657
Craig Mautner84984fa2014-06-19 11:19:20 -0700658 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700659
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700660 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700661 if (top == null) {
662 return false;
663 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700664 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700665 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700666 }
667
Craig Mautner299f9602015-01-26 09:47:33 -0800668 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700669 if (!mService.mBooting && !mService.mBooted) {
670 // Not ready yet!
671 return false;
672 }
673
Craig Mautner84984fa2014-06-19 11:19:20 -0700674 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700675 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700676 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700677 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678
Craig Mautner84984fa2014-06-19 11:19:20 -0700679 if (prev != null) {
680 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
681 }
682
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700683 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
684 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800685 final String myReason = reason + " resumeHomeStackTask";
686
Mark Lua56ea122015-10-08 13:31:01 +0800687 // Only resume home activity if isn't finishing.
688 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700689 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800690 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700691 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800692 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700693 }
694
Craig Mautner8d341ef2013-03-26 09:03:27 -0700695 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700696 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700697 }
698
699 /**
700 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
701 * @param id Id of the task we would like returned.
702 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
703 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700704 * @param stackId The stack to restore the task to (default launch stack will be used if
705 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700706 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700707 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800708 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800710 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
712 ActivityStack stack = stacks.get(stackNdx);
713 TaskRecord task = stack.taskForIdLocked(id);
714 if (task != null) {
715 return task;
716 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700717 }
718 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800719
720 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700721 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800722 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800723 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700724 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800725 return null;
726 }
727
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700728 if (!restoreFromRecents) {
729 return task;
730 }
731
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700732 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
734 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800735 return null;
736 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800738 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700739 }
740
Craig Mautner6170f732013-04-02 13:05:23 -0700741 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
746 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
747 if (r != null) {
748 return r;
749 }
Craig Mautner6170f732013-04-02 13:05:23 -0700750 }
751 }
752 return null;
753 }
754
Tony Mak853304c2016-04-18 15:17:41 +0100755 /**
756 * TODO: Handle freefom mode.
757 * @return true when credential confirmation is needed for the user and there is any
758 * activity started by the user in any visible stack.
759 */
760 boolean isUserLockedProfile(@UserIdInt int userId) {
761 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
762 return false;
763 }
764 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
765 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
766 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
767 for (final ActivityStack activityStack : activityStacks) {
768 if (activityStack == null) {
769 continue;
770 }
771 if (activityStack.topRunningActivityLocked() == null) {
772 continue;
773 }
774 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
775 continue;
776 }
777 if (activityStack.isDockedStack() && mIsDockMinimized) {
778 continue;
779 }
780 final TaskRecord topTask = activityStack.topTask();
781 if (topTask == null) {
782 continue;
783 }
784 // To handle the case that work app is in the task but just is not the top one.
785 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
786 final ActivityRecord activityRecord = topTask.mActivities.get(i);
787 if (activityRecord.userId == userId) {
788 return true;
789 }
790 }
791 }
792 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000793 }
794
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800795 void setNextTaskIdForUserLocked(int taskId, int userId) {
796 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
797 if (taskId > currentTaskId) {
798 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700799 }
800 }
801
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700802 static int nextTaskIdForUser(int taskId, int userId) {
803 int nextTaskId = taskId + 1;
804 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
805 // Wrap around as there will be smaller task ids that are available now.
806 nextTaskId -= MAX_TASK_IDS_PER_USER;
807 }
808 return nextTaskId;
809 }
810
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800811 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800812 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
813 // for a userId u, a taskId can only be in the range
814 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
815 // 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 -0700816 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800817 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
818 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
819 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700820 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800821 if (candidateTaskId == currentTaskId) {
822 // Something wrong!
823 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
824 throw new IllegalStateException("Cannot get an available task id."
825 + " Reached limit of " + MAX_TASK_IDS_PER_USER
826 + " running tasks per user.");
827 }
828 }
829 mCurTaskIdForUser.put(userId, candidateTaskId);
830 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700831 }
832
Chong Zhang6cda19c2016-06-14 19:07:56 -0700833 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800834 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 if (stack == null) {
836 return null;
837 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700838 ActivityRecord resumedActivity = stack.mResumedActivity;
839 if (resumedActivity == null || resumedActivity.app == null) {
840 resumedActivity = stack.mPausingActivity;
841 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700842 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700843 }
844 }
845 return resumedActivity;
846 }
847
Dianne Hackbornff072722014-09-24 10:56:28 -0700848 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700849 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
852 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
854 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700855 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 continue;
857 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700858 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 if (hr != null) {
860 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
861 && processName.equals(hr.processName)) {
862 try {
George Mount2c92c972014-03-20 09:38:23 -0700863 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 didSomething = true;
865 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700866 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 Slog.w(TAG, "Exception in new application when starting activity "
868 + hr.intent.getComponent().flattenToShortString(), e);
869 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700870 }
Craig Mautner20e72272013-04-01 13:45:53 -0700871 }
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
873 }
874 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700875 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700876 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700877 }
Craig Mautner20e72272013-04-01 13:45:53 -0700878 return didSomething;
879 }
880
881 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800882 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
883 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
885 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700886 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 continue;
888 }
889 final ActivityRecord resumedActivity = stack.mResumedActivity;
890 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700891 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800892 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 return false;
894 }
Craig Mautner20e72272013-04-01 13:45:53 -0700895 }
896 }
Wei Wang65c7a152016-06-02 18:51:22 -0700897 // Send launch end powerhint when idle
898 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700899 return true;
900 }
901
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
904 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800905 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
906 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700907 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700909 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 return false;
911 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 }
913 }
914 }
915 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700916 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
918 mLastFocusedStack + " to=" + mFocusedStack);
919 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700920 return true;
921 }
922
923 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800924 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
926 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
928 final ActivityStack stack = stacks.get(stackNdx);
929 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800930 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700931 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800932 return false;
933 }
934 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700936 }
937 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800938 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 }
940
Craig Mautner2acc3892013-09-23 10:28:14 -0700941 /**
942 * Pause all activities in either all of the stacks or just the back stacks.
943 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700944 * @param resuming The resuming activity.
945 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
946 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700947 * @return true if any activity was paused as a result of this call.
948 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700949 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700950 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
952 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
954 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700955 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700956 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700958 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
959 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700961 }
962 }
963 return someActivityPaused;
964 }
965
Craig Mautnerde4ef022013-04-07 19:01:33 -0700966 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700967 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
969 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
971 final ActivityStack stack = stacks.get(stackNdx);
972 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700975 Slog.d(TAG_STATES,
976 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800977 pausing = false;
978 } else {
979 return false;
980 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700981 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700982 }
983 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700984 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 }
986
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700987 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700988 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500989 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
994 if (stack.mResumedActivity != null &&
995 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700996 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800997 }
998 }
999 }
1000 }
1001
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001002 void cancelInitializingActivities() {
1003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1004 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1005 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1006 stacks.get(stackNdx).cancelInitializingActivities();
1007 }
1008 }
1009 }
1010
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001011 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001012 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001013 }
1014
1015 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1016 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001017 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001018 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001019 if (w.who == null) {
1020 changed = true;
1021 w.timeout = false;
1022 if (r != null) {
1023 w.who = new ComponentName(r.info.packageName, r.info.name);
1024 }
1025 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1026 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001027 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001028 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001029 if (changed) {
1030 mService.notifyAll();
1031 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001032 }
1033
Chong Zhang5022da32016-06-21 16:31:37 -07001034 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1035 boolean changed = false;
1036 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1037 WaitResult w = mWaitingActivityLaunched.remove(i);
1038 if (w.who == null) {
1039 changed = true;
1040 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1041 // the starting activity ends up moving another activity to front, and it should
1042 // wait for this new activity to become visible instead.
1043 // Do not modify other fields.
1044 w.result = START_TASK_TO_FRONT;
1045 }
1046 }
1047 if (changed) {
1048 mService.notifyAll();
1049 }
1050 }
1051
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001052 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1053 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001054 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001055 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001056 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001057 if (w.who == null) {
1058 changed = true;
1059 w.timeout = timeout;
1060 if (r != null) {
1061 w.who = new ComponentName(r.info.packageName, r.info.name);
1062 }
1063 w.thisTime = thisTime;
1064 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001065 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001068 if (changed) {
1069 mService.notifyAll();
1070 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001071 }
1072
Craig Mautner29219d92013-04-16 20:19:12 -07001073 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001074 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001075 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001076 if (r != null) {
1077 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001078 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001079
Andrii Kulian7318d632016-07-20 18:59:28 -07001080 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001081 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001082 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1083 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001084 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001085 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001086 if (r != null) {
1087 return r;
1088 }
1089 }
1090 }
1091 return null;
1092 }
1093
Dianne Hackborn09233282014-04-30 11:33:59 -07001094 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001095 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001096 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1097 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001098 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001100 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1102 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001103 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001105 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001106 }
1107 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001108
1109 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1110 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1111 while (maxNum > 0) {
1112 long mostRecentActiveTime = Long.MIN_VALUE;
1113 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001114 final int numTaskLists = runningTaskLists.size();
1115 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1116 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001117 if (!stackTaskList.isEmpty()) {
1118 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1119 if (lastActiveTime > mostRecentActiveTime) {
1120 mostRecentActiveTime = lastActiveTime;
1121 selectedStackList = stackTaskList;
1122 }
1123 }
1124 }
1125 if (selectedStackList != null) {
1126 list.add(selectedStackList.remove(0));
1127 --maxNum;
1128 } else {
1129 break;
1130 }
1131 }
Craig Mautner20e72272013-04-01 13:45:53 -07001132 }
1133
Todd Kennedy7440f172015-12-09 14:31:22 -08001134 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1135 ProfilerInfo profilerInfo) {
1136 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001137 if (aInfo != null) {
1138 // Store the found target back into the intent, because now that
1139 // we have it we never want to do this again. For example, if the
1140 // user navigates back to this point in the history, we should
1141 // always restart the exact same activity.
1142 intent.setComponent(new ComponentName(
1143 aInfo.applicationInfo.packageName, aInfo.name));
1144
1145 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001146 if (!aInfo.processName.equals("system")) {
1147 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001148 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001149 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001150
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001151 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1152 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1153 }
1154
Man Caocfa78b22015-06-11 20:14:34 -07001155 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1156 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1157 }
1158
1159 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001160 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001161 }
1162 }
1163 }
1164 return aInfo;
1165 }
1166
Todd Kennedy7440f172015-12-09 14:31:22 -08001167 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001168 return resolveIntent(intent, resolvedType, userId, 0);
1169 }
1170
1171 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001172 try {
1173 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001174 PackageManager.MATCH_DEFAULT_ONLY | flags
1175 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001176 } catch (RemoteException e) {
1177 }
1178 return null;
1179 }
1180
1181 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1182 ProfilerInfo profilerInfo, int userId) {
1183 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1184 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1185 }
1186
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001187 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1188 boolean andResume, boolean checkConfig) throws RemoteException {
1189
1190 if (!allPausedActivitiesComplete()) {
1191 // While there are activities pausing we skipping starting any new activities until
1192 // pauses are complete. NOTE: that we also do this for activities that are starting in
1193 // the paused state because they will first be resumed then paused on the client side.
1194 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1195 "realStartActivityLocked: Skipping start of r=" + r
1196 + " some activities pausing...");
1197 return false;
1198 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001199
Craig Mautner2568c3a2015-03-26 14:22:34 -07001200 if (andResume) {
1201 r.startFreezingScreenLocked(app, 0);
1202 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001203
Craig Mautner2568c3a2015-03-26 14:22:34 -07001204 // schedule launch ticks to collect information about slow apps.
1205 r.startLaunchTickingLocked();
1206 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001207
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001208 // Have the window manager re-evaluate the orientation of the screen based on the new
1209 // activity order. Note that as a result of this, it can call back into the activity
1210 // manager with a new orientation. We don't care about that, because the activity is not
1211 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001212 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001213 final int displayId = r.getDisplayId();
1214 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1215 getDisplayOverrideConfiguration(displayId),
1216 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001217 // Deferring resume here because we're going to launch new activity shortly.
1218 // We don't want to perform a redundant launch of the same record while ensuring
1219 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001220 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1221 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 }
1223
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001224 if (mKeyguardController.isKeyguardLocked()) {
1225 mWindowManager.notifyUnknownAppVisibilityLaunched(r.appToken);
1226 }
1227
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 r.app = app;
1229 app.waitingToKill = null;
1230 r.launchCount++;
1231 r.lastLaunchTime = SystemClock.uptimeMillis();
1232
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001233 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
1235 int idx = app.activities.indexOf(r);
1236 if (idx < 0) {
1237 app.activities.add(r);
1238 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001239 mService.updateLruProcessLocked(app, true, null);
1240 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001241
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001243 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1244 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001245 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 }
1247
Andrii Kulian02b7a832016-10-06 23:11:56 -07001248 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 try {
1250 if (app.thread == null) {
1251 throw new RemoteException();
1252 }
1253 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001254 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 if (andResume) {
1256 results = r.results;
1257 newIntents = r.newIntents;
1258 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001259 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1260 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1261 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 if (andResume) {
1263 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1264 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001265 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001267 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001268 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001269 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001271 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1272 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 r.sleeping = false;
1274 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001275 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 mService.showAskCompatModeDialogLocked(r);
1277 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001278 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1280 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1281 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001282 final String profileFile = mService.mProfileFile;
1283 if (profileFile != null) {
1284 ParcelFileDescriptor profileFd = mService.mProfileFd;
1285 if (profileFd != null) {
1286 try {
1287 profileFd = profileFd.dup();
1288 } catch (IOException e) {
1289 if (profileFd != null) {
1290 try {
1291 profileFd.close();
1292 } catch (IOException o) {
1293 }
1294 profileFd = null;
1295 }
1296 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001298
1299 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1300 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 }
1302 }
1303 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001304
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305 if (andResume) {
1306 app.hasShownUi = true;
1307 app.pendingUiClean = true;
1308 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001309 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001310 // Because we could be starting an Activity in the system process this may not go across
1311 // a Binder interface which would create a new Configuration. Consequently we have to
1312 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001314 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001315 new Configuration(mService.getGlobalConfiguration()),
1316 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1317 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1318 r.persistentState, results, newIntents, !andResume,
1319 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001320
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001321 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 // This may be a heavy-weight process! Note that the package
1323 // manager will ensure that only activity can run in the main
1324 // process of the .apk, which is the only thing that will be
1325 // considered heavy-weight.
1326 if (app.processName.equals(app.info.packageName)) {
1327 if (mService.mHeavyWeightProcess != null
1328 && mService.mHeavyWeightProcess != app) {
1329 Slog.w(TAG, "Starting new heavy weight process " + app
1330 + " when already running "
1331 + mService.mHeavyWeightProcess);
1332 }
1333 mService.mHeavyWeightProcess = app;
1334 Message msg = mService.mHandler.obtainMessage(
1335 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1336 msg.obj = r;
1337 mService.mHandler.sendMessage(msg);
1338 }
1339 }
1340
1341 } catch (RemoteException e) {
1342 if (r.launchFailed) {
1343 // This is the second time we failed -- finish activity
1344 // and give up.
1345 Slog.e(TAG, "Second failure launching "
1346 + r.intent.getComponent().flattenToShortString()
1347 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001348 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001349 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1350 "2nd-crash", false);
1351 return false;
1352 }
1353
1354 // This is the first time we failed -- restart process and
1355 // retry.
1356 app.activities.remove(r);
1357 throw e;
1358 }
1359
1360 r.launchFailed = false;
1361 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001362 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 }
1364
1365 if (andResume) {
1366 // As part of the process of launching, ActivityThread also performs
1367 // a resume.
1368 stack.minimalResumeActivityLocked(r);
1369 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001370 // This activity is not starting in the resumed state... which should look like we asked
1371 // it to pause+stop (but remain visible), and it has done so and reported back the
1372 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001373 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001374 "Moving to PAUSED: " + r + " (starting in paused state)");
1375 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 }
1377
1378 // Launch the new version setup screen if needed. We do this -after-
1379 // launching the initial activity (that is, home), so that it can have
1380 // a chance to initialize itself while in the background, making the
1381 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001382 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 mService.startSetupActivityLocked();
1384 }
1385
Dianne Hackborn465fa392014-09-14 14:21:18 -07001386 // Update any services we are bound to that might care about whether
1387 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001388 if (r.app != null) {
1389 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1390 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001391
Craig Mautner2420ead2013-04-01 17:13:20 -07001392 return true;
1393 }
1394
Craig Mautnere79d42682013-04-01 19:01:53 -07001395 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001396 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001397 // Is this activity's application already running?
1398 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001399 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001400
Andrii Kulian02b7a832016-10-06 23:11:56 -07001401 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001402
1403 if (app != null && app.thread != null) {
1404 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001405 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1406 || !"android".equals(r.info.packageName)) {
1407 // Don't add this if it is a platform component that is marked
1408 // to run in multiple processes, because this is actually
1409 // part of the framework so doesn't make sense to track as a
1410 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001411 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1412 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001413 }
George Mount2c92c972014-03-20 09:38:23 -07001414 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001415 return;
1416 } catch (RemoteException e) {
1417 Slog.w(TAG, "Exception when starting activity "
1418 + r.intent.getComponent().flattenToShortString(), e);
1419 }
1420
1421 // If a dead object exception was thrown -- fall through to
1422 // restart the application.
1423 }
1424
1425 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001426 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001427 }
1428
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001429 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001430 String resultWho, int requestCode, int callingPid, int callingUid,
1431 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001432 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001433 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1434 callingUid);
1435 if (startAnyPerm == PERMISSION_GRANTED) {
1436 return true;
1437 }
1438 final int componentRestriction = getComponentRestrictionForCallingPackage(
1439 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1440 final int actionRestriction = getActionRestrictionForCallingPackage(
1441 intent.getAction(), callingPackage, callingPid, callingUid);
1442 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1443 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1444 if (resultRecord != null) {
1445 resultStack.sendActivityResultLocked(-1,
1446 resultRecord, resultWho, requestCode,
1447 Activity.RESULT_CANCELED, null);
1448 }
1449 final String msg;
1450 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1451 msg = "Permission Denial: starting " + intent.toString()
1452 + " from " + callerApp + " (pid=" + callingPid
1453 + ", uid=" + callingUid + ")" + " with revoked permission "
1454 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1455 } else if (!aInfo.exported) {
1456 msg = "Permission Denial: starting " + intent.toString()
1457 + " from " + callerApp + " (pid=" + callingPid
1458 + ", uid=" + callingUid + ")"
1459 + " not exported from uid " + aInfo.applicationInfo.uid;
1460 } else {
1461 msg = "Permission Denial: starting " + intent.toString()
1462 + " from " + callerApp + " (pid=" + callingPid
1463 + ", uid=" + callingUid + ")"
1464 + " requires " + aInfo.permission;
1465 }
1466 Slog.w(TAG, msg);
1467 throw new SecurityException(msg);
1468 }
1469
1470 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1471 final String message = "Appop Denial: starting " + intent.toString()
1472 + " from " + callerApp + " (pid=" + callingPid
1473 + ", uid=" + callingUid + ")"
1474 + " requires " + AppOpsManager.permissionToOp(
1475 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1476 Slog.w(TAG, message);
1477 return false;
1478 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1479 final String message = "Appop Denial: starting " + intent.toString()
1480 + " from " + callerApp + " (pid=" + callingPid
1481 + ", uid=" + callingUid + ")"
1482 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1483 Slog.w(TAG, message);
1484 return false;
1485 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001486 if (options != null && options.getLaunchTaskId() != -1) {
1487 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1488 callingPid, callingUid);
1489 if (startInTaskPerm != PERMISSION_GRANTED) {
1490 final String msg = "Permission Denial: starting " + intent.toString()
1491 + " from " + callerApp + " (pid=" + callingPid
1492 + ", uid=" + callingUid + ") with launchTaskId="
1493 + options.getLaunchTaskId();
1494 Slog.w(TAG, msg);
1495 throw new SecurityException(msg);
1496 }
1497 }
1498
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001499 return true;
1500 }
1501
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001502 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001503 final long identity = Binder.clearCallingIdentity();
1504 try {
1505 return UserManager.get(mService.mContext).getUserInfo(userId);
1506 } finally {
1507 Binder.restoreCallingIdentity(identity);
1508 }
1509 }
1510
Svet Ganov99b60432015-06-27 13:15:22 -07001511 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001512 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001513 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1514 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001515 == PackageManager.PERMISSION_DENIED) {
1516 return ACTIVITY_RESTRICTION_PERMISSION;
1517 }
1518
Christopher Tateff7add02015-08-17 10:23:22 -07001519 if (activityInfo.permission == null) {
1520 return ACTIVITY_RESTRICTION_NONE;
1521 }
1522
Svet Ganov99b60432015-06-27 13:15:22 -07001523 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1524 if (opCode == AppOpsManager.OP_NONE) {
1525 return ACTIVITY_RESTRICTION_NONE;
1526 }
1527
1528 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1529 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001530 if (!ignoreTargetSecurity) {
1531 return ACTIVITY_RESTRICTION_APPOP;
1532 }
Svet Ganov99b60432015-06-27 13:15:22 -07001533 }
1534
1535 return ACTIVITY_RESTRICTION_NONE;
1536 }
1537
Svetoslav7008b512015-06-24 18:47:07 -07001538 private int getActionRestrictionForCallingPackage(String action,
1539 String callingPackage, int callingPid, int callingUid) {
1540 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001541 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001542 }
1543
1544 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1545 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001546 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001547 }
1548
1549 final PackageInfo packageInfo;
1550 try {
1551 packageInfo = mService.mContext.getPackageManager()
1552 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1553 } catch (PackageManager.NameNotFoundException e) {
1554 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001555 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001556 }
1557
1558 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001559 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001560 }
1561
1562 if (mService.checkPermission(permission, callingPid, callingUid) ==
1563 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001564 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001565 }
1566
1567 final int opCode = AppOpsManager.permissionToOpCode(permission);
1568 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001569 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001570 }
1571
1572 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1573 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001574 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001575 }
1576
Svet Ganov99b60432015-06-27 13:15:22 -07001577 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001578 }
1579
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001580 void setLaunchSource(int uid) {
1581 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1582 }
1583
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001584 void acquireLaunchWakelock() {
1585 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1586 throw new IllegalStateException("Calling must be system uid");
1587 }
1588 mLaunchingActivity.acquire();
1589 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1590 // To be safe, don't allow the wake lock to be held for too long.
1591 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1592 }
1593 }
1594
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001595 /**
1596 * Called when the frontmost task is idle.
1597 * @return the state of mService.mBooting before this was called.
1598 */
1599 private boolean checkFinishBootingLocked() {
1600 final boolean booting = mService.mBooting;
1601 boolean enableScreen = false;
1602 mService.mBooting = false;
1603 if (!mService.mBooted) {
1604 mService.mBooted = true;
1605 enableScreen = true;
1606 }
1607 if (booting || enableScreen) {
1608 mService.postFinishBooting(booting, enableScreen);
1609 }
1610 return booting;
1611 }
1612
Craig Mautnerf3333272013-04-22 10:55:53 -07001613 // Checked.
1614 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1615 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001616 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001617
Craig Mautnerf3333272013-04-22 10:55:53 -07001618 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001619 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 int NS = 0;
1621 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001622 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001623 boolean activityRemoved = false;
1624
Wale Ogunwale7d701172015-03-11 15:36:30 -07001625 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001626 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001627 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1628 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001629 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1630 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001631 if (fromTimeout) {
1632 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001633 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001634
1635 // This is a hack to semi-deal with a race condition
1636 // in the client where it can be constructed with a
1637 // newer configuration from when we asked it to launch.
1638 // We'll update with whatever configuration it now says
1639 // it used to launch.
1640 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001641 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001642 }
1643
1644 // We are now idle. If someone is waiting for a thumbnail from
1645 // us, we can now deliver.
1646 r.idle = true;
1647
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001648 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001649 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001650 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001651 }
1652 }
1653
1654 if (allResumedActivitiesIdle()) {
1655 if (r != null) {
1656 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001657 }
1658
1659 if (mLaunchingActivity.isHeld()) {
1660 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1661 if (VALIDATE_WAKE_LOCK_CALLER &&
1662 Binder.getCallingUid() != Process.myUid()) {
1663 throw new IllegalStateException("Calling must be system uid");
1664 }
1665 mLaunchingActivity.release();
1666 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001667 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001668 }
1669
1670 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001671 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001672 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001673 if ((NF = mFinishingActivities.size()) > 0) {
1674 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 mFinishingActivities.clear();
1676 }
1677
Craig Mautnerf3333272013-04-22 10:55:53 -07001678 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001679 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001680 mStartingUsers.clear();
1681 }
1682
Craig Mautnerf3333272013-04-22 10:55:53 -07001683 // Stop any activities that are scheduled to do so but have been
1684 // waiting for the next one to start.
1685 for (int i = 0; i < NS; i++) {
1686 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001687 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001688 if (stack != null) {
1689 if (r.finishing) {
1690 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1691 } else {
1692 stack.stopActivityLocked(r);
1693 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001694 }
1695 }
1696
1697 // Finish any activities that are scheduled to do so but have been
1698 // waiting for the next one to start.
1699 for (int i = 0; i < NF; i++) {
1700 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001701 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001702 if (stack != null) {
1703 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1704 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001705 }
1706
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001707 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001708 // Complete user switch
1709 if (startingUsers != null) {
1710 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001711 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001712 }
1713 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001714 }
1715
1716 mService.trimApplications();
1717 //dump();
1718 //mWindowManager.dump();
1719
Craig Mautnerf3333272013-04-22 10:55:53 -07001720 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001721 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 }
1723
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001724 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001725 }
1726
Craig Mautner8e569572013-10-11 17:36:59 -07001727 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001728 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1732 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1733 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001734 }
Craig Mautner19091252013-10-05 00:03:53 -07001735 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001736 }
1737
1738 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001739 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1740 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001741 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1742 stacks.get(stackNdx).closeSystemDialogsLocked();
1743 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001744 }
1745 }
1746
Craig Mautner93529a42013-10-04 15:03:13 -07001747 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001748 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001749 }
1750
Craig Mautner8d341ef2013-03-26 09:03:27 -07001751 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001752 * Update the last used stack id for non-current user (current user's last
1753 * used stack is the focused stack)
1754 */
1755 void updateUserStackLocked(int userId, ActivityStack stack) {
1756 if (userId != mCurrentUser) {
1757 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1758 }
1759 }
1760
1761 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001762 * @return true if some activity was finished (or would have finished if doit were true).
1763 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001764 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1765 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001766 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001770 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001771 if (stack.finishDisabledPackageActivitiesLocked(
1772 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001773 didSomething = true;
1774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001775 }
1776 }
1777 return didSomething;
1778 }
1779
Dianne Hackborna413dc02013-07-12 12:02:55 -07001780 void updatePreviousProcessLocked(ActivityRecord r) {
1781 // Now that this process has stopped, we may want to consider
1782 // it to be the previous app to try to keep around in case
1783 // the user wants to return to it.
1784
1785 // First, found out what is currently the foreground app, so that
1786 // we don't blow away the previous app if this activity is being
1787 // hosted by the process that is actually still the foreground.
1788 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001789 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1790 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001791 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1792 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001793 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001794 if (stack.mResumedActivity != null) {
1795 fgApp = stack.mResumedActivity.app;
1796 } else if (stack.mPausingActivity != null) {
1797 fgApp = stack.mPausingActivity.app;
1798 }
1799 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001800 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001801 }
1802 }
1803
1804 // Now set this one as the previous process, only if that really
1805 // makes sense to.
1806 if (r.app != null && fgApp != null && r.app != fgApp
1807 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001808 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001809 mService.mPreviousProcess = r.app;
1810 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1811 }
1812 }
1813
Wale Ogunwaled046a012015-12-24 13:05:59 -08001814 boolean resumeFocusedStackTopActivityLocked() {
1815 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001816 }
1817
Wale Ogunwaled046a012015-12-24 13:05:59 -08001818 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001819 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001820 if (targetStack != null && isFocusedStack(targetStack)) {
1821 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001822 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001823 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1824 if (r == null || r.state != RESUMED) {
1825 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1826 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001827 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001828 }
1829
Todd Kennedy39bfee52016-02-24 10:28:21 -08001830 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1832 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1834 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1835 }
1836 }
1837 }
1838
Adrian Roos20d7df32016-01-12 18:59:43 +01001839 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1840 TaskRecord finishedTask = null;
1841 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1843 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001844 final int numStacks = stacks.size();
1845 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1846 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001847 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1848 if (stack == focusedStack || finishedTask == null) {
1849 finishedTask = t;
1850 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001851 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001852 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001853 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001854 }
1855
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001856 void finishVoiceTask(IVoiceInteractionSession session) {
1857 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1858 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1859 final int numStacks = stacks.size();
1860 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1861 final ActivityStack stack = stacks.get(stackNdx);
1862 stack.finishVoiceTask(session);
1863 }
1864 }
1865 }
1866
Andrii Kulianc27916642016-04-12 17:59:27 -07001867 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1868 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001869 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1870 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001871 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001872 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1873 // Caller wants the home activity moved with it. To accomplish this,
1874 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001875 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001876 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001877 ActivityStack currentStack = task.getStack();
1878 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001879 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1880 + task + " to front. Stack is null");
1881 return;
1882 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001883
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001884 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001885 int stackId = options.getLaunchStackId();
1886 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001887 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001888 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001889 if (stackId == INVALID_STACK_ID) {
1890 stackId = task.getLaunchStackId();
1891 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001892 if (stackId != currentStack.mStackId) {
1893 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1894 !FORCE_FOCUS, reason);
1895 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001896 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1897 // still need moveTaskToFrontLocked() below for any transition settings.
1898 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001899 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1900 resizeStackLocked(stackId, bounds,
1901 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001902 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001903 } else {
1904 // WM resizeTask must be done after the task is moved to the correct stack,
1905 // because Task's setBounds() also updates dim layer's bounds, but that has
1906 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001907 mWindowManager.resizeTask(task.taskId, task.mBounds,
1908 task.getOverrideConfiguration(), false /* relayout */,
1909 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001910 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001911 }
1912 }
1913
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001914 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001915 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001916 r == null ? null : r.appTimeTracker, reason);
1917
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001918 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001919 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001920
Andrii Kulian02b7a832016-10-06 23:11:56 -07001921 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001922 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001923 }
1924
Wale Ogunwale854809c2015-12-27 16:18:19 -08001925 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001926 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001927 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001928 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001929 return false;
1930 }
1931 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1932 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001933 }
1934
Craig Mautner967212c2013-04-13 21:10:58 -07001935 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001936 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001937 }
1938
1939 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001940 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1941 if (activityContainer != null) {
1942 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001943 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001944 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001945 return null;
1946 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001947 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001948 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001949 }
1950
Craig Mautner967212c2013-04-13 21:10:58 -07001951 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001952 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1954 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 }
1956 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001957 }
1958
Jorim Jaggife762342016-10-13 14:33:27 +02001959 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
1960 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
1961 }
1962
Craig Mautneree2e45a2014-06-27 12:10:03 -07001963 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001964 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001965 }
1966
1967 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001968 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1969 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1970 final TaskRecord task = tasks.get(taskNdx);
1971 if (task.isHomeTask()) {
1972 final ArrayList<ActivityRecord> activities = task.mActivities;
1973 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1974 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001975 if (r.isHomeActivity()
1976 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001977 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001978 }
1979 }
1980 }
1981 }
1982 return null;
1983 }
1984
Chong Zhangb15758a2015-11-17 12:12:03 -08001985 /**
1986 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1987 * the docked stack itself, or if it's side-by-side to the docked stack.
1988 */
1989 boolean isStackDockedInEffect(int stackId) {
1990 return stackId == DOCKED_STACK_ID ||
1991 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1992 }
1993
Todd Kennedyca4d8422015-01-15 15:19:22 -08001994 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001995 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001996 ActivityContainer activityContainer =
1997 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001998 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001999 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2000 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002001 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002002 return activityContainer;
2003 }
2004
Craig Mautner34b73df2014-01-12 21:11:08 -08002005 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002006 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2007 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2008 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002009 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2010 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002011 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002012 }
2013 }
2014
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002015 void deleteActivityContainerRecord(int stackId) {
2016 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2017 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2018 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002019 }
2020
Jorim Jaggidc249c42015-12-15 14:57:31 -08002021 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002022 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002023 if (stackId == DOCKED_STACK_ID) {
2024 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002025 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002026 return;
2027 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002028 final ActivityStack stack = getStack(stackId);
2029 if (stack == null) {
2030 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2031 return;
2032 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002033
Jorim Jaggidc249c42015-12-15 14:57:31 -08002034 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002035 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2036 // stack size changing so things don't get out of sync.
2037 return;
2038 }
2039
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002040 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002041 mWindowManager.deferSurfaceLayout();
2042 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002043 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002044 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002045 stack.ensureVisibleActivitiesConfigurationLocked(
2046 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002047 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002048 } finally {
2049 mWindowManager.continueSurfaceLayout();
2050 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002051 }
2052 }
2053
Jorim Jaggi192086e2016-03-11 17:17:03 +01002054 void deferUpdateBounds(int stackId) {
2055 final ActivityStack stack = getStack(stackId);
2056 if (stack != null) {
2057 stack.deferUpdateBounds();
2058 }
2059 }
2060
2061 void continueUpdateBounds(int stackId) {
2062 final ActivityStack stack = getStack(stackId);
2063 if (stack != null) {
2064 stack.continueUpdateBounds();
2065 }
2066 }
2067
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002068 void notifyAppTransitionDone() {
2069 continueUpdateBounds(HOME_STACK_ID);
2070 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2071 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002072 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002073 mWindowManager.setTaskDockedResizing(taskId, false);
2074 }
2075 }
2076 mResizingTasksDuringAnimation.clear();
2077 }
2078
Jorim Jaggi192086e2016-03-11 17:17:03 +01002079 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002080 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002081 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002082
Jorim Jaggi192086e2016-03-11 17:17:03 +01002083 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2084 return;
2085 }
2086
Andrii Kulian1e32e022016-09-16 15:29:34 -07002087 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002088 }
2089
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002090 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2091 final ActivityStack stack = getStack(fromStackId);
2092 if (stack == null) {
2093 return;
2094 }
2095
2096 mWindowManager.deferSurfaceLayout();
2097 try {
2098 if (fromStackId == DOCKED_STACK_ID) {
2099
2100 // We are moving all tasks from the docked stack to the fullscreen stack,
2101 // which is dismissing the docked stack, so resize all other stacks to
2102 // fullscreen here already so we don't end up with resize trashing.
2103 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2104 if (StackId.isResizeableByDockedStack(i)) {
2105 ActivityStack otherStack = getStack(i);
2106 if (otherStack != null) {
2107 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2108 true /* allowResizeInDockedMode */, DEFER_RESUME);
2109 }
2110 }
2111 }
2112
2113 // Also disable docked stack resizing since we have manually adjusted the
2114 // size of other stacks above and we don't want to trigger a docked stack
2115 // resize when we remove task from it below and it is detached from the
2116 // display because it no longer contains any tasks.
2117 mAllowDockedStackResize = false;
2118 }
2119 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2120 final int size = tasks.size();
2121 if (onTop) {
2122 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002123 final TaskRecord task = tasks.get(i);
2124 if (fromStackId == PINNED_STACK_ID) {
2125 // Update the return-to to reflect where the pinned stack task was moved
2126 // from so that we retain the stack that was previously visible if the
2127 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2128 task.setTaskToReturnTo(getFocusedStack().getStackId() == HOME_STACK_ID
2129 ? HOME_ACTIVITY_TYPE : APPLICATION_ACTIVITY_TYPE);
2130 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002131 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002132 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002133 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002134 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002135
2136 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2137 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002138 } else {
2139 for (int i = size - 1; i >= 0; i--) {
2140 positionTaskInStackLocked(tasks.get(i).taskId,
2141 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2142 }
2143 }
2144 } finally {
2145 mAllowDockedStackResize = true;
2146 mWindowManager.continueSurfaceLayout();
2147 }
2148 }
2149
Jorim Jaggidc249c42015-12-15 14:57:31 -08002150 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002151 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2152 boolean preserveWindows) {
2153 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2154 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2155 false /* deferResume */);
2156 }
2157
2158 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2159 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2160 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002161
2162 if (!mAllowDockedStackResize) {
2163 // Docked stack resize currently disabled.
2164 return;
2165 }
2166
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2168 if (stack == null) {
2169 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2170 return;
2171 }
2172
2173 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2174 mWindowManager.deferSurfaceLayout();
2175 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002176 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2177 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 ActivityRecord r = stack.topRunningActivityLocked();
2179 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2180 tempDockedTaskInsetBounds);
2181
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002182 // TODO: Checking for isAttached might not be needed as if the user passes in null
2183 // dockedBounds then they want the docked stack to be dismissed.
2184 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2185 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186 // In this case we make all other static stacks fullscreen and move all
2187 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002188 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002189
2190 // stack shouldn't contain anymore activities, so nothing to resume.
2191 r = null;
2192 } else {
2193 // Docked stacks occupy a dedicated region on screen so the size of all other
2194 // static stacks need to be adjusted so they don't overlap with the docked stack.
2195 // We get the bounds to use from window manager which has been adjusted for any
2196 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002197 mWindowManager.getStackDockedModeBounds(
2198 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002199 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002200 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2201 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2202 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002203 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002204 }
2205 }
2206 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002207 if (!deferResume) {
2208 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2209 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002210 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002211 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002212 mWindowManager.continueSurfaceLayout();
2213 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2214 }
2215
2216 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2217 tempDockedTaskBounds != null
2218 || tempDockedTaskInsetBounds != null
2219 || tempOtherTaskBounds != null
2220 || tempOtherTaskInsetBounds != null);
2221 }
2222
Robert Carr0d00c2e2016-02-29 17:45:02 -08002223 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2224 final ActivityStack stack = getStack(PINNED_STACK_ID);
2225 if (stack == null) {
2226 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2227 return;
2228 }
2229 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2230 mWindowManager.deferSurfaceLayout();
2231 try {
2232 ActivityRecord r = stack.topRunningActivityLocked();
2233 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2234 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002235 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002236 } finally {
2237 mWindowManager.continueSurfaceLayout();
2238 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2239 }
2240 }
2241
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002242 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2243 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002244 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002245 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 }
2248
Chong Zhang87b21722015-09-21 15:39:51 -07002249 // If this is a forced resize, let it go through even if the bounds is not changing,
2250 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002251 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002252 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002253 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002254 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002255 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002256 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002257
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002258 if (!mWindowManager.isValidTaskId(task.taskId)) {
2259 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002260 // All we can do for now is update the bounds so it can be used when the task is
2261 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002262 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002263 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002264 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002265 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002266 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002267 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002268 }
2269
Chong Zhang6de2ae82015-09-30 18:25:21 -07002270 // Do not move the task to another stack here.
2271 // This method assumes that the task is already placed in the right stack.
2272 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002273
Chong Zhang6de2ae82015-09-30 18:25:21 -07002274 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002275
Andrii Kulian8072d112016-09-16 11:11:01 -07002276 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002277 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002278 // way and the activity was kept the way it was. If it's false, it means the activity had
2279 // to be relaunched due to configuration change.
2280 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002281 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002282 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002283 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002284 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002285
2286 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002287 // All other activities must be made visible with their correct configuration.
2288 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2289 if (!kept) {
2290 resumeFocusedStackTopActivityLocked();
2291 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002292 }
2293 }
2294 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002295 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2296 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002297
2298 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002299 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002300 }
2301
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002302 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002303 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2304 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002305 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002306 }
2307
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002308 ActivityContainer activityContainer = new ActivityContainer(stackId);
2309 mActivityContainers.put(stackId, activityContainer);
Andrii Kulian839def92016-11-02 10:58:58 -07002310 activityContainer.addToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002311 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002312 }
2313
2314 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002315 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002316 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2317 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002318 break;
2319 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002320 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002322 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002323 }
2324
Chong Zhang5dcb2752015-08-18 13:50:26 -07002325 /**
2326 * Restores a recent task to a stack
2327 * @param task The recent task to be restored.
2328 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002329 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002330 * @return true if the task has been restored successfully.
2331 */
2332 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2333 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002334 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002335 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2336 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2337 // Put it in the fullscreen stack.
2338 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002339 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002340
Andrii Kulian02b7a832016-10-06 23:11:56 -07002341 final ActivityStack currentStack = task.getStack();
2342 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002343 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002344 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002345 // Nothing else to do since it is already restored in the right stack.
2346 return true;
2347 }
2348 // Remove current stack association, so we can re-associate the task with the
2349 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002350 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002351 }
2352
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002353 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002354 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002355
2356 if (stack == null) {
2357 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2359 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002360 return false;
2361 }
2362
Wale Ogunwale5f986092015-12-04 15:35:38 -08002363 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002364 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2365 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002366 final ArrayList<ActivityRecord> activities = task.mActivities;
2367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002368 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002369 }
2370 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002371 }
2372
Wale Ogunwale040b4702015-08-06 18:10:50 -07002373 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002374 * Move stack with all its existing content to specified display.
2375 * @param stackId Id of stack to move.
2376 * @param displayId Id of display to move stack to.
2377 */
2378 void moveStackToDisplayLocked(int stackId, int displayId) {
2379 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2380 if (activityDisplay == null) {
2381 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2382 + displayId);
2383 }
2384 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2385 if (activityContainer != null) {
2386 if (activityContainer.isAttachedLocked()) {
2387 if (activityContainer.getDisplayId() == displayId) {
2388 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2389 + " to its current displayId=" + displayId);
2390 }
2391
2392 activityContainer.moveToDisplayLocked(activityDisplay);
2393 } else {
2394 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2395 + stackId + " is not attached to any display.");
2396 }
2397 } else {
2398 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2399 + stackId);
2400 }
2401
2402 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2403 !PRESERVE_WINDOWS);
2404 // TODO(multi-display): resize stacks properly if moved from split-screen.
2405 }
2406
2407 /**
Wale Ogunwale040b4702015-08-06 18:10:50 -07002408 * Moves the specified task record to the input stack id.
2409 * WARNING: This method performs an unchecked/raw move of the task and
2410 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002411 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002412 * @param task Task to move.
2413 * @param stackId Id of stack to move task to.
2414 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002415 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002416 * @param reason Reason the task is been moved.
2417 * @return The stack the task was moved to.
2418 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002419 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002420 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002421
2422 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2423 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2424 + "support multi-window task=" + task + " to stackId=" + stackId);
2425 }
2426
Wale Ogunwale06579d62016-04-30 15:29:06 -07002427 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002428 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002429 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002430 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002431 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2432 // Whenever we are moving the top activity from the front stack we want to make sure to move
2433 // the stack to the front.
2434 final boolean wasFront = isFrontStack(prevStack)
2435 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002436
Chong Zhangd545dce2016-02-29 18:09:17 -08002437 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002438 // We don't allow moving a unresizeable task to the docked stack since the docked
2439 // stack is used for split-screen mode and will cause things like the docked divider to
2440 // show up. We instead leave the task in its current stack or move it to the fullscreen
2441 // stack if it isn't currently in a stack.
2442 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002443 Slog.w(TAG, "Can not move unresizeable task=" + task
2444 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2445 }
2446
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002447 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2448 // if a docked stack is created below which will lead to the stack we are moving from and
2449 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002450 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002451 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002452 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002453 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002454 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002455
2456 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002457 // move focus to the new stack by moving the stack to the front.
2458 stack.moveToFrontAndResumeStateIfNeeded(
2459 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002460
Wale Ogunwale040b4702015-08-06 18:10:50 -07002461 return stack;
2462 }
2463
Chong Zhange4fbd322016-03-01 14:44:03 -08002464 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002465 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002466 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2467 false /* deferResume */);
2468 }
2469
2470 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2471 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002472 final TaskRecord task = anyTaskForIdLocked(taskId);
2473 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002474 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002475 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002476 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002477
Andrii Kulian02b7a832016-10-06 23:11:56 -07002478 final ActivityStack currentStack = task.getStack();
2479 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002480 // You are already in the right stack silly...
2481 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002482 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002483 }
2484
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002485 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2486 throw new IllegalArgumentException("moveTaskToStack:"
2487 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2488 }
2489
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002490 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002491 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002492 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002493 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002494 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002495 // We are about to relaunch the activity because its configuration changed due to
2496 // being maximized, i.e. size change. The activity will first remove the old window
2497 // and then add a new one. This call will tell window manager about this, so it can
2498 // preserve the old window until the new one is drawn. This prevents having a gap
2499 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002500 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002501 // Note here we always set the replacing window first, as the flags might be needed
2502 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002503 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002504 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002505
2506 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002507 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002508 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002509 try {
2510 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002511 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002512 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002513
Wale Ogunwale961f4852016-02-01 20:25:54 -08002514 if (!animate) {
2515 stack.mNoAnimActivities.add(topActivity);
2516 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002517
Wale Ogunwale961f4852016-02-01 20:25:54 -08002518 // We might trigger a configuration change. Save the current task bounds for freezing.
2519 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2520
2521 // Make sure the task has the appropriate bounds/size for the stack it is in.
2522 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002523 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2524 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002525 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2526 Rect bounds = task.getLaunchBounds();
2527 if (bounds == null) {
2528 stack.layoutTaskInStack(task, null);
2529 bounds = task.mBounds;
2530 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002531 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2532 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002533 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002534 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2535 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002536 }
2537 } finally {
2538 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002539 }
2540
2541 if (mightReplaceWindow) {
2542 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2543 // window), we need to clear the replace window settings. Otherwise, we schedule a
2544 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002545 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002546 }
2547
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002548 if (!deferResume) {
2549
2550 // The task might have already been running and its visibility needs to be synchronized with
2551 // the visibility of the stack / windows.
2552 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2553 resumeFocusedStackTopActivityLocked();
2554 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002555
Jorim Jaggid53f0922016-04-06 22:16:23 -07002556 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002557
2558 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002559 }
2560
Wale Ogunwale079a0042015-10-24 11:44:07 -07002561 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2562 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2563 if (stack == null) {
2564 throw new IllegalArgumentException(
2565 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2566 }
2567
2568 final ActivityRecord r = stack.topRunningActivityLocked();
2569 if (r == null) {
2570 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2571 + " in stack=" + stack);
2572 return false;
2573 }
2574
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002575 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002576 Slog.w(TAG,
2577 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002578 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002579 return false;
2580 }
2581
Wale Ogunwale480dca02016-02-06 13:58:29 -08002582 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002583 return true;
2584 }
2585
Wale Ogunwale480dca02016-02-06 13:58:29 -08002586 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2587 mWindowManager.deferSurfaceLayout();
2588 try {
2589 final TaskRecord task = r.task;
2590
Andrii Kulian02b7a832016-10-06 23:11:56 -07002591 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002592 // An activity can't be pinned and visible behind at the same time. Go ahead and
2593 // release it from been visible behind before pinning.
2594 requestVisibleBehindLocked(r, false);
2595 }
2596
Wale Ogunwale480dca02016-02-06 13:58:29 -08002597 // Need to make sure the pinned stack exist so we can resize it below...
2598 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2599
2600 // Resize the pinned stack to match the current size of the task the activity we are
2601 // going to be moving is currently contained in. We do this to have the right starting
2602 // animation bounds for the pinned stack to the desired bounds the caller wants.
2603 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2604 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002605 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002606
2607 if (task.mActivities.size() == 1) {
2608 // There is only one activity in the task. So, we can just move the task over to
2609 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002610 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2611 // Move the home stack forward if the task we just moved to the pinned stack
2612 // was launched from home so home should be visible behind it.
2613 moveHomeStackToFront(reason);
2614 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002615 moveTaskToStackLocked(
2616 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2617 } else {
2618 stack.moveActivityToStack(r);
2619 }
2620 } finally {
2621 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002622 }
2623
Wale Ogunwale480dca02016-02-06 13:58:29 -08002624 // The task might have already been running and its visibility needs to be synchronized
2625 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002626 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002627 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002628
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002629 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002630 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002631 }
2632
Chong Zhang6cda19c2016-06-14 19:07:56 -07002633 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2634 if (r == null || !r.isFocusable()) {
2635 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2636 "moveActivityStackToFront: unfocusable r=" + r);
2637 return false;
2638 }
2639
2640 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002641 final ActivityStack stack = r.getStack();
2642 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002643 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2644 + r + " task=" + task);
2645 return false;
2646 }
2647
Chong Zhang6cda19c2016-06-14 19:07:56 -07002648 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2649 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2650 "moveActivityStackToFront: already on top, r=" + r);
2651 return false;
2652 }
2653
2654 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2655 "moveActivityStackToFront: r=" + r);
2656
2657 stack.moveToFront(reason, task);
2658 return true;
2659 }
2660
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002661 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2662 final TaskRecord task = anyTaskForIdLocked(taskId);
2663 if (task == null) {
2664 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2665 return;
2666 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002667 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2668
2669 task.updateOverrideConfigurationForStack(stack);
2670
2671 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002672 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002673 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002674 // The task might have already been running and its visibility needs to be synchronized with
2675 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002676 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002677 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002678 }
2679
Craig Mautnerac6f8432013-07-17 13:24:59 -07002680 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002681 mTmpFindTaskResult.r = null;
2682 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002683 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2685 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2687 final ActivityStack stack = stacks.get(stackNdx);
2688 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002689 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002690 continue;
2691 }
2692 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002693 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2694 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002695 continue;
2696 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002697 stack.findTaskLocked(r, mTmpFindTaskResult);
2698 // It is possible to have task in multiple stacks with the same root affinity.
2699 // If the match we found was based on root affinity we keep on looking to see if
2700 // there is a better match in another stack. We eventually return the match based
2701 // on root affinity if we don't find a better match.
2702 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2703 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002705 }
2706 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002707 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2708 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002709 }
2710
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002711 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2712 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2714 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002716 final ActivityRecord ar = stacks.get(stackNdx)
2717 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002718 if (ar != null) {
2719 return ar;
2720 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002721 }
2722 }
2723 return null;
2724 }
2725
Craig Mautner8d341ef2013-03-26 09:03:27 -07002726 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002727 scheduleSleepTimeout();
2728 if (!mGoingToSleep.isHeld()) {
2729 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002730 if (mLaunchingActivity.isHeld()) {
2731 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2732 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002733 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002734 mLaunchingActivity.release();
2735 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002738 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002739 }
2740
2741 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002742 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002743
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002744 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002745 final long endTime = System.currentTimeMillis() + timeout;
2746 while (true) {
2747 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2751 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2752 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002753 }
2754 if (cantShutdown) {
2755 long timeRemaining = endTime - System.currentTimeMillis();
2756 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002757 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002758 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 } catch (InterruptedException e) {
2760 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002761 } else {
2762 Slog.w(TAG, "Activity manager shutdown timed out");
2763 timedout = true;
2764 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002765 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002766 } else {
2767 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002768 }
2769 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002770
2771 // Force checkReadyForSleep to complete.
2772 mSleepTimeout = true;
2773 checkReadyForSleepLocked();
2774
Craig Mautner8d341ef2013-03-26 09:03:27 -07002775 return timedout;
2776 }
2777
2778 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002779 removeSleepTimeouts();
2780 if (mGoingToSleep.isHeld()) {
2781 mGoingToSleep.release();
2782 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002783 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2784 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002785 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2786 final ActivityStack stack = stacks.get(stackNdx);
2787 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002788 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002789 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002790 }
Craig Mautner5314a402013-09-26 12:40:16 -07002791 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002793 mGoingToSleepActivities.clear();
2794 }
2795
2796 void activitySleptLocked(ActivityRecord r) {
2797 mGoingToSleepActivities.remove(r);
2798 checkReadyForSleepLocked();
2799 }
2800
2801 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002802 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002803 // Do not care.
2804 return;
2805 }
2806
2807 if (!mSleepTimeout) {
2808 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002809 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2810 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002811 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2812 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2813 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002814 }
2815
2816 if (mStoppingActivities.size() > 0) {
2817 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002818 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002819 + mStoppingActivities.size() + " activities");
2820 scheduleIdleLocked();
2821 dontSleep = true;
2822 }
2823
2824 if (mGoingToSleepActivities.size() > 0) {
2825 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002826 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002827 + mGoingToSleepActivities.size() + " activities");
2828 dontSleep = true;
2829 }
2830
2831 if (dontSleep) {
2832 return;
2833 }
2834 }
2835
Wei Wang65c7a152016-06-02 18:51:22 -07002836 // Send launch end powerhint before going sleep
2837 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2838
Craig Mautnere0a38842013-12-16 16:14:02 -08002839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2840 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2842 stacks.get(stackNdx).goToSleep();
2843 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002844 }
2845
2846 removeSleepTimeouts();
2847
2848 if (mGoingToSleep.isHeld()) {
2849 mGoingToSleep.release();
2850 }
2851 if (mService.mShuttingDown) {
2852 mService.notifyAll();
2853 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002854 }
2855
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002856 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002857 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002858 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002859 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002860 }
2861 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002862 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002863 mWindowManager.executeAppTransition();
2864 return true;
2865 }
2866 return false;
2867 }
2868
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2871 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002872 int stackNdx = stacks.size() - 1;
2873 while (stackNdx >= 0) {
2874 stacks.get(stackNdx).handleAppCrashLocked(app);
2875 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002876 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002877 }
2878 }
2879
Jose Lima4b6c6692014-08-12 17:41:12 -07002880 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002881 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002882 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002883 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2884 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002885 return false;
2886 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002887
2888 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2889 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2890 + " visible=" + visible + " stackId=" + stack.mStackId
2891 + " can't contain visible behind activities");
2892 return false;
2893 }
2894
Jose Lima4b6c6692014-08-12 17:41:12 -07002895 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002896 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2897 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002898
2899 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002900 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002901 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002902 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002903 return true;
2904 }
2905
2906 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002907 if (visible && top.fullscreen) {
2908 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002909 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2910 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2911 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2912 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002913 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002914 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2915 // Only the activity set as currently visible behind should actively reset its
2916 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002917 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2918 "requestVisibleBehind: returning visible=" + visible
2919 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2920 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002921 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002922 }
2923
Jose Lima4b6c6692014-08-12 17:41:12 -07002924 stack.setVisibleBehindActivity(visible ? r : null);
2925 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002926 // If there is a translucent home activity, we need to force it stop being translucent,
2927 // because we can't depend on the application to necessarily perform that operation.
2928 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002929 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002930 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002931 mService.convertFromTranslucent(next.appToken);
2932 }
2933 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002934 if (top.app != null && top.app.thread != null) {
2935 // Notify the top app of the change.
2936 try {
2937 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2938 } catch (RemoteException e) {
2939 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002940 }
2941 return true;
2942 }
2943
Craig Mautnerbb742462014-07-07 15:28:55 -07002944 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002945 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002946 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002947 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002948
2949 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002950 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002951 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002952 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Craig Mautnerbb742462014-07-07 15:28:55 -07002953 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002954
2955 // When launching tasks behind, update the last active time of the top task after the new
2956 // task has been shown briefly
2957 final ActivityRecord top = stack.topActivity();
2958 if (top != null) {
2959 top.task.touchActiveTime();
2960 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002961 }
2962
2963 void scheduleLaunchTaskBehindComplete(IBinder token) {
2964 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2965 }
2966
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002967 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2968 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02002969 mKeyguardController.beginActivityVisibilityUpdate();
2970 try {
2971 // First the front stacks. In case any are not fullscreen and are in front of home.
2972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2973 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2974 final int topStackNdx = stacks.size() - 1;
2975 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2976 final ActivityStack stack = stacks.get(stackNdx);
2977 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
2978 }
Craig Mautner580ea812013-04-25 12:58:38 -07002979 }
Jorim Jaggife762342016-10-13 14:33:27 +02002980 } finally {
2981 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002983 }
2984
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002985 void invalidateTaskLayers() {
2986 mTaskLayersChanged = true;
2987 }
2988
2989 void rankTaskLayersIfNeeded() {
2990 if (!mTaskLayersChanged) {
2991 return;
2992 }
2993 mTaskLayersChanged = false;
2994 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2995 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2996 int baseLayer = 0;
2997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2998 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2999 }
3000 }
3001 }
3002
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003003 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3005 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3006 final int topStackNdx = stacks.size() - 1;
3007 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3008 final ActivityStack stack = stacks.get(stackNdx);
3009 stack.clearOtherAppTimeTrackers(except);
3010 }
3011 }
3012 }
3013
Craig Mautner8d341ef2013-03-26 09:03:27 -07003014 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003015 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3016 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003017 final int numStacks = stacks.size();
3018 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3019 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003020 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003021 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003022 }
3023 }
3024
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003025 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3026 // Examine all activities currently running in the process.
3027 TaskRecord firstTask = null;
3028 // Tasks is non-null only if two or more tasks are found.
3029 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003030 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3031 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003032 ActivityRecord r = app.activities.get(i);
3033 // First, if we find an activity that is in the process of being destroyed,
3034 // then we just aren't going to do anything for now; we want things to settle
3035 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003036 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003037 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003038 return;
3039 }
3040 // Don't consider any activies that are currently not in a state where they
3041 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003042 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3043 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003044 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003045 continue;
3046 }
3047 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003048 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003049 + " from " + r);
3050 if (firstTask == null) {
3051 firstTask = r.task;
3052 } else if (firstTask != r.task) {
3053 if (tasks == null) {
3054 tasks = new ArraySet<>();
3055 tasks.add(firstTask);
3056 }
3057 tasks.add(r.task);
3058 }
3059 }
3060 }
3061 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003062 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003063 return;
3064 }
3065 // If we have activities in multiple tasks that are in a position to be destroyed,
3066 // let's iterate through the tasks and release the oldest one.
3067 final int numDisplays = mActivityDisplays.size();
3068 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3069 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3070 // Step through all stacks starting from behind, to hit the oldest things first.
3071 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3072 final ActivityStack stack = stacks.get(stackNdx);
3073 // Try to release activities in this stack; if we manage to, we are done.
3074 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3075 return;
3076 }
3077 }
3078 }
3079 }
3080
Amith Yamasani37a40c22015-06-17 13:25:42 -07003081 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003082 final int focusStackId = mFocusedStack.getStackId();
3083 // We dismiss the docked stack whenever we switch users.
3084 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3085
3086 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003087 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003088 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003089
Craig Mautner858d8a62013-04-23 17:08:34 -07003090 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003091 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3092 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003094 final ActivityStack stack = stacks.get(stackNdx);
3095 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003096 TaskRecord task = stack.topTask();
3097 if (task != null) {
3098 mWindowManager.moveTaskToTop(task.taskId);
3099 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003101 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003102
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003103 ActivityStack stack = getStack(restoreStackId);
3104 if (stack == null) {
3105 stack = mHomeStack;
3106 }
3107 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003108 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003109 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003110 } else {
3111 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003112 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 }
Craig Mautner93529a42013-10-04 15:03:13 -07003114 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003115 }
3116
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003117 /** Checks whether the userid is a profile of the current user. */
3118 boolean isCurrentProfileLocked(int userId) {
3119 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003120 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003121 }
3122
Craig Mautnerde4ef022013-04-07 19:01:33 -07003123 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003124 ArrayList<ActivityRecord> stops = null;
3125
3126 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003127 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3128 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003129 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3130 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003131 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003132 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003133 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3134 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003135 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003136 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003137 if (s.finishing) {
3138 // If this activity is finishing, it is sitting on top of
3139 // everyone else but we now know it is no longer needed...
3140 // so get rid of it. Otherwise, we need to go through the
3141 // normal flow and hide it once we determine that it is
3142 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003143 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003144 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003145 }
3146 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003147 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003148 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003149 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003150 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003151 }
3152 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003153 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003154 }
3155 }
3156
3157 return stops;
3158 }
3159
Craig Mautnercf910b02013-04-23 11:23:27 -07003160 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003161 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3162 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3163 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3164 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003165 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003166 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003167 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003168 if (r == null) Slog.e(TAG,
3169 "validateTop...: null top activity, stack=" + stack);
3170 else {
3171 final ActivityRecord pausing = stack.mPausingActivity;
3172 if (pausing != null && pausing == r) Slog.e(TAG,
3173 "validateTop...: top stack has pausing activity r=" + r
3174 + " state=" + state);
3175 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3176 "validateTop...: activity in front not resumed r=" + r
3177 + " state=" + state);
3178 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003179 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003180 final ActivityRecord resumed = stack.mResumedActivity;
3181 if (resumed != null && resumed == r) Slog.e(TAG,
3182 "validateTop...: back stack has resumed activity r=" + r
3183 + " state=" + state);
3184 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3185 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003186 }
3187 }
3188 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003189 }
3190
Craig Mautnere0570202015-05-13 13:06:11 -07003191 private String lockTaskModeToString() {
3192 switch (mLockTaskModeState) {
3193 case LOCK_TASK_MODE_LOCKED:
3194 return "LOCKED";
3195 case LOCK_TASK_MODE_PINNED:
3196 return "PINNED";
3197 case LOCK_TASK_MODE_NONE:
3198 return "NONE";
3199 default: return "unknown=" + mLockTaskModeState;
3200 }
3201 }
3202
Craig Mautner27084302013-03-25 08:05:25 -07003203 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003204 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003205 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003206 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003207 pw.print(prefix);
3208 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003209 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003210 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003211 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003212 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3213 if (packages.size() > 0) {
3214 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3215 for (int i = 0; i < packages.size(); ++i) {
3216 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3217 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3218 }
3219 }
3220 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3221 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003222 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003223
Winson43d1f262016-06-14 16:05:55 -07003224 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003225 * Dump all connected displays' configurations.
3226 * @param prefix Prefix to apply to each line of the dump.
3227 */
3228 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3229 pw.print(prefix); pw.println("Display override configurations:");
3230 final int displayCount = mActivityDisplays.size();
3231 for (int i = 0; i < displayCount; i++) {
3232 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3233 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3234 pw.println(activityDisplay.getOverrideConfiguration());
3235 }
3236 }
3237
3238 /**
Winson43d1f262016-06-14 16:05:55 -07003239 * Dumps the activities matching the given {@param name} in the either the focused stack
3240 * or all visible stacks if {@param dumpVisibleStacks} is true.
3241 */
3242 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3243 if (dumpVisibleStacks) {
3244 ArrayList<ActivityRecord> activities = new ArrayList<>();
3245 int numDisplays = mActivityDisplays.size();
3246 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3247 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3248 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3249 ActivityStack stack = stacks.get(stackNdx);
3250 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3251 activities.addAll(stack.getDumpActivitiesLocked(name));
3252 }
3253 }
3254 }
3255 return activities;
3256 } else {
3257 return mFocusedStack.getDumpActivitiesLocked(name);
3258 }
Craig Mautner20e72272013-04-01 13:45:53 -07003259 }
3260
Dianne Hackborn390517b2013-05-30 15:03:32 -07003261 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3262 boolean needSep, String prefix) {
3263 if (activity != null) {
3264 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3265 if (needSep) {
3266 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003267 }
3268 pw.print(prefix);
3269 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003270 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003271 }
3272 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003273 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003274 }
3275
Craig Mautner8d341ef2013-03-26 09:03:27 -07003276 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3277 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003278 boolean printed = false;
3279 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003280 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3281 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003282 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003283 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003284 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003285 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 final ActivityStack stack = stacks.get(stackNdx);
3287 StringBuilder stackHeader = new StringBuilder(128);
3288 stackHeader.append(" Stack #");
3289 stackHeader.append(stack.mStackId);
3290 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003291 stackHeader.append("\n");
3292 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3293 stackHeader.append("\n");
3294 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003295 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3296 needSep, stackHeader.toString());
3297 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3298 !dumpAll, false, dumpPackage, true,
3299 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003300
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 needSep = printed;
3302 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3303 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003304 if (pr) {
3305 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003306 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003307 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003308 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3309 " mResumedActivity: ");
3310 if (pr) {
3311 printed = true;
3312 needSep = false;
3313 }
3314 if (dumpAll) {
3315 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3316 " mLastPausedActivity: ");
3317 if (pr) {
3318 printed = true;
3319 needSep = true;
3320 }
3321 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3322 needSep, " mLastNoHistoryActivity: ");
3323 }
3324 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003325 }
3326 }
3327
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003328 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3329 false, dumpPackage, true, " Activities waiting to finish:", null);
3330 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3331 false, dumpPackage, true, " Activities waiting to stop:", null);
3332 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3333 false, dumpPackage, true, " Activities waiting for another to become visible:",
3334 null);
3335 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3336 false, dumpPackage, true, " Activities waiting to sleep:", null);
3337 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3338 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003339
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003340 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 }
3342
Dianne Hackborn390517b2013-05-30 15:03:32 -07003343 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003345 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003347 String innerPrefix = null;
3348 String[] args = null;
3349 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003350 for (int i=list.size()-1; i>=0; i--) {
3351 final ActivityRecord r = list.get(i);
3352 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3353 continue;
3354 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003355 if (innerPrefix == null) {
3356 innerPrefix = prefix + " ";
3357 args = new String[0];
3358 }
3359 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 final boolean full = !brief && (complete || !r.isInHistory());
3361 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003362 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003363 needNL = false;
3364 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003365 if (header1 != null) {
3366 pw.println(header1);
3367 header1 = null;
3368 }
3369 if (header2 != null) {
3370 pw.println(header2);
3371 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003372 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003373 if (lastTask != r.task) {
3374 lastTask = r.task;
3375 pw.print(prefix);
3376 pw.print(full ? "* " : " ");
3377 pw.println(lastTask);
3378 if (full) {
3379 lastTask.dump(pw, prefix + " ");
3380 } else if (complete) {
3381 // Complete + brief == give a summary. Isn't that obvious?!?
3382 if (lastTask.intent != null) {
3383 pw.print(prefix); pw.print(" ");
3384 pw.println(lastTask.intent.toInsecureStringWithClip());
3385 }
3386 }
3387 }
3388 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3389 pw.print(" #"); pw.print(i); pw.print(": ");
3390 pw.println(r);
3391 if (full) {
3392 r.dump(pw, innerPrefix);
3393 } else if (complete) {
3394 // Complete + brief == give a summary. Isn't that obvious?!?
3395 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3396 if (r.app != null) {
3397 pw.print(innerPrefix); pw.println(r.app);
3398 }
3399 }
3400 if (client && r.app != null && r.app.thread != null) {
3401 // flush anything that is already in the PrintWriter since the thread is going
3402 // to write to the file descriptor directly
3403 pw.flush();
3404 try {
3405 TransferPipe tp = new TransferPipe();
3406 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003407 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003408 // Short timeout, since blocking here can
3409 // deadlock with the application.
3410 tp.go(fd, 2000);
3411 } finally {
3412 tp.kill();
3413 }
3414 } catch (IOException e) {
3415 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3416 } catch (RemoteException e) {
3417 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3418 }
3419 needNL = true;
3420 }
3421 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003422 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003423 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003424
Craig Mautnerf3333272013-04-22 10:55:53 -07003425 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003426 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3427 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003428 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3429 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003430 }
3431
3432 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003433 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003434 }
3435
3436 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003437 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3438 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003439 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3440 }
3441
Craig Mautner05d29032013-05-03 13:40:13 -07003442 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003443 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3444 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3445 }
Craig Mautner05d29032013-05-03 13:40:13 -07003446 }
3447
Craig Mautner0eea92c2013-05-16 13:35:39 -07003448 void removeSleepTimeouts() {
3449 mSleepTimeout = false;
3450 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3451 }
3452
3453 final void scheduleSleepTimeout() {
3454 removeSleepTimeouts();
3455 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3456 }
3457
Craig Mautner4a1cb222013-12-04 16:14:06 -08003458 @Override
3459 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003460 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3462 }
3463
3464 @Override
3465 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003466 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3468 }
3469
3470 @Override
3471 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003472 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3474 }
3475
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003476 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003477 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003479 newDisplay = mActivityDisplays.get(displayId) == null;
3480 if (newDisplay) {
3481 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003482 if (activityDisplay.mDisplay == null) {
3483 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3484 return;
3485 }
Craig Mautner4504de52013-12-20 09:06:56 -08003486 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003487 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003488 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 }
Craig Mautner4504de52013-12-20 09:06:56 -08003490 if (newDisplay) {
3491 mWindowManager.onDisplayAdded(displayId);
3492 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 }
3494
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003495 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003496 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003497 mService.mContext.getResources().getDimensionPixelSize(
3498 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003499 }
3500
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003501 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003502 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003503 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3504 if (activityDisplay != null) {
3505 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003506 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07003507 stacks.get(stackNdx).mActivityContainer.removeLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003509 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 }
3511 }
3512 mWindowManager.onDisplayRemoved(displayId);
3513 }
3514
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003515 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003516 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3518 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003519 // TODO: Update the bounds.
3520 }
3521 }
3522 mWindowManager.onDisplayChanged(displayId);
3523 }
3524
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003525 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003526 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 StackInfo info = new StackInfo();
3528 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003529 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003531 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003532 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003533 info.position = display != null
3534 ? display.mStacks.indexOf(stack)
3535 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536
3537 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3538 final int numTasks = tasks.size();
3539 int[] taskIds = new int[numTasks];
3540 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003541 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003542 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 for (int i = 0; i < numTasks; ++i) {
3544 final TaskRecord task = tasks.get(i);
3545 taskIds[i] = task.taskId;
3546 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3547 : task.realActivity != null ? task.realActivity.flattenToString()
3548 : task.getTopActivity() != null ? task.getTopActivity().packageName
3549 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003550 taskBounds[i] = new Rect();
3551 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003552 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 }
3554 info.taskIds = taskIds;
3555 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003556 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003557 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003558
3559 final ActivityRecord top = stack.topRunningActivityLocked();
3560 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003561 return info;
3562 }
3563
3564 StackInfo getStackInfoLocked(int stackId) {
3565 ActivityStack stack = getStack(stackId);
3566 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003567 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 }
3569 return null;
3570 }
3571
3572 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003573 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003574 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3575 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003576 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003577 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 }
3579 }
3580 return list;
3581 }
3582
Craig Mautner15df08a2015-04-01 12:17:18 -07003583 TaskRecord getLockedTaskLocked() {
3584 final int top = mLockTaskModeTasks.size() - 1;
3585 if (top >= 0) {
3586 return mLockTaskModeTasks.get(top);
3587 }
3588 return null;
3589 }
3590
3591 boolean isLockedTask(TaskRecord task) {
3592 return mLockTaskModeTasks.contains(task);
3593 }
3594
3595 boolean isLastLockedTask(TaskRecord task) {
3596 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3597 }
3598
3599 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003600 if (!mLockTaskModeTasks.remove(task)) {
3601 return;
3602 }
3603 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3604 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003605 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003606 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3607 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003608 final Message lockTaskMsg = Message.obtain();
3609 lockTaskMsg.arg1 = task.userId;
3610 lockTaskMsg.what = LOCK_TASK_END_MSG;
3611 mHandler.sendMessage(lockTaskMsg);
3612 }
3613 }
3614
Andrii Kulianc27916642016-04-12 17:59:27 -07003615 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3616 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3617 false /* forceNonResizable */);
3618 }
3619
Jorim Jaggid53f0922016-04-06 22:16:23 -07003620 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003621 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003622 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3623 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003624 return;
3625 }
3626
Jorim Jaggicd13d332016-04-27 15:40:20 -07003627 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003628 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003629 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003630 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003631
Andrii Kulianc27916642016-04-12 17:59:27 -07003632 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3633 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003634 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003635 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3636 && !topActivity.noDisplay) {
3637 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003638 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3639 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003640 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003641 }
3642
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003643 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003644 if (mLockTaskNotify != null) {
3645 mLockTaskNotify.showToast(mLockTaskModeState);
3646 }
Jason Monka8f569c2014-07-07 12:15:21 -04003647 }
3648
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003649 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3650 if (mLockTaskModeTasks.contains(task)) {
3651 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3652 }
3653 }
3654
Craig Mautner432f64e2015-05-20 14:59:57 -07003655 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3656 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003657 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003658 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003659 final TaskRecord lockedTask = getLockedTaskLocked();
3660 if (lockedTask != null) {
3661 removeLockedTaskLocked(lockedTask);
3662 if (!mLockTaskModeTasks.isEmpty()) {
3663 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003664 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3665 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003666 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003667 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003668 return;
3669 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003670 }
Craig Mautnere0570202015-05-13 13:06:11 -07003671 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3672 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003673 return;
3674 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003675
3676 // Should have already been checked, but do it again.
3677 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003678 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3679 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003680 return;
3681 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003682 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003683 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003684 return;
3685 }
3686
3687 if (mLockTaskModeTasks.isEmpty()) {
3688 // First locktask.
3689 final Message lockTaskMsg = Message.obtain();
3690 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3691 lockTaskMsg.arg1 = task.userId;
3692 lockTaskMsg.what = LOCK_TASK_START_MSG;
3693 lockTaskMsg.arg2 = lockTaskModeState;
3694 mHandler.sendMessage(lockTaskMsg);
3695 }
3696 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003697 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3698 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003699 mLockTaskModeTasks.remove(task);
3700 mLockTaskModeTasks.add(task);
3701
3702 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003703 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003704 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003705
3706 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003707 findTaskToMoveToFrontLocked(task, 0, null, reason,
3708 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003709 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003710 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003711 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003712 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003713 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003714 }
3715
3716 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003717 return isLockTaskModeViolation(task, false);
3718 }
3719
3720 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3721 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003722 return false;
3723 }
3724 final int lockTaskAuth = task.mLockTaskAuth;
3725 switch (lockTaskAuth) {
3726 case LOCK_TASK_AUTH_DONT_LOCK:
3727 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003728 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003729 case LOCK_TASK_AUTH_LAUNCHABLE:
3730 case LOCK_TASK_AUTH_WHITELISTED:
3731 return false;
3732 case LOCK_TASK_AUTH_PINNABLE:
3733 // Pinnable tasks can't be launched on top of locktask tasks.
3734 return !mLockTaskModeTasks.isEmpty();
3735 default:
3736 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3737 return true;
3738 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003739 }
3740
Craig Mautner15df08a2015-04-01 12:17:18 -07003741 void onLockTaskPackagesUpdatedLocked() {
3742 boolean didSomething = false;
3743 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3744 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003745 final boolean wasWhitelisted =
3746 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3747 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003748 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003749 final boolean isWhitelisted =
3750 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3751 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3752 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003753 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003754 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3755 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003756 removeLockedTaskLocked(lockedTask);
3757 lockedTask.performClearTaskLocked();
3758 didSomething = true;
3759 }
3760 }
3761 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3762 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3763 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3764 final ActivityStack stack = stacks.get(stackNdx);
3765 stack.onLockTaskPackagesUpdatedLocked();
3766 }
3767 }
Craig Mautnere0570202015-05-13 13:06:11 -07003768 final ActivityRecord r = topRunningActivityLocked();
3769 final TaskRecord task = r != null ? r.task : null;
3770 if (mLockTaskModeTasks.isEmpty() && task != null
3771 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3772 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003773 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3774 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3775 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3776 false);
3777 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003778 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003779 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003780 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003781 }
3782 }
3783
Benjamin Franz43261142015-02-11 15:59:44 +00003784 int getLockTaskModeState() {
3785 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003786 }
3787
Jorim Jaggife89d122015-12-22 16:28:44 +01003788 void activityRelaunchedLocked(IBinder token) {
3789 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003790 if (mService.isSleepingOrShuttingDownLocked()) {
3791 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3792 if (r != null) {
3793 r.setSleeping(true, true);
3794 }
3795 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003796 }
3797
3798 void activityRelaunchingLocked(ActivityRecord r) {
3799 mWindowManager.notifyAppRelaunching(r.appToken);
3800 }
3801
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003802 void logStackState() {
3803 mActivityMetricsLogger.logWindowState();
3804 }
3805
Andrii Kulian933076d2016-03-29 17:04:42 -07003806 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003807 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3808 final ActivityRecord r = task.mActivities.get(i);
3809 if (r.app != null && r.app.thread != null) {
3810 mMultiWindowModeChangedActivities.add(r);
3811 }
3812 }
3813
3814 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3815 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3816 }
3817 }
3818
Andrii Kulian933076d2016-03-29 17:04:42 -07003819 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003820 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003821 if (prevStack == null || prevStack == stack
3822 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3823 return;
3824 }
3825
3826 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3827 final ActivityRecord r = task.mActivities.get(i);
3828 if (r.app != null && r.app.thread != null) {
3829 mPipModeChangedActivities.add(r);
3830 }
3831 }
3832
3833 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3834 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3835 }
3836 }
3837
Tony Mak853304c2016-04-18 15:17:41 +01003838 void setDockedStackMinimized(boolean minimized) {
3839 mIsDockMinimized = minimized;
3840 if (minimized) {
3841 // Docked stack is not visible, no need to confirm credentials for its top activity.
3842 return;
3843 }
3844 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3845 if (dockedStack == null) {
3846 return;
3847 }
3848 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3849 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3850 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3851 }
3852 }
3853
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003854 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003855
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003856 public ActivityStackSupervisorHandler(Looper looper) {
3857 super(looper);
3858 }
3859
Craig Mautnerf3333272013-04-22 10:55:53 -07003860 void activityIdleInternal(ActivityRecord r) {
3861 synchronized (mService) {
3862 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3863 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003864 }
3865
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003866 @Override
3867 public void handleMessage(Message msg) {
3868 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003869 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3870 synchronized (mService) {
3871 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3872 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003873 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003874 }
3875 }
3876 } break;
3877 case REPORT_PIP_MODE_CHANGED_MSG: {
3878 synchronized (mService) {
3879 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3880 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003881 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003882 }
3883 }
3884 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003885 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003886 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3887 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003888 if (mService.mDidDexOpt) {
3889 mService.mDidDexOpt = false;
3890 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3891 nmsg.obj = msg.obj;
3892 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3893 return;
3894 }
3895 // We don't at this point know if the activity is fullscreen,
3896 // so we need to be conservative and assume it isn't.
3897 activityIdleInternal((ActivityRecord)msg.obj);
3898 } break;
3899 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003900 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003901 activityIdleInternal((ActivityRecord)msg.obj);
3902 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003903 case RESUME_TOP_ACTIVITY_MSG: {
3904 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003905 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003906 }
3907 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003908 case SLEEP_TIMEOUT_MSG: {
3909 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003910 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003911 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3912 mSleepTimeout = true;
3913 checkReadyForSleepLocked();
3914 }
3915 }
3916 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003917 case LAUNCH_TIMEOUT_MSG: {
3918 if (mService.mDidDexOpt) {
3919 mService.mDidDexOpt = false;
3920 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3921 return;
3922 }
3923 synchronized (mService) {
3924 if (mLaunchingActivity.isHeld()) {
3925 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3926 if (VALIDATE_WAKE_LOCK_CALLER
3927 && Binder.getCallingUid() != Process.myUid()) {
3928 throw new IllegalStateException("Calling must be system uid");
3929 }
3930 mLaunchingActivity.release();
3931 }
3932 }
3933 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003935 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 } break;
3937 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003938 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 } break;
3940 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003941 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003943 case CONTAINER_CALLBACK_VISIBILITY: {
3944 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003945 final IActivityContainerCallback callback = container.mCallback;
3946 if (callback != null) {
3947 try {
3948 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3949 } catch (RemoteException e) {
3950 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003951 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003952 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003953 case LOCK_TASK_START_MSG: {
3954 // When lock task starts, we disable the status bars.
3955 try {
Jason Monk62515be2014-05-21 16:06:19 -04003956 if (mLockTaskNotify == null) {
3957 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003958 }
Jason Monk62515be2014-05-21 16:06:19 -04003959 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003960 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003961 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003962 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003964 flags = StatusBarManager.DISABLE_MASK
3965 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003966 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003967 flags = StatusBarManager.DISABLE_MASK
3968 & (~StatusBarManager.DISABLE_BACK)
3969 & (~StatusBarManager.DISABLE_HOME)
3970 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003971 }
3972 getStatusBarService().disable(flags, mToken,
3973 mService.mContext.getPackageName());
3974 }
3975 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003976 if (getDevicePolicyManager() != null) {
3977 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003978 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003979 }
justinzhang5286d3f2014-05-12 17:06:01 -04003980 } catch (RemoteException ex) {
3981 throw new RuntimeException(ex);
3982 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003983 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003984 case LOCK_TASK_END_MSG: {
3985 // When lock task ends, we enable the status bars.
3986 try {
Jason Monk62515be2014-05-21 16:06:19 -04003987 if (getStatusBarService() != null) {
3988 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3989 mService.mContext.getPackageName());
3990 }
3991 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003992 if (getDevicePolicyManager() != null) {
3993 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3994 msg.arg1);
3995 }
Jason Monk62515be2014-05-21 16:06:19 -04003996 if (mLockTaskNotify == null) {
3997 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3998 }
3999 mLockTaskNotify.show(false);
4000 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004001 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004002 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004003 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004004 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004005 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004006 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004007 new LockPatternUtils(mService.mContext)
4008 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004009 }
4010 } catch (SettingNotFoundException e) {
4011 // No setting, don't lock.
4012 }
justinzhang5286d3f2014-05-12 17:06:01 -04004013 } catch (RemoteException ex) {
4014 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004015 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004016 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004017 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004018 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004019 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4020 if (mLockTaskNotify == null) {
4021 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4022 }
4023 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4024 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004025 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4026 final ActivityContainer container = (ActivityContainer) msg.obj;
4027 final IActivityContainerCallback callback = container.mCallback;
4028 if (callback != null) {
4029 try {
4030 callback.onAllActivitiesComplete(container.asBinder());
4031 } catch (RemoteException e) {
4032 }
4033 }
4034 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004035 case LAUNCH_TASK_BEHIND_COMPLETE: {
4036 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004037 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004038 if (r != null) {
4039 handleLaunchTaskBehindCompleteLocked(r);
4040 }
4041 }
4042 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004043
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004044 }
4045 }
4046 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004047
Ying Wangb081a592014-04-22 15:20:16 -07004048 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004049 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4050 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004051 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004054 ActivityRecord mParentActivity = null;
4055 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004056
Craig Mautnere3a00d72014-04-16 08:31:19 -07004057 boolean mVisible = true;
4058
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004060 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004062 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4063 final static int CONTAINER_STATE_NO_SURFACE = 1;
4064 final static int CONTAINER_STATE_FINISHING = 2;
4065 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4066
4067 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004068 synchronized (mService) {
4069 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004070 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004071 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004072 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004074 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075
Andrii Kulian839def92016-11-02 10:58:58 -07004076 /**
4077 * Adds the stack to specified display. Also calls WindowManager to do the same from
4078 * {@link ActivityStack#addToDisplay(ActivityDisplay, boolean)}.
4079 * @param activityDisplay The display to add the stack to.
4080 * @param onTop If true the stack will be place at the top of the display, else at the
4081 * bottom.
4082 */
4083 void addToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
4084 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004085 + " to display=" + activityDisplay + " onTop=" + onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004086 if (mActivityDisplay != null) {
4087 throw new IllegalStateException("ActivityContainer is already attached, " +
4088 "displayId=" + mActivityDisplay.mDisplayId);
4089 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004090 mActivityDisplay = activityDisplay;
Andrii Kulian839def92016-11-02 10:58:58 -07004091 mStack.addToDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004092 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 }
4094
4095 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004096 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004098 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4099 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 return;
4101 }
Andrii Kulian839def92016-11-02 10:58:58 -07004102 addToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 }
4104 }
4105
4106 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004107 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004108 synchronized (mService) {
4109 if (mActivityDisplay != null) {
4110 return mActivityDisplay.mDisplayId;
4111 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004112 }
4113 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004114 }
4115
Jeff Brownca9bc702014-02-11 14:32:56 -08004116 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004117 public int getStackId() {
4118 synchronized (mService) {
4119 return mStackId;
4120 }
4121 }
4122
4123 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004124 public boolean injectEvent(InputEvent event) {
4125 final long origId = Binder.clearCallingIdentity();
4126 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004127 synchronized (mService) {
4128 if (mActivityDisplay != null) {
4129 return mInputManagerInternal.injectInputEvent(event,
4130 mActivityDisplay.mDisplayId,
4131 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4132 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004133 }
4134 return false;
4135 } finally {
4136 Binder.restoreCallingIdentity(origId);
4137 }
4138 }
4139
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004140 @Override
4141 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004142 synchronized (mService) {
4143 if (mContainerState == CONTAINER_STATE_FINISHING) {
4144 return;
4145 }
4146 mContainerState = CONTAINER_STATE_FINISHING;
4147
Craig Mautnerd163e752014-06-13 17:18:47 -07004148 long origId = Binder.clearCallingIdentity();
4149 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004150 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004151 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004152 } finally {
4153 Binder.restoreCallingIdentity(origId);
4154 }
4155 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004156 }
4157
Andrii Kulian839def92016-11-02 10:58:58 -07004158 /** Remove the stack completely. */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004159 void removeLocked() {
4160 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004161 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004162 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004163 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004164 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004165 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004166 }
4167
Andrii Kulian839def92016-11-02 10:58:58 -07004168 /**
4169 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4170 * completely or re-parented.
4171 */
4172 private void removeFromDisplayLocked() {
4173 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4174 + " current displayId=" + mActivityDisplay.mDisplayId);
4175
4176 mActivityDisplay.detachActivitiesLocked(mStack);
4177 mActivityDisplay = null;
4178 }
4179
4180 /**
4181 * Move the stack to specified display.
4182 * @param activityDisplay Target display to move the stack to.
4183 */
4184 void moveToDisplayLocked(ActivityDisplay activityDisplay) {
4185 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4186 + mActivityDisplay + " to display=" + activityDisplay
4187 + " Callers=" + Debug.getCallers(2));
4188
4189 removeFromDisplayLocked();
4190
4191 mActivityDisplay = activityDisplay;
4192 mStack.moveToDisplay(activityDisplay);
4193 activityDisplay.attachActivities(mStack, ON_TOP);
4194 }
4195
Craig Mautner4a1cb222013-12-04 16:14:06 -08004196 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004197 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004198 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004199 }
4200
4201 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004202 public final int startActivityIntentSender(IIntentSender intentSender)
4203 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004204 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4205
4206 if (!(intentSender instanceof PendingIntentRecord)) {
4207 throw new IllegalArgumentException("Bad PendingIntent object");
4208 }
4209
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004210 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004211 Binder.getCallingUid(), mCurrentUser, false,
4212 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004213
4214 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4215 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4216 pendingIntent.key.requestResolvedType);
4217
4218 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4219 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4220 }
4221
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004222 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004223 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004224 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004225 throw new SecurityException(
4226 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4227 }
4228 }
4229
Craig Mautnerdf88d732014-01-27 09:21:32 -08004230 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004231 public IBinder asBinder() {
4232 return this;
4233 }
4234
Craig Mautner4504de52013-12-20 09:06:56 -08004235 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004236 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004237 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004238 }
4239
Craig Mautner4a1cb222013-12-04 16:14:06 -08004240 ActivityStackSupervisor getOuter() {
4241 return ActivityStackSupervisor.this;
4242 }
4243
Craig Mautnerd163e752014-06-13 17:18:47 -07004244 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004245 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004246 }
4247
Craig Mautner6985bad2014-04-21 15:22:06 -07004248 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004249 void setVisible(boolean visible) {
4250 if (mVisible != visible) {
4251 mVisible = visible;
4252 if (mCallback != null) {
4253 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4254 0 /* unused */, this).sendToTarget();
4255 }
4256 }
4257 }
4258
Craig Mautner6985bad2014-04-21 15:22:06 -07004259 void setDrawn() {
4260 }
4261
Craig Mautner1b4bf852014-05-26 15:06:32 -07004262 // You can always start a new task on a regular ActivityStack.
4263 boolean isEligibleForNewTasks() {
4264 return true;
4265 }
4266
Craig Mautnerd163e752014-06-13 17:18:47 -07004267 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004268 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004269 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004270 }
4271
Craig Mautner34b73df2014-01-12 21:11:08 -08004272 @Override
4273 public String toString() {
4274 return mIdString + (mActivityDisplay == null ? "N" : "A");
4275 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004276 }
4277
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004278 private class VirtualActivityContainer extends ActivityContainer {
4279 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004280 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004281
4282 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4283 super(getNextStackId());
4284 mParentActivity = parent;
4285 mCallback = callback;
4286 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004287 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004288 }
4289
4290 @Override
4291 public void setSurface(Surface surface, int width, int height, int density) {
4292 super.setSurface(surface, width, height, density);
4293
4294 synchronized (mService) {
4295 final long origId = Binder.clearCallingIdentity();
4296 try {
4297 setSurfaceLocked(surface, width, height, density);
4298 } finally {
4299 Binder.restoreCallingIdentity(origId);
4300 }
4301 }
4302 }
4303
4304 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4305 if (mContainerState == CONTAINER_STATE_FINISHING) {
4306 return;
4307 }
4308 VirtualActivityDisplay virtualActivityDisplay =
4309 (VirtualActivityDisplay) mActivityDisplay;
4310 if (virtualActivityDisplay == null) {
4311 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004312 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004313 mActivityDisplay = virtualActivityDisplay;
4314 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004315 addToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004316 }
4317
4318 if (mSurface != null) {
4319 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004320 }
4321
Craig Mautner6985bad2014-04-21 15:22:06 -07004322 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004323 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004324 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004325 } else {
4326 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004327 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004328 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004329 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004330 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004331 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004332
Craig Mautnerd163e752014-06-13 17:18:47 -07004333 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004334
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004335 if (DEBUG_STACK) Slog.d(TAG_STACK,
4336 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004337 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004338
Craig Mautner6985bad2014-04-21 15:22:06 -07004339 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004340 boolean isAttachedLocked() {
4341 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004342 }
4343
4344 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004345 void setDrawn() {
4346 synchronized (mService) {
4347 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004348 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004349 }
4350 }
4351
Craig Mautner1b4bf852014-05-26 15:06:32 -07004352 // Never start a new task on an ActivityView if it isn't explicitly specified.
4353 @Override
4354 boolean isEligibleForNewTasks() {
4355 return false;
4356 }
4357
Craig Mautnerd163e752014-06-13 17:18:47 -07004358 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004359 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004360 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4361 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4362 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4363 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4364 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004365 }
4366 }
4367
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4369 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004370 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004372 int mDisplayId;
4373 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004374 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4375 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004376
Craig Mautner4a1cb222013-12-04 16:14:06 -08004377 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4378 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004379 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004380
Jose Lima4b6c6692014-08-12 17:41:12 -07004381 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004382
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004383 ActivityDisplay() {
4384 }
Craig Mautner4504de52013-12-20 09:06:56 -08004385
Craig Mautner1a70a162014-09-13 12:09:31 -07004386 // After instantiation, check that mDisplay is not null before using this. The alternative
4387 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004388 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004389 final Display display = mDisplayManager.getDisplay(displayId);
4390 if (display == null) {
4391 return;
4392 }
4393 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004394 }
4395
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004396 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004397 mDisplay = display;
4398 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004399 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004400
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004401 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004402 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004403 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4404 + " onTop=" + onTop);
4405 if (onTop) {
4406 mStacks.add(stack);
4407 } else {
4408 mStacks.add(0, stack);
4409 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004410 }
4411
Craig Mautnere0a38842013-12-16 16:14:02 -08004412 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004413 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004414 + " from displayId=" + mDisplayId);
4415 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004416 }
4417
Jose Lima4b6c6692014-08-12 17:41:12 -07004418 void setVisibleBehindActivity(ActivityRecord r) {
4419 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004420 }
4421
Jose Lima4b6c6692014-08-12 17:41:12 -07004422 boolean hasVisibleBehindActivity() {
4423 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004424 }
4425
Craig Mautner34b73df2014-01-12 21:11:08 -08004426 @Override
4427 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004428 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4429 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004430
4431 @Override
4432 protected int getChildCount() {
4433 return mStacks.size();
4434 }
4435
4436 @Override
4437 protected ConfigurationContainer getChildAt(int index) {
4438 return mStacks.get(index);
4439 }
4440
4441 @Override
4442 protected ConfigurationContainer getParent() {
4443 return ActivityStackSupervisor.this;
4444 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004445 }
4446
4447 class VirtualActivityDisplay extends ActivityDisplay {
4448 VirtualDisplay mVirtualDisplay;
4449
Craig Mautner6985bad2014-04-21 15:22:06 -07004450 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004451 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004452 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4453 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4454 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4455 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004456
4457 init(mVirtualDisplay.getDisplay());
4458
4459 mWindowManager.handleDisplayAdded(mDisplayId);
4460 }
4461
4462 void setSurface(Surface surface) {
4463 if (mVirtualDisplay != null) {
4464 mVirtualDisplay.setSurface(surface);
4465 }
4466 }
4467
4468 @Override
4469 void detachActivitiesLocked(ActivityStack stack) {
4470 super.detachActivitiesLocked(stack);
4471 if (mVirtualDisplay != null) {
4472 mVirtualDisplay.release();
4473 mVirtualDisplay = null;
4474 }
4475 }
4476
4477 @Override
4478 public String toString() {
4479 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004480 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004481 }
Jose Lima58e66d62014-05-27 20:00:27 -07004482
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004483 ActivityStack findStackBehind(ActivityStack stack) {
4484 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004485 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004486 if (display == null) {
4487 return null;
4488 }
4489 final ArrayList<ActivityStack> stacks = display.mStacks;
4490 for (int i = stacks.size() - 1; i >= 0; i--) {
4491 if (stacks.get(i) == stack && i > 0) {
4492 return stacks.get(i - 1);
4493 }
4494 }
4495 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4496 + " in=" + stacks);
4497 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004498
Jorim Jaggic69bd222016-03-15 14:38:37 +01004499 /**
4500 * Puts a task into resizing mode during the next app transition.
4501 *
4502 * @param taskId the id of the task to put into resizing mode
4503 */
4504 private void setResizingDuringAnimation(int taskId) {
4505 mResizingTasksDuringAnimation.add(taskId);
4506 mWindowManager.setTaskDockedResizing(taskId, true);
4507 }
4508
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004509 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4510 final TaskRecord task;
4511 final int callingUid;
4512 final String callingPackage;
4513 final Intent intent;
4514 final int userId;
4515 final ActivityOptions activityOptions = (bOptions != null)
4516 ? new ActivityOptions(bOptions) : null;
4517 final int launchStackId = (activityOptions != null)
4518 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004519 if (launchStackId == HOME_STACK_ID) {
4520 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4521 + taskId + " can't be launch in the home stack.");
4522 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004523
4524 if (launchStackId == DOCKED_STACK_ID) {
4525 mWindowManager.setDockedStackCreateState(
4526 activityOptions.getDockCreateMode(), null /* initialBounds */);
4527
4528 // Defer updating the stack in which recents is until the app transition is done, to
4529 // not run into issues where we still need to draw the task in recents but the
4530 // docked stack is already created.
4531 deferUpdateBounds(HOME_STACK_ID);
4532 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4533 }
4534
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004535 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4536 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004537 continueUpdateBounds(HOME_STACK_ID);
4538 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004539 throw new IllegalArgumentException(
4540 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4541 }
4542
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004543 // Since we don't have an actual source record here, we assume that the currently focused
4544 // activity was the source.
4545 final ActivityStack focusedStack = getFocusedStack();
4546 final ActivityRecord sourceRecord =
4547 focusedStack != null ? focusedStack.topActivity() : null;
4548
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004549 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004550 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004551 moveTaskToStackLocked(
4552 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4553 ANIMATE);
4554 }
4555 }
4556
4557 // If the user must confirm credentials (e.g. when first launching a work app and the
4558 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4559 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4560 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004561 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004562 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004563 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004564 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4565 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004566
4567 // If we are launching the task in the docked stack, put it into resizing mode so
4568 // the window renders full-screen with the background filling the void. Also only
4569 // call this at the end to make sure that tasks exists on the window manager side.
4570 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004571 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004572 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004573
4574 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4575 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004576 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4577 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004578 return ActivityManager.START_TASK_TO_FRONT;
4579 }
4580 callingUid = task.mCallingUid;
4581 callingPackage = task.mCallingPackage;
4582 intent = task.intent;
4583 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4584 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004585 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4586 null, null, 0, 0, bOptions, userId, null, task);
4587 if (launchStackId == DOCKED_STACK_ID) {
4588 setResizingDuringAnimation(task.taskId);
4589 }
4590 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004591 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004592
4593 /**
4594 * @return a list of activities which are the top ones in each visible stack. The first
4595 * entry will be the focused activity.
4596 */
4597 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004598 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4599 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004600 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004601 if (display == null) {
4602 return Collections.EMPTY_LIST;
4603 }
4604 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4605 final ArrayList<ActivityStack> stacks = display.mStacks;
4606 for (int i = stacks.size() - 1; i >= 0; i--) {
4607 ActivityStack stack = stacks.get(i);
4608 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4609 ActivityRecord top = stack.topActivity();
4610 if (top != null) {
4611 if (stack == mFocusedStack) {
4612 topActivityTokens.add(0, top.appToken);
4613 } else {
4614 topActivityTokens.add(top.appToken);
4615 }
4616 }
4617 }
4618 }
4619 return topActivityTokens;
4620 }
Craig Mautner27084302013-03-25 08:05:25 -07004621}