blob: 1e89f2aa56872b8c8c033511ef7a91397f78e487 [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;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010055import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070056import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070057import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070060import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070062import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070063import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070064import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040065import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070067import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070068import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070069import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010070import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070071import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070072import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040073import android.provider.Settings;
74import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070075import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070077import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070078import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070079import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080082import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080084import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080085import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080086
Dianne Hackborn85d558c2014-11-04 10:31:54 -080087import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070088import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040089import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070090import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040091import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080092import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095
Craig Mautner8d341ef2013-03-26 09:03:27 -070096import java.io.FileDescriptor;
97import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070098import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070099import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700100import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800102import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700103import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700104
Jorim Jaggife89d122015-12-22 16:28:44 +0100105import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100106import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100107import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
108import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
109import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
110import static android.app.ActivityManager.RESIZE_MODE_FORCED;
111import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
112import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
113import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
114import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
115import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
116import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
117import static android.app.ActivityManager.StackId.HOME_STACK_ID;
118import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
119import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
120import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
121import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
122import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
123import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500124import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100125import static android.content.pm.PackageManager.PERMISSION_GRANTED;
126import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
151import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800153import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100154import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100155import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100156import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
157import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
158import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
160import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
161import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
162import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
163import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
164import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
165import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
166import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100172import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100173
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 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100429 * Set of tasks that are in resizing mode during an app transition to fill the "void".
430 */
431 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
432
433 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700434 * Description of a request to start a new activity, which has been held
435 * due to app switches being disabled.
436 */
437 static class PendingActivityLaunch {
438 final ActivityRecord r;
439 final ActivityRecord sourceRecord;
440 final int startFlags;
441 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700442 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700443
444 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700445 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700446 r = _r;
447 sourceRecord = _sourceRecord;
448 startFlags = _startFlags;
449 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700450 callerApp = _callerApp;
451 }
452
453 void sendErrorResult(String message) {
454 try {
455 if (callerApp.thread != null) {
456 callerApp.thread.scheduleCrash(message);
457 }
458 } catch (RemoteException e) {
459 Slog.e(TAG, "Exception scheduling crash of failed "
460 + "activity launcher sourceRecord=" + sourceRecord, e);
461 }
Craig Mautneree36c772014-07-16 14:56:05 -0700462 }
463 }
464
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800465 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700466 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700467 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800468 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800469 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700470 }
471
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800472 void setRecentTasks(RecentTasks recentTasks) {
473 mRecentTasks = recentTasks;
474 }
475
Jeff Brown2c43c332014-06-12 22:38:59 -0700476 /**
477 * At the time when the constructor runs, the power manager has not yet been
478 * initialized. So we initialize our wakelocks afterwards.
479 */
480 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700482 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700483 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700484 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700485 }
486
justinzhang5286d3f2014-05-12 17:06:01 -0400487 // This function returns a IStatusBarService. The value is from ServiceManager.
488 // getService and is cached.
489 private IStatusBarService getStatusBarService() {
490 synchronized (mService) {
491 if (mStatusBarService == null) {
492 mStatusBarService = IStatusBarService.Stub.asInterface(
493 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
494 if (mStatusBarService == null) {
495 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
496 }
497 }
498 return mStatusBarService;
499 }
500 }
501
Jason Monk35c62a42014-06-17 10:24:47 -0400502 private IDevicePolicyManager getDevicePolicyManager() {
503 synchronized (mService) {
504 if (mDevicePolicyManager == null) {
505 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
506 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
507 if (mDevicePolicyManager == null) {
508 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
509 }
510 }
511 return mDevicePolicyManager;
512 }
513 }
514
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700515 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 synchronized (mService) {
517 mWindowManager = wm;
518
519 mDisplayManager =
520 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
521 mDisplayManager.registerDisplayListener(this, null);
522
523 Display[] displays = mDisplayManager.getDisplays();
524 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
525 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800526 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700527 if (activityDisplay.mDisplay == null) {
528 throw new IllegalStateException("Default Display does not exist");
529 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800530 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700531 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 }
533
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800534 mHomeStack = mFocusedStack = mLastFocusedStack =
535 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800536
537 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 }
Craig Mautner27084302013-03-25 08:05:25 -0700539 }
540
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200541 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700542 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200543 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700544 }
545
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700546 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700548 }
549
Craig Mautnerde4ef022013-04-07 19:01:33 -0700550 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700552 }
553
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700554 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700555 if (stack == null) {
556 return false;
557 }
558
Craig Mautnerdf88d732014-01-27 09:21:32 -0800559 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
560 if (parent != null) {
561 stack = parent.task.stack;
562 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800563 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700564 }
565
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 /** The top most stack. */
567 boolean isFrontStack(ActivityStack stack) {
568 if (stack == null) {
569 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700571
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700572 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
573 if (parent != null) {
574 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800575 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700576 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
577 }
578
Wale Ogunwaled046a012015-12-24 13:05:59 -0800579 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800580 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
581 if (!focusCandidate.isFocusable()) {
582 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
583 focusCandidate = focusCandidate.getNextFocusableStackLocked();
584 }
585
586 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800587 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800588 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800589
Wale Ogunwaled046a012015-12-24 13:05:59 -0800590 EventLogTags.writeAmFocusedStack(
591 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
592 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
593 }
594
595 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800596 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800597 // The focus activity should always be the top activity in the focused stack.
598 // There will be chaos and anarchy if it isn't...
599 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
600 }
Craig Mautnerde313752015-01-22 14:28:03 -0800601
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800602 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800603 if (r != null && r.idle) {
604 checkFinishBootingLocked();
605 }
606 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700607 }
608
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700609 void moveHomeStackToFront(String reason) {
610 mHomeStack.moveToFront(reason);
611 }
612
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700613 /** Returns true if the focus activity was adjusted to the home stack top activity. */
614 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700615 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
616 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700617 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700618 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700619
Craig Mautner84984fa2014-06-19 11:19:20 -0700620 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700621
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700622 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700623 if (top == null) {
624 return false;
625 }
626 mService.setFocusedActivityLocked(top, reason);
627 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700628 }
629
Craig Mautner299f9602015-01-26 09:47:33 -0800630 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700631 if (!mService.mBooting && !mService.mBooted) {
632 // Not ready yet!
633 return false;
634 }
635
Craig Mautner84984fa2014-06-19 11:19:20 -0700636 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
637 mWindowManager.showRecentApps();
638 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700639 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700640
Craig Mautner84984fa2014-06-19 11:19:20 -0700641 if (prev != null) {
642 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
643 }
644
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700645 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
646 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800647 final String myReason = reason + " resumeHomeStackTask";
648
Mark Lua56ea122015-10-08 13:31:01 +0800649 // Only resume home activity if isn't finishing.
650 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800651 mService.setFocusedActivityLocked(r, myReason);
652 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700653 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700655 }
656
Craig Mautner8d341ef2013-03-26 09:03:27 -0700657 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700658 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700659 }
660
661 /**
662 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
663 * @param id Id of the task we would like returned.
664 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
665 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700666 * @param stackId The stack to restore the task to (default launch stack will be used if
667 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700668 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700669 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
674 ActivityStack stack = stacks.get(stackNdx);
675 TaskRecord task = stack.taskForIdLocked(id);
676 if (task != null) {
677 return task;
678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700679 }
680 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800681
682 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700683 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800684 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800685 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700686 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800687 return null;
688 }
689
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700690 if (!restoreFromRecents) {
691 return task;
692 }
693
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700694 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
696 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800697 return null;
698 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700699 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800700 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700701 }
702
Craig Mautner6170f732013-04-02 13:05:23 -0700703 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
708 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
709 if (r != null) {
710 return r;
711 }
Craig Mautner6170f732013-04-02 13:05:23 -0700712 }
713 }
714 return null;
715 }
716
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000717 boolean isFocusedUserLockedProfile() {
718 final int userId = mFocusedStack.topRunningActivityLocked().userId;
719 return userId != UserHandle.myUserId()
720 && mService.mUserController.shouldConfirmCredentials(userId);
721 }
722
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800723 void setNextTaskIdForUserLocked(int taskId, int userId) {
724 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
725 if (taskId > currentTaskId) {
726 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700727 }
728 }
729
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800730 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800731 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
732 // for a userId u, a taskId can only be in the range
733 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
734 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
735 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800736 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
737 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
738 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800739 candidateTaskId++;
740 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
741 // Wrap around as there will be smaller task ids that are available now.
742 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700743 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800744 if (candidateTaskId == currentTaskId) {
745 // Something wrong!
746 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
747 throw new IllegalStateException("Cannot get an available task id."
748 + " Reached limit of " + MAX_TASK_IDS_PER_USER
749 + " running tasks per user.");
750 }
751 }
752 mCurTaskIdForUser.put(userId, candidateTaskId);
753 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700754 }
755
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800757 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700758 if (stack == null) {
759 return null;
760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700761 ActivityRecord resumedActivity = stack.mResumedActivity;
762 if (resumedActivity == null || resumedActivity.app == null) {
763 resumedActivity = stack.mPausingActivity;
764 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700765 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700766 }
767 }
768 return resumedActivity;
769 }
770
Dianne Hackbornff072722014-09-24 10:56:28 -0700771 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700772 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800774 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
775 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
777 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700778 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 continue;
780 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700781 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 if (hr != null) {
783 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
784 && processName.equals(hr.processName)) {
785 try {
George Mount2c92c972014-03-20 09:38:23 -0700786 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 didSomething = true;
788 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700789 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 Slog.w(TAG, "Exception in new application when starting activity "
791 + hr.intent.getComponent().flattenToShortString(), e);
792 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700793 }
Craig Mautner20e72272013-04-01 13:45:53 -0700794 }
Craig Mautner20e72272013-04-01 13:45:53 -0700795 }
796 }
797 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700798 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700799 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700800 }
Craig Mautner20e72272013-04-01 13:45:53 -0700801 return didSomething;
802 }
803
804 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800805 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
806 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
808 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700809 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 continue;
811 }
812 final ActivityRecord resumedActivity = stack.mResumedActivity;
813 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700814 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800815 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 return false;
817 }
Craig Mautner20e72272013-04-01 13:45:53 -0700818 }
819 }
820 return true;
821 }
822
Craig Mautnerde4ef022013-04-07 19:01:33 -0700823 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
827 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700828 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700830 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 return false;
832 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700833 }
834 }
835 }
836 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700837 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
839 mLastFocusedStack + " to=" + mFocusedStack);
840 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700841 return true;
842 }
843
844 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800845 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
847 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
849 final ActivityStack stack = stacks.get(stackNdx);
850 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800851 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700852 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800853 return false;
854 }
855 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700857 }
858 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800859 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700860 }
861
Craig Mautner2acc3892013-09-23 10:28:14 -0700862 /**
863 * Pause all activities in either all of the stacks or just the back stacks.
864 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700865 * @return true if any activity was paused as a result of this call.
866 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700867 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700868 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800869 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
870 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
872 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700873 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700874 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700876 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
877 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700879 }
880 }
881 return someActivityPaused;
882 }
883
Craig Mautnerde4ef022013-04-07 19:01:33 -0700884 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700885 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
887 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
889 final ActivityStack stack = stacks.get(stackNdx);
890 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700891 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700893 Slog.d(TAG_STATES,
894 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800895 pausing = false;
896 } else {
897 return false;
898 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700899 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700900 }
901 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700902 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700903 }
904
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700905 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
906 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500907 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800908 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
909 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
911 final ActivityStack stack = stacks.get(stackNdx);
912 if (stack.mResumedActivity != null &&
913 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700914 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800915 }
916 }
917 }
918 }
919
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800920 void cancelInitializingActivities() {
921 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
922 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
923 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
924 stacks.get(stackNdx).cancelInitializingActivities();
925 }
926 }
927 }
928
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700929 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700930 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700931 }
932
933 void sendWaitingVisibleReportLocked(ActivityRecord r) {
934 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700935 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700937 if (w.who == null) {
938 changed = true;
939 w.timeout = false;
940 if (r != null) {
941 w.who = new ComponentName(r.info.packageName, r.info.name);
942 }
943 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
944 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700945 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700947 if (changed) {
948 mService.notifyAll();
949 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700950 }
951
952 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
953 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700954 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700955 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700956 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700957 if (w.who == null) {
958 changed = true;
959 w.timeout = timeout;
960 if (r != null) {
961 w.who = new ComponentName(r.info.packageName, r.info.name);
962 }
963 w.thisTime = thisTime;
964 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700965 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700967 if (changed) {
968 mService.notifyAll();
969 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700970 }
971
Craig Mautner29219d92013-04-16 20:19:12 -0700972 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800973 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700974 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700975 if (r != null) {
976 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700977 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700978
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
982 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800983 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700984 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700985 if (r != null) {
986 return r;
987 }
988 }
989 }
990 return null;
991 }
992
Dianne Hackborn09233282014-04-30 11:33:59 -0700993 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700994 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
996 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800997 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800998 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1001 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001002 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001003 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001004 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001005 }
1006 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001007
1008 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1009 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1010 while (maxNum > 0) {
1011 long mostRecentActiveTime = Long.MIN_VALUE;
1012 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001013 final int numTaskLists = runningTaskLists.size();
1014 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1015 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001016 if (!stackTaskList.isEmpty()) {
1017 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1018 if (lastActiveTime > mostRecentActiveTime) {
1019 mostRecentActiveTime = lastActiveTime;
1020 selectedStackList = stackTaskList;
1021 }
1022 }
1023 }
1024 if (selectedStackList != null) {
1025 list.add(selectedStackList.remove(0));
1026 --maxNum;
1027 } else {
1028 break;
1029 }
1030 }
Craig Mautner20e72272013-04-01 13:45:53 -07001031 }
1032
Todd Kennedy7440f172015-12-09 14:31:22 -08001033 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1034 ProfilerInfo profilerInfo) {
1035 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001036 if (aInfo != null) {
1037 // Store the found target back into the intent, because now that
1038 // we have it we never want to do this again. For example, if the
1039 // user navigates back to this point in the history, we should
1040 // always restart the exact same activity.
1041 intent.setComponent(new ComponentName(
1042 aInfo.applicationInfo.packageName, aInfo.name));
1043
1044 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001045 if (!aInfo.processName.equals("system")) {
1046 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047 mService.setDebugApp(aInfo.processName, true, false);
1048 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001050 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1051 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1052 }
1053
Man Caocfa78b22015-06-11 20:14:34 -07001054 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1055 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1056 }
1057
1058 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001059 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001060 }
1061 }
1062 }
1063 return aInfo;
1064 }
1065
Todd Kennedy7440f172015-12-09 14:31:22 -08001066 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001067 return resolveIntent(intent, resolvedType, userId, 0);
1068 }
1069
1070 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001071 try {
1072 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001073 PackageManager.MATCH_DEFAULT_ONLY | flags
1074 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001075 } catch (RemoteException e) {
1076 }
1077 return null;
1078 }
1079
1080 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1081 ProfilerInfo profilerInfo, int userId) {
1082 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1083 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1084 }
1085
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001086 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1087 boolean andResume, boolean checkConfig) throws RemoteException {
1088
1089 if (!allPausedActivitiesComplete()) {
1090 // While there are activities pausing we skipping starting any new activities until
1091 // pauses are complete. NOTE: that we also do this for activities that are starting in
1092 // the paused state because they will first be resumed then paused on the client side.
1093 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1094 "realStartActivityLocked: Skipping start of r=" + r
1095 + " some activities pausing...");
1096 return false;
1097 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001098
Craig Mautner2568c3a2015-03-26 14:22:34 -07001099 if (andResume) {
1100 r.startFreezingScreenLocked(app, 0);
1101 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001102
Craig Mautner2568c3a2015-03-26 14:22:34 -07001103 // schedule launch ticks to collect information about slow apps.
1104 r.startLaunchTickingLocked();
1105 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001106
1107 // Have the window manager re-evaluate the orientation of
1108 // the screen based on the new activity order. Note that
1109 // as a result of this, it can call back into the activity
1110 // manager with a new orientation. We don't care about that,
1111 // because the activity is not currently running so we are
1112 // just restarting it anyway.
1113 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001115 mService.mConfiguration,
1116 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001117 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001118 }
1119
1120 r.app = app;
1121 app.waitingToKill = null;
1122 r.launchCount++;
1123 r.lastLaunchTime = SystemClock.uptimeMillis();
1124
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001125 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001126
1127 int idx = app.activities.indexOf(r);
1128 if (idx < 0) {
1129 app.activities.add(r);
1130 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001131 mService.updateLruProcessLocked(app, true, null);
1132 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001133
Craig Mautner15df08a2015-04-01 12:17:18 -07001134 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001135 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1136 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001137 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001138 }
1139
1140 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001141 try {
1142 if (app.thread == null) {
1143 throw new RemoteException();
1144 }
1145 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001146 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 if (andResume) {
1148 results = r.results;
1149 newIntents = r.newIntents;
1150 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001151 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1152 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1153 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 if (andResume) {
1155 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1156 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001157 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001159 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001160 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001161 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001162 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001163 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 r.sleeping = false;
1165 r.forceNewConfig = false;
1166 mService.showAskCompatModeDialogLocked(r);
1167 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001168 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001169 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1170 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1171 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001172 final String profileFile = mService.mProfileFile;
1173 if (profileFile != null) {
1174 ParcelFileDescriptor profileFd = mService.mProfileFd;
1175 if (profileFd != null) {
1176 try {
1177 profileFd = profileFd.dup();
1178 } catch (IOException e) {
1179 if (profileFd != null) {
1180 try {
1181 profileFd.close();
1182 } catch (IOException o) {
1183 }
1184 profileFd = null;
1185 }
1186 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001188
1189 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1190 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 }
1192 }
1193 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001194
Craig Mautner2568c3a2015-03-26 14:22:34 -07001195 if (andResume) {
1196 app.hasShownUi = true;
1197 app.pendingUiClean = true;
1198 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001199 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001201 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001202 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001203 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001204 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001205
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001206 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 // This may be a heavy-weight process! Note that the package
1208 // manager will ensure that only activity can run in the main
1209 // process of the .apk, which is the only thing that will be
1210 // considered heavy-weight.
1211 if (app.processName.equals(app.info.packageName)) {
1212 if (mService.mHeavyWeightProcess != null
1213 && mService.mHeavyWeightProcess != app) {
1214 Slog.w(TAG, "Starting new heavy weight process " + app
1215 + " when already running "
1216 + mService.mHeavyWeightProcess);
1217 }
1218 mService.mHeavyWeightProcess = app;
1219 Message msg = mService.mHandler.obtainMessage(
1220 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1221 msg.obj = r;
1222 mService.mHandler.sendMessage(msg);
1223 }
1224 }
1225
1226 } catch (RemoteException e) {
1227 if (r.launchFailed) {
1228 // This is the second time we failed -- finish activity
1229 // and give up.
1230 Slog.e(TAG, "Second failure launching "
1231 + r.intent.getComponent().flattenToShortString()
1232 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001233 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1235 "2nd-crash", false);
1236 return false;
1237 }
1238
1239 // This is the first time we failed -- restart process and
1240 // retry.
1241 app.activities.remove(r);
1242 throw e;
1243 }
1244
1245 r.launchFailed = false;
1246 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001247 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 }
1249
1250 if (andResume) {
1251 // As part of the process of launching, ActivityThread also performs
1252 // a resume.
1253 stack.minimalResumeActivityLocked(r);
1254 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001255 // This activity is not starting in the resumed state... which should look like we asked
1256 // it to pause+stop (but remain visible), and it has done so and reported back the
1257 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001258 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001259 "Moving to PAUSED: " + r + " (starting in paused state)");
1260 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001261 }
1262
1263 // Launch the new version setup screen if needed. We do this -after-
1264 // launching the initial activity (that is, home), so that it can have
1265 // a chance to initialize itself while in the background, making the
1266 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001267 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 mService.startSetupActivityLocked();
1269 }
1270
Dianne Hackborn465fa392014-09-14 14:21:18 -07001271 // Update any services we are bound to that might care about whether
1272 // their client may have activities.
1273 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1274
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 return true;
1276 }
1277
Craig Mautnere79d42682013-04-01 19:01:53 -07001278 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001279 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001280 // Is this activity's application already running?
1281 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001282 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001283
1284 r.task.stack.setLaunchTime(r);
1285
1286 if (app != null && app.thread != null) {
1287 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001288 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1289 || !"android".equals(r.info.packageName)) {
1290 // Don't add this if it is a platform component that is marked
1291 // to run in multiple processes, because this is actually
1292 // part of the framework so doesn't make sense to track as a
1293 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001294 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1295 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001296 }
George Mount2c92c972014-03-20 09:38:23 -07001297 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001298 return;
1299 } catch (RemoteException e) {
1300 Slog.w(TAG, "Exception when starting activity "
1301 + r.intent.getComponent().flattenToShortString(), e);
1302 }
1303
1304 // If a dead object exception was thrown -- fall through to
1305 // restart the application.
1306 }
1307
1308 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001309 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001310 }
1311
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001312 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001313 String resultWho, int requestCode, int callingPid, int callingUid,
1314 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001315 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001316 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1317 callingUid);
1318 if (startAnyPerm == PERMISSION_GRANTED) {
1319 return true;
1320 }
1321 final int componentRestriction = getComponentRestrictionForCallingPackage(
1322 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1323 final int actionRestriction = getActionRestrictionForCallingPackage(
1324 intent.getAction(), callingPackage, callingPid, callingUid);
1325 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1326 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1327 if (resultRecord != null) {
1328 resultStack.sendActivityResultLocked(-1,
1329 resultRecord, resultWho, requestCode,
1330 Activity.RESULT_CANCELED, null);
1331 }
1332 final String msg;
1333 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1334 msg = "Permission Denial: starting " + intent.toString()
1335 + " from " + callerApp + " (pid=" + callingPid
1336 + ", uid=" + callingUid + ")" + " with revoked permission "
1337 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1338 } else if (!aInfo.exported) {
1339 msg = "Permission Denial: starting " + intent.toString()
1340 + " from " + callerApp + " (pid=" + callingPid
1341 + ", uid=" + callingUid + ")"
1342 + " not exported from uid " + aInfo.applicationInfo.uid;
1343 } else {
1344 msg = "Permission Denial: starting " + intent.toString()
1345 + " from " + callerApp + " (pid=" + callingPid
1346 + ", uid=" + callingUid + ")"
1347 + " requires " + aInfo.permission;
1348 }
1349 Slog.w(TAG, msg);
1350 throw new SecurityException(msg);
1351 }
1352
1353 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1354 final String message = "Appop Denial: starting " + intent.toString()
1355 + " from " + callerApp + " (pid=" + callingPid
1356 + ", uid=" + callingUid + ")"
1357 + " requires " + AppOpsManager.permissionToOp(
1358 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1359 Slog.w(TAG, message);
1360 return false;
1361 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1362 final String message = "Appop Denial: starting " + intent.toString()
1363 + " from " + callerApp + " (pid=" + callingPid
1364 + ", uid=" + callingUid + ")"
1365 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1366 Slog.w(TAG, message);
1367 return false;
1368 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001369 if (options != null && options.getLaunchTaskId() != -1) {
1370 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1371 callingPid, callingUid);
1372 if (startInTaskPerm != PERMISSION_GRANTED) {
1373 final String msg = "Permission Denial: starting " + intent.toString()
1374 + " from " + callerApp + " (pid=" + callingPid
1375 + ", uid=" + callingUid + ") with launchTaskId="
1376 + options.getLaunchTaskId();
1377 Slog.w(TAG, msg);
1378 throw new SecurityException(msg);
1379 }
1380 }
1381
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001382 return true;
1383 }
1384
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001385 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001386 final long identity = Binder.clearCallingIdentity();
1387 try {
1388 return UserManager.get(mService.mContext).getUserInfo(userId);
1389 } finally {
1390 Binder.restoreCallingIdentity(identity);
1391 }
1392 }
1393
Svet Ganov99b60432015-06-27 13:15:22 -07001394 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001395 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001396 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1397 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001398 == PackageManager.PERMISSION_DENIED) {
1399 return ACTIVITY_RESTRICTION_PERMISSION;
1400 }
1401
Christopher Tateff7add02015-08-17 10:23:22 -07001402 if (activityInfo.permission == null) {
1403 return ACTIVITY_RESTRICTION_NONE;
1404 }
1405
Svet Ganov99b60432015-06-27 13:15:22 -07001406 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1407 if (opCode == AppOpsManager.OP_NONE) {
1408 return ACTIVITY_RESTRICTION_NONE;
1409 }
1410
1411 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1412 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001413 if (!ignoreTargetSecurity) {
1414 return ACTIVITY_RESTRICTION_APPOP;
1415 }
Svet Ganov99b60432015-06-27 13:15:22 -07001416 }
1417
1418 return ACTIVITY_RESTRICTION_NONE;
1419 }
1420
Svetoslav7008b512015-06-24 18:47:07 -07001421 private int getActionRestrictionForCallingPackage(String action,
1422 String callingPackage, int callingPid, int callingUid) {
1423 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001424 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001425 }
1426
1427 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1428 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001429 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001430 }
1431
1432 final PackageInfo packageInfo;
1433 try {
1434 packageInfo = mService.mContext.getPackageManager()
1435 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1436 } catch (PackageManager.NameNotFoundException e) {
1437 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001438 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001439 }
1440
1441 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001442 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001443 }
1444
1445 if (mService.checkPermission(permission, callingPid, callingUid) ==
1446 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001447 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001448 }
1449
1450 final int opCode = AppOpsManager.permissionToOpCode(permission);
1451 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001452 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001453 }
1454
1455 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1456 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001457 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001458 }
1459
Svet Ganov99b60432015-06-27 13:15:22 -07001460 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001461 }
1462
Wale Ogunwaled046a012015-12-24 13:05:59 -08001463 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001464 if (r == null) {
1465 // Not sure what you are trying to do, but it is not going to work...
1466 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001467 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001468 final TaskRecord task = r.task;
1469 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001470 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001471 return false;
1472 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001473 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001474 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001475 }
1476
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001477 void setLaunchSource(int uid) {
1478 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1479 }
1480
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001481 void acquireLaunchWakelock() {
1482 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1483 throw new IllegalStateException("Calling must be system uid");
1484 }
1485 mLaunchingActivity.acquire();
1486 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1487 // To be safe, don't allow the wake lock to be held for too long.
1488 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1489 }
1490 }
1491
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001492 /**
1493 * Called when the frontmost task is idle.
1494 * @return the state of mService.mBooting before this was called.
1495 */
1496 private boolean checkFinishBootingLocked() {
1497 final boolean booting = mService.mBooting;
1498 boolean enableScreen = false;
1499 mService.mBooting = false;
1500 if (!mService.mBooted) {
1501 mService.mBooted = true;
1502 enableScreen = true;
1503 }
1504 if (booting || enableScreen) {
1505 mService.postFinishBooting(booting, enableScreen);
1506 }
1507 return booting;
1508 }
1509
Craig Mautnerf3333272013-04-22 10:55:53 -07001510 // Checked.
1511 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1512 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001513 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001514
Craig Mautnerf3333272013-04-22 10:55:53 -07001515 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001516 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001517 int NS = 0;
1518 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001519 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 boolean activityRemoved = false;
1521
Wale Ogunwale7d701172015-03-11 15:36:30 -07001522 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001523 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001524 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1525 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001526 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1527 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001528 if (fromTimeout) {
1529 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001530 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001531
1532 // This is a hack to semi-deal with a race condition
1533 // in the client where it can be constructed with a
1534 // newer configuration from when we asked it to launch.
1535 // We'll update with whatever configuration it now says
1536 // it used to launch.
1537 if (config != null) {
1538 r.configuration = config;
1539 }
1540
1541 // We are now idle. If someone is waiting for a thumbnail from
1542 // us, we can now deliver.
1543 r.idle = true;
1544
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001545 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001546 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001547 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001548 }
1549 }
1550
1551 if (allResumedActivitiesIdle()) {
1552 if (r != null) {
1553 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001554 }
1555
1556 if (mLaunchingActivity.isHeld()) {
1557 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1558 if (VALIDATE_WAKE_LOCK_CALLER &&
1559 Binder.getCallingUid() != Process.myUid()) {
1560 throw new IllegalStateException("Calling must be system uid");
1561 }
1562 mLaunchingActivity.release();
1563 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001564 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001565 }
1566
1567 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001568 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001569 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001570 if ((NF = mFinishingActivities.size()) > 0) {
1571 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001572 mFinishingActivities.clear();
1573 }
1574
Craig Mautnerf3333272013-04-22 10:55:53 -07001575 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001576 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001577 mStartingUsers.clear();
1578 }
1579
Craig Mautnerf3333272013-04-22 10:55:53 -07001580 // Stop any activities that are scheduled to do so but have been
1581 // waiting for the next one to start.
1582 for (int i = 0; i < NS; i++) {
1583 r = stops.get(i);
1584 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001585 if (stack != null) {
1586 if (r.finishing) {
1587 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1588 } else {
1589 stack.stopActivityLocked(r);
1590 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001591 }
1592 }
1593
1594 // Finish any activities that are scheduled to do so but have been
1595 // waiting for the next one to start.
1596 for (int i = 0; i < NF; i++) {
1597 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001598 final ActivityStack stack = r.task.stack;
1599 if (stack != null) {
1600 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1601 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001602 }
1603
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001604 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001605 // Complete user switch
1606 if (startingUsers != null) {
1607 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001608 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001609 }
1610 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001611 }
1612
1613 mService.trimApplications();
1614 //dump();
1615 //mWindowManager.dump();
1616
Craig Mautnerf3333272013-04-22 10:55:53 -07001617 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001618 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001619 }
1620
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001621 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001622 }
1623
Craig Mautner8e569572013-10-11 17:36:59 -07001624 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001625 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001626 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1627 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1629 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1630 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001631 }
Craig Mautner19091252013-10-05 00:03:53 -07001632 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001633 }
1634
1635 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001636 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1637 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001638 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1639 stacks.get(stackNdx).closeSystemDialogsLocked();
1640 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001641 }
1642 }
1643
Craig Mautner93529a42013-10-04 15:03:13 -07001644 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001645 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001646 }
1647
Craig Mautner8d341ef2013-03-26 09:03:27 -07001648 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001649 * Update the last used stack id for non-current user (current user's last
1650 * used stack is the focused stack)
1651 */
1652 void updateUserStackLocked(int userId, ActivityStack stack) {
1653 if (userId != mCurrentUser) {
1654 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1655 }
1656 }
1657
1658 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001659 * @return true if some activity was finished (or would have finished if doit were true).
1660 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001661 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1662 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001663 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001664 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1665 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001667 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001668 if (stack.finishDisabledPackageActivitiesLocked(
1669 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001670 didSomething = true;
1671 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001672 }
1673 }
1674 return didSomething;
1675 }
1676
Dianne Hackborna413dc02013-07-12 12:02:55 -07001677 void updatePreviousProcessLocked(ActivityRecord r) {
1678 // Now that this process has stopped, we may want to consider
1679 // it to be the previous app to try to keep around in case
1680 // the user wants to return to it.
1681
1682 // First, found out what is currently the foreground app, so that
1683 // we don't blow away the previous app if this activity is being
1684 // hosted by the process that is actually still the foreground.
1685 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1687 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1689 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001690 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001691 if (stack.mResumedActivity != null) {
1692 fgApp = stack.mResumedActivity.app;
1693 } else if (stack.mPausingActivity != null) {
1694 fgApp = stack.mPausingActivity.app;
1695 }
1696 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001697 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001698 }
1699 }
1700
1701 // Now set this one as the previous process, only if that really
1702 // makes sense to.
1703 if (r.app != null && fgApp != null && r.app != fgApp
1704 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001705 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001706 mService.mPreviousProcess = r.app;
1707 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1708 }
1709 }
1710
Wale Ogunwaled046a012015-12-24 13:05:59 -08001711 boolean resumeFocusedStackTopActivityLocked() {
1712 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001713 }
1714
Wale Ogunwaled046a012015-12-24 13:05:59 -08001715 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001716 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001717 if (targetStack != null && isFocusedStack(targetStack)) {
1718 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001719 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001720 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1721 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001722 }
1723
Todd Kennedy39bfee52016-02-24 10:28:21 -08001724 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1728 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1729 }
1730 }
1731 }
1732
Adrian Roos20d7df32016-01-12 18:59:43 +01001733 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1734 TaskRecord finishedTask = null;
1735 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1737 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001738 final int numStacks = stacks.size();
1739 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1740 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001741 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1742 if (stack == focusedStack || finishedTask == null) {
1743 finishedTask = t;
1744 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001745 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001746 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001747 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001748 }
1749
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001750 void finishVoiceTask(IVoiceInteractionSession session) {
1751 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1752 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1753 final int numStacks = stacks.size();
1754 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1755 final ActivityStack stack = stacks.get(stackNdx);
1756 stack.finishVoiceTask(session);
1757 }
1758 }
1759 }
1760
Chong Zhang280d3322015-11-03 17:27:26 -08001761 void findTaskToMoveToFrontLocked(
1762 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001763 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1764 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001765 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001766 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1767 // Caller wants the home activity moved with it. To accomplish this,
1768 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001769 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001770 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001771 if (task.stack == null) {
1772 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1773 + task + " to front. Stack is null");
1774 return;
1775 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001776
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001777 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001778 int stackId = options.getLaunchStackId();
1779 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001780 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001781 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001782 if (stackId == INVALID_STACK_ID) {
1783 stackId = task.getLaunchStackId();
1784 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001785 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001786 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1787 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1788 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001789 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1790 // still need moveTaskToFrontLocked() below for any transition settings.
1791 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001792 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1793 resizeStackLocked(stackId, bounds,
1794 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1795 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1796 } else {
1797 // WM resizeTask must be done after the task is moved to the correct stack,
1798 // because Task's setBounds() also updates dim layer's bounds, but that has
1799 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001800 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001801 false /* relayout */, false /* forced */);
1802 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001803 }
1804 }
1805
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001806 final ActivityRecord r = task.getTopActivity();
1807 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1808 r == null ? null : r.appTimeTracker, reason);
1809
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001810 if (DEBUG_STACK) Slog.d(TAG_STACK,
1811 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001812
1813 showNonResizeableDockToastIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001814 }
1815
Wale Ogunwale854809c2015-12-27 16:18:19 -08001816 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001817 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001818 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001819 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001820 return false;
1821 }
1822 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1823 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001824 }
1825
Craig Mautner967212c2013-04-13 21:10:58 -07001826 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001827 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001828 }
1829
1830 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001831 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1832 if (activityContainer != null) {
1833 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001834 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001835 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001836 return null;
1837 }
1838 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001839 }
1840
Craig Mautner967212c2013-04-13 21:10:58 -07001841 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001842 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001843 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1844 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001845 }
1846 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001847 }
1848
Craig Mautner4a1cb222013-12-04 16:14:06 -08001849 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001850 ActivityRecord homeActivity = getHomeActivity();
1851 if (homeActivity != null) {
1852 return homeActivity.appToken;
1853 }
1854 return null;
1855 }
1856
1857 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001858 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001859 }
1860
1861 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001862 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1863 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1864 final TaskRecord task = tasks.get(taskNdx);
1865 if (task.isHomeTask()) {
1866 final ArrayList<ActivityRecord> activities = task.mActivities;
1867 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1868 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001869 if (r.isHomeActivity()
1870 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001871 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001872 }
1873 }
1874 }
1875 }
1876 return null;
1877 }
1878
Chong Zhangb15758a2015-11-17 12:12:03 -08001879 /**
1880 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1881 * the docked stack itself, or if it's side-by-side to the docked stack.
1882 */
1883 boolean isStackDockedInEffect(int stackId) {
1884 return stackId == DOCKED_STACK_ID ||
1885 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1886 }
1887
Todd Kennedyca4d8422015-01-15 15:19:22 -08001888 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001889 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001890 ActivityContainer activityContainer =
1891 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001892 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001893 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1894 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001895 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001896 return activityContainer;
1897 }
1898
Craig Mautner34b73df2014-01-12 21:11:08 -08001899 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001900 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1901 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1902 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001903 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1904 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001905 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001906 }
1907 }
1908
Craig Mautner95da1082014-02-24 17:54:35 -08001909 void deleteActivityContainer(IActivityContainer container) {
1910 ActivityContainer activityContainer = (ActivityContainer)container;
1911 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001912 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1913 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001914 final int stackId = activityContainer.mStackId;
1915 mActivityContainers.remove(stackId);
1916 mWindowManager.removeStack(stackId);
1917 }
1918 }
1919
Jorim Jaggidc249c42015-12-15 14:57:31 -08001920 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1921 boolean preserveWindows, boolean allowResizeInDockedMode) {
1922 if (stackId == DOCKED_STACK_ID) {
1923 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1924 preserveWindows);
1925 return;
1926 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001927 final ActivityStack stack = getStack(stackId);
1928 if (stack == null) {
1929 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1930 return;
1931 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001932
Jorim Jaggidc249c42015-12-15 14:57:31 -08001933 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001934 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1935 // stack size changing so things don't get out of sync.
1936 return;
1937 }
1938
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001939 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001940 mWindowManager.deferSurfaceLayout();
1941 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001942 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1943 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001944 } finally {
1945 mWindowManager.continueSurfaceLayout();
1946 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001947 }
1948 }
1949
Jorim Jaggi192086e2016-03-11 17:17:03 +01001950 void deferUpdateBounds(int stackId) {
1951 final ActivityStack stack = getStack(stackId);
1952 if (stack != null) {
1953 stack.deferUpdateBounds();
1954 }
1955 }
1956
1957 void continueUpdateBounds(int stackId) {
1958 final ActivityStack stack = getStack(stackId);
1959 if (stack != null) {
1960 stack.continueUpdateBounds();
1961 }
1962 }
1963
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001964 void notifyAppTransitionDone() {
1965 continueUpdateBounds(HOME_STACK_ID);
1966 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1967 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
1968 if (anyTaskForIdLocked(taskId) != null) {
1969 mWindowManager.setTaskDockedResizing(taskId, false);
1970 }
1971 }
1972 mResizingTasksDuringAnimation.clear();
1973 }
1974
Jorim Jaggi192086e2016-03-11 17:17:03 +01001975 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001976 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001977 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001978
Jorim Jaggi192086e2016-03-11 17:17:03 +01001979 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1980 return;
1981 }
1982
Jorim Jaggidc249c42015-12-15 14:57:31 -08001983 mTmpBounds.clear();
1984 mTmpConfigs.clear();
1985 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001986 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001987 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001988 final TaskRecord task = tasks.get(i);
1989 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001990 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1991 // For freeform stack we don't adjust the size of the tasks to match that
1992 // of the stack, but we do try to make sure the tasks are still contained
1993 // with the bounds of the stack.
1994 tempRect2.set(task.mBounds);
1995 fitWithinBounds(tempRect2, bounds);
1996 task.updateOverrideConfiguration(tempRect2);
1997 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001998 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001999 tempTaskBounds != null ? tempTaskBounds : bounds,
2000 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002001 }
2002 }
2003
2004 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2005 mTmpBounds.put(task.taskId, task.mBounds);
2006 if (tempTaskInsetBounds != null) {
2007 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2008 }
2009 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002010
2011 // We might trigger a configuration change. Save the current task bounds for freezing.
2012 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002013 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2014 mTmpBounds, mTmpInsetBounds);
2015 stack.setBounds(bounds);
2016 }
2017
2018 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2019 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002020 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002021 return;
2022 }
2023 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2024 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002025 if (!updated) {
2026 resumeFocusedStackTopActivityLocked();
2027 }
2028 }
2029
2030 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2031 Rect tempDockedTaskInsetBounds,
2032 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2033 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2034 if (stack == null) {
2035 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2036 return;
2037 }
2038
2039 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2040 mWindowManager.deferSurfaceLayout();
2041 try {
2042 ActivityRecord r = stack.topRunningActivityLocked();
2043 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2044 tempDockedTaskInsetBounds);
2045
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002046 // TODO: Checking for isAttached might not be needed as if the user passes in null
2047 // dockedBounds then they want the docked stack to be dismissed.
2048 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2049 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002050 // In this case we make all other static stacks fullscreen and move all
2051 // docked stack tasks to the fullscreen stack.
2052 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2053 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2054 resizeStackLocked(i, null, null, null, preserveWindows,
2055 true /* allowResizeInDockedMode */);
2056 }
2057 }
2058
2059 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2060 final int count = tasks.size();
2061 for (int i = 0; i < count; i++) {
2062 moveTaskToStackLocked(tasks.get(i).taskId,
2063 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2064 false /* animate */);
2065 }
2066
2067 // stack shouldn't contain anymore activities, so nothing to resume.
2068 r = null;
2069 } else {
2070 // Docked stacks occupy a dedicated region on screen so the size of all other
2071 // static stacks need to be adjusted so they don't overlap with the docked stack.
2072 // We get the bounds to use from window manager which has been adjusted for any
2073 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002074 mWindowManager.getStackDockedModeBounds(
2075 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002076 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002077 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2078 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2079 tempOtherTaskInsetBounds, preserveWindows,
2080 true /* allowResizeInDockedMode */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002081 }
2082 }
2083 }
2084 ensureConfigurationAndResume(stack, r, preserveWindows);
2085 } finally {
2086 mWindowManager.continueSurfaceLayout();
2087 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2088 }
2089
2090 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2091 tempDockedTaskBounds != null
2092 || tempDockedTaskInsetBounds != null
2093 || tempOtherTaskBounds != null
2094 || tempOtherTaskInsetBounds != null);
2095 }
2096
Robert Carr0d00c2e2016-02-29 17:45:02 -08002097 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2098 final ActivityStack stack = getStack(PINNED_STACK_ID);
2099 if (stack == null) {
2100 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2101 return;
2102 }
2103 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2104 mWindowManager.deferSurfaceLayout();
2105 try {
2106 ActivityRecord r = stack.topRunningActivityLocked();
2107 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2108 null);
2109 ensureConfigurationAndResume(stack, r, false);
2110 } finally {
2111 mWindowManager.continueSurfaceLayout();
2112 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2113 }
2114 }
2115
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002116 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2117 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002118 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002119 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002120 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002121 }
2122
Chong Zhang87b21722015-09-21 15:39:51 -07002123 // If this is a forced resize, let it go through even if the bounds is not changing,
2124 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002125 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002126 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002127 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002128 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002129 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002130 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002131
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002132 if (!mWindowManager.isValidTaskId(task.taskId)) {
2133 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002134 // All we can do for now is update the bounds so it can be used when the task is
2135 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002136 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002137 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2138 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002139 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002140 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002141 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002142 }
2143
Chong Zhang6de2ae82015-09-30 18:25:21 -07002144 // Do not move the task to another stack here.
2145 // This method assumes that the task is already placed in the right stack.
2146 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002147
Chong Zhang6de2ae82015-09-30 18:25:21 -07002148 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002149
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002150 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002151 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002152 // way and the activity was kept the way it was. If it's false, it means the activity had
2153 // to be relaunched due to configuration change.
2154 boolean kept = true;
2155 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002156 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002157 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002158 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002159 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002160
2161 if (!deferResume) {
2162
2163 // All other activities must be made visible with their correct configuration.
2164 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2165 if (!kept) {
2166 resumeFocusedStackTopActivityLocked();
2167 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002168 }
2169 }
2170 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002171 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002172
2173 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002174 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002175 }
2176
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002177 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002178 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2179 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002180 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002181 }
2182
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002183 ActivityContainer activityContainer = new ActivityContainer(stackId);
2184 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002185 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002186 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002187 }
2188
2189 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002190 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002191 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2192 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002193 break;
2194 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002195 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002196 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002197 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002198 }
2199
Chong Zhang5dcb2752015-08-18 13:50:26 -07002200 /**
2201 * Restores a recent task to a stack
2202 * @param task The recent task to be restored.
2203 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002204 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002205 * @return true if the task has been restored successfully.
2206 */
2207 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2208 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002209 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002210 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2211 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2212 // Put it in the fullscreen stack.
2213 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002214 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002215
Wale Ogunwale706ed792015-08-02 10:29:44 -07002216 if (task.stack != null) {
2217 // Task has already been restored once. See if we need to do anything more
2218 if (task.stack.mStackId == stackId) {
2219 // Nothing else to do since it is already restored in the right stack.
2220 return true;
2221 }
2222 // Remove current stack association, so we can re-associate the task with the
2223 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002224 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002225 }
2226
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002227 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002228 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002229
2230 if (stack == null) {
2231 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002232 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2233 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002234 return false;
2235 }
2236
Wale Ogunwale5f986092015-12-04 15:35:38 -08002237 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002238 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2239 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002240 final ArrayList<ActivityRecord> activities = task.mActivities;
2241 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002242 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002243 }
2244 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002245 }
2246
Wale Ogunwale040b4702015-08-06 18:10:50 -07002247 /**
2248 * Moves the specified task record to the input stack id.
2249 * WARNING: This method performs an unchecked/raw move of the task and
2250 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002251 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002252 * @param task Task to move.
2253 * @param stackId Id of stack to move task to.
2254 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002255 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002256 * @param reason Reason the task is been moved.
2257 * @return The stack the task was moved to.
2258 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002259 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002260 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002261
2262 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2263 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2264 + "support multi-window task=" + task + " to stackId=" + stackId);
2265 }
2266
Chong Zhang02898352015-08-21 17:27:14 -07002267 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002268 final ActivityStack prevStack = task.stack;
2269 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2270 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2271 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2272 // Whenever we are moving the top activity from the front stack we want to make sure to move
2273 // the stack to the front.
2274 final boolean wasFront = isFrontStack(prevStack)
2275 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002276
Chong Zhangd545dce2016-02-29 18:09:17 -08002277 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002278 // We don't allow moving a unresizeable task to the docked stack since the docked
2279 // stack is used for split-screen mode and will cause things like the docked divider to
2280 // show up. We instead leave the task in its current stack or move it to the fullscreen
2281 // stack if it isn't currently in a stack.
2282 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002283 Slog.w(TAG, "Can not move unresizeable task=" + task
2284 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2285 }
2286
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002287 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2288 // if a docked stack is created below which will lead to the stack we are moving from and
2289 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002290 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002291 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002292 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002293 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002294 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002295
2296 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002297 // move focus to the new stack by moving the stack to the front.
2298 stack.moveToFrontAndResumeStateIfNeeded(
2299 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002300
Wale Ogunwale040b4702015-08-06 18:10:50 -07002301 return stack;
2302 }
2303
Chong Zhange4fbd322016-03-01 14:44:03 -08002304 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002305 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002306 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2307 false /* deferResume */);
2308 }
2309
2310 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2311 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002312 final TaskRecord task = anyTaskForIdLocked(taskId);
2313 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002314 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002315 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002316 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002317
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002318 if (task.stack != null && task.stack.mStackId == stackId) {
2319 // You are already in the right stack silly...
2320 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002321 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002322 }
2323
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002324 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2325 throw new IllegalArgumentException("moveTaskToStack:"
2326 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2327 }
2328
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002329 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002330 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002331 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002332 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002333 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002334 // We are about to relaunch the activity because its configuration changed due to
2335 // being maximized, i.e. size change. The activity will first remove the old window
2336 // and then add a new one. This call will tell window manager about this, so it can
2337 // preserve the old window until the new one is drawn. This prevents having a gap
2338 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002339 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002340 // Note here we always set the replacing window first, as the flags might be needed
2341 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002342 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002343 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002344
2345 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002346 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002347 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002348 try {
2349 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002350 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002351 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002352
Wale Ogunwale961f4852016-02-01 20:25:54 -08002353 if (!animate) {
2354 stack.mNoAnimActivities.add(topActivity);
2355 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002356
Wale Ogunwale961f4852016-02-01 20:25:54 -08002357 // We might trigger a configuration change. Save the current task bounds for freezing.
2358 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2359
2360 // Make sure the task has the appropriate bounds/size for the stack it is in.
2361 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002362 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2363 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002364 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2365 Rect bounds = task.getLaunchBounds();
2366 if (bounds == null) {
2367 stack.layoutTaskInStack(task, null);
2368 bounds = task.mBounds;
2369 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002370 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2371 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002372 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002373 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2374 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002375 }
2376 } finally {
2377 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002378 }
2379
2380 if (mightReplaceWindow) {
2381 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2382 // window), we need to clear the replace window settings. Otherwise, we schedule a
2383 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002384 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002385 }
2386
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002387 if (!deferResume) {
2388
2389 // The task might have already been running and its visibility needs to be synchronized with
2390 // the visibility of the stack / windows.
2391 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2392 resumeFocusedStackTopActivityLocked();
2393 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002394
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002395 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002396
2397 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002398 }
2399
Wale Ogunwale079a0042015-10-24 11:44:07 -07002400 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2401 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2402 if (stack == null) {
2403 throw new IllegalArgumentException(
2404 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2405 }
2406
2407 final ActivityRecord r = stack.topRunningActivityLocked();
2408 if (r == null) {
2409 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2410 + " in stack=" + stack);
2411 return false;
2412 }
2413
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002414 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002415 Slog.w(TAG,
2416 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002417 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002418 return false;
2419 }
2420
Wale Ogunwale480dca02016-02-06 13:58:29 -08002421 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002422 return true;
2423 }
2424
Wale Ogunwale480dca02016-02-06 13:58:29 -08002425 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2426 mWindowManager.deferSurfaceLayout();
2427 try {
2428 final TaskRecord task = r.task;
2429
2430 // Need to make sure the pinned stack exist so we can resize it below...
2431 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2432
2433 // Resize the pinned stack to match the current size of the task the activity we are
2434 // going to be moving is currently contained in. We do this to have the right starting
2435 // animation bounds for the pinned stack to the desired bounds the caller wants.
2436 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2437 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2438 true /* allowResizeInDockedMode */);
2439
2440 if (task.mActivities.size() == 1) {
2441 // There is only one activity in the task. So, we can just move the task over to
2442 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002443 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2444 // Move the home stack forward if the task we just moved to the pinned stack
2445 // was launched from home so home should be visible behind it.
2446 moveHomeStackToFront(reason);
2447 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002448 moveTaskToStackLocked(
2449 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2450 } else {
2451 stack.moveActivityToStack(r);
2452 }
2453 } finally {
2454 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002455 }
2456
Wale Ogunwale480dca02016-02-06 13:58:29 -08002457 // The task might have already been running and its visibility needs to be synchronized
2458 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002459 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002460 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002461
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002462 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002463 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002464 }
2465
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002466 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2467 final TaskRecord task = anyTaskForIdLocked(taskId);
2468 if (task == null) {
2469 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2470 return;
2471 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002472 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2473
2474 task.updateOverrideConfigurationForStack(stack);
2475
2476 mWindowManager.positionTaskInStack(
2477 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002478 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002479 // The task might have already been running and its visibility needs to be synchronized with
2480 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002481 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2482 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002483 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002484 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002485 }
2486
Craig Mautnerac6f8432013-07-17 13:24:59 -07002487 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002488 mTmpFindTaskResult.r = null;
2489 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002490 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2494 final ActivityStack stack = stacks.get(stackNdx);
2495 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002496 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002497 continue;
2498 }
2499 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002500 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2501 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002502 continue;
2503 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002504 stack.findTaskLocked(r, mTmpFindTaskResult);
2505 // It is possible to have task in multiple stacks with the same root affinity.
2506 // If the match we found was based on root affinity we keep on looking to see if
2507 // there is a better match in another stack. We eventually return the match based
2508 // on root affinity if we don't find a better match.
2509 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2510 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002511 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002512 }
2513 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002514 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2515 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002516 }
2517
2518 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002519 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2520 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002521 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2522 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2523 if (ar != null) {
2524 return ar;
2525 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002526 }
2527 }
2528 return null;
2529 }
2530
Craig Mautner8d341ef2013-03-26 09:03:27 -07002531 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002532 scheduleSleepTimeout();
2533 if (!mGoingToSleep.isHeld()) {
2534 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002535 if (mLaunchingActivity.isHeld()) {
2536 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2537 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002538 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002539 mLaunchingActivity.release();
2540 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002541 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002542 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002543 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002544 }
2545
2546 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002547 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002548
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002549 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002550 final long endTime = System.currentTimeMillis() + timeout;
2551 while (true) {
2552 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2556 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2557 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002558 }
2559 if (cantShutdown) {
2560 long timeRemaining = endTime - System.currentTimeMillis();
2561 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002562 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002563 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 } catch (InterruptedException e) {
2565 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002566 } else {
2567 Slog.w(TAG, "Activity manager shutdown timed out");
2568 timedout = true;
2569 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002570 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002571 } else {
2572 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002573 }
2574 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002575
2576 // Force checkReadyForSleep to complete.
2577 mSleepTimeout = true;
2578 checkReadyForSleepLocked();
2579
Craig Mautner8d341ef2013-03-26 09:03:27 -07002580 return timedout;
2581 }
2582
2583 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002584 removeSleepTimeouts();
2585 if (mGoingToSleep.isHeld()) {
2586 mGoingToSleep.release();
2587 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2589 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002590 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2591 final ActivityStack stack = stacks.get(stackNdx);
2592 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002593 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002594 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002595 }
Craig Mautner5314a402013-09-26 12:40:16 -07002596 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002597 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002598 mGoingToSleepActivities.clear();
2599 }
2600
2601 void activitySleptLocked(ActivityRecord r) {
2602 mGoingToSleepActivities.remove(r);
2603 checkReadyForSleepLocked();
2604 }
2605
2606 void checkReadyForSleepLocked() {
2607 if (!mService.isSleepingOrShuttingDown()) {
2608 // Do not care.
2609 return;
2610 }
2611
2612 if (!mSleepTimeout) {
2613 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002614 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2615 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2617 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2618 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002619 }
2620
2621 if (mStoppingActivities.size() > 0) {
2622 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002623 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002624 + mStoppingActivities.size() + " activities");
2625 scheduleIdleLocked();
2626 dontSleep = true;
2627 }
2628
2629 if (mGoingToSleepActivities.size() > 0) {
2630 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002631 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002632 + mGoingToSleepActivities.size() + " activities");
2633 dontSleep = true;
2634 }
2635
2636 if (dontSleep) {
2637 return;
2638 }
2639 }
2640
Craig Mautnere0a38842013-12-16 16:14:02 -08002641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2642 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2644 stacks.get(stackNdx).goToSleep();
2645 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002646 }
2647
2648 removeSleepTimeouts();
2649
2650 if (mGoingToSleep.isHeld()) {
2651 mGoingToSleep.release();
2652 }
2653 if (mService.mShuttingDown) {
2654 mService.notifyAll();
2655 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 }
2657
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002658 boolean reportResumedActivityLocked(ActivityRecord r) {
2659 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002660 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002661 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002662 }
2663 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002664 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002665 mWindowManager.executeAppTransition();
2666 return true;
2667 }
2668 return false;
2669 }
2670
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2673 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002674 int stackNdx = stacks.size() - 1;
2675 while (stackNdx >= 0) {
2676 stacks.get(stackNdx).handleAppCrashLocked(app);
2677 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680 }
2681
Jose Lima4b6c6692014-08-12 17:41:12 -07002682 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002683 final ActivityStack stack = r.task.stack;
2684 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002685 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2686 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002687 return false;
2688 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002689 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002690 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2691 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002692
2693 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002694 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002695 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002696 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002697 return true;
2698 }
2699
2700 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002701 if (visible && top.fullscreen) {
2702 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002703 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2704 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2705 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2706 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002707 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002708 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2709 // Only the activity set as currently visible behind should actively reset its
2710 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002711 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2712 "requestVisibleBehind: returning visible=" + visible
2713 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2714 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002715 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002716 }
2717
Jose Lima4b6c6692014-08-12 17:41:12 -07002718 stack.setVisibleBehindActivity(visible ? r : null);
2719 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002720 // If there is a translucent home activity, we need to force it stop being translucent,
2721 // because we can't depend on the application to necessarily perform that operation.
2722 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002723 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002724 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002725 mService.convertFromTranslucent(next.appToken);
2726 }
2727 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002728 if (top.app != null && top.app.thread != null) {
2729 // Notify the top app of the change.
2730 try {
2731 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2732 } catch (RemoteException e) {
2733 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002734 }
2735 return true;
2736 }
2737
Craig Mautnerbb742462014-07-07 15:28:55 -07002738 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2739 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002740 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002741 final ActivityStack stack = task.stack;
2742
2743 r.mLaunchTaskBehind = false;
2744 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002745 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002746 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002747 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002748
2749 // When launching tasks behind, update the last active time of the top task after the new
2750 // task has been shown briefly
2751 final ActivityRecord top = stack.topActivity();
2752 if (top != null) {
2753 top.task.touchActiveTime();
2754 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002755 }
2756
2757 void scheduleLaunchTaskBehindComplete(IBinder token) {
2758 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2759 }
2760
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002761 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2762 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002763 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 final int topStackNdx = stacks.size() - 1;
2767 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2768 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002769 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002772 }
2773
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002774 void invalidateTaskLayers() {
2775 mTaskLayersChanged = true;
2776 }
2777
2778 void rankTaskLayersIfNeeded() {
2779 if (!mTaskLayersChanged) {
2780 return;
2781 }
2782 mTaskLayersChanged = false;
2783 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2784 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2785 int baseLayer = 0;
2786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2787 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2788 }
2789 }
2790 }
2791
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002792 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2795 final int topStackNdx = stacks.size() - 1;
2796 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2797 final ActivityStack stack = stacks.get(stackNdx);
2798 stack.clearOtherAppTimeTrackers(except);
2799 }
2800 }
2801 }
2802
Craig Mautner8d341ef2013-03-26 09:03:27 -07002803 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2805 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002806 final int numStacks = stacks.size();
2807 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2808 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002809 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002810 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 }
2812 }
2813
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002814 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2815 // Examine all activities currently running in the process.
2816 TaskRecord firstTask = null;
2817 // Tasks is non-null only if two or more tasks are found.
2818 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002819 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2820 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002821 ActivityRecord r = app.activities.get(i);
2822 // First, if we find an activity that is in the process of being destroyed,
2823 // then we just aren't going to do anything for now; we want things to settle
2824 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002825 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002827 return;
2828 }
2829 // Don't consider any activies that are currently not in a state where they
2830 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2832 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002833 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002834 continue;
2835 }
2836 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002838 + " from " + r);
2839 if (firstTask == null) {
2840 firstTask = r.task;
2841 } else if (firstTask != r.task) {
2842 if (tasks == null) {
2843 tasks = new ArraySet<>();
2844 tasks.add(firstTask);
2845 }
2846 tasks.add(r.task);
2847 }
2848 }
2849 }
2850 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002851 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002852 return;
2853 }
2854 // If we have activities in multiple tasks that are in a position to be destroyed,
2855 // let's iterate through the tasks and release the oldest one.
2856 final int numDisplays = mActivityDisplays.size();
2857 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2858 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2859 // Step through all stacks starting from behind, to hit the oldest things first.
2860 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2861 final ActivityStack stack = stacks.get(stackNdx);
2862 // Try to release activities in this stack; if we manage to, we are done.
2863 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2864 return;
2865 }
2866 }
2867 }
2868 }
2869
Amith Yamasani37a40c22015-06-17 13:25:42 -07002870 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002871 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002872 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002873 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002874
Craig Mautner858d8a62013-04-23 17:08:34 -07002875 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002876 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2877 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002878 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002879 final ActivityStack stack = stacks.get(stackNdx);
2880 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002881 TaskRecord task = stack.topTask();
2882 if (task != null) {
2883 mWindowManager.moveTaskToTop(task.taskId);
2884 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002885 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002886 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002887
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002888 ActivityStack stack = getStack(restoreStackId);
2889 if (stack == null) {
2890 stack = mHomeStack;
2891 }
2892 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002893 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002894 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002895 } else {
2896 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002897 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 }
Craig Mautner93529a42013-10-04 15:03:13 -07002899 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002900 }
2901
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002902 /** Checks whether the userid is a profile of the current user. */
2903 boolean isCurrentProfileLocked(int userId) {
2904 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002905 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002906 }
2907
Chong Zhang45c25ce2015-08-10 22:18:26 -07002908 /** Checks whether the activity should be shown for current user. */
2909 boolean okToShowLocked(ActivityRecord r) {
2910 return r != null && (isCurrentProfileLocked(r.userId)
2911 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2912 }
2913
Craig Mautnerde4ef022013-04-07 19:01:33 -07002914 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002915 ArrayList<ActivityRecord> stops = null;
2916
2917 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002918 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2919 ActivityRecord s = mStoppingActivities.get(activityNdx);
2920 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002921 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002922 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2923 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002924 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002925 if (s.finishing) {
2926 // If this activity is finishing, it is sitting on top of
2927 // everyone else but we now know it is no longer needed...
2928 // so get rid of it. Otherwise, we need to go through the
2929 // normal flow and hide it once we determine that it is
2930 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002931 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002932 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002933 }
2934 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002935 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002936 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002937 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002938 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002939 }
2940 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002941 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002942 }
2943 }
2944
2945 return stops;
2946 }
2947
Craig Mautnercf910b02013-04-23 11:23:27 -07002948 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2951 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2952 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002953 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002954 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002955 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002956 if (r == null) Slog.e(TAG,
2957 "validateTop...: null top activity, stack=" + stack);
2958 else {
2959 final ActivityRecord pausing = stack.mPausingActivity;
2960 if (pausing != null && pausing == r) Slog.e(TAG,
2961 "validateTop...: top stack has pausing activity r=" + r
2962 + " state=" + state);
2963 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2964 "validateTop...: activity in front not resumed r=" + r
2965 + " state=" + state);
2966 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002967 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002968 final ActivityRecord resumed = stack.mResumedActivity;
2969 if (resumed != null && resumed == r) Slog.e(TAG,
2970 "validateTop...: back stack has resumed activity r=" + r
2971 + " state=" + state);
2972 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2973 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002974 }
2975 }
2976 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002977 }
2978
Craig Mautnere0570202015-05-13 13:06:11 -07002979 private String lockTaskModeToString() {
2980 switch (mLockTaskModeState) {
2981 case LOCK_TASK_MODE_LOCKED:
2982 return "LOCKED";
2983 case LOCK_TASK_MODE_PINNED:
2984 return "PINNED";
2985 case LOCK_TASK_MODE_NONE:
2986 return "NONE";
2987 default: return "unknown=" + mLockTaskModeState;
2988 }
2989 }
2990
Craig Mautner27084302013-03-25 08:05:25 -07002991 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002992 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002993 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002994 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002995 pw.print(prefix);
2996 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002997 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002998 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002999 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3000 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3001 if (packages.size() > 0) {
3002 pw.println(" mLockTaskPackages (userId:packages)=");
3003 for (int i = 0; i < packages.size(); ++i) {
3004 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3005 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3006 }
3007 }
3008 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003009 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003010
Craig Mautner20e72272013-04-01 13:45:53 -07003011 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003012 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003013 }
3014
Dianne Hackborn390517b2013-05-30 15:03:32 -07003015 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3016 boolean needSep, String prefix) {
3017 if (activity != null) {
3018 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3019 if (needSep) {
3020 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003021 }
3022 pw.print(prefix);
3023 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003024 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003025 }
3026 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003027 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003028 }
3029
Craig Mautner8d341ef2013-03-26 09:03:27 -07003030 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3031 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003032 boolean printed = false;
3033 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3035 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003036 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003037 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003038 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003039 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003040 final ActivityStack stack = stacks.get(stackNdx);
3041 StringBuilder stackHeader = new StringBuilder(128);
3042 stackHeader.append(" Stack #");
3043 stackHeader.append(stack.mStackId);
3044 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003045 stackHeader.append("\n");
3046 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3047 stackHeader.append("\n");
3048 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3050 needSep, stackHeader.toString());
3051 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3052 !dumpAll, false, dumpPackage, true,
3053 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003054
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 needSep = printed;
3056 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3057 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003058 if (pr) {
3059 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003060 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003061 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3063 " mResumedActivity: ");
3064 if (pr) {
3065 printed = true;
3066 needSep = false;
3067 }
3068 if (dumpAll) {
3069 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3070 " mLastPausedActivity: ");
3071 if (pr) {
3072 printed = true;
3073 needSep = true;
3074 }
3075 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3076 needSep, " mLastNoHistoryActivity: ");
3077 }
3078 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003079 }
3080 }
3081
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003082 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3083 false, dumpPackage, true, " Activities waiting to finish:", null);
3084 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3085 false, dumpPackage, true, " Activities waiting to stop:", null);
3086 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3087 false, dumpPackage, true, " Activities waiting for another to become visible:",
3088 null);
3089 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3090 false, dumpPackage, true, " Activities waiting to sleep:", null);
3091 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3092 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003093
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003094 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003095 }
3096
Dianne Hackborn390517b2013-05-30 15:03:32 -07003097 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003098 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003099 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003100 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003101 String innerPrefix = null;
3102 String[] args = null;
3103 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003104 for (int i=list.size()-1; i>=0; i--) {
3105 final ActivityRecord r = list.get(i);
3106 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3107 continue;
3108 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003109 if (innerPrefix == null) {
3110 innerPrefix = prefix + " ";
3111 args = new String[0];
3112 }
3113 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003114 final boolean full = !brief && (complete || !r.isInHistory());
3115 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003116 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003117 needNL = false;
3118 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003119 if (header1 != null) {
3120 pw.println(header1);
3121 header1 = null;
3122 }
3123 if (header2 != null) {
3124 pw.println(header2);
3125 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003126 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003127 if (lastTask != r.task) {
3128 lastTask = r.task;
3129 pw.print(prefix);
3130 pw.print(full ? "* " : " ");
3131 pw.println(lastTask);
3132 if (full) {
3133 lastTask.dump(pw, prefix + " ");
3134 } else if (complete) {
3135 // Complete + brief == give a summary. Isn't that obvious?!?
3136 if (lastTask.intent != null) {
3137 pw.print(prefix); pw.print(" ");
3138 pw.println(lastTask.intent.toInsecureStringWithClip());
3139 }
3140 }
3141 }
3142 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3143 pw.print(" #"); pw.print(i); pw.print(": ");
3144 pw.println(r);
3145 if (full) {
3146 r.dump(pw, innerPrefix);
3147 } else if (complete) {
3148 // Complete + brief == give a summary. Isn't that obvious?!?
3149 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3150 if (r.app != null) {
3151 pw.print(innerPrefix); pw.println(r.app);
3152 }
3153 }
3154 if (client && r.app != null && r.app.thread != null) {
3155 // flush anything that is already in the PrintWriter since the thread is going
3156 // to write to the file descriptor directly
3157 pw.flush();
3158 try {
3159 TransferPipe tp = new TransferPipe();
3160 try {
3161 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3162 r.appToken, innerPrefix, args);
3163 // Short timeout, since blocking here can
3164 // deadlock with the application.
3165 tp.go(fd, 2000);
3166 } finally {
3167 tp.kill();
3168 }
3169 } catch (IOException e) {
3170 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3171 } catch (RemoteException e) {
3172 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3173 }
3174 needNL = true;
3175 }
3176 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003177 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003178 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003179
Craig Mautnerf3333272013-04-22 10:55:53 -07003180 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003181 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3182 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003183 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3184 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003185 }
3186
3187 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003188 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003189 }
3190
3191 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003192 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3193 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003194 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3195 }
3196
Craig Mautner05d29032013-05-03 13:40:13 -07003197 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003198 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3199 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3200 }
Craig Mautner05d29032013-05-03 13:40:13 -07003201 }
3202
Craig Mautner0eea92c2013-05-16 13:35:39 -07003203 void removeSleepTimeouts() {
3204 mSleepTimeout = false;
3205 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3206 }
3207
3208 final void scheduleSleepTimeout() {
3209 removeSleepTimeouts();
3210 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3211 }
3212
Craig Mautner4a1cb222013-12-04 16:14:06 -08003213 @Override
3214 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003215 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003216 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3217 }
3218
3219 @Override
3220 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003221 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003222 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3223 }
3224
3225 @Override
3226 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003227 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3229 }
3230
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003231 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003232 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003233 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003234 newDisplay = mActivityDisplays.get(displayId) == null;
3235 if (newDisplay) {
3236 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003237 if (activityDisplay.mDisplay == null) {
3238 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3239 return;
3240 }
Craig Mautner4504de52013-12-20 09:06:56 -08003241 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003242 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003243 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 }
Craig Mautner4504de52013-12-20 09:06:56 -08003245 if (newDisplay) {
3246 mWindowManager.onDisplayAdded(displayId);
3247 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003248 }
3249
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003250 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3251 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3252 return;
3253 }
3254 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3255 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3256 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3257 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3258 }
3259
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003260 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003262 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3263 if (activityDisplay != null) {
3264 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003265 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003266 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003267 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003268 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003269 }
3270 }
3271 mWindowManager.onDisplayRemoved(displayId);
3272 }
3273
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003274 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003275 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003276 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3277 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 // TODO: Update the bounds.
3279 }
3280 }
3281 mWindowManager.onDisplayChanged(displayId);
3282 }
3283
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003284 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003285 StackInfo info = new StackInfo();
3286 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3287 info.displayId = Display.DEFAULT_DISPLAY;
3288 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003289 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290
3291 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3292 final int numTasks = tasks.size();
3293 int[] taskIds = new int[numTasks];
3294 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003295 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003296 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003297 for (int i = 0; i < numTasks; ++i) {
3298 final TaskRecord task = tasks.get(i);
3299 taskIds[i] = task.taskId;
3300 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3301 : task.realActivity != null ? task.realActivity.flattenToString()
3302 : task.getTopActivity() != null ? task.getTopActivity().packageName
3303 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003304 taskBounds[i] = new Rect();
3305 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003306 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003307 }
3308 info.taskIds = taskIds;
3309 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003310 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003311 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003312 return info;
3313 }
3314
3315 StackInfo getStackInfoLocked(int stackId) {
3316 ActivityStack stack = getStack(stackId);
3317 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003318 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003319 }
3320 return null;
3321 }
3322
3323 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003324 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003325 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3326 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003328 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003329 }
3330 }
3331 return list;
3332 }
3333
Craig Mautner15df08a2015-04-01 12:17:18 -07003334 TaskRecord getLockedTaskLocked() {
3335 final int top = mLockTaskModeTasks.size() - 1;
3336 if (top >= 0) {
3337 return mLockTaskModeTasks.get(top);
3338 }
3339 return null;
3340 }
3341
3342 boolean isLockedTask(TaskRecord task) {
3343 return mLockTaskModeTasks.contains(task);
3344 }
3345
3346 boolean isLastLockedTask(TaskRecord task) {
3347 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3348 }
3349
3350 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003351 if (!mLockTaskModeTasks.remove(task)) {
3352 return;
3353 }
3354 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3355 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003356 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003357 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3358 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003359 final Message lockTaskMsg = Message.obtain();
3360 lockTaskMsg.arg1 = task.userId;
3361 lockTaskMsg.what = LOCK_TASK_END_MSG;
3362 mHandler.sendMessage(lockTaskMsg);
3363 }
3364 }
3365
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003366 void showNonResizeableDockToastIfNeeded(
3367 TaskRecord task, int preferredStackId, int actualStackId) {
3368 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3369 return;
3370 }
3371
Jorim Jaggi2adba072016-03-03 13:43:39 +01003372 if (!task.canGoInDockedStack()) {
3373 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003374 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003375 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3376 String packageName = task.getTopActivity() != null
3377 ? task.getTopActivity().appInfo.packageName : null;
3378 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3379 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003380 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003381 }
3382
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003383 void showLockTaskToast() {
3384 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003385 }
3386
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003387 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3388 if (mLockTaskModeTasks.contains(task)) {
3389 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3390 }
3391 }
3392
Craig Mautner432f64e2015-05-20 14:59:57 -07003393 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3394 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003395 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003396 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003397 final TaskRecord lockedTask = getLockedTaskLocked();
3398 if (lockedTask != null) {
3399 removeLockedTaskLocked(lockedTask);
3400 if (!mLockTaskModeTasks.isEmpty()) {
3401 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003402 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3403 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003404 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003405 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003406 return;
3407 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003408 }
Craig Mautnere0570202015-05-13 13:06:11 -07003409 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3410 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003411 return;
3412 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003413
3414 // Should have already been checked, but do it again.
3415 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003416 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3417 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003418 return;
3419 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003420 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003421 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003422 return;
3423 }
3424
3425 if (mLockTaskModeTasks.isEmpty()) {
3426 // First locktask.
3427 final Message lockTaskMsg = Message.obtain();
3428 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3429 lockTaskMsg.arg1 = task.userId;
3430 lockTaskMsg.what = LOCK_TASK_START_MSG;
3431 lockTaskMsg.arg2 = lockTaskModeState;
3432 mHandler.sendMessage(lockTaskMsg);
3433 }
3434 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003435 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3436 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003437 mLockTaskModeTasks.remove(task);
3438 mLockTaskModeTasks.add(task);
3439
3440 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003441 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003442 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003443
3444 if (andResume) {
3445 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003446 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003447 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003448 }
3449
3450 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003451 return isLockTaskModeViolation(task, false);
3452 }
3453
3454 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3455 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003456 return false;
3457 }
3458 final int lockTaskAuth = task.mLockTaskAuth;
3459 switch (lockTaskAuth) {
3460 case LOCK_TASK_AUTH_DONT_LOCK:
3461 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003462 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003463 case LOCK_TASK_AUTH_LAUNCHABLE:
3464 case LOCK_TASK_AUTH_WHITELISTED:
3465 return false;
3466 case LOCK_TASK_AUTH_PINNABLE:
3467 // Pinnable tasks can't be launched on top of locktask tasks.
3468 return !mLockTaskModeTasks.isEmpty();
3469 default:
3470 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3471 return true;
3472 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003473 }
3474
Craig Mautner15df08a2015-04-01 12:17:18 -07003475 void onLockTaskPackagesUpdatedLocked() {
3476 boolean didSomething = false;
3477 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3478 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003479 final boolean wasWhitelisted =
3480 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3481 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003482 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003483 final boolean isWhitelisted =
3484 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3485 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3486 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003487 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003488 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3489 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003490 removeLockedTaskLocked(lockedTask);
3491 lockedTask.performClearTaskLocked();
3492 didSomething = true;
3493 }
3494 }
3495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3496 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3498 final ActivityStack stack = stacks.get(stackNdx);
3499 stack.onLockTaskPackagesUpdatedLocked();
3500 }
3501 }
Craig Mautnere0570202015-05-13 13:06:11 -07003502 final ActivityRecord r = topRunningActivityLocked();
3503 final TaskRecord task = r != null ? r.task : null;
3504 if (mLockTaskModeTasks.isEmpty() && task != null
3505 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3506 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003507 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3508 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3509 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3510 false);
3511 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003512 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003513 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003514 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003515 }
3516 }
3517
Benjamin Franz43261142015-02-11 15:59:44 +00003518 int getLockTaskModeState() {
3519 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003520 }
3521
Jorim Jaggife89d122015-12-22 16:28:44 +01003522 void activityRelaunchedLocked(IBinder token) {
3523 mWindowManager.notifyAppRelaunchingFinished(token);
3524 }
3525
3526 void activityRelaunchingLocked(ActivityRecord r) {
3527 mWindowManager.notifyAppRelaunching(r.appToken);
3528 }
3529
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003530 void logStackState() {
3531 mActivityMetricsLogger.logWindowState();
3532 }
3533
Andrii Kulian933076d2016-03-29 17:04:42 -07003534 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003535 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3536 final ActivityRecord r = task.mActivities.get(i);
3537 if (r.app != null && r.app.thread != null) {
3538 mMultiWindowModeChangedActivities.add(r);
3539 }
3540 }
3541
3542 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3543 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3544 }
3545 }
3546
Andrii Kulian933076d2016-03-29 17:04:42 -07003547 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003548 final ActivityStack stack = task.stack;
3549 if (prevStack == null || prevStack == stack
3550 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3551 return;
3552 }
3553
3554 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3555 final ActivityRecord r = task.mActivities.get(i);
3556 if (r.app != null && r.app.thread != null) {
3557 mPipModeChangedActivities.add(r);
3558 }
3559 }
3560
3561 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3562 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3563 }
3564 }
3565
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003566 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003567
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003568 public ActivityStackSupervisorHandler(Looper looper) {
3569 super(looper);
3570 }
3571
Craig Mautnerf3333272013-04-22 10:55:53 -07003572 void activityIdleInternal(ActivityRecord r) {
3573 synchronized (mService) {
3574 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3575 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003576 }
3577
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003578 @Override
3579 public void handleMessage(Message msg) {
3580 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003581 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3582 synchronized (mService) {
3583 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3584 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003585 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003586 }
3587 }
3588 } break;
3589 case REPORT_PIP_MODE_CHANGED_MSG: {
3590 synchronized (mService) {
3591 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3592 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003593 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003594 }
3595 }
3596 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003597 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3599 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003600 if (mService.mDidDexOpt) {
3601 mService.mDidDexOpt = false;
3602 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3603 nmsg.obj = msg.obj;
3604 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3605 return;
3606 }
3607 // We don't at this point know if the activity is fullscreen,
3608 // so we need to be conservative and assume it isn't.
3609 activityIdleInternal((ActivityRecord)msg.obj);
3610 } break;
3611 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003612 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003613 activityIdleInternal((ActivityRecord)msg.obj);
3614 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003615 case RESUME_TOP_ACTIVITY_MSG: {
3616 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003617 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003618 }
3619 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003620 case SLEEP_TIMEOUT_MSG: {
3621 synchronized (mService) {
3622 if (mService.isSleepingOrShuttingDown()) {
3623 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3624 mSleepTimeout = true;
3625 checkReadyForSleepLocked();
3626 }
3627 }
3628 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003629 case LAUNCH_TIMEOUT_MSG: {
3630 if (mService.mDidDexOpt) {
3631 mService.mDidDexOpt = false;
3632 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3633 return;
3634 }
3635 synchronized (mService) {
3636 if (mLaunchingActivity.isHeld()) {
3637 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3638 if (VALIDATE_WAKE_LOCK_CALLER
3639 && Binder.getCallingUid() != Process.myUid()) {
3640 throw new IllegalStateException("Calling must be system uid");
3641 }
3642 mLaunchingActivity.release();
3643 }
3644 }
3645 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003646 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003647 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003648 } break;
3649 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003650 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003651 } break;
3652 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003653 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003654 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003655 case CONTAINER_CALLBACK_VISIBILITY: {
3656 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003657 final IActivityContainerCallback callback = container.mCallback;
3658 if (callback != null) {
3659 try {
3660 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3661 } catch (RemoteException e) {
3662 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003663 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003664 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003665 case LOCK_TASK_START_MSG: {
3666 // When lock task starts, we disable the status bars.
3667 try {
Jason Monk62515be2014-05-21 16:06:19 -04003668 if (mLockTaskNotify == null) {
3669 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003670 }
Jason Monk62515be2014-05-21 16:06:19 -04003671 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003672 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003673 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003674 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003675 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003676 flags = StatusBarManager.DISABLE_MASK
3677 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003678 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003679 flags = StatusBarManager.DISABLE_MASK
3680 & (~StatusBarManager.DISABLE_BACK)
3681 & (~StatusBarManager.DISABLE_HOME)
3682 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003683 }
3684 getStatusBarService().disable(flags, mToken,
3685 mService.mContext.getPackageName());
3686 }
3687 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003688 if (getDevicePolicyManager() != null) {
3689 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003690 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003691 }
justinzhang5286d3f2014-05-12 17:06:01 -04003692 } catch (RemoteException ex) {
3693 throw new RuntimeException(ex);
3694 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003695 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003696 case LOCK_TASK_END_MSG: {
3697 // When lock task ends, we enable the status bars.
3698 try {
Jason Monk62515be2014-05-21 16:06:19 -04003699 if (getStatusBarService() != null) {
3700 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3701 mService.mContext.getPackageName());
3702 }
3703 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003704 if (getDevicePolicyManager() != null) {
3705 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3706 msg.arg1);
3707 }
Jason Monk62515be2014-05-21 16:06:19 -04003708 if (mLockTaskNotify == null) {
3709 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3710 }
3711 mLockTaskNotify.show(false);
3712 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003713 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003714 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003715 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003716 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003717 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003718 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003719 new LockPatternUtils(mService.mContext)
3720 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003721 }
3722 } catch (SettingNotFoundException e) {
3723 // No setting, don't lock.
3724 }
justinzhang5286d3f2014-05-12 17:06:01 -04003725 } catch (RemoteException ex) {
3726 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003727 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003728 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003729 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003730 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003731 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3732 if (mLockTaskNotify == null) {
3733 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3734 }
3735 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3736 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003737 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3738 final ActivityContainer container = (ActivityContainer) msg.obj;
3739 final IActivityContainerCallback callback = container.mCallback;
3740 if (callback != null) {
3741 try {
3742 callback.onAllActivitiesComplete(container.asBinder());
3743 } catch (RemoteException e) {
3744 }
3745 }
3746 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003747 case LAUNCH_TASK_BEHIND_COMPLETE: {
3748 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003749 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003750 if (r != null) {
3751 handleLaunchTaskBehindCompleteLocked(r);
3752 }
3753 }
3754 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003755
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003756 }
3757 }
3758 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003759
Ying Wangb081a592014-04-22 15:20:16 -07003760 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003761 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3762 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003764 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003765 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003766 ActivityRecord mParentActivity = null;
3767 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003768
Craig Mautnere3a00d72014-04-16 08:31:19 -07003769 boolean mVisible = true;
3770
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003772 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003774 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3775 final static int CONTAINER_STATE_NO_SURFACE = 1;
3776 final static int CONTAINER_STATE_FINISHING = 2;
3777 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3778
3779 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 synchronized (mService) {
3781 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003782 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003783 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003784 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003786 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003788 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003789 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003790 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003791 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003792 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003793 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 }
3795
3796 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003797 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003798 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003799 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3800 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 return;
3802 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003803 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 }
3805 }
3806
3807 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003808 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003809 synchronized (mService) {
3810 if (mActivityDisplay != null) {
3811 return mActivityDisplay.mDisplayId;
3812 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003813 }
3814 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
3816
Jeff Brownca9bc702014-02-11 14:32:56 -08003817 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003818 public int getStackId() {
3819 synchronized (mService) {
3820 return mStackId;
3821 }
3822 }
3823
3824 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003825 public boolean injectEvent(InputEvent event) {
3826 final long origId = Binder.clearCallingIdentity();
3827 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003828 synchronized (mService) {
3829 if (mActivityDisplay != null) {
3830 return mInputManagerInternal.injectInputEvent(event,
3831 mActivityDisplay.mDisplayId,
3832 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3833 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003834 }
3835 return false;
3836 } finally {
3837 Binder.restoreCallingIdentity(origId);
3838 }
3839 }
3840
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003841 @Override
3842 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003843 synchronized (mService) {
3844 if (mContainerState == CONTAINER_STATE_FINISHING) {
3845 return;
3846 }
3847 mContainerState = CONTAINER_STATE_FINISHING;
3848
Craig Mautnerd163e752014-06-13 17:18:47 -07003849 long origId = Binder.clearCallingIdentity();
3850 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003851 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003852 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003853 } finally {
3854 Binder.restoreCallingIdentity(origId);
3855 }
3856 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003857 }
3858
Craig Mautner60257702014-09-17 15:02:33 -07003859 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003860 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003861 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 if (mActivityDisplay != null) {
3863 mActivityDisplay.detachActivitiesLocked(mStack);
3864 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003865 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 }
3867 }
3868
3869 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003870 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003871 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 }
3873
3874 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003875 public final int startActivityIntentSender(IIntentSender intentSender)
3876 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003877 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3878
3879 if (!(intentSender instanceof PendingIntentRecord)) {
3880 throw new IllegalArgumentException("Bad PendingIntent object");
3881 }
3882
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003883 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003884 Binder.getCallingUid(), mCurrentUser, false,
3885 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003886
3887 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3888 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3889 pendingIntent.key.requestResolvedType);
3890
3891 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3892 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3893 }
3894
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003895 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003896 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003897 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003898 throw new SecurityException(
3899 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3900 }
3901 }
3902
Craig Mautnerdf88d732014-01-27 09:21:32 -08003903 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003904 public IBinder asBinder() {
3905 return this;
3906 }
3907
Craig Mautner4504de52013-12-20 09:06:56 -08003908 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003909 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003910 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003911 }
3912
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 ActivityStackSupervisor getOuter() {
3914 return ActivityStackSupervisor.this;
3915 }
3916
Craig Mautnerd163e752014-06-13 17:18:47 -07003917 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003918 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 }
3920
Craig Mautner6985bad2014-04-21 15:22:06 -07003921 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003922 void setVisible(boolean visible) {
3923 if (mVisible != visible) {
3924 mVisible = visible;
3925 if (mCallback != null) {
3926 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3927 0 /* unused */, this).sendToTarget();
3928 }
3929 }
3930 }
3931
Craig Mautner6985bad2014-04-21 15:22:06 -07003932 void setDrawn() {
3933 }
3934
Craig Mautner1b4bf852014-05-26 15:06:32 -07003935 // You can always start a new task on a regular ActivityStack.
3936 boolean isEligibleForNewTasks() {
3937 return true;
3938 }
3939
Craig Mautnerd163e752014-06-13 17:18:47 -07003940 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003941 detachLocked();
3942 deleteActivityContainer(this);
3943 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003944 }
3945
Craig Mautner34b73df2014-01-12 21:11:08 -08003946 @Override
3947 public String toString() {
3948 return mIdString + (mActivityDisplay == null ? "N" : "A");
3949 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003950 }
3951
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003952 private class VirtualActivityContainer extends ActivityContainer {
3953 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003954 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955
3956 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3957 super(getNextStackId());
3958 mParentActivity = parent;
3959 mCallback = callback;
3960 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003961 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003962 }
3963
3964 @Override
3965 public void setSurface(Surface surface, int width, int height, int density) {
3966 super.setSurface(surface, width, height, density);
3967
3968 synchronized (mService) {
3969 final long origId = Binder.clearCallingIdentity();
3970 try {
3971 setSurfaceLocked(surface, width, height, density);
3972 } finally {
3973 Binder.restoreCallingIdentity(origId);
3974 }
3975 }
3976 }
3977
3978 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3979 if (mContainerState == CONTAINER_STATE_FINISHING) {
3980 return;
3981 }
3982 VirtualActivityDisplay virtualActivityDisplay =
3983 (VirtualActivityDisplay) mActivityDisplay;
3984 if (virtualActivityDisplay == null) {
3985 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003986 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003987 mActivityDisplay = virtualActivityDisplay;
3988 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003989 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003990 }
3991
3992 if (mSurface != null) {
3993 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003994 }
3995
Craig Mautner6985bad2014-04-21 15:22:06 -07003996 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003997 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003998 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003999 } else {
4000 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004001 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004002 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004003 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004004 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004006
Craig Mautnerd163e752014-06-13 17:18:47 -07004007 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004008
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004009 if (DEBUG_STACK) Slog.d(TAG_STACK,
4010 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004011 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004012
Craig Mautner6985bad2014-04-21 15:22:06 -07004013 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004014 boolean isAttachedLocked() {
4015 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004016 }
4017
4018 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004019 void setDrawn() {
4020 synchronized (mService) {
4021 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004022 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004023 }
4024 }
4025
Craig Mautner1b4bf852014-05-26 15:06:32 -07004026 // Never start a new task on an ActivityView if it isn't explicitly specified.
4027 @Override
4028 boolean isEligibleForNewTasks() {
4029 return false;
4030 }
4031
Craig Mautnerd163e752014-06-13 17:18:47 -07004032 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004033 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004034 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4035 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4036 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4037 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4038 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004039 }
4040 }
4041
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4043 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004044 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004046 int mDisplayId;
4047 Display mDisplay;
4048 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004049
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4051 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004052 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053
Jose Lima4b6c6692014-08-12 17:41:12 -07004054 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004055
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004056 ActivityDisplay() {
4057 }
Craig Mautner4504de52013-12-20 09:06:56 -08004058
Craig Mautner1a70a162014-09-13 12:09:31 -07004059 // After instantiation, check that mDisplay is not null before using this. The alternative
4060 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004061 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004062 final Display display = mDisplayManager.getDisplay(displayId);
4063 if (display == null) {
4064 return;
4065 }
4066 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004067 }
4068
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004069 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004070 mDisplay = display;
4071 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004073 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004074
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004075 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004076 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004077 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4078 + " onTop=" + onTop);
4079 if (onTop) {
4080 mStacks.add(stack);
4081 } else {
4082 mStacks.add(0, stack);
4083 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004084 }
4085
Craig Mautnere0a38842013-12-16 16:14:02 -08004086 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004087 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004088 + " from displayId=" + mDisplayId);
4089 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 }
4091
Jose Lima4b6c6692014-08-12 17:41:12 -07004092 void setVisibleBehindActivity(ActivityRecord r) {
4093 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004094 }
4095
Jose Lima4b6c6692014-08-12 17:41:12 -07004096 boolean hasVisibleBehindActivity() {
4097 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004098 }
4099
Craig Mautner34b73df2014-01-12 21:11:08 -08004100 @Override
4101 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004102 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4103 }
4104 }
4105
4106 class VirtualActivityDisplay extends ActivityDisplay {
4107 VirtualDisplay mVirtualDisplay;
4108
Craig Mautner6985bad2014-04-21 15:22:06 -07004109 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004110 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004111 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4112 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4113 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4114 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004115
4116 init(mVirtualDisplay.getDisplay());
4117
4118 mWindowManager.handleDisplayAdded(mDisplayId);
4119 }
4120
4121 void setSurface(Surface surface) {
4122 if (mVirtualDisplay != null) {
4123 mVirtualDisplay.setSurface(surface);
4124 }
4125 }
4126
4127 @Override
4128 void detachActivitiesLocked(ActivityStack stack) {
4129 super.detachActivitiesLocked(stack);
4130 if (mVirtualDisplay != null) {
4131 mVirtualDisplay.release();
4132 mVirtualDisplay = null;
4133 }
4134 }
4135
4136 @Override
4137 public String toString() {
4138 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004139 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004140 }
Jose Lima58e66d62014-05-27 20:00:27 -07004141
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004142 /**
4143 * Adjust bounds to stay within stack bounds.
4144 *
4145 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4146 * that keep them unchanged, but be contained within the stack bounds.
4147 *
4148 * @param bounds Bounds to be adjusted.
4149 * @param stackBounds Bounds within which the other bounds should remain.
4150 */
4151 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4152 if (stackBounds == null || stackBounds.contains(bounds)) {
4153 return;
4154 }
4155
4156 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4157 final int maxRight = stackBounds.right
4158 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4159 int horizontalDiff = stackBounds.left - bounds.left;
4160 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4161 || (bounds.left + horizontalDiff >= maxRight)) {
4162 horizontalDiff = maxRight - bounds.left;
4163 }
4164 bounds.left += horizontalDiff;
4165 bounds.right += horizontalDiff;
4166 }
4167
4168 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4169 final int maxBottom = stackBounds.bottom
4170 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4171 int verticalDiff = stackBounds.top - bounds.top;
4172 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4173 || (bounds.top + verticalDiff >= maxBottom)) {
4174 verticalDiff = maxBottom - bounds.top;
4175 }
4176 bounds.top += verticalDiff;
4177 bounds.bottom += verticalDiff;
4178 }
4179 }
4180
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004181 ActivityStack findStackBehind(ActivityStack stack) {
4182 // TODO(multi-display): We are only looking for stacks on the default display.
4183 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4184 if (display == null) {
4185 return null;
4186 }
4187 final ArrayList<ActivityStack> stacks = display.mStacks;
4188 for (int i = stacks.size() - 1; i >= 0; i--) {
4189 if (stacks.get(i) == stack && i > 0) {
4190 return stacks.get(i - 1);
4191 }
4192 }
4193 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4194 + " in=" + stacks);
4195 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004196
Jorim Jaggic69bd222016-03-15 14:38:37 +01004197 /**
4198 * Puts a task into resizing mode during the next app transition.
4199 *
4200 * @param taskId the id of the task to put into resizing mode
4201 */
4202 private void setResizingDuringAnimation(int taskId) {
4203 mResizingTasksDuringAnimation.add(taskId);
4204 mWindowManager.setTaskDockedResizing(taskId, true);
4205 }
4206
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004207 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4208 final TaskRecord task;
4209 final int callingUid;
4210 final String callingPackage;
4211 final Intent intent;
4212 final int userId;
4213 final ActivityOptions activityOptions = (bOptions != null)
4214 ? new ActivityOptions(bOptions) : null;
4215 final int launchStackId = (activityOptions != null)
4216 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4217
4218 if (launchStackId == HOME_STACK_ID) {
4219 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4220 + taskId + " can't be launch in the home stack.");
4221 }
4222 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4223 if (task == null) {
4224 throw new IllegalArgumentException(
4225 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4226 }
4227
4228 if (launchStackId != INVALID_STACK_ID) {
4229 if (launchStackId == DOCKED_STACK_ID) {
4230 mWindowManager.setDockedStackCreateState(
4231 activityOptions.getDockCreateMode(), null /* initialBounds */);
4232
4233 // Defer updating the stack in which recents is until the app transition is done, to
4234 // not run into issues where we still need to draw the task in recents but the
4235 // docked stack is already created.
4236 deferUpdateBounds(HOME_STACK_ID);
4237 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4238 }
4239 if (task.stack.mStackId != launchStackId) {
4240 moveTaskToStackLocked(
4241 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4242 ANIMATE);
4243 }
4244 }
4245
4246 // If the user must confirm credentials (e.g. when first launching a work app and the
4247 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4248 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4249 && task.getRootActivity() != null) {
4250 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4251
4252 // If we are launching the task in the docked stack, put it into resizing mode so
4253 // the window renders full-screen with the background filling the void. Also only
4254 // call this at the end to make sure that tasks exists on the window manager side.
4255 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004256 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004257 }
4258 return ActivityManager.START_TASK_TO_FRONT;
4259 }
4260 callingUid = task.mCallingUid;
4261 callingPackage = task.mCallingPackage;
4262 intent = task.intent;
4263 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4264 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004265 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4266 null, null, 0, 0, bOptions, userId, null, task);
4267 if (launchStackId == DOCKED_STACK_ID) {
4268 setResizingDuringAnimation(task.taskId);
4269 }
4270 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004271 }
Craig Mautner27084302013-03-25 08:05:25 -07004272}