blob: 6c09178cc2af21fce50b45fdf9d3e4c95086a3ef [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
2046 if (stack.mFullscreen) {
2047 // The dock stack went fullscreen which is kinda like dismissing it.
2048 // In this case we make all other static stacks fullscreen and move all
2049 // docked stack tasks to the fullscreen stack.
2050 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2051 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2052 resizeStackLocked(i, null, null, null, preserveWindows,
2053 true /* allowResizeInDockedMode */);
2054 }
2055 }
2056
2057 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2058 final int count = tasks.size();
2059 for (int i = 0; i < count; i++) {
2060 moveTaskToStackLocked(tasks.get(i).taskId,
2061 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2062 false /* animate */);
2063 }
2064
2065 // stack shouldn't contain anymore activities, so nothing to resume.
2066 r = null;
2067 } else {
2068 // Docked stacks occupy a dedicated region on screen so the size of all other
2069 // static stacks need to be adjusted so they don't overlap with the docked stack.
2070 // We get the bounds to use from window manager which has been adjusted for any
2071 // screen controls and is also the same for all stacks.
Andrii Kulian41da89c2016-04-01 15:21:31 -07002072 if (dockedBounds != null) {
2073 mWindowManager.getStackDockedModeBounds(
2074 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
2075 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002076 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2077 if (StackId.isResizeableByDockedStack(i)) {
2078 ActivityStack otherStack = getStack(i);
2079 if (otherStack != null) {
Andrii Kulian41da89c2016-04-01 15:21:31 -07002080 resizeStackLocked(i, dockedBounds != null ? tempRect : null,
2081 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002082 true /* allowResizeInDockedMode */);
2083 }
2084 }
2085 }
2086 }
2087 ensureConfigurationAndResume(stack, r, preserveWindows);
2088 } finally {
2089 mWindowManager.continueSurfaceLayout();
2090 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2091 }
2092
2093 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2094 tempDockedTaskBounds != null
2095 || tempDockedTaskInsetBounds != null
2096 || tempOtherTaskBounds != null
2097 || tempOtherTaskInsetBounds != null);
2098 }
2099
Robert Carr0d00c2e2016-02-29 17:45:02 -08002100 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2101 final ActivityStack stack = getStack(PINNED_STACK_ID);
2102 if (stack == null) {
2103 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2104 return;
2105 }
2106 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2107 mWindowManager.deferSurfaceLayout();
2108 try {
2109 ActivityRecord r = stack.topRunningActivityLocked();
2110 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2111 null);
2112 ensureConfigurationAndResume(stack, r, false);
2113 } finally {
2114 mWindowManager.continueSurfaceLayout();
2115 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2116 }
2117 }
2118
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002119 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2120 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002121 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002122 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002123 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002124 }
2125
Chong Zhang87b21722015-09-21 15:39:51 -07002126 // If this is a forced resize, let it go through even if the bounds is not changing,
2127 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002128 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002129 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002130 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002131 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002132 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002133 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002134
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002135 if (!mWindowManager.isValidTaskId(task.taskId)) {
2136 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002137 // All we can do for now is update the bounds so it can be used when the task is
2138 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002139 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002140 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2141 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002142 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002143 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002144 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002145 }
2146
Chong Zhang6de2ae82015-09-30 18:25:21 -07002147 // Do not move the task to another stack here.
2148 // This method assumes that the task is already placed in the right stack.
2149 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002150
Chong Zhang6de2ae82015-09-30 18:25:21 -07002151 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002152
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002153 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002154 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002155 // way and the activity was kept the way it was. If it's false, it means the activity had
2156 // to be relaunched due to configuration change.
2157 boolean kept = true;
2158 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002159 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002160 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002161 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002162 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002163
2164 if (!deferResume) {
2165
2166 // All other activities must be made visible with their correct configuration.
2167 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2168 if (!kept) {
2169 resumeFocusedStackTopActivityLocked();
2170 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002171 }
2172 }
2173 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002174 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002175
2176 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002177 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002178 }
2179
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002180 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002181 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2182 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002183 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002184 }
2185
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002186 ActivityContainer activityContainer = new ActivityContainer(stackId);
2187 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002188 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002189 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002190 }
2191
2192 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002193 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002194 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2195 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002196 break;
2197 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002198 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002199 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002200 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002201 }
2202
Chong Zhang5dcb2752015-08-18 13:50:26 -07002203 /**
2204 * Restores a recent task to a stack
2205 * @param task The recent task to be restored.
2206 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002207 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002208 * @return true if the task has been restored successfully.
2209 */
2210 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2211 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002212 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002213 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2214 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2215 // Put it in the fullscreen stack.
2216 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002217 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002218
Wale Ogunwale706ed792015-08-02 10:29:44 -07002219 if (task.stack != null) {
2220 // Task has already been restored once. See if we need to do anything more
2221 if (task.stack.mStackId == stackId) {
2222 // Nothing else to do since it is already restored in the right stack.
2223 return true;
2224 }
2225 // Remove current stack association, so we can re-associate the task with the
2226 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002227 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002228 }
2229
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002230 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002231 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002232
2233 if (stack == null) {
2234 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002235 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2236 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002237 return false;
2238 }
2239
Wale Ogunwale5f986092015-12-04 15:35:38 -08002240 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002241 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2242 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002243 final ArrayList<ActivityRecord> activities = task.mActivities;
2244 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002245 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002246 }
2247 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002248 }
2249
Wale Ogunwale040b4702015-08-06 18:10:50 -07002250 /**
2251 * Moves the specified task record to the input stack id.
2252 * WARNING: This method performs an unchecked/raw move of the task and
2253 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002254 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002255 * @param task Task to move.
2256 * @param stackId Id of stack to move task to.
2257 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002258 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002259 * @param reason Reason the task is been moved.
2260 * @return The stack the task was moved to.
2261 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002262 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002263 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002264
2265 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2266 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2267 + "support multi-window task=" + task + " to stackId=" + stackId);
2268 }
2269
Chong Zhang02898352015-08-21 17:27:14 -07002270 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002271 final ActivityStack prevStack = task.stack;
2272 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2273 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2274 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2275 // Whenever we are moving the top activity from the front stack we want to make sure to move
2276 // the stack to the front.
2277 final boolean wasFront = isFrontStack(prevStack)
2278 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002279
Chong Zhangd545dce2016-02-29 18:09:17 -08002280 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002281 // We don't allow moving a unresizeable task to the docked stack since the docked
2282 // stack is used for split-screen mode and will cause things like the docked divider to
2283 // show up. We instead leave the task in its current stack or move it to the fullscreen
2284 // stack if it isn't currently in a stack.
2285 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002286 Slog.w(TAG, "Can not move unresizeable task=" + task
2287 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2288 }
2289
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002290 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2291 // if a docked stack is created below which will lead to the stack we are moving from and
2292 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002293 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002294 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002295 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002296 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002297 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002298
2299 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002300 // move focus to the new stack by moving the stack to the front.
2301 stack.moveToFrontAndResumeStateIfNeeded(
2302 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002303
Wale Ogunwale040b4702015-08-06 18:10:50 -07002304 return stack;
2305 }
2306
Chong Zhange4fbd322016-03-01 14:44:03 -08002307 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002308 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002309 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2310 false /* deferResume */);
2311 }
2312
2313 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2314 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002315 final TaskRecord task = anyTaskForIdLocked(taskId);
2316 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002317 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002318 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002319 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002320
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002321 if (task.stack != null && task.stack.mStackId == stackId) {
2322 // You are already in the right stack silly...
2323 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002324 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002325 }
2326
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002327 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2328 throw new IllegalArgumentException("moveTaskToStack:"
2329 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2330 }
2331
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002332 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002333 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002334 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002335 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002336 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002337 // We are about to relaunch the activity because its configuration changed due to
2338 // being maximized, i.e. size change. The activity will first remove the old window
2339 // and then add a new one. This call will tell window manager about this, so it can
2340 // preserve the old window until the new one is drawn. This prevents having a gap
2341 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002342 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002343 // Note here we always set the replacing window first, as the flags might be needed
2344 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002345 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002346 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002347
2348 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002349 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002350 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002351 try {
2352 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002353 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002354 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002355
Wale Ogunwale961f4852016-02-01 20:25:54 -08002356 if (!animate) {
2357 stack.mNoAnimActivities.add(topActivity);
2358 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002359
Wale Ogunwale961f4852016-02-01 20:25:54 -08002360 // We might trigger a configuration change. Save the current task bounds for freezing.
2361 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2362
2363 // Make sure the task has the appropriate bounds/size for the stack it is in.
2364 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002365 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2366 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002367 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2368 Rect bounds = task.getLaunchBounds();
2369 if (bounds == null) {
2370 stack.layoutTaskInStack(task, null);
2371 bounds = task.mBounds;
2372 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002373 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2374 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002375 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002376 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2377 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002378 }
2379 } finally {
2380 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002381 }
2382
2383 if (mightReplaceWindow) {
2384 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2385 // window), we need to clear the replace window settings. Otherwise, we schedule a
2386 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002387 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002388 }
2389
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002390 if (!deferResume) {
2391
2392 // The task might have already been running and its visibility needs to be synchronized with
2393 // the visibility of the stack / windows.
2394 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2395 resumeFocusedStackTopActivityLocked();
2396 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002397
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002398 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002399
2400 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002401 }
2402
Wale Ogunwale079a0042015-10-24 11:44:07 -07002403 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2404 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2405 if (stack == null) {
2406 throw new IllegalArgumentException(
2407 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2408 }
2409
2410 final ActivityRecord r = stack.topRunningActivityLocked();
2411 if (r == null) {
2412 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2413 + " in stack=" + stack);
2414 return false;
2415 }
2416
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002417 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002418 Slog.w(TAG,
2419 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002420 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002421 return false;
2422 }
2423
Wale Ogunwale480dca02016-02-06 13:58:29 -08002424 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002425 return true;
2426 }
2427
Wale Ogunwale480dca02016-02-06 13:58:29 -08002428 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2429 mWindowManager.deferSurfaceLayout();
2430 try {
2431 final TaskRecord task = r.task;
2432
2433 // Need to make sure the pinned stack exist so we can resize it below...
2434 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2435
2436 // Resize the pinned stack to match the current size of the task the activity we are
2437 // going to be moving is currently contained in. We do this to have the right starting
2438 // animation bounds for the pinned stack to the desired bounds the caller wants.
2439 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2440 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2441 true /* allowResizeInDockedMode */);
2442
2443 if (task.mActivities.size() == 1) {
2444 // There is only one activity in the task. So, we can just move the task over to
2445 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002446 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2447 // Move the home stack forward if the task we just moved to the pinned stack
2448 // was launched from home so home should be visible behind it.
2449 moveHomeStackToFront(reason);
2450 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002451 moveTaskToStackLocked(
2452 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2453 } else {
2454 stack.moveActivityToStack(r);
2455 }
2456 } finally {
2457 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002458 }
2459
Wale Ogunwale480dca02016-02-06 13:58:29 -08002460 // The task might have already been running and its visibility needs to be synchronized
2461 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002462 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002463 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002464
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002465 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002466 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002467 }
2468
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002469 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2470 final TaskRecord task = anyTaskForIdLocked(taskId);
2471 if (task == null) {
2472 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2473 return;
2474 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002475 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2476
2477 task.updateOverrideConfigurationForStack(stack);
2478
2479 mWindowManager.positionTaskInStack(
2480 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002481 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002482 // The task might have already been running and its visibility needs to be synchronized with
2483 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002484 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2485 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002486 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002487 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002488 }
2489
Craig Mautnerac6f8432013-07-17 13:24:59 -07002490 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002491 mTmpFindTaskResult.r = null;
2492 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002493 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2495 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002496 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2497 final ActivityStack stack = stacks.get(stackNdx);
2498 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002499 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002500 continue;
2501 }
2502 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002503 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2504 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002505 continue;
2506 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002507 stack.findTaskLocked(r, mTmpFindTaskResult);
2508 // It is possible to have task in multiple stacks with the same root affinity.
2509 // If the match we found was based on root affinity we keep on looking to see if
2510 // there is a better match in another stack. We eventually return the match based
2511 // on root affinity if we don't find a better match.
2512 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2513 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002514 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002515 }
2516 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002517 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2518 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 }
2520
2521 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2525 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2526 if (ar != null) {
2527 return ar;
2528 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002529 }
2530 }
2531 return null;
2532 }
2533
Craig Mautner8d341ef2013-03-26 09:03:27 -07002534 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002535 scheduleSleepTimeout();
2536 if (!mGoingToSleep.isHeld()) {
2537 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002538 if (mLaunchingActivity.isHeld()) {
2539 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2540 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002541 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002542 mLaunchingActivity.release();
2543 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002544 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002545 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002546 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 }
2548
2549 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002550 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002551
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002552 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002553 final long endTime = System.currentTimeMillis() + timeout;
2554 while (true) {
2555 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002556 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2557 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2559 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2560 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002561 }
2562 if (cantShutdown) {
2563 long timeRemaining = endTime - System.currentTimeMillis();
2564 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002565 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002566 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002567 } catch (InterruptedException e) {
2568 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002569 } else {
2570 Slog.w(TAG, "Activity manager shutdown timed out");
2571 timedout = true;
2572 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002573 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002574 } else {
2575 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002576 }
2577 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002578
2579 // Force checkReadyForSleep to complete.
2580 mSleepTimeout = true;
2581 checkReadyForSleepLocked();
2582
Craig Mautner8d341ef2013-03-26 09:03:27 -07002583 return timedout;
2584 }
2585
2586 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002587 removeSleepTimeouts();
2588 if (mGoingToSleep.isHeld()) {
2589 mGoingToSleep.release();
2590 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2592 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002593 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2594 final ActivityStack stack = stacks.get(stackNdx);
2595 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002596 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002597 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002598 }
Craig Mautner5314a402013-09-26 12:40:16 -07002599 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002600 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002601 mGoingToSleepActivities.clear();
2602 }
2603
2604 void activitySleptLocked(ActivityRecord r) {
2605 mGoingToSleepActivities.remove(r);
2606 checkReadyForSleepLocked();
2607 }
2608
2609 void checkReadyForSleepLocked() {
2610 if (!mService.isSleepingOrShuttingDown()) {
2611 // Do not care.
2612 return;
2613 }
2614
2615 if (!mSleepTimeout) {
2616 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2618 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2620 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2621 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002622 }
2623
2624 if (mStoppingActivities.size() > 0) {
2625 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002626 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002627 + mStoppingActivities.size() + " activities");
2628 scheduleIdleLocked();
2629 dontSleep = true;
2630 }
2631
2632 if (mGoingToSleepActivities.size() > 0) {
2633 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002634 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002635 + mGoingToSleepActivities.size() + " activities");
2636 dontSleep = true;
2637 }
2638
2639 if (dontSleep) {
2640 return;
2641 }
2642 }
2643
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2647 stacks.get(stackNdx).goToSleep();
2648 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002649 }
2650
2651 removeSleepTimeouts();
2652
2653 if (mGoingToSleep.isHeld()) {
2654 mGoingToSleep.release();
2655 }
2656 if (mService.mShuttingDown) {
2657 mService.notifyAll();
2658 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 }
2660
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002661 boolean reportResumedActivityLocked(ActivityRecord r) {
2662 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002663 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002664 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002665 }
2666 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002667 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002668 mWindowManager.executeAppTransition();
2669 return true;
2670 }
2671 return false;
2672 }
2673
Craig Mautner8d341ef2013-03-26 09:03:27 -07002674 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002675 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2676 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002677 int stackNdx = stacks.size() - 1;
2678 while (stackNdx >= 0) {
2679 stacks.get(stackNdx).handleAppCrashLocked(app);
2680 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002681 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002682 }
2683 }
2684
Jose Lima4b6c6692014-08-12 17:41:12 -07002685 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002686 final ActivityStack stack = r.task.stack;
2687 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002688 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2689 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002690 return false;
2691 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002692 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002693 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2694 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002695
2696 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002697 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002698 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002699 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002700 return true;
2701 }
2702
2703 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002704 if (visible && top.fullscreen) {
2705 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002706 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2707 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2708 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2709 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002710 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002711 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2712 // Only the activity set as currently visible behind should actively reset its
2713 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002714 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2715 "requestVisibleBehind: returning visible=" + visible
2716 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2717 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002718 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002719 }
2720
Jose Lima4b6c6692014-08-12 17:41:12 -07002721 stack.setVisibleBehindActivity(visible ? r : null);
2722 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002723 // If there is a translucent home activity, we need to force it stop being translucent,
2724 // because we can't depend on the application to necessarily perform that operation.
2725 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002726 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002727 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002728 mService.convertFromTranslucent(next.appToken);
2729 }
2730 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002731 if (top.app != null && top.app.thread != null) {
2732 // Notify the top app of the change.
2733 try {
2734 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2735 } catch (RemoteException e) {
2736 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002737 }
2738 return true;
2739 }
2740
Craig Mautnerbb742462014-07-07 15:28:55 -07002741 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2742 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002743 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002744 final ActivityStack stack = task.stack;
2745
2746 r.mLaunchTaskBehind = false;
2747 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002748 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002749 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002750 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002751
2752 // When launching tasks behind, update the last active time of the top task after the new
2753 // task has been shown briefly
2754 final ActivityRecord top = stack.topActivity();
2755 if (top != null) {
2756 top.task.touchActiveTime();
2757 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002758 }
2759
2760 void scheduleLaunchTaskBehindComplete(IBinder token) {
2761 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2762 }
2763
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002764 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2765 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002766 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002769 final int topStackNdx = stacks.size() - 1;
2770 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2771 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002772 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002773 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002774 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002775 }
2776
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002777 void invalidateTaskLayers() {
2778 mTaskLayersChanged = true;
2779 }
2780
2781 void rankTaskLayersIfNeeded() {
2782 if (!mTaskLayersChanged) {
2783 return;
2784 }
2785 mTaskLayersChanged = false;
2786 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2787 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2788 int baseLayer = 0;
2789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2790 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2791 }
2792 }
2793 }
2794
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002795 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2796 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2797 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2798 final int topStackNdx = stacks.size() - 1;
2799 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2800 final ActivityStack stack = stacks.get(stackNdx);
2801 stack.clearOtherAppTimeTrackers(except);
2802 }
2803 }
2804 }
2805
Craig Mautner8d341ef2013-03-26 09:03:27 -07002806 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 final int numStacks = stacks.size();
2810 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2811 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002812 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002813 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
2815 }
2816
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002817 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2818 // Examine all activities currently running in the process.
2819 TaskRecord firstTask = null;
2820 // Tasks is non-null only if two or more tasks are found.
2821 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002822 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2823 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002824 ActivityRecord r = app.activities.get(i);
2825 // First, if we find an activity that is in the process of being destroyed,
2826 // then we just aren't going to do anything for now; we want things to settle
2827 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002828 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002830 return;
2831 }
2832 // Don't consider any activies that are currently not in a state where they
2833 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002834 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2835 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002836 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002837 continue;
2838 }
2839 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002840 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002841 + " from " + r);
2842 if (firstTask == null) {
2843 firstTask = r.task;
2844 } else if (firstTask != r.task) {
2845 if (tasks == null) {
2846 tasks = new ArraySet<>();
2847 tasks.add(firstTask);
2848 }
2849 tasks.add(r.task);
2850 }
2851 }
2852 }
2853 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002854 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002855 return;
2856 }
2857 // If we have activities in multiple tasks that are in a position to be destroyed,
2858 // let's iterate through the tasks and release the oldest one.
2859 final int numDisplays = mActivityDisplays.size();
2860 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2861 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2862 // Step through all stacks starting from behind, to hit the oldest things first.
2863 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2864 final ActivityStack stack = stacks.get(stackNdx);
2865 // Try to release activities in this stack; if we manage to, we are done.
2866 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2867 return;
2868 }
2869 }
2870 }
2871 }
2872
Amith Yamasani37a40c22015-06-17 13:25:42 -07002873 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002874 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002875 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002876 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002877
Craig Mautner858d8a62013-04-23 17:08:34 -07002878 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002879 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2880 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002882 final ActivityStack stack = stacks.get(stackNdx);
2883 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002884 TaskRecord task = stack.topTask();
2885 if (task != null) {
2886 mWindowManager.moveTaskToTop(task.taskId);
2887 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002889 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002890
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002891 ActivityStack stack = getStack(restoreStackId);
2892 if (stack == null) {
2893 stack = mHomeStack;
2894 }
2895 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002896 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002897 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 } else {
2899 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002900 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002901 }
Craig Mautner93529a42013-10-04 15:03:13 -07002902 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002903 }
2904
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002905 /** Checks whether the userid is a profile of the current user. */
2906 boolean isCurrentProfileLocked(int userId) {
2907 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002908 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002909 }
2910
Chong Zhang45c25ce2015-08-10 22:18:26 -07002911 /** Checks whether the activity should be shown for current user. */
2912 boolean okToShowLocked(ActivityRecord r) {
2913 return r != null && (isCurrentProfileLocked(r.userId)
2914 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2915 }
2916
Craig Mautnerde4ef022013-04-07 19:01:33 -07002917 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002918 ArrayList<ActivityRecord> stops = null;
2919
2920 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002921 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2922 ActivityRecord s = mStoppingActivities.get(activityNdx);
2923 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002924 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002925 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2926 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002927 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002928 if (s.finishing) {
2929 // If this activity is finishing, it is sitting on top of
2930 // everyone else but we now know it is no longer needed...
2931 // so get rid of it. Otherwise, we need to go through the
2932 // normal flow and hide it once we determine that it is
2933 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002934 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002935 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002936 }
2937 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002938 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002939 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002940 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002941 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002942 }
2943 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002944 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002945 }
2946 }
2947
2948 return stops;
2949 }
2950
Craig Mautnercf910b02013-04-23 11:23:27 -07002951 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2953 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2955 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002956 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002957 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002958 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002959 if (r == null) Slog.e(TAG,
2960 "validateTop...: null top activity, stack=" + stack);
2961 else {
2962 final ActivityRecord pausing = stack.mPausingActivity;
2963 if (pausing != null && pausing == r) Slog.e(TAG,
2964 "validateTop...: top stack has pausing activity r=" + r
2965 + " state=" + state);
2966 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2967 "validateTop...: activity in front not resumed r=" + r
2968 + " state=" + state);
2969 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002970 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002971 final ActivityRecord resumed = stack.mResumedActivity;
2972 if (resumed != null && resumed == r) Slog.e(TAG,
2973 "validateTop...: back stack has resumed activity r=" + r
2974 + " state=" + state);
2975 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2976 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002977 }
2978 }
2979 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002980 }
2981
Craig Mautnere0570202015-05-13 13:06:11 -07002982 private String lockTaskModeToString() {
2983 switch (mLockTaskModeState) {
2984 case LOCK_TASK_MODE_LOCKED:
2985 return "LOCKED";
2986 case LOCK_TASK_MODE_PINNED:
2987 return "PINNED";
2988 case LOCK_TASK_MODE_NONE:
2989 return "NONE";
2990 default: return "unknown=" + mLockTaskModeState;
2991 }
2992 }
2993
Craig Mautner27084302013-03-25 08:05:25 -07002994 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002995 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002996 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002997 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002998 pw.print(prefix);
2999 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003000 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003001 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003002 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3003 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3004 if (packages.size() > 0) {
3005 pw.println(" mLockTaskPackages (userId:packages)=");
3006 for (int i = 0; i < packages.size(); ++i) {
3007 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3008 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3009 }
3010 }
3011 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003012 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003013
Craig Mautner20e72272013-04-01 13:45:53 -07003014 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003015 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003016 }
3017
Dianne Hackborn390517b2013-05-30 15:03:32 -07003018 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3019 boolean needSep, String prefix) {
3020 if (activity != null) {
3021 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3022 if (needSep) {
3023 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003024 }
3025 pw.print(prefix);
3026 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003027 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003028 }
3029 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003030 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003031 }
3032
Craig Mautner8d341ef2013-03-26 09:03:27 -07003033 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3034 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003035 boolean printed = false;
3036 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003037 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3038 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003039 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003040 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003041 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003042 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003043 final ActivityStack stack = stacks.get(stackNdx);
3044 StringBuilder stackHeader = new StringBuilder(128);
3045 stackHeader.append(" Stack #");
3046 stackHeader.append(stack.mStackId);
3047 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003048 stackHeader.append("\n");
3049 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3050 stackHeader.append("\n");
3051 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003052 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3053 needSep, stackHeader.toString());
3054 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3055 !dumpAll, false, dumpPackage, true,
3056 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057
Craig Mautner4a1cb222013-12-04 16:14:06 -08003058 needSep = printed;
3059 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3060 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003061 if (pr) {
3062 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003063 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003064 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3066 " mResumedActivity: ");
3067 if (pr) {
3068 printed = true;
3069 needSep = false;
3070 }
3071 if (dumpAll) {
3072 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3073 " mLastPausedActivity: ");
3074 if (pr) {
3075 printed = true;
3076 needSep = true;
3077 }
3078 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3079 needSep, " mLastNoHistoryActivity: ");
3080 }
3081 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003082 }
3083 }
3084
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003085 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3086 false, dumpPackage, true, " Activities waiting to finish:", null);
3087 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3088 false, dumpPackage, true, " Activities waiting to stop:", null);
3089 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3090 false, dumpPackage, true, " Activities waiting for another to become visible:",
3091 null);
3092 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3093 false, dumpPackage, true, " Activities waiting to sleep:", null);
3094 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3095 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003096
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003097 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003098 }
3099
Dianne Hackborn390517b2013-05-30 15:03:32 -07003100 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003101 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003102 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003103 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003104 String innerPrefix = null;
3105 String[] args = null;
3106 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003107 for (int i=list.size()-1; i>=0; i--) {
3108 final ActivityRecord r = list.get(i);
3109 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3110 continue;
3111 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003112 if (innerPrefix == null) {
3113 innerPrefix = prefix + " ";
3114 args = new String[0];
3115 }
3116 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003117 final boolean full = !brief && (complete || !r.isInHistory());
3118 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003119 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 needNL = false;
3121 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003122 if (header1 != null) {
3123 pw.println(header1);
3124 header1 = null;
3125 }
3126 if (header2 != null) {
3127 pw.println(header2);
3128 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003129 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003130 if (lastTask != r.task) {
3131 lastTask = r.task;
3132 pw.print(prefix);
3133 pw.print(full ? "* " : " ");
3134 pw.println(lastTask);
3135 if (full) {
3136 lastTask.dump(pw, prefix + " ");
3137 } else if (complete) {
3138 // Complete + brief == give a summary. Isn't that obvious?!?
3139 if (lastTask.intent != null) {
3140 pw.print(prefix); pw.print(" ");
3141 pw.println(lastTask.intent.toInsecureStringWithClip());
3142 }
3143 }
3144 }
3145 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3146 pw.print(" #"); pw.print(i); pw.print(": ");
3147 pw.println(r);
3148 if (full) {
3149 r.dump(pw, innerPrefix);
3150 } else if (complete) {
3151 // Complete + brief == give a summary. Isn't that obvious?!?
3152 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3153 if (r.app != null) {
3154 pw.print(innerPrefix); pw.println(r.app);
3155 }
3156 }
3157 if (client && r.app != null && r.app.thread != null) {
3158 // flush anything that is already in the PrintWriter since the thread is going
3159 // to write to the file descriptor directly
3160 pw.flush();
3161 try {
3162 TransferPipe tp = new TransferPipe();
3163 try {
3164 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3165 r.appToken, innerPrefix, args);
3166 // Short timeout, since blocking here can
3167 // deadlock with the application.
3168 tp.go(fd, 2000);
3169 } finally {
3170 tp.kill();
3171 }
3172 } catch (IOException e) {
3173 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3174 } catch (RemoteException e) {
3175 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3176 }
3177 needNL = true;
3178 }
3179 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003180 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003181 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003182
Craig Mautnerf3333272013-04-22 10:55:53 -07003183 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003184 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3185 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003186 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3187 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003188 }
3189
3190 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003191 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003192 }
3193
3194 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003195 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3196 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003197 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3198 }
3199
Craig Mautner05d29032013-05-03 13:40:13 -07003200 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003201 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3202 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3203 }
Craig Mautner05d29032013-05-03 13:40:13 -07003204 }
3205
Craig Mautner0eea92c2013-05-16 13:35:39 -07003206 void removeSleepTimeouts() {
3207 mSleepTimeout = false;
3208 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3209 }
3210
3211 final void scheduleSleepTimeout() {
3212 removeSleepTimeouts();
3213 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3214 }
3215
Craig Mautner4a1cb222013-12-04 16:14:06 -08003216 @Override
3217 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003218 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003219 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3220 }
3221
3222 @Override
3223 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003224 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3226 }
3227
3228 @Override
3229 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003230 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3232 }
3233
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003234 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003235 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003236 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003237 newDisplay = mActivityDisplays.get(displayId) == null;
3238 if (newDisplay) {
3239 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003240 if (activityDisplay.mDisplay == null) {
3241 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3242 return;
3243 }
Craig Mautner4504de52013-12-20 09:06:56 -08003244 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003245 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003246 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003247 }
Craig Mautner4504de52013-12-20 09:06:56 -08003248 if (newDisplay) {
3249 mWindowManager.onDisplayAdded(displayId);
3250 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003251 }
3252
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003253 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3254 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3255 return;
3256 }
3257 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3258 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3259 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3260 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3261 }
3262
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003263 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003264 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003265 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3266 if (activityDisplay != null) {
3267 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003268 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003269 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003270 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003271 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003272 }
3273 }
3274 mWindowManager.onDisplayRemoved(displayId);
3275 }
3276
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003277 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003279 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3280 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003281 // TODO: Update the bounds.
3282 }
3283 }
3284 mWindowManager.onDisplayChanged(displayId);
3285 }
3286
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003287 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003288 StackInfo info = new StackInfo();
3289 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3290 info.displayId = Display.DEFAULT_DISPLAY;
3291 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003292 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003293
3294 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3295 final int numTasks = tasks.size();
3296 int[] taskIds = new int[numTasks];
3297 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003298 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003299 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 for (int i = 0; i < numTasks; ++i) {
3301 final TaskRecord task = tasks.get(i);
3302 taskIds[i] = task.taskId;
3303 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3304 : task.realActivity != null ? task.realActivity.flattenToString()
3305 : task.getTopActivity() != null ? task.getTopActivity().packageName
3306 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003307 taskBounds[i] = new Rect();
3308 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003309 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003310 }
3311 info.taskIds = taskIds;
3312 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003313 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003314 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 return info;
3316 }
3317
3318 StackInfo getStackInfoLocked(int stackId) {
3319 ActivityStack stack = getStack(stackId);
3320 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003321 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 }
3323 return null;
3324 }
3325
3326 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003327 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003328 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3329 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003330 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003331 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 }
3333 }
3334 return list;
3335 }
3336
Craig Mautner15df08a2015-04-01 12:17:18 -07003337 TaskRecord getLockedTaskLocked() {
3338 final int top = mLockTaskModeTasks.size() - 1;
3339 if (top >= 0) {
3340 return mLockTaskModeTasks.get(top);
3341 }
3342 return null;
3343 }
3344
3345 boolean isLockedTask(TaskRecord task) {
3346 return mLockTaskModeTasks.contains(task);
3347 }
3348
3349 boolean isLastLockedTask(TaskRecord task) {
3350 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3351 }
3352
3353 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003354 if (!mLockTaskModeTasks.remove(task)) {
3355 return;
3356 }
3357 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3358 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003359 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003360 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3361 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003362 final Message lockTaskMsg = Message.obtain();
3363 lockTaskMsg.arg1 = task.userId;
3364 lockTaskMsg.what = LOCK_TASK_END_MSG;
3365 mHandler.sendMessage(lockTaskMsg);
3366 }
3367 }
3368
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003369 void showNonResizeableDockToastIfNeeded(
3370 TaskRecord task, int preferredStackId, int actualStackId) {
3371 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3372 return;
3373 }
3374
Jorim Jaggi2adba072016-03-03 13:43:39 +01003375 if (!task.canGoInDockedStack()) {
3376 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003377 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003378 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3379 String packageName = task.getTopActivity() != null
3380 ? task.getTopActivity().appInfo.packageName : null;
3381 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3382 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003383 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003384 }
3385
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003386 void showLockTaskToast() {
3387 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003388 }
3389
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003390 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3391 if (mLockTaskModeTasks.contains(task)) {
3392 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3393 }
3394 }
3395
Craig Mautner432f64e2015-05-20 14:59:57 -07003396 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3397 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003398 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003399 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003400 final TaskRecord lockedTask = getLockedTaskLocked();
3401 if (lockedTask != null) {
3402 removeLockedTaskLocked(lockedTask);
3403 if (!mLockTaskModeTasks.isEmpty()) {
3404 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003405 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3406 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003407 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003408 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003409 return;
3410 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003411 }
Craig Mautnere0570202015-05-13 13:06:11 -07003412 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3413 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003414 return;
3415 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003416
3417 // Should have already been checked, but do it again.
3418 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003419 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3420 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003421 return;
3422 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003423 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003424 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003425 return;
3426 }
3427
3428 if (mLockTaskModeTasks.isEmpty()) {
3429 // First locktask.
3430 final Message lockTaskMsg = Message.obtain();
3431 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3432 lockTaskMsg.arg1 = task.userId;
3433 lockTaskMsg.what = LOCK_TASK_START_MSG;
3434 lockTaskMsg.arg2 = lockTaskModeState;
3435 mHandler.sendMessage(lockTaskMsg);
3436 }
3437 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003438 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3439 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003440 mLockTaskModeTasks.remove(task);
3441 mLockTaskModeTasks.add(task);
3442
3443 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003444 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003445 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003446
3447 if (andResume) {
3448 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003449 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003450 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003451 }
3452
3453 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003454 return isLockTaskModeViolation(task, false);
3455 }
3456
3457 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3458 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003459 return false;
3460 }
3461 final int lockTaskAuth = task.mLockTaskAuth;
3462 switch (lockTaskAuth) {
3463 case LOCK_TASK_AUTH_DONT_LOCK:
3464 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003465 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003466 case LOCK_TASK_AUTH_LAUNCHABLE:
3467 case LOCK_TASK_AUTH_WHITELISTED:
3468 return false;
3469 case LOCK_TASK_AUTH_PINNABLE:
3470 // Pinnable tasks can't be launched on top of locktask tasks.
3471 return !mLockTaskModeTasks.isEmpty();
3472 default:
3473 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3474 return true;
3475 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003476 }
3477
Craig Mautner15df08a2015-04-01 12:17:18 -07003478 void onLockTaskPackagesUpdatedLocked() {
3479 boolean didSomething = false;
3480 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3481 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003482 final boolean wasWhitelisted =
3483 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3484 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003485 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003486 final boolean isWhitelisted =
3487 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3488 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3489 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003490 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003491 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3492 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003493 removeLockedTaskLocked(lockedTask);
3494 lockedTask.performClearTaskLocked();
3495 didSomething = true;
3496 }
3497 }
3498 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3499 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3500 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3501 final ActivityStack stack = stacks.get(stackNdx);
3502 stack.onLockTaskPackagesUpdatedLocked();
3503 }
3504 }
Craig Mautnere0570202015-05-13 13:06:11 -07003505 final ActivityRecord r = topRunningActivityLocked();
3506 final TaskRecord task = r != null ? r.task : null;
3507 if (mLockTaskModeTasks.isEmpty() && task != null
3508 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3509 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003510 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3511 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3512 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3513 false);
3514 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003515 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003516 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003517 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003518 }
3519 }
3520
Benjamin Franz43261142015-02-11 15:59:44 +00003521 int getLockTaskModeState() {
3522 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003523 }
3524
Jorim Jaggife89d122015-12-22 16:28:44 +01003525 void activityRelaunchedLocked(IBinder token) {
3526 mWindowManager.notifyAppRelaunchingFinished(token);
3527 }
3528
3529 void activityRelaunchingLocked(ActivityRecord r) {
3530 mWindowManager.notifyAppRelaunching(r.appToken);
3531 }
3532
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003533 void logStackState() {
3534 mActivityMetricsLogger.logWindowState();
3535 }
3536
Andrii Kulian933076d2016-03-29 17:04:42 -07003537 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003538 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3539 final ActivityRecord r = task.mActivities.get(i);
3540 if (r.app != null && r.app.thread != null) {
3541 mMultiWindowModeChangedActivities.add(r);
3542 }
3543 }
3544
3545 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3546 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3547 }
3548 }
3549
Andrii Kulian933076d2016-03-29 17:04:42 -07003550 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003551 final ActivityStack stack = task.stack;
3552 if (prevStack == null || prevStack == stack
3553 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3554 return;
3555 }
3556
3557 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3558 final ActivityRecord r = task.mActivities.get(i);
3559 if (r.app != null && r.app.thread != null) {
3560 mPipModeChangedActivities.add(r);
3561 }
3562 }
3563
3564 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3565 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3566 }
3567 }
3568
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003569 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003570
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003571 public ActivityStackSupervisorHandler(Looper looper) {
3572 super(looper);
3573 }
3574
Craig Mautnerf3333272013-04-22 10:55:53 -07003575 void activityIdleInternal(ActivityRecord r) {
3576 synchronized (mService) {
3577 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3578 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003579 }
3580
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003581 @Override
3582 public void handleMessage(Message msg) {
3583 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003584 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3585 synchronized (mService) {
3586 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3587 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003588 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003589 }
3590 }
3591 } break;
3592 case REPORT_PIP_MODE_CHANGED_MSG: {
3593 synchronized (mService) {
3594 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3595 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003596 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003597 }
3598 }
3599 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003600 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003601 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3602 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003603 if (mService.mDidDexOpt) {
3604 mService.mDidDexOpt = false;
3605 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3606 nmsg.obj = msg.obj;
3607 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3608 return;
3609 }
3610 // We don't at this point know if the activity is fullscreen,
3611 // so we need to be conservative and assume it isn't.
3612 activityIdleInternal((ActivityRecord)msg.obj);
3613 } break;
3614 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003615 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003616 activityIdleInternal((ActivityRecord)msg.obj);
3617 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003618 case RESUME_TOP_ACTIVITY_MSG: {
3619 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003620 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003621 }
3622 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003623 case SLEEP_TIMEOUT_MSG: {
3624 synchronized (mService) {
3625 if (mService.isSleepingOrShuttingDown()) {
3626 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3627 mSleepTimeout = true;
3628 checkReadyForSleepLocked();
3629 }
3630 }
3631 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003632 case LAUNCH_TIMEOUT_MSG: {
3633 if (mService.mDidDexOpt) {
3634 mService.mDidDexOpt = false;
3635 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3636 return;
3637 }
3638 synchronized (mService) {
3639 if (mLaunchingActivity.isHeld()) {
3640 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3641 if (VALIDATE_WAKE_LOCK_CALLER
3642 && Binder.getCallingUid() != Process.myUid()) {
3643 throw new IllegalStateException("Calling must be system uid");
3644 }
3645 mLaunchingActivity.release();
3646 }
3647 }
3648 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003650 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003651 } break;
3652 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003653 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003654 } break;
3655 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003656 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003657 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003658 case CONTAINER_CALLBACK_VISIBILITY: {
3659 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003660 final IActivityContainerCallback callback = container.mCallback;
3661 if (callback != null) {
3662 try {
3663 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3664 } catch (RemoteException e) {
3665 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003666 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003667 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003668 case LOCK_TASK_START_MSG: {
3669 // When lock task starts, we disable the status bars.
3670 try {
Jason Monk62515be2014-05-21 16:06:19 -04003671 if (mLockTaskNotify == null) {
3672 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003673 }
Jason Monk62515be2014-05-21 16:06:19 -04003674 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003675 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003676 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003677 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003678 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003679 flags = StatusBarManager.DISABLE_MASK
3680 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003681 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003682 flags = StatusBarManager.DISABLE_MASK
3683 & (~StatusBarManager.DISABLE_BACK)
3684 & (~StatusBarManager.DISABLE_HOME)
3685 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003686 }
3687 getStatusBarService().disable(flags, mToken,
3688 mService.mContext.getPackageName());
3689 }
3690 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003691 if (getDevicePolicyManager() != null) {
3692 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003693 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003694 }
justinzhang5286d3f2014-05-12 17:06:01 -04003695 } catch (RemoteException ex) {
3696 throw new RuntimeException(ex);
3697 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003698 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003699 case LOCK_TASK_END_MSG: {
3700 // When lock task ends, we enable the status bars.
3701 try {
Jason Monk62515be2014-05-21 16:06:19 -04003702 if (getStatusBarService() != null) {
3703 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3704 mService.mContext.getPackageName());
3705 }
3706 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003707 if (getDevicePolicyManager() != null) {
3708 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3709 msg.arg1);
3710 }
Jason Monk62515be2014-05-21 16:06:19 -04003711 if (mLockTaskNotify == null) {
3712 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3713 }
3714 mLockTaskNotify.show(false);
3715 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003716 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003717 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003718 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003719 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003720 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003721 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003722 new LockPatternUtils(mService.mContext)
3723 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003724 }
3725 } catch (SettingNotFoundException e) {
3726 // No setting, don't lock.
3727 }
justinzhang5286d3f2014-05-12 17:06:01 -04003728 } catch (RemoteException ex) {
3729 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003730 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003731 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003732 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003733 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003734 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3735 if (mLockTaskNotify == null) {
3736 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3737 }
3738 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3739 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003740 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3741 final ActivityContainer container = (ActivityContainer) msg.obj;
3742 final IActivityContainerCallback callback = container.mCallback;
3743 if (callback != null) {
3744 try {
3745 callback.onAllActivitiesComplete(container.asBinder());
3746 } catch (RemoteException e) {
3747 }
3748 }
3749 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003750 case LAUNCH_TASK_BEHIND_COMPLETE: {
3751 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003752 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003753 if (r != null) {
3754 handleLaunchTaskBehindCompleteLocked(r);
3755 }
3756 }
3757 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003758
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003759 }
3760 }
3761 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003762
Ying Wangb081a592014-04-22 15:20:16 -07003763 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003764 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3765 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003766 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003767 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003769 ActivityRecord mParentActivity = null;
3770 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003771
Craig Mautnere3a00d72014-04-16 08:31:19 -07003772 boolean mVisible = true;
3773
Craig Mautner4a1cb222013-12-04 16:14:06 -08003774 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003775 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003777 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3778 final static int CONTAINER_STATE_NO_SURFACE = 1;
3779 final static int CONTAINER_STATE_FINISHING = 2;
3780 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3781
3782 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003783 synchronized (mService) {
3784 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003785 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003786 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003787 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003788 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003789 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003790
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003791 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003792 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003793 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003794 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003795 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003796 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 }
3798
3799 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003800 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003802 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3803 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 return;
3805 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003806 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003807 }
3808 }
3809
3810 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003811 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003812 synchronized (mService) {
3813 if (mActivityDisplay != null) {
3814 return mActivityDisplay.mDisplayId;
3815 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003816 }
3817 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003818 }
3819
Jeff Brownca9bc702014-02-11 14:32:56 -08003820 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003821 public int getStackId() {
3822 synchronized (mService) {
3823 return mStackId;
3824 }
3825 }
3826
3827 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003828 public boolean injectEvent(InputEvent event) {
3829 final long origId = Binder.clearCallingIdentity();
3830 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003831 synchronized (mService) {
3832 if (mActivityDisplay != null) {
3833 return mInputManagerInternal.injectInputEvent(event,
3834 mActivityDisplay.mDisplayId,
3835 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3836 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003837 }
3838 return false;
3839 } finally {
3840 Binder.restoreCallingIdentity(origId);
3841 }
3842 }
3843
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003844 @Override
3845 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003846 synchronized (mService) {
3847 if (mContainerState == CONTAINER_STATE_FINISHING) {
3848 return;
3849 }
3850 mContainerState = CONTAINER_STATE_FINISHING;
3851
Craig Mautnerd163e752014-06-13 17:18:47 -07003852 long origId = Binder.clearCallingIdentity();
3853 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003854 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003855 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003856 } finally {
3857 Binder.restoreCallingIdentity(origId);
3858 }
3859 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003860 }
3861
Craig Mautner60257702014-09-17 15:02:33 -07003862 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003863 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003864 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003865 if (mActivityDisplay != null) {
3866 mActivityDisplay.detachActivitiesLocked(mStack);
3867 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003868 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 }
3870 }
3871
3872 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003873 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003874 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003875 }
3876
3877 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003878 public final int startActivityIntentSender(IIntentSender intentSender)
3879 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003880 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3881
3882 if (!(intentSender instanceof PendingIntentRecord)) {
3883 throw new IllegalArgumentException("Bad PendingIntent object");
3884 }
3885
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003886 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003887 Binder.getCallingUid(), mCurrentUser, false,
3888 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003889
3890 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3891 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3892 pendingIntent.key.requestResolvedType);
3893
3894 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3895 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3896 }
3897
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003898 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003899 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003900 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003901 throw new SecurityException(
3902 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3903 }
3904 }
3905
Craig Mautnerdf88d732014-01-27 09:21:32 -08003906 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 public IBinder asBinder() {
3908 return this;
3909 }
3910
Craig Mautner4504de52013-12-20 09:06:56 -08003911 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003912 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003913 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003914 }
3915
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916 ActivityStackSupervisor getOuter() {
3917 return ActivityStackSupervisor.this;
3918 }
3919
Craig Mautnerd163e752014-06-13 17:18:47 -07003920 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003921 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 }
3923
Craig Mautner6985bad2014-04-21 15:22:06 -07003924 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003925 void setVisible(boolean visible) {
3926 if (mVisible != visible) {
3927 mVisible = visible;
3928 if (mCallback != null) {
3929 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3930 0 /* unused */, this).sendToTarget();
3931 }
3932 }
3933 }
3934
Craig Mautner6985bad2014-04-21 15:22:06 -07003935 void setDrawn() {
3936 }
3937
Craig Mautner1b4bf852014-05-26 15:06:32 -07003938 // You can always start a new task on a regular ActivityStack.
3939 boolean isEligibleForNewTasks() {
3940 return true;
3941 }
3942
Craig Mautnerd163e752014-06-13 17:18:47 -07003943 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003944 detachLocked();
3945 deleteActivityContainer(this);
3946 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003947 }
3948
Craig Mautner34b73df2014-01-12 21:11:08 -08003949 @Override
3950 public String toString() {
3951 return mIdString + (mActivityDisplay == null ? "N" : "A");
3952 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003953 }
3954
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955 private class VirtualActivityContainer extends ActivityContainer {
3956 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003957 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958
3959 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3960 super(getNextStackId());
3961 mParentActivity = parent;
3962 mCallback = callback;
3963 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003964 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003965 }
3966
3967 @Override
3968 public void setSurface(Surface surface, int width, int height, int density) {
3969 super.setSurface(surface, width, height, density);
3970
3971 synchronized (mService) {
3972 final long origId = Binder.clearCallingIdentity();
3973 try {
3974 setSurfaceLocked(surface, width, height, density);
3975 } finally {
3976 Binder.restoreCallingIdentity(origId);
3977 }
3978 }
3979 }
3980
3981 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3982 if (mContainerState == CONTAINER_STATE_FINISHING) {
3983 return;
3984 }
3985 VirtualActivityDisplay virtualActivityDisplay =
3986 (VirtualActivityDisplay) mActivityDisplay;
3987 if (virtualActivityDisplay == null) {
3988 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003989 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003990 mActivityDisplay = virtualActivityDisplay;
3991 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003992 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003993 }
3994
3995 if (mSurface != null) {
3996 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003997 }
3998
Craig Mautner6985bad2014-04-21 15:22:06 -07003999 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004000 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004001 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004002 } else {
4003 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004004 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004005 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004006 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004007 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004008 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004009
Craig Mautnerd163e752014-06-13 17:18:47 -07004010 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004011
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004012 if (DEBUG_STACK) Slog.d(TAG_STACK,
4013 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004014 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015
Craig Mautner6985bad2014-04-21 15:22:06 -07004016 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004017 boolean isAttachedLocked() {
4018 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004019 }
4020
4021 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004022 void setDrawn() {
4023 synchronized (mService) {
4024 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004025 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004026 }
4027 }
4028
Craig Mautner1b4bf852014-05-26 15:06:32 -07004029 // Never start a new task on an ActivityView if it isn't explicitly specified.
4030 @Override
4031 boolean isEligibleForNewTasks() {
4032 return false;
4033 }
4034
Craig Mautnerd163e752014-06-13 17:18:47 -07004035 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004036 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004037 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4038 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4039 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4040 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4041 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004042 }
4043 }
4044
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4046 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004047 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004049 int mDisplayId;
4050 Display mDisplay;
4051 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004052
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4054 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004055 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056
Jose Lima4b6c6692014-08-12 17:41:12 -07004057 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004058
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004059 ActivityDisplay() {
4060 }
Craig Mautner4504de52013-12-20 09:06:56 -08004061
Craig Mautner1a70a162014-09-13 12:09:31 -07004062 // After instantiation, check that mDisplay is not null before using this. The alternative
4063 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004064 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004065 final Display display = mDisplayManager.getDisplay(displayId);
4066 if (display == null) {
4067 return;
4068 }
4069 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004070 }
4071
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004072 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004073 mDisplay = display;
4074 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004076 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004078 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004079 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004080 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4081 + " onTop=" + onTop);
4082 if (onTop) {
4083 mStacks.add(stack);
4084 } else {
4085 mStacks.add(0, stack);
4086 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004087 }
4088
Craig Mautnere0a38842013-12-16 16:14:02 -08004089 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004090 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004091 + " from displayId=" + mDisplayId);
4092 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 }
4094
Jose Lima4b6c6692014-08-12 17:41:12 -07004095 void setVisibleBehindActivity(ActivityRecord r) {
4096 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004097 }
4098
Jose Lima4b6c6692014-08-12 17:41:12 -07004099 boolean hasVisibleBehindActivity() {
4100 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004101 }
4102
Craig Mautner34b73df2014-01-12 21:11:08 -08004103 @Override
4104 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004105 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4106 }
4107 }
4108
4109 class VirtualActivityDisplay extends ActivityDisplay {
4110 VirtualDisplay mVirtualDisplay;
4111
Craig Mautner6985bad2014-04-21 15:22:06 -07004112 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004113 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004114 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4115 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4116 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4117 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004118
4119 init(mVirtualDisplay.getDisplay());
4120
4121 mWindowManager.handleDisplayAdded(mDisplayId);
4122 }
4123
4124 void setSurface(Surface surface) {
4125 if (mVirtualDisplay != null) {
4126 mVirtualDisplay.setSurface(surface);
4127 }
4128 }
4129
4130 @Override
4131 void detachActivitiesLocked(ActivityStack stack) {
4132 super.detachActivitiesLocked(stack);
4133 if (mVirtualDisplay != null) {
4134 mVirtualDisplay.release();
4135 mVirtualDisplay = null;
4136 }
4137 }
4138
4139 @Override
4140 public String toString() {
4141 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004142 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004143 }
Jose Lima58e66d62014-05-27 20:00:27 -07004144
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004145 /**
4146 * Adjust bounds to stay within stack bounds.
4147 *
4148 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4149 * that keep them unchanged, but be contained within the stack bounds.
4150 *
4151 * @param bounds Bounds to be adjusted.
4152 * @param stackBounds Bounds within which the other bounds should remain.
4153 */
4154 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4155 if (stackBounds == null || stackBounds.contains(bounds)) {
4156 return;
4157 }
4158
4159 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4160 final int maxRight = stackBounds.right
4161 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4162 int horizontalDiff = stackBounds.left - bounds.left;
4163 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4164 || (bounds.left + horizontalDiff >= maxRight)) {
4165 horizontalDiff = maxRight - bounds.left;
4166 }
4167 bounds.left += horizontalDiff;
4168 bounds.right += horizontalDiff;
4169 }
4170
4171 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4172 final int maxBottom = stackBounds.bottom
4173 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4174 int verticalDiff = stackBounds.top - bounds.top;
4175 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4176 || (bounds.top + verticalDiff >= maxBottom)) {
4177 verticalDiff = maxBottom - bounds.top;
4178 }
4179 bounds.top += verticalDiff;
4180 bounds.bottom += verticalDiff;
4181 }
4182 }
4183
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004184 ActivityStack findStackBehind(ActivityStack stack) {
4185 // TODO(multi-display): We are only looking for stacks on the default display.
4186 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4187 if (display == null) {
4188 return null;
4189 }
4190 final ArrayList<ActivityStack> stacks = display.mStacks;
4191 for (int i = stacks.size() - 1; i >= 0; i--) {
4192 if (stacks.get(i) == stack && i > 0) {
4193 return stacks.get(i - 1);
4194 }
4195 }
4196 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4197 + " in=" + stacks);
4198 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004199
Jorim Jaggic69bd222016-03-15 14:38:37 +01004200 /**
4201 * Puts a task into resizing mode during the next app transition.
4202 *
4203 * @param taskId the id of the task to put into resizing mode
4204 */
4205 private void setResizingDuringAnimation(int taskId) {
4206 mResizingTasksDuringAnimation.add(taskId);
4207 mWindowManager.setTaskDockedResizing(taskId, true);
4208 }
4209
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004210 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4211 final TaskRecord task;
4212 final int callingUid;
4213 final String callingPackage;
4214 final Intent intent;
4215 final int userId;
4216 final ActivityOptions activityOptions = (bOptions != null)
4217 ? new ActivityOptions(bOptions) : null;
4218 final int launchStackId = (activityOptions != null)
4219 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4220
4221 if (launchStackId == HOME_STACK_ID) {
4222 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4223 + taskId + " can't be launch in the home stack.");
4224 }
4225 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4226 if (task == null) {
4227 throw new IllegalArgumentException(
4228 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4229 }
4230
4231 if (launchStackId != INVALID_STACK_ID) {
4232 if (launchStackId == DOCKED_STACK_ID) {
4233 mWindowManager.setDockedStackCreateState(
4234 activityOptions.getDockCreateMode(), null /* initialBounds */);
4235
4236 // Defer updating the stack in which recents is until the app transition is done, to
4237 // not run into issues where we still need to draw the task in recents but the
4238 // docked stack is already created.
4239 deferUpdateBounds(HOME_STACK_ID);
4240 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4241 }
4242 if (task.stack.mStackId != launchStackId) {
4243 moveTaskToStackLocked(
4244 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4245 ANIMATE);
4246 }
4247 }
4248
4249 // If the user must confirm credentials (e.g. when first launching a work app and the
4250 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4251 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4252 && task.getRootActivity() != null) {
4253 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4254
4255 // If we are launching the task in the docked stack, put it into resizing mode so
4256 // the window renders full-screen with the background filling the void. Also only
4257 // call this at the end to make sure that tasks exists on the window manager side.
4258 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004259 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004260 }
4261 return ActivityManager.START_TASK_TO_FRONT;
4262 }
4263 callingUid = task.mCallingUid;
4264 callingPackage = task.mCallingPackage;
4265 intent = task.intent;
4266 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4267 userId = task.userId;
Jorim Jaggic69bd222016-03-15 14:38:37 +01004268 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4269 null, null, 0, 0, bOptions, userId, null, task);
4270 if (launchStackId == DOCKED_STACK_ID) {
4271 setResizingDuringAnimation(task.taskId);
4272 }
4273 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004274 }
Craig Mautner27084302013-03-25 08:05:25 -07004275}