blob: 175cba7dd099e4b8f7b35d331959b8bb9043fcef [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
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070020import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070021import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080022import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080024import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070025import android.app.ActivityOptions;
26import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070027import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080028import android.app.IActivityContainer;
29import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070030import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080031import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070032import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070033import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040034import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040035import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070037import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070039import android.content.Intent;
40import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070042import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010045import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080047import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.hardware.display.DisplayManager;
49import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080050import android.hardware.display.DisplayManagerGlobal;
51import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080052import android.hardware.input.InputManager;
53import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.os.Binder;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070055import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070059import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070061import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070062import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070063import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040064import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070066import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070067import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070068import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010069import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070070import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070071import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040072import android.provider.Settings;
73import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070074import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070075import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070076import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080079import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080081import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080083import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080085
Dianne Hackborn85d558c2014-11-04 10:31:54 -080086import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070087import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040088import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070089import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040090import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094
Craig Mautner8d341ef2013-03-26 09:03:27 -070095import java.io.FileDescriptor;
96import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070097import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -070099import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800101import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700102import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700103
Jorim Jaggi2adba072016-03-03 13:43:39 +0100104import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100105import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100106import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100107import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
108import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
109import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
110import static android.app.ActivityManager.RESIZE_MODE_FORCED;
111import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
112import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
113import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
114import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
115import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
116import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
117import static android.app.ActivityManager.StackId.HOME_STACK_ID;
118import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
119import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
120import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
121import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
122import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
123import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500124import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100125import static android.content.pm.PackageManager.PERMISSION_GRANTED;
126import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
151import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800153import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100154import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100155import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100156import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
157import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
158import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
160import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
161import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
162import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
163import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
164import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
165import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
166import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
172
Craig Mautner4a1cb222013-12-04 16:14:06 -0800173public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800174 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700175 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700177 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700178 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700180 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800184 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800186
Craig Mautnerf3333272013-04-22 10:55:53 -0700187 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700189
Craig Mautner0eea92c2013-05-16 13:35:39 -0700190 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700192
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700193 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700195
Craig Mautner05d29032013-05-03 13:40:13 -0700196 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
197 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
198 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700200 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800201 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
202 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
203 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700204 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400205 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
206 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700207 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700208 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700209 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800210 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
211 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212
Wale Ogunwale040b4702015-08-06 18:10:50 -0700213 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214
Jason Monk62515be2014-05-21 16:06:19 -0400215 private static final String LOCK_TASK_TAG = "Lock-to-App";
216
Wale Ogunwale040b4702015-08-06 18:10:50 -0700217 // Used to indicate if an object (e.g. stack) that we are trying to get
218 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800219 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700221 // Used to indicate that windows of activities should be preserved during the resize.
222 static final boolean PRESERVE_WINDOWS = true;
223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 // Used to indicate if an object (e.g. task) should be moved/created
225 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700226 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227
228 // Used to indicate that an objects (e.g. task) removal from its container
229 // (e.g. stack) is due to it moving to another container.
230 static final boolean MOVING = true;
231
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700232 // Force the focus to change to the stack we are moving a task to..
233 static final boolean FORCE_FOCUS = true;
234
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700235 // Restore task from the saved recents if it can't be found in any live stack.
236 static final boolean RESTORE_FROM_RECENTS = true;
237
Svetoslav7008b512015-06-24 18:47:07 -0700238 // Activity actions an app cannot start if it uses a permission which is not granted.
239 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
240 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700241
Svetoslav7008b512015-06-24 18:47:07 -0700242 static {
243 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
244 Manifest.permission.CAMERA);
245 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
246 Manifest.permission.CAMERA);
247 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
248 Manifest.permission.CALL_PHONE);
249 }
250
Svet Ganov99b60432015-06-27 13:15:22 -0700251 /** Action restriction: launching the activity is not restricted. */
252 private static final int ACTIVITY_RESTRICTION_NONE = 0;
253 /** Action restriction: launching the activity is restricted by a permission. */
254 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
255 /** Action restriction: launching the activity is restricted by an app op. */
256 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700257
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700258 // The height/width divide used when fitting a task within a bounds with method
259 // {@link #fitWithinBounds}.
260 // We always want the task to to be visible in the bounds without affecting its size when
261 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
262 // the input bounds right or bottom side minus the width or height divided by this value.
263 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
264
justinzhang5286d3f2014-05-12 17:06:01 -0400265 /** Status Bar Service **/
266 private IBinder mToken = new Binder();
267 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400268 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400269
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700270 // For debugging to make sure the caller when acquiring/releasing our
271 // wake lock is the system process.
272 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800273 /** The number of distinct task ids that can be assigned to the tasks of a single user */
274 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700275
Craig Mautner27084302013-03-25 08:05:25 -0700276 final ActivityManagerService mService;
277
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800278 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800279
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700280 final ActivityStackSupervisorHandler mHandler;
281
282 /** Short cut */
283 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800284 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700286 /** Counter for next free stack ID to use for dynamic activity stacks. */
287 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700288
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800289 /**
290 * Maps the task identifier that activities are currently being started in to the userId of the
291 * task. Each time a new task is created, the entry for the userId of the task is incremented
292 */
293 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700294
Craig Mautner2420ead2013-04-01 17:13:20 -0700295 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800296 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700297
Craig Mautnere0a38842013-12-16 16:14:02 -0800298 /** The stack containing the launcher app. Assumed to always be attached to
299 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800300 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700301
Craig Mautnere0a38842013-12-16 16:14:02 -0800302 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800303 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700304
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
306 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800307 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800308 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700309
310 /** List of activities that are waiting for a new activity to become visible before completing
311 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800312 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700313
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700314 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800315 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316
317 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319
Craig Mautnerde4ef022013-04-07 19:01:33 -0700320 /** List of activities that are ready to be stopped, but waiting for the next activity to
321 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
Craig Mautnerf3333272013-04-22 10:55:53 -0700324 /** List of activities that are ready to be finished, but waiting for the previous activity to
325 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800326 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700327
Craig Mautner0eea92c2013-05-16 13:35:39 -0700328 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800329 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330
Wale Ogunwale22e25262016-02-01 10:32:02 -0800331 /** List of activities whose multi-window mode changed that we need to report to the
332 * application */
333 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
334
335 /** List of activities whose picture-in-picture mode changed that we need to report to the
336 * application */
337 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
338
Craig Mautnerf3333272013-04-22 10:55:53 -0700339 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700340 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700341
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700342 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700343 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700344
Craig Mautnerde4ef022013-04-07 19:01:33 -0700345 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
346 * is being brought in front of us. */
347 boolean mUserLeaving = false;
348
Craig Mautner0eea92c2013-05-16 13:35:39 -0700349 /** Set when we have taken too long waiting to go to sleep. */
350 boolean mSleepTimeout = false;
351
352 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700353 * We don't want to allow the device to go to sleep while in the process
354 * of launching an activity. This is primarily to allow alarm intent
355 * receivers to launch an activity and get that to run before the device
356 * goes back to sleep.
357 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700358 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700359
360 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700361 * Set when the system is going to sleep, until we have
362 * successfully paused the current activity and released our wake lock.
363 * At that point the system is allowed to actually sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700367 /** Stack id of the front stack when user switched, indexed by userId. */
368 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700369
Craig Mautner4504de52013-12-20 09:06:56 -0800370 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700372 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373
374 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700375 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800376
Jeff Brownca9bc702014-02-11 14:32:56 -0800377 InputManagerInternal mInputManagerInternal;
378
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
380 * may be finished until there is only one entry left. If this is empty the system is not
381 * in lockTask mode. */
382 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000383 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700384 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
385 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000386 */
387 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400388 /**
389 * Notifies the user when entering/exiting lock-task.
390 */
391 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800392
Wale Ogunwaled046a012015-12-24 13:05:59 -0800393 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800394 boolean inResumeTopActivity;
395
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700396 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700397 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700398 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700399
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700400 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
401 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800402 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
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.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800406 int mDefaultMinimalSizeOfResizeableTask = -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
Wale Ogunwale39381972015-12-17 17:15:29 -0800415 static class FindTaskResult {
416 ActivityRecord r;
417 boolean matchedByRootAffinity;
418 }
419 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
420
Craig Mautneree36c772014-07-16 14:56:05 -0700421 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100422 * Used to keep track whether app visibilities got changed since the last pause. Useful to
423 * determine whether to invoke the task stack change listener after pausing.
424 */
425 boolean mAppVisibilitiesChangedSinceLastPause;
426
427 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700428 * Description of a request to start a new activity, which has been held
429 * due to app switches being disabled.
430 */
431 static class PendingActivityLaunch {
432 final ActivityRecord r;
433 final ActivityRecord sourceRecord;
434 final int startFlags;
435 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700436 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700437
438 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700439 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700440 r = _r;
441 sourceRecord = _sourceRecord;
442 startFlags = _startFlags;
443 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700444 callerApp = _callerApp;
445 }
446
447 void sendErrorResult(String message) {
448 try {
449 if (callerApp.thread != null) {
450 callerApp.thread.scheduleCrash(message);
451 }
452 } catch (RemoteException e) {
453 Slog.e(TAG, "Exception scheduling crash of failed "
454 + "activity launcher sourceRecord=" + sourceRecord, e);
455 }
Craig Mautneree36c772014-07-16 14:56:05 -0700456 }
457 }
458
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800459 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700460 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700461 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800462 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800463 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700464 }
465
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800466 void setRecentTasks(RecentTasks recentTasks) {
467 mRecentTasks = recentTasks;
468 }
469
Jeff Brown2c43c332014-06-12 22:38:59 -0700470 /**
471 * At the time when the constructor runs, the power manager has not yet been
472 * initialized. So we initialize our wakelocks afterwards.
473 */
474 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800475 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700476 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700477 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700478 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700479 }
480
justinzhang5286d3f2014-05-12 17:06:01 -0400481 // This function returns a IStatusBarService. The value is from ServiceManager.
482 // getService and is cached.
483 private IStatusBarService getStatusBarService() {
484 synchronized (mService) {
485 if (mStatusBarService == null) {
486 mStatusBarService = IStatusBarService.Stub.asInterface(
487 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
488 if (mStatusBarService == null) {
489 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
490 }
491 }
492 return mStatusBarService;
493 }
494 }
495
Jason Monk35c62a42014-06-17 10:24:47 -0400496 private IDevicePolicyManager getDevicePolicyManager() {
497 synchronized (mService) {
498 if (mDevicePolicyManager == null) {
499 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
500 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
501 if (mDevicePolicyManager == null) {
502 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
503 }
504 }
505 return mDevicePolicyManager;
506 }
507 }
508
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700509 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 synchronized (mService) {
511 mWindowManager = wm;
512
513 mDisplayManager =
514 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
515 mDisplayManager.registerDisplayListener(this, null);
516
517 Display[] displays = mDisplayManager.getDisplays();
518 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
519 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800520 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700521 if (activityDisplay.mDisplay == null) {
522 throw new IllegalStateException("Default Display does not exist");
523 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800524 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700525 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800526 }
527
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800528 mHomeStack = mFocusedStack = mLastFocusedStack =
529 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800530
531 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 }
Craig Mautner27084302013-03-25 08:05:25 -0700533 }
534
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200535 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700536 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200537 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700538 }
539
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700540 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800541 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700542 }
543
Craig Mautnerde4ef022013-04-07 19:01:33 -0700544 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700546 }
547
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700548 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700549 if (stack == null) {
550 return false;
551 }
552
Craig Mautnerdf88d732014-01-27 09:21:32 -0800553 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
554 if (parent != null) {
555 stack = parent.task.stack;
556 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800557 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700558 }
559
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700560 /** The top most stack. */
561 boolean isFrontStack(ActivityStack stack) {
562 if (stack == null) {
563 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700565
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
567 if (parent != null) {
568 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800569 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700570 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
571 }
572
Wale Ogunwaled046a012015-12-24 13:05:59 -0800573 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800574 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
575 if (!focusCandidate.isFocusable()) {
576 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
577 focusCandidate = focusCandidate.getNextFocusableStackLocked();
578 }
579
580 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800581 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800582 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800583
Wale Ogunwaled046a012015-12-24 13:05:59 -0800584 EventLogTags.writeAmFocusedStack(
585 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
586 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
587 }
588
589 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800590 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800591 // The focus activity should always be the top activity in the focused stack.
592 // There will be chaos and anarchy if it isn't...
593 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
594 }
Craig Mautnerde313752015-01-22 14:28:03 -0800595
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800596 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800597 if (r != null && r.idle) {
598 checkFinishBootingLocked();
599 }
600 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700601 }
602
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700603 void moveHomeStackToFront(String reason) {
604 mHomeStack.moveToFront(reason);
605 }
606
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700607 /** Returns true if the focus activity was adjusted to the home stack top activity. */
608 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700609 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
610 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700611 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700612 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700613
Craig Mautner84984fa2014-06-19 11:19:20 -0700614 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700615
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700616 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700617 if (top == null) {
618 return false;
619 }
620 mService.setFocusedActivityLocked(top, reason);
621 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700622 }
623
Craig Mautner299f9602015-01-26 09:47:33 -0800624 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700625 if (!mService.mBooting && !mService.mBooted) {
626 // Not ready yet!
627 return false;
628 }
629
Craig Mautner84984fa2014-06-19 11:19:20 -0700630 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
631 mWindowManager.showRecentApps();
632 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700633 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700634
Craig Mautner84984fa2014-06-19 11:19:20 -0700635 if (prev != null) {
636 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
637 }
638
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700639 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
640 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800641 final String myReason = reason + " resumeHomeStackTask";
642
Mark Lua56ea122015-10-08 13:31:01 +0800643 // Only resume home activity if isn't finishing.
644 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800645 mService.setFocusedActivityLocked(r, myReason);
646 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700647 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800648 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700649 }
650
Craig Mautner8d341ef2013-03-26 09:03:27 -0700651 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700652 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700653 }
654
655 /**
656 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
657 * @param id Id of the task we would like returned.
658 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
659 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700660 * @param stackId The stack to restore the task to (default launch stack will be used if
661 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700662 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700663 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800666 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
668 ActivityStack stack = stacks.get(stackNdx);
669 TaskRecord task = stack.taskForIdLocked(id);
670 if (task != null) {
671 return task;
672 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700673 }
674 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800675
676 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700677 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800678 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800679 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700680 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800681 return null;
682 }
683
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700684 if (!restoreFromRecents) {
685 return task;
686 }
687
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700688 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700689 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
690 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800691 return null;
692 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800694 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700695 }
696
Craig Mautner6170f732013-04-02 13:05:23 -0700697 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800698 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800700 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800701 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
702 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
703 if (r != null) {
704 return r;
705 }
Craig Mautner6170f732013-04-02 13:05:23 -0700706 }
707 }
708 return null;
709 }
710
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000711 boolean isFocusedUserLockedProfile() {
712 final int userId = mFocusedStack.topRunningActivityLocked().userId;
713 return userId != UserHandle.myUserId()
714 && mService.mUserController.shouldConfirmCredentials(userId);
715 }
716
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800717 void setNextTaskIdForUserLocked(int taskId, int userId) {
718 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
719 if (taskId > currentTaskId) {
720 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700721 }
722 }
723
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800724 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800725 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
726 // for a userId u, a taskId can only be in the range
727 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
728 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
729 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800730 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
731 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
732 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800733 candidateTaskId++;
734 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
735 // Wrap around as there will be smaller task ids that are available now.
736 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700737 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800738 if (candidateTaskId == currentTaskId) {
739 // Something wrong!
740 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
741 throw new IllegalStateException("Cannot get an available task id."
742 + " Reached limit of " + MAX_TASK_IDS_PER_USER
743 + " running tasks per user.");
744 }
745 }
746 mCurTaskIdForUser.put(userId, candidateTaskId);
747 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700748 }
749
Craig Mautnerde4ef022013-04-07 19:01:33 -0700750 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800751 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700752 if (stack == null) {
753 return null;
754 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 ActivityRecord resumedActivity = stack.mResumedActivity;
756 if (resumedActivity == null || resumedActivity.app == null) {
757 resumedActivity = stack.mPausingActivity;
758 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700759 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700760 }
761 }
762 return resumedActivity;
763 }
764
Dianne Hackbornff072722014-09-24 10:56:28 -0700765 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700766 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
769 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
771 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700772 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 continue;
774 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700775 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 if (hr != null) {
777 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
778 && processName.equals(hr.processName)) {
779 try {
George Mount2c92c972014-03-20 09:38:23 -0700780 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800781 didSomething = true;
782 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700783 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 Slog.w(TAG, "Exception in new application when starting activity "
785 + hr.intent.getComponent().flattenToShortString(), e);
786 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700787 }
Craig Mautner20e72272013-04-01 13:45:53 -0700788 }
Craig Mautner20e72272013-04-01 13:45:53 -0700789 }
790 }
791 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700792 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700793 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700794 }
Craig Mautner20e72272013-04-01 13:45:53 -0700795 return didSomething;
796 }
797
798 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800799 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
800 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
802 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700803 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 continue;
805 }
806 final ActivityRecord resumedActivity = stack.mResumedActivity;
807 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700808 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800809 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 return false;
811 }
Craig Mautner20e72272013-04-01 13:45:53 -0700812 }
813 }
814 return true;
815 }
816
Craig Mautnerde4ef022013-04-07 19:01:33 -0700817 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
819 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
821 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700822 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700824 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 return false;
826 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700827 }
828 }
829 }
830 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700831 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800832 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
833 mLastFocusedStack + " to=" + mFocusedStack);
834 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700835 return true;
836 }
837
838 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800839 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800840 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
841 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
843 final ActivityStack stack = stacks.get(stackNdx);
844 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800845 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700846 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800847 return false;
848 }
849 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700851 }
852 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800853 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700854 }
855
Craig Mautner2acc3892013-09-23 10:28:14 -0700856 /**
857 * Pause all activities in either all of the stacks or just the back stacks.
858 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700859 * @return true if any activity was paused as a result of this call.
860 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700861 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700862 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
864 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
866 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700867 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700868 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700870 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
871 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700873 }
874 }
875 return someActivityPaused;
876 }
877
Craig Mautnerde4ef022013-04-07 19:01:33 -0700878 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700879 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
881 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
883 final ActivityStack stack = stacks.get(stackNdx);
884 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700885 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700887 Slog.d(TAG_STATES,
888 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 pausing = false;
890 } else {
891 return false;
892 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700893 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700894 }
895 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700896 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 }
898
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700899 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
900 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500901 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800902 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
903 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
904 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
905 final ActivityStack stack = stacks.get(stackNdx);
906 if (stack.mResumedActivity != null &&
907 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700908 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800909 }
910 }
911 }
912 }
913
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800914 void cancelInitializingActivities() {
915 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
916 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
918 stacks.get(stackNdx).cancelInitializingActivities();
919 }
920 }
921 }
922
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700923 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700924 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700925 }
926
927 void sendWaitingVisibleReportLocked(ActivityRecord r) {
928 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700929 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700930 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700931 if (w.who == null) {
932 changed = true;
933 w.timeout = false;
934 if (r != null) {
935 w.who = new ComponentName(r.info.packageName, r.info.name);
936 }
937 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
938 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700939 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700940 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700941 if (changed) {
942 mService.notifyAll();
943 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700944 }
945
946 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
947 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700949 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700950 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700951 if (w.who == null) {
952 changed = true;
953 w.timeout = timeout;
954 if (r != null) {
955 w.who = new ComponentName(r.info.packageName, r.info.name);
956 }
957 w.thisTime = thisTime;
958 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700959 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700960 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700961 if (changed) {
962 mService.notifyAll();
963 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700964 }
965
Craig Mautner29219d92013-04-16 20:19:12 -0700966 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800967 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700968 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700969 if (r != null) {
970 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700971 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700972
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800974 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
976 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800977 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700978 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700979 if (r != null) {
980 return r;
981 }
982 }
983 }
984 return null;
985 }
986
Dianne Hackborn09233282014-04-30 11:33:59 -0700987 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700988 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800989 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
990 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800993 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
995 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700996 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700998 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700999 }
1000 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001001
1002 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1003 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1004 while (maxNum > 0) {
1005 long mostRecentActiveTime = Long.MIN_VALUE;
1006 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001007 final int numTaskLists = runningTaskLists.size();
1008 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1009 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001010 if (!stackTaskList.isEmpty()) {
1011 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1012 if (lastActiveTime > mostRecentActiveTime) {
1013 mostRecentActiveTime = lastActiveTime;
1014 selectedStackList = stackTaskList;
1015 }
1016 }
1017 }
1018 if (selectedStackList != null) {
1019 list.add(selectedStackList.remove(0));
1020 --maxNum;
1021 } else {
1022 break;
1023 }
1024 }
Craig Mautner20e72272013-04-01 13:45:53 -07001025 }
1026
Todd Kennedy7440f172015-12-09 14:31:22 -08001027 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1028 ProfilerInfo profilerInfo) {
1029 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001030 if (aInfo != null) {
1031 // Store the found target back into the intent, because now that
1032 // we have it we never want to do this again. For example, if the
1033 // user navigates back to this point in the history, we should
1034 // always restart the exact same activity.
1035 intent.setComponent(new ComponentName(
1036 aInfo.applicationInfo.packageName, aInfo.name));
1037
1038 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001039 if (!aInfo.processName.equals("system")) {
1040 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041 mService.setDebugApp(aInfo.processName, true, false);
1042 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001043
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001044 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1045 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1046 }
1047
Man Caocfa78b22015-06-11 20:14:34 -07001048 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1049 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1050 }
1051
1052 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001053 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001054 }
1055 }
1056 }
1057 return aInfo;
1058 }
1059
Todd Kennedy7440f172015-12-09 14:31:22 -08001060 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001061 return resolveIntent(intent, resolvedType, userId, 0);
1062 }
1063
1064 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001065 try {
1066 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001067 PackageManager.MATCH_DEFAULT_ONLY | flags
1068 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001069 } catch (RemoteException e) {
1070 }
1071 return null;
1072 }
1073
1074 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1075 ProfilerInfo profilerInfo, int userId) {
1076 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1077 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1078 }
1079
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001080 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1081 boolean andResume, boolean checkConfig) throws RemoteException {
1082
1083 if (!allPausedActivitiesComplete()) {
1084 // While there are activities pausing we skipping starting any new activities until
1085 // pauses are complete. NOTE: that we also do this for activities that are starting in
1086 // the paused state because they will first be resumed then paused on the client side.
1087 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1088 "realStartActivityLocked: Skipping start of r=" + r
1089 + " some activities pausing...");
1090 return false;
1091 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001092
Craig Mautner2568c3a2015-03-26 14:22:34 -07001093 if (andResume) {
1094 r.startFreezingScreenLocked(app, 0);
1095 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001096
Craig Mautner2568c3a2015-03-26 14:22:34 -07001097 // schedule launch ticks to collect information about slow apps.
1098 r.startLaunchTickingLocked();
1099 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001100
1101 // Have the window manager re-evaluate the orientation of
1102 // the screen based on the new activity order. Note that
1103 // as a result of this, it can call back into the activity
1104 // manager with a new orientation. We don't care about that,
1105 // because the activity is not currently running so we are
1106 // just restarting it anyway.
1107 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001108 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001109 mService.mConfiguration,
1110 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001111 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 }
1113
1114 r.app = app;
1115 app.waitingToKill = null;
1116 r.launchCount++;
1117 r.lastLaunchTime = SystemClock.uptimeMillis();
1118
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001119 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001120
1121 int idx = app.activities.indexOf(r);
1122 if (idx < 0) {
1123 app.activities.add(r);
1124 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001125 mService.updateLruProcessLocked(app, true, null);
1126 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001127
Craig Mautner15df08a2015-04-01 12:17:18 -07001128 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001129 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1130 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001131 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001132 }
1133
1134 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001135 try {
1136 if (app.thread == null) {
1137 throw new RemoteException();
1138 }
1139 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001140 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001141 if (andResume) {
1142 results = r.results;
1143 newIntents = r.newIntents;
1144 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001145 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1146 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1147 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001148 if (andResume) {
1149 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1150 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001151 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001152 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001153 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001154 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001155 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001157 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 r.sleeping = false;
1159 r.forceNewConfig = false;
1160 mService.showAskCompatModeDialogLocked(r);
1161 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001162 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001163 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1164 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1165 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001166 final String profileFile = mService.mProfileFile;
1167 if (profileFile != null) {
1168 ParcelFileDescriptor profileFd = mService.mProfileFd;
1169 if (profileFd != null) {
1170 try {
1171 profileFd = profileFd.dup();
1172 } catch (IOException e) {
1173 if (profileFd != null) {
1174 try {
1175 profileFd.close();
1176 } catch (IOException o) {
1177 }
1178 profileFd = null;
1179 }
1180 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001181 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001182
1183 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1184 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001185 }
1186 }
1187 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001188
Craig Mautner2568c3a2015-03-26 14:22:34 -07001189 if (andResume) {
1190 app.hasShownUi = true;
1191 app.pendingUiClean = true;
1192 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001193 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001194 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001195 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001196 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001197 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001198 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001199
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001200 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 // This may be a heavy-weight process! Note that the package
1202 // manager will ensure that only activity can run in the main
1203 // process of the .apk, which is the only thing that will be
1204 // considered heavy-weight.
1205 if (app.processName.equals(app.info.packageName)) {
1206 if (mService.mHeavyWeightProcess != null
1207 && mService.mHeavyWeightProcess != app) {
1208 Slog.w(TAG, "Starting new heavy weight process " + app
1209 + " when already running "
1210 + mService.mHeavyWeightProcess);
1211 }
1212 mService.mHeavyWeightProcess = app;
1213 Message msg = mService.mHandler.obtainMessage(
1214 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1215 msg.obj = r;
1216 mService.mHandler.sendMessage(msg);
1217 }
1218 }
1219
1220 } catch (RemoteException e) {
1221 if (r.launchFailed) {
1222 // This is the second time we failed -- finish activity
1223 // and give up.
1224 Slog.e(TAG, "Second failure launching "
1225 + r.intent.getComponent().flattenToShortString()
1226 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001227 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1229 "2nd-crash", false);
1230 return false;
1231 }
1232
1233 // This is the first time we failed -- restart process and
1234 // retry.
1235 app.activities.remove(r);
1236 throw e;
1237 }
1238
1239 r.launchFailed = false;
1240 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001241 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 }
1243
1244 if (andResume) {
1245 // As part of the process of launching, ActivityThread also performs
1246 // a resume.
1247 stack.minimalResumeActivityLocked(r);
1248 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001249 // This activity is not starting in the resumed state... which should look like we asked
1250 // it to pause+stop (but remain visible), and it has done so and reported back the
1251 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001252 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001253 "Moving to PAUSED: " + r + " (starting in paused state)");
1254 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
1256
1257 // Launch the new version setup screen if needed. We do this -after-
1258 // launching the initial activity (that is, home), so that it can have
1259 // a chance to initialize itself while in the background, making the
1260 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001261 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 mService.startSetupActivityLocked();
1263 }
1264
Dianne Hackborn465fa392014-09-14 14:21:18 -07001265 // Update any services we are bound to that might care about whether
1266 // their client may have activities.
1267 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1268
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 return true;
1270 }
1271
Craig Mautnere79d42682013-04-01 19:01:53 -07001272 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001273 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001274 // Is this activity's application already running?
1275 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001276 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001277
1278 r.task.stack.setLaunchTime(r);
1279
1280 if (app != null && app.thread != null) {
1281 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001282 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1283 || !"android".equals(r.info.packageName)) {
1284 // Don't add this if it is a platform component that is marked
1285 // to run in multiple processes, because this is actually
1286 // part of the framework so doesn't make sense to track as a
1287 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001288 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1289 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001290 }
George Mount2c92c972014-03-20 09:38:23 -07001291 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001292 return;
1293 } catch (RemoteException e) {
1294 Slog.w(TAG, "Exception when starting activity "
1295 + r.intent.getComponent().flattenToShortString(), e);
1296 }
1297
1298 // If a dead object exception was thrown -- fall through to
1299 // restart the application.
1300 }
1301
1302 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001303 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001304 }
1305
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001306 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001307 String resultWho, int requestCode, int callingPid, int callingUid,
1308 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001309 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001310 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1311 callingUid);
1312 if (startAnyPerm == PERMISSION_GRANTED) {
1313 return true;
1314 }
1315 final int componentRestriction = getComponentRestrictionForCallingPackage(
1316 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1317 final int actionRestriction = getActionRestrictionForCallingPackage(
1318 intent.getAction(), callingPackage, callingPid, callingUid);
1319 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1320 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1321 if (resultRecord != null) {
1322 resultStack.sendActivityResultLocked(-1,
1323 resultRecord, resultWho, requestCode,
1324 Activity.RESULT_CANCELED, null);
1325 }
1326 final String msg;
1327 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1328 msg = "Permission Denial: starting " + intent.toString()
1329 + " from " + callerApp + " (pid=" + callingPid
1330 + ", uid=" + callingUid + ")" + " with revoked permission "
1331 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1332 } else if (!aInfo.exported) {
1333 msg = "Permission Denial: starting " + intent.toString()
1334 + " from " + callerApp + " (pid=" + callingPid
1335 + ", uid=" + callingUid + ")"
1336 + " not exported from uid " + aInfo.applicationInfo.uid;
1337 } else {
1338 msg = "Permission Denial: starting " + intent.toString()
1339 + " from " + callerApp + " (pid=" + callingPid
1340 + ", uid=" + callingUid + ")"
1341 + " requires " + aInfo.permission;
1342 }
1343 Slog.w(TAG, msg);
1344 throw new SecurityException(msg);
1345 }
1346
1347 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1348 final String message = "Appop Denial: starting " + intent.toString()
1349 + " from " + callerApp + " (pid=" + callingPid
1350 + ", uid=" + callingUid + ")"
1351 + " requires " + AppOpsManager.permissionToOp(
1352 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1353 Slog.w(TAG, message);
1354 return false;
1355 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1356 final String message = "Appop Denial: starting " + intent.toString()
1357 + " from " + callerApp + " (pid=" + callingPid
1358 + ", uid=" + callingUid + ")"
1359 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1360 Slog.w(TAG, message);
1361 return false;
1362 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001363 if (options != null && options.getLaunchTaskId() != -1) {
1364 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1365 callingPid, callingUid);
1366 if (startInTaskPerm != PERMISSION_GRANTED) {
1367 final String msg = "Permission Denial: starting " + intent.toString()
1368 + " from " + callerApp + " (pid=" + callingPid
1369 + ", uid=" + callingUid + ") with launchTaskId="
1370 + options.getLaunchTaskId();
1371 Slog.w(TAG, msg);
1372 throw new SecurityException(msg);
1373 }
1374 }
1375
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001376 return true;
1377 }
1378
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001379 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001380 final long identity = Binder.clearCallingIdentity();
1381 try {
1382 return UserManager.get(mService.mContext).getUserInfo(userId);
1383 } finally {
1384 Binder.restoreCallingIdentity(identity);
1385 }
1386 }
1387
Svet Ganov99b60432015-06-27 13:15:22 -07001388 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001389 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001390 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1391 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001392 == PackageManager.PERMISSION_DENIED) {
1393 return ACTIVITY_RESTRICTION_PERMISSION;
1394 }
1395
Christopher Tateff7add02015-08-17 10:23:22 -07001396 if (activityInfo.permission == null) {
1397 return ACTIVITY_RESTRICTION_NONE;
1398 }
1399
Svet Ganov99b60432015-06-27 13:15:22 -07001400 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1401 if (opCode == AppOpsManager.OP_NONE) {
1402 return ACTIVITY_RESTRICTION_NONE;
1403 }
1404
1405 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1406 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001407 if (!ignoreTargetSecurity) {
1408 return ACTIVITY_RESTRICTION_APPOP;
1409 }
Svet Ganov99b60432015-06-27 13:15:22 -07001410 }
1411
1412 return ACTIVITY_RESTRICTION_NONE;
1413 }
1414
Svetoslav7008b512015-06-24 18:47:07 -07001415 private int getActionRestrictionForCallingPackage(String action,
1416 String callingPackage, int callingPid, int callingUid) {
1417 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001418 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001419 }
1420
1421 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1422 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001423 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001424 }
1425
1426 final PackageInfo packageInfo;
1427 try {
1428 packageInfo = mService.mContext.getPackageManager()
1429 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1430 } catch (PackageManager.NameNotFoundException e) {
1431 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001432 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001433 }
1434
1435 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001436 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001437 }
1438
1439 if (mService.checkPermission(permission, callingPid, callingUid) ==
1440 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001441 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001442 }
1443
1444 final int opCode = AppOpsManager.permissionToOpCode(permission);
1445 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001446 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001447 }
1448
1449 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1450 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001451 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001452 }
1453
Svet Ganov99b60432015-06-27 13:15:22 -07001454 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001455 }
1456
Wale Ogunwaled046a012015-12-24 13:05:59 -08001457 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001458 if (r == null) {
1459 // Not sure what you are trying to do, but it is not going to work...
1460 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001461 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001462 final TaskRecord task = r.task;
1463 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001464 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001465 return false;
1466 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001467 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001468 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001469 }
1470
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001471 void setLaunchSource(int uid) {
1472 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1473 }
1474
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001475 void acquireLaunchWakelock() {
1476 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1477 throw new IllegalStateException("Calling must be system uid");
1478 }
1479 mLaunchingActivity.acquire();
1480 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1481 // To be safe, don't allow the wake lock to be held for too long.
1482 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1483 }
1484 }
1485
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001486 /**
1487 * Called when the frontmost task is idle.
1488 * @return the state of mService.mBooting before this was called.
1489 */
1490 private boolean checkFinishBootingLocked() {
1491 final boolean booting = mService.mBooting;
1492 boolean enableScreen = false;
1493 mService.mBooting = false;
1494 if (!mService.mBooted) {
1495 mService.mBooted = true;
1496 enableScreen = true;
1497 }
1498 if (booting || enableScreen) {
1499 mService.postFinishBooting(booting, enableScreen);
1500 }
1501 return booting;
1502 }
1503
Craig Mautnerf3333272013-04-22 10:55:53 -07001504 // Checked.
1505 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1506 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001507 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001508
Craig Mautnerf3333272013-04-22 10:55:53 -07001509 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001510 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 int NS = 0;
1512 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001513 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001514 boolean activityRemoved = false;
1515
Wale Ogunwale7d701172015-03-11 15:36:30 -07001516 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001517 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001518 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1519 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1521 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001522 if (fromTimeout) {
1523 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001524 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001525
1526 // This is a hack to semi-deal with a race condition
1527 // in the client where it can be constructed with a
1528 // newer configuration from when we asked it to launch.
1529 // We'll update with whatever configuration it now says
1530 // it used to launch.
1531 if (config != null) {
1532 r.configuration = config;
1533 }
1534
1535 // We are now idle. If someone is waiting for a thumbnail from
1536 // us, we can now deliver.
1537 r.idle = true;
1538
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001539 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001540 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001541 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001542 }
1543 }
1544
1545 if (allResumedActivitiesIdle()) {
1546 if (r != null) {
1547 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001548 }
1549
1550 if (mLaunchingActivity.isHeld()) {
1551 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1552 if (VALIDATE_WAKE_LOCK_CALLER &&
1553 Binder.getCallingUid() != Process.myUid()) {
1554 throw new IllegalStateException("Calling must be system uid");
1555 }
1556 mLaunchingActivity.release();
1557 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001558 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001559 }
1560
1561 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001562 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001563 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001564 if ((NF = mFinishingActivities.size()) > 0) {
1565 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 mFinishingActivities.clear();
1567 }
1568
Craig Mautnerf3333272013-04-22 10:55:53 -07001569 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001570 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001571 mStartingUsers.clear();
1572 }
1573
Craig Mautnerf3333272013-04-22 10:55:53 -07001574 // Stop any activities that are scheduled to do so but have been
1575 // waiting for the next one to start.
1576 for (int i = 0; i < NS; i++) {
1577 r = stops.get(i);
1578 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001579 if (stack != null) {
1580 if (r.finishing) {
1581 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1582 } else {
1583 stack.stopActivityLocked(r);
1584 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001585 }
1586 }
1587
1588 // Finish any activities that are scheduled to do so but have been
1589 // waiting for the next one to start.
1590 for (int i = 0; i < NF; i++) {
1591 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001592 final ActivityStack stack = r.task.stack;
1593 if (stack != null) {
1594 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1595 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 }
1597
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001598 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001599 // Complete user switch
1600 if (startingUsers != null) {
1601 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001602 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001603 }
1604 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001605 }
1606
1607 mService.trimApplications();
1608 //dump();
1609 //mWindowManager.dump();
1610
Craig Mautnerf3333272013-04-22 10:55:53 -07001611 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001612 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001613 }
1614
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001615 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001616 }
1617
Craig Mautner8e569572013-10-11 17:36:59 -07001618 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001619 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001620 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1621 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001622 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1623 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1624 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001625 }
Craig Mautner19091252013-10-05 00:03:53 -07001626 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001627 }
1628
1629 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001632 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1633 stacks.get(stackNdx).closeSystemDialogsLocked();
1634 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001635 }
1636 }
1637
Craig Mautner93529a42013-10-04 15:03:13 -07001638 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001639 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001640 }
1641
Craig Mautner8d341ef2013-03-26 09:03:27 -07001642 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001643 * Update the last used stack id for non-current user (current user's last
1644 * used stack is the focused stack)
1645 */
1646 void updateUserStackLocked(int userId, ActivityStack stack) {
1647 if (userId != mCurrentUser) {
1648 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1649 }
1650 }
1651
1652 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001653 * @return true if some activity was finished (or would have finished if doit were true).
1654 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001655 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1656 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001657 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001658 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1659 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001660 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001661 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001662 if (stack.finishDisabledPackageActivitiesLocked(
1663 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001664 didSomething = true;
1665 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001666 }
1667 }
1668 return didSomething;
1669 }
1670
Dianne Hackborna413dc02013-07-12 12:02:55 -07001671 void updatePreviousProcessLocked(ActivityRecord r) {
1672 // Now that this process has stopped, we may want to consider
1673 // it to be the previous app to try to keep around in case
1674 // the user wants to return to it.
1675
1676 // First, found out what is currently the foreground app, so that
1677 // we don't blow away the previous app if this activity is being
1678 // hosted by the process that is actually still the foreground.
1679 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001680 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1681 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001682 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1683 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001684 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001685 if (stack.mResumedActivity != null) {
1686 fgApp = stack.mResumedActivity.app;
1687 } else if (stack.mPausingActivity != null) {
1688 fgApp = stack.mPausingActivity.app;
1689 }
1690 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001691 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001692 }
1693 }
1694
1695 // Now set this one as the previous process, only if that really
1696 // makes sense to.
1697 if (r.app != null && fgApp != null && r.app != fgApp
1698 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001699 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001700 mService.mPreviousProcess = r.app;
1701 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1702 }
1703 }
1704
Wale Ogunwaled046a012015-12-24 13:05:59 -08001705 boolean resumeFocusedStackTopActivityLocked() {
1706 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001707 }
1708
Wale Ogunwaled046a012015-12-24 13:05:59 -08001709 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001710 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001711 if (targetStack != null && isFocusedStack(targetStack)) {
1712 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001713 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001714 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1715 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001716 }
1717
Todd Kennedy39bfee52016-02-24 10:28:21 -08001718 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1720 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1722 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1723 }
1724 }
1725 }
1726
Adrian Roos20d7df32016-01-12 18:59:43 +01001727 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1728 TaskRecord finishedTask = null;
1729 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001730 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1731 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001732 final int numStacks = stacks.size();
1733 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1734 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001735 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1736 if (stack == focusedStack || finishedTask == null) {
1737 finishedTask = t;
1738 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001739 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001740 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001741 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001742 }
1743
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001744 void finishVoiceTask(IVoiceInteractionSession session) {
1745 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1746 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1747 final int numStacks = stacks.size();
1748 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1749 final ActivityStack stack = stacks.get(stackNdx);
1750 stack.finishVoiceTask(session);
1751 }
1752 }
1753 }
1754
Chong Zhang280d3322015-11-03 17:27:26 -08001755 void findTaskToMoveToFrontLocked(
1756 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001757 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1758 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001759 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001760 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1761 // Caller wants the home activity moved with it. To accomplish this,
1762 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001763 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001764 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001765 if (task.stack == null) {
1766 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1767 + task + " to front. Stack is null");
1768 return;
1769 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001770
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001771 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001772 int stackId = options.getLaunchStackId();
1773 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001774 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001775 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001776 if (stackId == INVALID_STACK_ID) {
1777 stackId = task.getLaunchStackId();
1778 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001779 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001780 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1781 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1782 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001783 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1784 // still need moveTaskToFrontLocked() below for any transition settings.
1785 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001786 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1787 resizeStackLocked(stackId, bounds,
1788 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1789 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1790 } else {
1791 // WM resizeTask must be done after the task is moved to the correct stack,
1792 // because Task's setBounds() also updates dim layer's bounds, but that has
1793 // dependency on the stack.
1794 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1795 false /* relayout */, false /* forced */);
1796 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001797 }
1798 }
1799
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001800 final ActivityRecord r = task.getTopActivity();
1801 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1802 r == null ? null : r.appTimeTracker, reason);
1803
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001804 if (DEBUG_STACK) Slog.d(TAG_STACK,
1805 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001806
1807 showNonResizeableDockToastIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001808 }
1809
Wale Ogunwale854809c2015-12-27 16:18:19 -08001810 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001811 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001812 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001813 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001814 return false;
1815 }
1816 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1817 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001818 }
1819
Craig Mautner967212c2013-04-13 21:10:58 -07001820 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001821 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001822 }
1823
1824 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001825 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1826 if (activityContainer != null) {
1827 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001828 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001829 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001830 return null;
1831 }
1832 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001833 }
1834
Craig Mautner967212c2013-04-13 21:10:58 -07001835 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001836 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1838 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001839 }
1840 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001841 }
1842
Craig Mautner4a1cb222013-12-04 16:14:06 -08001843 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001844 ActivityRecord homeActivity = getHomeActivity();
1845 if (homeActivity != null) {
1846 return homeActivity.appToken;
1847 }
1848 return null;
1849 }
1850
1851 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001852 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001853 }
1854
1855 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001856 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1857 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1858 final TaskRecord task = tasks.get(taskNdx);
1859 if (task.isHomeTask()) {
1860 final ArrayList<ActivityRecord> activities = task.mActivities;
1861 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1862 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001863 if (r.isHomeActivity()
1864 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001865 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001866 }
1867 }
1868 }
1869 }
1870 return null;
1871 }
1872
Chong Zhangb15758a2015-11-17 12:12:03 -08001873 /**
1874 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1875 * the docked stack itself, or if it's side-by-side to the docked stack.
1876 */
1877 boolean isStackDockedInEffect(int stackId) {
1878 return stackId == DOCKED_STACK_ID ||
1879 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1880 }
1881
Todd Kennedyca4d8422015-01-15 15:19:22 -08001882 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001883 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001884 ActivityContainer activityContainer =
1885 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001886 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001887 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1888 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001889 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001890 return activityContainer;
1891 }
1892
Craig Mautner34b73df2014-01-12 21:11:08 -08001893 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001894 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1895 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1896 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001897 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1898 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001899 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001900 }
1901 }
1902
Craig Mautner95da1082014-02-24 17:54:35 -08001903 void deleteActivityContainer(IActivityContainer container) {
1904 ActivityContainer activityContainer = (ActivityContainer)container;
1905 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001906 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1907 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001908 final int stackId = activityContainer.mStackId;
1909 mActivityContainers.remove(stackId);
1910 mWindowManager.removeStack(stackId);
1911 }
1912 }
1913
Jorim Jaggidc249c42015-12-15 14:57:31 -08001914 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1915 boolean preserveWindows, boolean allowResizeInDockedMode) {
1916 if (stackId == DOCKED_STACK_ID) {
1917 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1918 preserveWindows);
1919 return;
1920 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001921 final ActivityStack stack = getStack(stackId);
1922 if (stack == null) {
1923 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1924 return;
1925 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001926
Jorim Jaggidc249c42015-12-15 14:57:31 -08001927 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001928 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1929 // stack size changing so things don't get out of sync.
1930 return;
1931 }
1932
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001933 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001934 mWindowManager.deferSurfaceLayout();
1935 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001936 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1937 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001938 } finally {
1939 mWindowManager.continueSurfaceLayout();
1940 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001941 }
1942 }
1943
Jorim Jaggi192086e2016-03-11 17:17:03 +01001944 void deferUpdateBounds(int stackId) {
1945 final ActivityStack stack = getStack(stackId);
1946 if (stack != null) {
1947 stack.deferUpdateBounds();
1948 }
1949 }
1950
1951 void continueUpdateBounds(int stackId) {
1952 final ActivityStack stack = getStack(stackId);
1953 if (stack != null) {
1954 stack.continueUpdateBounds();
1955 }
1956 }
1957
1958 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001959 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001960 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001961
Jorim Jaggi192086e2016-03-11 17:17:03 +01001962 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1963 return;
1964 }
1965
Jorim Jaggidc249c42015-12-15 14:57:31 -08001966 mTmpBounds.clear();
1967 mTmpConfigs.clear();
1968 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001969 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001970 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001971 final TaskRecord task = tasks.get(i);
1972 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001973 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1974 // For freeform stack we don't adjust the size of the tasks to match that
1975 // of the stack, but we do try to make sure the tasks are still contained
1976 // with the bounds of the stack.
1977 tempRect2.set(task.mBounds);
1978 fitWithinBounds(tempRect2, bounds);
1979 task.updateOverrideConfiguration(tempRect2);
1980 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001981 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001982 tempTaskBounds != null ? tempTaskBounds : bounds,
1983 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001984 }
1985 }
1986
1987 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1988 mTmpBounds.put(task.taskId, task.mBounds);
1989 if (tempTaskInsetBounds != null) {
1990 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1991 }
1992 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001993
1994 // We might trigger a configuration change. Save the current task bounds for freezing.
1995 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001996 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1997 mTmpBounds, mTmpInsetBounds);
1998 stack.setBounds(bounds);
1999 }
2000
2001 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2002 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002003 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002004 return;
2005 }
2006 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2007 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002008 if (!updated) {
2009 resumeFocusedStackTopActivityLocked();
2010 }
2011 }
2012
2013 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2014 Rect tempDockedTaskInsetBounds,
2015 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2016 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2017 if (stack == null) {
2018 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2019 return;
2020 }
2021
2022 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2023 mWindowManager.deferSurfaceLayout();
2024 try {
2025 ActivityRecord r = stack.topRunningActivityLocked();
2026 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2027 tempDockedTaskInsetBounds);
2028
2029 if (stack.mFullscreen) {
2030 // The dock stack went fullscreen which is kinda like dismissing it.
2031 // In this case we make all other static stacks fullscreen and move all
2032 // docked stack tasks to the fullscreen stack.
2033 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2034 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2035 resizeStackLocked(i, null, null, null, preserveWindows,
2036 true /* allowResizeInDockedMode */);
2037 }
2038 }
2039
2040 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2041 final int count = tasks.size();
2042 for (int i = 0; i < count; i++) {
2043 moveTaskToStackLocked(tasks.get(i).taskId,
2044 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2045 false /* animate */);
2046 }
2047
2048 // stack shouldn't contain anymore activities, so nothing to resume.
2049 r = null;
2050 } else {
2051 // Docked stacks occupy a dedicated region on screen so the size of all other
2052 // static stacks need to be adjusted so they don't overlap with the docked stack.
2053 // We get the bounds to use from window manager which has been adjusted for any
2054 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002055 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002056 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002057 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2058 if (StackId.isResizeableByDockedStack(i)) {
2059 ActivityStack otherStack = getStack(i);
2060 if (otherStack != null) {
2061 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2062 tempOtherTaskInsetBounds, preserveWindows,
2063 true /* allowResizeInDockedMode */);
2064 }
2065 }
2066 }
2067 }
2068 ensureConfigurationAndResume(stack, r, preserveWindows);
2069 } finally {
2070 mWindowManager.continueSurfaceLayout();
2071 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2072 }
2073
2074 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2075 tempDockedTaskBounds != null
2076 || tempDockedTaskInsetBounds != null
2077 || tempOtherTaskBounds != null
2078 || tempOtherTaskInsetBounds != null);
2079 }
2080
Robert Carr0d00c2e2016-02-29 17:45:02 -08002081 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2082 final ActivityStack stack = getStack(PINNED_STACK_ID);
2083 if (stack == null) {
2084 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2085 return;
2086 }
2087 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2088 mWindowManager.deferSurfaceLayout();
2089 try {
2090 ActivityRecord r = stack.topRunningActivityLocked();
2091 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2092 null);
2093 ensureConfigurationAndResume(stack, r, false);
2094 } finally {
2095 mWindowManager.continueSurfaceLayout();
2096 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2097 }
2098 }
2099
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002100 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2101 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002102 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002103 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002104 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002105 }
2106
Chong Zhang87b21722015-09-21 15:39:51 -07002107 // If this is a forced resize, let it go through even if the bounds is not changing,
2108 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002109 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002110 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002111 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002112 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002113 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002114 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002115
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002116 if (!mWindowManager.isValidTaskId(task.taskId)) {
2117 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002118 // All we can do for now is update the bounds so it can be used when the task is
2119 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002120 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002121 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2122 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002123 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002124 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002125 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002126 }
2127
Chong Zhang6de2ae82015-09-30 18:25:21 -07002128 // Do not move the task to another stack here.
2129 // This method assumes that the task is already placed in the right stack.
2130 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002131
Chong Zhang6de2ae82015-09-30 18:25:21 -07002132 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002133
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002134 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002135 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002136 // way and the activity was kept the way it was. If it's false, it means the activity had
2137 // to be relaunched due to configuration change.
2138 boolean kept = true;
2139 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002140 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002141 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002142 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002143 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002144
2145 if (!deferResume) {
2146
2147 // All other activities must be made visible with their correct configuration.
2148 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2149 if (!kept) {
2150 resumeFocusedStackTopActivityLocked();
2151 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002152 }
2153 }
2154 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002155 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002156
2157 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002158 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002159 }
2160
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002161 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002162 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2163 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002164 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002165 }
2166
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002167 ActivityContainer activityContainer = new ActivityContainer(stackId);
2168 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002169 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002170 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002171 }
2172
2173 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002174 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002175 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2176 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002177 break;
2178 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002179 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002180 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002181 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002182 }
2183
Chong Zhang5dcb2752015-08-18 13:50:26 -07002184 /**
2185 * Restores a recent task to a stack
2186 * @param task The recent task to be restored.
2187 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002188 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002189 * @return true if the task has been restored successfully.
2190 */
2191 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2192 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002193 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002194 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2195 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2196 // Put it in the fullscreen stack.
2197 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002198 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002199
Wale Ogunwale706ed792015-08-02 10:29:44 -07002200 if (task.stack != null) {
2201 // Task has already been restored once. See if we need to do anything more
2202 if (task.stack.mStackId == stackId) {
2203 // Nothing else to do since it is already restored in the right stack.
2204 return true;
2205 }
2206 // Remove current stack association, so we can re-associate the task with the
2207 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002208 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002209 }
2210
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002211 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002212 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002213
2214 if (stack == null) {
2215 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002216 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2217 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002218 return false;
2219 }
2220
Wale Ogunwale5f986092015-12-04 15:35:38 -08002221 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002222 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2223 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002224 final ArrayList<ActivityRecord> activities = task.mActivities;
2225 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002226 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002227 }
2228 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002229 }
2230
Wale Ogunwale040b4702015-08-06 18:10:50 -07002231 /**
2232 * Moves the specified task record to the input stack id.
2233 * WARNING: This method performs an unchecked/raw move of the task and
2234 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002235 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002236 * @param task Task to move.
2237 * @param stackId Id of stack to move task to.
2238 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002239 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002240 * @param reason Reason the task is been moved.
2241 * @return The stack the task was moved to.
2242 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002243 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002244 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002245
2246 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2247 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2248 + "support multi-window task=" + task + " to stackId=" + stackId);
2249 }
2250
Chong Zhang02898352015-08-21 17:27:14 -07002251 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002252 final ActivityStack prevStack = task.stack;
2253 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2254 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2255 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2256 // Whenever we are moving the top activity from the front stack we want to make sure to move
2257 // the stack to the front.
2258 final boolean wasFront = isFrontStack(prevStack)
2259 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002260
Chong Zhangd545dce2016-02-29 18:09:17 -08002261 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002262 // We don't allow moving a unresizeable task to the docked stack since the docked
2263 // stack is used for split-screen mode and will cause things like the docked divider to
2264 // show up. We instead leave the task in its current stack or move it to the fullscreen
2265 // stack if it isn't currently in a stack.
2266 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002267 Slog.w(TAG, "Can not move unresizeable task=" + task
2268 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2269 }
2270
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002271 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2272 // if a docked stack is created below which will lead to the stack we are moving from and
2273 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002274 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002275 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002276 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002277 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002278 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002279
2280 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002281 // move focus to the new stack by moving the stack to the front.
2282 stack.moveToFrontAndResumeStateIfNeeded(
2283 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002284
Wale Ogunwale040b4702015-08-06 18:10:50 -07002285 return stack;
2286 }
2287
Chong Zhange4fbd322016-03-01 14:44:03 -08002288 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002289 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002290 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2291 false /* deferResume */);
2292 }
2293
2294 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2295 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002296 final TaskRecord task = anyTaskForIdLocked(taskId);
2297 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002298 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002299 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002300 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002301
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002302 if (task.stack != null && task.stack.mStackId == stackId) {
2303 // You are already in the right stack silly...
2304 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002305 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002306 }
2307
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002308 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2309 throw new IllegalArgumentException("moveTaskToStack:"
2310 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2311 }
2312
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002313 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002314 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002315 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002316 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002317 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002318 // We are about to relaunch the activity because its configuration changed due to
2319 // being maximized, i.e. size change. The activity will first remove the old window
2320 // and then add a new one. This call will tell window manager about this, so it can
2321 // preserve the old window until the new one is drawn. This prevents having a gap
2322 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002323 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002324 // Note here we always set the replacing window first, as the flags might be needed
2325 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002326 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002327 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002328
2329 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002330 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002331 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002332 try {
2333 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002334 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002335 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002336
Wale Ogunwale961f4852016-02-01 20:25:54 -08002337 if (!animate) {
2338 stack.mNoAnimActivities.add(topActivity);
2339 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002340
Wale Ogunwale961f4852016-02-01 20:25:54 -08002341 // We might trigger a configuration change. Save the current task bounds for freezing.
2342 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2343
2344 // Make sure the task has the appropriate bounds/size for the stack it is in.
2345 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002346 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2347 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002348 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2349 Rect bounds = task.getLaunchBounds();
2350 if (bounds == null) {
2351 stack.layoutTaskInStack(task, null);
2352 bounds = task.mBounds;
2353 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002354 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2355 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002356 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002357 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2358 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002359 }
2360 } finally {
2361 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002362 }
2363
2364 if (mightReplaceWindow) {
2365 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2366 // window), we need to clear the replace window settings. Otherwise, we schedule a
2367 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002368 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002369 }
2370
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002371 if (!deferResume) {
2372
2373 // The task might have already been running and its visibility needs to be synchronized with
2374 // the visibility of the stack / windows.
2375 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2376 resumeFocusedStackTopActivityLocked();
2377 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002378
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002379 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002380
2381 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002382 }
2383
Wale Ogunwale079a0042015-10-24 11:44:07 -07002384 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2385 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2386 if (stack == null) {
2387 throw new IllegalArgumentException(
2388 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2389 }
2390
2391 final ActivityRecord r = stack.topRunningActivityLocked();
2392 if (r == null) {
2393 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2394 + " in stack=" + stack);
2395 return false;
2396 }
2397
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002398 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002399 Slog.w(TAG,
2400 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002401 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002402 return false;
2403 }
2404
Wale Ogunwale480dca02016-02-06 13:58:29 -08002405 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002406 return true;
2407 }
2408
Wale Ogunwale480dca02016-02-06 13:58:29 -08002409 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2410 mWindowManager.deferSurfaceLayout();
2411 try {
2412 final TaskRecord task = r.task;
2413
2414 // Need to make sure the pinned stack exist so we can resize it below...
2415 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2416
2417 // Resize the pinned stack to match the current size of the task the activity we are
2418 // going to be moving is currently contained in. We do this to have the right starting
2419 // animation bounds for the pinned stack to the desired bounds the caller wants.
2420 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2421 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2422 true /* allowResizeInDockedMode */);
2423
2424 if (task.mActivities.size() == 1) {
2425 // There is only one activity in the task. So, we can just move the task over to
2426 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002427 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2428 // Move the home stack forward if the task we just moved to the pinned stack
2429 // was launched from home so home should be visible behind it.
2430 moveHomeStackToFront(reason);
2431 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002432 moveTaskToStackLocked(
2433 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2434 } else {
2435 stack.moveActivityToStack(r);
2436 }
2437 } finally {
2438 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002439 }
2440
Wale Ogunwale480dca02016-02-06 13:58:29 -08002441 // The task might have already been running and its visibility needs to be synchronized
2442 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002443 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002444 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002445
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002446 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002447 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002448 }
2449
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002450 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2451 final TaskRecord task = anyTaskForIdLocked(taskId);
2452 if (task == null) {
2453 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2454 return;
2455 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002456 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2457
2458 task.updateOverrideConfigurationForStack(stack);
2459
2460 mWindowManager.positionTaskInStack(
2461 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002462 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002463 // The task might have already been running and its visibility needs to be synchronized with
2464 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002465 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2466 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002467 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002468 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002469 }
2470
Craig Mautnerac6f8432013-07-17 13:24:59 -07002471 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002472 mTmpFindTaskResult.r = null;
2473 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002474 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002475 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2476 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002477 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2478 final ActivityStack stack = stacks.get(stackNdx);
2479 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002480 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002481 continue;
2482 }
2483 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002484 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2485 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 continue;
2487 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002488 stack.findTaskLocked(r, mTmpFindTaskResult);
2489 // It is possible to have task in multiple stacks with the same root affinity.
2490 // If the match we found was based on root affinity we keep on looking to see if
2491 // there is a better match in another stack. We eventually return the match based
2492 // on root affinity if we don't find a better match.
2493 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2494 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002496 }
2497 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002498 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2499 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002500 }
2501
2502 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002503 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2504 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002505 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2506 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2507 if (ar != null) {
2508 return ar;
2509 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002510 }
2511 }
2512 return null;
2513 }
2514
Craig Mautner8d341ef2013-03-26 09:03:27 -07002515 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002516 scheduleSleepTimeout();
2517 if (!mGoingToSleep.isHeld()) {
2518 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002519 if (mLaunchingActivity.isHeld()) {
2520 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2521 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002522 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002523 mLaunchingActivity.release();
2524 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002525 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002526 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002527 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002528 }
2529
2530 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002531 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002532
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002533 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002534 final long endTime = System.currentTimeMillis() + timeout;
2535 while (true) {
2536 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2538 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2540 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2541 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002542 }
2543 if (cantShutdown) {
2544 long timeRemaining = endTime - System.currentTimeMillis();
2545 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002547 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002548 } catch (InterruptedException e) {
2549 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002550 } else {
2551 Slog.w(TAG, "Activity manager shutdown timed out");
2552 timedout = true;
2553 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002554 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002555 } else {
2556 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002557 }
2558 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002559
2560 // Force checkReadyForSleep to complete.
2561 mSleepTimeout = true;
2562 checkReadyForSleepLocked();
2563
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 return timedout;
2565 }
2566
2567 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002568 removeSleepTimeouts();
2569 if (mGoingToSleep.isHeld()) {
2570 mGoingToSleep.release();
2571 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002572 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2573 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2575 final ActivityStack stack = stacks.get(stackNdx);
2576 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002577 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002578 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002579 }
Craig Mautner5314a402013-09-26 12:40:16 -07002580 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002581 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002582 mGoingToSleepActivities.clear();
2583 }
2584
2585 void activitySleptLocked(ActivityRecord r) {
2586 mGoingToSleepActivities.remove(r);
2587 checkReadyForSleepLocked();
2588 }
2589
2590 void checkReadyForSleepLocked() {
2591 if (!mService.isSleepingOrShuttingDown()) {
2592 // Do not care.
2593 return;
2594 }
2595
2596 if (!mSleepTimeout) {
2597 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002598 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2599 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002600 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2601 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2602 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002603 }
2604
2605 if (mStoppingActivities.size() > 0) {
2606 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002607 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002608 + mStoppingActivities.size() + " activities");
2609 scheduleIdleLocked();
2610 dontSleep = true;
2611 }
2612
2613 if (mGoingToSleepActivities.size() > 0) {
2614 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002615 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002616 + mGoingToSleepActivities.size() + " activities");
2617 dontSleep = true;
2618 }
2619
2620 if (dontSleep) {
2621 return;
2622 }
2623 }
2624
Craig Mautnere0a38842013-12-16 16:14:02 -08002625 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2626 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002627 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2628 stacks.get(stackNdx).goToSleep();
2629 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002630 }
2631
2632 removeSleepTimeouts();
2633
2634 if (mGoingToSleep.isHeld()) {
2635 mGoingToSleep.release();
2636 }
2637 if (mService.mShuttingDown) {
2638 mService.notifyAll();
2639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002640 }
2641
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002642 boolean reportResumedActivityLocked(ActivityRecord r) {
2643 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002644 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002645 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002646 }
2647 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002648 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002649 mWindowManager.executeAppTransition();
2650 return true;
2651 }
2652 return false;
2653 }
2654
Craig Mautner8d341ef2013-03-26 09:03:27 -07002655 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002656 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2657 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002658 int stackNdx = stacks.size() - 1;
2659 while (stackNdx >= 0) {
2660 stacks.get(stackNdx).handleAppCrashLocked(app);
2661 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002662 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002663 }
2664 }
2665
Jose Lima4b6c6692014-08-12 17:41:12 -07002666 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002667 final ActivityStack stack = r.task.stack;
2668 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002669 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2670 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002671 return false;
2672 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002673 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002674 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2675 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002676
2677 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002678 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002679 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002680 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002681 return true;
2682 }
2683
2684 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002685 if (visible && top.fullscreen) {
2686 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002687 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2688 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2689 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2690 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002691 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002692 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2693 // Only the activity set as currently visible behind should actively reset its
2694 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002695 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2696 "requestVisibleBehind: returning visible=" + visible
2697 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2698 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002699 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002700 }
2701
Jose Lima4b6c6692014-08-12 17:41:12 -07002702 stack.setVisibleBehindActivity(visible ? r : null);
2703 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002704 // If there is a translucent home activity, we need to force it stop being translucent,
2705 // because we can't depend on the application to necessarily perform that operation.
2706 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002707 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002708 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002709 mService.convertFromTranslucent(next.appToken);
2710 }
2711 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002712 if (top.app != null && top.app.thread != null) {
2713 // Notify the top app of the change.
2714 try {
2715 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2716 } catch (RemoteException e) {
2717 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002718 }
2719 return true;
2720 }
2721
Craig Mautnerbb742462014-07-07 15:28:55 -07002722 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2723 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2724 r.mLaunchTaskBehind = false;
2725 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002726 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002727 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002728 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002729 mWindowManager.setAppVisibility(r.appToken, false);
2730 }
2731
2732 void scheduleLaunchTaskBehindComplete(IBinder token) {
2733 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2734 }
2735
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002736 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2737 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002738 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002739 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2740 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002741 final int topStackNdx = stacks.size() - 1;
2742 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2743 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002744 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002745 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002747 }
2748
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002749 void invalidateTaskLayers() {
2750 mTaskLayersChanged = true;
2751 }
2752
2753 void rankTaskLayersIfNeeded() {
2754 if (!mTaskLayersChanged) {
2755 return;
2756 }
2757 mTaskLayersChanged = false;
2758 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2759 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2760 int baseLayer = 0;
2761 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2762 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2763 }
2764 }
2765 }
2766
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002767 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2769 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2770 final int topStackNdx = stacks.size() - 1;
2771 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2772 final ActivityStack stack = stacks.get(stackNdx);
2773 stack.clearOtherAppTimeTrackers(except);
2774 }
2775 }
2776 }
2777
Craig Mautner8d341ef2013-03-26 09:03:27 -07002778 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002779 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2780 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002781 final int numStacks = stacks.size();
2782 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2783 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002784 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002785 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 }
2787 }
2788
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002789 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2790 // Examine all activities currently running in the process.
2791 TaskRecord firstTask = null;
2792 // Tasks is non-null only if two or more tasks are found.
2793 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002794 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2795 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002796 ActivityRecord r = app.activities.get(i);
2797 // First, if we find an activity that is in the process of being destroyed,
2798 // then we just aren't going to do anything for now; we want things to settle
2799 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002800 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002801 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002802 return;
2803 }
2804 // Don't consider any activies that are currently not in a state where they
2805 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002806 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2807 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002808 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002809 continue;
2810 }
2811 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002812 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002813 + " from " + r);
2814 if (firstTask == null) {
2815 firstTask = r.task;
2816 } else if (firstTask != r.task) {
2817 if (tasks == null) {
2818 tasks = new ArraySet<>();
2819 tasks.add(firstTask);
2820 }
2821 tasks.add(r.task);
2822 }
2823 }
2824 }
2825 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002827 return;
2828 }
2829 // If we have activities in multiple tasks that are in a position to be destroyed,
2830 // let's iterate through the tasks and release the oldest one.
2831 final int numDisplays = mActivityDisplays.size();
2832 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2833 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2834 // Step through all stacks starting from behind, to hit the oldest things first.
2835 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2836 final ActivityStack stack = stacks.get(stackNdx);
2837 // Try to release activities in this stack; if we manage to, we are done.
2838 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2839 return;
2840 }
2841 }
2842 }
2843 }
2844
Amith Yamasani37a40c22015-06-17 13:25:42 -07002845 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002846 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002847 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002848 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002849
Craig Mautner858d8a62013-04-23 17:08:34 -07002850 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2852 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002854 final ActivityStack stack = stacks.get(stackNdx);
2855 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002856 TaskRecord task = stack.topTask();
2857 if (task != null) {
2858 mWindowManager.moveTaskToTop(task.taskId);
2859 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002860 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002861 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002862
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002863 ActivityStack stack = getStack(restoreStackId);
2864 if (stack == null) {
2865 stack = mHomeStack;
2866 }
2867 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002868 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002869 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002870 } else {
2871 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002872 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002873 }
Craig Mautner93529a42013-10-04 15:03:13 -07002874 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002875 }
2876
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002877 /** Checks whether the userid is a profile of the current user. */
2878 boolean isCurrentProfileLocked(int userId) {
2879 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002880 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002881 }
2882
Chong Zhang45c25ce2015-08-10 22:18:26 -07002883 /** Checks whether the activity should be shown for current user. */
2884 boolean okToShowLocked(ActivityRecord r) {
2885 return r != null && (isCurrentProfileLocked(r.userId)
2886 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2887 }
2888
Craig Mautnerde4ef022013-04-07 19:01:33 -07002889 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002890 ArrayList<ActivityRecord> stops = null;
2891
2892 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002893 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2894 ActivityRecord s = mStoppingActivities.get(activityNdx);
2895 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002896 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002897 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2898 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002899 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002900 if (s.finishing) {
2901 // If this activity is finishing, it is sitting on top of
2902 // everyone else but we now know it is no longer needed...
2903 // so get rid of it. Otherwise, we need to go through the
2904 // normal flow and hide it once we determine that it is
2905 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002906 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002907 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002908 }
2909 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002910 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002911 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002912 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002913 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002914 }
2915 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002916 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002917 }
2918 }
2919
2920 return stops;
2921 }
2922
Craig Mautnercf910b02013-04-23 11:23:27 -07002923 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2927 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002928 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002929 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002930 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002931 if (r == null) Slog.e(TAG,
2932 "validateTop...: null top activity, stack=" + stack);
2933 else {
2934 final ActivityRecord pausing = stack.mPausingActivity;
2935 if (pausing != null && pausing == r) Slog.e(TAG,
2936 "validateTop...: top stack has pausing activity r=" + r
2937 + " state=" + state);
2938 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2939 "validateTop...: activity in front not resumed r=" + r
2940 + " state=" + state);
2941 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002942 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002943 final ActivityRecord resumed = stack.mResumedActivity;
2944 if (resumed != null && resumed == r) Slog.e(TAG,
2945 "validateTop...: back stack has resumed activity r=" + r
2946 + " state=" + state);
2947 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2948 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002949 }
2950 }
2951 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002952 }
2953
Craig Mautnere0570202015-05-13 13:06:11 -07002954 private String lockTaskModeToString() {
2955 switch (mLockTaskModeState) {
2956 case LOCK_TASK_MODE_LOCKED:
2957 return "LOCKED";
2958 case LOCK_TASK_MODE_PINNED:
2959 return "PINNED";
2960 case LOCK_TASK_MODE_NONE:
2961 return "NONE";
2962 default: return "unknown=" + mLockTaskModeState;
2963 }
2964 }
2965
Craig Mautner27084302013-03-25 08:05:25 -07002966 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002967 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002968 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002969 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002970 pw.print(prefix);
2971 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002972 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002973 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002974 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2975 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2976 if (packages.size() > 0) {
2977 pw.println(" mLockTaskPackages (userId:packages)=");
2978 for (int i = 0; i < packages.size(); ++i) {
2979 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2980 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2981 }
2982 }
2983 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002984 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002985
Craig Mautner20e72272013-04-01 13:45:53 -07002986 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002987 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002988 }
2989
Dianne Hackborn390517b2013-05-30 15:03:32 -07002990 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2991 boolean needSep, String prefix) {
2992 if (activity != null) {
2993 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2994 if (needSep) {
2995 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002996 }
2997 pw.print(prefix);
2998 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002999 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003000 }
3001 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003002 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003003 }
3004
Craig Mautner8d341ef2013-03-26 09:03:27 -07003005 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3006 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003007 boolean printed = false;
3008 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003009 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3010 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003011 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003012 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003013 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 final ActivityStack stack = stacks.get(stackNdx);
3016 StringBuilder stackHeader = new StringBuilder(128);
3017 stackHeader.append(" Stack #");
3018 stackHeader.append(stack.mStackId);
3019 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003020 stackHeader.append("\n");
3021 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3022 stackHeader.append("\n");
3023 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3025 needSep, stackHeader.toString());
3026 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3027 !dumpAll, false, dumpPackage, true,
3028 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003029
Craig Mautner4a1cb222013-12-04 16:14:06 -08003030 needSep = printed;
3031 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3032 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003033 if (pr) {
3034 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003035 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003036 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3038 " mResumedActivity: ");
3039 if (pr) {
3040 printed = true;
3041 needSep = false;
3042 }
3043 if (dumpAll) {
3044 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3045 " mLastPausedActivity: ");
3046 if (pr) {
3047 printed = true;
3048 needSep = true;
3049 }
3050 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3051 needSep, " mLastNoHistoryActivity: ");
3052 }
3053 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003054 }
3055 }
3056
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003057 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3058 false, dumpPackage, true, " Activities waiting to finish:", null);
3059 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3060 false, dumpPackage, true, " Activities waiting to stop:", null);
3061 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3062 false, dumpPackage, true, " Activities waiting for another to become visible:",
3063 null);
3064 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3065 false, dumpPackage, true, " Activities waiting to sleep:", null);
3066 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3067 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003068
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003069 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003070 }
3071
Dianne Hackborn390517b2013-05-30 15:03:32 -07003072 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003074 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003075 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003076 String innerPrefix = null;
3077 String[] args = null;
3078 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003079 for (int i=list.size()-1; i>=0; i--) {
3080 final ActivityRecord r = list.get(i);
3081 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3082 continue;
3083 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003084 if (innerPrefix == null) {
3085 innerPrefix = prefix + " ";
3086 args = new String[0];
3087 }
3088 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003089 final boolean full = !brief && (complete || !r.isInHistory());
3090 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003091 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003092 needNL = false;
3093 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003094 if (header1 != null) {
3095 pw.println(header1);
3096 header1 = null;
3097 }
3098 if (header2 != null) {
3099 pw.println(header2);
3100 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003101 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003102 if (lastTask != r.task) {
3103 lastTask = r.task;
3104 pw.print(prefix);
3105 pw.print(full ? "* " : " ");
3106 pw.println(lastTask);
3107 if (full) {
3108 lastTask.dump(pw, prefix + " ");
3109 } else if (complete) {
3110 // Complete + brief == give a summary. Isn't that obvious?!?
3111 if (lastTask.intent != null) {
3112 pw.print(prefix); pw.print(" ");
3113 pw.println(lastTask.intent.toInsecureStringWithClip());
3114 }
3115 }
3116 }
3117 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3118 pw.print(" #"); pw.print(i); pw.print(": ");
3119 pw.println(r);
3120 if (full) {
3121 r.dump(pw, innerPrefix);
3122 } else if (complete) {
3123 // Complete + brief == give a summary. Isn't that obvious?!?
3124 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3125 if (r.app != null) {
3126 pw.print(innerPrefix); pw.println(r.app);
3127 }
3128 }
3129 if (client && r.app != null && r.app.thread != null) {
3130 // flush anything that is already in the PrintWriter since the thread is going
3131 // to write to the file descriptor directly
3132 pw.flush();
3133 try {
3134 TransferPipe tp = new TransferPipe();
3135 try {
3136 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3137 r.appToken, innerPrefix, args);
3138 // Short timeout, since blocking here can
3139 // deadlock with the application.
3140 tp.go(fd, 2000);
3141 } finally {
3142 tp.kill();
3143 }
3144 } catch (IOException e) {
3145 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3146 } catch (RemoteException e) {
3147 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3148 }
3149 needNL = true;
3150 }
3151 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003152 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003153 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003154
Craig Mautnerf3333272013-04-22 10:55:53 -07003155 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3157 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003158 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3159 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003160 }
3161
3162 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003163 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003164 }
3165
3166 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003167 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3168 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003169 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3170 }
3171
Craig Mautner05d29032013-05-03 13:40:13 -07003172 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003173 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3174 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3175 }
Craig Mautner05d29032013-05-03 13:40:13 -07003176 }
3177
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178 void removeSleepTimeouts() {
3179 mSleepTimeout = false;
3180 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3181 }
3182
3183 final void scheduleSleepTimeout() {
3184 removeSleepTimeouts();
3185 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3186 }
3187
Craig Mautner4a1cb222013-12-04 16:14:06 -08003188 @Override
3189 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003190 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3192 }
3193
3194 @Override
3195 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003196 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3198 }
3199
3200 @Override
3201 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003202 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3204 }
3205
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003206 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003207 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003208 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003209 newDisplay = mActivityDisplays.get(displayId) == null;
3210 if (newDisplay) {
3211 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003212 if (activityDisplay.mDisplay == null) {
3213 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3214 return;
3215 }
Craig Mautner4504de52013-12-20 09:06:56 -08003216 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003217 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003218 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 }
Craig Mautner4504de52013-12-20 09:06:56 -08003220 if (newDisplay) {
3221 mWindowManager.onDisplayAdded(displayId);
3222 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 }
3224
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003225 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3226 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3227 return;
3228 }
3229 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3230 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3231 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3232 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3233 }
3234
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003235 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003237 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3238 if (activityDisplay != null) {
3239 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003240 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003241 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003242 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003243 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 }
3245 }
3246 mWindowManager.onDisplayRemoved(displayId);
3247 }
3248
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003249 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003250 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003251 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3252 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003253 // TODO: Update the bounds.
3254 }
3255 }
3256 mWindowManager.onDisplayChanged(displayId);
3257 }
3258
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003259 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003260 StackInfo info = new StackInfo();
3261 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3262 info.displayId = Display.DEFAULT_DISPLAY;
3263 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003264 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003265
3266 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3267 final int numTasks = tasks.size();
3268 int[] taskIds = new int[numTasks];
3269 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003270 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003271 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003272 for (int i = 0; i < numTasks; ++i) {
3273 final TaskRecord task = tasks.get(i);
3274 taskIds[i] = task.taskId;
3275 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3276 : task.realActivity != null ? task.realActivity.flattenToString()
3277 : task.getTopActivity() != null ? task.getTopActivity().packageName
3278 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003279 taskBounds[i] = new Rect();
3280 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003281 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003282 }
3283 info.taskIds = taskIds;
3284 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003285 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003286 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 return info;
3288 }
3289
3290 StackInfo getStackInfoLocked(int stackId) {
3291 ActivityStack stack = getStack(stackId);
3292 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003293 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003294 }
3295 return null;
3296 }
3297
3298 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003299 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003300 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3301 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003303 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003304 }
3305 }
3306 return list;
3307 }
3308
Craig Mautner15df08a2015-04-01 12:17:18 -07003309 TaskRecord getLockedTaskLocked() {
3310 final int top = mLockTaskModeTasks.size() - 1;
3311 if (top >= 0) {
3312 return mLockTaskModeTasks.get(top);
3313 }
3314 return null;
3315 }
3316
3317 boolean isLockedTask(TaskRecord task) {
3318 return mLockTaskModeTasks.contains(task);
3319 }
3320
3321 boolean isLastLockedTask(TaskRecord task) {
3322 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3323 }
3324
3325 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003326 if (!mLockTaskModeTasks.remove(task)) {
3327 return;
3328 }
3329 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3330 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003331 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003332 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3333 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003334 final Message lockTaskMsg = Message.obtain();
3335 lockTaskMsg.arg1 = task.userId;
3336 lockTaskMsg.what = LOCK_TASK_END_MSG;
3337 mHandler.sendMessage(lockTaskMsg);
3338 }
3339 }
3340
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003341 void showNonResizeableDockToastIfNeeded(
3342 TaskRecord task, int preferredStackId, int actualStackId) {
3343 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3344 return;
3345 }
3346
Jorim Jaggi2adba072016-03-03 13:43:39 +01003347 if (!task.canGoInDockedStack()) {
3348 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003349 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003350 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3351 String packageName = task.getTopActivity() != null
3352 ? task.getTopActivity().appInfo.packageName : null;
3353 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3354 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003355 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003356 }
3357
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003358 void showLockTaskToast() {
3359 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003360 }
3361
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003362 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3363 if (mLockTaskModeTasks.contains(task)) {
3364 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3365 }
3366 }
3367
Craig Mautner432f64e2015-05-20 14:59:57 -07003368 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3369 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003370 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003371 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003372 final TaskRecord lockedTask = getLockedTaskLocked();
3373 if (lockedTask != null) {
3374 removeLockedTaskLocked(lockedTask);
3375 if (!mLockTaskModeTasks.isEmpty()) {
3376 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003377 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3378 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003379 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003380 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003381 return;
3382 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003383 }
Craig Mautnere0570202015-05-13 13:06:11 -07003384 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3385 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003386 return;
3387 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003388
3389 // Should have already been checked, but do it again.
3390 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003391 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3392 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003393 return;
3394 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003395 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003396 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003397 return;
3398 }
3399
3400 if (mLockTaskModeTasks.isEmpty()) {
3401 // First locktask.
3402 final Message lockTaskMsg = Message.obtain();
3403 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3404 lockTaskMsg.arg1 = task.userId;
3405 lockTaskMsg.what = LOCK_TASK_START_MSG;
3406 lockTaskMsg.arg2 = lockTaskModeState;
3407 mHandler.sendMessage(lockTaskMsg);
3408 }
3409 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003410 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3411 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003412 mLockTaskModeTasks.remove(task);
3413 mLockTaskModeTasks.add(task);
3414
3415 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003416 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003417 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003418
3419 if (andResume) {
3420 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003421 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003422 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003423 }
3424
3425 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003426 return isLockTaskModeViolation(task, false);
3427 }
3428
3429 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3430 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003431 return false;
3432 }
3433 final int lockTaskAuth = task.mLockTaskAuth;
3434 switch (lockTaskAuth) {
3435 case LOCK_TASK_AUTH_DONT_LOCK:
3436 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003437 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003438 case LOCK_TASK_AUTH_LAUNCHABLE:
3439 case LOCK_TASK_AUTH_WHITELISTED:
3440 return false;
3441 case LOCK_TASK_AUTH_PINNABLE:
3442 // Pinnable tasks can't be launched on top of locktask tasks.
3443 return !mLockTaskModeTasks.isEmpty();
3444 default:
3445 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3446 return true;
3447 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003448 }
3449
Craig Mautner15df08a2015-04-01 12:17:18 -07003450 void onLockTaskPackagesUpdatedLocked() {
3451 boolean didSomething = false;
3452 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3453 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003454 final boolean wasWhitelisted =
3455 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3456 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003457 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003458 final boolean isWhitelisted =
3459 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3460 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3461 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003462 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003463 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3464 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003465 removeLockedTaskLocked(lockedTask);
3466 lockedTask.performClearTaskLocked();
3467 didSomething = true;
3468 }
3469 }
3470 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3471 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3472 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3473 final ActivityStack stack = stacks.get(stackNdx);
3474 stack.onLockTaskPackagesUpdatedLocked();
3475 }
3476 }
Craig Mautnere0570202015-05-13 13:06:11 -07003477 final ActivityRecord r = topRunningActivityLocked();
3478 final TaskRecord task = r != null ? r.task : null;
3479 if (mLockTaskModeTasks.isEmpty() && task != null
3480 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3481 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003482 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3483 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3484 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3485 false);
3486 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003487 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003488 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003489 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003490 }
3491 }
3492
Benjamin Franz43261142015-02-11 15:59:44 +00003493 int getLockTaskModeState() {
3494 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003495 }
3496
Jorim Jaggife89d122015-12-22 16:28:44 +01003497 void activityRelaunchedLocked(IBinder token) {
3498 mWindowManager.notifyAppRelaunchingFinished(token);
3499 }
3500
3501 void activityRelaunchingLocked(ActivityRecord r) {
3502 mWindowManager.notifyAppRelaunching(r.appToken);
3503 }
3504
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003505 void logStackState() {
3506 mActivityMetricsLogger.logWindowState();
3507 }
3508
Wale Ogunwale22e25262016-02-01 10:32:02 -08003509 void scheduleReportMultiWindowChanged(TaskRecord task) {
3510 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3511 final ActivityRecord r = task.mActivities.get(i);
3512 if (r.app != null && r.app.thread != null) {
3513 mMultiWindowModeChangedActivities.add(r);
3514 }
3515 }
3516
3517 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3518 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3519 }
3520 }
3521
3522 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3523 final ActivityStack stack = task.stack;
3524 if (prevStack == null || prevStack == stack
3525 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3526 return;
3527 }
3528
3529 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3530 final ActivityRecord r = task.mActivities.get(i);
3531 if (r.app != null && r.app.thread != null) {
3532 mPipModeChangedActivities.add(r);
3533 }
3534 }
3535
3536 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3537 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3538 }
3539 }
3540
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003541 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003542
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003543 public ActivityStackSupervisorHandler(Looper looper) {
3544 super(looper);
3545 }
3546
Craig Mautnerf3333272013-04-22 10:55:53 -07003547 void activityIdleInternal(ActivityRecord r) {
3548 synchronized (mService) {
3549 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3550 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003551 }
3552
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003553 @Override
3554 public void handleMessage(Message msg) {
3555 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003556 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3557 synchronized (mService) {
3558 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3559 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3560 r.scheduleMultiWindowChanged();
3561 }
3562 }
3563 } break;
3564 case REPORT_PIP_MODE_CHANGED_MSG: {
3565 synchronized (mService) {
3566 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3567 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3568 r.schedulePictureInPictureChanged();
3569 }
3570 }
3571 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003572 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003573 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3574 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003575 if (mService.mDidDexOpt) {
3576 mService.mDidDexOpt = false;
3577 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3578 nmsg.obj = msg.obj;
3579 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3580 return;
3581 }
3582 // We don't at this point know if the activity is fullscreen,
3583 // so we need to be conservative and assume it isn't.
3584 activityIdleInternal((ActivityRecord)msg.obj);
3585 } break;
3586 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003587 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003588 activityIdleInternal((ActivityRecord)msg.obj);
3589 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003590 case RESUME_TOP_ACTIVITY_MSG: {
3591 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003592 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003593 }
3594 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003595 case SLEEP_TIMEOUT_MSG: {
3596 synchronized (mService) {
3597 if (mService.isSleepingOrShuttingDown()) {
3598 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3599 mSleepTimeout = true;
3600 checkReadyForSleepLocked();
3601 }
3602 }
3603 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003604 case LAUNCH_TIMEOUT_MSG: {
3605 if (mService.mDidDexOpt) {
3606 mService.mDidDexOpt = false;
3607 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3608 return;
3609 }
3610 synchronized (mService) {
3611 if (mLaunchingActivity.isHeld()) {
3612 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3613 if (VALIDATE_WAKE_LOCK_CALLER
3614 && Binder.getCallingUid() != Process.myUid()) {
3615 throw new IllegalStateException("Calling must be system uid");
3616 }
3617 mLaunchingActivity.release();
3618 }
3619 }
3620 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003621 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003622 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003623 } break;
3624 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003625 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003626 } break;
3627 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003628 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003629 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003630 case CONTAINER_CALLBACK_VISIBILITY: {
3631 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003632 final IActivityContainerCallback callback = container.mCallback;
3633 if (callback != null) {
3634 try {
3635 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3636 } catch (RemoteException e) {
3637 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003638 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003639 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003640 case LOCK_TASK_START_MSG: {
3641 // When lock task starts, we disable the status bars.
3642 try {
Jason Monk62515be2014-05-21 16:06:19 -04003643 if (mLockTaskNotify == null) {
3644 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003645 }
Jason Monk62515be2014-05-21 16:06:19 -04003646 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003647 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003648 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003649 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003650 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003651 flags = StatusBarManager.DISABLE_MASK
3652 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003653 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003654 flags = StatusBarManager.DISABLE_MASK
3655 & (~StatusBarManager.DISABLE_BACK)
3656 & (~StatusBarManager.DISABLE_HOME)
3657 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003658 }
3659 getStatusBarService().disable(flags, mToken,
3660 mService.mContext.getPackageName());
3661 }
3662 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003663 if (getDevicePolicyManager() != null) {
3664 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003665 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003666 }
justinzhang5286d3f2014-05-12 17:06:01 -04003667 } catch (RemoteException ex) {
3668 throw new RuntimeException(ex);
3669 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003670 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003671 case LOCK_TASK_END_MSG: {
3672 // When lock task ends, we enable the status bars.
3673 try {
Jason Monk62515be2014-05-21 16:06:19 -04003674 if (getStatusBarService() != null) {
3675 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3676 mService.mContext.getPackageName());
3677 }
3678 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003679 if (getDevicePolicyManager() != null) {
3680 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3681 msg.arg1);
3682 }
Jason Monk62515be2014-05-21 16:06:19 -04003683 if (mLockTaskNotify == null) {
3684 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3685 }
3686 mLockTaskNotify.show(false);
3687 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003688 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003689 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003690 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003691 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003692 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003693 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003694 new LockPatternUtils(mService.mContext)
3695 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003696 }
3697 } catch (SettingNotFoundException e) {
3698 // No setting, don't lock.
3699 }
justinzhang5286d3f2014-05-12 17:06:01 -04003700 } catch (RemoteException ex) {
3701 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003702 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003703 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003704 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003705 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003706 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3707 if (mLockTaskNotify == null) {
3708 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3709 }
3710 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3711 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003712 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3713 final ActivityContainer container = (ActivityContainer) msg.obj;
3714 final IActivityContainerCallback callback = container.mCallback;
3715 if (callback != null) {
3716 try {
3717 callback.onAllActivitiesComplete(container.asBinder());
3718 } catch (RemoteException e) {
3719 }
3720 }
3721 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003722 case LAUNCH_TASK_BEHIND_COMPLETE: {
3723 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003724 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003725 if (r != null) {
3726 handleLaunchTaskBehindCompleteLocked(r);
3727 }
3728 }
3729 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003730
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003731 }
3732 }
3733 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003734
Ying Wangb081a592014-04-22 15:20:16 -07003735 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003736 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3737 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003739 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003741 ActivityRecord mParentActivity = null;
3742 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003743
Craig Mautnere3a00d72014-04-16 08:31:19 -07003744 boolean mVisible = true;
3745
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003747 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003749 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3750 final static int CONTAINER_STATE_NO_SURFACE = 1;
3751 final static int CONTAINER_STATE_FINISHING = 2;
3752 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3753
3754 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 synchronized (mService) {
3756 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003757 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003758 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003759 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003760 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003761 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003763 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003764 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003765 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003766 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003767 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003768 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 }
3770
3771 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003772 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003774 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3775 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 return;
3777 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003778 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779 }
3780 }
3781
3782 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003783 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003784 synchronized (mService) {
3785 if (mActivityDisplay != null) {
3786 return mActivityDisplay.mDisplayId;
3787 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003788 }
3789 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790 }
3791
Jeff Brownca9bc702014-02-11 14:32:56 -08003792 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003793 public int getStackId() {
3794 synchronized (mService) {
3795 return mStackId;
3796 }
3797 }
3798
3799 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003800 public boolean injectEvent(InputEvent event) {
3801 final long origId = Binder.clearCallingIdentity();
3802 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003803 synchronized (mService) {
3804 if (mActivityDisplay != null) {
3805 return mInputManagerInternal.injectInputEvent(event,
3806 mActivityDisplay.mDisplayId,
3807 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3808 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003809 }
3810 return false;
3811 } finally {
3812 Binder.restoreCallingIdentity(origId);
3813 }
3814 }
3815
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003816 @Override
3817 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003818 synchronized (mService) {
3819 if (mContainerState == CONTAINER_STATE_FINISHING) {
3820 return;
3821 }
3822 mContainerState = CONTAINER_STATE_FINISHING;
3823
Craig Mautnerd163e752014-06-13 17:18:47 -07003824 long origId = Binder.clearCallingIdentity();
3825 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003826 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003827 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003828 } finally {
3829 Binder.restoreCallingIdentity(origId);
3830 }
3831 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003832 }
3833
Craig Mautner60257702014-09-17 15:02:33 -07003834 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003835 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003836 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003837 if (mActivityDisplay != null) {
3838 mActivityDisplay.detachActivitiesLocked(mStack);
3839 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003840 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 }
3842 }
3843
3844 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003845 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003846 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 }
3848
3849 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003850 public final int startActivityIntentSender(IIntentSender intentSender)
3851 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003852 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3853
3854 if (!(intentSender instanceof PendingIntentRecord)) {
3855 throw new IllegalArgumentException("Bad PendingIntent object");
3856 }
3857
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003858 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003859 Binder.getCallingUid(), mCurrentUser, false,
3860 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003861
3862 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3863 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3864 pendingIntent.key.requestResolvedType);
3865
3866 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3867 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3868 }
3869
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003870 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003871 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003872 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003873 throw new SecurityException(
3874 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3875 }
3876 }
3877
Craig Mautnerdf88d732014-01-27 09:21:32 -08003878 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003879 public IBinder asBinder() {
3880 return this;
3881 }
3882
Craig Mautner4504de52013-12-20 09:06:56 -08003883 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003884 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003885 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003886 }
3887
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 ActivityStackSupervisor getOuter() {
3889 return ActivityStackSupervisor.this;
3890 }
3891
Craig Mautnerd163e752014-06-13 17:18:47 -07003892 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003893 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894 }
3895
Craig Mautner6985bad2014-04-21 15:22:06 -07003896 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003897 void setVisible(boolean visible) {
3898 if (mVisible != visible) {
3899 mVisible = visible;
3900 if (mCallback != null) {
3901 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3902 0 /* unused */, this).sendToTarget();
3903 }
3904 }
3905 }
3906
Craig Mautner6985bad2014-04-21 15:22:06 -07003907 void setDrawn() {
3908 }
3909
Craig Mautner1b4bf852014-05-26 15:06:32 -07003910 // You can always start a new task on a regular ActivityStack.
3911 boolean isEligibleForNewTasks() {
3912 return true;
3913 }
3914
Craig Mautnerd163e752014-06-13 17:18:47 -07003915 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003916 detachLocked();
3917 deleteActivityContainer(this);
3918 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003919 }
3920
Craig Mautner34b73df2014-01-12 21:11:08 -08003921 @Override
3922 public String toString() {
3923 return mIdString + (mActivityDisplay == null ? "N" : "A");
3924 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003925 }
3926
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003927 private class VirtualActivityContainer extends ActivityContainer {
3928 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003929 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003930
3931 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3932 super(getNextStackId());
3933 mParentActivity = parent;
3934 mCallback = callback;
3935 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003936 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003937 }
3938
3939 @Override
3940 public void setSurface(Surface surface, int width, int height, int density) {
3941 super.setSurface(surface, width, height, density);
3942
3943 synchronized (mService) {
3944 final long origId = Binder.clearCallingIdentity();
3945 try {
3946 setSurfaceLocked(surface, width, height, density);
3947 } finally {
3948 Binder.restoreCallingIdentity(origId);
3949 }
3950 }
3951 }
3952
3953 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3954 if (mContainerState == CONTAINER_STATE_FINISHING) {
3955 return;
3956 }
3957 VirtualActivityDisplay virtualActivityDisplay =
3958 (VirtualActivityDisplay) mActivityDisplay;
3959 if (virtualActivityDisplay == null) {
3960 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003961 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003962 mActivityDisplay = virtualActivityDisplay;
3963 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003964 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003965 }
3966
3967 if (mSurface != null) {
3968 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003969 }
3970
Craig Mautner6985bad2014-04-21 15:22:06 -07003971 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003972 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003973 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003974 } else {
3975 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003976 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003977 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003978 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003979 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003980 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003981
Craig Mautnerd163e752014-06-13 17:18:47 -07003982 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003983
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003984 if (DEBUG_STACK) Slog.d(TAG_STACK,
3985 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003986 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003987
Craig Mautner6985bad2014-04-21 15:22:06 -07003988 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003989 boolean isAttachedLocked() {
3990 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003991 }
3992
3993 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003994 void setDrawn() {
3995 synchronized (mService) {
3996 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003997 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003998 }
3999 }
4000
Craig Mautner1b4bf852014-05-26 15:06:32 -07004001 // Never start a new task on an ActivityView if it isn't explicitly specified.
4002 @Override
4003 boolean isEligibleForNewTasks() {
4004 return false;
4005 }
4006
Craig Mautnerd163e752014-06-13 17:18:47 -07004007 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004008 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004009 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4010 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4011 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4012 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4013 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004014 }
4015 }
4016
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4018 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004019 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004020 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004021 int mDisplayId;
4022 Display mDisplay;
4023 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004024
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4026 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004027 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004028
Jose Lima4b6c6692014-08-12 17:41:12 -07004029 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004030
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004031 ActivityDisplay() {
4032 }
Craig Mautner4504de52013-12-20 09:06:56 -08004033
Craig Mautner1a70a162014-09-13 12:09:31 -07004034 // After instantiation, check that mDisplay is not null before using this. The alternative
4035 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004036 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004037 final Display display = mDisplayManager.getDisplay(displayId);
4038 if (display == null) {
4039 return;
4040 }
4041 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004042 }
4043
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004044 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004045 mDisplay = display;
4046 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004048 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004049
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004050 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004051 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004052 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4053 + " onTop=" + onTop);
4054 if (onTop) {
4055 mStacks.add(stack);
4056 } else {
4057 mStacks.add(0, stack);
4058 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
4060
Craig Mautnere0a38842013-12-16 16:14:02 -08004061 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004062 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004063 + " from displayId=" + mDisplayId);
4064 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 }
4066
Jose Lima4b6c6692014-08-12 17:41:12 -07004067 void setVisibleBehindActivity(ActivityRecord r) {
4068 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004069 }
4070
Jose Lima4b6c6692014-08-12 17:41:12 -07004071 boolean hasVisibleBehindActivity() {
4072 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004073 }
4074
Craig Mautner34b73df2014-01-12 21:11:08 -08004075 @Override
4076 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004077 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4078 }
4079 }
4080
4081 class VirtualActivityDisplay extends ActivityDisplay {
4082 VirtualDisplay mVirtualDisplay;
4083
Craig Mautner6985bad2014-04-21 15:22:06 -07004084 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004086 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4087 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4088 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4089 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004090
4091 init(mVirtualDisplay.getDisplay());
4092
4093 mWindowManager.handleDisplayAdded(mDisplayId);
4094 }
4095
4096 void setSurface(Surface surface) {
4097 if (mVirtualDisplay != null) {
4098 mVirtualDisplay.setSurface(surface);
4099 }
4100 }
4101
4102 @Override
4103 void detachActivitiesLocked(ActivityStack stack) {
4104 super.detachActivitiesLocked(stack);
4105 if (mVirtualDisplay != null) {
4106 mVirtualDisplay.release();
4107 mVirtualDisplay = null;
4108 }
4109 }
4110
4111 @Override
4112 public String toString() {
4113 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004114 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004115 }
Jose Lima58e66d62014-05-27 20:00:27 -07004116
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004117 /**
4118 * Adjust bounds to stay within stack bounds.
4119 *
4120 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4121 * that keep them unchanged, but be contained within the stack bounds.
4122 *
4123 * @param bounds Bounds to be adjusted.
4124 * @param stackBounds Bounds within which the other bounds should remain.
4125 */
4126 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4127 if (stackBounds == null || stackBounds.contains(bounds)) {
4128 return;
4129 }
4130
4131 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4132 final int maxRight = stackBounds.right
4133 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4134 int horizontalDiff = stackBounds.left - bounds.left;
4135 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4136 || (bounds.left + horizontalDiff >= maxRight)) {
4137 horizontalDiff = maxRight - bounds.left;
4138 }
4139 bounds.left += horizontalDiff;
4140 bounds.right += horizontalDiff;
4141 }
4142
4143 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4144 final int maxBottom = stackBounds.bottom
4145 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4146 int verticalDiff = stackBounds.top - bounds.top;
4147 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4148 || (bounds.top + verticalDiff >= maxBottom)) {
4149 verticalDiff = maxBottom - bounds.top;
4150 }
4151 bounds.top += verticalDiff;
4152 bounds.bottom += verticalDiff;
4153 }
4154 }
4155
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004156 ActivityStack findStackBehind(ActivityStack stack) {
4157 // TODO(multi-display): We are only looking for stacks on the default display.
4158 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4159 if (display == null) {
4160 return null;
4161 }
4162 final ArrayList<ActivityStack> stacks = display.mStacks;
4163 for (int i = stacks.size() - 1; i >= 0; i--) {
4164 if (stacks.get(i) == stack && i > 0) {
4165 return stacks.get(i - 1);
4166 }
4167 }
4168 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4169 + " in=" + stacks);
4170 }
Craig Mautner27084302013-03-25 08:05:25 -07004171}