blob: 834a4de268ee7303860994f34683293dd1c92481 [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;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700103import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -0700105import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700106import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400107import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -0400108import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700110import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700112import android.content.Intent;
113import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700115import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.pm.PackageManager;
117import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100118import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.res.Configuration;
Winson Chung303c6b72016-10-24 17:12:49 -0700120import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800121import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800122import android.hardware.display.DisplayManager;
123import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800124import android.hardware.display.DisplayManagerGlobal;
125import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800126import android.hardware.input.InputManager;
127import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100129import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700130import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700131import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700133import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700134import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700136import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700137import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700138import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400139import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700141import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700142import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700143import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100144import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700145import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700146import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400147import android.provider.Settings;
148import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700149import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700150import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700151import android.util.ArraySet;
Winson Chung303c6b72016-10-24 17:12:49 -0700152import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700153import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700154import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800156import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800157import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800158import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800159import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800160
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800161import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700162import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400163import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700164import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400165import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800166import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700167import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700168import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700169
Craig Mautner8d341ef2013-03-26 09:03:27 -0700170import java.io.FileDescriptor;
171import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700172import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700173import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700174import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700175import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700176import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800177import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700178import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700179
Jorim Jaggife762342016-10-13 14:33:27 +0200180public class ActivityStackSupervisor extends ConfigurationContainer
Andrii Kulian1779e612016-10-12 21:58:25 -0700181 implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700184 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700186 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800193 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800195
Craig Mautnerf3333272013-04-22 10:55:53 -0700196 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700198
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700204
Craig Mautner05d29032013-05-03 13:40:13 -0700205 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
206 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
207 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
211 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
212 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700213 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400214 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
215 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700216 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700217 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700218 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800219 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
220 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223
Jason Monk62515be2014-05-21 16:06:19 -0400224 private static final String LOCK_TASK_TAG = "Lock-to-App";
225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 // Used to indicate if an object (e.g. stack) that we are trying to get
227 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800228 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700230 // Used to indicate that windows of activities should be preserved during the resize.
231 static final boolean PRESERVE_WINDOWS = true;
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. task) should be moved/created
234 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700235 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
237 // Used to indicate that an objects (e.g. task) removal from its container
238 // (e.g. stack) is due to it moving to another container.
239 static final boolean MOVING = true;
240
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700241 // Force the focus to change to the stack we are moving a task to..
242 static final boolean FORCE_FOCUS = true;
243
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700244 // Restore task from the saved recents if it can't be found in any live stack.
245 static final boolean RESTORE_FROM_RECENTS = true;
246
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700247 // Don't execute any calls to resume.
248 static final boolean DEFER_RESUME = true;
249
Svetoslav7008b512015-06-24 18:47:07 -0700250 // Activity actions an app cannot start if it uses a permission which is not granted.
251 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
252 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700253
Svetoslav7008b512015-06-24 18:47:07 -0700254 static {
255 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
256 Manifest.permission.CAMERA);
257 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
258 Manifest.permission.CAMERA);
259 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
260 Manifest.permission.CALL_PHONE);
261 }
262
Svet Ganov99b60432015-06-27 13:15:22 -0700263 /** Action restriction: launching the activity is not restricted. */
264 private static final int ACTIVITY_RESTRICTION_NONE = 0;
265 /** Action restriction: launching the activity is restricted by a permission. */
266 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
267 /** Action restriction: launching the activity is restricted by an app op. */
268 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700269
justinzhang5286d3f2014-05-12 17:06:01 -0400270 /** Status Bar Service **/
271 private IBinder mToken = new Binder();
272 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400273 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400274
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700275 // For debugging to make sure the caller when acquiring/releasing our
276 // wake lock is the system process.
277 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800278 /** The number of distinct task ids that can be assigned to the tasks of a single user */
279 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700280
Craig Mautner27084302013-03-25 08:05:25 -0700281 final ActivityManagerService mService;
282
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800284
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285 final ActivityStackSupervisorHandler mHandler;
286
287 /** Short cut */
288 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800289 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700291 /** Counter for next free stack ID to use for dynamic activity stacks. */
292 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700293
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800294 /**
295 * Maps the task identifier that activities are currently being started in to the userId of the
296 * task. Each time a new task is created, the entry for the userId of the task is incremented
297 */
298 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700299
Craig Mautner2420ead2013-04-01 17:13:20 -0700300 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800301 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700302
Craig Mautnere0a38842013-12-16 16:14:02 -0800303 /** The stack containing the launcher app. Assumed to always be attached to
304 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800305 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700306
Craig Mautnere0a38842013-12-16 16:14:02 -0800307 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700309
Craig Mautner4a1cb222013-12-04 16:14:06 -0800310 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
311 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700314
315 /** List of activities that are waiting for a new activity to become visible before completing
316 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
322 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 /** List of activities that are ready to be stopped, but waiting for the next activity to
326 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700328
Craig Mautnerf3333272013-04-22 10:55:53 -0700329 /** List of activities that are ready to be finished, but waiting for the previous activity to
330 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800331 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700332
Craig Mautner0eea92c2013-05-16 13:35:39 -0700333 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800334 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700335
Wale Ogunwale22e25262016-02-01 10:32:02 -0800336 /** List of activities whose multi-window mode changed that we need to report to the
337 * application */
338 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
339
340 /** List of activities whose picture-in-picture mode changed that we need to report to the
341 * application */
342 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
343
Craig Mautnerf3333272013-04-22 10:55:53 -0700344 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700345 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700346
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
348 * is being brought in front of us. */
349 boolean mUserLeaving = false;
350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** Set when we have taken too long waiting to go to sleep. */
352 boolean mSleepTimeout = false;
353
354 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355 * We don't want to allow the device to go to sleep while in the process
356 * of launching an activity. This is primarily to allow alarm intent
357 * receivers to launch an activity and get that to run before the device
358 * goes back to sleep.
359 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700360 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361
362 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 * Set when the system is going to sleep, until we have
364 * successfully paused the current activity and released our wake lock.
365 * At that point the system is allowed to actually sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700369 /** Stack id of the front stack when user switched, indexed by userId. */
370 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700371
Craig Mautner4504de52013-12-20 09:06:56 -0800372 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700374 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375
376 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378
Jeff Brownca9bc702014-02-11 14:32:56 -0800379 InputManagerInternal mInputManagerInternal;
380
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
382 * may be finished until there is only one entry left. If this is empty the system is not
383 * in lockTask mode. */
384 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000385 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700386 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
387 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000388 */
389 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400390 /**
391 * Notifies the user when entering/exiting lock-task.
392 */
393 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800394
Wale Ogunwaled046a012015-12-24 13:05:59 -0800395 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800396 boolean inResumeTopActivity;
397
Andrii Kulian1e32e022016-09-16 15:29:34 -0700398 /**
399 * Temporary rect used during docked stack resize calculation so we don't need to create a new
400 * object each time.
401 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700402 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700403
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700404 // The default minimal size that will be used if the activity doesn't specify its minimal size.
405 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700406 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700407
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700408 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
409 private boolean mTaskLayersChanged = true;
410
Jorim Jaggi275561a2016-02-23 10:11:02 -0500411 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800412
Jorim Jaggidc249c42015-12-15 14:57:31 -0800413 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
414
Andrii Kulian1779e612016-10-12 21:58:25 -0700415 @Override
416 protected int getChildCount() {
417 return mActivityDisplays.size();
418 }
419
420 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700421 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700422 return mActivityDisplays.valueAt(index);
423 }
424
425 @Override
426 protected ConfigurationContainer getParent() {
427 return null;
428 }
429
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700430 Configuration getDisplayOverrideConfiguration(int displayId) {
431 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
432 if (activityDisplay == null) {
433 throw new IllegalArgumentException("No display found with id: " + displayId);
434 }
435
436 return activityDisplay.getOverrideConfiguration();
437 }
438
439 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
440 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
441 if (activityDisplay == null) {
442 throw new IllegalArgumentException("No display found with id: " + displayId);
443 }
444
445 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
446 }
447
Wale Ogunwale39381972015-12-17 17:15:29 -0800448 static class FindTaskResult {
449 ActivityRecord r;
450 boolean matchedByRootAffinity;
451 }
452 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
453
Craig Mautneree36c772014-07-16 14:56:05 -0700454 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100455 * Used to keep track whether app visibilities got changed since the last pause. Useful to
456 * determine whether to invoke the task stack change listener after pausing.
457 */
458 boolean mAppVisibilitiesChangedSinceLastPause;
459
460 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100461 * Set of tasks that are in resizing mode during an app transition to fill the "void".
462 */
463 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
464
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700465
466 /**
467 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
468 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
469 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
470 * like the docked stack going empty.
471 */
472 private boolean mAllowDockedStackResize = true;
473
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100474 /**
Tony Mak853304c2016-04-18 15:17:41 +0100475 * Is dock currently minimized.
476 */
477 boolean mIsDockMinimized;
478
Jorim Jaggife762342016-10-13 14:33:27 +0200479 final KeyguardController mKeyguardController;
480
Tony Mak853304c2016-04-18 15:17:41 +0100481 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700482 * Description of a request to start a new activity, which has been held
483 * due to app switches being disabled.
484 */
485 static class PendingActivityLaunch {
486 final ActivityRecord r;
487 final ActivityRecord sourceRecord;
488 final int startFlags;
489 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700490 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700491
492 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700493 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700494 r = _r;
495 sourceRecord = _sourceRecord;
496 startFlags = _startFlags;
497 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700498 callerApp = _callerApp;
499 }
500
501 void sendErrorResult(String message) {
502 try {
503 if (callerApp.thread != null) {
504 callerApp.thread.scheduleCrash(message);
505 }
506 } catch (RemoteException e) {
507 Slog.e(TAG, "Exception scheduling crash of failed "
508 + "activity launcher sourceRecord=" + sourceRecord, e);
509 }
Craig Mautneree36c772014-07-16 14:56:05 -0700510 }
511 }
512
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800513 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700514 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700515 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800516 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800517 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jorim Jaggife762342016-10-13 14:33:27 +0200518 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700519 }
520
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800521 void setRecentTasks(RecentTasks recentTasks) {
522 mRecentTasks = recentTasks;
523 }
524
Jeff Brown2c43c332014-06-12 22:38:59 -0700525 /**
526 * At the time when the constructor runs, the power manager has not yet been
527 * initialized. So we initialize our wakelocks afterwards.
528 */
529 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700531 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700532 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700533 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700534 }
535
justinzhang5286d3f2014-05-12 17:06:01 -0400536 // This function returns a IStatusBarService. The value is from ServiceManager.
537 // getService and is cached.
538 private IStatusBarService getStatusBarService() {
539 synchronized (mService) {
540 if (mStatusBarService == null) {
541 mStatusBarService = IStatusBarService.Stub.asInterface(
542 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
543 if (mStatusBarService == null) {
544 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
545 }
546 }
547 return mStatusBarService;
548 }
549 }
550
Jason Monk35c62a42014-06-17 10:24:47 -0400551 private IDevicePolicyManager getDevicePolicyManager() {
552 synchronized (mService) {
553 if (mDevicePolicyManager == null) {
554 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
555 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
556 if (mDevicePolicyManager == null) {
557 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
558 }
559 }
560 return mDevicePolicyManager;
561 }
562 }
563
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700564 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 synchronized (mService) {
566 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200567 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568
569 mDisplayManager =
570 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
571 mDisplayManager.registerDisplayListener(this, null);
572
573 Display[] displays = mDisplayManager.getDisplays();
574 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
575 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800576 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700577 if (activityDisplay.mDisplay == null) {
578 throw new IllegalStateException("Default Display does not exist");
579 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800580 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700581 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 }
583
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800584 mHomeStack = mFocusedStack = mLastFocusedStack =
585 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800586
587 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800588 }
Craig Mautner27084302013-03-25 08:05:25 -0700589 }
590
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700591 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700593 }
594
Craig Mautnerde4ef022013-04-07 19:01:33 -0700595 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700597 }
598
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700599 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700600 if (stack == null) {
601 return false;
602 }
603
Craig Mautnerdf88d732014-01-27 09:21:32 -0800604 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
605 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700606 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800607 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800608 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700609 }
610
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700611 /** The top most stack. */
612 boolean isFrontStack(ActivityStack stack) {
613 if (stack == null) {
614 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700616
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700617 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
618 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700619 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800620 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700621 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
622 }
623
Wale Ogunwaled046a012015-12-24 13:05:59 -0800624 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800625 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
626 if (!focusCandidate.isFocusable()) {
627 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
628 focusCandidate = focusCandidate.getNextFocusableStackLocked();
629 }
630
631 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800632 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800633 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800634
Wale Ogunwaled046a012015-12-24 13:05:59 -0800635 EventLogTags.writeAmFocusedStack(
636 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
637 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
638 }
639
640 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800641 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800642 if (r != null && r.idle) {
643 checkFinishBootingLocked();
644 }
645 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700646 }
647
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700648 void moveHomeStackToFront(String reason) {
649 mHomeStack.moveToFront(reason);
650 }
651
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700652 /** Returns true if the focus activity was adjusted to the home stack top activity. */
653 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700654 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700655 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700656 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700657 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700658
Craig Mautner84984fa2014-06-19 11:19:20 -0700659 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700660
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700661 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700662 if (top == null) {
663 return false;
664 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700665 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700666 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700667 }
668
Craig Mautner299f9602015-01-26 09:47:33 -0800669 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700670 if (!mService.mBooting && !mService.mBooted) {
671 // Not ready yet!
672 return false;
673 }
674
Craig Mautner84984fa2014-06-19 11:19:20 -0700675 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700676 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700677 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700678 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679
Craig Mautner84984fa2014-06-19 11:19:20 -0700680 if (prev != null) {
681 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
682 }
683
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700684 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
685 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800686 final String myReason = reason + " resumeHomeStackTask";
687
Mark Lua56ea122015-10-08 13:31:01 +0800688 // Only resume home activity if isn't finishing.
689 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700690 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800691 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700692 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800693 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700694 }
695
Craig Mautner8d341ef2013-03-26 09:03:27 -0700696 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700697 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700698 }
699
700 /**
701 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
702 * @param id Id of the task we would like returned.
703 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
704 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700705 * @param stackId The stack to restore the task to (default launch stack will be used if
706 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700707 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700708 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800709 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800711 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
713 ActivityStack stack = stacks.get(stackNdx);
714 TaskRecord task = stack.taskForIdLocked(id);
715 if (task != null) {
716 return task;
717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700718 }
719 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800720
721 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700722 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800723 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800724 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700725 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800726 return null;
727 }
728
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700729 if (!restoreFromRecents) {
730 return task;
731 }
732
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700733 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700734 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
735 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800736 return null;
737 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700738 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800739 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700740 }
741
Craig Mautner6170f732013-04-02 13:05:23 -0700742 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800743 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800744 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800745 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
747 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
748 if (r != null) {
749 return r;
750 }
Craig Mautner6170f732013-04-02 13:05:23 -0700751 }
752 }
753 return null;
754 }
755
Tony Mak853304c2016-04-18 15:17:41 +0100756 /**
757 * TODO: Handle freefom mode.
758 * @return true when credential confirmation is needed for the user and there is any
759 * activity started by the user in any visible stack.
760 */
761 boolean isUserLockedProfile(@UserIdInt int userId) {
762 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
763 return false;
764 }
765 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
766 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
767 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
768 for (final ActivityStack activityStack : activityStacks) {
769 if (activityStack == null) {
770 continue;
771 }
772 if (activityStack.topRunningActivityLocked() == null) {
773 continue;
774 }
775 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
776 continue;
777 }
778 if (activityStack.isDockedStack() && mIsDockMinimized) {
779 continue;
780 }
781 final TaskRecord topTask = activityStack.topTask();
782 if (topTask == null) {
783 continue;
784 }
785 // To handle the case that work app is in the task but just is not the top one.
786 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
787 final ActivityRecord activityRecord = topTask.mActivities.get(i);
788 if (activityRecord.userId == userId) {
789 return true;
790 }
791 }
792 }
793 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000794 }
795
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800796 void setNextTaskIdForUserLocked(int taskId, int userId) {
797 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
798 if (taskId > currentTaskId) {
799 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700800 }
801 }
802
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700803 static int nextTaskIdForUser(int taskId, int userId) {
804 int nextTaskId = taskId + 1;
805 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
806 // Wrap around as there will be smaller task ids that are available now.
807 nextTaskId -= MAX_TASK_IDS_PER_USER;
808 }
809 return nextTaskId;
810 }
811
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800812 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800813 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
814 // for a userId u, a taskId can only be in the range
815 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
816 // 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 -0700817 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800818 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
819 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
820 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700821 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800822 if (candidateTaskId == currentTaskId) {
823 // Something wrong!
824 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
825 throw new IllegalStateException("Cannot get an available task id."
826 + " Reached limit of " + MAX_TASK_IDS_PER_USER
827 + " running tasks per user.");
828 }
829 }
830 mCurTaskIdForUser.put(userId, candidateTaskId);
831 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700832 }
833
Chong Zhang6cda19c2016-06-14 19:07:56 -0700834 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800835 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700836 if (stack == null) {
837 return null;
838 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700839 ActivityRecord resumedActivity = stack.mResumedActivity;
840 if (resumedActivity == null || resumedActivity.app == null) {
841 resumedActivity = stack.mPausingActivity;
842 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700843 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700844 }
845 }
846 return resumedActivity;
847 }
848
Dianne Hackbornff072722014-09-24 10:56:28 -0700849 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700850 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
853 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
855 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700856 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800857 continue;
858 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700859 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800860 if (hr != null) {
861 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
862 && processName.equals(hr.processName)) {
863 try {
George Mount2c92c972014-03-20 09:38:23 -0700864 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 didSomething = true;
866 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700867 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 Slog.w(TAG, "Exception in new application when starting activity "
869 + hr.intent.getComponent().flattenToShortString(), e);
870 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700871 }
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
874 }
875 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700876 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700877 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700878 }
Craig Mautner20e72272013-04-01 13:45:53 -0700879 return didSomething;
880 }
881
882 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800883 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
884 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
886 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700887 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 continue;
889 }
890 final ActivityRecord resumedActivity = stack.mResumedActivity;
891 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700892 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800893 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800894 return false;
895 }
Craig Mautner20e72272013-04-01 13:45:53 -0700896 }
897 }
Wei Wang65c7a152016-06-02 18:51:22 -0700898 // Send launch end powerhint when idle
899 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700900 return true;
901 }
902
Craig Mautnerde4ef022013-04-07 19:01:33 -0700903 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800904 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
905 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800906 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
907 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700908 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700910 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 return false;
912 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 }
914 }
915 }
916 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700917 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
919 mLastFocusedStack + " to=" + mFocusedStack);
920 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700921 return true;
922 }
923
924 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800925 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800926 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
927 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800928 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
929 final ActivityStack stack = stacks.get(stackNdx);
930 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800931 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700932 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800933 return false;
934 }
935 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 }
938 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800939 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700940 }
941
Craig Mautner2acc3892013-09-23 10:28:14 -0700942 /**
943 * Pause all activities in either all of the stacks or just the back stacks.
944 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700945 * @param resuming The resuming activity.
946 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
947 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700948 * @return true if any activity was paused as a result of this call.
949 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700950 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700951 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
953 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
955 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700956 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700957 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800958 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700959 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
960 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700962 }
963 }
964 return someActivityPaused;
965 }
966
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700968 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800969 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
970 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
972 final ActivityStack stack = stacks.get(stackNdx);
973 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700974 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700976 Slog.d(TAG_STATES,
977 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800978 pausing = false;
979 } else {
980 return false;
981 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700982 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700983 }
984 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700985 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700986 }
987
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700988 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700989 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500990 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800991 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
992 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
993 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
994 final ActivityStack stack = stacks.get(stackNdx);
995 if (stack.mResumedActivity != null &&
996 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700997 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800998 }
999 }
1000 }
1001 }
1002
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001003 void cancelInitializingActivities() {
1004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1005 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1006 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1007 stacks.get(stackNdx).cancelInitializingActivities();
1008 }
1009 }
1010 }
1011
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001012 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001013 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001014 }
1015
1016 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1017 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001018 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001019 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001020 if (w.who == null) {
1021 changed = true;
1022 w.timeout = false;
1023 if (r != null) {
1024 w.who = new ComponentName(r.info.packageName, r.info.name);
1025 }
1026 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1027 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001028 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001029 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001030 if (changed) {
1031 mService.notifyAll();
1032 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001033 }
1034
Chong Zhang5022da32016-06-21 16:31:37 -07001035 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1036 boolean changed = false;
1037 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1038 WaitResult w = mWaitingActivityLaunched.remove(i);
1039 if (w.who == null) {
1040 changed = true;
1041 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1042 // the starting activity ends up moving another activity to front, and it should
1043 // wait for this new activity to become visible instead.
1044 // Do not modify other fields.
1045 w.result = START_TASK_TO_FRONT;
1046 }
1047 }
1048 if (changed) {
1049 mService.notifyAll();
1050 }
1051 }
1052
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001053 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1054 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001055 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001056 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001057 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001058 if (w.who == null) {
1059 changed = true;
1060 w.timeout = timeout;
1061 if (r != null) {
1062 w.who = new ComponentName(r.info.packageName, r.info.name);
1063 }
1064 w.thisTime = thisTime;
1065 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001066 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001069 if (changed) {
1070 mService.notifyAll();
1071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001072 }
1073
Craig Mautner29219d92013-04-16 20:19:12 -07001074 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001075 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001076 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001077 if (r != null) {
1078 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001079 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001080
Andrii Kulian7318d632016-07-20 18:59:28 -07001081 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001082 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1084 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001085 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001086 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001087 if (r != null) {
1088 return r;
1089 }
1090 }
1091 }
1092 return null;
1093 }
1094
Dianne Hackborn09233282014-04-30 11:33:59 -07001095 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001096 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001097 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1098 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001099 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001100 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001101 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001102 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1103 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001104 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001105 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001106 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001107 }
1108 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001109
1110 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1111 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1112 while (maxNum > 0) {
1113 long mostRecentActiveTime = Long.MIN_VALUE;
1114 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001115 final int numTaskLists = runningTaskLists.size();
1116 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1117 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001118 if (!stackTaskList.isEmpty()) {
1119 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1120 if (lastActiveTime > mostRecentActiveTime) {
1121 mostRecentActiveTime = lastActiveTime;
1122 selectedStackList = stackTaskList;
1123 }
1124 }
1125 }
1126 if (selectedStackList != null) {
1127 list.add(selectedStackList.remove(0));
1128 --maxNum;
1129 } else {
1130 break;
1131 }
1132 }
Craig Mautner20e72272013-04-01 13:45:53 -07001133 }
1134
Todd Kennedy7440f172015-12-09 14:31:22 -08001135 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1136 ProfilerInfo profilerInfo) {
1137 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001138 if (aInfo != null) {
1139 // Store the found target back into the intent, because now that
1140 // we have it we never want to do this again. For example, if the
1141 // user navigates back to this point in the history, we should
1142 // always restart the exact same activity.
1143 intent.setComponent(new ComponentName(
1144 aInfo.applicationInfo.packageName, aInfo.name));
1145
1146 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001147 if (!aInfo.processName.equals("system")) {
1148 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001149 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001150 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001151
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001152 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1153 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1154 }
1155
Man Caocfa78b22015-06-11 20:14:34 -07001156 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1157 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1158 }
1159
1160 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001161 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001162 }
1163 }
1164 }
1165 return aInfo;
1166 }
1167
Todd Kennedy7440f172015-12-09 14:31:22 -08001168 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001169 return resolveIntent(intent, resolvedType, userId, 0);
1170 }
1171
1172 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001173 try {
1174 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001175 PackageManager.MATCH_DEFAULT_ONLY | flags
1176 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001177 } catch (RemoteException e) {
1178 }
1179 return null;
1180 }
1181
1182 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1183 ProfilerInfo profilerInfo, int userId) {
1184 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1185 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1186 }
1187
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001188 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1189 boolean andResume, boolean checkConfig) throws RemoteException {
1190
1191 if (!allPausedActivitiesComplete()) {
1192 // While there are activities pausing we skipping starting any new activities until
1193 // pauses are complete. NOTE: that we also do this for activities that are starting in
1194 // the paused state because they will first be resumed then paused on the client side.
1195 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1196 "realStartActivityLocked: Skipping start of r=" + r
1197 + " some activities pausing...");
1198 return false;
1199 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001200
Craig Mautner2568c3a2015-03-26 14:22:34 -07001201 if (andResume) {
1202 r.startFreezingScreenLocked(app, 0);
1203 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204
Craig Mautner2568c3a2015-03-26 14:22:34 -07001205 // schedule launch ticks to collect information about slow apps.
1206 r.startLaunchTickingLocked();
1207 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001209 // Have the window manager re-evaluate the orientation of the screen based on the new
1210 // activity order. Note that as a result of this, it can call back into the activity
1211 // manager with a new orientation. We don't care about that, because the activity is not
1212 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001213 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001214 final int displayId = r.getDisplayId();
1215 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1216 getDisplayOverrideConfiguration(displayId),
1217 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001218 // Deferring resume here because we're going to launch new activity shortly.
1219 // We don't want to perform a redundant launch of the same record while ensuring
1220 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001221 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1222 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 }
1224
1225 r.app = app;
1226 app.waitingToKill = null;
1227 r.launchCount++;
1228 r.lastLaunchTime = SystemClock.uptimeMillis();
1229
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001230 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231
1232 int idx = app.activities.indexOf(r);
1233 if (idx < 0) {
1234 app.activities.add(r);
1235 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001236 mService.updateLruProcessLocked(app, true, null);
1237 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001238
Craig Mautner15df08a2015-04-01 12:17:18 -07001239 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001240 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1241 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001242 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 }
1244
Andrii Kulian02b7a832016-10-06 23:11:56 -07001245 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001246 try {
1247 if (app.thread == null) {
1248 throw new RemoteException();
1249 }
1250 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001251 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 if (andResume) {
1253 results = r.results;
1254 newIntents = r.newIntents;
1255 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001256 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1257 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1258 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 if (andResume) {
1260 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1261 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001262 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001264 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001265 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001266 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001268 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1269 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 r.sleeping = false;
1271 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001272 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 mService.showAskCompatModeDialogLocked(r);
1274 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001275 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1277 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1278 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001279 final String profileFile = mService.mProfileFile;
1280 if (profileFile != null) {
1281 ParcelFileDescriptor profileFd = mService.mProfileFd;
1282 if (profileFd != null) {
1283 try {
1284 profileFd = profileFd.dup();
1285 } catch (IOException e) {
1286 if (profileFd != null) {
1287 try {
1288 profileFd.close();
1289 } catch (IOException o) {
1290 }
1291 profileFd = null;
1292 }
1293 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001295
1296 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1297 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 }
1299 }
1300 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001301
Craig Mautner2568c3a2015-03-26 14:22:34 -07001302 if (andResume) {
1303 app.hasShownUi = true;
1304 app.pendingUiClean = true;
1305 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001306 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001307 // Because we could be starting an Activity in the system process this may not go across
1308 // a Binder interface which would create a new Configuration. Consequently we have to
1309 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001310 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001311 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001312 new Configuration(mService.getGlobalConfiguration()),
1313 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1314 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1315 r.persistentState, results, newIntents, !andResume,
1316 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001317
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001318 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 // This may be a heavy-weight process! Note that the package
1320 // manager will ensure that only activity can run in the main
1321 // process of the .apk, which is the only thing that will be
1322 // considered heavy-weight.
1323 if (app.processName.equals(app.info.packageName)) {
1324 if (mService.mHeavyWeightProcess != null
1325 && mService.mHeavyWeightProcess != app) {
1326 Slog.w(TAG, "Starting new heavy weight process " + app
1327 + " when already running "
1328 + mService.mHeavyWeightProcess);
1329 }
1330 mService.mHeavyWeightProcess = app;
1331 Message msg = mService.mHandler.obtainMessage(
1332 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1333 msg.obj = r;
1334 mService.mHandler.sendMessage(msg);
1335 }
1336 }
1337
1338 } catch (RemoteException e) {
1339 if (r.launchFailed) {
1340 // This is the second time we failed -- finish activity
1341 // and give up.
1342 Slog.e(TAG, "Second failure launching "
1343 + r.intent.getComponent().flattenToShortString()
1344 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001345 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001346 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1347 "2nd-crash", false);
1348 return false;
1349 }
1350
1351 // This is the first time we failed -- restart process and
1352 // retry.
1353 app.activities.remove(r);
1354 throw e;
1355 }
1356
1357 r.launchFailed = false;
1358 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001359 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001360 }
1361
1362 if (andResume) {
1363 // As part of the process of launching, ActivityThread also performs
1364 // a resume.
1365 stack.minimalResumeActivityLocked(r);
1366 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001367 // This activity is not starting in the resumed state... which should look like we asked
1368 // it to pause+stop (but remain visible), and it has done so and reported back the
1369 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001370 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001371 "Moving to PAUSED: " + r + " (starting in paused state)");
1372 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001373 }
1374
1375 // Launch the new version setup screen if needed. We do this -after-
1376 // launching the initial activity (that is, home), so that it can have
1377 // a chance to initialize itself while in the background, making the
1378 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001379 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 mService.startSetupActivityLocked();
1381 }
1382
Dianne Hackborn465fa392014-09-14 14:21:18 -07001383 // Update any services we are bound to that might care about whether
1384 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001385 if (r.app != null) {
1386 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1387 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001388
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 return true;
1390 }
1391
Craig Mautnere79d42682013-04-01 19:01:53 -07001392 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001393 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001394 // Is this activity's application already running?
1395 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001396 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001397
Andrii Kulian02b7a832016-10-06 23:11:56 -07001398 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001399
1400 if (app != null && app.thread != null) {
1401 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001402 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1403 || !"android".equals(r.info.packageName)) {
1404 // Don't add this if it is a platform component that is marked
1405 // to run in multiple processes, because this is actually
1406 // part of the framework so doesn't make sense to track as a
1407 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001408 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1409 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001410 }
George Mount2c92c972014-03-20 09:38:23 -07001411 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001412 return;
1413 } catch (RemoteException e) {
1414 Slog.w(TAG, "Exception when starting activity "
1415 + r.intent.getComponent().flattenToShortString(), e);
1416 }
1417
1418 // If a dead object exception was thrown -- fall through to
1419 // restart the application.
1420 }
1421
1422 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001423 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001424 }
1425
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001426 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001427 String resultWho, int requestCode, int callingPid, int callingUid,
1428 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001429 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001430 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1431 callingUid);
1432 if (startAnyPerm == PERMISSION_GRANTED) {
1433 return true;
1434 }
1435 final int componentRestriction = getComponentRestrictionForCallingPackage(
1436 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1437 final int actionRestriction = getActionRestrictionForCallingPackage(
1438 intent.getAction(), callingPackage, callingPid, callingUid);
1439 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1440 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1441 if (resultRecord != null) {
1442 resultStack.sendActivityResultLocked(-1,
1443 resultRecord, resultWho, requestCode,
1444 Activity.RESULT_CANCELED, null);
1445 }
1446 final String msg;
1447 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1448 msg = "Permission Denial: starting " + intent.toString()
1449 + " from " + callerApp + " (pid=" + callingPid
1450 + ", uid=" + callingUid + ")" + " with revoked permission "
1451 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1452 } else if (!aInfo.exported) {
1453 msg = "Permission Denial: starting " + intent.toString()
1454 + " from " + callerApp + " (pid=" + callingPid
1455 + ", uid=" + callingUid + ")"
1456 + " not exported from uid " + aInfo.applicationInfo.uid;
1457 } else {
1458 msg = "Permission Denial: starting " + intent.toString()
1459 + " from " + callerApp + " (pid=" + callingPid
1460 + ", uid=" + callingUid + ")"
1461 + " requires " + aInfo.permission;
1462 }
1463 Slog.w(TAG, msg);
1464 throw new SecurityException(msg);
1465 }
1466
1467 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1468 final String message = "Appop Denial: starting " + intent.toString()
1469 + " from " + callerApp + " (pid=" + callingPid
1470 + ", uid=" + callingUid + ")"
1471 + " requires " + AppOpsManager.permissionToOp(
1472 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1473 Slog.w(TAG, message);
1474 return false;
1475 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1476 final String message = "Appop Denial: starting " + intent.toString()
1477 + " from " + callerApp + " (pid=" + callingPid
1478 + ", uid=" + callingUid + ")"
1479 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1480 Slog.w(TAG, message);
1481 return false;
1482 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001483 if (options != null && options.getLaunchTaskId() != -1) {
1484 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1485 callingPid, callingUid);
1486 if (startInTaskPerm != PERMISSION_GRANTED) {
1487 final String msg = "Permission Denial: starting " + intent.toString()
1488 + " from " + callerApp + " (pid=" + callingPid
1489 + ", uid=" + callingUid + ") with launchTaskId="
1490 + options.getLaunchTaskId();
1491 Slog.w(TAG, msg);
1492 throw new SecurityException(msg);
1493 }
1494 }
1495
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001496 return true;
1497 }
1498
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001499 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001500 final long identity = Binder.clearCallingIdentity();
1501 try {
1502 return UserManager.get(mService.mContext).getUserInfo(userId);
1503 } finally {
1504 Binder.restoreCallingIdentity(identity);
1505 }
1506 }
1507
Svet Ganov99b60432015-06-27 13:15:22 -07001508 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001509 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001510 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1511 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001512 == PackageManager.PERMISSION_DENIED) {
1513 return ACTIVITY_RESTRICTION_PERMISSION;
1514 }
1515
Christopher Tateff7add02015-08-17 10:23:22 -07001516 if (activityInfo.permission == null) {
1517 return ACTIVITY_RESTRICTION_NONE;
1518 }
1519
Svet Ganov99b60432015-06-27 13:15:22 -07001520 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1521 if (opCode == AppOpsManager.OP_NONE) {
1522 return ACTIVITY_RESTRICTION_NONE;
1523 }
1524
1525 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1526 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001527 if (!ignoreTargetSecurity) {
1528 return ACTIVITY_RESTRICTION_APPOP;
1529 }
Svet Ganov99b60432015-06-27 13:15:22 -07001530 }
1531
1532 return ACTIVITY_RESTRICTION_NONE;
1533 }
1534
Svetoslav7008b512015-06-24 18:47:07 -07001535 private int getActionRestrictionForCallingPackage(String action,
1536 String callingPackage, int callingPid, int callingUid) {
1537 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001538 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001539 }
1540
1541 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1542 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001543 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001544 }
1545
1546 final PackageInfo packageInfo;
1547 try {
1548 packageInfo = mService.mContext.getPackageManager()
1549 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1550 } catch (PackageManager.NameNotFoundException e) {
1551 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001552 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001553 }
1554
1555 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001556 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001557 }
1558
1559 if (mService.checkPermission(permission, callingPid, callingUid) ==
1560 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001561 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001562 }
1563
1564 final int opCode = AppOpsManager.permissionToOpCode(permission);
1565 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001566 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001567 }
1568
1569 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1570 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001571 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001572 }
1573
Svet Ganov99b60432015-06-27 13:15:22 -07001574 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001575 }
1576
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001577 void setLaunchSource(int uid) {
1578 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1579 }
1580
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001581 void acquireLaunchWakelock() {
1582 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1583 throw new IllegalStateException("Calling must be system uid");
1584 }
1585 mLaunchingActivity.acquire();
1586 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1587 // To be safe, don't allow the wake lock to be held for too long.
1588 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1589 }
1590 }
1591
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001592 /**
1593 * Called when the frontmost task is idle.
1594 * @return the state of mService.mBooting before this was called.
1595 */
1596 private boolean checkFinishBootingLocked() {
1597 final boolean booting = mService.mBooting;
1598 boolean enableScreen = false;
1599 mService.mBooting = false;
1600 if (!mService.mBooted) {
1601 mService.mBooted = true;
1602 enableScreen = true;
1603 }
1604 if (booting || enableScreen) {
1605 mService.postFinishBooting(booting, enableScreen);
1606 }
1607 return booting;
1608 }
1609
Craig Mautnerf3333272013-04-22 10:55:53 -07001610 // Checked.
1611 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1612 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001613 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001614
Craig Mautnerf3333272013-04-22 10:55:53 -07001615 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001616 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001617 int NS = 0;
1618 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001619 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 boolean activityRemoved = false;
1621
Wale Ogunwale7d701172015-03-11 15:36:30 -07001622 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001623 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001624 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1625 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001626 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1627 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001628 if (fromTimeout) {
1629 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001630 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001631
1632 // This is a hack to semi-deal with a race condition
1633 // in the client where it can be constructed with a
1634 // newer configuration from when we asked it to launch.
1635 // We'll update with whatever configuration it now says
1636 // it used to launch.
1637 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001638 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001639 }
1640
1641 // We are now idle. If someone is waiting for a thumbnail from
1642 // us, we can now deliver.
1643 r.idle = true;
1644
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001645 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001646 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001647 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001648 }
1649 }
1650
1651 if (allResumedActivitiesIdle()) {
1652 if (r != null) {
1653 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001654 }
1655
1656 if (mLaunchingActivity.isHeld()) {
1657 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1658 if (VALIDATE_WAKE_LOCK_CALLER &&
1659 Binder.getCallingUid() != Process.myUid()) {
1660 throw new IllegalStateException("Calling must be system uid");
1661 }
1662 mLaunchingActivity.release();
1663 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001664 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001665 }
1666
1667 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001668 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001669 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001670 if ((NF = mFinishingActivities.size()) > 0) {
1671 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001672 mFinishingActivities.clear();
1673 }
1674
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001676 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001677 mStartingUsers.clear();
1678 }
1679
Craig Mautnerf3333272013-04-22 10:55:53 -07001680 // Stop any activities that are scheduled to do so but have been
1681 // waiting for the next one to start.
1682 for (int i = 0; i < NS; i++) {
1683 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001684 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001685 if (stack != null) {
1686 if (r.finishing) {
1687 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1688 } else {
1689 stack.stopActivityLocked(r);
1690 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001691 }
1692 }
1693
1694 // Finish any activities that are scheduled to do so but have been
1695 // waiting for the next one to start.
1696 for (int i = 0; i < NF; i++) {
1697 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001698 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001699 if (stack != null) {
1700 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1701 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001702 }
1703
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001704 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001705 // Complete user switch
1706 if (startingUsers != null) {
1707 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001708 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001709 }
1710 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001711 }
1712
1713 mService.trimApplications();
1714 //dump();
1715 //mWindowManager.dump();
1716
Craig Mautnerf3333272013-04-22 10:55:53 -07001717 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001718 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001719 }
1720
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001721 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001722 }
1723
Craig Mautner8e569572013-10-11 17:36:59 -07001724 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001725 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1729 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1730 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001731 }
Craig Mautner19091252013-10-05 00:03:53 -07001732 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001733 }
1734
1735 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1737 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1739 stacks.get(stackNdx).closeSystemDialogsLocked();
1740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001741 }
1742 }
1743
Craig Mautner93529a42013-10-04 15:03:13 -07001744 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001745 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001746 }
1747
Craig Mautner8d341ef2013-03-26 09:03:27 -07001748 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001749 * Update the last used stack id for non-current user (current user's last
1750 * used stack is the focused stack)
1751 */
1752 void updateUserStackLocked(int userId, ActivityStack stack) {
1753 if (userId != mCurrentUser) {
1754 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1755 }
1756 }
1757
1758 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001759 * @return true if some activity was finished (or would have finished if doit were true).
1760 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001761 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1762 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001763 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001767 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001768 if (stack.finishDisabledPackageActivitiesLocked(
1769 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001770 didSomething = true;
1771 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001772 }
1773 }
1774 return didSomething;
1775 }
1776
Dianne Hackborna413dc02013-07-12 12:02:55 -07001777 void updatePreviousProcessLocked(ActivityRecord r) {
1778 // Now that this process has stopped, we may want to consider
1779 // it to be the previous app to try to keep around in case
1780 // the user wants to return to it.
1781
1782 // First, found out what is currently the foreground app, so that
1783 // we don't blow away the previous app if this activity is being
1784 // hosted by the process that is actually still the foreground.
1785 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001786 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1787 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1789 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001790 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001791 if (stack.mResumedActivity != null) {
1792 fgApp = stack.mResumedActivity.app;
1793 } else if (stack.mPausingActivity != null) {
1794 fgApp = stack.mPausingActivity.app;
1795 }
1796 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001797 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001798 }
1799 }
1800
1801 // Now set this one as the previous process, only if that really
1802 // makes sense to.
1803 if (r.app != null && fgApp != null && r.app != fgApp
1804 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001805 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001806 mService.mPreviousProcess = r.app;
1807 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1808 }
1809 }
1810
Wale Ogunwaled046a012015-12-24 13:05:59 -08001811 boolean resumeFocusedStackTopActivityLocked() {
1812 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001813 }
1814
Wale Ogunwaled046a012015-12-24 13:05:59 -08001815 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001816 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001817 if (targetStack != null && isFocusedStack(targetStack)) {
1818 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001819 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001820 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1821 if (r == null || r.state != RESUMED) {
1822 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1823 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001824 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001825 }
1826
Todd Kennedy39bfee52016-02-24 10:28:21 -08001827 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1829 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1831 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1832 }
1833 }
1834 }
1835
Adrian Roos20d7df32016-01-12 18:59:43 +01001836 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1837 TaskRecord finishedTask = null;
1838 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1840 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001841 final int numStacks = stacks.size();
1842 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1843 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001844 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1845 if (stack == focusedStack || finishedTask == null) {
1846 finishedTask = t;
1847 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001848 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001849 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001850 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001851 }
1852
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001853 void finishVoiceTask(IVoiceInteractionSession session) {
1854 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1855 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1856 final int numStacks = stacks.size();
1857 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1858 final ActivityStack stack = stacks.get(stackNdx);
1859 stack.finishVoiceTask(session);
1860 }
1861 }
1862 }
1863
Andrii Kulianc27916642016-04-12 17:59:27 -07001864 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1865 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001866 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1867 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001868 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001869 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1870 // Caller wants the home activity moved with it. To accomplish this,
1871 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001872 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001873 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001874 ActivityStack currentStack = task.getStack();
1875 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001876 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1877 + task + " to front. Stack is null");
1878 return;
1879 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001880
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001881 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001882 int stackId = options.getLaunchStackId();
1883 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001884 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001885 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001886 if (stackId == INVALID_STACK_ID) {
1887 stackId = task.getLaunchStackId();
1888 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001889 if (stackId != currentStack.mStackId) {
1890 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1891 !FORCE_FOCUS, reason);
1892 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001893 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1894 // still need moveTaskToFrontLocked() below for any transition settings.
1895 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001896 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1897 resizeStackLocked(stackId, bounds,
1898 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001899 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001900 } else {
1901 // WM resizeTask must be done after the task is moved to the correct stack,
1902 // because Task's setBounds() also updates dim layer's bounds, but that has
1903 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001904 mWindowManager.resizeTask(task.taskId, task.mBounds,
1905 task.getOverrideConfiguration(), false /* relayout */,
1906 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001907 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001908 }
1909 }
1910
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001911 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001912 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001913 r == null ? null : r.appTimeTracker, reason);
1914
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001915 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001916 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001917
Andrii Kulian02b7a832016-10-06 23:11:56 -07001918 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001919 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001920 }
1921
Wale Ogunwale854809c2015-12-27 16:18:19 -08001922 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001923 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001924 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001925 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001926 return false;
1927 }
1928 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1929 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001930 }
1931
Craig Mautner967212c2013-04-13 21:10:58 -07001932 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001933 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001934 }
1935
1936 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001937 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1938 if (activityContainer != null) {
1939 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001940 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001941 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001942 return null;
1943 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001944 // TODO(multi-display): Allow creating stacks on secondary displays.
Jorim Jaggife762342016-10-13 14:33:27 +02001945 return createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001946 }
1947
Craig Mautner967212c2013-04-13 21:10:58 -07001948 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001949 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001950 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1951 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001952 }
1953 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001954 }
1955
Jorim Jaggife762342016-10-13 14:33:27 +02001956 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
1957 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
1958 }
1959
Craig Mautneree2e45a2014-06-27 12:10:03 -07001960 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001961 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001962 }
1963
1964 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001965 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1966 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1967 final TaskRecord task = tasks.get(taskNdx);
1968 if (task.isHomeTask()) {
1969 final ArrayList<ActivityRecord> activities = task.mActivities;
1970 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1971 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001972 if (r.isHomeActivity()
1973 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001974 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001975 }
1976 }
1977 }
1978 }
1979 return null;
1980 }
1981
Chong Zhangb15758a2015-11-17 12:12:03 -08001982 /**
1983 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1984 * the docked stack itself, or if it's side-by-side to the docked stack.
1985 */
1986 boolean isStackDockedInEffect(int stackId) {
1987 return stackId == DOCKED_STACK_ID ||
1988 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1989 }
1990
Todd Kennedyca4d8422015-01-15 15:19:22 -08001991 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001992 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001993 ActivityContainer activityContainer =
1994 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001995 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001996 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1997 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001998 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001999 return activityContainer;
2000 }
2001
Craig Mautner34b73df2014-01-12 21:11:08 -08002002 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002003 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2004 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2005 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002006 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2007 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002008 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002009 }
2010 }
2011
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002012 void deleteActivityContainerRecord(int stackId) {
2013 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2014 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2015 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002016 }
2017
Jorim Jaggidc249c42015-12-15 14:57:31 -08002018 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002019 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002020 if (stackId == DOCKED_STACK_ID) {
2021 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002022 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002023 return;
2024 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002025 final ActivityStack stack = getStack(stackId);
2026 if (stack == null) {
2027 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2028 return;
2029 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002030
Jorim Jaggidc249c42015-12-15 14:57:31 -08002031 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002032 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2033 // stack size changing so things don't get out of sync.
2034 return;
2035 }
2036
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002037 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002038 mWindowManager.deferSurfaceLayout();
2039 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002040 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002041 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002042 stack.ensureVisibleActivitiesConfigurationLocked(
2043 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002044 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002045 } finally {
2046 mWindowManager.continueSurfaceLayout();
2047 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002048 }
2049 }
2050
Jorim Jaggi192086e2016-03-11 17:17:03 +01002051 void deferUpdateBounds(int stackId) {
2052 final ActivityStack stack = getStack(stackId);
2053 if (stack != null) {
2054 stack.deferUpdateBounds();
2055 }
2056 }
2057
2058 void continueUpdateBounds(int stackId) {
2059 final ActivityStack stack = getStack(stackId);
2060 if (stack != null) {
2061 stack.continueUpdateBounds();
2062 }
2063 }
2064
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002065 void notifyAppTransitionDone() {
2066 continueUpdateBounds(HOME_STACK_ID);
2067 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2068 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002069 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002070 mWindowManager.setTaskDockedResizing(taskId, false);
2071 }
2072 }
2073 mResizingTasksDuringAnimation.clear();
2074 }
2075
Jorim Jaggi192086e2016-03-11 17:17:03 +01002076 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002077 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002078 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002079
Jorim Jaggi192086e2016-03-11 17:17:03 +01002080 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2081 return;
2082 }
2083
Andrii Kulian1e32e022016-09-16 15:29:34 -07002084 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002085 }
2086
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002087 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2088 final ActivityStack stack = getStack(fromStackId);
2089 if (stack == null) {
2090 return;
2091 }
2092
2093 mWindowManager.deferSurfaceLayout();
2094 try {
2095 if (fromStackId == DOCKED_STACK_ID) {
2096
2097 // We are moving all tasks from the docked stack to the fullscreen stack,
2098 // which is dismissing the docked stack, so resize all other stacks to
2099 // fullscreen here already so we don't end up with resize trashing.
2100 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2101 if (StackId.isResizeableByDockedStack(i)) {
2102 ActivityStack otherStack = getStack(i);
2103 if (otherStack != null) {
2104 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2105 true /* allowResizeInDockedMode */, DEFER_RESUME);
2106 }
2107 }
2108 }
2109
2110 // Also disable docked stack resizing since we have manually adjusted the
2111 // size of other stacks above and we don't want to trigger a docked stack
2112 // resize when we remove task from it below and it is detached from the
2113 // display because it no longer contains any tasks.
2114 mAllowDockedStackResize = false;
2115 }
2116 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2117 final int size = tasks.size();
2118 if (onTop) {
2119 for (int i = 0; i < size; i++) {
2120 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002121 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002122 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002123 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002124
2125 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2126 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002127 } else {
2128 for (int i = size - 1; i >= 0; i--) {
2129 positionTaskInStackLocked(tasks.get(i).taskId,
2130 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2131 }
2132 }
2133 } finally {
2134 mAllowDockedStackResize = true;
2135 mWindowManager.continueSurfaceLayout();
2136 }
2137 }
2138
Jorim Jaggidc249c42015-12-15 14:57:31 -08002139 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002140 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2141 boolean preserveWindows) {
2142 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2143 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2144 false /* deferResume */);
2145 }
2146
2147 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2148 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2149 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002150
2151 if (!mAllowDockedStackResize) {
2152 // Docked stack resize currently disabled.
2153 return;
2154 }
2155
Jorim Jaggidc249c42015-12-15 14:57:31 -08002156 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2157 if (stack == null) {
2158 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2159 return;
2160 }
2161
2162 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2163 mWindowManager.deferSurfaceLayout();
2164 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002165 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2166 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 ActivityRecord r = stack.topRunningActivityLocked();
2168 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2169 tempDockedTaskInsetBounds);
2170
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002171 // TODO: Checking for isAttached might not be needed as if the user passes in null
2172 // dockedBounds then they want the docked stack to be dismissed.
2173 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2174 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002175 // In this case we make all other static stacks fullscreen and move all
2176 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002177 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178
2179 // stack shouldn't contain anymore activities, so nothing to resume.
2180 r = null;
2181 } else {
2182 // Docked stacks occupy a dedicated region on screen so the size of all other
2183 // static stacks need to be adjusted so they don't overlap with the docked stack.
2184 // We get the bounds to use from window manager which has been adjusted for any
2185 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002186 mWindowManager.getStackDockedModeBounds(
2187 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002188 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002189 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2190 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2191 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002192 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002193 }
2194 }
2195 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002196 if (!deferResume) {
2197 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2198 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002199 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002200 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002201 mWindowManager.continueSurfaceLayout();
2202 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2203 }
2204
2205 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2206 tempDockedTaskBounds != null
2207 || tempDockedTaskInsetBounds != null
2208 || tempOtherTaskBounds != null
2209 || tempOtherTaskInsetBounds != null);
2210 }
2211
Robert Carr0d00c2e2016-02-29 17:45:02 -08002212 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2213 final ActivityStack stack = getStack(PINNED_STACK_ID);
2214 if (stack == null) {
2215 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2216 return;
2217 }
2218 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2219 mWindowManager.deferSurfaceLayout();
2220 try {
2221 ActivityRecord r = stack.topRunningActivityLocked();
2222 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2223 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002224 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002225 } finally {
2226 mWindowManager.continueSurfaceLayout();
2227 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2228 }
2229 }
2230
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002231 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2232 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002233 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002234 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002235 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002236 }
2237
Chong Zhang87b21722015-09-21 15:39:51 -07002238 // If this is a forced resize, let it go through even if the bounds is not changing,
2239 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002240 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002241 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002242 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002243 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002244 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002245 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002246
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 if (!mWindowManager.isValidTaskId(task.taskId)) {
2248 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002249 // All we can do for now is update the bounds so it can be used when the task is
2250 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002251 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002252 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002253 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002254 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002255 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002256 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002257 }
2258
Chong Zhang6de2ae82015-09-30 18:25:21 -07002259 // Do not move the task to another stack here.
2260 // This method assumes that the task is already placed in the right stack.
2261 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002262
Chong Zhang6de2ae82015-09-30 18:25:21 -07002263 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002264
Andrii Kulian8072d112016-09-16 11:11:01 -07002265 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002266 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002267 // way and the activity was kept the way it was. If it's false, it means the activity had
2268 // to be relaunched due to configuration change.
2269 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002270 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002271 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002272 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002273 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002274
2275 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002276 // All other activities must be made visible with their correct configuration.
2277 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2278 if (!kept) {
2279 resumeFocusedStackTopActivityLocked();
2280 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002281 }
2282 }
2283 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002284 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2285 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002286
2287 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002288 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002289 }
2290
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002291 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002292 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2293 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002294 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002295 }
2296
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002297 ActivityContainer activityContainer = new ActivityContainer(stackId);
2298 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002299 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002300 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002301 }
2302
2303 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002304 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002305 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2306 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002307 break;
2308 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002309 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002310 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002311 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002312 }
2313
Chong Zhang5dcb2752015-08-18 13:50:26 -07002314 /**
2315 * Restores a recent task to a stack
2316 * @param task The recent task to be restored.
2317 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002318 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002319 * @return true if the task has been restored successfully.
2320 */
2321 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2322 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002323 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002324 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2325 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2326 // Put it in the fullscreen stack.
2327 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002328 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002329
Andrii Kulian02b7a832016-10-06 23:11:56 -07002330 final ActivityStack currentStack = task.getStack();
2331 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002332 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002333 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002334 // Nothing else to do since it is already restored in the right stack.
2335 return true;
2336 }
2337 // Remove current stack association, so we can re-associate the task with the
2338 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002339 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002340 }
2341
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002342 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002343 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002344
2345 if (stack == null) {
2346 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002347 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2348 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002349 return false;
2350 }
2351
Wale Ogunwale5f986092015-12-04 15:35:38 -08002352 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002353 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2354 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002355 final ArrayList<ActivityRecord> activities = task.mActivities;
2356 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002357 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002358 }
2359 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002360 }
2361
Wale Ogunwale040b4702015-08-06 18:10:50 -07002362 /**
2363 * Moves the specified task record to the input stack id.
2364 * WARNING: This method performs an unchecked/raw move of the task and
2365 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002366 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002367 * @param task Task to move.
2368 * @param stackId Id of stack to move task to.
2369 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002370 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002371 * @param reason Reason the task is been moved.
2372 * @return The stack the task was moved to.
2373 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002374 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002375 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002376
2377 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2378 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2379 + "support multi-window task=" + task + " to stackId=" + stackId);
2380 }
2381
Wale Ogunwale06579d62016-04-30 15:29:06 -07002382 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002383 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002384 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002385 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002386 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2387 // Whenever we are moving the top activity from the front stack we want to make sure to move
2388 // the stack to the front.
2389 final boolean wasFront = isFrontStack(prevStack)
2390 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002391
Chong Zhangd545dce2016-02-29 18:09:17 -08002392 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002393 // We don't allow moving a unresizeable task to the docked stack since the docked
2394 // stack is used for split-screen mode and will cause things like the docked divider to
2395 // show up. We instead leave the task in its current stack or move it to the fullscreen
2396 // stack if it isn't currently in a stack.
2397 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002398 Slog.w(TAG, "Can not move unresizeable task=" + task
2399 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2400 }
2401
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002402 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2403 // if a docked stack is created below which will lead to the stack we are moving from and
2404 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002405 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002406 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002407 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002408 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002409 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002410
2411 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002412 // move focus to the new stack by moving the stack to the front.
2413 stack.moveToFrontAndResumeStateIfNeeded(
2414 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002415
Wale Ogunwale040b4702015-08-06 18:10:50 -07002416 return stack;
2417 }
2418
Chong Zhange4fbd322016-03-01 14:44:03 -08002419 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002420 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002421 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2422 false /* deferResume */);
2423 }
2424
2425 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2426 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002427 final TaskRecord task = anyTaskForIdLocked(taskId);
2428 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002429 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002430 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002431 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002432
Andrii Kulian02b7a832016-10-06 23:11:56 -07002433 final ActivityStack currentStack = task.getStack();
2434 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002435 // You are already in the right stack silly...
2436 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002437 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002438 }
2439
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002440 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2441 throw new IllegalArgumentException("moveTaskToStack:"
2442 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2443 }
2444
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002445 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002446 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002447 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002448 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002449 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002450 // We are about to relaunch the activity because its configuration changed due to
2451 // being maximized, i.e. size change. The activity will first remove the old window
2452 // and then add a new one. This call will tell window manager about this, so it can
2453 // preserve the old window until the new one is drawn. This prevents having a gap
2454 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002455 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002456 // Note here we always set the replacing window first, as the flags might be needed
2457 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002458 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002459 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002460
2461 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002462 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002463 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002464 try {
2465 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002466 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002467 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002468
Wale Ogunwale961f4852016-02-01 20:25:54 -08002469 if (!animate) {
2470 stack.mNoAnimActivities.add(topActivity);
2471 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002472
Wale Ogunwale961f4852016-02-01 20:25:54 -08002473 // We might trigger a configuration change. Save the current task bounds for freezing.
2474 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2475
2476 // Make sure the task has the appropriate bounds/size for the stack it is in.
2477 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002478 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2479 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002480 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2481 Rect bounds = task.getLaunchBounds();
2482 if (bounds == null) {
2483 stack.layoutTaskInStack(task, null);
2484 bounds = task.mBounds;
2485 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002486 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2487 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002488 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002489 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2490 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002491 }
2492 } finally {
2493 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002494 }
2495
2496 if (mightReplaceWindow) {
2497 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2498 // window), we need to clear the replace window settings. Otherwise, we schedule a
2499 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002500 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002501 }
2502
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002503 if (!deferResume) {
2504
2505 // The task might have already been running and its visibility needs to be synchronized with
2506 // the visibility of the stack / windows.
2507 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2508 resumeFocusedStackTopActivityLocked();
2509 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002510
Jorim Jaggid53f0922016-04-06 22:16:23 -07002511 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002512
2513 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002514 }
2515
Wale Ogunwale079a0042015-10-24 11:44:07 -07002516 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2517 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2518 if (stack == null) {
2519 throw new IllegalArgumentException(
2520 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2521 }
2522
2523 final ActivityRecord r = stack.topRunningActivityLocked();
2524 if (r == null) {
2525 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2526 + " in stack=" + stack);
2527 return false;
2528 }
2529
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002530 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002531 Slog.w(TAG,
2532 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002533 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002534 return false;
2535 }
2536
Wale Ogunwale480dca02016-02-06 13:58:29 -08002537 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002538 return true;
2539 }
2540
Wale Ogunwale480dca02016-02-06 13:58:29 -08002541 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2542 mWindowManager.deferSurfaceLayout();
2543 try {
2544 final TaskRecord task = r.task;
2545
Andrii Kulian02b7a832016-10-06 23:11:56 -07002546 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002547 // An activity can't be pinned and visible behind at the same time. Go ahead and
2548 // release it from been visible behind before pinning.
2549 requestVisibleBehindLocked(r, false);
2550 }
2551
Wale Ogunwale480dca02016-02-06 13:58:29 -08002552 // Need to make sure the pinned stack exist so we can resize it below...
2553 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2554
2555 // Resize the pinned stack to match the current size of the task the activity we are
2556 // going to be moving is currently contained in. We do this to have the right starting
2557 // animation bounds for the pinned stack to the desired bounds the caller wants.
2558 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2559 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002560 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002561
2562 if (task.mActivities.size() == 1) {
2563 // There is only one activity in the task. So, we can just move the task over to
2564 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002565 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2566 // Move the home stack forward if the task we just moved to the pinned stack
2567 // was launched from home so home should be visible behind it.
2568 moveHomeStackToFront(reason);
2569 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002570 moveTaskToStackLocked(
2571 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2572 } else {
2573 stack.moveActivityToStack(r);
2574 }
2575 } finally {
2576 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002577 }
2578
Wale Ogunwale480dca02016-02-06 13:58:29 -08002579 // The task might have already been running and its visibility needs to be synchronized
2580 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002581 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002582 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002583
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002584 mWindowManager.animateResizePinnedStack(bounds, -1);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002585 mService.mTaskChangeNotificationController.notifyActivityPinned();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002586 }
2587
Chong Zhang6cda19c2016-06-14 19:07:56 -07002588 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2589 if (r == null || !r.isFocusable()) {
2590 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2591 "moveActivityStackToFront: unfocusable r=" + r);
2592 return false;
2593 }
2594
2595 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002596 final ActivityStack stack = r.getStack();
2597 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002598 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2599 + r + " task=" + task);
2600 return false;
2601 }
2602
Chong Zhang6cda19c2016-06-14 19:07:56 -07002603 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2604 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2605 "moveActivityStackToFront: already on top, r=" + r);
2606 return false;
2607 }
2608
2609 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2610 "moveActivityStackToFront: r=" + r);
2611
2612 stack.moveToFront(reason, task);
2613 return true;
2614 }
2615
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002616 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2617 final TaskRecord task = anyTaskForIdLocked(taskId);
2618 if (task == null) {
2619 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2620 return;
2621 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002622 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2623
2624 task.updateOverrideConfigurationForStack(stack);
2625
2626 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002627 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002628 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002629 // The task might have already been running and its visibility needs to be synchronized with
2630 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002631 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002632 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002633 }
2634
Craig Mautnerac6f8432013-07-17 13:24:59 -07002635 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002636 mTmpFindTaskResult.r = null;
2637 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2640 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2642 final ActivityStack stack = stacks.get(stackNdx);
2643 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002644 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002645 continue;
2646 }
2647 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002648 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2649 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002650 continue;
2651 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002652 stack.findTaskLocked(r, mTmpFindTaskResult);
2653 // It is possible to have task in multiple stacks with the same root affinity.
2654 // If the match we found was based on root affinity we keep on looking to see if
2655 // there is a better match in another stack. We eventually return the match based
2656 // on root affinity if we don't find a better match.
2657 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2658 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002659 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002660 }
2661 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002662 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2663 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002664 }
2665
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002666 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2667 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002671 final ActivityRecord ar = stacks.get(stackNdx)
2672 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 if (ar != null) {
2674 return ar;
2675 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002676 }
2677 }
2678 return null;
2679 }
2680
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002682 scheduleSleepTimeout();
2683 if (!mGoingToSleep.isHeld()) {
2684 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002685 if (mLaunchingActivity.isHeld()) {
2686 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2687 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002688 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002689 mLaunchingActivity.release();
2690 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002692 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002693 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002694 }
2695
2696 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002697 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002698
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002699 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002700 final long endTime = System.currentTimeMillis() + timeout;
2701 while (true) {
2702 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2706 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2707 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002708 }
2709 if (cantShutdown) {
2710 long timeRemaining = endTime - System.currentTimeMillis();
2711 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002713 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002714 } catch (InterruptedException e) {
2715 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002716 } else {
2717 Slog.w(TAG, "Activity manager shutdown timed out");
2718 timedout = true;
2719 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002721 } else {
2722 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002723 }
2724 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002725
2726 // Force checkReadyForSleep to complete.
2727 mSleepTimeout = true;
2728 checkReadyForSleepLocked();
2729
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 return timedout;
2731 }
2732
2733 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 removeSleepTimeouts();
2735 if (mGoingToSleep.isHeld()) {
2736 mGoingToSleep.release();
2737 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002738 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2739 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002740 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2741 final ActivityStack stack = stacks.get(stackNdx);
2742 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002743 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002744 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002745 }
Craig Mautner5314a402013-09-26 12:40:16 -07002746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002748 mGoingToSleepActivities.clear();
2749 }
2750
2751 void activitySleptLocked(ActivityRecord r) {
2752 mGoingToSleepActivities.remove(r);
2753 checkReadyForSleepLocked();
2754 }
2755
2756 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002757 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002758 // Do not care.
2759 return;
2760 }
2761
2762 if (!mSleepTimeout) {
2763 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2767 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2768 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002769 }
2770
2771 if (mStoppingActivities.size() > 0) {
2772 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002773 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002774 + mStoppingActivities.size() + " activities");
2775 scheduleIdleLocked();
2776 dontSleep = true;
2777 }
2778
2779 if (mGoingToSleepActivities.size() > 0) {
2780 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002781 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002782 + mGoingToSleepActivities.size() + " activities");
2783 dontSleep = true;
2784 }
2785
2786 if (dontSleep) {
2787 return;
2788 }
2789 }
2790
Wei Wang65c7a152016-06-02 18:51:22 -07002791 // Send launch end powerhint before going sleep
2792 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2793
Craig Mautnere0a38842013-12-16 16:14:02 -08002794 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2795 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002796 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2797 stacks.get(stackNdx).goToSleep();
2798 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002799 }
2800
2801 removeSleepTimeouts();
2802
2803 if (mGoingToSleep.isHeld()) {
2804 mGoingToSleep.release();
2805 }
2806 if (mService.mShuttingDown) {
2807 mService.notifyAll();
2808 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002811 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002812 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002813 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002814 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002815 }
2816 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002817 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002818 mWindowManager.executeAppTransition();
2819 return true;
2820 }
2821 return false;
2822 }
2823
Craig Mautner8d341ef2013-03-26 09:03:27 -07002824 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2826 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002827 int stackNdx = stacks.size() - 1;
2828 while (stackNdx >= 0) {
2829 stacks.get(stackNdx).handleAppCrashLocked(app);
2830 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002831 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002832 }
2833 }
2834
Jose Lima4b6c6692014-08-12 17:41:12 -07002835 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002836 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002837 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002838 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2839 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002840 return false;
2841 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002842
2843 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2844 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2845 + " visible=" + visible + " stackId=" + stack.mStackId
2846 + " can't contain visible behind activities");
2847 return false;
2848 }
2849
Jose Lima4b6c6692014-08-12 17:41:12 -07002850 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002851 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2852 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002853
2854 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002855 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002856 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002857 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002858 return true;
2859 }
2860
2861 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002862 if (visible && top.fullscreen) {
2863 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002864 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2865 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2866 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2867 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002869 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2870 // Only the activity set as currently visible behind should actively reset its
2871 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002872 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2873 "requestVisibleBehind: returning visible=" + visible
2874 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2875 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002876 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002877 }
2878
Jose Lima4b6c6692014-08-12 17:41:12 -07002879 stack.setVisibleBehindActivity(visible ? r : null);
2880 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002881 // If there is a translucent home activity, we need to force it stop being translucent,
2882 // because we can't depend on the application to necessarily perform that operation.
2883 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002884 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002885 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002886 mService.convertFromTranslucent(next.appToken);
2887 }
2888 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002889 if (top.app != null && top.app.thread != null) {
2890 // Notify the top app of the change.
2891 try {
2892 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2893 } catch (RemoteException e) {
2894 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002895 }
2896 return true;
2897 }
2898
Craig Mautnerbb742462014-07-07 15:28:55 -07002899 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002900 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002901 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002902 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002903
2904 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002905 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002906 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07002907 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Craig Mautnerbb742462014-07-07 15:28:55 -07002908 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002909
2910 // When launching tasks behind, update the last active time of the top task after the new
2911 // task has been shown briefly
2912 final ActivityRecord top = stack.topActivity();
2913 if (top != null) {
2914 top.task.touchActiveTime();
2915 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002916 }
2917
2918 void scheduleLaunchTaskBehindComplete(IBinder token) {
2919 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2920 }
2921
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002922 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2923 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02002924 mKeyguardController.beginActivityVisibilityUpdate();
2925 try {
2926 // First the front stacks. In case any are not fullscreen and are in front of home.
2927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2929 final int topStackNdx = stacks.size() - 1;
2930 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2931 final ActivityStack stack = stacks.get(stackNdx);
2932 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
2933 }
Craig Mautner580ea812013-04-25 12:58:38 -07002934 }
Jorim Jaggife762342016-10-13 14:33:27 +02002935 } finally {
2936 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002938 }
2939
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002940 void invalidateTaskLayers() {
2941 mTaskLayersChanged = true;
2942 }
2943
2944 void rankTaskLayersIfNeeded() {
2945 if (!mTaskLayersChanged) {
2946 return;
2947 }
2948 mTaskLayersChanged = false;
2949 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2951 int baseLayer = 0;
2952 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2953 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2954 }
2955 }
2956 }
2957
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002958 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2959 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2960 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2961 final int topStackNdx = stacks.size() - 1;
2962 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2963 final ActivityStack stack = stacks.get(stackNdx);
2964 stack.clearOtherAppTimeTrackers(except);
2965 }
2966 }
2967 }
2968
Craig Mautner8d341ef2013-03-26 09:03:27 -07002969 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2971 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002972 final int numStacks = stacks.size();
2973 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2974 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002975 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002976 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 }
2978 }
2979
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002980 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2981 // Examine all activities currently running in the process.
2982 TaskRecord firstTask = null;
2983 // Tasks is non-null only if two or more tasks are found.
2984 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002985 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2986 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002987 ActivityRecord r = app.activities.get(i);
2988 // First, if we find an activity that is in the process of being destroyed,
2989 // then we just aren't going to do anything for now; we want things to settle
2990 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002991 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002992 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002993 return;
2994 }
2995 // Don't consider any activies that are currently not in a state where they
2996 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002997 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2998 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002999 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003000 continue;
3001 }
3002 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003003 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003004 + " from " + r);
3005 if (firstTask == null) {
3006 firstTask = r.task;
3007 } else if (firstTask != r.task) {
3008 if (tasks == null) {
3009 tasks = new ArraySet<>();
3010 tasks.add(firstTask);
3011 }
3012 tasks.add(r.task);
3013 }
3014 }
3015 }
3016 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003017 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003018 return;
3019 }
3020 // If we have activities in multiple tasks that are in a position to be destroyed,
3021 // let's iterate through the tasks and release the oldest one.
3022 final int numDisplays = mActivityDisplays.size();
3023 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3024 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3025 // Step through all stacks starting from behind, to hit the oldest things first.
3026 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3027 final ActivityStack stack = stacks.get(stackNdx);
3028 // Try to release activities in this stack; if we manage to, we are done.
3029 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3030 return;
3031 }
3032 }
3033 }
3034 }
3035
Amith Yamasani37a40c22015-06-17 13:25:42 -07003036 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003037 final int focusStackId = mFocusedStack.getStackId();
3038 // We dismiss the docked stack whenever we switch users.
3039 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3040
3041 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003042 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003043 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003044
Craig Mautner858d8a62013-04-23 17:08:34 -07003045 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003046 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3047 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003048 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003049 final ActivityStack stack = stacks.get(stackNdx);
3050 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003051 TaskRecord task = stack.topTask();
3052 if (task != null) {
3053 mWindowManager.moveTaskToTop(task.taskId);
3054 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003056 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003057
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003058 ActivityStack stack = getStack(restoreStackId);
3059 if (stack == null) {
3060 stack = mHomeStack;
3061 }
3062 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003063 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003064 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003065 } else {
3066 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003067 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003068 }
Craig Mautner93529a42013-10-04 15:03:13 -07003069 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003070 }
3071
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003072 /** Checks whether the userid is a profile of the current user. */
3073 boolean isCurrentProfileLocked(int userId) {
3074 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003075 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003076 }
3077
Craig Mautnerde4ef022013-04-07 19:01:33 -07003078 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 ArrayList<ActivityRecord> stops = null;
3080
3081 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003082 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3083 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003084 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3085 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003086 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003087 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003088 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3089 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003090 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003091 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003092 if (s.finishing) {
3093 // If this activity is finishing, it is sitting on top of
3094 // everyone else but we now know it is no longer needed...
3095 // so get rid of it. Otherwise, we need to go through the
3096 // normal flow and hide it once we determine that it is
3097 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003098 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003099 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003100 }
3101 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003102 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003103 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003104 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003105 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003106 }
3107 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003108 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003109 }
3110 }
3111
3112 return stops;
3113 }
3114
Craig Mautnercf910b02013-04-23 11:23:27 -07003115 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3117 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3118 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3119 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003120 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003121 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003122 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003123 if (r == null) Slog.e(TAG,
3124 "validateTop...: null top activity, stack=" + stack);
3125 else {
3126 final ActivityRecord pausing = stack.mPausingActivity;
3127 if (pausing != null && pausing == r) Slog.e(TAG,
3128 "validateTop...: top stack has pausing activity r=" + r
3129 + " state=" + state);
3130 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3131 "validateTop...: activity in front not resumed r=" + r
3132 + " state=" + state);
3133 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003134 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003135 final ActivityRecord resumed = stack.mResumedActivity;
3136 if (resumed != null && resumed == r) Slog.e(TAG,
3137 "validateTop...: back stack has resumed activity r=" + r
3138 + " state=" + state);
3139 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3140 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003141 }
3142 }
3143 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003144 }
3145
Craig Mautnere0570202015-05-13 13:06:11 -07003146 private String lockTaskModeToString() {
3147 switch (mLockTaskModeState) {
3148 case LOCK_TASK_MODE_LOCKED:
3149 return "LOCKED";
3150 case LOCK_TASK_MODE_PINNED:
3151 return "PINNED";
3152 case LOCK_TASK_MODE_NONE:
3153 return "NONE";
3154 default: return "unknown=" + mLockTaskModeState;
3155 }
3156 }
3157
Craig Mautner27084302013-03-25 08:05:25 -07003158 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003159 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003160 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003161 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003162 pw.print(prefix);
3163 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003164 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003165 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003166 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3167 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3168 if (packages.size() > 0) {
3169 pw.println(" mLockTaskPackages (userId:packages)=");
3170 for (int i = 0; i < packages.size(); ++i) {
3171 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3172 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3173 }
3174 }
3175 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003176 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177
Winson43d1f262016-06-14 16:05:55 -07003178 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003179 * Dump all connected displays' configurations.
3180 * @param prefix Prefix to apply to each line of the dump.
3181 */
3182 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3183 pw.print(prefix); pw.println("Display override configurations:");
3184 final int displayCount = mActivityDisplays.size();
3185 for (int i = 0; i < displayCount; i++) {
3186 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3187 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3188 pw.println(activityDisplay.getOverrideConfiguration());
3189 }
3190 }
3191
3192 /**
Winson43d1f262016-06-14 16:05:55 -07003193 * Dumps the activities matching the given {@param name} in the either the focused stack
3194 * or all visible stacks if {@param dumpVisibleStacks} is true.
3195 */
3196 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3197 if (dumpVisibleStacks) {
3198 ArrayList<ActivityRecord> activities = new ArrayList<>();
3199 int numDisplays = mActivityDisplays.size();
3200 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3201 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3202 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3203 ActivityStack stack = stacks.get(stackNdx);
3204 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3205 activities.addAll(stack.getDumpActivitiesLocked(name));
3206 }
3207 }
3208 }
3209 return activities;
3210 } else {
3211 return mFocusedStack.getDumpActivitiesLocked(name);
3212 }
Craig Mautner20e72272013-04-01 13:45:53 -07003213 }
3214
Dianne Hackborn390517b2013-05-30 15:03:32 -07003215 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3216 boolean needSep, String prefix) {
3217 if (activity != null) {
3218 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3219 if (needSep) {
3220 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003221 }
3222 pw.print(prefix);
3223 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003224 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003225 }
3226 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003227 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003228 }
3229
Craig Mautner8d341ef2013-03-26 09:03:27 -07003230 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3231 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003232 boolean printed = false;
3233 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003234 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3235 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003236 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003237 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003238 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003239 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003240 final ActivityStack stack = stacks.get(stackNdx);
3241 StringBuilder stackHeader = new StringBuilder(128);
3242 stackHeader.append(" Stack #");
3243 stackHeader.append(stack.mStackId);
3244 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003245 stackHeader.append("\n");
3246 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3247 stackHeader.append("\n");
3248 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003249 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3250 needSep, stackHeader.toString());
3251 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3252 !dumpAll, false, dumpPackage, true,
3253 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003254
Craig Mautner4a1cb222013-12-04 16:14:06 -08003255 needSep = printed;
3256 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3257 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003258 if (pr) {
3259 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003260 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003261 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003262 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3263 " mResumedActivity: ");
3264 if (pr) {
3265 printed = true;
3266 needSep = false;
3267 }
3268 if (dumpAll) {
3269 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3270 " mLastPausedActivity: ");
3271 if (pr) {
3272 printed = true;
3273 needSep = true;
3274 }
3275 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3276 needSep, " mLastNoHistoryActivity: ");
3277 }
3278 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003279 }
3280 }
3281
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003282 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3283 false, dumpPackage, true, " Activities waiting to finish:", null);
3284 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3285 false, dumpPackage, true, " Activities waiting to stop:", null);
3286 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3287 false, dumpPackage, true, " Activities waiting for another to become visible:",
3288 null);
3289 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3290 false, dumpPackage, true, " Activities waiting to sleep:", null);
3291 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3292 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003293
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003294 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 }
3296
Dianne Hackborn390517b2013-05-30 15:03:32 -07003297 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003299 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003300 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003301 String innerPrefix = null;
3302 String[] args = null;
3303 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003304 for (int i=list.size()-1; i>=0; i--) {
3305 final ActivityRecord r = list.get(i);
3306 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3307 continue;
3308 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003309 if (innerPrefix == null) {
3310 innerPrefix = prefix + " ";
3311 args = new String[0];
3312 }
3313 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003314 final boolean full = !brief && (complete || !r.isInHistory());
3315 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003316 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003317 needNL = false;
3318 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003319 if (header1 != null) {
3320 pw.println(header1);
3321 header1 = null;
3322 }
3323 if (header2 != null) {
3324 pw.println(header2);
3325 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003326 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003327 if (lastTask != r.task) {
3328 lastTask = r.task;
3329 pw.print(prefix);
3330 pw.print(full ? "* " : " ");
3331 pw.println(lastTask);
3332 if (full) {
3333 lastTask.dump(pw, prefix + " ");
3334 } else if (complete) {
3335 // Complete + brief == give a summary. Isn't that obvious?!?
3336 if (lastTask.intent != null) {
3337 pw.print(prefix); pw.print(" ");
3338 pw.println(lastTask.intent.toInsecureStringWithClip());
3339 }
3340 }
3341 }
3342 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3343 pw.print(" #"); pw.print(i); pw.print(": ");
3344 pw.println(r);
3345 if (full) {
3346 r.dump(pw, innerPrefix);
3347 } else if (complete) {
3348 // Complete + brief == give a summary. Isn't that obvious?!?
3349 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3350 if (r.app != null) {
3351 pw.print(innerPrefix); pw.println(r.app);
3352 }
3353 }
3354 if (client && r.app != null && r.app.thread != null) {
3355 // flush anything that is already in the PrintWriter since the thread is going
3356 // to write to the file descriptor directly
3357 pw.flush();
3358 try {
3359 TransferPipe tp = new TransferPipe();
3360 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003361 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362 // Short timeout, since blocking here can
3363 // deadlock with the application.
3364 tp.go(fd, 2000);
3365 } finally {
3366 tp.kill();
3367 }
3368 } catch (IOException e) {
3369 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3370 } catch (RemoteException e) {
3371 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3372 }
3373 needNL = true;
3374 }
3375 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003376 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003378
Craig Mautnerf3333272013-04-22 10:55:53 -07003379 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003380 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3381 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003382 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3383 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003384 }
3385
3386 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003387 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003388 }
3389
3390 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003391 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3392 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003393 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3394 }
3395
Craig Mautner05d29032013-05-03 13:40:13 -07003396 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003397 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3398 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3399 }
Craig Mautner05d29032013-05-03 13:40:13 -07003400 }
3401
Craig Mautner0eea92c2013-05-16 13:35:39 -07003402 void removeSleepTimeouts() {
3403 mSleepTimeout = false;
3404 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3405 }
3406
3407 final void scheduleSleepTimeout() {
3408 removeSleepTimeouts();
3409 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3410 }
3411
Craig Mautner4a1cb222013-12-04 16:14:06 -08003412 @Override
3413 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003414 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3416 }
3417
3418 @Override
3419 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003420 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3422 }
3423
3424 @Override
3425 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003426 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003427 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3428 }
3429
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003430 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003431 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003432 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003433 newDisplay = mActivityDisplays.get(displayId) == null;
3434 if (newDisplay) {
3435 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003436 if (activityDisplay.mDisplay == null) {
3437 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3438 return;
3439 }
Craig Mautner4504de52013-12-20 09:06:56 -08003440 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003441 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003442 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003443 }
Craig Mautner4504de52013-12-20 09:06:56 -08003444 if (newDisplay) {
3445 mWindowManager.onDisplayAdded(displayId);
3446 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003447 }
3448
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003449 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003450 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003451 mService.mContext.getResources().getDimensionPixelSize(
3452 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003453 }
3454
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003455 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003456 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003457 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3458 if (activityDisplay != null) {
3459 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003460 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07003461 stacks.get(stackNdx).mActivityContainer.removeLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003463 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 }
3465 }
3466 mWindowManager.onDisplayRemoved(displayId);
3467 }
3468
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003469 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003471 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3472 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 // TODO: Update the bounds.
3474 }
3475 }
3476 mWindowManager.onDisplayChanged(displayId);
3477 }
3478
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003479 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003480 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 StackInfo info = new StackInfo();
3482 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003483 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003484 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003485 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003486 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003487 info.position = display != null
3488 ? display.mStacks.indexOf(stack)
3489 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490
3491 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3492 final int numTasks = tasks.size();
3493 int[] taskIds = new int[numTasks];
3494 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003495 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003496 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 for (int i = 0; i < numTasks; ++i) {
3498 final TaskRecord task = tasks.get(i);
3499 taskIds[i] = task.taskId;
3500 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3501 : task.realActivity != null ? task.realActivity.flattenToString()
3502 : task.getTopActivity() != null ? task.getTopActivity().packageName
3503 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003504 taskBounds[i] = new Rect();
3505 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003506 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003507 }
3508 info.taskIds = taskIds;
3509 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003510 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003511 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003512
3513 final ActivityRecord top = stack.topRunningActivityLocked();
3514 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003515 return info;
3516 }
3517
3518 StackInfo getStackInfoLocked(int stackId) {
3519 ActivityStack stack = getStack(stackId);
3520 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003521 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 }
3523 return null;
3524 }
3525
3526 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003527 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3529 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003531 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 }
3533 }
3534 return list;
3535 }
3536
Craig Mautner15df08a2015-04-01 12:17:18 -07003537 TaskRecord getLockedTaskLocked() {
3538 final int top = mLockTaskModeTasks.size() - 1;
3539 if (top >= 0) {
3540 return mLockTaskModeTasks.get(top);
3541 }
3542 return null;
3543 }
3544
3545 boolean isLockedTask(TaskRecord task) {
3546 return mLockTaskModeTasks.contains(task);
3547 }
3548
3549 boolean isLastLockedTask(TaskRecord task) {
3550 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3551 }
3552
3553 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003554 if (!mLockTaskModeTasks.remove(task)) {
3555 return;
3556 }
3557 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3558 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003559 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003560 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3561 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003562 final Message lockTaskMsg = Message.obtain();
3563 lockTaskMsg.arg1 = task.userId;
3564 lockTaskMsg.what = LOCK_TASK_END_MSG;
3565 mHandler.sendMessage(lockTaskMsg);
3566 }
3567 }
3568
Andrii Kulianc27916642016-04-12 17:59:27 -07003569 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3570 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3571 false /* forceNonResizable */);
3572 }
3573
Jorim Jaggid53f0922016-04-06 22:16:23 -07003574 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003575 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003576 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3577 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003578 return;
3579 }
3580
Jorim Jaggicd13d332016-04-27 15:40:20 -07003581 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003582 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003583 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003584 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003585
Andrii Kulianc27916642016-04-12 17:59:27 -07003586 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3587 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003588 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003589 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3590 && !topActivity.noDisplay) {
3591 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003592 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3593 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003594 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003595 }
3596
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003597 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003598 if (mLockTaskNotify != null) {
3599 mLockTaskNotify.showToast(mLockTaskModeState);
3600 }
Jason Monka8f569c2014-07-07 12:15:21 -04003601 }
3602
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003603 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3604 if (mLockTaskModeTasks.contains(task)) {
3605 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3606 }
3607 }
3608
Craig Mautner432f64e2015-05-20 14:59:57 -07003609 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3610 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003611 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003612 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003613 final TaskRecord lockedTask = getLockedTaskLocked();
3614 if (lockedTask != null) {
3615 removeLockedTaskLocked(lockedTask);
3616 if (!mLockTaskModeTasks.isEmpty()) {
3617 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003618 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3619 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003620 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003621 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003622 return;
3623 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003624 }
Craig Mautnere0570202015-05-13 13:06:11 -07003625 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3626 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003627 return;
3628 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003629
3630 // Should have already been checked, but do it again.
3631 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003632 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3633 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003634 return;
3635 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003636 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003637 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003638 return;
3639 }
3640
3641 if (mLockTaskModeTasks.isEmpty()) {
3642 // First locktask.
3643 final Message lockTaskMsg = Message.obtain();
3644 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3645 lockTaskMsg.arg1 = task.userId;
3646 lockTaskMsg.what = LOCK_TASK_START_MSG;
3647 lockTaskMsg.arg2 = lockTaskModeState;
3648 mHandler.sendMessage(lockTaskMsg);
3649 }
3650 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003651 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3652 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003653 mLockTaskModeTasks.remove(task);
3654 mLockTaskModeTasks.add(task);
3655
3656 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003657 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003658 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003659
3660 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003661 findTaskToMoveToFrontLocked(task, 0, null, reason,
3662 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003663 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003664 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003665 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003666 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003667 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003668 }
3669
3670 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003671 return isLockTaskModeViolation(task, false);
3672 }
3673
3674 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3675 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003676 return false;
3677 }
3678 final int lockTaskAuth = task.mLockTaskAuth;
3679 switch (lockTaskAuth) {
3680 case LOCK_TASK_AUTH_DONT_LOCK:
3681 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003682 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003683 case LOCK_TASK_AUTH_LAUNCHABLE:
3684 case LOCK_TASK_AUTH_WHITELISTED:
3685 return false;
3686 case LOCK_TASK_AUTH_PINNABLE:
3687 // Pinnable tasks can't be launched on top of locktask tasks.
3688 return !mLockTaskModeTasks.isEmpty();
3689 default:
3690 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3691 return true;
3692 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003693 }
3694
Craig Mautner15df08a2015-04-01 12:17:18 -07003695 void onLockTaskPackagesUpdatedLocked() {
3696 boolean didSomething = false;
3697 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3698 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003699 final boolean wasWhitelisted =
3700 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3701 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003702 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003703 final boolean isWhitelisted =
3704 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3705 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3706 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003707 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003708 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3709 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003710 removeLockedTaskLocked(lockedTask);
3711 lockedTask.performClearTaskLocked();
3712 didSomething = true;
3713 }
3714 }
3715 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3716 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3717 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3718 final ActivityStack stack = stacks.get(stackNdx);
3719 stack.onLockTaskPackagesUpdatedLocked();
3720 }
3721 }
Craig Mautnere0570202015-05-13 13:06:11 -07003722 final ActivityRecord r = topRunningActivityLocked();
3723 final TaskRecord task = r != null ? r.task : null;
3724 if (mLockTaskModeTasks.isEmpty() && task != null
3725 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3726 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003727 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3728 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3729 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3730 false);
3731 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003732 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003733 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003734 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003735 }
3736 }
3737
Benjamin Franz43261142015-02-11 15:59:44 +00003738 int getLockTaskModeState() {
3739 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003740 }
3741
Jorim Jaggife89d122015-12-22 16:28:44 +01003742 void activityRelaunchedLocked(IBinder token) {
3743 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003744 if (mService.isSleepingOrShuttingDownLocked()) {
3745 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3746 if (r != null) {
3747 r.setSleeping(true, true);
3748 }
3749 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003750 }
3751
3752 void activityRelaunchingLocked(ActivityRecord r) {
3753 mWindowManager.notifyAppRelaunching(r.appToken);
3754 }
3755
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003756 void logStackState() {
3757 mActivityMetricsLogger.logWindowState();
3758 }
3759
Andrii Kulian933076d2016-03-29 17:04:42 -07003760 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003761 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3762 final ActivityRecord r = task.mActivities.get(i);
3763 if (r.app != null && r.app.thread != null) {
3764 mMultiWindowModeChangedActivities.add(r);
3765 }
3766 }
3767
3768 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3769 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3770 }
3771 }
3772
Andrii Kulian933076d2016-03-29 17:04:42 -07003773 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003774 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003775 if (prevStack == null || prevStack == stack
3776 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3777 return;
3778 }
3779
3780 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3781 final ActivityRecord r = task.mActivities.get(i);
3782 if (r.app != null && r.app.thread != null) {
3783 mPipModeChangedActivities.add(r);
3784 }
3785 }
3786
3787 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3788 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3789 }
3790 }
3791
Tony Mak853304c2016-04-18 15:17:41 +01003792 void setDockedStackMinimized(boolean minimized) {
3793 mIsDockMinimized = minimized;
3794 if (minimized) {
3795 // Docked stack is not visible, no need to confirm credentials for its top activity.
3796 return;
3797 }
3798 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3799 if (dockedStack == null) {
3800 return;
3801 }
3802 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3803 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3804 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3805 }
3806 }
3807
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003808 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003809
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003810 public ActivityStackSupervisorHandler(Looper looper) {
3811 super(looper);
3812 }
3813
Craig Mautnerf3333272013-04-22 10:55:53 -07003814 void activityIdleInternal(ActivityRecord r) {
3815 synchronized (mService) {
3816 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3817 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003818 }
3819
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003820 @Override
3821 public void handleMessage(Message msg) {
3822 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003823 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3824 synchronized (mService) {
3825 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3826 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003827 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003828 }
3829 }
3830 } break;
3831 case REPORT_PIP_MODE_CHANGED_MSG: {
3832 synchronized (mService) {
3833 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3834 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003835 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003836 }
3837 }
3838 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003839 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003840 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3841 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003842 if (mService.mDidDexOpt) {
3843 mService.mDidDexOpt = false;
3844 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3845 nmsg.obj = msg.obj;
3846 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3847 return;
3848 }
3849 // We don't at this point know if the activity is fullscreen,
3850 // so we need to be conservative and assume it isn't.
3851 activityIdleInternal((ActivityRecord)msg.obj);
3852 } break;
3853 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003854 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003855 activityIdleInternal((ActivityRecord)msg.obj);
3856 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003857 case RESUME_TOP_ACTIVITY_MSG: {
3858 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003859 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003860 }
3861 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003862 case SLEEP_TIMEOUT_MSG: {
3863 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003864 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003865 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3866 mSleepTimeout = true;
3867 checkReadyForSleepLocked();
3868 }
3869 }
3870 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003871 case LAUNCH_TIMEOUT_MSG: {
3872 if (mService.mDidDexOpt) {
3873 mService.mDidDexOpt = false;
3874 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3875 return;
3876 }
3877 synchronized (mService) {
3878 if (mLaunchingActivity.isHeld()) {
3879 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3880 if (VALIDATE_WAKE_LOCK_CALLER
3881 && Binder.getCallingUid() != Process.myUid()) {
3882 throw new IllegalStateException("Calling must be system uid");
3883 }
3884 mLaunchingActivity.release();
3885 }
3886 }
3887 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003889 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 } break;
3891 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003892 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003893 } break;
3894 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003895 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003897 case CONTAINER_CALLBACK_VISIBILITY: {
3898 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003899 final IActivityContainerCallback callback = container.mCallback;
3900 if (callback != null) {
3901 try {
3902 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3903 } catch (RemoteException e) {
3904 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003905 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003906 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003907 case LOCK_TASK_START_MSG: {
3908 // When lock task starts, we disable the status bars.
3909 try {
Jason Monk62515be2014-05-21 16:06:19 -04003910 if (mLockTaskNotify == null) {
3911 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003912 }
Jason Monk62515be2014-05-21 16:06:19 -04003913 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003914 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003915 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003916 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003917 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003918 flags = StatusBarManager.DISABLE_MASK
3919 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003920 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003921 flags = StatusBarManager.DISABLE_MASK
3922 & (~StatusBarManager.DISABLE_BACK)
3923 & (~StatusBarManager.DISABLE_HOME)
3924 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003925 }
3926 getStatusBarService().disable(flags, mToken,
3927 mService.mContext.getPackageName());
3928 }
3929 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003930 if (getDevicePolicyManager() != null) {
3931 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003932 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003933 }
justinzhang5286d3f2014-05-12 17:06:01 -04003934 } catch (RemoteException ex) {
3935 throw new RuntimeException(ex);
3936 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003937 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003938 case LOCK_TASK_END_MSG: {
3939 // When lock task ends, we enable the status bars.
3940 try {
Jason Monk62515be2014-05-21 16:06:19 -04003941 if (getStatusBarService() != null) {
3942 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3943 mService.mContext.getPackageName());
3944 }
3945 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003946 if (getDevicePolicyManager() != null) {
3947 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3948 msg.arg1);
3949 }
Jason Monk62515be2014-05-21 16:06:19 -04003950 if (mLockTaskNotify == null) {
3951 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3952 }
3953 mLockTaskNotify.show(false);
3954 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003955 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003956 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003957 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003958 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003959 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003960 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003961 new LockPatternUtils(mService.mContext)
3962 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003963 }
3964 } catch (SettingNotFoundException e) {
3965 // No setting, don't lock.
3966 }
justinzhang5286d3f2014-05-12 17:06:01 -04003967 } catch (RemoteException ex) {
3968 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003969 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003971 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003972 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003973 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3974 if (mLockTaskNotify == null) {
3975 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3976 }
3977 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3978 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003979 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3980 final ActivityContainer container = (ActivityContainer) msg.obj;
3981 final IActivityContainerCallback callback = container.mCallback;
3982 if (callback != null) {
3983 try {
3984 callback.onAllActivitiesComplete(container.asBinder());
3985 } catch (RemoteException e) {
3986 }
3987 }
3988 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003989 case LAUNCH_TASK_BEHIND_COMPLETE: {
3990 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003991 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003992 if (r != null) {
3993 handleLaunchTaskBehindCompleteLocked(r);
3994 }
3995 }
3996 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003997
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003998 }
3999 }
4000 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004001
Ying Wangb081a592014-04-22 15:20:16 -07004002 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004003 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4004 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004006 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004008 ActivityRecord mParentActivity = null;
4009 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004010
Craig Mautnere3a00d72014-04-16 08:31:19 -07004011 boolean mVisible = true;
4012
Craig Mautner4a1cb222013-12-04 16:14:06 -08004013 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004014 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004016 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4017 final static int CONTAINER_STATE_NO_SURFACE = 1;
4018 final static int CONTAINER_STATE_FINISHING = 2;
4019 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4020
4021 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004022 synchronized (mService) {
4023 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004024 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004025 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004026 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004028 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004030 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004031 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004032 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004033 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004034 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004035 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 }
4037
4038 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004039 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004041 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4042 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 return;
4044 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004045 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046 }
4047 }
4048
4049 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004050 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004051 synchronized (mService) {
4052 if (mActivityDisplay != null) {
4053 return mActivityDisplay.mDisplayId;
4054 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004055 }
4056 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004057 }
4058
Jeff Brownca9bc702014-02-11 14:32:56 -08004059 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004060 public int getStackId() {
4061 synchronized (mService) {
4062 return mStackId;
4063 }
4064 }
4065
4066 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004067 public boolean injectEvent(InputEvent event) {
4068 final long origId = Binder.clearCallingIdentity();
4069 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004070 synchronized (mService) {
4071 if (mActivityDisplay != null) {
4072 return mInputManagerInternal.injectInputEvent(event,
4073 mActivityDisplay.mDisplayId,
4074 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4075 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004076 }
4077 return false;
4078 } finally {
4079 Binder.restoreCallingIdentity(origId);
4080 }
4081 }
4082
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004083 @Override
4084 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004085 synchronized (mService) {
4086 if (mContainerState == CONTAINER_STATE_FINISHING) {
4087 return;
4088 }
4089 mContainerState = CONTAINER_STATE_FINISHING;
4090
Craig Mautnerd163e752014-06-13 17:18:47 -07004091 long origId = Binder.clearCallingIdentity();
4092 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004093 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004094 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004095 } finally {
4096 Binder.restoreCallingIdentity(origId);
4097 }
4098 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004099 }
4100
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004101 void removeLocked() {
4102 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004103 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004104 if (mActivityDisplay != null) {
4105 mActivityDisplay.detachActivitiesLocked(mStack);
4106 mActivityDisplay = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004107 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004108 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109 }
4110
4111 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004112 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004113 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004114 }
4115
4116 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004117 public final int startActivityIntentSender(IIntentSender intentSender)
4118 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004119 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4120
4121 if (!(intentSender instanceof PendingIntentRecord)) {
4122 throw new IllegalArgumentException("Bad PendingIntent object");
4123 }
4124
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004125 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004126 Binder.getCallingUid(), mCurrentUser, false,
4127 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004128
4129 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4130 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4131 pendingIntent.key.requestResolvedType);
4132
4133 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4134 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4135 }
4136
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004137 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004138 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004139 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004140 throw new SecurityException(
4141 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4142 }
4143 }
4144
Craig Mautnerdf88d732014-01-27 09:21:32 -08004145 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004146 public IBinder asBinder() {
4147 return this;
4148 }
4149
Craig Mautner4504de52013-12-20 09:06:56 -08004150 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004151 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004152 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004153 }
4154
Craig Mautner4a1cb222013-12-04 16:14:06 -08004155 ActivityStackSupervisor getOuter() {
4156 return ActivityStackSupervisor.this;
4157 }
4158
Craig Mautnerd163e752014-06-13 17:18:47 -07004159 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004160 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004161 }
4162
Craig Mautner6985bad2014-04-21 15:22:06 -07004163 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004164 void setVisible(boolean visible) {
4165 if (mVisible != visible) {
4166 mVisible = visible;
4167 if (mCallback != null) {
4168 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4169 0 /* unused */, this).sendToTarget();
4170 }
4171 }
4172 }
4173
Craig Mautner6985bad2014-04-21 15:22:06 -07004174 void setDrawn() {
4175 }
4176
Craig Mautner1b4bf852014-05-26 15:06:32 -07004177 // You can always start a new task on a regular ActivityStack.
4178 boolean isEligibleForNewTasks() {
4179 return true;
4180 }
4181
Craig Mautnerd163e752014-06-13 17:18:47 -07004182 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004183 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004184 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004185 }
4186
Craig Mautner34b73df2014-01-12 21:11:08 -08004187 @Override
4188 public String toString() {
4189 return mIdString + (mActivityDisplay == null ? "N" : "A");
4190 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004191 }
4192
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004193 private class VirtualActivityContainer extends ActivityContainer {
4194 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004195 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004196
4197 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4198 super(getNextStackId());
4199 mParentActivity = parent;
4200 mCallback = callback;
4201 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004202 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004203 }
4204
4205 @Override
4206 public void setSurface(Surface surface, int width, int height, int density) {
4207 super.setSurface(surface, width, height, density);
4208
4209 synchronized (mService) {
4210 final long origId = Binder.clearCallingIdentity();
4211 try {
4212 setSurfaceLocked(surface, width, height, density);
4213 } finally {
4214 Binder.restoreCallingIdentity(origId);
4215 }
4216 }
4217 }
4218
4219 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4220 if (mContainerState == CONTAINER_STATE_FINISHING) {
4221 return;
4222 }
4223 VirtualActivityDisplay virtualActivityDisplay =
4224 (VirtualActivityDisplay) mActivityDisplay;
4225 if (virtualActivityDisplay == null) {
4226 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004227 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004228 mActivityDisplay = virtualActivityDisplay;
4229 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004230 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 }
4232
4233 if (mSurface != null) {
4234 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004235 }
4236
Craig Mautner6985bad2014-04-21 15:22:06 -07004237 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004238 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004239 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004240 } else {
4241 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004242 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004243 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004244 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004245 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004246 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004247
Craig Mautnerd163e752014-06-13 17:18:47 -07004248 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004249
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004250 if (DEBUG_STACK) Slog.d(TAG_STACK,
4251 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004252 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004253
Craig Mautner6985bad2014-04-21 15:22:06 -07004254 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004255 boolean isAttachedLocked() {
4256 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004257 }
4258
4259 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004260 void setDrawn() {
4261 synchronized (mService) {
4262 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004263 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004264 }
4265 }
4266
Craig Mautner1b4bf852014-05-26 15:06:32 -07004267 // Never start a new task on an ActivityView if it isn't explicitly specified.
4268 @Override
4269 boolean isEligibleForNewTasks() {
4270 return false;
4271 }
4272
Craig Mautnerd163e752014-06-13 17:18:47 -07004273 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004274 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004275 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4276 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4277 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4278 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4279 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004280 }
4281 }
4282
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4284 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004285 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004287 int mDisplayId;
4288 Display mDisplay;
Winson Chung303c6b72016-10-24 17:12:49 -07004289 private final DisplayMetrics mRealMetrics = new DisplayMetrics();
4290 private final Point mRealSize = new Point();
Craig Mautnered6649f2013-12-02 14:08:25 -08004291
Craig Mautner4a1cb222013-12-04 16:14:06 -08004292 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4293 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004294 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004295
Jose Lima4b6c6692014-08-12 17:41:12 -07004296 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004297
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004298 ActivityDisplay() {
4299 }
Craig Mautner4504de52013-12-20 09:06:56 -08004300
Craig Mautner1a70a162014-09-13 12:09:31 -07004301 // After instantiation, check that mDisplay is not null before using this. The alternative
4302 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004303 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004304 final Display display = mDisplayManager.getDisplay(displayId);
4305 if (display == null) {
4306 return;
4307 }
4308 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004309 }
4310
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004311 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004312 mDisplay = display;
4313 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004314 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004315
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004316 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004317 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004318 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4319 + " onTop=" + onTop);
4320 if (onTop) {
4321 mStacks.add(stack);
4322 } else {
4323 mStacks.add(0, stack);
4324 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004325 }
4326
Craig Mautnere0a38842013-12-16 16:14:02 -08004327 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004328 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004329 + " from displayId=" + mDisplayId);
4330 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004331 }
4332
Jose Lima4b6c6692014-08-12 17:41:12 -07004333 void setVisibleBehindActivity(ActivityRecord r) {
4334 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004335 }
4336
Jose Lima4b6c6692014-08-12 17:41:12 -07004337 boolean hasVisibleBehindActivity() {
4338 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004339 }
4340
Craig Mautner34b73df2014-01-12 21:11:08 -08004341 @Override
4342 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004343 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4344 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004345
4346 @Override
4347 protected int getChildCount() {
4348 return mStacks.size();
4349 }
4350
4351 @Override
4352 protected ConfigurationContainer getChildAt(int index) {
4353 return mStacks.get(index);
4354 }
4355
4356 @Override
4357 protected ConfigurationContainer getParent() {
4358 return ActivityStackSupervisor.this;
4359 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004360 }
4361
4362 class VirtualActivityDisplay extends ActivityDisplay {
4363 VirtualDisplay mVirtualDisplay;
4364
Craig Mautner6985bad2014-04-21 15:22:06 -07004365 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004366 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004367 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4368 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4369 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4370 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004371
4372 init(mVirtualDisplay.getDisplay());
4373
4374 mWindowManager.handleDisplayAdded(mDisplayId);
4375 }
4376
4377 void setSurface(Surface surface) {
4378 if (mVirtualDisplay != null) {
4379 mVirtualDisplay.setSurface(surface);
4380 }
4381 }
4382
4383 @Override
4384 void detachActivitiesLocked(ActivityStack stack) {
4385 super.detachActivitiesLocked(stack);
4386 if (mVirtualDisplay != null) {
4387 mVirtualDisplay.release();
4388 mVirtualDisplay = null;
4389 }
4390 }
4391
4392 @Override
4393 public String toString() {
4394 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004395 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004396 }
Jose Lima58e66d62014-05-27 20:00:27 -07004397
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004398 ActivityStack findStackBehind(ActivityStack stack) {
4399 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004400 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004401 if (display == null) {
4402 return null;
4403 }
4404 final ArrayList<ActivityStack> stacks = display.mStacks;
4405 for (int i = stacks.size() - 1; i >= 0; i--) {
4406 if (stacks.get(i) == stack && i > 0) {
4407 return stacks.get(i - 1);
4408 }
4409 }
4410 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4411 + " in=" + stacks);
4412 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004413
Jorim Jaggic69bd222016-03-15 14:38:37 +01004414 /**
4415 * Puts a task into resizing mode during the next app transition.
4416 *
4417 * @param taskId the id of the task to put into resizing mode
4418 */
4419 private void setResizingDuringAnimation(int taskId) {
4420 mResizingTasksDuringAnimation.add(taskId);
4421 mWindowManager.setTaskDockedResizing(taskId, true);
4422 }
4423
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004424 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4425 final TaskRecord task;
4426 final int callingUid;
4427 final String callingPackage;
4428 final Intent intent;
4429 final int userId;
4430 final ActivityOptions activityOptions = (bOptions != null)
4431 ? new ActivityOptions(bOptions) : null;
4432 final int launchStackId = (activityOptions != null)
4433 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004434 if (launchStackId == HOME_STACK_ID) {
4435 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4436 + taskId + " can't be launch in the home stack.");
4437 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004438
4439 if (launchStackId == DOCKED_STACK_ID) {
4440 mWindowManager.setDockedStackCreateState(
4441 activityOptions.getDockCreateMode(), null /* initialBounds */);
4442
4443 // Defer updating the stack in which recents is until the app transition is done, to
4444 // not run into issues where we still need to draw the task in recents but the
4445 // docked stack is already created.
4446 deferUpdateBounds(HOME_STACK_ID);
4447 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4448 }
4449
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004450 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4451 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004452 continueUpdateBounds(HOME_STACK_ID);
4453 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004454 throw new IllegalArgumentException(
4455 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4456 }
4457
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004458 // Since we don't have an actual source record here, we assume that the currently focused
4459 // activity was the source.
4460 final ActivityStack focusedStack = getFocusedStack();
4461 final ActivityRecord sourceRecord =
4462 focusedStack != null ? focusedStack.topActivity() : null;
4463
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004464 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004465 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004466 moveTaskToStackLocked(
4467 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4468 ANIMATE);
4469 }
4470 }
4471
4472 // If the user must confirm credentials (e.g. when first launching a work app and the
4473 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4474 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4475 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004476 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004477 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004478 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004479 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4480 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004481
4482 // If we are launching the task in the docked stack, put it into resizing mode so
4483 // the window renders full-screen with the background filling the void. Also only
4484 // call this at the end to make sure that tasks exists on the window manager side.
4485 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004486 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004487 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004488
4489 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4490 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004491 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4492 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004493 return ActivityManager.START_TASK_TO_FRONT;
4494 }
4495 callingUid = task.mCallingUid;
4496 callingPackage = task.mCallingPackage;
4497 intent = task.intent;
4498 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4499 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004500 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4501 null, null, 0, 0, bOptions, userId, null, task);
4502 if (launchStackId == DOCKED_STACK_ID) {
4503 setResizingDuringAnimation(task.taskId);
4504 }
4505 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004506 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004507
4508 /**
4509 * @return a list of activities which are the top ones in each visible stack. The first
4510 * entry will be the focused activity.
4511 */
4512 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004513 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4514 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Jorim Jaggife762342016-10-13 14:33:27 +02004515 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Amith Yamasanie8222e52016-04-08 15:28:47 -07004516 if (display == null) {
4517 return Collections.EMPTY_LIST;
4518 }
4519 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4520 final ArrayList<ActivityStack> stacks = display.mStacks;
4521 for (int i = stacks.size() - 1; i >= 0; i--) {
4522 ActivityStack stack = stacks.get(i);
4523 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4524 ActivityRecord top = stack.topActivity();
4525 if (top != null) {
4526 if (stack == mFocusedStack) {
4527 topActivityTokens.add(0, top.appToken);
4528 } else {
4529 topActivityTokens.add(top.appToken);
4530 }
4531 }
4532 }
4533 }
4534 return topActivityTokens;
4535 }
Craig Mautner27084302013-03-25 08:05:25 -07004536}