blob: 22e10b10e149beddfb3a30b759a035ef60b87504 [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 Jaggi2adba072016-03-03 13:43:39 +0100105import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100106import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100107import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100108import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
109import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
110import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
111import static android.app.ActivityManager.RESIZE_MODE_FORCED;
112import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
113import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
114import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
115import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
116import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
117import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
118import static android.app.ActivityManager.StackId.HOME_STACK_ID;
119import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
120import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
121import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
122import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
123import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
124import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500125import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800154import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100155import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100156import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100157import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
158import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
161import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
162import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
163import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
164import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
165import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
166import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
167import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
170import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
171import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
172import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100173import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100174
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800176 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700177 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700179 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700180 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800186 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800188
Craig Mautnerf3333272013-04-22 10:55:53 -0700189 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700191
Craig Mautner0eea92c2013-05-16 13:35:39 -0700192 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700194
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700195 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700197
Craig Mautner05d29032013-05-03 13:40:13 -0700198 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
199 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
200 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700201 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800203 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
204 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
205 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700206 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400207 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
208 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700209 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700210 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700211 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800212 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
213 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216
Jason Monk62515be2014-05-21 16:06:19 -0400217 private static final String LOCK_TASK_TAG = "Lock-to-App";
218
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219 // Used to indicate if an object (e.g. stack) that we are trying to get
220 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800221 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700223 // Used to indicate that windows of activities should be preserved during the resize.
224 static final boolean PRESERVE_WINDOWS = true;
225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 // Used to indicate if an object (e.g. task) should be moved/created
227 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700228 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229
230 // Used to indicate that an objects (e.g. task) removal from its container
231 // (e.g. stack) is due to it moving to another container.
232 static final boolean MOVING = true;
233
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700234 // Force the focus to change to the stack we are moving a task to..
235 static final boolean FORCE_FOCUS = true;
236
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700237 // Restore task from the saved recents if it can't be found in any live stack.
238 static final boolean RESTORE_FROM_RECENTS = true;
239
Svetoslav7008b512015-06-24 18:47:07 -0700240 // Activity actions an app cannot start if it uses a permission which is not granted.
241 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
242 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700243
Svetoslav7008b512015-06-24 18:47:07 -0700244 static {
245 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
246 Manifest.permission.CAMERA);
247 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
248 Manifest.permission.CAMERA);
249 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
250 Manifest.permission.CALL_PHONE);
251 }
252
Svet Ganov99b60432015-06-27 13:15:22 -0700253 /** Action restriction: launching the activity is not restricted. */
254 private static final int ACTIVITY_RESTRICTION_NONE = 0;
255 /** Action restriction: launching the activity is restricted by a permission. */
256 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
257 /** Action restriction: launching the activity is restricted by an app op. */
258 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700259
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700260 // The height/width divide used when fitting a task within a bounds with method
261 // {@link #fitWithinBounds}.
262 // We always want the task to to be visible in the bounds without affecting its size when
263 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
264 // the input bounds right or bottom side minus the width or height divided by this value.
265 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
266
justinzhang5286d3f2014-05-12 17:06:01 -0400267 /** Status Bar Service **/
268 private IBinder mToken = new Binder();
269 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400270 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400271
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700272 // For debugging to make sure the caller when acquiring/releasing our
273 // wake lock is the system process.
274 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800275 /** The number of distinct task ids that can be assigned to the tasks of a single user */
276 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700277
Craig Mautner27084302013-03-25 08:05:25 -0700278 final ActivityManagerService mService;
279
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800280 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800281
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282 final ActivityStackSupervisorHandler mHandler;
283
284 /** Short cut */
285 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800286 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700288 /** Counter for next free stack ID to use for dynamic activity stacks. */
289 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700290
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800291 /**
292 * Maps the task identifier that activities are currently being started in to the userId of the
293 * task. Each time a new task is created, the entry for the userId of the task is incremented
294 */
295 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700296
Craig Mautner2420ead2013-04-01 17:13:20 -0700297 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800298 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700299
Craig Mautnere0a38842013-12-16 16:14:02 -0800300 /** The stack containing the launcher app. Assumed to always be attached to
301 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800302 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700303
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800305 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner4a1cb222013-12-04 16:14:06 -0800307 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
308 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800309 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800310 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
312 /** List of activities that are waiting for a new activity to become visible before completing
313 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318
319 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
Craig Mautnerde4ef022013-04-07 19:01:33 -0700322 /** List of activities that are ready to be stopped, but waiting for the next activity to
323 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325
Craig Mautnerf3333272013-04-22 10:55:53 -0700326 /** List of activities that are ready to be finished, but waiting for the previous activity to
327 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700329
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800331 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700332
Wale Ogunwale22e25262016-02-01 10:32:02 -0800333 /** List of activities whose multi-window mode changed that we need to report to the
334 * application */
335 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
336
337 /** List of activities whose picture-in-picture mode changed that we need to report to the
338 * application */
339 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
340
Craig Mautnerf3333272013-04-22 10:55:53 -0700341 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700342 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700343
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700344 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700345 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700346
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
348 * is being brought in front of us. */
349 boolean mUserLeaving = false;
350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** Set when we have taken too long waiting to go to sleep. */
352 boolean mSleepTimeout = false;
353
354 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355 * We don't want to allow the device to go to sleep while in the process
356 * of launching an activity. This is primarily to allow alarm intent
357 * receivers to launch an activity and get that to run before the device
358 * goes back to sleep.
359 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700360 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700361
362 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 * Set when the system is going to sleep, until we have
364 * successfully paused the current activity and released our wake lock.
365 * At that point the system is allowed to actually sleep.
366 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700367 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700369 /** Stack id of the front stack when user switched, indexed by userId. */
370 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700371
Craig Mautner4504de52013-12-20 09:06:56 -0800372 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800373 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700374 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375
376 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378
Jeff Brownca9bc702014-02-11 14:32:56 -0800379 InputManagerInternal mInputManagerInternal;
380
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
382 * may be finished until there is only one entry left. If this is empty the system is not
383 * in lockTask mode. */
384 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000385 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700386 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
387 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000388 */
389 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400390 /**
391 * Notifies the user when entering/exiting lock-task.
392 */
393 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800394
Wale Ogunwaled046a012015-12-24 13:05:59 -0800395 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800396 boolean inResumeTopActivity;
397
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700398 // 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 -0700399 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700400 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700401
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700402 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
403 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800404 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700405
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700406 // The default minimal size that will be used if the activity doesn't specify its minimal size.
407 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800408 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700410 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
411 private boolean mTaskLayersChanged = true;
412
Jorim Jaggi275561a2016-02-23 10:11:02 -0500413 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800414
Jorim Jaggidc249c42015-12-15 14:57:31 -0800415 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
416
Wale Ogunwale39381972015-12-17 17:15:29 -0800417 static class FindTaskResult {
418 ActivityRecord r;
419 boolean matchedByRootAffinity;
420 }
421 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
422
Craig Mautneree36c772014-07-16 14:56:05 -0700423 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100424 * Used to keep track whether app visibilities got changed since the last pause. Useful to
425 * determine whether to invoke the task stack change listener after pausing.
426 */
427 boolean mAppVisibilitiesChangedSinceLastPause;
428
429 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100430 * Set of tasks that are in resizing mode during an app transition to fill the "void".
431 */
432 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
433
434 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700435 * Description of a request to start a new activity, which has been held
436 * due to app switches being disabled.
437 */
438 static class PendingActivityLaunch {
439 final ActivityRecord r;
440 final ActivityRecord sourceRecord;
441 final int startFlags;
442 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700443 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700444
445 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700446 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700447 r = _r;
448 sourceRecord = _sourceRecord;
449 startFlags = _startFlags;
450 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700451 callerApp = _callerApp;
452 }
453
454 void sendErrorResult(String message) {
455 try {
456 if (callerApp.thread != null) {
457 callerApp.thread.scheduleCrash(message);
458 }
459 } catch (RemoteException e) {
460 Slog.e(TAG, "Exception scheduling crash of failed "
461 + "activity launcher sourceRecord=" + sourceRecord, e);
462 }
Craig Mautneree36c772014-07-16 14:56:05 -0700463 }
464 }
465
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800466 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700467 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700468 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800469 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800470 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700471 }
472
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800473 void setRecentTasks(RecentTasks recentTasks) {
474 mRecentTasks = recentTasks;
475 }
476
Jeff Brown2c43c332014-06-12 22:38:59 -0700477 /**
478 * At the time when the constructor runs, the power manager has not yet been
479 * initialized. So we initialize our wakelocks afterwards.
480 */
481 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800482 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700483 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700484 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700485 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700486 }
487
justinzhang5286d3f2014-05-12 17:06:01 -0400488 // This function returns a IStatusBarService. The value is from ServiceManager.
489 // getService and is cached.
490 private IStatusBarService getStatusBarService() {
491 synchronized (mService) {
492 if (mStatusBarService == null) {
493 mStatusBarService = IStatusBarService.Stub.asInterface(
494 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
495 if (mStatusBarService == null) {
496 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
497 }
498 }
499 return mStatusBarService;
500 }
501 }
502
Jason Monk35c62a42014-06-17 10:24:47 -0400503 private IDevicePolicyManager getDevicePolicyManager() {
504 synchronized (mService) {
505 if (mDevicePolicyManager == null) {
506 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
507 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
508 if (mDevicePolicyManager == null) {
509 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
510 }
511 }
512 return mDevicePolicyManager;
513 }
514 }
515
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700516 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800517 synchronized (mService) {
518 mWindowManager = wm;
519
520 mDisplayManager =
521 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
522 mDisplayManager.registerDisplayListener(this, null);
523
524 Display[] displays = mDisplayManager.getDisplays();
525 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
526 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800527 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700528 if (activityDisplay.mDisplay == null) {
529 throw new IllegalStateException("Default Display does not exist");
530 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800531 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700532 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800533 }
534
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800535 mHomeStack = mFocusedStack = mLastFocusedStack =
536 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800537
538 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 }
Craig Mautner27084302013-03-25 08:05:25 -0700540 }
541
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200542 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700543 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200544 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700545 }
546
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700547 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700549 }
550
Craig Mautnerde4ef022013-04-07 19:01:33 -0700551 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700553 }
554
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700555 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700556 if (stack == null) {
557 return false;
558 }
559
Craig Mautnerdf88d732014-01-27 09:21:32 -0800560 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
561 if (parent != null) {
562 stack = parent.task.stack;
563 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800564 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700565 }
566
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 /** The top most stack. */
568 boolean isFrontStack(ActivityStack stack) {
569 if (stack == null) {
570 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700572
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700573 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
574 if (parent != null) {
575 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800576 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
578 }
579
Wale Ogunwaled046a012015-12-24 13:05:59 -0800580 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800581 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
582 if (!focusCandidate.isFocusable()) {
583 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
584 focusCandidate = focusCandidate.getNextFocusableStackLocked();
585 }
586
587 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800588 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800589 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800590
Wale Ogunwaled046a012015-12-24 13:05:59 -0800591 EventLogTags.writeAmFocusedStack(
592 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
593 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
594 }
595
596 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800597 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800598 // The focus activity should always be the top activity in the focused stack.
599 // There will be chaos and anarchy if it isn't...
600 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
601 }
Craig Mautnerde313752015-01-22 14:28:03 -0800602
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800603 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800604 if (r != null && r.idle) {
605 checkFinishBootingLocked();
606 }
607 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700608 }
609
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700610 void moveHomeStackToFront(String reason) {
611 mHomeStack.moveToFront(reason);
612 }
613
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700614 /** Returns true if the focus activity was adjusted to the home stack top activity. */
615 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700616 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
617 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700618 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700619 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700620
Craig Mautner84984fa2014-06-19 11:19:20 -0700621 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700622
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700623 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700624 if (top == null) {
625 return false;
626 }
627 mService.setFocusedActivityLocked(top, reason);
628 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700629 }
630
Craig Mautner299f9602015-01-26 09:47:33 -0800631 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700632 if (!mService.mBooting && !mService.mBooted) {
633 // Not ready yet!
634 return false;
635 }
636
Craig Mautner84984fa2014-06-19 11:19:20 -0700637 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
638 mWindowManager.showRecentApps();
639 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700640 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700641
Craig Mautner84984fa2014-06-19 11:19:20 -0700642 if (prev != null) {
643 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
644 }
645
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700646 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
647 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800648 final String myReason = reason + " resumeHomeStackTask";
649
Mark Lua56ea122015-10-08 13:31:01 +0800650 // Only resume home activity if isn't finishing.
651 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800652 mService.setFocusedActivityLocked(r, myReason);
653 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700654 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800655 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700656 }
657
Craig Mautner8d341ef2013-03-26 09:03:27 -0700658 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700659 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700660 }
661
662 /**
663 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
664 * @param id Id of the task we would like returned.
665 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
666 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700667 * @param stackId The stack to restore the task to (default launch stack will be used if
668 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700669 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700670 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800671 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 ActivityStack stack = stacks.get(stackNdx);
676 TaskRecord task = stack.taskForIdLocked(id);
677 if (task != null) {
678 return task;
679 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700680 }
681 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800682
683 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700684 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800685 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800686 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700687 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800688 return null;
689 }
690
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700691 if (!restoreFromRecents) {
692 return task;
693 }
694
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700695 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700696 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
697 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800698 return null;
699 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700700 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800701 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700702 }
703
Craig Mautner6170f732013-04-02 13:05:23 -0700704 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800707 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
709 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
710 if (r != null) {
711 return r;
712 }
Craig Mautner6170f732013-04-02 13:05:23 -0700713 }
714 }
715 return null;
716 }
717
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000718 boolean isFocusedUserLockedProfile() {
719 final int userId = mFocusedStack.topRunningActivityLocked().userId;
720 return userId != UserHandle.myUserId()
721 && mService.mUserController.shouldConfirmCredentials(userId);
722 }
723
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800724 void setNextTaskIdForUserLocked(int taskId, int userId) {
725 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
726 if (taskId > currentTaskId) {
727 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700728 }
729 }
730
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800731 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800732 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
733 // for a userId u, a taskId can only be in the range
734 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
735 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
736 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800737 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
738 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
739 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800740 candidateTaskId++;
741 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
742 // Wrap around as there will be smaller task ids that are available now.
743 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800745 if (candidateTaskId == currentTaskId) {
746 // Something wrong!
747 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
748 throw new IllegalStateException("Cannot get an available task id."
749 + " Reached limit of " + MAX_TASK_IDS_PER_USER
750 + " running tasks per user.");
751 }
752 }
753 mCurTaskIdForUser.put(userId, candidateTaskId);
754 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700755 }
756
Craig Mautnerde4ef022013-04-07 19:01:33 -0700757 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800758 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700759 if (stack == null) {
760 return null;
761 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700762 ActivityRecord resumedActivity = stack.mResumedActivity;
763 if (resumedActivity == null || resumedActivity.app == null) {
764 resumedActivity = stack.mPausingActivity;
765 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700766 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 }
768 }
769 return resumedActivity;
770 }
771
Dianne Hackbornff072722014-09-24 10:56:28 -0700772 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700773 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800775 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700779 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 continue;
781 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700782 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 if (hr != null) {
784 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
785 && processName.equals(hr.processName)) {
786 try {
George Mount2c92c972014-03-20 09:38:23 -0700787 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 didSomething = true;
789 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700790 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 Slog.w(TAG, "Exception in new application when starting activity "
792 + hr.intent.getComponent().flattenToShortString(), e);
793 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700794 }
Craig Mautner20e72272013-04-01 13:45:53 -0700795 }
Craig Mautner20e72272013-04-01 13:45:53 -0700796 }
797 }
798 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700799 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700800 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700801 }
Craig Mautner20e72272013-04-01 13:45:53 -0700802 return didSomething;
803 }
804
805 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800806 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
807 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
809 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700810 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 continue;
812 }
813 final ActivityRecord resumedActivity = stack.mResumedActivity;
814 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700815 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800816 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 return false;
818 }
Craig Mautner20e72272013-04-01 13:45:53 -0700819 }
820 }
821 return true;
822 }
823
Craig Mautnerde4ef022013-04-07 19:01:33 -0700824 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
826 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700829 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700831 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800832 return false;
833 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700834 }
835 }
836 }
837 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700838 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
840 mLastFocusedStack + " to=" + mFocusedStack);
841 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700842 return true;
843 }
844
845 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800846 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800847 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
848 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
850 final ActivityStack stack = stacks.get(stackNdx);
851 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800852 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700853 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800854 return false;
855 }
856 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800857 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700858 }
859 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800860 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700861 }
862
Craig Mautner2acc3892013-09-23 10:28:14 -0700863 /**
864 * Pause all activities in either all of the stacks or just the back stacks.
865 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700866 * @return true if any activity was paused as a result of this call.
867 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700869 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800870 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
871 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700874 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700875 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800876 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700877 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
878 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700880 }
881 }
882 return someActivityPaused;
883 }
884
Craig Mautnerde4ef022013-04-07 19:01:33 -0700885 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700886 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
890 final ActivityStack stack = stacks.get(stackNdx);
891 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700892 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700894 Slog.d(TAG_STATES,
895 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 pausing = false;
897 } else {
898 return false;
899 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700900 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700901 }
902 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700903 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700904 }
905
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
907 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500908 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800909 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
910 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
911 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
912 final ActivityStack stack = stacks.get(stackNdx);
913 if (stack.mResumedActivity != null &&
914 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700915 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800916 }
917 }
918 }
919 }
920
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800921 void cancelInitializingActivities() {
922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
923 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
925 stacks.get(stackNdx).cancelInitializingActivities();
926 }
927 }
928 }
929
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700930 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700931 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700932 }
933
934 void sendWaitingVisibleReportLocked(ActivityRecord r) {
935 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700936 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (w.who == null) {
939 changed = true;
940 w.timeout = false;
941 if (r != null) {
942 w.who = new ComponentName(r.info.packageName, r.info.name);
943 }
944 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
945 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 if (changed) {
949 mService.notifyAll();
950 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700951 }
952
953 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
954 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700955 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700957 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700958 if (w.who == null) {
959 changed = true;
960 w.timeout = timeout;
961 if (r != null) {
962 w.who = new ComponentName(r.info.packageName, r.info.name);
963 }
964 w.thisTime = thisTime;
965 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700966 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700967 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700968 if (changed) {
969 mService.notifyAll();
970 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700971 }
972
Craig Mautner29219d92013-04-16 20:19:12 -0700973 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800974 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700975 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700976 if (r != null) {
977 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700978 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700979
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800981 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
983 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800984 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700985 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700986 if (r != null) {
987 return r;
988 }
989 }
990 }
991 return null;
992 }
993
Dianne Hackborn09233282014-04-30 11:33:59 -0700994 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700995 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
997 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800999 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001001 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1002 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001003 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001004 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001005 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001006 }
1007 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001008
1009 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1010 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1011 while (maxNum > 0) {
1012 long mostRecentActiveTime = Long.MIN_VALUE;
1013 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 final int numTaskLists = runningTaskLists.size();
1015 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1016 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001017 if (!stackTaskList.isEmpty()) {
1018 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1019 if (lastActiveTime > mostRecentActiveTime) {
1020 mostRecentActiveTime = lastActiveTime;
1021 selectedStackList = stackTaskList;
1022 }
1023 }
1024 }
1025 if (selectedStackList != null) {
1026 list.add(selectedStackList.remove(0));
1027 --maxNum;
1028 } else {
1029 break;
1030 }
1031 }
Craig Mautner20e72272013-04-01 13:45:53 -07001032 }
1033
Todd Kennedy7440f172015-12-09 14:31:22 -08001034 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1035 ProfilerInfo profilerInfo) {
1036 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 if (aInfo != null) {
1038 // Store the found target back into the intent, because now that
1039 // we have it we never want to do this again. For example, if the
1040 // user navigates back to this point in the history, we should
1041 // always restart the exact same activity.
1042 intent.setComponent(new ComponentName(
1043 aInfo.applicationInfo.packageName, aInfo.name));
1044
1045 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001046 if (!aInfo.processName.equals("system")) {
1047 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001048 mService.setDebugApp(aInfo.processName, true, false);
1049 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001050
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001051 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1052 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1053 }
1054
Man Caocfa78b22015-06-11 20:14:34 -07001055 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1056 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1057 }
1058
1059 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001060 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001061 }
1062 }
1063 }
1064 return aInfo;
1065 }
1066
Todd Kennedy7440f172015-12-09 14:31:22 -08001067 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001068 return resolveIntent(intent, resolvedType, userId, 0);
1069 }
1070
1071 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001072 try {
1073 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001074 PackageManager.MATCH_DEFAULT_ONLY | flags
1075 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001076 } catch (RemoteException e) {
1077 }
1078 return null;
1079 }
1080
1081 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1082 ProfilerInfo profilerInfo, int userId) {
1083 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1084 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1085 }
1086
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001087 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1088 boolean andResume, boolean checkConfig) throws RemoteException {
1089
1090 if (!allPausedActivitiesComplete()) {
1091 // While there are activities pausing we skipping starting any new activities until
1092 // pauses are complete. NOTE: that we also do this for activities that are starting in
1093 // the paused state because they will first be resumed then paused on the client side.
1094 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1095 "realStartActivityLocked: Skipping start of r=" + r
1096 + " some activities pausing...");
1097 return false;
1098 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001099
Craig Mautner2568c3a2015-03-26 14:22:34 -07001100 if (andResume) {
1101 r.startFreezingScreenLocked(app, 0);
1102 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001103
Craig Mautner2568c3a2015-03-26 14:22:34 -07001104 // schedule launch ticks to collect information about slow apps.
1105 r.startLaunchTickingLocked();
1106 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001107
1108 // Have the window manager re-evaluate the orientation of
1109 // the screen based on the new activity order. Note that
1110 // as a result of this, it can call back into the activity
1111 // manager with a new orientation. We don't care about that,
1112 // because the activity is not currently running so we are
1113 // just restarting it anyway.
1114 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 mService.mConfiguration,
1117 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001118 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001119 }
1120
1121 r.app = app;
1122 app.waitingToKill = null;
1123 r.launchCount++;
1124 r.lastLaunchTime = SystemClock.uptimeMillis();
1125
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001126 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001127
1128 int idx = app.activities.indexOf(r);
1129 if (idx < 0) {
1130 app.activities.add(r);
1131 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001132 mService.updateLruProcessLocked(app, true, null);
1133 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001134
Craig Mautner15df08a2015-04-01 12:17:18 -07001135 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001136 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1137 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001138 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001139 }
1140
1141 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001142 try {
1143 if (app.thread == null) {
1144 throw new RemoteException();
1145 }
1146 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001147 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001148 if (andResume) {
1149 results = r.results;
1150 newIntents = r.newIntents;
1151 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001152 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1153 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1154 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 if (andResume) {
1156 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1157 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001158 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001160 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001161 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001162 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001163 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001164 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001165 r.sleeping = false;
1166 r.forceNewConfig = false;
1167 mService.showAskCompatModeDialogLocked(r);
1168 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001169 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1171 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1172 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001173 final String profileFile = mService.mProfileFile;
1174 if (profileFile != null) {
1175 ParcelFileDescriptor profileFd = mService.mProfileFd;
1176 if (profileFd != null) {
1177 try {
1178 profileFd = profileFd.dup();
1179 } catch (IOException e) {
1180 if (profileFd != null) {
1181 try {
1182 profileFd.close();
1183 } catch (IOException o) {
1184 }
1185 profileFd = null;
1186 }
1187 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001189
1190 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1191 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 }
1193 }
1194 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001195
Craig Mautner2568c3a2015-03-26 14:22:34 -07001196 if (andResume) {
1197 app.hasShownUi = true;
1198 app.pendingUiClean = true;
1199 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001200 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001202 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001203 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001204 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001205 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001207 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 // This may be a heavy-weight process! Note that the package
1209 // manager will ensure that only activity can run in the main
1210 // process of the .apk, which is the only thing that will be
1211 // considered heavy-weight.
1212 if (app.processName.equals(app.info.packageName)) {
1213 if (mService.mHeavyWeightProcess != null
1214 && mService.mHeavyWeightProcess != app) {
1215 Slog.w(TAG, "Starting new heavy weight process " + app
1216 + " when already running "
1217 + mService.mHeavyWeightProcess);
1218 }
1219 mService.mHeavyWeightProcess = app;
1220 Message msg = mService.mHandler.obtainMessage(
1221 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1222 msg.obj = r;
1223 mService.mHandler.sendMessage(msg);
1224 }
1225 }
1226
1227 } catch (RemoteException e) {
1228 if (r.launchFailed) {
1229 // This is the second time we failed -- finish activity
1230 // and give up.
1231 Slog.e(TAG, "Second failure launching "
1232 + r.intent.getComponent().flattenToShortString()
1233 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001234 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1236 "2nd-crash", false);
1237 return false;
1238 }
1239
1240 // This is the first time we failed -- restart process and
1241 // retry.
1242 app.activities.remove(r);
1243 throw e;
1244 }
1245
1246 r.launchFailed = false;
1247 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001248 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 }
1250
1251 if (andResume) {
1252 // As part of the process of launching, ActivityThread also performs
1253 // a resume.
1254 stack.minimalResumeActivityLocked(r);
1255 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001256 // This activity is not starting in the resumed state... which should look like we asked
1257 // it to pause+stop (but remain visible), and it has done so and reported back the
1258 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001259 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001260 "Moving to PAUSED: " + r + " (starting in paused state)");
1261 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 }
1263
1264 // Launch the new version setup screen if needed. We do this -after-
1265 // launching the initial activity (that is, home), so that it can have
1266 // a chance to initialize itself while in the background, making the
1267 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001268 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 mService.startSetupActivityLocked();
1270 }
1271
Dianne Hackborn465fa392014-09-14 14:21:18 -07001272 // Update any services we are bound to that might care about whether
1273 // their client may have activities.
1274 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1275
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 return true;
1277 }
1278
Craig Mautnere79d42682013-04-01 19:01:53 -07001279 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001280 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001281 // Is this activity's application already running?
1282 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001283 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001284
1285 r.task.stack.setLaunchTime(r);
1286
1287 if (app != null && app.thread != null) {
1288 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001289 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1290 || !"android".equals(r.info.packageName)) {
1291 // Don't add this if it is a platform component that is marked
1292 // to run in multiple processes, because this is actually
1293 // part of the framework so doesn't make sense to track as a
1294 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001295 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1296 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001297 }
George Mount2c92c972014-03-20 09:38:23 -07001298 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001299 return;
1300 } catch (RemoteException e) {
1301 Slog.w(TAG, "Exception when starting activity "
1302 + r.intent.getComponent().flattenToShortString(), e);
1303 }
1304
1305 // If a dead object exception was thrown -- fall through to
1306 // restart the application.
1307 }
1308
1309 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001310 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001311 }
1312
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001313 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001314 String resultWho, int requestCode, int callingPid, int callingUid,
1315 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001316 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001317 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1318 callingUid);
1319 if (startAnyPerm == PERMISSION_GRANTED) {
1320 return true;
1321 }
1322 final int componentRestriction = getComponentRestrictionForCallingPackage(
1323 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1324 final int actionRestriction = getActionRestrictionForCallingPackage(
1325 intent.getAction(), callingPackage, callingPid, callingUid);
1326 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1327 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1328 if (resultRecord != null) {
1329 resultStack.sendActivityResultLocked(-1,
1330 resultRecord, resultWho, requestCode,
1331 Activity.RESULT_CANCELED, null);
1332 }
1333 final String msg;
1334 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1335 msg = "Permission Denial: starting " + intent.toString()
1336 + " from " + callerApp + " (pid=" + callingPid
1337 + ", uid=" + callingUid + ")" + " with revoked permission "
1338 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1339 } else if (!aInfo.exported) {
1340 msg = "Permission Denial: starting " + intent.toString()
1341 + " from " + callerApp + " (pid=" + callingPid
1342 + ", uid=" + callingUid + ")"
1343 + " not exported from uid " + aInfo.applicationInfo.uid;
1344 } else {
1345 msg = "Permission Denial: starting " + intent.toString()
1346 + " from " + callerApp + " (pid=" + callingPid
1347 + ", uid=" + callingUid + ")"
1348 + " requires " + aInfo.permission;
1349 }
1350 Slog.w(TAG, msg);
1351 throw new SecurityException(msg);
1352 }
1353
1354 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1355 final String message = "Appop Denial: starting " + intent.toString()
1356 + " from " + callerApp + " (pid=" + callingPid
1357 + ", uid=" + callingUid + ")"
1358 + " requires " + AppOpsManager.permissionToOp(
1359 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1360 Slog.w(TAG, message);
1361 return false;
1362 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1363 final String message = "Appop Denial: starting " + intent.toString()
1364 + " from " + callerApp + " (pid=" + callingPid
1365 + ", uid=" + callingUid + ")"
1366 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1367 Slog.w(TAG, message);
1368 return false;
1369 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001370 if (options != null && options.getLaunchTaskId() != -1) {
1371 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1372 callingPid, callingUid);
1373 if (startInTaskPerm != PERMISSION_GRANTED) {
1374 final String msg = "Permission Denial: starting " + intent.toString()
1375 + " from " + callerApp + " (pid=" + callingPid
1376 + ", uid=" + callingUid + ") with launchTaskId="
1377 + options.getLaunchTaskId();
1378 Slog.w(TAG, msg);
1379 throw new SecurityException(msg);
1380 }
1381 }
1382
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001383 return true;
1384 }
1385
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001386 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001387 final long identity = Binder.clearCallingIdentity();
1388 try {
1389 return UserManager.get(mService.mContext).getUserInfo(userId);
1390 } finally {
1391 Binder.restoreCallingIdentity(identity);
1392 }
1393 }
1394
Svet Ganov99b60432015-06-27 13:15:22 -07001395 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001396 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001397 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1398 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001399 == PackageManager.PERMISSION_DENIED) {
1400 return ACTIVITY_RESTRICTION_PERMISSION;
1401 }
1402
Christopher Tateff7add02015-08-17 10:23:22 -07001403 if (activityInfo.permission == null) {
1404 return ACTIVITY_RESTRICTION_NONE;
1405 }
1406
Svet Ganov99b60432015-06-27 13:15:22 -07001407 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1408 if (opCode == AppOpsManager.OP_NONE) {
1409 return ACTIVITY_RESTRICTION_NONE;
1410 }
1411
1412 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1413 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001414 if (!ignoreTargetSecurity) {
1415 return ACTIVITY_RESTRICTION_APPOP;
1416 }
Svet Ganov99b60432015-06-27 13:15:22 -07001417 }
1418
1419 return ACTIVITY_RESTRICTION_NONE;
1420 }
1421
Svetoslav7008b512015-06-24 18:47:07 -07001422 private int getActionRestrictionForCallingPackage(String action,
1423 String callingPackage, int callingPid, int callingUid) {
1424 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001425 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001426 }
1427
1428 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1429 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001430 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001431 }
1432
1433 final PackageInfo packageInfo;
1434 try {
1435 packageInfo = mService.mContext.getPackageManager()
1436 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1437 } catch (PackageManager.NameNotFoundException e) {
1438 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001439 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001440 }
1441
1442 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001443 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001444 }
1445
1446 if (mService.checkPermission(permission, callingPid, callingUid) ==
1447 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001448 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001449 }
1450
1451 final int opCode = AppOpsManager.permissionToOpCode(permission);
1452 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001453 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001454 }
1455
1456 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1457 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001458 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001459 }
1460
Svet Ganov99b60432015-06-27 13:15:22 -07001461 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001462 }
1463
Wale Ogunwaled046a012015-12-24 13:05:59 -08001464 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001465 if (r == null) {
1466 // Not sure what you are trying to do, but it is not going to work...
1467 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001468 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001469 final TaskRecord task = r.task;
1470 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001471 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001472 return false;
1473 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001474 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001475 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001476 }
1477
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001478 void setLaunchSource(int uid) {
1479 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1480 }
1481
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001482 void acquireLaunchWakelock() {
1483 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1484 throw new IllegalStateException("Calling must be system uid");
1485 }
1486 mLaunchingActivity.acquire();
1487 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1488 // To be safe, don't allow the wake lock to be held for too long.
1489 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1490 }
1491 }
1492
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001493 /**
1494 * Called when the frontmost task is idle.
1495 * @return the state of mService.mBooting before this was called.
1496 */
1497 private boolean checkFinishBootingLocked() {
1498 final boolean booting = mService.mBooting;
1499 boolean enableScreen = false;
1500 mService.mBooting = false;
1501 if (!mService.mBooted) {
1502 mService.mBooted = true;
1503 enableScreen = true;
1504 }
1505 if (booting || enableScreen) {
1506 mService.postFinishBooting(booting, enableScreen);
1507 }
1508 return booting;
1509 }
1510
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 // Checked.
1512 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1513 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001514 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001515
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001517 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 int NS = 0;
1519 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 boolean activityRemoved = false;
1522
Wale Ogunwale7d701172015-03-11 15:36:30 -07001523 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001524 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001525 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1526 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001527 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1528 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001529 if (fromTimeout) {
1530 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001531 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001532
1533 // This is a hack to semi-deal with a race condition
1534 // in the client where it can be constructed with a
1535 // newer configuration from when we asked it to launch.
1536 // We'll update with whatever configuration it now says
1537 // it used to launch.
1538 if (config != null) {
1539 r.configuration = config;
1540 }
1541
1542 // We are now idle. If someone is waiting for a thumbnail from
1543 // us, we can now deliver.
1544 r.idle = true;
1545
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001546 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001547 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001548 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001549 }
1550 }
1551
1552 if (allResumedActivitiesIdle()) {
1553 if (r != null) {
1554 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001555 }
1556
1557 if (mLaunchingActivity.isHeld()) {
1558 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1559 if (VALIDATE_WAKE_LOCK_CALLER &&
1560 Binder.getCallingUid() != Process.myUid()) {
1561 throw new IllegalStateException("Calling must be system uid");
1562 }
1563 mLaunchingActivity.release();
1564 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001565 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 }
1567
1568 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001569 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001571 if ((NF = mFinishingActivities.size()) > 0) {
1572 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001573 mFinishingActivities.clear();
1574 }
1575
Craig Mautnerf3333272013-04-22 10:55:53 -07001576 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001577 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001578 mStartingUsers.clear();
1579 }
1580
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 // Stop any activities that are scheduled to do so but have been
1582 // waiting for the next one to start.
1583 for (int i = 0; i < NS; i++) {
1584 r = stops.get(i);
1585 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001586 if (stack != null) {
1587 if (r.finishing) {
1588 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1589 } else {
1590 stack.stopActivityLocked(r);
1591 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 }
1593 }
1594
1595 // Finish any activities that are scheduled to do so but have been
1596 // waiting for the next one to start.
1597 for (int i = 0; i < NF; i++) {
1598 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001599 final ActivityStack stack = r.task.stack;
1600 if (stack != null) {
1601 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1602 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001603 }
1604
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001605 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001606 // Complete user switch
1607 if (startingUsers != null) {
1608 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001609 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001610 }
1611 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001612 }
1613
1614 mService.trimApplications();
1615 //dump();
1616 //mWindowManager.dump();
1617
Craig Mautnerf3333272013-04-22 10:55:53 -07001618 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001619 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 }
1621
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001622 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001623 }
1624
Craig Mautner8e569572013-10-11 17:36:59 -07001625 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001626 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1628 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1630 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1631 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001632 }
Craig Mautner19091252013-10-05 00:03:53 -07001633 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001634 }
1635
1636 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1640 stacks.get(stackNdx).closeSystemDialogsLocked();
1641 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001642 }
1643 }
1644
Craig Mautner93529a42013-10-04 15:03:13 -07001645 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001646 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001647 }
1648
Craig Mautner8d341ef2013-03-26 09:03:27 -07001649 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001650 * Update the last used stack id for non-current user (current user's last
1651 * used stack is the focused stack)
1652 */
1653 void updateUserStackLocked(int userId, ActivityStack stack) {
1654 if (userId != mCurrentUser) {
1655 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1656 }
1657 }
1658
1659 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001660 * @return true if some activity was finished (or would have finished if doit were true).
1661 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001662 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1663 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001664 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001665 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1666 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001667 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001668 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001669 if (stack.finishDisabledPackageActivitiesLocked(
1670 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001671 didSomething = true;
1672 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001673 }
1674 }
1675 return didSomething;
1676 }
1677
Dianne Hackborna413dc02013-07-12 12:02:55 -07001678 void updatePreviousProcessLocked(ActivityRecord r) {
1679 // Now that this process has stopped, we may want to consider
1680 // it to be the previous app to try to keep around in case
1681 // the user wants to return to it.
1682
1683 // First, found out what is currently the foreground app, so that
1684 // we don't blow away the previous app if this activity is being
1685 // hosted by the process that is actually still the foreground.
1686 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001687 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1688 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001689 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1690 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001691 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001692 if (stack.mResumedActivity != null) {
1693 fgApp = stack.mResumedActivity.app;
1694 } else if (stack.mPausingActivity != null) {
1695 fgApp = stack.mPausingActivity.app;
1696 }
1697 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001698 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001699 }
1700 }
1701
1702 // Now set this one as the previous process, only if that really
1703 // makes sense to.
1704 if (r.app != null && fgApp != null && r.app != fgApp
1705 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001706 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001707 mService.mPreviousProcess = r.app;
1708 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1709 }
1710 }
1711
Wale Ogunwaled046a012015-12-24 13:05:59 -08001712 boolean resumeFocusedStackTopActivityLocked() {
1713 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001714 }
1715
Wale Ogunwaled046a012015-12-24 13:05:59 -08001716 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001717 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001718 if (targetStack != null && isFocusedStack(targetStack)) {
1719 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001720 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001721 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1722 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 }
1724
Todd Kennedy39bfee52016-02-24 10:28:21 -08001725 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1729 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1730 }
1731 }
1732 }
1733
Adrian Roos20d7df32016-01-12 18:59:43 +01001734 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1735 TaskRecord finishedTask = null;
1736 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001739 final int numStacks = stacks.size();
1740 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1741 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001742 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1743 if (stack == focusedStack || finishedTask == null) {
1744 finishedTask = t;
1745 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001746 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001747 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001748 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001749 }
1750
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001751 void finishVoiceTask(IVoiceInteractionSession session) {
1752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1753 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1754 final int numStacks = stacks.size();
1755 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1756 final ActivityStack stack = stacks.get(stackNdx);
1757 stack.finishVoiceTask(session);
1758 }
1759 }
1760 }
1761
Chong Zhang280d3322015-11-03 17:27:26 -08001762 void findTaskToMoveToFrontLocked(
1763 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001764 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1765 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001766 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001767 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1768 // Caller wants the home activity moved with it. To accomplish this,
1769 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001770 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001771 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001772 if (task.stack == null) {
1773 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1774 + task + " to front. Stack is null");
1775 return;
1776 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001777
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001778 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001779 int stackId = options.getLaunchStackId();
1780 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001781 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001782 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001783 if (stackId == INVALID_STACK_ID) {
1784 stackId = task.getLaunchStackId();
1785 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001786 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001787 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1788 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1789 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001790 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1791 // still need moveTaskToFrontLocked() below for any transition settings.
1792 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001793 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1794 resizeStackLocked(stackId, bounds,
1795 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1796 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1797 } else {
1798 // WM resizeTask must be done after the task is moved to the correct stack,
1799 // because Task's setBounds() also updates dim layer's bounds, but that has
1800 // dependency on the stack.
1801 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1802 false /* relayout */, false /* forced */);
1803 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001804 }
1805 }
1806
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001807 final ActivityRecord r = task.getTopActivity();
1808 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1809 r == null ? null : r.appTimeTracker, reason);
1810
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001811 if (DEBUG_STACK) Slog.d(TAG_STACK,
1812 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001813
1814 showNonResizeableDockToastIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001815 }
1816
Wale Ogunwale854809c2015-12-27 16:18:19 -08001817 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001818 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001819 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001820 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001821 return false;
1822 }
1823 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1824 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001825 }
1826
Craig Mautner967212c2013-04-13 21:10:58 -07001827 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001828 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001829 }
1830
1831 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001832 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1833 if (activityContainer != null) {
1834 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001835 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001836 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001837 return null;
1838 }
1839 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001840 }
1841
Craig Mautner967212c2013-04-13 21:10:58 -07001842 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001843 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001844 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1845 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001846 }
1847 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001848 }
1849
Craig Mautner4a1cb222013-12-04 16:14:06 -08001850 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001851 ActivityRecord homeActivity = getHomeActivity();
1852 if (homeActivity != null) {
1853 return homeActivity.appToken;
1854 }
1855 return null;
1856 }
1857
1858 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001859 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001860 }
1861
1862 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001863 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1864 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1865 final TaskRecord task = tasks.get(taskNdx);
1866 if (task.isHomeTask()) {
1867 final ArrayList<ActivityRecord> activities = task.mActivities;
1868 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1869 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001870 if (r.isHomeActivity()
1871 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001872 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001873 }
1874 }
1875 }
1876 }
1877 return null;
1878 }
1879
Chong Zhangb15758a2015-11-17 12:12:03 -08001880 /**
1881 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1882 * the docked stack itself, or if it's side-by-side to the docked stack.
1883 */
1884 boolean isStackDockedInEffect(int stackId) {
1885 return stackId == DOCKED_STACK_ID ||
1886 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1887 }
1888
Todd Kennedyca4d8422015-01-15 15:19:22 -08001889 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001890 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001891 ActivityContainer activityContainer =
1892 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001893 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001894 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1895 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001896 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001897 return activityContainer;
1898 }
1899
Craig Mautner34b73df2014-01-12 21:11:08 -08001900 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001901 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1902 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1903 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001904 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1905 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001906 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001907 }
1908 }
1909
Craig Mautner95da1082014-02-24 17:54:35 -08001910 void deleteActivityContainer(IActivityContainer container) {
1911 ActivityContainer activityContainer = (ActivityContainer)container;
1912 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001913 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1914 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001915 final int stackId = activityContainer.mStackId;
1916 mActivityContainers.remove(stackId);
1917 mWindowManager.removeStack(stackId);
1918 }
1919 }
1920
Jorim Jaggidc249c42015-12-15 14:57:31 -08001921 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1922 boolean preserveWindows, boolean allowResizeInDockedMode) {
1923 if (stackId == DOCKED_STACK_ID) {
1924 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1925 preserveWindows);
1926 return;
1927 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001928 final ActivityStack stack = getStack(stackId);
1929 if (stack == null) {
1930 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1931 return;
1932 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001933
Jorim Jaggidc249c42015-12-15 14:57:31 -08001934 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001935 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1936 // stack size changing so things don't get out of sync.
1937 return;
1938 }
1939
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001940 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001941 mWindowManager.deferSurfaceLayout();
1942 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001943 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1944 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001945 } finally {
1946 mWindowManager.continueSurfaceLayout();
1947 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001948 }
1949 }
1950
Jorim Jaggi192086e2016-03-11 17:17:03 +01001951 void deferUpdateBounds(int stackId) {
1952 final ActivityStack stack = getStack(stackId);
1953 if (stack != null) {
1954 stack.deferUpdateBounds();
1955 }
1956 }
1957
1958 void continueUpdateBounds(int stackId) {
1959 final ActivityStack stack = getStack(stackId);
1960 if (stack != null) {
1961 stack.continueUpdateBounds();
1962 }
1963 }
1964
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01001965 void notifyAppTransitionDone() {
1966 continueUpdateBounds(HOME_STACK_ID);
1967 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
1968 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
1969 if (anyTaskForIdLocked(taskId) != null) {
1970 mWindowManager.setTaskDockedResizing(taskId, false);
1971 }
1972 }
1973 mResizingTasksDuringAnimation.clear();
1974 }
1975
Jorim Jaggi192086e2016-03-11 17:17:03 +01001976 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08001977 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001978 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001979
Jorim Jaggi192086e2016-03-11 17:17:03 +01001980 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
1981 return;
1982 }
1983
Jorim Jaggidc249c42015-12-15 14:57:31 -08001984 mTmpBounds.clear();
1985 mTmpConfigs.clear();
1986 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001987 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001988 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001989 final TaskRecord task = tasks.get(i);
1990 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001991 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1992 // For freeform stack we don't adjust the size of the tasks to match that
1993 // of the stack, but we do try to make sure the tasks are still contained
1994 // with the bounds of the stack.
1995 tempRect2.set(task.mBounds);
1996 fitWithinBounds(tempRect2, bounds);
1997 task.updateOverrideConfiguration(tempRect2);
1998 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001999 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08002000 tempTaskBounds != null ? tempTaskBounds : bounds,
2001 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002002 }
2003 }
2004
2005 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2006 mTmpBounds.put(task.taskId, task.mBounds);
2007 if (tempTaskInsetBounds != null) {
2008 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2009 }
2010 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002011
2012 // We might trigger a configuration change. Save the current task bounds for freezing.
2013 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002014 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2015 mTmpBounds, mTmpInsetBounds);
2016 stack.setBounds(bounds);
2017 }
2018
2019 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2020 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002021 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002022 return;
2023 }
2024 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2025 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002026 if (!updated) {
2027 resumeFocusedStackTopActivityLocked();
2028 }
2029 }
2030
2031 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2032 Rect tempDockedTaskInsetBounds,
2033 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
2034 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2035 if (stack == null) {
2036 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2037 return;
2038 }
2039
2040 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2041 mWindowManager.deferSurfaceLayout();
2042 try {
2043 ActivityRecord r = stack.topRunningActivityLocked();
2044 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2045 tempDockedTaskInsetBounds);
2046
2047 if (stack.mFullscreen) {
2048 // The dock stack went fullscreen which is kinda like dismissing it.
2049 // In this case we make all other static stacks fullscreen and move all
2050 // docked stack tasks to the fullscreen stack.
2051 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2052 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2053 resizeStackLocked(i, null, null, null, preserveWindows,
2054 true /* allowResizeInDockedMode */);
2055 }
2056 }
2057
2058 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2059 final int count = tasks.size();
2060 for (int i = 0; i < count; i++) {
2061 moveTaskToStackLocked(tasks.get(i).taskId,
2062 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2063 false /* animate */);
2064 }
2065
2066 // stack shouldn't contain anymore activities, so nothing to resume.
2067 r = null;
2068 } else {
2069 // Docked stacks occupy a dedicated region on screen so the size of all other
2070 // static stacks need to be adjusted so they don't overlap with the docked stack.
2071 // We get the bounds to use from window manager which has been adjusted for any
2072 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002073 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002074 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002075 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2076 if (StackId.isResizeableByDockedStack(i)) {
2077 ActivityStack otherStack = getStack(i);
2078 if (otherStack != null) {
2079 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2080 tempOtherTaskInsetBounds, preserveWindows,
2081 true /* allowResizeInDockedMode */);
2082 }
2083 }
2084 }
2085 }
2086 ensureConfigurationAndResume(stack, r, preserveWindows);
2087 } finally {
2088 mWindowManager.continueSurfaceLayout();
2089 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2090 }
2091
2092 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2093 tempDockedTaskBounds != null
2094 || tempDockedTaskInsetBounds != null
2095 || tempOtherTaskBounds != null
2096 || tempOtherTaskInsetBounds != null);
2097 }
2098
Robert Carr0d00c2e2016-02-29 17:45:02 -08002099 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2100 final ActivityStack stack = getStack(PINNED_STACK_ID);
2101 if (stack == null) {
2102 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2103 return;
2104 }
2105 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2106 mWindowManager.deferSurfaceLayout();
2107 try {
2108 ActivityRecord r = stack.topRunningActivityLocked();
2109 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2110 null);
2111 ensureConfigurationAndResume(stack, r, false);
2112 } finally {
2113 mWindowManager.continueSurfaceLayout();
2114 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2115 }
2116 }
2117
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002118 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2119 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002120 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002121 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002122 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002123 }
2124
Chong Zhang87b21722015-09-21 15:39:51 -07002125 // If this is a forced resize, let it go through even if the bounds is not changing,
2126 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002127 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002128 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002129 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002130 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002131 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002132 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002133
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002134 if (!mWindowManager.isValidTaskId(task.taskId)) {
2135 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002136 // All we can do for now is update the bounds so it can be used when the task is
2137 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002138 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002139 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2140 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002141 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002142 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002143 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002144 }
2145
Chong Zhang6de2ae82015-09-30 18:25:21 -07002146 // Do not move the task to another stack here.
2147 // This method assumes that the task is already placed in the right stack.
2148 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002149
Chong Zhang6de2ae82015-09-30 18:25:21 -07002150 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002151
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002152 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002153 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002154 // way and the activity was kept the way it was. If it's false, it means the activity had
2155 // to be relaunched due to configuration change.
2156 boolean kept = true;
2157 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002158 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002159 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002160 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002161 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002162
2163 if (!deferResume) {
2164
2165 // All other activities must be made visible with their correct configuration.
2166 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2167 if (!kept) {
2168 resumeFocusedStackTopActivityLocked();
2169 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002170 }
2171 }
2172 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002173 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002174
2175 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002176 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002177 }
2178
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002179 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002180 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2181 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002182 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002183 }
2184
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002185 ActivityContainer activityContainer = new ActivityContainer(stackId);
2186 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002187 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002188 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002189 }
2190
2191 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002192 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002193 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2194 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002195 break;
2196 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002197 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002198 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002199 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002200 }
2201
Chong Zhang5dcb2752015-08-18 13:50:26 -07002202 /**
2203 * Restores a recent task to a stack
2204 * @param task The recent task to be restored.
2205 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002206 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002207 * @return true if the task has been restored successfully.
2208 */
2209 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2210 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002211 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002212 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2213 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2214 // Put it in the fullscreen stack.
2215 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002216 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002217
Wale Ogunwale706ed792015-08-02 10:29:44 -07002218 if (task.stack != null) {
2219 // Task has already been restored once. See if we need to do anything more
2220 if (task.stack.mStackId == stackId) {
2221 // Nothing else to do since it is already restored in the right stack.
2222 return true;
2223 }
2224 // Remove current stack association, so we can re-associate the task with the
2225 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002226 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002227 }
2228
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002229 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002230 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002231
2232 if (stack == null) {
2233 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002234 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2235 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002236 return false;
2237 }
2238
Wale Ogunwale5f986092015-12-04 15:35:38 -08002239 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002240 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2241 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002242 final ArrayList<ActivityRecord> activities = task.mActivities;
2243 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002244 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002245 }
2246 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002247 }
2248
Wale Ogunwale040b4702015-08-06 18:10:50 -07002249 /**
2250 * Moves the specified task record to the input stack id.
2251 * WARNING: This method performs an unchecked/raw move of the task and
2252 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002253 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002254 * @param task Task to move.
2255 * @param stackId Id of stack to move task to.
2256 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002257 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002258 * @param reason Reason the task is been moved.
2259 * @return The stack the task was moved to.
2260 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002261 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002262 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002263
2264 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2265 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2266 + "support multi-window task=" + task + " to stackId=" + stackId);
2267 }
2268
Chong Zhang02898352015-08-21 17:27:14 -07002269 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002270 final ActivityStack prevStack = task.stack;
2271 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2272 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2273 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2274 // Whenever we are moving the top activity from the front stack we want to make sure to move
2275 // the stack to the front.
2276 final boolean wasFront = isFrontStack(prevStack)
2277 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002278
Chong Zhangd545dce2016-02-29 18:09:17 -08002279 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002280 // We don't allow moving a unresizeable task to the docked stack since the docked
2281 // stack is used for split-screen mode and will cause things like the docked divider to
2282 // show up. We instead leave the task in its current stack or move it to the fullscreen
2283 // stack if it isn't currently in a stack.
2284 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002285 Slog.w(TAG, "Can not move unresizeable task=" + task
2286 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2287 }
2288
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002289 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2290 // if a docked stack is created below which will lead to the stack we are moving from and
2291 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002292 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002293 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002294 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002295 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002296 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002297
2298 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002299 // move focus to the new stack by moving the stack to the front.
2300 stack.moveToFrontAndResumeStateIfNeeded(
2301 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002302
Wale Ogunwale040b4702015-08-06 18:10:50 -07002303 return stack;
2304 }
2305
Chong Zhange4fbd322016-03-01 14:44:03 -08002306 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002307 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002308 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2309 false /* deferResume */);
2310 }
2311
2312 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2313 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002314 final TaskRecord task = anyTaskForIdLocked(taskId);
2315 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002316 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002317 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002318 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002319
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002320 if (task.stack != null && task.stack.mStackId == stackId) {
2321 // You are already in the right stack silly...
2322 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002323 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002324 }
2325
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002326 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2327 throw new IllegalArgumentException("moveTaskToStack:"
2328 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2329 }
2330
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002331 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002332 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002333 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002334 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002335 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002336 // We are about to relaunch the activity because its configuration changed due to
2337 // being maximized, i.e. size change. The activity will first remove the old window
2338 // and then add a new one. This call will tell window manager about this, so it can
2339 // preserve the old window until the new one is drawn. This prevents having a gap
2340 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002341 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002342 // Note here we always set the replacing window first, as the flags might be needed
2343 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002344 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002345 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002346
2347 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002348 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002349 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002350 try {
2351 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002352 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002353 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002354
Wale Ogunwale961f4852016-02-01 20:25:54 -08002355 if (!animate) {
2356 stack.mNoAnimActivities.add(topActivity);
2357 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002358
Wale Ogunwale961f4852016-02-01 20:25:54 -08002359 // We might trigger a configuration change. Save the current task bounds for freezing.
2360 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2361
2362 // Make sure the task has the appropriate bounds/size for the stack it is in.
2363 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002364 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2365 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002366 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2367 Rect bounds = task.getLaunchBounds();
2368 if (bounds == null) {
2369 stack.layoutTaskInStack(task, null);
2370 bounds = task.mBounds;
2371 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002372 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2373 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002374 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002375 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2376 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002377 }
2378 } finally {
2379 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002380 }
2381
2382 if (mightReplaceWindow) {
2383 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2384 // window), we need to clear the replace window settings. Otherwise, we schedule a
2385 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002386 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002387 }
2388
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002389 if (!deferResume) {
2390
2391 // The task might have already been running and its visibility needs to be synchronized with
2392 // the visibility of the stack / windows.
2393 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2394 resumeFocusedStackTopActivityLocked();
2395 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002396
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002397 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002398
2399 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002400 }
2401
Wale Ogunwale079a0042015-10-24 11:44:07 -07002402 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2403 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2404 if (stack == null) {
2405 throw new IllegalArgumentException(
2406 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2407 }
2408
2409 final ActivityRecord r = stack.topRunningActivityLocked();
2410 if (r == null) {
2411 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2412 + " in stack=" + stack);
2413 return false;
2414 }
2415
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002416 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002417 Slog.w(TAG,
2418 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002419 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002420 return false;
2421 }
2422
Wale Ogunwale480dca02016-02-06 13:58:29 -08002423 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002424 return true;
2425 }
2426
Wale Ogunwale480dca02016-02-06 13:58:29 -08002427 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2428 mWindowManager.deferSurfaceLayout();
2429 try {
2430 final TaskRecord task = r.task;
2431
2432 // Need to make sure the pinned stack exist so we can resize it below...
2433 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2434
2435 // Resize the pinned stack to match the current size of the task the activity we are
2436 // going to be moving is currently contained in. We do this to have the right starting
2437 // animation bounds for the pinned stack to the desired bounds the caller wants.
2438 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2439 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2440 true /* allowResizeInDockedMode */);
2441
2442 if (task.mActivities.size() == 1) {
2443 // There is only one activity in the task. So, we can just move the task over to
2444 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002445 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2446 // Move the home stack forward if the task we just moved to the pinned stack
2447 // was launched from home so home should be visible behind it.
2448 moveHomeStackToFront(reason);
2449 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002450 moveTaskToStackLocked(
2451 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2452 } else {
2453 stack.moveActivityToStack(r);
2454 }
2455 } finally {
2456 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002457 }
2458
Wale Ogunwale480dca02016-02-06 13:58:29 -08002459 // The task might have already been running and its visibility needs to be synchronized
2460 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002461 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002462 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002463
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002464 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002465 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002466 }
2467
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002468 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2469 final TaskRecord task = anyTaskForIdLocked(taskId);
2470 if (task == null) {
2471 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2472 return;
2473 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002474 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2475
2476 task.updateOverrideConfigurationForStack(stack);
2477
2478 mWindowManager.positionTaskInStack(
2479 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002480 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002481 // The task might have already been running and its visibility needs to be synchronized with
2482 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002483 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2484 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002485 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002486 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002487 }
2488
Craig Mautnerac6f8432013-07-17 13:24:59 -07002489 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002490 mTmpFindTaskResult.r = null;
2491 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002492 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2496 final ActivityStack stack = stacks.get(stackNdx);
2497 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002498 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002499 continue;
2500 }
2501 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002502 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2503 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 continue;
2505 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002506 stack.findTaskLocked(r, mTmpFindTaskResult);
2507 // It is possible to have task in multiple stacks with the same root affinity.
2508 // If the match we found was based on root affinity we keep on looking to see if
2509 // there is a better match in another stack. We eventually return the match based
2510 // on root affinity if we don't find a better match.
2511 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2512 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002513 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514 }
2515 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002516 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2517 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002518 }
2519
2520 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2522 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002523 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2524 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2525 if (ar != null) {
2526 return ar;
2527 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002528 }
2529 }
2530 return null;
2531 }
2532
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002534 scheduleSleepTimeout();
2535 if (!mGoingToSleep.isHeld()) {
2536 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002537 if (mLaunchingActivity.isHeld()) {
2538 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2539 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002540 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002541 mLaunchingActivity.release();
2542 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002544 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002545 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 }
2547
2548 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002549 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002550
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002551 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002552 final long endTime = System.currentTimeMillis() + timeout;
2553 while (true) {
2554 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002555 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2556 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2558 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2559 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002560 }
2561 if (cantShutdown) {
2562 long timeRemaining = endTime - System.currentTimeMillis();
2563 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002565 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002566 } catch (InterruptedException e) {
2567 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002568 } else {
2569 Slog.w(TAG, "Activity manager shutdown timed out");
2570 timedout = true;
2571 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002572 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002573 } else {
2574 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002575 }
2576 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002577
2578 // Force checkReadyForSleep to complete.
2579 mSleepTimeout = true;
2580 checkReadyForSleepLocked();
2581
Craig Mautner8d341ef2013-03-26 09:03:27 -07002582 return timedout;
2583 }
2584
2585 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002586 removeSleepTimeouts();
2587 if (mGoingToSleep.isHeld()) {
2588 mGoingToSleep.release();
2589 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002590 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2591 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002592 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2593 final ActivityStack stack = stacks.get(stackNdx);
2594 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002595 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002596 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002597 }
Craig Mautner5314a402013-09-26 12:40:16 -07002598 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002599 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002600 mGoingToSleepActivities.clear();
2601 }
2602
2603 void activitySleptLocked(ActivityRecord r) {
2604 mGoingToSleepActivities.remove(r);
2605 checkReadyForSleepLocked();
2606 }
2607
2608 void checkReadyForSleepLocked() {
2609 if (!mService.isSleepingOrShuttingDown()) {
2610 // Do not care.
2611 return;
2612 }
2613
2614 if (!mSleepTimeout) {
2615 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002616 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2617 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002618 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2619 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2620 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002621 }
2622
2623 if (mStoppingActivities.size() > 0) {
2624 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002625 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002626 + mStoppingActivities.size() + " activities");
2627 scheduleIdleLocked();
2628 dontSleep = true;
2629 }
2630
2631 if (mGoingToSleepActivities.size() > 0) {
2632 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002633 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002634 + mGoingToSleepActivities.size() + " activities");
2635 dontSleep = true;
2636 }
2637
2638 if (dontSleep) {
2639 return;
2640 }
2641 }
2642
Craig Mautnere0a38842013-12-16 16:14:02 -08002643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2646 stacks.get(stackNdx).goToSleep();
2647 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002648 }
2649
2650 removeSleepTimeouts();
2651
2652 if (mGoingToSleep.isHeld()) {
2653 mGoingToSleep.release();
2654 }
2655 if (mService.mShuttingDown) {
2656 mService.notifyAll();
2657 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002658 }
2659
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002660 boolean reportResumedActivityLocked(ActivityRecord r) {
2661 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002662 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002663 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002664 }
2665 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002666 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002667 mWindowManager.executeAppTransition();
2668 return true;
2669 }
2670 return false;
2671 }
2672
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002676 int stackNdx = stacks.size() - 1;
2677 while (stackNdx >= 0) {
2678 stacks.get(stackNdx).handleAppCrashLocked(app);
2679 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 }
2682 }
2683
Jose Lima4b6c6692014-08-12 17:41:12 -07002684 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002685 final ActivityStack stack = r.task.stack;
2686 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002687 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2688 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002689 return false;
2690 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002691 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002692 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2693 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002694
2695 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002696 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002697 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002698 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002699 return true;
2700 }
2701
2702 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002703 if (visible && top.fullscreen) {
2704 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002705 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2706 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2707 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2708 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002709 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002710 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2711 // Only the activity set as currently visible behind should actively reset its
2712 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002713 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2714 "requestVisibleBehind: returning visible=" + visible
2715 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2716 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002717 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002718 }
2719
Jose Lima4b6c6692014-08-12 17:41:12 -07002720 stack.setVisibleBehindActivity(visible ? r : null);
2721 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002722 // If there is a translucent home activity, we need to force it stop being translucent,
2723 // because we can't depend on the application to necessarily perform that operation.
2724 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002725 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002726 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002727 mService.convertFromTranslucent(next.appToken);
2728 }
2729 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002730 if (top.app != null && top.app.thread != null) {
2731 // Notify the top app of the change.
2732 try {
2733 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2734 } catch (RemoteException e) {
2735 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002736 }
2737 return true;
2738 }
2739
Craig Mautnerbb742462014-07-07 15:28:55 -07002740 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2741 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2742 r.mLaunchTaskBehind = false;
2743 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002744 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002745 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002746 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002747 mWindowManager.setAppVisibility(r.appToken, false);
2748 }
2749
2750 void scheduleLaunchTaskBehindComplete(IBinder token) {
2751 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2752 }
2753
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002754 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2755 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002756 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002757 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2758 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002759 final int topStackNdx = stacks.size() - 1;
2760 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2761 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002762 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002763 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002764 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002765 }
2766
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002767 void invalidateTaskLayers() {
2768 mTaskLayersChanged = true;
2769 }
2770
2771 void rankTaskLayersIfNeeded() {
2772 if (!mTaskLayersChanged) {
2773 return;
2774 }
2775 mTaskLayersChanged = false;
2776 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2777 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2778 int baseLayer = 0;
2779 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2780 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2781 }
2782 }
2783 }
2784
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002785 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2786 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2787 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2788 final int topStackNdx = stacks.size() - 1;
2789 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2790 final ActivityStack stack = stacks.get(stackNdx);
2791 stack.clearOtherAppTimeTrackers(except);
2792 }
2793 }
2794 }
2795
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002799 final int numStacks = stacks.size();
2800 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2801 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002802 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002803 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002804 }
2805 }
2806
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002807 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2808 // Examine all activities currently running in the process.
2809 TaskRecord firstTask = null;
2810 // Tasks is non-null only if two or more tasks are found.
2811 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002812 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2813 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002814 ActivityRecord r = app.activities.get(i);
2815 // First, if we find an activity that is in the process of being destroyed,
2816 // then we just aren't going to do anything for now; we want things to settle
2817 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002818 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002819 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002820 return;
2821 }
2822 // Don't consider any activies that are currently not in a state where they
2823 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002824 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2825 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002827 continue;
2828 }
2829 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002830 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002831 + " from " + r);
2832 if (firstTask == null) {
2833 firstTask = r.task;
2834 } else if (firstTask != r.task) {
2835 if (tasks == null) {
2836 tasks = new ArraySet<>();
2837 tasks.add(firstTask);
2838 }
2839 tasks.add(r.task);
2840 }
2841 }
2842 }
2843 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002844 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002845 return;
2846 }
2847 // If we have activities in multiple tasks that are in a position to be destroyed,
2848 // let's iterate through the tasks and release the oldest one.
2849 final int numDisplays = mActivityDisplays.size();
2850 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2851 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2852 // Step through all stacks starting from behind, to hit the oldest things first.
2853 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2854 final ActivityStack stack = stacks.get(stackNdx);
2855 // Try to release activities in this stack; if we manage to, we are done.
2856 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2857 return;
2858 }
2859 }
2860 }
2861 }
2862
Amith Yamasani37a40c22015-06-17 13:25:42 -07002863 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002864 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002865 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002866 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002867
Craig Mautner858d8a62013-04-23 17:08:34 -07002868 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002869 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2870 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002871 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002872 final ActivityStack stack = stacks.get(stackNdx);
2873 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002874 TaskRecord task = stack.topTask();
2875 if (task != null) {
2876 mWindowManager.moveTaskToTop(task.taskId);
2877 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002878 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002879 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002880
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002881 ActivityStack stack = getStack(restoreStackId);
2882 if (stack == null) {
2883 stack = mHomeStack;
2884 }
2885 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002886 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002887 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002888 } else {
2889 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002890 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002891 }
Craig Mautner93529a42013-10-04 15:03:13 -07002892 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002893 }
2894
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002895 /** Checks whether the userid is a profile of the current user. */
2896 boolean isCurrentProfileLocked(int userId) {
2897 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002898 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002899 }
2900
Chong Zhang45c25ce2015-08-10 22:18:26 -07002901 /** Checks whether the activity should be shown for current user. */
2902 boolean okToShowLocked(ActivityRecord r) {
2903 return r != null && (isCurrentProfileLocked(r.userId)
2904 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2905 }
2906
Craig Mautnerde4ef022013-04-07 19:01:33 -07002907 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002908 ArrayList<ActivityRecord> stops = null;
2909
2910 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002911 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2912 ActivityRecord s = mStoppingActivities.get(activityNdx);
2913 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002914 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002915 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2916 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002917 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002918 if (s.finishing) {
2919 // If this activity is finishing, it is sitting on top of
2920 // everyone else but we now know it is no longer needed...
2921 // so get rid of it. Otherwise, we need to go through the
2922 // normal flow and hide it once we determine that it is
2923 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002924 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002925 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002926 }
2927 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002928 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002929 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002930 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002931 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002932 }
2933 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002934 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002935 }
2936 }
2937
2938 return stops;
2939 }
2940
Craig Mautnercf910b02013-04-23 11:23:27 -07002941 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002942 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2943 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2944 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2945 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002946 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002947 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002948 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002949 if (r == null) Slog.e(TAG,
2950 "validateTop...: null top activity, stack=" + stack);
2951 else {
2952 final ActivityRecord pausing = stack.mPausingActivity;
2953 if (pausing != null && pausing == r) Slog.e(TAG,
2954 "validateTop...: top stack has pausing activity r=" + r
2955 + " state=" + state);
2956 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2957 "validateTop...: activity in front not resumed r=" + r
2958 + " state=" + state);
2959 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002960 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002961 final ActivityRecord resumed = stack.mResumedActivity;
2962 if (resumed != null && resumed == r) Slog.e(TAG,
2963 "validateTop...: back stack has resumed activity r=" + r
2964 + " state=" + state);
2965 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2966 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002967 }
2968 }
2969 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002970 }
2971
Craig Mautnere0570202015-05-13 13:06:11 -07002972 private String lockTaskModeToString() {
2973 switch (mLockTaskModeState) {
2974 case LOCK_TASK_MODE_LOCKED:
2975 return "LOCKED";
2976 case LOCK_TASK_MODE_PINNED:
2977 return "PINNED";
2978 case LOCK_TASK_MODE_NONE:
2979 return "NONE";
2980 default: return "unknown=" + mLockTaskModeState;
2981 }
2982 }
2983
Craig Mautner27084302013-03-25 08:05:25 -07002984 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002985 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002986 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002987 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002988 pw.print(prefix);
2989 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002990 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002991 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002992 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2993 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2994 if (packages.size() > 0) {
2995 pw.println(" mLockTaskPackages (userId:packages)=");
2996 for (int i = 0; i < packages.size(); ++i) {
2997 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2998 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2999 }
3000 }
3001 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003002 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003003
Craig Mautner20e72272013-04-01 13:45:53 -07003004 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003005 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003006 }
3007
Dianne Hackborn390517b2013-05-30 15:03:32 -07003008 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3009 boolean needSep, String prefix) {
3010 if (activity != null) {
3011 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3012 if (needSep) {
3013 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003014 }
3015 pw.print(prefix);
3016 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003017 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003018 }
3019 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003020 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003021 }
3022
Craig Mautner8d341ef2013-03-26 09:03:27 -07003023 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3024 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003025 boolean printed = false;
3026 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003027 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3028 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003029 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003030 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003032 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 final ActivityStack stack = stacks.get(stackNdx);
3034 StringBuilder stackHeader = new StringBuilder(128);
3035 stackHeader.append(" Stack #");
3036 stackHeader.append(stack.mStackId);
3037 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003038 stackHeader.append("\n");
3039 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3040 stackHeader.append("\n");
3041 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003042 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3043 needSep, stackHeader.toString());
3044 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3045 !dumpAll, false, dumpPackage, true,
3046 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003047
Craig Mautner4a1cb222013-12-04 16:14:06 -08003048 needSep = printed;
3049 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3050 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003051 if (pr) {
3052 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003054 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003055 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3056 " mResumedActivity: ");
3057 if (pr) {
3058 printed = true;
3059 needSep = false;
3060 }
3061 if (dumpAll) {
3062 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3063 " mLastPausedActivity: ");
3064 if (pr) {
3065 printed = true;
3066 needSep = true;
3067 }
3068 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3069 needSep, " mLastNoHistoryActivity: ");
3070 }
3071 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003072 }
3073 }
3074
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003075 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3076 false, dumpPackage, true, " Activities waiting to finish:", null);
3077 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3078 false, dumpPackage, true, " Activities waiting to stop:", null);
3079 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3080 false, dumpPackage, true, " Activities waiting for another to become visible:",
3081 null);
3082 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3083 false, dumpPackage, true, " Activities waiting to sleep:", null);
3084 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3085 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003086
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003087 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003088 }
3089
Dianne Hackborn390517b2013-05-30 15:03:32 -07003090 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003092 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003093 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003094 String innerPrefix = null;
3095 String[] args = null;
3096 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003097 for (int i=list.size()-1; i>=0; i--) {
3098 final ActivityRecord r = list.get(i);
3099 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3100 continue;
3101 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003102 if (innerPrefix == null) {
3103 innerPrefix = prefix + " ";
3104 args = new String[0];
3105 }
3106 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003107 final boolean full = !brief && (complete || !r.isInHistory());
3108 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003109 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003110 needNL = false;
3111 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003112 if (header1 != null) {
3113 pw.println(header1);
3114 header1 = null;
3115 }
3116 if (header2 != null) {
3117 pw.println(header2);
3118 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003119 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 if (lastTask != r.task) {
3121 lastTask = r.task;
3122 pw.print(prefix);
3123 pw.print(full ? "* " : " ");
3124 pw.println(lastTask);
3125 if (full) {
3126 lastTask.dump(pw, prefix + " ");
3127 } else if (complete) {
3128 // Complete + brief == give a summary. Isn't that obvious?!?
3129 if (lastTask.intent != null) {
3130 pw.print(prefix); pw.print(" ");
3131 pw.println(lastTask.intent.toInsecureStringWithClip());
3132 }
3133 }
3134 }
3135 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3136 pw.print(" #"); pw.print(i); pw.print(": ");
3137 pw.println(r);
3138 if (full) {
3139 r.dump(pw, innerPrefix);
3140 } else if (complete) {
3141 // Complete + brief == give a summary. Isn't that obvious?!?
3142 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3143 if (r.app != null) {
3144 pw.print(innerPrefix); pw.println(r.app);
3145 }
3146 }
3147 if (client && r.app != null && r.app.thread != null) {
3148 // flush anything that is already in the PrintWriter since the thread is going
3149 // to write to the file descriptor directly
3150 pw.flush();
3151 try {
3152 TransferPipe tp = new TransferPipe();
3153 try {
3154 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3155 r.appToken, innerPrefix, args);
3156 // Short timeout, since blocking here can
3157 // deadlock with the application.
3158 tp.go(fd, 2000);
3159 } finally {
3160 tp.kill();
3161 }
3162 } catch (IOException e) {
3163 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3164 } catch (RemoteException e) {
3165 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3166 }
3167 needNL = true;
3168 }
3169 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003170 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003171 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003172
Craig Mautnerf3333272013-04-22 10:55:53 -07003173 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003174 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3175 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003176 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3177 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003178 }
3179
3180 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003181 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003182 }
3183
3184 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003185 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3186 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003187 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3188 }
3189
Craig Mautner05d29032013-05-03 13:40:13 -07003190 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003191 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3192 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3193 }
Craig Mautner05d29032013-05-03 13:40:13 -07003194 }
3195
Craig Mautner0eea92c2013-05-16 13:35:39 -07003196 void removeSleepTimeouts() {
3197 mSleepTimeout = false;
3198 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3199 }
3200
3201 final void scheduleSleepTimeout() {
3202 removeSleepTimeouts();
3203 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3204 }
3205
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 @Override
3207 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003208 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003209 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3210 }
3211
3212 @Override
3213 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003214 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003215 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3216 }
3217
3218 @Override
3219 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003220 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3222 }
3223
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003224 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003225 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003226 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003227 newDisplay = mActivityDisplays.get(displayId) == null;
3228 if (newDisplay) {
3229 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003230 if (activityDisplay.mDisplay == null) {
3231 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3232 return;
3233 }
Craig Mautner4504de52013-12-20 09:06:56 -08003234 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003235 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003236 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 }
Craig Mautner4504de52013-12-20 09:06:56 -08003238 if (newDisplay) {
3239 mWindowManager.onDisplayAdded(displayId);
3240 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003241 }
3242
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003243 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3244 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3245 return;
3246 }
3247 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3248 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3249 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3250 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3251 }
3252
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003253 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003254 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003255 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3256 if (activityDisplay != null) {
3257 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003258 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003259 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003260 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003261 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003262 }
3263 }
3264 mWindowManager.onDisplayRemoved(displayId);
3265 }
3266
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003267 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003268 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003269 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3270 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 // TODO: Update the bounds.
3272 }
3273 }
3274 mWindowManager.onDisplayChanged(displayId);
3275 }
3276
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003277 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 StackInfo info = new StackInfo();
3279 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3280 info.displayId = Display.DEFAULT_DISPLAY;
3281 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003282 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283
3284 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3285 final int numTasks = tasks.size();
3286 int[] taskIds = new int[numTasks];
3287 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003288 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003289 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 for (int i = 0; i < numTasks; ++i) {
3291 final TaskRecord task = tasks.get(i);
3292 taskIds[i] = task.taskId;
3293 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3294 : task.realActivity != null ? task.realActivity.flattenToString()
3295 : task.getTopActivity() != null ? task.getTopActivity().packageName
3296 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003297 taskBounds[i] = new Rect();
3298 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003299 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 }
3301 info.taskIds = taskIds;
3302 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003303 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003304 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 return info;
3306 }
3307
3308 StackInfo getStackInfoLocked(int stackId) {
3309 ActivityStack stack = getStack(stackId);
3310 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003311 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003312 }
3313 return null;
3314 }
3315
3316 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003317 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003318 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3319 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003321 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 }
3323 }
3324 return list;
3325 }
3326
Craig Mautner15df08a2015-04-01 12:17:18 -07003327 TaskRecord getLockedTaskLocked() {
3328 final int top = mLockTaskModeTasks.size() - 1;
3329 if (top >= 0) {
3330 return mLockTaskModeTasks.get(top);
3331 }
3332 return null;
3333 }
3334
3335 boolean isLockedTask(TaskRecord task) {
3336 return mLockTaskModeTasks.contains(task);
3337 }
3338
3339 boolean isLastLockedTask(TaskRecord task) {
3340 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3341 }
3342
3343 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003344 if (!mLockTaskModeTasks.remove(task)) {
3345 return;
3346 }
3347 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3348 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003349 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003350 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3351 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003352 final Message lockTaskMsg = Message.obtain();
3353 lockTaskMsg.arg1 = task.userId;
3354 lockTaskMsg.what = LOCK_TASK_END_MSG;
3355 mHandler.sendMessage(lockTaskMsg);
3356 }
3357 }
3358
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003359 void showNonResizeableDockToastIfNeeded(
3360 TaskRecord task, int preferredStackId, int actualStackId) {
3361 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3362 return;
3363 }
3364
Jorim Jaggi2adba072016-03-03 13:43:39 +01003365 if (!task.canGoInDockedStack()) {
3366 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003367 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
Jorim Jaggi2adba072016-03-03 13:43:39 +01003368 } else if (task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
3369 String packageName = task.getTopActivity() != null
3370 ? task.getTopActivity().appInfo.packageName : null;
3371 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3372 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003373 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003374 }
3375
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003376 void showLockTaskToast() {
3377 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003378 }
3379
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003380 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3381 if (mLockTaskModeTasks.contains(task)) {
3382 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3383 }
3384 }
3385
Craig Mautner432f64e2015-05-20 14:59:57 -07003386 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3387 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003388 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003389 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003390 final TaskRecord lockedTask = getLockedTaskLocked();
3391 if (lockedTask != null) {
3392 removeLockedTaskLocked(lockedTask);
3393 if (!mLockTaskModeTasks.isEmpty()) {
3394 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003395 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3396 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003397 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003398 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003399 return;
3400 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003401 }
Craig Mautnere0570202015-05-13 13:06:11 -07003402 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3403 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003404 return;
3405 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003406
3407 // Should have already been checked, but do it again.
3408 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003409 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3410 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003411 return;
3412 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003413 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003414 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003415 return;
3416 }
3417
3418 if (mLockTaskModeTasks.isEmpty()) {
3419 // First locktask.
3420 final Message lockTaskMsg = Message.obtain();
3421 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3422 lockTaskMsg.arg1 = task.userId;
3423 lockTaskMsg.what = LOCK_TASK_START_MSG;
3424 lockTaskMsg.arg2 = lockTaskModeState;
3425 mHandler.sendMessage(lockTaskMsg);
3426 }
3427 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003428 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3429 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003430 mLockTaskModeTasks.remove(task);
3431 mLockTaskModeTasks.add(task);
3432
3433 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003434 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003435 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003436
3437 if (andResume) {
3438 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003439 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003440 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003441 }
3442
3443 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003444 return isLockTaskModeViolation(task, false);
3445 }
3446
3447 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3448 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003449 return false;
3450 }
3451 final int lockTaskAuth = task.mLockTaskAuth;
3452 switch (lockTaskAuth) {
3453 case LOCK_TASK_AUTH_DONT_LOCK:
3454 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003455 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003456 case LOCK_TASK_AUTH_LAUNCHABLE:
3457 case LOCK_TASK_AUTH_WHITELISTED:
3458 return false;
3459 case LOCK_TASK_AUTH_PINNABLE:
3460 // Pinnable tasks can't be launched on top of locktask tasks.
3461 return !mLockTaskModeTasks.isEmpty();
3462 default:
3463 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3464 return true;
3465 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003466 }
3467
Craig Mautner15df08a2015-04-01 12:17:18 -07003468 void onLockTaskPackagesUpdatedLocked() {
3469 boolean didSomething = false;
3470 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3471 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003472 final boolean wasWhitelisted =
3473 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3474 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003475 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003476 final boolean isWhitelisted =
3477 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3478 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3479 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003480 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003481 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3482 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003483 removeLockedTaskLocked(lockedTask);
3484 lockedTask.performClearTaskLocked();
3485 didSomething = true;
3486 }
3487 }
3488 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3489 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3490 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3491 final ActivityStack stack = stacks.get(stackNdx);
3492 stack.onLockTaskPackagesUpdatedLocked();
3493 }
3494 }
Craig Mautnere0570202015-05-13 13:06:11 -07003495 final ActivityRecord r = topRunningActivityLocked();
3496 final TaskRecord task = r != null ? r.task : null;
3497 if (mLockTaskModeTasks.isEmpty() && task != null
3498 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3499 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003500 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3501 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3502 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3503 false);
3504 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003505 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003506 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003507 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003508 }
3509 }
3510
Benjamin Franz43261142015-02-11 15:59:44 +00003511 int getLockTaskModeState() {
3512 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003513 }
3514
Jorim Jaggife89d122015-12-22 16:28:44 +01003515 void activityRelaunchedLocked(IBinder token) {
3516 mWindowManager.notifyAppRelaunchingFinished(token);
3517 }
3518
3519 void activityRelaunchingLocked(ActivityRecord r) {
3520 mWindowManager.notifyAppRelaunching(r.appToken);
3521 }
3522
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003523 void logStackState() {
3524 mActivityMetricsLogger.logWindowState();
3525 }
3526
Wale Ogunwale22e25262016-02-01 10:32:02 -08003527 void scheduleReportMultiWindowChanged(TaskRecord task) {
3528 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3529 final ActivityRecord r = task.mActivities.get(i);
3530 if (r.app != null && r.app.thread != null) {
3531 mMultiWindowModeChangedActivities.add(r);
3532 }
3533 }
3534
3535 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3536 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3537 }
3538 }
3539
3540 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3541 final ActivityStack stack = task.stack;
3542 if (prevStack == null || prevStack == stack
3543 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3544 return;
3545 }
3546
3547 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3548 final ActivityRecord r = task.mActivities.get(i);
3549 if (r.app != null && r.app.thread != null) {
3550 mPipModeChangedActivities.add(r);
3551 }
3552 }
3553
3554 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3555 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3556 }
3557 }
3558
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003559 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003560
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003561 public ActivityStackSupervisorHandler(Looper looper) {
3562 super(looper);
3563 }
3564
Craig Mautnerf3333272013-04-22 10:55:53 -07003565 void activityIdleInternal(ActivityRecord r) {
3566 synchronized (mService) {
3567 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3568 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003569 }
3570
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003571 @Override
3572 public void handleMessage(Message msg) {
3573 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003574 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3575 synchronized (mService) {
3576 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3577 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3578 r.scheduleMultiWindowChanged();
3579 }
3580 }
3581 } break;
3582 case REPORT_PIP_MODE_CHANGED_MSG: {
3583 synchronized (mService) {
3584 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3585 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3586 r.schedulePictureInPictureChanged();
3587 }
3588 }
3589 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003590 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3592 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003593 if (mService.mDidDexOpt) {
3594 mService.mDidDexOpt = false;
3595 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3596 nmsg.obj = msg.obj;
3597 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3598 return;
3599 }
3600 // We don't at this point know if the activity is fullscreen,
3601 // so we need to be conservative and assume it isn't.
3602 activityIdleInternal((ActivityRecord)msg.obj);
3603 } break;
3604 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003605 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003606 activityIdleInternal((ActivityRecord)msg.obj);
3607 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003608 case RESUME_TOP_ACTIVITY_MSG: {
3609 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003610 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003611 }
3612 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003613 case SLEEP_TIMEOUT_MSG: {
3614 synchronized (mService) {
3615 if (mService.isSleepingOrShuttingDown()) {
3616 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3617 mSleepTimeout = true;
3618 checkReadyForSleepLocked();
3619 }
3620 }
3621 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003622 case LAUNCH_TIMEOUT_MSG: {
3623 if (mService.mDidDexOpt) {
3624 mService.mDidDexOpt = false;
3625 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3626 return;
3627 }
3628 synchronized (mService) {
3629 if (mLaunchingActivity.isHeld()) {
3630 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3631 if (VALIDATE_WAKE_LOCK_CALLER
3632 && Binder.getCallingUid() != Process.myUid()) {
3633 throw new IllegalStateException("Calling must be system uid");
3634 }
3635 mLaunchingActivity.release();
3636 }
3637 }
3638 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003640 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003641 } break;
3642 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003643 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003644 } break;
3645 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003646 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003647 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003648 case CONTAINER_CALLBACK_VISIBILITY: {
3649 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003650 final IActivityContainerCallback callback = container.mCallback;
3651 if (callback != null) {
3652 try {
3653 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3654 } catch (RemoteException e) {
3655 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003656 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003657 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003658 case LOCK_TASK_START_MSG: {
3659 // When lock task starts, we disable the status bars.
3660 try {
Jason Monk62515be2014-05-21 16:06:19 -04003661 if (mLockTaskNotify == null) {
3662 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003663 }
Jason Monk62515be2014-05-21 16:06:19 -04003664 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003665 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003666 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003667 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003668 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003669 flags = StatusBarManager.DISABLE_MASK
3670 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003671 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003672 flags = StatusBarManager.DISABLE_MASK
3673 & (~StatusBarManager.DISABLE_BACK)
3674 & (~StatusBarManager.DISABLE_HOME)
3675 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003676 }
3677 getStatusBarService().disable(flags, mToken,
3678 mService.mContext.getPackageName());
3679 }
3680 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003681 if (getDevicePolicyManager() != null) {
3682 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003683 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003684 }
justinzhang5286d3f2014-05-12 17:06:01 -04003685 } catch (RemoteException ex) {
3686 throw new RuntimeException(ex);
3687 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003688 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003689 case LOCK_TASK_END_MSG: {
3690 // When lock task ends, we enable the status bars.
3691 try {
Jason Monk62515be2014-05-21 16:06:19 -04003692 if (getStatusBarService() != null) {
3693 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3694 mService.mContext.getPackageName());
3695 }
3696 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003697 if (getDevicePolicyManager() != null) {
3698 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3699 msg.arg1);
3700 }
Jason Monk62515be2014-05-21 16:06:19 -04003701 if (mLockTaskNotify == null) {
3702 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3703 }
3704 mLockTaskNotify.show(false);
3705 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003706 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003707 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003708 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003709 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003710 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003711 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003712 new LockPatternUtils(mService.mContext)
3713 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003714 }
3715 } catch (SettingNotFoundException e) {
3716 // No setting, don't lock.
3717 }
justinzhang5286d3f2014-05-12 17:06:01 -04003718 } catch (RemoteException ex) {
3719 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003720 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003721 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003722 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003723 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003724 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3725 if (mLockTaskNotify == null) {
3726 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3727 }
3728 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3729 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003730 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3731 final ActivityContainer container = (ActivityContainer) msg.obj;
3732 final IActivityContainerCallback callback = container.mCallback;
3733 if (callback != null) {
3734 try {
3735 callback.onAllActivitiesComplete(container.asBinder());
3736 } catch (RemoteException e) {
3737 }
3738 }
3739 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003740 case LAUNCH_TASK_BEHIND_COMPLETE: {
3741 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003742 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003743 if (r != null) {
3744 handleLaunchTaskBehindCompleteLocked(r);
3745 }
3746 }
3747 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003748
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003749 }
3750 }
3751 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003752
Ying Wangb081a592014-04-22 15:20:16 -07003753 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003754 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3755 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003756 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003757 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003758 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003759 ActivityRecord mParentActivity = null;
3760 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003761
Craig Mautnere3a00d72014-04-16 08:31:19 -07003762 boolean mVisible = true;
3763
Craig Mautner4a1cb222013-12-04 16:14:06 -08003764 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003765 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003766
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003767 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3768 final static int CONTAINER_STATE_NO_SURFACE = 1;
3769 final static int CONTAINER_STATE_FINISHING = 2;
3770 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3771
3772 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 synchronized (mService) {
3774 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003775 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003776 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003777 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003779 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003781 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003782 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003783 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003784 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003785 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003786 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 }
3788
3789 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003790 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003792 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3793 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 return;
3795 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003796 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 }
3798 }
3799
3800 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003801 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003802 synchronized (mService) {
3803 if (mActivityDisplay != null) {
3804 return mActivityDisplay.mDisplayId;
3805 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003806 }
3807 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003808 }
3809
Jeff Brownca9bc702014-02-11 14:32:56 -08003810 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003811 public int getStackId() {
3812 synchronized (mService) {
3813 return mStackId;
3814 }
3815 }
3816
3817 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003818 public boolean injectEvent(InputEvent event) {
3819 final long origId = Binder.clearCallingIdentity();
3820 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003821 synchronized (mService) {
3822 if (mActivityDisplay != null) {
3823 return mInputManagerInternal.injectInputEvent(event,
3824 mActivityDisplay.mDisplayId,
3825 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3826 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003827 }
3828 return false;
3829 } finally {
3830 Binder.restoreCallingIdentity(origId);
3831 }
3832 }
3833
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003834 @Override
3835 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003836 synchronized (mService) {
3837 if (mContainerState == CONTAINER_STATE_FINISHING) {
3838 return;
3839 }
3840 mContainerState = CONTAINER_STATE_FINISHING;
3841
Craig Mautnerd163e752014-06-13 17:18:47 -07003842 long origId = Binder.clearCallingIdentity();
3843 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003844 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003845 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003846 } finally {
3847 Binder.restoreCallingIdentity(origId);
3848 }
3849 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003850 }
3851
Craig Mautner60257702014-09-17 15:02:33 -07003852 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003853 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003854 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003855 if (mActivityDisplay != null) {
3856 mActivityDisplay.detachActivitiesLocked(mStack);
3857 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003858 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859 }
3860 }
3861
3862 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003863 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003864 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 }
3866
3867 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003868 public final int startActivityIntentSender(IIntentSender intentSender)
3869 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003870 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3871
3872 if (!(intentSender instanceof PendingIntentRecord)) {
3873 throw new IllegalArgumentException("Bad PendingIntent object");
3874 }
3875
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003876 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003877 Binder.getCallingUid(), mCurrentUser, false,
3878 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003879
3880 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3881 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3882 pendingIntent.key.requestResolvedType);
3883
3884 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3885 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3886 }
3887
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003888 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003889 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003890 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003891 throw new SecurityException(
3892 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3893 }
3894 }
3895
Craig Mautnerdf88d732014-01-27 09:21:32 -08003896 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003897 public IBinder asBinder() {
3898 return this;
3899 }
3900
Craig Mautner4504de52013-12-20 09:06:56 -08003901 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003902 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003903 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003904 }
3905
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 ActivityStackSupervisor getOuter() {
3907 return ActivityStackSupervisor.this;
3908 }
3909
Craig Mautnerd163e752014-06-13 17:18:47 -07003910 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003911 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003912 }
3913
Craig Mautner6985bad2014-04-21 15:22:06 -07003914 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003915 void setVisible(boolean visible) {
3916 if (mVisible != visible) {
3917 mVisible = visible;
3918 if (mCallback != null) {
3919 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3920 0 /* unused */, this).sendToTarget();
3921 }
3922 }
3923 }
3924
Craig Mautner6985bad2014-04-21 15:22:06 -07003925 void setDrawn() {
3926 }
3927
Craig Mautner1b4bf852014-05-26 15:06:32 -07003928 // You can always start a new task on a regular ActivityStack.
3929 boolean isEligibleForNewTasks() {
3930 return true;
3931 }
3932
Craig Mautnerd163e752014-06-13 17:18:47 -07003933 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003934 detachLocked();
3935 deleteActivityContainer(this);
3936 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003937 }
3938
Craig Mautner34b73df2014-01-12 21:11:08 -08003939 @Override
3940 public String toString() {
3941 return mIdString + (mActivityDisplay == null ? "N" : "A");
3942 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003943 }
3944
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 private class VirtualActivityContainer extends ActivityContainer {
3946 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003947 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003948
3949 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3950 super(getNextStackId());
3951 mParentActivity = parent;
3952 mCallback = callback;
3953 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003954 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003955 }
3956
3957 @Override
3958 public void setSurface(Surface surface, int width, int height, int density) {
3959 super.setSurface(surface, width, height, density);
3960
3961 synchronized (mService) {
3962 final long origId = Binder.clearCallingIdentity();
3963 try {
3964 setSurfaceLocked(surface, width, height, density);
3965 } finally {
3966 Binder.restoreCallingIdentity(origId);
3967 }
3968 }
3969 }
3970
3971 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3972 if (mContainerState == CONTAINER_STATE_FINISHING) {
3973 return;
3974 }
3975 VirtualActivityDisplay virtualActivityDisplay =
3976 (VirtualActivityDisplay) mActivityDisplay;
3977 if (virtualActivityDisplay == null) {
3978 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003979 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003980 mActivityDisplay = virtualActivityDisplay;
3981 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003982 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003983 }
3984
3985 if (mSurface != null) {
3986 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003987 }
3988
Craig Mautner6985bad2014-04-21 15:22:06 -07003989 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003990 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003991 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003992 } else {
3993 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003994 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003995 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003996 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003997 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003998 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003999
Craig Mautnerd163e752014-06-13 17:18:47 -07004000 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004001
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004002 if (DEBUG_STACK) Slog.d(TAG_STACK,
4003 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004004 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005
Craig Mautner6985bad2014-04-21 15:22:06 -07004006 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004007 boolean isAttachedLocked() {
4008 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004009 }
4010
4011 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004012 void setDrawn() {
4013 synchronized (mService) {
4014 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004015 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004016 }
4017 }
4018
Craig Mautner1b4bf852014-05-26 15:06:32 -07004019 // Never start a new task on an ActivityView if it isn't explicitly specified.
4020 @Override
4021 boolean isEligibleForNewTasks() {
4022 return false;
4023 }
4024
Craig Mautnerd163e752014-06-13 17:18:47 -07004025 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004026 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004027 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4028 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4029 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4030 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4031 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004032 }
4033 }
4034
Craig Mautner4a1cb222013-12-04 16:14:06 -08004035 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4036 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004037 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004039 int mDisplayId;
4040 Display mDisplay;
4041 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004042
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4044 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004045 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046
Jose Lima4b6c6692014-08-12 17:41:12 -07004047 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004048
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004049 ActivityDisplay() {
4050 }
Craig Mautner4504de52013-12-20 09:06:56 -08004051
Craig Mautner1a70a162014-09-13 12:09:31 -07004052 // After instantiation, check that mDisplay is not null before using this. The alternative
4053 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004054 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004055 final Display display = mDisplayManager.getDisplay(displayId);
4056 if (display == null) {
4057 return;
4058 }
4059 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004060 }
4061
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004062 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004063 mDisplay = display;
4064 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004066 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004067
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004068 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004069 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004070 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4071 + " onTop=" + onTop);
4072 if (onTop) {
4073 mStacks.add(stack);
4074 } else {
4075 mStacks.add(0, stack);
4076 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 }
4078
Craig Mautnere0a38842013-12-16 16:14:02 -08004079 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004080 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004081 + " from displayId=" + mDisplayId);
4082 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084
Jose Lima4b6c6692014-08-12 17:41:12 -07004085 void setVisibleBehindActivity(ActivityRecord r) {
4086 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004087 }
4088
Jose Lima4b6c6692014-08-12 17:41:12 -07004089 boolean hasVisibleBehindActivity() {
4090 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004091 }
4092
Craig Mautner34b73df2014-01-12 21:11:08 -08004093 @Override
4094 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004095 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4096 }
4097 }
4098
4099 class VirtualActivityDisplay extends ActivityDisplay {
4100 VirtualDisplay mVirtualDisplay;
4101
Craig Mautner6985bad2014-04-21 15:22:06 -07004102 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004103 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004104 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4105 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4106 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4107 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004108
4109 init(mVirtualDisplay.getDisplay());
4110
4111 mWindowManager.handleDisplayAdded(mDisplayId);
4112 }
4113
4114 void setSurface(Surface surface) {
4115 if (mVirtualDisplay != null) {
4116 mVirtualDisplay.setSurface(surface);
4117 }
4118 }
4119
4120 @Override
4121 void detachActivitiesLocked(ActivityStack stack) {
4122 super.detachActivitiesLocked(stack);
4123 if (mVirtualDisplay != null) {
4124 mVirtualDisplay.release();
4125 mVirtualDisplay = null;
4126 }
4127 }
4128
4129 @Override
4130 public String toString() {
4131 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004132 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004133 }
Jose Lima58e66d62014-05-27 20:00:27 -07004134
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004135 /**
4136 * Adjust bounds to stay within stack bounds.
4137 *
4138 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4139 * that keep them unchanged, but be contained within the stack bounds.
4140 *
4141 * @param bounds Bounds to be adjusted.
4142 * @param stackBounds Bounds within which the other bounds should remain.
4143 */
4144 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4145 if (stackBounds == null || stackBounds.contains(bounds)) {
4146 return;
4147 }
4148
4149 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4150 final int maxRight = stackBounds.right
4151 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4152 int horizontalDiff = stackBounds.left - bounds.left;
4153 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4154 || (bounds.left + horizontalDiff >= maxRight)) {
4155 horizontalDiff = maxRight - bounds.left;
4156 }
4157 bounds.left += horizontalDiff;
4158 bounds.right += horizontalDiff;
4159 }
4160
4161 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4162 final int maxBottom = stackBounds.bottom
4163 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4164 int verticalDiff = stackBounds.top - bounds.top;
4165 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4166 || (bounds.top + verticalDiff >= maxBottom)) {
4167 verticalDiff = maxBottom - bounds.top;
4168 }
4169 bounds.top += verticalDiff;
4170 bounds.bottom += verticalDiff;
4171 }
4172 }
4173
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004174 ActivityStack findStackBehind(ActivityStack stack) {
4175 // TODO(multi-display): We are only looking for stacks on the default display.
4176 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4177 if (display == null) {
4178 return null;
4179 }
4180 final ArrayList<ActivityStack> stacks = display.mStacks;
4181 for (int i = stacks.size() - 1; i >= 0; i--) {
4182 if (stacks.get(i) == stack && i > 0) {
4183 return stacks.get(i - 1);
4184 }
4185 }
4186 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4187 + " in=" + stacks);
4188 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004189
4190 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4191 final TaskRecord task;
4192 final int callingUid;
4193 final String callingPackage;
4194 final Intent intent;
4195 final int userId;
4196 final ActivityOptions activityOptions = (bOptions != null)
4197 ? new ActivityOptions(bOptions) : null;
4198 final int launchStackId = (activityOptions != null)
4199 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
4200
4201 if (launchStackId == HOME_STACK_ID) {
4202 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4203 + taskId + " can't be launch in the home stack.");
4204 }
4205 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4206 if (task == null) {
4207 throw new IllegalArgumentException(
4208 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4209 }
4210
4211 if (launchStackId != INVALID_STACK_ID) {
4212 if (launchStackId == DOCKED_STACK_ID) {
4213 mWindowManager.setDockedStackCreateState(
4214 activityOptions.getDockCreateMode(), null /* initialBounds */);
4215
4216 // Defer updating the stack in which recents is until the app transition is done, to
4217 // not run into issues where we still need to draw the task in recents but the
4218 // docked stack is already created.
4219 deferUpdateBounds(HOME_STACK_ID);
4220 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4221 }
4222 if (task.stack.mStackId != launchStackId) {
4223 moveTaskToStackLocked(
4224 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4225 ANIMATE);
4226 }
4227 }
4228
4229 // If the user must confirm credentials (e.g. when first launching a work app and the
4230 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4231 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4232 && task.getRootActivity() != null) {
4233 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
4234
4235 // If we are launching the task in the docked stack, put it into resizing mode so
4236 // the window renders full-screen with the background filling the void. Also only
4237 // call this at the end to make sure that tasks exists on the window manager side.
4238 if (launchStackId == DOCKED_STACK_ID) {
4239 mResizingTasksDuringAnimation.add(task.taskId);
4240 mWindowManager.setTaskDockedResizing(task.taskId, true);
4241 }
4242 return ActivityManager.START_TASK_TO_FRONT;
4243 }
4244 callingUid = task.mCallingUid;
4245 callingPackage = task.mCallingPackage;
4246 intent = task.intent;
4247 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4248 userId = task.userId;
4249 return mService.startActivityInPackage(callingUid, callingPackage, intent, null, null, null,
4250 0, 0, bOptions, userId, null, task);
4251 }
Craig Mautner27084302013-03-25 08:05:25 -07004252}