blob: fc003b8d40d54ad89686d76f5d7b8d2f805238d4 [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;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800125import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800154import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100155import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100156import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100157import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
158import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
161import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
162import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
163import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
164import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
165import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
166import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
167import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
172import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
173
Craig Mautner4a1cb222013-12-04 16:14:06 -0800174public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800175 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700178 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700182 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800185 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800187
Craig Mautnerf3333272013-04-22 10:55:53 -0700188 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700190
Craig Mautner0eea92c2013-05-16 13:35:39 -0700191 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700194 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700196
Craig Mautner05d29032013-05-03 13:40:13 -0700197 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
198 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
199 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800202 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
203 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
204 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700205 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400206 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
207 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700208 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700209 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700210 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800211 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
212 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800213
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215
Jason Monk62515be2014-05-21 16:06:19 -0400216 private static final String LOCK_TASK_TAG = "Lock-to-App";
217
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218 // Used to indicate if an object (e.g. stack) that we are trying to get
219 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800220 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700222 // Used to indicate that windows of activities should be preserved during the resize.
223 static final boolean PRESERVE_WINDOWS = true;
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. task) should be moved/created
226 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700227 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
229 // Used to indicate that an objects (e.g. task) removal from its container
230 // (e.g. stack) is due to it moving to another container.
231 static final boolean MOVING = true;
232
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700233 // Force the focus to change to the stack we are moving a task to..
234 static final boolean FORCE_FOCUS = true;
235
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700236 // Restore task from the saved recents if it can't be found in any live stack.
237 static final boolean RESTORE_FROM_RECENTS = true;
238
Svetoslav7008b512015-06-24 18:47:07 -0700239 // Activity actions an app cannot start if it uses a permission which is not granted.
240 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
241 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700242
Svetoslav7008b512015-06-24 18:47:07 -0700243 static {
244 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
245 Manifest.permission.CAMERA);
246 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
247 Manifest.permission.CAMERA);
248 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
249 Manifest.permission.CALL_PHONE);
250 }
251
Svet Ganov99b60432015-06-27 13:15:22 -0700252 /** Action restriction: launching the activity is not restricted. */
253 private static final int ACTIVITY_RESTRICTION_NONE = 0;
254 /** Action restriction: launching the activity is restricted by a permission. */
255 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
256 /** Action restriction: launching the activity is restricted by an app op. */
257 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700258
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700259 // The height/width divide used when fitting a task within a bounds with method
260 // {@link #fitWithinBounds}.
261 // We always want the task to to be visible in the bounds without affecting its size when
262 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
263 // the input bounds right or bottom side minus the width or height divided by this value.
264 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
265
justinzhang5286d3f2014-05-12 17:06:01 -0400266 /** Status Bar Service **/
267 private IBinder mToken = new Binder();
268 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400269 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400270
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700271 // For debugging to make sure the caller when acquiring/releasing our
272 // wake lock is the system process.
273 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800274 /** The number of distinct task ids that can be assigned to the tasks of a single user */
275 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700276
Craig Mautner27084302013-03-25 08:05:25 -0700277 final ActivityManagerService mService;
278
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800279 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800280
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700281 final ActivityStackSupervisorHandler mHandler;
282
283 /** Short cut */
284 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800285 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700286
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700287 /** Counter for next free stack ID to use for dynamic activity stacks. */
288 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700289
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800290 /**
291 * Maps the task identifier that activities are currently being started in to the userId of the
292 * task. Each time a new task is created, the entry for the userId of the task is incremented
293 */
294 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700295
Craig Mautner2420ead2013-04-01 17:13:20 -0700296 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800297 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700298
Craig Mautnere0a38842013-12-16 16:14:02 -0800299 /** The stack containing the launcher app. Assumed to always be attached to
300 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800301 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700302
Craig Mautnere0a38842013-12-16 16:14:02 -0800303 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800304 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700305
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
307 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800309 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700310
311 /** List of activities that are waiting for a new activity to become visible before completing
312 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800313 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317
318 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800319 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321 /** List of activities that are ready to be stopped, but waiting for the next activity to
322 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
Craig Mautnerf3333272013-04-22 10:55:53 -0700325 /** List of activities that are ready to be finished, but waiting for the previous activity to
326 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700328
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800330 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700331
Wale Ogunwale22e25262016-02-01 10:32:02 -0800332 /** List of activities whose multi-window mode changed that we need to report to the
333 * application */
334 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
335
336 /** List of activities whose picture-in-picture mode changed that we need to report to the
337 * application */
338 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
339
Craig Mautnerf3333272013-04-22 10:55:53 -0700340 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700341 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700342
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700343 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700344 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700345
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
347 * is being brought in front of us. */
348 boolean mUserLeaving = false;
349
Craig Mautner0eea92c2013-05-16 13:35:39 -0700350 /** Set when we have taken too long waiting to go to sleep. */
351 boolean mSleepTimeout = false;
352
353 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700354 * We don't want to allow the device to go to sleep while in the process
355 * of launching an activity. This is primarily to allow alarm intent
356 * receivers to launch an activity and get that to run before the device
357 * goes back to sleep.
358 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700359 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360
361 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700362 * Set when the system is going to sleep, until we have
363 * successfully paused the current activity and released our wake lock.
364 * At that point the system is allowed to actually sleep.
365 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700366 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700368 /** Stack id of the front stack when user switched, indexed by userId. */
369 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700370
Craig Mautner4504de52013-12-20 09:06:56 -0800371 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800372 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700373 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374
375 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700376 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377
Jeff Brownca9bc702014-02-11 14:32:56 -0800378 InputManagerInternal mInputManagerInternal;
379
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
381 * may be finished until there is only one entry left. If this is empty the system is not
382 * in lockTask mode. */
383 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000384 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700385 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
386 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000387 */
388 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400389 /**
390 * Notifies the user when entering/exiting lock-task.
391 */
392 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800393
Wale Ogunwaled046a012015-12-24 13:05:59 -0800394 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800395 boolean inResumeTopActivity;
396
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700397 // 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 -0700398 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700399 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700400
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700401 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
402 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800403 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700404
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700405 // The default minimal size that will be used if the activity doesn't specify its minimal size.
406 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800407 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700408
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700409 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
410 private boolean mTaskLayersChanged = true;
411
Jorim Jaggi275561a2016-02-23 10:11:02 -0500412 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800413
Jorim Jaggidc249c42015-12-15 14:57:31 -0800414 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
415
Wale Ogunwale39381972015-12-17 17:15:29 -0800416 static class FindTaskResult {
417 ActivityRecord r;
418 boolean matchedByRootAffinity;
419 }
420 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
421
Craig Mautneree36c772014-07-16 14:56:05 -0700422 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100423 * Used to keep track whether app visibilities got changed since the last pause. Useful to
424 * determine whether to invoke the task stack change listener after pausing.
425 */
426 boolean mAppVisibilitiesChangedSinceLastPause;
427
428 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700429 * Description of a request to start a new activity, which has been held
430 * due to app switches being disabled.
431 */
432 static class PendingActivityLaunch {
433 final ActivityRecord r;
434 final ActivityRecord sourceRecord;
435 final int startFlags;
436 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700437 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700438
439 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700440 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700441 r = _r;
442 sourceRecord = _sourceRecord;
443 startFlags = _startFlags;
444 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700445 callerApp = _callerApp;
446 }
447
448 void sendErrorResult(String message) {
449 try {
450 if (callerApp.thread != null) {
451 callerApp.thread.scheduleCrash(message);
452 }
453 } catch (RemoteException e) {
454 Slog.e(TAG, "Exception scheduling crash of failed "
455 + "activity launcher sourceRecord=" + sourceRecord, e);
456 }
Craig Mautneree36c772014-07-16 14:56:05 -0700457 }
458 }
459
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800460 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700461 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700462 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800463 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800464 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700465 }
466
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800467 void setRecentTasks(RecentTasks recentTasks) {
468 mRecentTasks = recentTasks;
469 }
470
Jeff Brown2c43c332014-06-12 22:38:59 -0700471 /**
472 * At the time when the constructor runs, the power manager has not yet been
473 * initialized. So we initialize our wakelocks afterwards.
474 */
475 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700477 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700478 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700479 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700480 }
481
justinzhang5286d3f2014-05-12 17:06:01 -0400482 // This function returns a IStatusBarService. The value is from ServiceManager.
483 // getService and is cached.
484 private IStatusBarService getStatusBarService() {
485 synchronized (mService) {
486 if (mStatusBarService == null) {
487 mStatusBarService = IStatusBarService.Stub.asInterface(
488 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
489 if (mStatusBarService == null) {
490 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
491 }
492 }
493 return mStatusBarService;
494 }
495 }
496
Jason Monk35c62a42014-06-17 10:24:47 -0400497 private IDevicePolicyManager getDevicePolicyManager() {
498 synchronized (mService) {
499 if (mDevicePolicyManager == null) {
500 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
501 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
502 if (mDevicePolicyManager == null) {
503 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
504 }
505 }
506 return mDevicePolicyManager;
507 }
508 }
509
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700510 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 synchronized (mService) {
512 mWindowManager = wm;
513
514 mDisplayManager =
515 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
516 mDisplayManager.registerDisplayListener(this, null);
517
518 Display[] displays = mDisplayManager.getDisplays();
519 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
520 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700522 if (activityDisplay.mDisplay == null) {
523 throw new IllegalStateException("Default Display does not exist");
524 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800525 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700526 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 }
528
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800529 mHomeStack = mFocusedStack = mLastFocusedStack =
530 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800531
532 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800533 }
Craig Mautner27084302013-03-25 08:05:25 -0700534 }
535
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200536 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700537 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200538 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700539 }
540
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700541 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700543 }
544
Craig Mautnerde4ef022013-04-07 19:01:33 -0700545 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800546 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700547 }
548
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700549 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700550 if (stack == null) {
551 return false;
552 }
553
Craig Mautnerdf88d732014-01-27 09:21:32 -0800554 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
555 if (parent != null) {
556 stack = parent.task.stack;
557 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800558 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700559 }
560
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700561 /** The top most stack. */
562 boolean isFrontStack(ActivityStack stack) {
563 if (stack == null) {
564 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700566
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
568 if (parent != null) {
569 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800570 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700571 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
572 }
573
Wale Ogunwaled046a012015-12-24 13:05:59 -0800574 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800575 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
576 if (!focusCandidate.isFocusable()) {
577 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
578 focusCandidate = focusCandidate.getNextFocusableStackLocked();
579 }
580
581 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800582 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800583 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800584
Wale Ogunwaled046a012015-12-24 13:05:59 -0800585 EventLogTags.writeAmFocusedStack(
586 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
587 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
588 }
589
590 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800591 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800592 // The focus activity should always be the top activity in the focused stack.
593 // There will be chaos and anarchy if it isn't...
594 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
595 }
Craig Mautnerde313752015-01-22 14:28:03 -0800596
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800597 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800598 if (r != null && r.idle) {
599 checkFinishBootingLocked();
600 }
601 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700602 }
603
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700604 void moveHomeStackToFront(String reason) {
605 mHomeStack.moveToFront(reason);
606 }
607
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700608 /** Returns true if the focus activity was adjusted to the home stack top activity. */
609 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700610 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
611 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700612 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700613 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700614
Craig Mautner84984fa2014-06-19 11:19:20 -0700615 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700616
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700617 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700618 if (top == null) {
619 return false;
620 }
621 mService.setFocusedActivityLocked(top, reason);
622 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700623 }
624
Craig Mautner299f9602015-01-26 09:47:33 -0800625 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700626 if (!mService.mBooting && !mService.mBooted) {
627 // Not ready yet!
628 return false;
629 }
630
Craig Mautner84984fa2014-06-19 11:19:20 -0700631 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
632 mWindowManager.showRecentApps();
633 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700634 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700635
Craig Mautner84984fa2014-06-19 11:19:20 -0700636 if (prev != null) {
637 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
638 }
639
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700640 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
641 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800642 final String myReason = reason + " resumeHomeStackTask";
643
Mark Lua56ea122015-10-08 13:31:01 +0800644 // Only resume home activity if isn't finishing.
645 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 mService.setFocusedActivityLocked(r, myReason);
647 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700648 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800649 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700650 }
651
Craig Mautner8d341ef2013-03-26 09:03:27 -0700652 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700653 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700654 }
655
656 /**
657 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
658 * @param id Id of the task we would like returned.
659 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
660 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700661 * @param stackId The stack to restore the task to (default launch stack will be used if
662 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700663 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700664 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800665 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800667 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
669 ActivityStack stack = stacks.get(stackNdx);
670 TaskRecord task = stack.taskForIdLocked(id);
671 if (task != null) {
672 return task;
673 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700674 }
675 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800676
677 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700678 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800679 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800680 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700681 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800682 return null;
683 }
684
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700685 if (!restoreFromRecents) {
686 return task;
687 }
688
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700689 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700690 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
691 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800692 return null;
693 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800695 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700696 }
697
Craig Mautner6170f732013-04-02 13:05:23 -0700698 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800699 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
703 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
704 if (r != null) {
705 return r;
706 }
Craig Mautner6170f732013-04-02 13:05:23 -0700707 }
708 }
709 return null;
710 }
711
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000712 boolean isFocusedUserLockedProfile() {
713 final int userId = mFocusedStack.topRunningActivityLocked().userId;
714 return userId != UserHandle.myUserId()
715 && mService.mUserController.shouldConfirmCredentials(userId);
716 }
717
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800718 void setNextTaskIdForUserLocked(int taskId, int userId) {
719 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
720 if (taskId > currentTaskId) {
721 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700722 }
723 }
724
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800725 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800726 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
727 // for a userId u, a taskId can only be in the range
728 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
729 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
730 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800731 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
732 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
733 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800734 candidateTaskId++;
735 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
736 // Wrap around as there will be smaller task ids that are available now.
737 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700738 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800739 if (candidateTaskId == currentTaskId) {
740 // Something wrong!
741 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
742 throw new IllegalStateException("Cannot get an available task id."
743 + " Reached limit of " + MAX_TASK_IDS_PER_USER
744 + " running tasks per user.");
745 }
746 }
747 mCurTaskIdForUser.put(userId, candidateTaskId);
748 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700749 }
750
Craig Mautnerde4ef022013-04-07 19:01:33 -0700751 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800752 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700753 if (stack == null) {
754 return null;
755 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 ActivityRecord resumedActivity = stack.mResumedActivity;
757 if (resumedActivity == null || resumedActivity.app == null) {
758 resumedActivity = stack.mPausingActivity;
759 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700760 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700761 }
762 }
763 return resumedActivity;
764 }
765
Dianne Hackbornff072722014-09-24 10:56:28 -0700766 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700767 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
770 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
772 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700773 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 continue;
775 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700776 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 if (hr != null) {
778 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
779 && processName.equals(hr.processName)) {
780 try {
George Mount2c92c972014-03-20 09:38:23 -0700781 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 didSomething = true;
783 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700784 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800785 Slog.w(TAG, "Exception in new application when starting activity "
786 + hr.intent.getComponent().flattenToShortString(), e);
787 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700788 }
Craig Mautner20e72272013-04-01 13:45:53 -0700789 }
Craig Mautner20e72272013-04-01 13:45:53 -0700790 }
791 }
792 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700793 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700794 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700795 }
Craig Mautner20e72272013-04-01 13:45:53 -0700796 return didSomething;
797 }
798
799 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800800 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
801 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
803 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700804 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800805 continue;
806 }
807 final ActivityRecord resumedActivity = stack.mResumedActivity;
808 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700809 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800810 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 return false;
812 }
Craig Mautner20e72272013-04-01 13:45:53 -0700813 }
814 }
815 return true;
816 }
817
Craig Mautnerde4ef022013-04-07 19:01:33 -0700818 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800819 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
820 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
822 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700823 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800824 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700825 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 return false;
827 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700828 }
829 }
830 }
831 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700832 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
834 mLastFocusedStack + " to=" + mFocusedStack);
835 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700836 return true;
837 }
838
839 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800840 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
842 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
844 final ActivityStack stack = stacks.get(stackNdx);
845 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800846 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700847 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800848 return false;
849 }
850 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700852 }
853 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800854 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700855 }
856
Craig Mautner2acc3892013-09-23 10:28:14 -0700857 /**
858 * Pause all activities in either all of the stacks or just the back stacks.
859 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700860 * @return true if any activity was paused as a result of this call.
861 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700862 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700863 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800864 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
865 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
867 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700868 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700869 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700871 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
872 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800873 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700874 }
875 }
876 return someActivityPaused;
877 }
878
Craig Mautnerde4ef022013-04-07 19:01:33 -0700879 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700880 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
882 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
884 final ActivityStack stack = stacks.get(stackNdx);
885 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700886 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700888 Slog.d(TAG_STATES,
889 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 pausing = false;
891 } else {
892 return false;
893 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700894 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 }
896 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700897 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700898 }
899
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700900 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
901 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500902 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800903 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
904 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
905 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
906 final ActivityStack stack = stacks.get(stackNdx);
907 if (stack.mResumedActivity != null &&
908 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910 }
911 }
912 }
913 }
914
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800915 void cancelInitializingActivities() {
916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
917 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
918 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
919 stacks.get(stackNdx).cancelInitializingActivities();
920 }
921 }
922 }
923
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700924 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700925 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700926 }
927
928 void sendWaitingVisibleReportLocked(ActivityRecord r) {
929 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700930 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700931 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700932 if (w.who == null) {
933 changed = true;
934 w.timeout = false;
935 if (r != null) {
936 w.who = new ComponentName(r.info.packageName, r.info.name);
937 }
938 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
939 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700940 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700941 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700942 if (changed) {
943 mService.notifyAll();
944 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700945 }
946
947 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
948 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700949 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700950 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700951 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700952 if (w.who == null) {
953 changed = true;
954 w.timeout = timeout;
955 if (r != null) {
956 w.who = new ComponentName(r.info.packageName, r.info.name);
957 }
958 w.thisTime = thisTime;
959 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700960 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700961 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700962 if (changed) {
963 mService.notifyAll();
964 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700965 }
966
Craig Mautner29219d92013-04-16 20:19:12 -0700967 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800968 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700969 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700970 if (r != null) {
971 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700972 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700973
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800975 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
977 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800978 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700979 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700980 if (r != null) {
981 return r;
982 }
983 }
984 }
985 return null;
986 }
987
Dianne Hackborn09233282014-04-30 11:33:59 -0700988 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700989 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
991 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800992 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800994 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
996 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700997 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800998 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700999 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001000 }
1001 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001002
1003 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1004 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1005 while (maxNum > 0) {
1006 long mostRecentActiveTime = Long.MIN_VALUE;
1007 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001008 final int numTaskLists = runningTaskLists.size();
1009 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1010 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001011 if (!stackTaskList.isEmpty()) {
1012 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1013 if (lastActiveTime > mostRecentActiveTime) {
1014 mostRecentActiveTime = lastActiveTime;
1015 selectedStackList = stackTaskList;
1016 }
1017 }
1018 }
1019 if (selectedStackList != null) {
1020 list.add(selectedStackList.remove(0));
1021 --maxNum;
1022 } else {
1023 break;
1024 }
1025 }
Craig Mautner20e72272013-04-01 13:45:53 -07001026 }
1027
Todd Kennedy7440f172015-12-09 14:31:22 -08001028 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1029 ProfilerInfo profilerInfo) {
1030 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 if (aInfo != null) {
1032 // Store the found target back into the intent, because now that
1033 // we have it we never want to do this again. For example, if the
1034 // user navigates back to this point in the history, we should
1035 // always restart the exact same activity.
1036 intent.setComponent(new ComponentName(
1037 aInfo.applicationInfo.packageName, aInfo.name));
1038
1039 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001040 if (!aInfo.processName.equals("system")) {
1041 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 mService.setDebugApp(aInfo.processName, true, false);
1043 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001045 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1046 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1047 }
1048
Man Caocfa78b22015-06-11 20:14:34 -07001049 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1050 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1051 }
1052
1053 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001054 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 }
1056 }
1057 }
1058 return aInfo;
1059 }
1060
Todd Kennedy7440f172015-12-09 14:31:22 -08001061 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001062 return resolveIntent(intent, resolvedType, userId, 0);
1063 }
1064
1065 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001066 try {
1067 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001068 PackageManager.MATCH_DEFAULT_ONLY | flags
1069 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001070 } catch (RemoteException e) {
1071 }
1072 return null;
1073 }
1074
1075 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1076 ProfilerInfo profilerInfo, int userId) {
1077 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1078 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1079 }
1080
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001081 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1082 boolean andResume, boolean checkConfig) throws RemoteException {
1083
1084 if (!allPausedActivitiesComplete()) {
1085 // While there are activities pausing we skipping starting any new activities until
1086 // pauses are complete. NOTE: that we also do this for activities that are starting in
1087 // the paused state because they will first be resumed then paused on the client side.
1088 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1089 "realStartActivityLocked: Skipping start of r=" + r
1090 + " some activities pausing...");
1091 return false;
1092 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001093
Craig Mautner2568c3a2015-03-26 14:22:34 -07001094 if (andResume) {
1095 r.startFreezingScreenLocked(app, 0);
1096 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001097
Craig Mautner2568c3a2015-03-26 14:22:34 -07001098 // schedule launch ticks to collect information about slow apps.
1099 r.startLaunchTickingLocked();
1100 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001101
1102 // Have the window manager re-evaluate the orientation of
1103 // the screen based on the new activity order. Note that
1104 // as a result of this, it can call back into the activity
1105 // manager with a new orientation. We don't care about that,
1106 // because the activity is not currently running so we are
1107 // just restarting it anyway.
1108 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001109 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001110 mService.mConfiguration,
1111 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001112 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001113 }
1114
1115 r.app = app;
1116 app.waitingToKill = null;
1117 r.launchCount++;
1118 r.lastLaunchTime = SystemClock.uptimeMillis();
1119
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001120 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001121
1122 int idx = app.activities.indexOf(r);
1123 if (idx < 0) {
1124 app.activities.add(r);
1125 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001126 mService.updateLruProcessLocked(app, true, null);
1127 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001128
Craig Mautner15df08a2015-04-01 12:17:18 -07001129 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001130 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1131 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001132 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001133 }
1134
1135 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001136 try {
1137 if (app.thread == null) {
1138 throw new RemoteException();
1139 }
1140 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001141 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 if (andResume) {
1143 results = r.results;
1144 newIntents = r.newIntents;
1145 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001146 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1147 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1148 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 if (andResume) {
1150 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1151 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001152 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001153 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001154 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001155 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001156 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001157 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001158 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 r.sleeping = false;
1160 r.forceNewConfig = false;
1161 mService.showAskCompatModeDialogLocked(r);
1162 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001163 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1165 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1166 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001167 final String profileFile = mService.mProfileFile;
1168 if (profileFile != null) {
1169 ParcelFileDescriptor profileFd = mService.mProfileFd;
1170 if (profileFd != null) {
1171 try {
1172 profileFd = profileFd.dup();
1173 } catch (IOException e) {
1174 if (profileFd != null) {
1175 try {
1176 profileFd.close();
1177 } catch (IOException o) {
1178 }
1179 profileFd = null;
1180 }
1181 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001183
1184 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1185 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 }
1187 }
1188 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001189
Craig Mautner2568c3a2015-03-26 14:22:34 -07001190 if (andResume) {
1191 app.hasShownUi = true;
1192 app.pendingUiClean = true;
1193 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001194 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001195 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001196 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001197 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001198 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001199 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001201 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 // This may be a heavy-weight process! Note that the package
1203 // manager will ensure that only activity can run in the main
1204 // process of the .apk, which is the only thing that will be
1205 // considered heavy-weight.
1206 if (app.processName.equals(app.info.packageName)) {
1207 if (mService.mHeavyWeightProcess != null
1208 && mService.mHeavyWeightProcess != app) {
1209 Slog.w(TAG, "Starting new heavy weight process " + app
1210 + " when already running "
1211 + mService.mHeavyWeightProcess);
1212 }
1213 mService.mHeavyWeightProcess = app;
1214 Message msg = mService.mHandler.obtainMessage(
1215 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1216 msg.obj = r;
1217 mService.mHandler.sendMessage(msg);
1218 }
1219 }
1220
1221 } catch (RemoteException e) {
1222 if (r.launchFailed) {
1223 // This is the second time we failed -- finish activity
1224 // and give up.
1225 Slog.e(TAG, "Second failure launching "
1226 + r.intent.getComponent().flattenToShortString()
1227 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001228 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1230 "2nd-crash", false);
1231 return false;
1232 }
1233
1234 // This is the first time we failed -- restart process and
1235 // retry.
1236 app.activities.remove(r);
1237 throw e;
1238 }
1239
1240 r.launchFailed = false;
1241 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001242 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 }
1244
1245 if (andResume) {
1246 // As part of the process of launching, ActivityThread also performs
1247 // a resume.
1248 stack.minimalResumeActivityLocked(r);
1249 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001250 // This activity is not starting in the resumed state... which should look like we asked
1251 // it to pause+stop (but remain visible), and it has done so and reported back the
1252 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001253 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001254 "Moving to PAUSED: " + r + " (starting in paused state)");
1255 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 }
1257
1258 // Launch the new version setup screen if needed. We do this -after-
1259 // launching the initial activity (that is, home), so that it can have
1260 // a chance to initialize itself while in the background, making the
1261 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001262 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 mService.startSetupActivityLocked();
1264 }
1265
Dianne Hackborn465fa392014-09-14 14:21:18 -07001266 // Update any services we are bound to that might care about whether
1267 // their client may have activities.
1268 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1269
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 return true;
1271 }
1272
Craig Mautnere79d42682013-04-01 19:01:53 -07001273 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001274 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001275 // Is this activity's application already running?
1276 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001277 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001278
1279 r.task.stack.setLaunchTime(r);
1280
1281 if (app != null && app.thread != null) {
1282 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001283 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1284 || !"android".equals(r.info.packageName)) {
1285 // Don't add this if it is a platform component that is marked
1286 // to run in multiple processes, because this is actually
1287 // part of the framework so doesn't make sense to track as a
1288 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001289 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1290 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001291 }
George Mount2c92c972014-03-20 09:38:23 -07001292 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001293 return;
1294 } catch (RemoteException e) {
1295 Slog.w(TAG, "Exception when starting activity "
1296 + r.intent.getComponent().flattenToShortString(), e);
1297 }
1298
1299 // If a dead object exception was thrown -- fall through to
1300 // restart the application.
1301 }
1302
1303 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001304 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001305 }
1306
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001307 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001308 String resultWho, int requestCode, int callingPid, int callingUid,
1309 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001310 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001311 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1312 callingUid);
1313 if (startAnyPerm == PERMISSION_GRANTED) {
1314 return true;
1315 }
1316 final int componentRestriction = getComponentRestrictionForCallingPackage(
1317 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1318 final int actionRestriction = getActionRestrictionForCallingPackage(
1319 intent.getAction(), callingPackage, callingPid, callingUid);
1320 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1321 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1322 if (resultRecord != null) {
1323 resultStack.sendActivityResultLocked(-1,
1324 resultRecord, resultWho, requestCode,
1325 Activity.RESULT_CANCELED, null);
1326 }
1327 final String msg;
1328 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1329 msg = "Permission Denial: starting " + intent.toString()
1330 + " from " + callerApp + " (pid=" + callingPid
1331 + ", uid=" + callingUid + ")" + " with revoked permission "
1332 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1333 } else if (!aInfo.exported) {
1334 msg = "Permission Denial: starting " + intent.toString()
1335 + " from " + callerApp + " (pid=" + callingPid
1336 + ", uid=" + callingUid + ")"
1337 + " not exported from uid " + aInfo.applicationInfo.uid;
1338 } else {
1339 msg = "Permission Denial: starting " + intent.toString()
1340 + " from " + callerApp + " (pid=" + callingPid
1341 + ", uid=" + callingUid + ")"
1342 + " requires " + aInfo.permission;
1343 }
1344 Slog.w(TAG, msg);
1345 throw new SecurityException(msg);
1346 }
1347
1348 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1349 final String message = "Appop Denial: starting " + intent.toString()
1350 + " from " + callerApp + " (pid=" + callingPid
1351 + ", uid=" + callingUid + ")"
1352 + " requires " + AppOpsManager.permissionToOp(
1353 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1354 Slog.w(TAG, message);
1355 return false;
1356 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1357 final String message = "Appop Denial: starting " + intent.toString()
1358 + " from " + callerApp + " (pid=" + callingPid
1359 + ", uid=" + callingUid + ")"
1360 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1361 Slog.w(TAG, message);
1362 return false;
1363 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001364 if (options != null && options.getLaunchTaskId() != -1) {
1365 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1366 callingPid, callingUid);
1367 if (startInTaskPerm != PERMISSION_GRANTED) {
1368 final String msg = "Permission Denial: starting " + intent.toString()
1369 + " from " + callerApp + " (pid=" + callingPid
1370 + ", uid=" + callingUid + ") with launchTaskId="
1371 + options.getLaunchTaskId();
1372 Slog.w(TAG, msg);
1373 throw new SecurityException(msg);
1374 }
1375 }
1376
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001377 return true;
1378 }
1379
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001380 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001381 final long identity = Binder.clearCallingIdentity();
1382 try {
1383 return UserManager.get(mService.mContext).getUserInfo(userId);
1384 } finally {
1385 Binder.restoreCallingIdentity(identity);
1386 }
1387 }
1388
Svet Ganov99b60432015-06-27 13:15:22 -07001389 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001390 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001391 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1392 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001393 == PackageManager.PERMISSION_DENIED) {
1394 return ACTIVITY_RESTRICTION_PERMISSION;
1395 }
1396
Christopher Tateff7add02015-08-17 10:23:22 -07001397 if (activityInfo.permission == null) {
1398 return ACTIVITY_RESTRICTION_NONE;
1399 }
1400
Svet Ganov99b60432015-06-27 13:15:22 -07001401 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1402 if (opCode == AppOpsManager.OP_NONE) {
1403 return ACTIVITY_RESTRICTION_NONE;
1404 }
1405
1406 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1407 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001408 if (!ignoreTargetSecurity) {
1409 return ACTIVITY_RESTRICTION_APPOP;
1410 }
Svet Ganov99b60432015-06-27 13:15:22 -07001411 }
1412
1413 return ACTIVITY_RESTRICTION_NONE;
1414 }
1415
Svetoslav7008b512015-06-24 18:47:07 -07001416 private int getActionRestrictionForCallingPackage(String action,
1417 String callingPackage, int callingPid, int callingUid) {
1418 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001419 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001420 }
1421
1422 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1423 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001424 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001425 }
1426
1427 final PackageInfo packageInfo;
1428 try {
1429 packageInfo = mService.mContext.getPackageManager()
1430 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1431 } catch (PackageManager.NameNotFoundException e) {
1432 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001433 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001434 }
1435
1436 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001437 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001438 }
1439
1440 if (mService.checkPermission(permission, callingPid, callingUid) ==
1441 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001442 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001443 }
1444
1445 final int opCode = AppOpsManager.permissionToOpCode(permission);
1446 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001447 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001448 }
1449
1450 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1451 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001452 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001453 }
1454
Svet Ganov99b60432015-06-27 13:15:22 -07001455 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001456 }
1457
Wale Ogunwaled046a012015-12-24 13:05:59 -08001458 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001459 if (r == null) {
1460 // Not sure what you are trying to do, but it is not going to work...
1461 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001462 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001463 final TaskRecord task = r.task;
1464 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001465 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001466 return false;
1467 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001468 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001469 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001470 }
1471
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001472 void setLaunchSource(int uid) {
1473 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1474 }
1475
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001476 void acquireLaunchWakelock() {
1477 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1478 throw new IllegalStateException("Calling must be system uid");
1479 }
1480 mLaunchingActivity.acquire();
1481 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1482 // To be safe, don't allow the wake lock to be held for too long.
1483 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1484 }
1485 }
1486
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001487 /**
1488 * Called when the frontmost task is idle.
1489 * @return the state of mService.mBooting before this was called.
1490 */
1491 private boolean checkFinishBootingLocked() {
1492 final boolean booting = mService.mBooting;
1493 boolean enableScreen = false;
1494 mService.mBooting = false;
1495 if (!mService.mBooted) {
1496 mService.mBooted = true;
1497 enableScreen = true;
1498 }
1499 if (booting || enableScreen) {
1500 mService.postFinishBooting(booting, enableScreen);
1501 }
1502 return booting;
1503 }
1504
Craig Mautnerf3333272013-04-22 10:55:53 -07001505 // Checked.
1506 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1507 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001508 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001509
Craig Mautnerf3333272013-04-22 10:55:53 -07001510 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001511 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001512 int NS = 0;
1513 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001514 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001515 boolean activityRemoved = false;
1516
Wale Ogunwale7d701172015-03-11 15:36:30 -07001517 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001519 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1520 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1522 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001523 if (fromTimeout) {
1524 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001525 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001526
1527 // This is a hack to semi-deal with a race condition
1528 // in the client where it can be constructed with a
1529 // newer configuration from when we asked it to launch.
1530 // We'll update with whatever configuration it now says
1531 // it used to launch.
1532 if (config != null) {
1533 r.configuration = config;
1534 }
1535
1536 // We are now idle. If someone is waiting for a thumbnail from
1537 // us, we can now deliver.
1538 r.idle = true;
1539
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001540 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001541 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001542 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001543 }
1544 }
1545
1546 if (allResumedActivitiesIdle()) {
1547 if (r != null) {
1548 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001549 }
1550
1551 if (mLaunchingActivity.isHeld()) {
1552 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1553 if (VALIDATE_WAKE_LOCK_CALLER &&
1554 Binder.getCallingUid() != Process.myUid()) {
1555 throw new IllegalStateException("Calling must be system uid");
1556 }
1557 mLaunchingActivity.release();
1558 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001559 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001560 }
1561
1562 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001563 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001564 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001565 if ((NF = mFinishingActivities.size()) > 0) {
1566 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001567 mFinishingActivities.clear();
1568 }
1569
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001571 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001572 mStartingUsers.clear();
1573 }
1574
Craig Mautnerf3333272013-04-22 10:55:53 -07001575 // Stop any activities that are scheduled to do so but have been
1576 // waiting for the next one to start.
1577 for (int i = 0; i < NS; i++) {
1578 r = stops.get(i);
1579 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001580 if (stack != null) {
1581 if (r.finishing) {
1582 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1583 } else {
1584 stack.stopActivityLocked(r);
1585 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001586 }
1587 }
1588
1589 // Finish any activities that are scheduled to do so but have been
1590 // waiting for the next one to start.
1591 for (int i = 0; i < NF; i++) {
1592 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001593 final ActivityStack stack = r.task.stack;
1594 if (stack != null) {
1595 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1596 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001597 }
1598
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001599 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001600 // Complete user switch
1601 if (startingUsers != null) {
1602 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001603 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001604 }
1605 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001606 }
1607
1608 mService.trimApplications();
1609 //dump();
1610 //mWindowManager.dump();
1611
Craig Mautnerf3333272013-04-22 10:55:53 -07001612 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001613 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001614 }
1615
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001616 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001617 }
1618
Craig Mautner8e569572013-10-11 17:36:59 -07001619 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001620 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001621 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1622 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001623 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1624 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1625 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001626 }
Craig Mautner19091252013-10-05 00:03:53 -07001627 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001628 }
1629
1630 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1632 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001633 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1634 stacks.get(stackNdx).closeSystemDialogsLocked();
1635 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001636 }
1637 }
1638
Craig Mautner93529a42013-10-04 15:03:13 -07001639 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001640 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001641 }
1642
Craig Mautner8d341ef2013-03-26 09:03:27 -07001643 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001644 * Update the last used stack id for non-current user (current user's last
1645 * used stack is the focused stack)
1646 */
1647 void updateUserStackLocked(int userId, ActivityStack stack) {
1648 if (userId != mCurrentUser) {
1649 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1650 }
1651 }
1652
1653 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001654 * @return true if some activity was finished (or would have finished if doit were true).
1655 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001656 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1657 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001658 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001659 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1660 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001661 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001662 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001663 if (stack.finishDisabledPackageActivitiesLocked(
1664 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001665 didSomething = true;
1666 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001667 }
1668 }
1669 return didSomething;
1670 }
1671
Dianne Hackborna413dc02013-07-12 12:02:55 -07001672 void updatePreviousProcessLocked(ActivityRecord r) {
1673 // Now that this process has stopped, we may want to consider
1674 // it to be the previous app to try to keep around in case
1675 // the user wants to return to it.
1676
1677 // First, found out what is currently the foreground app, so that
1678 // we don't blow away the previous app if this activity is being
1679 // hosted by the process that is actually still the foreground.
1680 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001683 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1684 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001685 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001686 if (stack.mResumedActivity != null) {
1687 fgApp = stack.mResumedActivity.app;
1688 } else if (stack.mPausingActivity != null) {
1689 fgApp = stack.mPausingActivity.app;
1690 }
1691 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001692 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001693 }
1694 }
1695
1696 // Now set this one as the previous process, only if that really
1697 // makes sense to.
1698 if (r.app != null && fgApp != null && r.app != fgApp
1699 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001700 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001701 mService.mPreviousProcess = r.app;
1702 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1703 }
1704 }
1705
Wale Ogunwaled046a012015-12-24 13:05:59 -08001706 boolean resumeFocusedStackTopActivityLocked() {
1707 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001708 }
1709
Wale Ogunwaled046a012015-12-24 13:05:59 -08001710 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001711 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001712 if (targetStack != null && isFocusedStack(targetStack)) {
1713 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001714 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001715 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1716 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001717 }
1718
Todd Kennedy39bfee52016-02-24 10:28:21 -08001719 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1721 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1723 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1724 }
1725 }
1726 }
1727
Adrian Roos20d7df32016-01-12 18:59:43 +01001728 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1729 TaskRecord finishedTask = null;
1730 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001731 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1732 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001733 final int numStacks = stacks.size();
1734 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1735 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001736 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1737 if (stack == focusedStack || finishedTask == null) {
1738 finishedTask = t;
1739 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001741 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001742 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001743 }
1744
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001745 void finishVoiceTask(IVoiceInteractionSession session) {
1746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1747 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1748 final int numStacks = stacks.size();
1749 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1750 final ActivityStack stack = stacks.get(stackNdx);
1751 stack.finishVoiceTask(session);
1752 }
1753 }
1754 }
1755
Chong Zhang280d3322015-11-03 17:27:26 -08001756 void findTaskToMoveToFrontLocked(
1757 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001758 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1759 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001760 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001761 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1762 // Caller wants the home activity moved with it. To accomplish this,
1763 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001764 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001765 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001766 if (task.stack == null) {
1767 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1768 + task + " to front. Stack is null");
1769 return;
1770 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001771
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001772 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001773 int stackId = options.getLaunchStackId();
1774 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001775 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001776 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001777 if (stackId == INVALID_STACK_ID) {
1778 stackId = task.getLaunchStackId();
1779 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001780 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001781 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1782 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1783 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001784 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1785 // still need moveTaskToFrontLocked() below for any transition settings.
1786 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001787 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1788 resizeStackLocked(stackId, bounds,
1789 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1790 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1791 } else {
1792 // WM resizeTask must be done after the task is moved to the correct stack,
1793 // because Task's setBounds() also updates dim layer's bounds, but that has
1794 // dependency on the stack.
1795 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1796 false /* relayout */, false /* forced */);
1797 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001798 }
1799 }
1800
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001801 final ActivityRecord r = task.getTopActivity();
1802 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1803 r == null ? null : r.appTimeTracker, reason);
1804
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001805 if (DEBUG_STACK) Slog.d(TAG_STACK,
1806 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001807
1808 showNonResizeableDockToastIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001809 }
1810
Wale Ogunwale854809c2015-12-27 16:18:19 -08001811 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001812 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001813 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001814 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001815 return false;
1816 }
1817 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1818 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001819 }
1820
Craig Mautner967212c2013-04-13 21:10:58 -07001821 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001822 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001823 }
1824
1825 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001826 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1827 if (activityContainer != null) {
1828 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001829 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001830 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001831 return null;
1832 }
1833 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001834 }
1835
Craig Mautner967212c2013-04-13 21:10:58 -07001836 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001837 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001838 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1839 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001840 }
1841 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001842 }
1843
Craig Mautner4a1cb222013-12-04 16:14:06 -08001844 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001845 ActivityRecord homeActivity = getHomeActivity();
1846 if (homeActivity != null) {
1847 return homeActivity.appToken;
1848 }
1849 return null;
1850 }
1851
1852 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001853 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001854 }
1855
1856 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001857 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1858 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1859 final TaskRecord task = tasks.get(taskNdx);
1860 if (task.isHomeTask()) {
1861 final ArrayList<ActivityRecord> activities = task.mActivities;
1862 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1863 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001864 if (r.isHomeActivity()
1865 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001866 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001867 }
1868 }
1869 }
1870 }
1871 return null;
1872 }
1873
Chong Zhangb15758a2015-11-17 12:12:03 -08001874 /**
1875 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1876 * the docked stack itself, or if it's side-by-side to the docked stack.
1877 */
1878 boolean isStackDockedInEffect(int stackId) {
1879 return stackId == DOCKED_STACK_ID ||
1880 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1881 }
1882
Todd Kennedyca4d8422015-01-15 15:19:22 -08001883 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001884 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001885 ActivityContainer activityContainer =
1886 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001887 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001888 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1889 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001890 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001891 return activityContainer;
1892 }
1893
Craig Mautner34b73df2014-01-12 21:11:08 -08001894 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001895 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1896 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1897 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001898 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1899 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001900 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001901 }
1902 }
1903
Craig Mautner95da1082014-02-24 17:54:35 -08001904 void deleteActivityContainer(IActivityContainer container) {
1905 ActivityContainer activityContainer = (ActivityContainer)container;
1906 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001907 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1908 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001909 final int stackId = activityContainer.mStackId;
1910 mActivityContainers.remove(stackId);
1911 mWindowManager.removeStack(stackId);
1912 }
1913 }
1914
Jorim Jaggidc249c42015-12-15 14:57:31 -08001915 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1916 boolean preserveWindows, boolean allowResizeInDockedMode) {
1917 if (stackId == DOCKED_STACK_ID) {
1918 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1919 preserveWindows);
1920 return;
1921 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001922 final ActivityStack stack = getStack(stackId);
1923 if (stack == null) {
1924 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1925 return;
1926 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001927
Jorim Jaggidc249c42015-12-15 14:57:31 -08001928 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001929 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1930 // stack size changing so things don't get out of sync.
1931 return;
1932 }
1933
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001934 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001935 mWindowManager.deferSurfaceLayout();
1936 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001937 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1938 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001939 } finally {
1940 mWindowManager.continueSurfaceLayout();
1941 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001942 }
1943 }
1944
Jorim Jaggidc249c42015-12-15 14:57:31 -08001945 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1946 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001947 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001948
1949 mTmpBounds.clear();
1950 mTmpConfigs.clear();
1951 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001952 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001953 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001954 final TaskRecord task = tasks.get(i);
1955 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001956 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1957 // For freeform stack we don't adjust the size of the tasks to match that
1958 // of the stack, but we do try to make sure the tasks are still contained
1959 // with the bounds of the stack.
1960 tempRect2.set(task.mBounds);
1961 fitWithinBounds(tempRect2, bounds);
1962 task.updateOverrideConfiguration(tempRect2);
1963 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001964 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001965 tempTaskBounds != null ? tempTaskBounds : bounds,
1966 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001967 }
1968 }
1969
1970 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1971 mTmpBounds.put(task.taskId, task.mBounds);
1972 if (tempTaskInsetBounds != null) {
1973 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1974 }
1975 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001976
1977 // We might trigger a configuration change. Save the current task bounds for freezing.
1978 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001979 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1980 mTmpBounds, mTmpInsetBounds);
1981 stack.setBounds(bounds);
1982 }
1983
1984 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1985 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08001986 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001987 return;
1988 }
1989 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1990 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001991 if (!updated) {
1992 resumeFocusedStackTopActivityLocked();
1993 }
1994 }
1995
1996 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1997 Rect tempDockedTaskInsetBounds,
1998 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1999 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2000 if (stack == null) {
2001 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2002 return;
2003 }
2004
2005 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2006 mWindowManager.deferSurfaceLayout();
2007 try {
2008 ActivityRecord r = stack.topRunningActivityLocked();
2009 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2010 tempDockedTaskInsetBounds);
2011
2012 if (stack.mFullscreen) {
2013 // The dock stack went fullscreen which is kinda like dismissing it.
2014 // In this case we make all other static stacks fullscreen and move all
2015 // docked stack tasks to the fullscreen stack.
2016 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2017 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2018 resizeStackLocked(i, null, null, null, preserveWindows,
2019 true /* allowResizeInDockedMode */);
2020 }
2021 }
2022
2023 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2024 final int count = tasks.size();
2025 for (int i = 0; i < count; i++) {
2026 moveTaskToStackLocked(tasks.get(i).taskId,
2027 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2028 false /* animate */);
2029 }
2030
2031 // stack shouldn't contain anymore activities, so nothing to resume.
2032 r = null;
2033 } else {
2034 // Docked stacks occupy a dedicated region on screen so the size of all other
2035 // static stacks need to be adjusted so they don't overlap with the docked stack.
2036 // We get the bounds to use from window manager which has been adjusted for any
2037 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002038 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002039 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002040 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2041 if (StackId.isResizeableByDockedStack(i)) {
2042 ActivityStack otherStack = getStack(i);
2043 if (otherStack != null) {
2044 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2045 tempOtherTaskInsetBounds, preserveWindows,
2046 true /* allowResizeInDockedMode */);
2047 }
2048 }
2049 }
2050 }
2051 ensureConfigurationAndResume(stack, r, preserveWindows);
2052 } finally {
2053 mWindowManager.continueSurfaceLayout();
2054 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2055 }
2056
2057 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2058 tempDockedTaskBounds != null
2059 || tempDockedTaskInsetBounds != null
2060 || tempOtherTaskBounds != null
2061 || tempOtherTaskInsetBounds != null);
2062 }
2063
Robert Carr0d00c2e2016-02-29 17:45:02 -08002064 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2065 final ActivityStack stack = getStack(PINNED_STACK_ID);
2066 if (stack == null) {
2067 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2068 return;
2069 }
2070 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2071 mWindowManager.deferSurfaceLayout();
2072 try {
2073 ActivityRecord r = stack.topRunningActivityLocked();
2074 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2075 null);
2076 ensureConfigurationAndResume(stack, r, false);
2077 } finally {
2078 mWindowManager.continueSurfaceLayout();
2079 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2080 }
2081 }
2082
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002083 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2084 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002085 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002086 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002087 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002088 }
2089
Chong Zhang87b21722015-09-21 15:39:51 -07002090 // If this is a forced resize, let it go through even if the bounds is not changing,
2091 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002092 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002093 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002094 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002095 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002096 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002097 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002098
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002099 if (!mWindowManager.isValidTaskId(task.taskId)) {
2100 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002101 // All we can do for now is update the bounds so it can be used when the task is
2102 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002103 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002104 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2105 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002106 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002107 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002108 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002109 }
2110
Chong Zhang6de2ae82015-09-30 18:25:21 -07002111 // Do not move the task to another stack here.
2112 // This method assumes that the task is already placed in the right stack.
2113 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002114
Chong Zhang6de2ae82015-09-30 18:25:21 -07002115 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002116
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002117 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002118 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002119 // way and the activity was kept the way it was. If it's false, it means the activity had
2120 // to be relaunched due to configuration change.
2121 boolean kept = true;
2122 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002123 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002124 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002125 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002126 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002127
2128 if (!deferResume) {
2129
2130 // All other activities must be made visible with their correct configuration.
2131 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2132 if (!kept) {
2133 resumeFocusedStackTopActivityLocked();
2134 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002135 }
2136 }
2137 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002138 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002139
2140 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002141 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002142 }
2143
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002144 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002145 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2146 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002147 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002148 }
2149
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002150 ActivityContainer activityContainer = new ActivityContainer(stackId);
2151 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002152 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002153 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002154 }
2155
2156 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002157 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002158 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2159 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002160 break;
2161 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002162 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002163 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002164 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002165 }
2166
Chong Zhang5dcb2752015-08-18 13:50:26 -07002167 /**
2168 * Restores a recent task to a stack
2169 * @param task The recent task to be restored.
2170 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002171 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002172 * @return true if the task has been restored successfully.
2173 */
2174 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2175 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002176 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002177 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2178 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2179 // Put it in the fullscreen stack.
2180 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002181 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002182
Wale Ogunwale706ed792015-08-02 10:29:44 -07002183 if (task.stack != null) {
2184 // Task has already been restored once. See if we need to do anything more
2185 if (task.stack.mStackId == stackId) {
2186 // Nothing else to do since it is already restored in the right stack.
2187 return true;
2188 }
2189 // Remove current stack association, so we can re-associate the task with the
2190 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002191 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002192 }
2193
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002194 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002195 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002196
2197 if (stack == null) {
2198 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002199 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2200 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002201 return false;
2202 }
2203
Wale Ogunwale5f986092015-12-04 15:35:38 -08002204 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002205 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2206 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002207 final ArrayList<ActivityRecord> activities = task.mActivities;
2208 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002209 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002210 }
2211 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002212 }
2213
Wale Ogunwale040b4702015-08-06 18:10:50 -07002214 /**
2215 * Moves the specified task record to the input stack id.
2216 * WARNING: This method performs an unchecked/raw move of the task and
2217 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002218 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002219 * @param task Task to move.
2220 * @param stackId Id of stack to move task to.
2221 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002222 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002223 * @param reason Reason the task is been moved.
2224 * @return The stack the task was moved to.
2225 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002226 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002227 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002228
2229 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2230 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2231 + "support multi-window task=" + task + " to stackId=" + stackId);
2232 }
2233
Chong Zhang02898352015-08-21 17:27:14 -07002234 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002235 final ActivityStack prevStack = task.stack;
2236 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2237 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2238 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2239 // Whenever we are moving the top activity from the front stack we want to make sure to move
2240 // the stack to the front.
2241 final boolean wasFront = isFrontStack(prevStack)
2242 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002243
Chong Zhangd545dce2016-02-29 18:09:17 -08002244 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002245 // We don't allow moving a unresizeable task to the docked stack since the docked
2246 // stack is used for split-screen mode and will cause things like the docked divider to
2247 // show up. We instead leave the task in its current stack or move it to the fullscreen
2248 // stack if it isn't currently in a stack.
2249 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002250 Slog.w(TAG, "Can not move unresizeable task=" + task
2251 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2252 }
2253
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002254 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2255 // if a docked stack is created below which will lead to the stack we are moving from and
2256 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002257 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002258 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002259 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002260 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002261 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002262
2263 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002264 // move focus to the new stack by moving the stack to the front.
2265 stack.moveToFrontAndResumeStateIfNeeded(
2266 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002267
Wale Ogunwale040b4702015-08-06 18:10:50 -07002268 return stack;
2269 }
2270
Chong Zhange4fbd322016-03-01 14:44:03 -08002271 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002272 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002273 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2274 false /* deferResume */);
2275 }
2276
2277 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2278 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002279 final TaskRecord task = anyTaskForIdLocked(taskId);
2280 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002281 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002282 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002283 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002284
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002285 if (task.stack != null && task.stack.mStackId == stackId) {
2286 // You are already in the right stack silly...
2287 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002288 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002289 }
2290
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002291 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2292 throw new IllegalArgumentException("moveTaskToStack:"
2293 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2294 }
2295
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002296 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002297 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002298 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002299 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002300 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002301 // We are about to relaunch the activity because its configuration changed due to
2302 // being maximized, i.e. size change. The activity will first remove the old window
2303 // and then add a new one. This call will tell window manager about this, so it can
2304 // preserve the old window until the new one is drawn. This prevents having a gap
2305 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002306 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002307 // Note here we always set the replacing window first, as the flags might be needed
2308 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002309 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002310 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002311
2312 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002313 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002314 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002315 try {
2316 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002317 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002318 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002319
Wale Ogunwale961f4852016-02-01 20:25:54 -08002320 if (!animate) {
2321 stack.mNoAnimActivities.add(topActivity);
2322 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002323
Wale Ogunwale961f4852016-02-01 20:25:54 -08002324 // We might trigger a configuration change. Save the current task bounds for freezing.
2325 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2326
2327 // Make sure the task has the appropriate bounds/size for the stack it is in.
2328 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002329 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2330 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002331 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2332 Rect bounds = task.getLaunchBounds();
2333 if (bounds == null) {
2334 stack.layoutTaskInStack(task, null);
2335 bounds = task.mBounds;
2336 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002337 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2338 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002339 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002340 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2341 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002342 }
2343 } finally {
2344 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002345 }
2346
2347 if (mightReplaceWindow) {
2348 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2349 // window), we need to clear the replace window settings. Otherwise, we schedule a
2350 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002351 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002352 }
2353
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002354 if (!deferResume) {
2355
2356 // The task might have already been running and its visibility needs to be synchronized with
2357 // the visibility of the stack / windows.
2358 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2359 resumeFocusedStackTopActivityLocked();
2360 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002361
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002362 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002363
2364 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002365 }
2366
Wale Ogunwale079a0042015-10-24 11:44:07 -07002367 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2368 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2369 if (stack == null) {
2370 throw new IllegalArgumentException(
2371 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2372 }
2373
2374 final ActivityRecord r = stack.topRunningActivityLocked();
2375 if (r == null) {
2376 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2377 + " in stack=" + stack);
2378 return false;
2379 }
2380
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002381 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002382 Slog.w(TAG,
2383 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002384 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002385 return false;
2386 }
2387
Wale Ogunwale480dca02016-02-06 13:58:29 -08002388 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002389 return true;
2390 }
2391
Wale Ogunwale480dca02016-02-06 13:58:29 -08002392 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2393 mWindowManager.deferSurfaceLayout();
2394 try {
2395 final TaskRecord task = r.task;
2396
2397 // Need to make sure the pinned stack exist so we can resize it below...
2398 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2399
2400 // Resize the pinned stack to match the current size of the task the activity we are
2401 // going to be moving is currently contained in. We do this to have the right starting
2402 // animation bounds for the pinned stack to the desired bounds the caller wants.
2403 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2404 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2405 true /* allowResizeInDockedMode */);
2406
2407 if (task.mActivities.size() == 1) {
2408 // There is only one activity in the task. So, we can just move the task over to
2409 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002410 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2411 // Move the home stack forward if the task we just moved to the pinned stack
2412 // was launched from home so home should be visible behind it.
2413 moveHomeStackToFront(reason);
2414 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002415 moveTaskToStackLocked(
2416 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2417 } else {
2418 stack.moveActivityToStack(r);
2419 }
2420 } finally {
2421 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002422 }
2423
Wale Ogunwale480dca02016-02-06 13:58:29 -08002424 // The task might have already been running and its visibility needs to be synchronized
2425 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002426 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002427 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002428
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002429 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002430 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002431 }
2432
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002433 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2434 final TaskRecord task = anyTaskForIdLocked(taskId);
2435 if (task == null) {
2436 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2437 return;
2438 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002439 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2440
2441 task.updateOverrideConfigurationForStack(stack);
2442
2443 mWindowManager.positionTaskInStack(
2444 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002445 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002446 // The task might have already been running and its visibility needs to be synchronized with
2447 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002448 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2449 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002450 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002451 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002452 }
2453
Craig Mautnerac6f8432013-07-17 13:24:59 -07002454 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002455 mTmpFindTaskResult.r = null;
2456 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002457 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002458 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2459 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002460 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2461 final ActivityStack stack = stacks.get(stackNdx);
2462 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002463 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002464 continue;
2465 }
2466 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002467 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2468 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002469 continue;
2470 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002471 stack.findTaskLocked(r, mTmpFindTaskResult);
2472 // It is possible to have task in multiple stacks with the same root affinity.
2473 // If the match we found was based on root affinity we keep on looking to see if
2474 // there is a better match in another stack. We eventually return the match based
2475 // on root affinity if we don't find a better match.
2476 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2477 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002478 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002479 }
2480 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002481 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2482 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002483 }
2484
2485 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002486 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2487 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2489 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2490 if (ar != null) {
2491 return ar;
2492 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002493 }
2494 }
2495 return null;
2496 }
2497
Craig Mautner8d341ef2013-03-26 09:03:27 -07002498 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002499 scheduleSleepTimeout();
2500 if (!mGoingToSleep.isHeld()) {
2501 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002502 if (mLaunchingActivity.isHeld()) {
2503 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2504 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002505 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002506 mLaunchingActivity.release();
2507 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002508 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002510 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002511 }
2512
2513 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002514 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002515
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002516 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002517 final long endTime = System.currentTimeMillis() + timeout;
2518 while (true) {
2519 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002522 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2523 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2524 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002525 }
2526 if (cantShutdown) {
2527 long timeRemaining = endTime - System.currentTimeMillis();
2528 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002530 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002531 } catch (InterruptedException e) {
2532 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002533 } else {
2534 Slog.w(TAG, "Activity manager shutdown timed out");
2535 timedout = true;
2536 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002537 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002538 } else {
2539 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002540 }
2541 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002542
2543 // Force checkReadyForSleep to complete.
2544 mSleepTimeout = true;
2545 checkReadyForSleepLocked();
2546
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 return timedout;
2548 }
2549
2550 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002551 removeSleepTimeouts();
2552 if (mGoingToSleep.isHeld()) {
2553 mGoingToSleep.release();
2554 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002555 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2556 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2558 final ActivityStack stack = stacks.get(stackNdx);
2559 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002560 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002561 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002562 }
Craig Mautner5314a402013-09-26 12:40:16 -07002563 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002565 mGoingToSleepActivities.clear();
2566 }
2567
2568 void activitySleptLocked(ActivityRecord r) {
2569 mGoingToSleepActivities.remove(r);
2570 checkReadyForSleepLocked();
2571 }
2572
2573 void checkReadyForSleepLocked() {
2574 if (!mService.isSleepingOrShuttingDown()) {
2575 // Do not care.
2576 return;
2577 }
2578
2579 if (!mSleepTimeout) {
2580 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2584 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2585 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002586 }
2587
2588 if (mStoppingActivities.size() > 0) {
2589 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002590 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002591 + mStoppingActivities.size() + " activities");
2592 scheduleIdleLocked();
2593 dontSleep = true;
2594 }
2595
2596 if (mGoingToSleepActivities.size() > 0) {
2597 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002598 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002599 + mGoingToSleepActivities.size() + " activities");
2600 dontSleep = true;
2601 }
2602
2603 if (dontSleep) {
2604 return;
2605 }
2606 }
2607
Craig Mautnere0a38842013-12-16 16:14:02 -08002608 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2609 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002610 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2611 stacks.get(stackNdx).goToSleep();
2612 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002613 }
2614
2615 removeSleepTimeouts();
2616
2617 if (mGoingToSleep.isHeld()) {
2618 mGoingToSleep.release();
2619 }
2620 if (mService.mShuttingDown) {
2621 mService.notifyAll();
2622 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002623 }
2624
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002625 boolean reportResumedActivityLocked(ActivityRecord r) {
2626 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002627 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002628 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002629 }
2630 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002631 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002632 mWindowManager.executeAppTransition();
2633 return true;
2634 }
2635 return false;
2636 }
2637
Craig Mautner8d341ef2013-03-26 09:03:27 -07002638 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2640 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002641 int stackNdx = stacks.size() - 1;
2642 while (stackNdx >= 0) {
2643 stacks.get(stackNdx).handleAppCrashLocked(app);
2644 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002646 }
2647 }
2648
Jose Lima4b6c6692014-08-12 17:41:12 -07002649 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002650 final ActivityStack stack = r.task.stack;
2651 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002652 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2653 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002654 return false;
2655 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002656 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002657 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2658 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002659
2660 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002661 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002662 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002663 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002664 return true;
2665 }
2666
2667 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002668 if (visible && top.fullscreen) {
2669 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002670 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2671 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2672 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2673 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002674 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002675 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2676 // Only the activity set as currently visible behind should actively reset its
2677 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002678 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2679 "requestVisibleBehind: returning visible=" + visible
2680 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2681 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002682 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002683 }
2684
Jose Lima4b6c6692014-08-12 17:41:12 -07002685 stack.setVisibleBehindActivity(visible ? r : null);
2686 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002687 // If there is a translucent home activity, we need to force it stop being translucent,
2688 // because we can't depend on the application to necessarily perform that operation.
2689 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002690 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002691 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002692 mService.convertFromTranslucent(next.appToken);
2693 }
2694 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002695 if (top.app != null && top.app.thread != null) {
2696 // Notify the top app of the change.
2697 try {
2698 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2699 } catch (RemoteException e) {
2700 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002701 }
2702 return true;
2703 }
2704
Craig Mautnerbb742462014-07-07 15:28:55 -07002705 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2706 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2707 r.mLaunchTaskBehind = false;
2708 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002709 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002710 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002711 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002712 mWindowManager.setAppVisibility(r.appToken, false);
2713 }
2714
2715 void scheduleLaunchTaskBehindComplete(IBinder token) {
2716 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2717 }
2718
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002719 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2720 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002721 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 final int topStackNdx = stacks.size() - 1;
2725 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2726 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002727 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002728 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002729 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002730 }
2731
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002732 void invalidateTaskLayers() {
2733 mTaskLayersChanged = true;
2734 }
2735
2736 void rankTaskLayersIfNeeded() {
2737 if (!mTaskLayersChanged) {
2738 return;
2739 }
2740 mTaskLayersChanged = false;
2741 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2742 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2743 int baseLayer = 0;
2744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2745 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2746 }
2747 }
2748 }
2749
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002750 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2751 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2752 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2753 final int topStackNdx = stacks.size() - 1;
2754 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2755 final ActivityStack stack = stacks.get(stackNdx);
2756 stack.clearOtherAppTimeTrackers(except);
2757 }
2758 }
2759 }
2760
Craig Mautner8d341ef2013-03-26 09:03:27 -07002761 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2763 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002764 final int numStacks = stacks.size();
2765 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2766 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002767 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002768 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
2770 }
2771
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002772 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2773 // Examine all activities currently running in the process.
2774 TaskRecord firstTask = null;
2775 // Tasks is non-null only if two or more tasks are found.
2776 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002777 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2778 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002779 ActivityRecord r = app.activities.get(i);
2780 // First, if we find an activity that is in the process of being destroyed,
2781 // then we just aren't going to do anything for now; we want things to settle
2782 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002783 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002784 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002785 return;
2786 }
2787 // Don't consider any activies that are currently not in a state where they
2788 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002789 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2790 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002791 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002792 continue;
2793 }
2794 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002795 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002796 + " from " + r);
2797 if (firstTask == null) {
2798 firstTask = r.task;
2799 } else if (firstTask != r.task) {
2800 if (tasks == null) {
2801 tasks = new ArraySet<>();
2802 tasks.add(firstTask);
2803 }
2804 tasks.add(r.task);
2805 }
2806 }
2807 }
2808 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002809 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002810 return;
2811 }
2812 // If we have activities in multiple tasks that are in a position to be destroyed,
2813 // let's iterate through the tasks and release the oldest one.
2814 final int numDisplays = mActivityDisplays.size();
2815 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2816 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2817 // Step through all stacks starting from behind, to hit the oldest things first.
2818 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2819 final ActivityStack stack = stacks.get(stackNdx);
2820 // Try to release activities in this stack; if we manage to, we are done.
2821 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2822 return;
2823 }
2824 }
2825 }
2826 }
2827
Amith Yamasani37a40c22015-06-17 13:25:42 -07002828 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002829 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002830 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002831 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002832
Craig Mautner858d8a62013-04-23 17:08:34 -07002833 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2835 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002837 final ActivityStack stack = stacks.get(stackNdx);
2838 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002839 TaskRecord task = stack.topTask();
2840 if (task != null) {
2841 mWindowManager.moveTaskToTop(task.taskId);
2842 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002843 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002844 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002845
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002846 ActivityStack stack = getStack(restoreStackId);
2847 if (stack == null) {
2848 stack = mHomeStack;
2849 }
2850 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002852 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002853 } else {
2854 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002855 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002856 }
Craig Mautner93529a42013-10-04 15:03:13 -07002857 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002858 }
2859
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002860 /** Checks whether the userid is a profile of the current user. */
2861 boolean isCurrentProfileLocked(int userId) {
2862 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002863 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002864 }
2865
Chong Zhang45c25ce2015-08-10 22:18:26 -07002866 /** Checks whether the activity should be shown for current user. */
2867 boolean okToShowLocked(ActivityRecord r) {
2868 return r != null && (isCurrentProfileLocked(r.userId)
2869 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2870 }
2871
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002873 ArrayList<ActivityRecord> stops = null;
2874
2875 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002876 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2877 ActivityRecord s = mStoppingActivities.get(activityNdx);
2878 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002879 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002880 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2881 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002882 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002883 if (s.finishing) {
2884 // If this activity is finishing, it is sitting on top of
2885 // everyone else but we now know it is no longer needed...
2886 // so get rid of it. Otherwise, we need to go through the
2887 // normal flow and hide it once we determine that it is
2888 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002889 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002890 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002891 }
2892 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002893 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002894 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002895 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002896 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002897 }
2898 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002899 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002900 }
2901 }
2902
2903 return stops;
2904 }
2905
Craig Mautnercf910b02013-04-23 11:23:27 -07002906 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002907 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2908 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2909 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2910 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002911 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002912 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002913 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002914 if (r == null) Slog.e(TAG,
2915 "validateTop...: null top activity, stack=" + stack);
2916 else {
2917 final ActivityRecord pausing = stack.mPausingActivity;
2918 if (pausing != null && pausing == r) Slog.e(TAG,
2919 "validateTop...: top stack has pausing activity r=" + r
2920 + " state=" + state);
2921 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2922 "validateTop...: activity in front not resumed r=" + r
2923 + " state=" + state);
2924 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002925 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002926 final ActivityRecord resumed = stack.mResumedActivity;
2927 if (resumed != null && resumed == r) Slog.e(TAG,
2928 "validateTop...: back stack has resumed activity r=" + r
2929 + " state=" + state);
2930 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2931 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002932 }
2933 }
2934 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002935 }
2936
Craig Mautnere0570202015-05-13 13:06:11 -07002937 private String lockTaskModeToString() {
2938 switch (mLockTaskModeState) {
2939 case LOCK_TASK_MODE_LOCKED:
2940 return "LOCKED";
2941 case LOCK_TASK_MODE_PINNED:
2942 return "PINNED";
2943 case LOCK_TASK_MODE_NONE:
2944 return "NONE";
2945 default: return "unknown=" + mLockTaskModeState;
2946 }
2947 }
2948
Craig Mautner27084302013-03-25 08:05:25 -07002949 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002950 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002952 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002953 pw.print(prefix);
2954 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002955 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002956 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002957 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2958 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2959 if (packages.size() > 0) {
2960 pw.println(" mLockTaskPackages (userId:packages)=");
2961 for (int i = 0; i < packages.size(); ++i) {
2962 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2963 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2964 }
2965 }
2966 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002967 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968
Craig Mautner20e72272013-04-01 13:45:53 -07002969 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002970 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002971 }
2972
Dianne Hackborn390517b2013-05-30 15:03:32 -07002973 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2974 boolean needSep, String prefix) {
2975 if (activity != null) {
2976 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2977 if (needSep) {
2978 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002979 }
2980 pw.print(prefix);
2981 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002982 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002983 }
2984 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002985 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002986 }
2987
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2989 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002990 boolean printed = false;
2991 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002992 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2993 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002994 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002995 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002996 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002998 final ActivityStack stack = stacks.get(stackNdx);
2999 StringBuilder stackHeader = new StringBuilder(128);
3000 stackHeader.append(" Stack #");
3001 stackHeader.append(stack.mStackId);
3002 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003003 stackHeader.append("\n");
3004 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3005 stackHeader.append("\n");
3006 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003007 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3008 needSep, stackHeader.toString());
3009 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3010 !dumpAll, false, dumpPackage, true,
3011 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003012
Craig Mautner4a1cb222013-12-04 16:14:06 -08003013 needSep = printed;
3014 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3015 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003016 if (pr) {
3017 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003018 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003019 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003020 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3021 " mResumedActivity: ");
3022 if (pr) {
3023 printed = true;
3024 needSep = false;
3025 }
3026 if (dumpAll) {
3027 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3028 " mLastPausedActivity: ");
3029 if (pr) {
3030 printed = true;
3031 needSep = true;
3032 }
3033 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3034 needSep, " mLastNoHistoryActivity: ");
3035 }
3036 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003037 }
3038 }
3039
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003040 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3041 false, dumpPackage, true, " Activities waiting to finish:", null);
3042 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3043 false, dumpPackage, true, " Activities waiting to stop:", null);
3044 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3045 false, dumpPackage, true, " Activities waiting for another to become visible:",
3046 null);
3047 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3048 false, dumpPackage, true, " Activities waiting to sleep:", null);
3049 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3050 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003051
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003052 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003053 }
3054
Dianne Hackborn390517b2013-05-30 15:03:32 -07003055 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003056 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003057 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003058 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003059 String innerPrefix = null;
3060 String[] args = null;
3061 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003062 for (int i=list.size()-1; i>=0; i--) {
3063 final ActivityRecord r = list.get(i);
3064 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3065 continue;
3066 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003067 if (innerPrefix == null) {
3068 innerPrefix = prefix + " ";
3069 args = new String[0];
3070 }
3071 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003072 final boolean full = !brief && (complete || !r.isInHistory());
3073 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003074 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003075 needNL = false;
3076 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003077 if (header1 != null) {
3078 pw.println(header1);
3079 header1 = null;
3080 }
3081 if (header2 != null) {
3082 pw.println(header2);
3083 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003084 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003085 if (lastTask != r.task) {
3086 lastTask = r.task;
3087 pw.print(prefix);
3088 pw.print(full ? "* " : " ");
3089 pw.println(lastTask);
3090 if (full) {
3091 lastTask.dump(pw, prefix + " ");
3092 } else if (complete) {
3093 // Complete + brief == give a summary. Isn't that obvious?!?
3094 if (lastTask.intent != null) {
3095 pw.print(prefix); pw.print(" ");
3096 pw.println(lastTask.intent.toInsecureStringWithClip());
3097 }
3098 }
3099 }
3100 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3101 pw.print(" #"); pw.print(i); pw.print(": ");
3102 pw.println(r);
3103 if (full) {
3104 r.dump(pw, innerPrefix);
3105 } else if (complete) {
3106 // Complete + brief == give a summary. Isn't that obvious?!?
3107 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3108 if (r.app != null) {
3109 pw.print(innerPrefix); pw.println(r.app);
3110 }
3111 }
3112 if (client && r.app != null && r.app.thread != null) {
3113 // flush anything that is already in the PrintWriter since the thread is going
3114 // to write to the file descriptor directly
3115 pw.flush();
3116 try {
3117 TransferPipe tp = new TransferPipe();
3118 try {
3119 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3120 r.appToken, innerPrefix, args);
3121 // Short timeout, since blocking here can
3122 // deadlock with the application.
3123 tp.go(fd, 2000);
3124 } finally {
3125 tp.kill();
3126 }
3127 } catch (IOException e) {
3128 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3129 } catch (RemoteException e) {
3130 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3131 }
3132 needNL = true;
3133 }
3134 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003135 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003136 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003137
Craig Mautnerf3333272013-04-22 10:55:53 -07003138 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003139 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3140 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003141 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3142 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003143 }
3144
3145 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003146 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003147 }
3148
3149 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003150 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3151 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003152 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3153 }
3154
Craig Mautner05d29032013-05-03 13:40:13 -07003155 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003156 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3157 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3158 }
Craig Mautner05d29032013-05-03 13:40:13 -07003159 }
3160
Craig Mautner0eea92c2013-05-16 13:35:39 -07003161 void removeSleepTimeouts() {
3162 mSleepTimeout = false;
3163 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3164 }
3165
3166 final void scheduleSleepTimeout() {
3167 removeSleepTimeouts();
3168 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3169 }
3170
Craig Mautner4a1cb222013-12-04 16:14:06 -08003171 @Override
3172 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003173 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3175 }
3176
3177 @Override
3178 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003179 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003180 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3181 }
3182
3183 @Override
3184 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003185 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3187 }
3188
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003189 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003190 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003192 newDisplay = mActivityDisplays.get(displayId) == null;
3193 if (newDisplay) {
3194 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003195 if (activityDisplay.mDisplay == null) {
3196 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3197 return;
3198 }
Craig Mautner4504de52013-12-20 09:06:56 -08003199 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003200 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003201 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003202 }
Craig Mautner4504de52013-12-20 09:06:56 -08003203 if (newDisplay) {
3204 mWindowManager.onDisplayAdded(displayId);
3205 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 }
3207
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003208 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3209 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3210 return;
3211 }
3212 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3213 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3214 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3215 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3216 }
3217
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003218 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003220 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3221 if (activityDisplay != null) {
3222 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003224 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003226 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003227 }
3228 }
3229 mWindowManager.onDisplayRemoved(displayId);
3230 }
3231
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003232 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003233 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003234 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3235 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 // TODO: Update the bounds.
3237 }
3238 }
3239 mWindowManager.onDisplayChanged(displayId);
3240 }
3241
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003242 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003243 StackInfo info = new StackInfo();
3244 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3245 info.displayId = Display.DEFAULT_DISPLAY;
3246 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003247 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003248
3249 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3250 final int numTasks = tasks.size();
3251 int[] taskIds = new int[numTasks];
3252 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003253 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003254 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003255 for (int i = 0; i < numTasks; ++i) {
3256 final TaskRecord task = tasks.get(i);
3257 taskIds[i] = task.taskId;
3258 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3259 : task.realActivity != null ? task.realActivity.flattenToString()
3260 : task.getTopActivity() != null ? task.getTopActivity().packageName
3261 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003262 taskBounds[i] = new Rect();
3263 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003264 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003265 }
3266 info.taskIds = taskIds;
3267 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003268 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003269 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003270 return info;
3271 }
3272
3273 StackInfo getStackInfoLocked(int stackId) {
3274 ActivityStack stack = getStack(stackId);
3275 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003276 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003277 }
3278 return null;
3279 }
3280
3281 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003282 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003283 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3284 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003285 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003286 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003287 }
3288 }
3289 return list;
3290 }
3291
Craig Mautner15df08a2015-04-01 12:17:18 -07003292 TaskRecord getLockedTaskLocked() {
3293 final int top = mLockTaskModeTasks.size() - 1;
3294 if (top >= 0) {
3295 return mLockTaskModeTasks.get(top);
3296 }
3297 return null;
3298 }
3299
3300 boolean isLockedTask(TaskRecord task) {
3301 return mLockTaskModeTasks.contains(task);
3302 }
3303
3304 boolean isLastLockedTask(TaskRecord task) {
3305 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3306 }
3307
3308 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003309 if (!mLockTaskModeTasks.remove(task)) {
3310 return;
3311 }
3312 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3313 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003314 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003315 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3316 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003317 final Message lockTaskMsg = Message.obtain();
3318 lockTaskMsg.arg1 = task.userId;
3319 lockTaskMsg.what = LOCK_TASK_END_MSG;
3320 mHandler.sendMessage(lockTaskMsg);
3321 }
3322 }
3323
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003324 void showNonResizeableDockToastIfNeeded(
3325 TaskRecord task, int preferredStackId, int actualStackId) {
3326 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3327 return;
3328 }
3329
Jorim Jaggi2adba072016-03-03 13:43:39 +01003330 if (!task.canGoInDockedStack()) {
3331 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003332 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003333 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3334 String packageName = task.getTopActivity() != null
3335 ? task.getTopActivity().appInfo.packageName : null;
3336 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3337 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003338 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003339 }
3340
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003341 void showLockTaskToast() {
3342 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003343 }
3344
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003345 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3346 if (mLockTaskModeTasks.contains(task)) {
3347 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3348 }
3349 }
3350
Craig Mautner432f64e2015-05-20 14:59:57 -07003351 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3352 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003353 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003354 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003355 final TaskRecord lockedTask = getLockedTaskLocked();
3356 if (lockedTask != null) {
3357 removeLockedTaskLocked(lockedTask);
3358 if (!mLockTaskModeTasks.isEmpty()) {
3359 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003360 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3361 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003362 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003363 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003364 return;
3365 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003366 }
Craig Mautnere0570202015-05-13 13:06:11 -07003367 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3368 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003369 return;
3370 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003371
3372 // Should have already been checked, but do it again.
3373 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003374 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3375 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003376 return;
3377 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003378 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003379 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003380 return;
3381 }
3382
3383 if (mLockTaskModeTasks.isEmpty()) {
3384 // First locktask.
3385 final Message lockTaskMsg = Message.obtain();
3386 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3387 lockTaskMsg.arg1 = task.userId;
3388 lockTaskMsg.what = LOCK_TASK_START_MSG;
3389 lockTaskMsg.arg2 = lockTaskModeState;
3390 mHandler.sendMessage(lockTaskMsg);
3391 }
3392 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003393 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3394 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003395 mLockTaskModeTasks.remove(task);
3396 mLockTaskModeTasks.add(task);
3397
3398 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003399 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003400 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003401
3402 if (andResume) {
3403 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003404 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003405 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003406 }
3407
3408 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003409 return isLockTaskModeViolation(task, false);
3410 }
3411
3412 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3413 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003414 return false;
3415 }
3416 final int lockTaskAuth = task.mLockTaskAuth;
3417 switch (lockTaskAuth) {
3418 case LOCK_TASK_AUTH_DONT_LOCK:
3419 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003420 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003421 case LOCK_TASK_AUTH_LAUNCHABLE:
3422 case LOCK_TASK_AUTH_WHITELISTED:
3423 return false;
3424 case LOCK_TASK_AUTH_PINNABLE:
3425 // Pinnable tasks can't be launched on top of locktask tasks.
3426 return !mLockTaskModeTasks.isEmpty();
3427 default:
3428 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3429 return true;
3430 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003431 }
3432
Craig Mautner15df08a2015-04-01 12:17:18 -07003433 void onLockTaskPackagesUpdatedLocked() {
3434 boolean didSomething = false;
3435 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3436 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003437 final boolean wasWhitelisted =
3438 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3439 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003440 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003441 final boolean isWhitelisted =
3442 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3443 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3444 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003445 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003446 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3447 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003448 removeLockedTaskLocked(lockedTask);
3449 lockedTask.performClearTaskLocked();
3450 didSomething = true;
3451 }
3452 }
3453 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3454 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3455 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3456 final ActivityStack stack = stacks.get(stackNdx);
3457 stack.onLockTaskPackagesUpdatedLocked();
3458 }
3459 }
Craig Mautnere0570202015-05-13 13:06:11 -07003460 final ActivityRecord r = topRunningActivityLocked();
3461 final TaskRecord task = r != null ? r.task : null;
3462 if (mLockTaskModeTasks.isEmpty() && task != null
3463 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3464 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003465 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3466 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3467 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3468 false);
3469 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003470 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003471 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003472 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003473 }
3474 }
3475
Benjamin Franz43261142015-02-11 15:59:44 +00003476 int getLockTaskModeState() {
3477 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003478 }
3479
Jorim Jaggife89d122015-12-22 16:28:44 +01003480 void activityRelaunchedLocked(IBinder token) {
3481 mWindowManager.notifyAppRelaunchingFinished(token);
3482 }
3483
3484 void activityRelaunchingLocked(ActivityRecord r) {
3485 mWindowManager.notifyAppRelaunching(r.appToken);
3486 }
3487
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003488 void logStackState() {
3489 mActivityMetricsLogger.logWindowState();
3490 }
3491
Wale Ogunwale22e25262016-02-01 10:32:02 -08003492 void scheduleReportMultiWindowChanged(TaskRecord task) {
3493 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3494 final ActivityRecord r = task.mActivities.get(i);
3495 if (r.app != null && r.app.thread != null) {
3496 mMultiWindowModeChangedActivities.add(r);
3497 }
3498 }
3499
3500 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3501 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3502 }
3503 }
3504
3505 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3506 final ActivityStack stack = task.stack;
3507 if (prevStack == null || prevStack == stack
3508 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3509 return;
3510 }
3511
3512 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3513 final ActivityRecord r = task.mActivities.get(i);
3514 if (r.app != null && r.app.thread != null) {
3515 mPipModeChangedActivities.add(r);
3516 }
3517 }
3518
3519 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3520 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3521 }
3522 }
3523
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003524 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003525
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003526 public ActivityStackSupervisorHandler(Looper looper) {
3527 super(looper);
3528 }
3529
Craig Mautnerf3333272013-04-22 10:55:53 -07003530 void activityIdleInternal(ActivityRecord r) {
3531 synchronized (mService) {
3532 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3533 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003534 }
3535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003536 @Override
3537 public void handleMessage(Message msg) {
3538 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003539 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3540 synchronized (mService) {
3541 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3542 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3543 r.scheduleMultiWindowChanged();
3544 }
3545 }
3546 } break;
3547 case REPORT_PIP_MODE_CHANGED_MSG: {
3548 synchronized (mService) {
3549 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3550 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3551 r.schedulePictureInPictureChanged();
3552 }
3553 }
3554 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003555 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003556 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3557 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003558 if (mService.mDidDexOpt) {
3559 mService.mDidDexOpt = false;
3560 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3561 nmsg.obj = msg.obj;
3562 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3563 return;
3564 }
3565 // We don't at this point know if the activity is fullscreen,
3566 // so we need to be conservative and assume it isn't.
3567 activityIdleInternal((ActivityRecord)msg.obj);
3568 } break;
3569 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003570 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003571 activityIdleInternal((ActivityRecord)msg.obj);
3572 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003573 case RESUME_TOP_ACTIVITY_MSG: {
3574 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003575 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003576 }
3577 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003578 case SLEEP_TIMEOUT_MSG: {
3579 synchronized (mService) {
3580 if (mService.isSleepingOrShuttingDown()) {
3581 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3582 mSleepTimeout = true;
3583 checkReadyForSleepLocked();
3584 }
3585 }
3586 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003587 case LAUNCH_TIMEOUT_MSG: {
3588 if (mService.mDidDexOpt) {
3589 mService.mDidDexOpt = false;
3590 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3591 return;
3592 }
3593 synchronized (mService) {
3594 if (mLaunchingActivity.isHeld()) {
3595 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3596 if (VALIDATE_WAKE_LOCK_CALLER
3597 && Binder.getCallingUid() != Process.myUid()) {
3598 throw new IllegalStateException("Calling must be system uid");
3599 }
3600 mLaunchingActivity.release();
3601 }
3602 }
3603 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003604 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003605 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606 } break;
3607 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003608 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003609 } break;
3610 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003611 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003613 case CONTAINER_CALLBACK_VISIBILITY: {
3614 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003615 final IActivityContainerCallback callback = container.mCallback;
3616 if (callback != null) {
3617 try {
3618 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3619 } catch (RemoteException e) {
3620 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003621 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003622 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003623 case LOCK_TASK_START_MSG: {
3624 // When lock task starts, we disable the status bars.
3625 try {
Jason Monk62515be2014-05-21 16:06:19 -04003626 if (mLockTaskNotify == null) {
3627 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003628 }
Jason Monk62515be2014-05-21 16:06:19 -04003629 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003630 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003631 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003632 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003633 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003634 flags = StatusBarManager.DISABLE_MASK
3635 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003636 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003637 flags = StatusBarManager.DISABLE_MASK
3638 & (~StatusBarManager.DISABLE_BACK)
3639 & (~StatusBarManager.DISABLE_HOME)
3640 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003641 }
3642 getStatusBarService().disable(flags, mToken,
3643 mService.mContext.getPackageName());
3644 }
3645 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003646 if (getDevicePolicyManager() != null) {
3647 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003648 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003649 }
justinzhang5286d3f2014-05-12 17:06:01 -04003650 } catch (RemoteException ex) {
3651 throw new RuntimeException(ex);
3652 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003653 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003654 case LOCK_TASK_END_MSG: {
3655 // When lock task ends, we enable the status bars.
3656 try {
Jason Monk62515be2014-05-21 16:06:19 -04003657 if (getStatusBarService() != null) {
3658 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3659 mService.mContext.getPackageName());
3660 }
3661 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003662 if (getDevicePolicyManager() != null) {
3663 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3664 msg.arg1);
3665 }
Jason Monk62515be2014-05-21 16:06:19 -04003666 if (mLockTaskNotify == null) {
3667 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3668 }
3669 mLockTaskNotify.show(false);
3670 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003671 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003672 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003673 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003674 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003675 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003676 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003677 new LockPatternUtils(mService.mContext)
3678 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003679 }
3680 } catch (SettingNotFoundException e) {
3681 // No setting, don't lock.
3682 }
justinzhang5286d3f2014-05-12 17:06:01 -04003683 } catch (RemoteException ex) {
3684 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003685 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003686 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003687 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003688 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003689 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3690 if (mLockTaskNotify == null) {
3691 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3692 }
3693 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3694 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003695 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3696 final ActivityContainer container = (ActivityContainer) msg.obj;
3697 final IActivityContainerCallback callback = container.mCallback;
3698 if (callback != null) {
3699 try {
3700 callback.onAllActivitiesComplete(container.asBinder());
3701 } catch (RemoteException e) {
3702 }
3703 }
3704 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003705 case LAUNCH_TASK_BEHIND_COMPLETE: {
3706 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003707 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003708 if (r != null) {
3709 handleLaunchTaskBehindCompleteLocked(r);
3710 }
3711 }
3712 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003713
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003714 }
3715 }
3716 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003717
Ying Wangb081a592014-04-22 15:20:16 -07003718 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003719 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3720 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003722 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003724 ActivityRecord mParentActivity = null;
3725 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003726
Craig Mautnere3a00d72014-04-16 08:31:19 -07003727 boolean mVisible = true;
3728
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003730 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003732 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3733 final static int CONTAINER_STATE_NO_SURFACE = 1;
3734 final static int CONTAINER_STATE_FINISHING = 2;
3735 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3736
3737 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003738 synchronized (mService) {
3739 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003740 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003741 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003742 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003744 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003746 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003747 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003748 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003749 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003750 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003751 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 }
3753
3754 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003755 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003757 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3758 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 return;
3760 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003761 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 }
3763 }
3764
3765 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003766 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003767 synchronized (mService) {
3768 if (mActivityDisplay != null) {
3769 return mActivityDisplay.mDisplayId;
3770 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003771 }
3772 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 }
3774
Jeff Brownca9bc702014-02-11 14:32:56 -08003775 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003776 public int getStackId() {
3777 synchronized (mService) {
3778 return mStackId;
3779 }
3780 }
3781
3782 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003783 public boolean injectEvent(InputEvent event) {
3784 final long origId = Binder.clearCallingIdentity();
3785 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003786 synchronized (mService) {
3787 if (mActivityDisplay != null) {
3788 return mInputManagerInternal.injectInputEvent(event,
3789 mActivityDisplay.mDisplayId,
3790 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3791 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003792 }
3793 return false;
3794 } finally {
3795 Binder.restoreCallingIdentity(origId);
3796 }
3797 }
3798
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003799 @Override
3800 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003801 synchronized (mService) {
3802 if (mContainerState == CONTAINER_STATE_FINISHING) {
3803 return;
3804 }
3805 mContainerState = CONTAINER_STATE_FINISHING;
3806
Craig Mautnerd163e752014-06-13 17:18:47 -07003807 long origId = Binder.clearCallingIdentity();
3808 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003809 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003810 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003811 } finally {
3812 Binder.restoreCallingIdentity(origId);
3813 }
3814 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003815 }
3816
Craig Mautner60257702014-09-17 15:02:33 -07003817 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003818 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003819 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003820 if (mActivityDisplay != null) {
3821 mActivityDisplay.detachActivitiesLocked(mStack);
3822 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003823 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 }
3825 }
3826
3827 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003829 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 }
3831
3832 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003833 public final int startActivityIntentSender(IIntentSender intentSender)
3834 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003835 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3836
3837 if (!(intentSender instanceof PendingIntentRecord)) {
3838 throw new IllegalArgumentException("Bad PendingIntent object");
3839 }
3840
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003841 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003842 Binder.getCallingUid(), mCurrentUser, false,
3843 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003844
3845 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3846 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3847 pendingIntent.key.requestResolvedType);
3848
3849 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3850 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3851 }
3852
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003853 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003854 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003855 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003856 throw new SecurityException(
3857 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3858 }
3859 }
3860
Craig Mautnerdf88d732014-01-27 09:21:32 -08003861 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003862 public IBinder asBinder() {
3863 return this;
3864 }
3865
Craig Mautner4504de52013-12-20 09:06:56 -08003866 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003867 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003868 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003869 }
3870
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 ActivityStackSupervisor getOuter() {
3872 return ActivityStackSupervisor.this;
3873 }
3874
Craig Mautnerd163e752014-06-13 17:18:47 -07003875 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003876 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003877 }
3878
Craig Mautner6985bad2014-04-21 15:22:06 -07003879 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003880 void setVisible(boolean visible) {
3881 if (mVisible != visible) {
3882 mVisible = visible;
3883 if (mCallback != null) {
3884 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3885 0 /* unused */, this).sendToTarget();
3886 }
3887 }
3888 }
3889
Craig Mautner6985bad2014-04-21 15:22:06 -07003890 void setDrawn() {
3891 }
3892
Craig Mautner1b4bf852014-05-26 15:06:32 -07003893 // You can always start a new task on a regular ActivityStack.
3894 boolean isEligibleForNewTasks() {
3895 return true;
3896 }
3897
Craig Mautnerd163e752014-06-13 17:18:47 -07003898 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003899 detachLocked();
3900 deleteActivityContainer(this);
3901 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003902 }
3903
Craig Mautner34b73df2014-01-12 21:11:08 -08003904 @Override
3905 public String toString() {
3906 return mIdString + (mActivityDisplay == null ? "N" : "A");
3907 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003908 }
3909
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003910 private class VirtualActivityContainer extends ActivityContainer {
3911 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003912 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003913
3914 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3915 super(getNextStackId());
3916 mParentActivity = parent;
3917 mCallback = callback;
3918 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003919 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003920 }
3921
3922 @Override
3923 public void setSurface(Surface surface, int width, int height, int density) {
3924 super.setSurface(surface, width, height, density);
3925
3926 synchronized (mService) {
3927 final long origId = Binder.clearCallingIdentity();
3928 try {
3929 setSurfaceLocked(surface, width, height, density);
3930 } finally {
3931 Binder.restoreCallingIdentity(origId);
3932 }
3933 }
3934 }
3935
3936 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3937 if (mContainerState == CONTAINER_STATE_FINISHING) {
3938 return;
3939 }
3940 VirtualActivityDisplay virtualActivityDisplay =
3941 (VirtualActivityDisplay) mActivityDisplay;
3942 if (virtualActivityDisplay == null) {
3943 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003944 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 mActivityDisplay = virtualActivityDisplay;
3946 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003947 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003948 }
3949
3950 if (mSurface != null) {
3951 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003952 }
3953
Craig Mautner6985bad2014-04-21 15:22:06 -07003954 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003956 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003957 } else {
3958 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003959 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003960 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003961 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003962 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003963 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003964
Craig Mautnerd163e752014-06-13 17:18:47 -07003965 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003966
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003967 if (DEBUG_STACK) Slog.d(TAG_STACK,
3968 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003969 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003970
Craig Mautner6985bad2014-04-21 15:22:06 -07003971 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003972 boolean isAttachedLocked() {
3973 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003974 }
3975
3976 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003977 void setDrawn() {
3978 synchronized (mService) {
3979 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003980 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003981 }
3982 }
3983
Craig Mautner1b4bf852014-05-26 15:06:32 -07003984 // Never start a new task on an ActivityView if it isn't explicitly specified.
3985 @Override
3986 boolean isEligibleForNewTasks() {
3987 return false;
3988 }
3989
Craig Mautnerd163e752014-06-13 17:18:47 -07003990 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003991 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003992 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3993 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3994 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3995 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3996 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003997 }
3998 }
3999
Craig Mautner4a1cb222013-12-04 16:14:06 -08004000 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4001 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004002 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004004 int mDisplayId;
4005 Display mDisplay;
4006 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004007
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4009 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004010 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004011
Jose Lima4b6c6692014-08-12 17:41:12 -07004012 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004013
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004014 ActivityDisplay() {
4015 }
Craig Mautner4504de52013-12-20 09:06:56 -08004016
Craig Mautner1a70a162014-09-13 12:09:31 -07004017 // After instantiation, check that mDisplay is not null before using this. The alternative
4018 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004019 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004020 final Display display = mDisplayManager.getDisplay(displayId);
4021 if (display == null) {
4022 return;
4023 }
4024 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004025 }
4026
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004027 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004028 mDisplay = display;
4029 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004031 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004032
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004033 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004034 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004035 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4036 + " onTop=" + onTop);
4037 if (onTop) {
4038 mStacks.add(stack);
4039 } else {
4040 mStacks.add(0, stack);
4041 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 }
4043
Craig Mautnere0a38842013-12-16 16:14:02 -08004044 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004045 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004046 + " from displayId=" + mDisplayId);
4047 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 }
4049
Jose Lima4b6c6692014-08-12 17:41:12 -07004050 void setVisibleBehindActivity(ActivityRecord r) {
4051 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004052 }
4053
Jose Lima4b6c6692014-08-12 17:41:12 -07004054 boolean hasVisibleBehindActivity() {
4055 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004056 }
4057
Craig Mautner34b73df2014-01-12 21:11:08 -08004058 @Override
4059 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004060 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4061 }
4062 }
4063
4064 class VirtualActivityDisplay extends ActivityDisplay {
4065 VirtualDisplay mVirtualDisplay;
4066
Craig Mautner6985bad2014-04-21 15:22:06 -07004067 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004068 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004069 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4070 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4071 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4072 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004073
4074 init(mVirtualDisplay.getDisplay());
4075
4076 mWindowManager.handleDisplayAdded(mDisplayId);
4077 }
4078
4079 void setSurface(Surface surface) {
4080 if (mVirtualDisplay != null) {
4081 mVirtualDisplay.setSurface(surface);
4082 }
4083 }
4084
4085 @Override
4086 void detachActivitiesLocked(ActivityStack stack) {
4087 super.detachActivitiesLocked(stack);
4088 if (mVirtualDisplay != null) {
4089 mVirtualDisplay.release();
4090 mVirtualDisplay = null;
4091 }
4092 }
4093
4094 @Override
4095 public String toString() {
4096 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004097 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004098 }
Jose Lima58e66d62014-05-27 20:00:27 -07004099
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004100 /**
4101 * Adjust bounds to stay within stack bounds.
4102 *
4103 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4104 * that keep them unchanged, but be contained within the stack bounds.
4105 *
4106 * @param bounds Bounds to be adjusted.
4107 * @param stackBounds Bounds within which the other bounds should remain.
4108 */
4109 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4110 if (stackBounds == null || stackBounds.contains(bounds)) {
4111 return;
4112 }
4113
4114 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4115 final int maxRight = stackBounds.right
4116 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4117 int horizontalDiff = stackBounds.left - bounds.left;
4118 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4119 || (bounds.left + horizontalDiff >= maxRight)) {
4120 horizontalDiff = maxRight - bounds.left;
4121 }
4122 bounds.left += horizontalDiff;
4123 bounds.right += horizontalDiff;
4124 }
4125
4126 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4127 final int maxBottom = stackBounds.bottom
4128 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4129 int verticalDiff = stackBounds.top - bounds.top;
4130 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4131 || (bounds.top + verticalDiff >= maxBottom)) {
4132 verticalDiff = maxBottom - bounds.top;
4133 }
4134 bounds.top += verticalDiff;
4135 bounds.bottom += verticalDiff;
4136 }
4137 }
4138
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004139 ActivityStack findStackBehind(ActivityStack stack) {
4140 // TODO(multi-display): We are only looking for stacks on the default display.
4141 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4142 if (display == null) {
4143 return null;
4144 }
4145 final ArrayList<ActivityStack> stacks = display.mStacks;
4146 for (int i = stacks.size() - 1; i >= 0; i--) {
4147 if (stacks.get(i) == stack && i > 0) {
4148 return stacks.get(i - 1);
4149 }
4150 }
4151 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4152 + " in=" + stacks);
4153 }
Craig Mautner27084302013-03-25 08:05:25 -07004154}