blob: d364d8569db0c42b771273ec6e320dcf257e5e62 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070020import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070021import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080022import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080024import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070025import android.app.ActivityOptions;
26import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070027import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080028import android.app.IActivityContainer;
29import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070030import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080031import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070032import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070033import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040034import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040035import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070037import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070039import android.content.Intent;
40import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070042import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010045import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080047import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.hardware.display.DisplayManager;
49import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080050import android.hardware.display.DisplayManagerGlobal;
51import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080052import android.hardware.input.InputManager;
53import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.os.Binder;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070055import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070059import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070061import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070062import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070063import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040064import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070066import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070067import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070068import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010069import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070070import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070071import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040072import android.provider.Settings;
73import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070074import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070075import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070076import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080079import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080081import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080083import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080085
Dianne Hackborn85d558c2014-11-04 10:31:54 -080086import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070087import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040088import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070089import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040090import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094
Craig Mautner8d341ef2013-03-26 09:03:27 -070095import java.io.FileDescriptor;
96import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070097import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -070099import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800101import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700102import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700103
Jorim Jaggife89d122015-12-22 16:28:44 +0100104import static android.Manifest.permission.START_ANY_ACTIVITY;
105import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
106import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
107import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
108import static android.app.ActivityManager.RESIZE_MODE_FORCED;
109import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
110import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
111import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
112import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
113import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
114import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
115import static android.app.ActivityManager.StackId.HOME_STACK_ID;
116import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
117import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
119import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
120import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
121import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500122import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800123import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100124import static android.content.pm.PackageManager.PERMISSION_GRANTED;
125import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
150import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
151import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800152import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100153import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
154import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
155import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
156import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
157import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
158import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
159import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
160import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
161import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
162import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
163import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
164import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
166import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
170
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800172 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700173 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700175 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700176 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700180 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800182 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184
Craig Mautnerf3333272013-04-22 10:55:53 -0700185 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700187
Craig Mautner0eea92c2013-05-16 13:35:39 -0700188 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700190
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700191 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700193
Craig Mautner05d29032013-05-03 13:40:13 -0700194 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
195 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
196 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700198 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800199 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
200 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
201 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700202 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400203 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
204 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700205 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700206 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700207 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800208 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
209 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210
Wale Ogunwale040b4702015-08-06 18:10:50 -0700211 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Jason Monk62515be2014-05-21 16:06:19 -0400213 private static final String LOCK_TASK_TAG = "Lock-to-App";
214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 // Used to indicate if an object (e.g. stack) that we are trying to get
216 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800217 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700219 // Used to indicate that windows of activities should be preserved during the resize.
220 static final boolean PRESERVE_WINDOWS = true;
221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 // Used to indicate if an object (e.g. task) should be moved/created
223 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700224 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225
226 // Used to indicate that an objects (e.g. task) removal from its container
227 // (e.g. stack) is due to it moving to another container.
228 static final boolean MOVING = true;
229
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700230 // Force the focus to change to the stack we are moving a task to..
231 static final boolean FORCE_FOCUS = true;
232
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700233 // Restore task from the saved recents if it can't be found in any live stack.
234 static final boolean RESTORE_FROM_RECENTS = true;
235
Svetoslav7008b512015-06-24 18:47:07 -0700236 // Activity actions an app cannot start if it uses a permission which is not granted.
237 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
238 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700239
Svetoslav7008b512015-06-24 18:47:07 -0700240 static {
241 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
242 Manifest.permission.CAMERA);
243 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
244 Manifest.permission.CAMERA);
245 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
246 Manifest.permission.CALL_PHONE);
247 }
248
Svet Ganov99b60432015-06-27 13:15:22 -0700249 /** Action restriction: launching the activity is not restricted. */
250 private static final int ACTIVITY_RESTRICTION_NONE = 0;
251 /** Action restriction: launching the activity is restricted by a permission. */
252 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
253 /** Action restriction: launching the activity is restricted by an app op. */
254 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700255
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700256 // The height/width divide used when fitting a task within a bounds with method
257 // {@link #fitWithinBounds}.
258 // We always want the task to to be visible in the bounds without affecting its size when
259 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
260 // the input bounds right or bottom side minus the width or height divided by this value.
261 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
262
justinzhang5286d3f2014-05-12 17:06:01 -0400263 /** Status Bar Service **/
264 private IBinder mToken = new Binder();
265 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400266 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400267
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700268 // For debugging to make sure the caller when acquiring/releasing our
269 // wake lock is the system process.
270 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800271 /** The number of distinct task ids that can be assigned to the tasks of a single user */
272 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700273
Craig Mautner27084302013-03-25 08:05:25 -0700274 final ActivityManagerService mService;
275
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800276 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800277
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700278 final ActivityStackSupervisorHandler mHandler;
279
280 /** Short cut */
281 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800282 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700284 /** Counter for next free stack ID to use for dynamic activity stacks. */
285 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700286
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800287 /**
288 * Maps the task identifier that activities are currently being started in to the userId of the
289 * task. Each time a new task is created, the entry for the userId of the task is incremented
290 */
291 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Craig Mautner2420ead2013-04-01 17:13:20 -0700293 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800294 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700295
Craig Mautnere0a38842013-12-16 16:14:02 -0800296 /** The stack containing the launcher app. Assumed to always be attached to
297 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800298 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700299
Craig Mautnere0a38842013-12-16 16:14:02 -0800300 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800301 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Craig Mautner4a1cb222013-12-04 16:14:06 -0800303 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
304 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800305 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700307
308 /** List of activities that are waiting for a new activity to become visible before completing
309 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700312 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800313 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700314
315 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318 /** List of activities that are ready to be stopped, but waiting for the next activity to
319 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
Craig Mautnerf3333272013-04-22 10:55:53 -0700322 /** List of activities that are ready to be finished, but waiting for the previous activity to
323 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700325
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700328
Wale Ogunwale22e25262016-02-01 10:32:02 -0800329 /** List of activities whose multi-window mode changed that we need to report to the
330 * application */
331 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
332
333 /** List of activities whose picture-in-picture mode changed that we need to report to the
334 * application */
335 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
336
Craig Mautnerf3333272013-04-22 10:55:53 -0700337 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700338 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700339
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700340 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700341 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700342
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
344 * is being brought in front of us. */
345 boolean mUserLeaving = false;
346
Craig Mautner0eea92c2013-05-16 13:35:39 -0700347 /** Set when we have taken too long waiting to go to sleep. */
348 boolean mSleepTimeout = false;
349
350 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700351 * We don't want to allow the device to go to sleep while in the process
352 * of launching an activity. This is primarily to allow alarm intent
353 * receivers to launch an activity and get that to run before the device
354 * goes back to sleep.
355 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700356 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700357
358 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700359 * Set when the system is going to sleep, until we have
360 * successfully paused the current activity and released our wake lock.
361 * At that point the system is allowed to actually sleep.
362 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700363 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700365 /** Stack id of the front stack when user switched, indexed by userId. */
366 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700367
Craig Mautner4504de52013-12-20 09:06:56 -0800368 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700370 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371
372 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700373 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374
Jeff Brownca9bc702014-02-11 14:32:56 -0800375 InputManagerInternal mInputManagerInternal;
376
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
378 * may be finished until there is only one entry left. If this is empty the system is not
379 * in lockTask mode. */
380 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000381 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700382 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
383 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000384 */
385 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400386 /**
387 * Notifies the user when entering/exiting lock-task.
388 */
389 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800390
Wale Ogunwaled046a012015-12-24 13:05:59 -0800391 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800392 boolean inResumeTopActivity;
393
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700394 // 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 -0700395 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700396 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700397
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700398 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
399 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800400 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700401
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700402 // The default minimal size that will be used if the activity doesn't specify its minimal size.
403 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800404 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700405
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700406 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
407 private boolean mTaskLayersChanged = true;
408
Jorim Jaggi275561a2016-02-23 10:11:02 -0500409 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800410
Jorim Jaggidc249c42015-12-15 14:57:31 -0800411 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
412
Wale Ogunwale39381972015-12-17 17:15:29 -0800413 static class FindTaskResult {
414 ActivityRecord r;
415 boolean matchedByRootAffinity;
416 }
417 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
418
Craig Mautneree36c772014-07-16 14:56:05 -0700419 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100420 * Used to keep track whether app visibilities got changed since the last pause. Useful to
421 * determine whether to invoke the task stack change listener after pausing.
422 */
423 boolean mAppVisibilitiesChangedSinceLastPause;
424
425 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700426 * Description of a request to start a new activity, which has been held
427 * due to app switches being disabled.
428 */
429 static class PendingActivityLaunch {
430 final ActivityRecord r;
431 final ActivityRecord sourceRecord;
432 final int startFlags;
433 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700434 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700435
436 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700437 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700438 r = _r;
439 sourceRecord = _sourceRecord;
440 startFlags = _startFlags;
441 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700442 callerApp = _callerApp;
443 }
444
445 void sendErrorResult(String message) {
446 try {
447 if (callerApp.thread != null) {
448 callerApp.thread.scheduleCrash(message);
449 }
450 } catch (RemoteException e) {
451 Slog.e(TAG, "Exception scheduling crash of failed "
452 + "activity launcher sourceRecord=" + sourceRecord, e);
453 }
Craig Mautneree36c772014-07-16 14:56:05 -0700454 }
455 }
456
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800457 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700458 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700459 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800460 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800461 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700462 }
463
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800464 void setRecentTasks(RecentTasks recentTasks) {
465 mRecentTasks = recentTasks;
466 }
467
Jeff Brown2c43c332014-06-12 22:38:59 -0700468 /**
469 * At the time when the constructor runs, the power manager has not yet been
470 * initialized. So we initialize our wakelocks afterwards.
471 */
472 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700474 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700475 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700476 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700477 }
478
justinzhang5286d3f2014-05-12 17:06:01 -0400479 // This function returns a IStatusBarService. The value is from ServiceManager.
480 // getService and is cached.
481 private IStatusBarService getStatusBarService() {
482 synchronized (mService) {
483 if (mStatusBarService == null) {
484 mStatusBarService = IStatusBarService.Stub.asInterface(
485 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
486 if (mStatusBarService == null) {
487 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
488 }
489 }
490 return mStatusBarService;
491 }
492 }
493
Jason Monk35c62a42014-06-17 10:24:47 -0400494 private IDevicePolicyManager getDevicePolicyManager() {
495 synchronized (mService) {
496 if (mDevicePolicyManager == null) {
497 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
498 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
499 if (mDevicePolicyManager == null) {
500 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
501 }
502 }
503 return mDevicePolicyManager;
504 }
505 }
506
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700507 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 synchronized (mService) {
509 mWindowManager = wm;
510
511 mDisplayManager =
512 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
513 mDisplayManager.registerDisplayListener(this, null);
514
515 Display[] displays = mDisplayManager.getDisplays();
516 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
517 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700519 if (activityDisplay.mDisplay == null) {
520 throw new IllegalStateException("Default Display does not exist");
521 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800522 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700523 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 }
525
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800526 mHomeStack = mFocusedStack = mLastFocusedStack =
527 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800528
529 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 }
Craig Mautner27084302013-03-25 08:05:25 -0700531 }
532
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200533 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700534 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200535 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700536 }
537
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700538 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700540 }
541
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700544 }
545
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700547 if (stack == null) {
548 return false;
549 }
550
Craig Mautnerdf88d732014-01-27 09:21:32 -0800551 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
552 if (parent != null) {
553 stack = parent.task.stack;
554 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800555 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700556 }
557
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700558 /** The top most stack. */
559 boolean isFrontStack(ActivityStack stack) {
560 if (stack == null) {
561 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700563
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700564 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
565 if (parent != null) {
566 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800567 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
569 }
570
Wale Ogunwaled046a012015-12-24 13:05:59 -0800571 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800572 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
573 if (!focusCandidate.isFocusable()) {
574 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
575 focusCandidate = focusCandidate.getNextFocusableStackLocked();
576 }
577
578 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800579 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800580 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800581
Wale Ogunwaled046a012015-12-24 13:05:59 -0800582 EventLogTags.writeAmFocusedStack(
583 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
584 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
585 }
586
587 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800588 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800589 // The focus activity should always be the top activity in the focused stack.
590 // There will be chaos and anarchy if it isn't...
591 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
592 }
Craig Mautnerde313752015-01-22 14:28:03 -0800593
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800594 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800595 if (r != null && r.idle) {
596 checkFinishBootingLocked();
597 }
598 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700599 }
600
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700601 void moveHomeStackToFront(String reason) {
602 mHomeStack.moveToFront(reason);
603 }
604
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700605 /** Returns true if the focus activity was adjusted to the home stack top activity. */
606 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700607 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
608 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700609 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700610 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700611
Craig Mautner84984fa2014-06-19 11:19:20 -0700612 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700613
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700614 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700615 if (top == null) {
616 return false;
617 }
618 mService.setFocusedActivityLocked(top, reason);
619 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700620 }
621
Craig Mautner299f9602015-01-26 09:47:33 -0800622 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700623 if (!mService.mBooting && !mService.mBooted) {
624 // Not ready yet!
625 return false;
626 }
627
Craig Mautner84984fa2014-06-19 11:19:20 -0700628 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
629 mWindowManager.showRecentApps();
630 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700631 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700632
Craig Mautner84984fa2014-06-19 11:19:20 -0700633 if (prev != null) {
634 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
635 }
636
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700637 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
638 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800639 final String myReason = reason + " resumeHomeStackTask";
640
Mark Lua56ea122015-10-08 13:31:01 +0800641 // Only resume home activity if isn't finishing.
642 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800643 mService.setFocusedActivityLocked(r, myReason);
644 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700645 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700647 }
648
Craig Mautner8d341ef2013-03-26 09:03:27 -0700649 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700650 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700651 }
652
653 /**
654 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
655 * @param id Id of the task we would like returned.
656 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
657 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700658 * @param stackId The stack to restore the task to (default launch stack will be used if
659 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700660 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700661 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800662 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 ActivityStack stack = stacks.get(stackNdx);
667 TaskRecord task = stack.taskForIdLocked(id);
668 if (task != null) {
669 return task;
670 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700671 }
672 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800673
674 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700675 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800676 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800677 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700678 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800679 return null;
680 }
681
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700682 if (!restoreFromRecents) {
683 return task;
684 }
685
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700686 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700687 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
688 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800689 return null;
690 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800692 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700693 }
694
Craig Mautner6170f732013-04-02 13:05:23 -0700695 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800698 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
700 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
701 if (r != null) {
702 return r;
703 }
Craig Mautner6170f732013-04-02 13:05:23 -0700704 }
705 }
706 return null;
707 }
708
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000709 boolean isFocusedUserLockedProfile() {
710 final int userId = mFocusedStack.topRunningActivityLocked().userId;
711 return userId != UserHandle.myUserId()
712 && mService.mUserController.shouldConfirmCredentials(userId);
713 }
714
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800715 void setNextTaskIdForUserLocked(int taskId, int userId) {
716 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
717 if (taskId > currentTaskId) {
718 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700719 }
720 }
721
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800722 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800723 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
724 // for a userId u, a taskId can only be in the range
725 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
726 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
727 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800728 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
729 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
730 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800731 candidateTaskId++;
732 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
733 // Wrap around as there will be smaller task ids that are available now.
734 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700735 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800736 if (candidateTaskId == currentTaskId) {
737 // Something wrong!
738 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
739 throw new IllegalStateException("Cannot get an available task id."
740 + " Reached limit of " + MAX_TASK_IDS_PER_USER
741 + " running tasks per user.");
742 }
743 }
744 mCurTaskIdForUser.put(userId, candidateTaskId);
745 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700746 }
747
Craig Mautnerde4ef022013-04-07 19:01:33 -0700748 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800749 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 if (stack == null) {
751 return null;
752 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700753 ActivityRecord resumedActivity = stack.mResumedActivity;
754 if (resumedActivity == null || resumedActivity.app == null) {
755 resumedActivity = stack.mPausingActivity;
756 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700757 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 }
759 }
760 return resumedActivity;
761 }
762
Dianne Hackbornff072722014-09-24 10:56:28 -0700763 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700764 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
767 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
769 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700770 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 continue;
772 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700773 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 if (hr != null) {
775 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
776 && processName.equals(hr.processName)) {
777 try {
George Mount2c92c972014-03-20 09:38:23 -0700778 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 didSomething = true;
780 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700781 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 Slog.w(TAG, "Exception in new application when starting activity "
783 + hr.intent.getComponent().flattenToShortString(), e);
784 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700785 }
Craig Mautner20e72272013-04-01 13:45:53 -0700786 }
Craig Mautner20e72272013-04-01 13:45:53 -0700787 }
788 }
789 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700790 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700791 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700792 }
Craig Mautner20e72272013-04-01 13:45:53 -0700793 return didSomething;
794 }
795
796 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
798 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
800 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700801 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 continue;
803 }
804 final ActivityRecord resumedActivity = stack.mResumedActivity;
805 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700806 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800807 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 return false;
809 }
Craig Mautner20e72272013-04-01 13:45:53 -0700810 }
811 }
812 return true;
813 }
814
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
817 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
819 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700820 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700822 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 return false;
824 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700825 }
826 }
827 }
828 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700829 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
831 mLastFocusedStack + " to=" + mFocusedStack);
832 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700833 return true;
834 }
835
836 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800837 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800838 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
839 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
841 final ActivityStack stack = stacks.get(stackNdx);
842 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800843 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700844 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800845 return false;
846 }
847 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700849 }
850 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800851 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700852 }
853
Craig Mautner2acc3892013-09-23 10:28:14 -0700854 /**
855 * Pause all activities in either all of the stacks or just the back stacks.
856 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700857 * @return true if any activity was paused as a result of this call.
858 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700859 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700860 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800861 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
862 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800863 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
864 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700865 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700866 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
869 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700871 }
872 }
873 return someActivityPaused;
874 }
875
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700877 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
881 final ActivityStack stack = stacks.get(stackNdx);
882 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700883 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700885 Slog.d(TAG_STATES,
886 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 pausing = false;
888 } else {
889 return false;
890 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700891 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700892 }
893 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700894 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 }
896
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700897 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
898 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500899 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
901 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
903 final ActivityStack stack = stacks.get(stackNdx);
904 if (stack.mResumedActivity != null &&
905 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800907 }
908 }
909 }
910 }
911
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800912 void cancelInitializingActivities() {
913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
914 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
915 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
916 stacks.get(stackNdx).cancelInitializingActivities();
917 }
918 }
919 }
920
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700921 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700922 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700923 }
924
925 void sendWaitingVisibleReportLocked(ActivityRecord r) {
926 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700927 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700928 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700929 if (w.who == null) {
930 changed = true;
931 w.timeout = false;
932 if (r != null) {
933 w.who = new ComponentName(r.info.packageName, r.info.name);
934 }
935 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
936 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700938 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700939 if (changed) {
940 mService.notifyAll();
941 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700942 }
943
944 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
945 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700946 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700948 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700949 if (w.who == null) {
950 changed = true;
951 w.timeout = timeout;
952 if (r != null) {
953 w.who = new ComponentName(r.info.packageName, r.info.name);
954 }
955 w.thisTime = thisTime;
956 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700957 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700958 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700959 if (changed) {
960 mService.notifyAll();
961 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700962 }
963
Craig Mautner29219d92013-04-16 20:19:12 -0700964 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800965 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700966 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700967 if (r != null) {
968 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700969 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700970
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800972 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
974 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800975 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700976 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700977 if (r != null) {
978 return r;
979 }
980 }
981 }
982 return null;
983 }
984
Dianne Hackborn09233282014-04-30 11:33:59 -0700985 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700986 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800987 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
988 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700994 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700996 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700997 }
998 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700999
1000 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1001 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1002 while (maxNum > 0) {
1003 long mostRecentActiveTime = Long.MIN_VALUE;
1004 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001005 final int numTaskLists = runningTaskLists.size();
1006 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1007 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001008 if (!stackTaskList.isEmpty()) {
1009 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1010 if (lastActiveTime > mostRecentActiveTime) {
1011 mostRecentActiveTime = lastActiveTime;
1012 selectedStackList = stackTaskList;
1013 }
1014 }
1015 }
1016 if (selectedStackList != null) {
1017 list.add(selectedStackList.remove(0));
1018 --maxNum;
1019 } else {
1020 break;
1021 }
1022 }
Craig Mautner20e72272013-04-01 13:45:53 -07001023 }
1024
Todd Kennedy7440f172015-12-09 14:31:22 -08001025 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1026 ProfilerInfo profilerInfo) {
1027 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 if (aInfo != null) {
1029 // Store the found target back into the intent, because now that
1030 // we have it we never want to do this again. For example, if the
1031 // user navigates back to this point in the history, we should
1032 // always restart the exact same activity.
1033 intent.setComponent(new ComponentName(
1034 aInfo.applicationInfo.packageName, aInfo.name));
1035
1036 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001037 if (!aInfo.processName.equals("system")) {
1038 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001039 mService.setDebugApp(aInfo.processName, true, false);
1040 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001042 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1043 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1044 }
1045
Man Caocfa78b22015-06-11 20:14:34 -07001046 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1047 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1048 }
1049
1050 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001051 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 }
1053 }
1054 }
1055 return aInfo;
1056 }
1057
Todd Kennedy7440f172015-12-09 14:31:22 -08001058 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001059 return resolveIntent(intent, resolvedType, userId, 0);
1060 }
1061
1062 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001063 try {
1064 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001065 PackageManager.MATCH_DEFAULT_ONLY | flags
1066 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001067 } catch (RemoteException e) {
1068 }
1069 return null;
1070 }
1071
1072 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1073 ProfilerInfo profilerInfo, int userId) {
1074 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1075 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1076 }
1077
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001078 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1079 boolean andResume, boolean checkConfig) throws RemoteException {
1080
1081 if (!allPausedActivitiesComplete()) {
1082 // While there are activities pausing we skipping starting any new activities until
1083 // pauses are complete. NOTE: that we also do this for activities that are starting in
1084 // the paused state because they will first be resumed then paused on the client side.
1085 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1086 "realStartActivityLocked: Skipping start of r=" + r
1087 + " some activities pausing...");
1088 return false;
1089 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001090
Craig Mautner2568c3a2015-03-26 14:22:34 -07001091 if (andResume) {
1092 r.startFreezingScreenLocked(app, 0);
1093 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001094
Craig Mautner2568c3a2015-03-26 14:22:34 -07001095 // schedule launch ticks to collect information about slow apps.
1096 r.startLaunchTickingLocked();
1097 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001098
1099 // Have the window manager re-evaluate the orientation of
1100 // the screen based on the new activity order. Note that
1101 // as a result of this, it can call back into the activity
1102 // manager with a new orientation. We don't care about that,
1103 // because the activity is not currently running so we are
1104 // just restarting it anyway.
1105 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001106 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001107 mService.mConfiguration,
1108 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001109 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001110 }
1111
1112 r.app = app;
1113 app.waitingToKill = null;
1114 r.launchCount++;
1115 r.lastLaunchTime = SystemClock.uptimeMillis();
1116
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001117 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001118
1119 int idx = app.activities.indexOf(r);
1120 if (idx < 0) {
1121 app.activities.add(r);
1122 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001123 mService.updateLruProcessLocked(app, true, null);
1124 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001125
Craig Mautner15df08a2015-04-01 12:17:18 -07001126 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001127 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1128 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001129 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001130 }
1131
1132 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 try {
1134 if (app.thread == null) {
1135 throw new RemoteException();
1136 }
1137 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001138 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001139 if (andResume) {
1140 results = r.results;
1141 newIntents = r.newIntents;
1142 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001143 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1144 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1145 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 if (andResume) {
1147 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1148 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001149 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001150 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001151 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001152 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001153 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001155 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 r.sleeping = false;
1157 r.forceNewConfig = false;
1158 mService.showAskCompatModeDialogLocked(r);
1159 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001160 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1162 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1163 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001164 final String profileFile = mService.mProfileFile;
1165 if (profileFile != null) {
1166 ParcelFileDescriptor profileFd = mService.mProfileFd;
1167 if (profileFd != null) {
1168 try {
1169 profileFd = profileFd.dup();
1170 } catch (IOException e) {
1171 if (profileFd != null) {
1172 try {
1173 profileFd.close();
1174 } catch (IOException o) {
1175 }
1176 profileFd = null;
1177 }
1178 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001179 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001180
1181 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1182 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 }
1184 }
1185 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001186
Craig Mautner2568c3a2015-03-26 14:22:34 -07001187 if (andResume) {
1188 app.hasShownUi = true;
1189 app.pendingUiClean = true;
1190 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001191 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001193 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001194 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001195 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001196 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001197
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001198 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001199 // This may be a heavy-weight process! Note that the package
1200 // manager will ensure that only activity can run in the main
1201 // process of the .apk, which is the only thing that will be
1202 // considered heavy-weight.
1203 if (app.processName.equals(app.info.packageName)) {
1204 if (mService.mHeavyWeightProcess != null
1205 && mService.mHeavyWeightProcess != app) {
1206 Slog.w(TAG, "Starting new heavy weight process " + app
1207 + " when already running "
1208 + mService.mHeavyWeightProcess);
1209 }
1210 mService.mHeavyWeightProcess = app;
1211 Message msg = mService.mHandler.obtainMessage(
1212 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1213 msg.obj = r;
1214 mService.mHandler.sendMessage(msg);
1215 }
1216 }
1217
1218 } catch (RemoteException e) {
1219 if (r.launchFailed) {
1220 // This is the second time we failed -- finish activity
1221 // and give up.
1222 Slog.e(TAG, "Second failure launching "
1223 + r.intent.getComponent().flattenToShortString()
1224 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001225 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1227 "2nd-crash", false);
1228 return false;
1229 }
1230
1231 // This is the first time we failed -- restart process and
1232 // retry.
1233 app.activities.remove(r);
1234 throw e;
1235 }
1236
1237 r.launchFailed = false;
1238 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001239 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
1241
1242 if (andResume) {
1243 // As part of the process of launching, ActivityThread also performs
1244 // a resume.
1245 stack.minimalResumeActivityLocked(r);
1246 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001247 // This activity is not starting in the resumed state... which should look like we asked
1248 // it to pause+stop (but remain visible), and it has done so and reported back the
1249 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001250 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001251 "Moving to PAUSED: " + r + " (starting in paused state)");
1252 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 }
1254
1255 // Launch the new version setup screen if needed. We do this -after-
1256 // launching the initial activity (that is, home), so that it can have
1257 // a chance to initialize itself while in the background, making the
1258 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001259 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 mService.startSetupActivityLocked();
1261 }
1262
Dianne Hackborn465fa392014-09-14 14:21:18 -07001263 // Update any services we are bound to that might care about whether
1264 // their client may have activities.
1265 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1266
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 return true;
1268 }
1269
Craig Mautnere79d42682013-04-01 19:01:53 -07001270 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001271 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001272 // Is this activity's application already running?
1273 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001274 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001275
1276 r.task.stack.setLaunchTime(r);
1277
1278 if (app != null && app.thread != null) {
1279 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001280 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1281 || !"android".equals(r.info.packageName)) {
1282 // Don't add this if it is a platform component that is marked
1283 // to run in multiple processes, because this is actually
1284 // part of the framework so doesn't make sense to track as a
1285 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001286 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1287 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001288 }
George Mount2c92c972014-03-20 09:38:23 -07001289 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001290 return;
1291 } catch (RemoteException e) {
1292 Slog.w(TAG, "Exception when starting activity "
1293 + r.intent.getComponent().flattenToShortString(), e);
1294 }
1295
1296 // If a dead object exception was thrown -- fall through to
1297 // restart the application.
1298 }
1299
1300 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001301 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001302 }
1303
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001304 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001305 String resultWho, int requestCode, int callingPid, int callingUid,
1306 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1307 ActivityRecord resultRecord, ActivityStack resultStack) {
1308 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1309 callingUid);
1310 if (startAnyPerm == PERMISSION_GRANTED) {
1311 return true;
1312 }
1313 final int componentRestriction = getComponentRestrictionForCallingPackage(
1314 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1315 final int actionRestriction = getActionRestrictionForCallingPackage(
1316 intent.getAction(), callingPackage, callingPid, callingUid);
1317 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1318 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1319 if (resultRecord != null) {
1320 resultStack.sendActivityResultLocked(-1,
1321 resultRecord, resultWho, requestCode,
1322 Activity.RESULT_CANCELED, null);
1323 }
1324 final String msg;
1325 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1326 msg = "Permission Denial: starting " + intent.toString()
1327 + " from " + callerApp + " (pid=" + callingPid
1328 + ", uid=" + callingUid + ")" + " with revoked permission "
1329 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1330 } else if (!aInfo.exported) {
1331 msg = "Permission Denial: starting " + intent.toString()
1332 + " from " + callerApp + " (pid=" + callingPid
1333 + ", uid=" + callingUid + ")"
1334 + " not exported from uid " + aInfo.applicationInfo.uid;
1335 } else {
1336 msg = "Permission Denial: starting " + intent.toString()
1337 + " from " + callerApp + " (pid=" + callingPid
1338 + ", uid=" + callingUid + ")"
1339 + " requires " + aInfo.permission;
1340 }
1341 Slog.w(TAG, msg);
1342 throw new SecurityException(msg);
1343 }
1344
1345 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1346 final String message = "Appop Denial: starting " + intent.toString()
1347 + " from " + callerApp + " (pid=" + callingPid
1348 + ", uid=" + callingUid + ")"
1349 + " requires " + AppOpsManager.permissionToOp(
1350 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1351 Slog.w(TAG, message);
1352 return false;
1353 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1354 final String message = "Appop Denial: starting " + intent.toString()
1355 + " from " + callerApp + " (pid=" + callingPid
1356 + ", uid=" + callingUid + ")"
1357 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1358 Slog.w(TAG, message);
1359 return false;
1360 }
1361 return true;
1362 }
1363
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001364 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001365 final long identity = Binder.clearCallingIdentity();
1366 try {
1367 return UserManager.get(mService.mContext).getUserInfo(userId);
1368 } finally {
1369 Binder.restoreCallingIdentity(identity);
1370 }
1371 }
1372
Svet Ganov99b60432015-06-27 13:15:22 -07001373 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001374 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001375 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1376 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001377 == PackageManager.PERMISSION_DENIED) {
1378 return ACTIVITY_RESTRICTION_PERMISSION;
1379 }
1380
Christopher Tateff7add02015-08-17 10:23:22 -07001381 if (activityInfo.permission == null) {
1382 return ACTIVITY_RESTRICTION_NONE;
1383 }
1384
Svet Ganov99b60432015-06-27 13:15:22 -07001385 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1386 if (opCode == AppOpsManager.OP_NONE) {
1387 return ACTIVITY_RESTRICTION_NONE;
1388 }
1389
1390 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1391 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001392 if (!ignoreTargetSecurity) {
1393 return ACTIVITY_RESTRICTION_APPOP;
1394 }
Svet Ganov99b60432015-06-27 13:15:22 -07001395 }
1396
1397 return ACTIVITY_RESTRICTION_NONE;
1398 }
1399
Svetoslav7008b512015-06-24 18:47:07 -07001400 private int getActionRestrictionForCallingPackage(String action,
1401 String callingPackage, int callingPid, int callingUid) {
1402 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001403 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001404 }
1405
1406 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1407 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001408 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001409 }
1410
1411 final PackageInfo packageInfo;
1412 try {
1413 packageInfo = mService.mContext.getPackageManager()
1414 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1415 } catch (PackageManager.NameNotFoundException e) {
1416 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001417 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001418 }
1419
1420 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001421 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001422 }
1423
1424 if (mService.checkPermission(permission, callingPid, callingUid) ==
1425 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001426 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001427 }
1428
1429 final int opCode = AppOpsManager.permissionToOpCode(permission);
1430 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001431 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001432 }
1433
1434 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1435 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001436 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001437 }
1438
Svet Ganov99b60432015-06-27 13:15:22 -07001439 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001440 }
1441
Wale Ogunwaled046a012015-12-24 13:05:59 -08001442 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001443 if (r == null) {
1444 // Not sure what you are trying to do, but it is not going to work...
1445 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001446 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001447 final TaskRecord task = r.task;
1448 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001449 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001450 return false;
1451 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001452 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001453 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001454 }
1455
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001456 void setLaunchSource(int uid) {
1457 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1458 }
1459
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001460 void acquireLaunchWakelock() {
1461 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1462 throw new IllegalStateException("Calling must be system uid");
1463 }
1464 mLaunchingActivity.acquire();
1465 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1466 // To be safe, don't allow the wake lock to be held for too long.
1467 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1468 }
1469 }
1470
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001471 /**
1472 * Called when the frontmost task is idle.
1473 * @return the state of mService.mBooting before this was called.
1474 */
1475 private boolean checkFinishBootingLocked() {
1476 final boolean booting = mService.mBooting;
1477 boolean enableScreen = false;
1478 mService.mBooting = false;
1479 if (!mService.mBooted) {
1480 mService.mBooted = true;
1481 enableScreen = true;
1482 }
1483 if (booting || enableScreen) {
1484 mService.postFinishBooting(booting, enableScreen);
1485 }
1486 return booting;
1487 }
1488
Craig Mautnerf3333272013-04-22 10:55:53 -07001489 // Checked.
1490 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1491 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001492 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001493
Craig Mautnerf3333272013-04-22 10:55:53 -07001494 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001495 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001496 int NS = 0;
1497 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001498 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001499 boolean activityRemoved = false;
1500
Wale Ogunwale7d701172015-03-11 15:36:30 -07001501 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001502 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001503 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1504 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001505 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1506 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001507 if (fromTimeout) {
1508 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001509 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001510
1511 // This is a hack to semi-deal with a race condition
1512 // in the client where it can be constructed with a
1513 // newer configuration from when we asked it to launch.
1514 // We'll update with whatever configuration it now says
1515 // it used to launch.
1516 if (config != null) {
1517 r.configuration = config;
1518 }
1519
1520 // We are now idle. If someone is waiting for a thumbnail from
1521 // us, we can now deliver.
1522 r.idle = true;
1523
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001524 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001525 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001526 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001527 }
1528 }
1529
1530 if (allResumedActivitiesIdle()) {
1531 if (r != null) {
1532 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001533 }
1534
1535 if (mLaunchingActivity.isHeld()) {
1536 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1537 if (VALIDATE_WAKE_LOCK_CALLER &&
1538 Binder.getCallingUid() != Process.myUid()) {
1539 throw new IllegalStateException("Calling must be system uid");
1540 }
1541 mLaunchingActivity.release();
1542 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001543 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001544 }
1545
1546 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001547 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001548 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001549 if ((NF = mFinishingActivities.size()) > 0) {
1550 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001551 mFinishingActivities.clear();
1552 }
1553
Craig Mautnerf3333272013-04-22 10:55:53 -07001554 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001555 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001556 mStartingUsers.clear();
1557 }
1558
Craig Mautnerf3333272013-04-22 10:55:53 -07001559 // Stop any activities that are scheduled to do so but have been
1560 // waiting for the next one to start.
1561 for (int i = 0; i < NS; i++) {
1562 r = stops.get(i);
1563 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001564 if (stack != null) {
1565 if (r.finishing) {
1566 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1567 } else {
1568 stack.stopActivityLocked(r);
1569 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 }
1571 }
1572
1573 // Finish any activities that are scheduled to do so but have been
1574 // waiting for the next one to start.
1575 for (int i = 0; i < NF; i++) {
1576 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001577 final ActivityStack stack = r.task.stack;
1578 if (stack != null) {
1579 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1580 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 }
1582
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001583 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001584 // Complete user switch
1585 if (startingUsers != null) {
1586 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001587 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001588 }
1589 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001590 }
1591
1592 mService.trimApplications();
1593 //dump();
1594 //mWindowManager.dump();
1595
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001597 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001598 }
1599
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001600 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001601 }
1602
Craig Mautner8e569572013-10-11 17:36:59 -07001603 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001604 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1608 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1609 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001610 }
Craig Mautner19091252013-10-05 00:03:53 -07001611 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001612 }
1613
1614 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001615 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1616 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001617 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1618 stacks.get(stackNdx).closeSystemDialogsLocked();
1619 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001620 }
1621 }
1622
Craig Mautner93529a42013-10-04 15:03:13 -07001623 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001624 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001625 }
1626
Craig Mautner8d341ef2013-03-26 09:03:27 -07001627 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001628 * Update the last used stack id for non-current user (current user's last
1629 * used stack is the focused stack)
1630 */
1631 void updateUserStackLocked(int userId, ActivityStack stack) {
1632 if (userId != mCurrentUser) {
1633 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1634 }
1635 }
1636
1637 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001638 * @return true if some activity was finished (or would have finished if doit were true).
1639 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001640 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1641 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001642 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001646 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001647 if (stack.finishDisabledPackageActivitiesLocked(
1648 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001649 didSomething = true;
1650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001651 }
1652 }
1653 return didSomething;
1654 }
1655
Dianne Hackborna413dc02013-07-12 12:02:55 -07001656 void updatePreviousProcessLocked(ActivityRecord r) {
1657 // Now that this process has stopped, we may want to consider
1658 // it to be the previous app to try to keep around in case
1659 // the user wants to return to it.
1660
1661 // First, found out what is currently the foreground app, so that
1662 // we don't blow away the previous app if this activity is being
1663 // hosted by the process that is actually still the foreground.
1664 ProcessRecord fgApp = null;
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;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001667 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1668 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001669 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001670 if (stack.mResumedActivity != null) {
1671 fgApp = stack.mResumedActivity.app;
1672 } else if (stack.mPausingActivity != null) {
1673 fgApp = stack.mPausingActivity.app;
1674 }
1675 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001676 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001677 }
1678 }
1679
1680 // Now set this one as the previous process, only if that really
1681 // makes sense to.
1682 if (r.app != null && fgApp != null && r.app != fgApp
1683 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001684 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001685 mService.mPreviousProcess = r.app;
1686 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1687 }
1688 }
1689
Wale Ogunwaled046a012015-12-24 13:05:59 -08001690 boolean resumeFocusedStackTopActivityLocked() {
1691 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001692 }
1693
Wale Ogunwaled046a012015-12-24 13:05:59 -08001694 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001695 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001696 if (targetStack != null && isFocusedStack(targetStack)) {
1697 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001698 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001699 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1700 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001701 }
1702
Todd Kennedy39bfee52016-02-24 10:28:21 -08001703 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1705 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1706 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1707 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1708 }
1709 }
1710 }
1711
Adrian Roos20d7df32016-01-12 18:59:43 +01001712 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1713 TaskRecord finishedTask = null;
1714 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001715 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1716 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001717 final int numStacks = stacks.size();
1718 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1719 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001720 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1721 if (stack == focusedStack || finishedTask == null) {
1722 finishedTask = t;
1723 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001724 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001725 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001726 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001727 }
1728
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001729 void finishVoiceTask(IVoiceInteractionSession session) {
1730 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1731 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1732 final int numStacks = stacks.size();
1733 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1734 final ActivityStack stack = stacks.get(stackNdx);
1735 stack.finishVoiceTask(session);
1736 }
1737 }
1738 }
1739
Chong Zhang280d3322015-11-03 17:27:26 -08001740 void findTaskToMoveToFrontLocked(
1741 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001742 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1743 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001744 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001745 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1746 // Caller wants the home activity moved with it. To accomplish this,
1747 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001748 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001749 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001750 if (task.stack == null) {
1751 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1752 + task + " to front. Stack is null");
1753 return;
1754 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001755
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001756 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001757 int stackId = options.getLaunchStackId();
1758 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001759 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001760 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001761 if (stackId == INVALID_STACK_ID) {
1762 stackId = task.getLaunchStackId();
1763 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001764 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001765 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1766 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1767 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001768 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1769 // still need moveTaskToFrontLocked() below for any transition settings.
1770 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001771 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1772 resizeStackLocked(stackId, bounds,
1773 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1774 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1775 } else {
1776 // WM resizeTask must be done after the task is moved to the correct stack,
1777 // because Task's setBounds() also updates dim layer's bounds, but that has
1778 // dependency on the stack.
1779 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1780 false /* relayout */, false /* forced */);
1781 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001782 }
1783 }
1784
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001785 final ActivityRecord r = task.getTopActivity();
1786 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1787 r == null ? null : r.appTimeTracker, reason);
1788
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001789 if (DEBUG_STACK) Slog.d(TAG_STACK,
1790 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001791 }
1792
Wale Ogunwale854809c2015-12-27 16:18:19 -08001793 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001794 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001795 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001796 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001797 return false;
1798 }
1799 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1800 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001801 }
1802
Craig Mautner967212c2013-04-13 21:10:58 -07001803 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001804 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001805 }
1806
1807 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001808 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1809 if (activityContainer != null) {
1810 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001811 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001812 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001813 return null;
1814 }
1815 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001816 }
1817
Craig Mautner967212c2013-04-13 21:10:58 -07001818 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001819 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1821 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001822 }
1823 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001824 }
1825
Craig Mautner4a1cb222013-12-04 16:14:06 -08001826 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001827 ActivityRecord homeActivity = getHomeActivity();
1828 if (homeActivity != null) {
1829 return homeActivity.appToken;
1830 }
1831 return null;
1832 }
1833
1834 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001835 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001836 }
1837
1838 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001839 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1840 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1841 final TaskRecord task = tasks.get(taskNdx);
1842 if (task.isHomeTask()) {
1843 final ArrayList<ActivityRecord> activities = task.mActivities;
1844 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1845 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001846 if (r.isHomeActivity()
1847 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001848 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001849 }
1850 }
1851 }
1852 }
1853 return null;
1854 }
1855
Chong Zhangb15758a2015-11-17 12:12:03 -08001856 /**
1857 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1858 * the docked stack itself, or if it's side-by-side to the docked stack.
1859 */
1860 boolean isStackDockedInEffect(int stackId) {
1861 return stackId == DOCKED_STACK_ID ||
1862 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1863 }
1864
Todd Kennedyca4d8422015-01-15 15:19:22 -08001865 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001866 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001867 ActivityContainer activityContainer =
1868 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001869 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001870 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1871 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001872 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001873 return activityContainer;
1874 }
1875
Craig Mautner34b73df2014-01-12 21:11:08 -08001876 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001877 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1878 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1879 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001880 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1881 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001882 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001883 }
1884 }
1885
Craig Mautner95da1082014-02-24 17:54:35 -08001886 void deleteActivityContainer(IActivityContainer container) {
1887 ActivityContainer activityContainer = (ActivityContainer)container;
1888 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001889 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1890 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001891 final int stackId = activityContainer.mStackId;
1892 mActivityContainers.remove(stackId);
1893 mWindowManager.removeStack(stackId);
1894 }
1895 }
1896
Jorim Jaggidc249c42015-12-15 14:57:31 -08001897 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1898 boolean preserveWindows, boolean allowResizeInDockedMode) {
1899 if (stackId == DOCKED_STACK_ID) {
1900 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1901 preserveWindows);
1902 return;
1903 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001904 final ActivityStack stack = getStack(stackId);
1905 if (stack == null) {
1906 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1907 return;
1908 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001909
Jorim Jaggidc249c42015-12-15 14:57:31 -08001910 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001911 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1912 // stack size changing so things don't get out of sync.
1913 return;
1914 }
1915
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001916 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001917 mWindowManager.deferSurfaceLayout();
1918 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001919 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1920 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001921 } finally {
1922 mWindowManager.continueSurfaceLayout();
1923 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001924 }
1925 }
1926
Jorim Jaggidc249c42015-12-15 14:57:31 -08001927 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1928 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001929 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001930
1931 mTmpBounds.clear();
1932 mTmpConfigs.clear();
1933 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001934 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001935 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001936 final TaskRecord task = tasks.get(i);
1937 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001938 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1939 // For freeform stack we don't adjust the size of the tasks to match that
1940 // of the stack, but we do try to make sure the tasks are still contained
1941 // with the bounds of the stack.
1942 tempRect2.set(task.mBounds);
1943 fitWithinBounds(tempRect2, bounds);
1944 task.updateOverrideConfiguration(tempRect2);
1945 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001946 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001947 tempTaskBounds != null ? tempTaskBounds : bounds,
1948 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001949 }
1950 }
1951
1952 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1953 mTmpBounds.put(task.taskId, task.mBounds);
1954 if (tempTaskInsetBounds != null) {
1955 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1956 }
1957 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001958
1959 // We might trigger a configuration change. Save the current task bounds for freezing.
1960 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001961 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1962 mTmpBounds, mTmpInsetBounds);
1963 stack.setBounds(bounds);
1964 }
1965
1966 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1967 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08001968 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001969 return;
1970 }
1971 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1972 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001973 if (!updated) {
1974 resumeFocusedStackTopActivityLocked();
1975 }
1976 }
1977
1978 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1979 Rect tempDockedTaskInsetBounds,
1980 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1981 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1982 if (stack == null) {
1983 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1984 return;
1985 }
1986
1987 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1988 mWindowManager.deferSurfaceLayout();
1989 try {
1990 ActivityRecord r = stack.topRunningActivityLocked();
1991 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1992 tempDockedTaskInsetBounds);
1993
1994 if (stack.mFullscreen) {
1995 // The dock stack went fullscreen which is kinda like dismissing it.
1996 // In this case we make all other static stacks fullscreen and move all
1997 // docked stack tasks to the fullscreen stack.
1998 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1999 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2000 resizeStackLocked(i, null, null, null, preserveWindows,
2001 true /* allowResizeInDockedMode */);
2002 }
2003 }
2004
2005 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2006 final int count = tasks.size();
2007 for (int i = 0; i < count; i++) {
2008 moveTaskToStackLocked(tasks.get(i).taskId,
2009 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2010 false /* animate */);
2011 }
2012
2013 // stack shouldn't contain anymore activities, so nothing to resume.
2014 r = null;
2015 } else {
2016 // Docked stacks occupy a dedicated region on screen so the size of all other
2017 // static stacks need to be adjusted so they don't overlap with the docked stack.
2018 // We get the bounds to use from window manager which has been adjusted for any
2019 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002020 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002021 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002022 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2023 if (StackId.isResizeableByDockedStack(i)) {
2024 ActivityStack otherStack = getStack(i);
2025 if (otherStack != null) {
2026 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2027 tempOtherTaskInsetBounds, preserveWindows,
2028 true /* allowResizeInDockedMode */);
2029 }
2030 }
2031 }
2032 }
2033 ensureConfigurationAndResume(stack, r, preserveWindows);
2034 } finally {
2035 mWindowManager.continueSurfaceLayout();
2036 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2037 }
2038
2039 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2040 tempDockedTaskBounds != null
2041 || tempDockedTaskInsetBounds != null
2042 || tempOtherTaskBounds != null
2043 || tempOtherTaskInsetBounds != null);
2044 }
2045
Robert Carr0d00c2e2016-02-29 17:45:02 -08002046 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2047 final ActivityStack stack = getStack(PINNED_STACK_ID);
2048 if (stack == null) {
2049 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2050 return;
2051 }
2052 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2053 mWindowManager.deferSurfaceLayout();
2054 try {
2055 ActivityRecord r = stack.topRunningActivityLocked();
2056 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2057 null);
2058 ensureConfigurationAndResume(stack, r, false);
2059 } finally {
2060 mWindowManager.continueSurfaceLayout();
2061 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2062 }
2063 }
2064
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002065 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2066 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002067 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002068 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002069 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002070 }
2071
Chong Zhang87b21722015-09-21 15:39:51 -07002072 // If this is a forced resize, let it go through even if the bounds is not changing,
2073 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002074 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002075 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002076 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002077 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002078 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002079 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002080
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002081 if (!mWindowManager.isValidTaskId(task.taskId)) {
2082 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002083 // All we can do for now is update the bounds so it can be used when the task is
2084 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002085 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002086 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2087 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002088 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002089 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002090 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002091 }
2092
Chong Zhang6de2ae82015-09-30 18:25:21 -07002093 // Do not move the task to another stack here.
2094 // This method assumes that the task is already placed in the right stack.
2095 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002096
Chong Zhang6de2ae82015-09-30 18:25:21 -07002097 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002098
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002099 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002100 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002101 // way and the activity was kept the way it was. If it's false, it means the activity had
2102 // to be relaunched due to configuration change.
2103 boolean kept = true;
2104 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002105 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002106 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002107 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002108 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002109
2110 if (!deferResume) {
2111
2112 // All other activities must be made visible with their correct configuration.
2113 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2114 if (!kept) {
2115 resumeFocusedStackTopActivityLocked();
2116 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002117 }
2118 }
2119 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002120 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002121
2122 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002123 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002124 }
2125
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002126 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002127 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2128 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002129 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002130 }
2131
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002132 ActivityContainer activityContainer = new ActivityContainer(stackId);
2133 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002134 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002135 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002136 }
2137
2138 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002139 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002140 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2141 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002142 break;
2143 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002144 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002145 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002146 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002147 }
2148
Chong Zhang5dcb2752015-08-18 13:50:26 -07002149 /**
2150 * Restores a recent task to a stack
2151 * @param task The recent task to be restored.
2152 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002153 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002154 * @return true if the task has been restored successfully.
2155 */
2156 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2157 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002158 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002159 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2160 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2161 // Put it in the fullscreen stack.
2162 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002163 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002164
Wale Ogunwale706ed792015-08-02 10:29:44 -07002165 if (task.stack != null) {
2166 // Task has already been restored once. See if we need to do anything more
2167 if (task.stack.mStackId == stackId) {
2168 // Nothing else to do since it is already restored in the right stack.
2169 return true;
2170 }
2171 // Remove current stack association, so we can re-associate the task with the
2172 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002173 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002174 }
2175
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002176 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002177 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002178
2179 if (stack == null) {
2180 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002181 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2182 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002183 return false;
2184 }
2185
Wale Ogunwale5f986092015-12-04 15:35:38 -08002186 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002187 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2188 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002189 final ArrayList<ActivityRecord> activities = task.mActivities;
2190 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002191 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002192 }
2193 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002194 }
2195
Wale Ogunwale040b4702015-08-06 18:10:50 -07002196 /**
2197 * Moves the specified task record to the input stack id.
2198 * WARNING: This method performs an unchecked/raw move of the task and
2199 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002200 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002201 * @param task Task to move.
2202 * @param stackId Id of stack to move task to.
2203 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002204 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002205 * @param reason Reason the task is been moved.
2206 * @return The stack the task was moved to.
2207 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002208 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002209 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002210
2211 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2212 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2213 + "support multi-window task=" + task + " to stackId=" + stackId);
2214 }
2215
Chong Zhang02898352015-08-21 17:27:14 -07002216 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002217 final ActivityStack prevStack = task.stack;
2218 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2219 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2220 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2221 // Whenever we are moving the top activity from the front stack we want to make sure to move
2222 // the stack to the front.
2223 final boolean wasFront = isFrontStack(prevStack)
2224 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002225
Chong Zhangd545dce2016-02-29 18:09:17 -08002226 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002227 // We don't allow moving a unresizeable task to the docked stack since the docked
2228 // stack is used for split-screen mode and will cause things like the docked divider to
2229 // show up. We instead leave the task in its current stack or move it to the fullscreen
2230 // stack if it isn't currently in a stack.
2231 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002232 Slog.w(TAG, "Can not move unresizeable task=" + task
2233 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2234 }
2235
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002236 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2237 // if a docked stack is created below which will lead to the stack we are moving from and
2238 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002239 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002240 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002241 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002242 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002243 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002244
2245 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002246 // move focus to the new stack by moving the stack to the front.
2247 stack.moveToFrontAndResumeStateIfNeeded(
2248 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002249
Wale Ogunwale040b4702015-08-06 18:10:50 -07002250 return stack;
2251 }
2252
Chong Zhange4fbd322016-03-01 14:44:03 -08002253 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002254 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002255 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2256 false /* deferResume */);
2257 }
2258
2259 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2260 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002261 final TaskRecord task = anyTaskForIdLocked(taskId);
2262 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002263 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002264 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002265 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002266
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002267 if (task.stack != null && task.stack.mStackId == stackId) {
2268 // You are already in the right stack silly...
2269 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002270 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002271 }
2272
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002273 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2274 throw new IllegalArgumentException("moveTaskToStack:"
2275 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2276 }
2277
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002278 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002279 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002280 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002281 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002282 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002283 // We are about to relaunch the activity because its configuration changed due to
2284 // being maximized, i.e. size change. The activity will first remove the old window
2285 // and then add a new one. This call will tell window manager about this, so it can
2286 // preserve the old window until the new one is drawn. This prevents having a gap
2287 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002288 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002289 // Note here we always set the replacing window first, as the flags might be needed
2290 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002291 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002292 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002293
2294 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002295 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002296 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002297 try {
2298 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002299 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002300 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002301
Wale Ogunwale961f4852016-02-01 20:25:54 -08002302 if (!animate) {
2303 stack.mNoAnimActivities.add(topActivity);
2304 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002305
Wale Ogunwale961f4852016-02-01 20:25:54 -08002306 // We might trigger a configuration change. Save the current task bounds for freezing.
2307 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2308
2309 // Make sure the task has the appropriate bounds/size for the stack it is in.
2310 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002311 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2312 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002313 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2314 Rect bounds = task.getLaunchBounds();
2315 if (bounds == null) {
2316 stack.layoutTaskInStack(task, null);
2317 bounds = task.mBounds;
2318 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002319 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2320 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002321 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002322 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2323 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002324 }
2325 } finally {
2326 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002327 }
2328
2329 if (mightReplaceWindow) {
2330 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2331 // window), we need to clear the replace window settings. Otherwise, we schedule a
2332 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002333 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002334 }
2335
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002336 if (!deferResume) {
2337
2338 // The task might have already been running and its visibility needs to be synchronized with
2339 // the visibility of the stack / windows.
2340 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2341 resumeFocusedStackTopActivityLocked();
2342 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002343
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002344 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002345
2346 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002347 }
2348
Wale Ogunwale079a0042015-10-24 11:44:07 -07002349 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2350 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2351 if (stack == null) {
2352 throw new IllegalArgumentException(
2353 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2354 }
2355
2356 final ActivityRecord r = stack.topRunningActivityLocked();
2357 if (r == null) {
2358 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2359 + " in stack=" + stack);
2360 return false;
2361 }
2362
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002363 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002364 Slog.w(TAG,
2365 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002366 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002367 return false;
2368 }
2369
Wale Ogunwale480dca02016-02-06 13:58:29 -08002370 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002371 return true;
2372 }
2373
Wale Ogunwale480dca02016-02-06 13:58:29 -08002374 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2375 mWindowManager.deferSurfaceLayout();
2376 try {
2377 final TaskRecord task = r.task;
2378
2379 // Need to make sure the pinned stack exist so we can resize it below...
2380 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2381
2382 // Resize the pinned stack to match the current size of the task the activity we are
2383 // going to be moving is currently contained in. We do this to have the right starting
2384 // animation bounds for the pinned stack to the desired bounds the caller wants.
2385 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2386 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2387 true /* allowResizeInDockedMode */);
2388
2389 if (task.mActivities.size() == 1) {
2390 // There is only one activity in the task. So, we can just move the task over to
2391 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002392 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2393 // Move the home stack forward if the task we just moved to the pinned stack
2394 // was launched from home so home should be visible behind it.
2395 moveHomeStackToFront(reason);
2396 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002397 moveTaskToStackLocked(
2398 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2399 } else {
2400 stack.moveActivityToStack(r);
2401 }
2402 } finally {
2403 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002404 }
2405
Wale Ogunwale480dca02016-02-06 13:58:29 -08002406 // The task might have already been running and its visibility needs to be synchronized
2407 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002408 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002409 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002410
Wale Ogunwale480dca02016-02-06 13:58:29 -08002411 mWindowManager.animateResizePinnedStack(bounds);
2412 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002413 }
2414
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002415 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2416 final TaskRecord task = anyTaskForIdLocked(taskId);
2417 if (task == null) {
2418 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2419 return;
2420 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002421 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2422
2423 task.updateOverrideConfigurationForStack(stack);
2424
2425 mWindowManager.positionTaskInStack(
2426 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002427 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002428 // The task might have already been running and its visibility needs to be synchronized with
2429 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002430 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2431 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002432 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002433 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002434 }
2435
Craig Mautnerac6f8432013-07-17 13:24:59 -07002436 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002437 mTmpFindTaskResult.r = null;
2438 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002439 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002440 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002442 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2443 final ActivityStack stack = stacks.get(stackNdx);
2444 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002445 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002446 continue;
2447 }
2448 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002449 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2450 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002451 continue;
2452 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002453 stack.findTaskLocked(r, mTmpFindTaskResult);
2454 // It is possible to have task in multiple stacks with the same root affinity.
2455 // If the match we found was based on root affinity we keep on looking to see if
2456 // there is a better match in another stack. We eventually return the match based
2457 // on root affinity if we don't find a better match.
2458 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2459 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002460 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002461 }
2462 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002463 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2464 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002465 }
2466
2467 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002468 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2471 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2472 if (ar != null) {
2473 return ar;
2474 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002475 }
2476 }
2477 return null;
2478 }
2479
Craig Mautner8d341ef2013-03-26 09:03:27 -07002480 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002481 scheduleSleepTimeout();
2482 if (!mGoingToSleep.isHeld()) {
2483 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002484 if (mLaunchingActivity.isHeld()) {
2485 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2486 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002487 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002488 mLaunchingActivity.release();
2489 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002490 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002491 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002492 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002493 }
2494
2495 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002496 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002497
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002498 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002499 final long endTime = System.currentTimeMillis() + timeout;
2500 while (true) {
2501 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002502 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2503 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2505 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2506 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002507 }
2508 if (cantShutdown) {
2509 long timeRemaining = endTime - System.currentTimeMillis();
2510 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002511 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002512 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002513 } catch (InterruptedException e) {
2514 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002515 } else {
2516 Slog.w(TAG, "Activity manager shutdown timed out");
2517 timedout = true;
2518 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002519 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002520 } else {
2521 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002522 }
2523 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002524
2525 // Force checkReadyForSleep to complete.
2526 mSleepTimeout = true;
2527 checkReadyForSleepLocked();
2528
Craig Mautner8d341ef2013-03-26 09:03:27 -07002529 return timedout;
2530 }
2531
2532 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002533 removeSleepTimeouts();
2534 if (mGoingToSleep.isHeld()) {
2535 mGoingToSleep.release();
2536 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2538 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002539 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2540 final ActivityStack stack = stacks.get(stackNdx);
2541 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002542 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002543 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002544 }
Craig Mautner5314a402013-09-26 12:40:16 -07002545 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002547 mGoingToSleepActivities.clear();
2548 }
2549
2550 void activitySleptLocked(ActivityRecord r) {
2551 mGoingToSleepActivities.remove(r);
2552 checkReadyForSleepLocked();
2553 }
2554
2555 void checkReadyForSleepLocked() {
2556 if (!mService.isSleepingOrShuttingDown()) {
2557 // Do not care.
2558 return;
2559 }
2560
2561 if (!mSleepTimeout) {
2562 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2564 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2566 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2567 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002568 }
2569
2570 if (mStoppingActivities.size() > 0) {
2571 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002572 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002573 + mStoppingActivities.size() + " activities");
2574 scheduleIdleLocked();
2575 dontSleep = true;
2576 }
2577
2578 if (mGoingToSleepActivities.size() > 0) {
2579 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002580 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002581 + mGoingToSleepActivities.size() + " activities");
2582 dontSleep = true;
2583 }
2584
2585 if (dontSleep) {
2586 return;
2587 }
2588 }
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 stacks.get(stackNdx).goToSleep();
2594 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002595 }
2596
2597 removeSleepTimeouts();
2598
2599 if (mGoingToSleep.isHeld()) {
2600 mGoingToSleep.release();
2601 }
2602 if (mService.mShuttingDown) {
2603 mService.notifyAll();
2604 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002605 }
2606
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002607 boolean reportResumedActivityLocked(ActivityRecord r) {
2608 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002609 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002610 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002611 }
2612 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002613 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002614 mWindowManager.executeAppTransition();
2615 return true;
2616 }
2617 return false;
2618 }
2619
Craig Mautner8d341ef2013-03-26 09:03:27 -07002620 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002621 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2622 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002623 int stackNdx = stacks.size() - 1;
2624 while (stackNdx >= 0) {
2625 stacks.get(stackNdx).handleAppCrashLocked(app);
2626 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002627 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002628 }
2629 }
2630
Jose Lima4b6c6692014-08-12 17:41:12 -07002631 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002632 final ActivityStack stack = r.task.stack;
2633 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002634 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2635 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002636 return false;
2637 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002638 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002639 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2640 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002641
2642 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002643 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002644 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002645 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002646 return true;
2647 }
2648
2649 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002650 if (visible && top.fullscreen) {
2651 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002652 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2653 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2654 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2655 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002656 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002657 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2658 // Only the activity set as currently visible behind should actively reset its
2659 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002660 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2661 "requestVisibleBehind: returning visible=" + visible
2662 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2663 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002664 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002665 }
2666
Jose Lima4b6c6692014-08-12 17:41:12 -07002667 stack.setVisibleBehindActivity(visible ? r : null);
2668 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002669 // If there is a translucent home activity, we need to force it stop being translucent,
2670 // because we can't depend on the application to necessarily perform that operation.
2671 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002672 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002673 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002674 mService.convertFromTranslucent(next.appToken);
2675 }
2676 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002677 if (top.app != null && top.app.thread != null) {
2678 // Notify the top app of the change.
2679 try {
2680 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2681 } catch (RemoteException e) {
2682 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002683 }
2684 return true;
2685 }
2686
Craig Mautnerbb742462014-07-07 15:28:55 -07002687 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2688 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2689 r.mLaunchTaskBehind = false;
2690 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002691 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002692 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002693 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002694 mWindowManager.setAppVisibility(r.appToken, false);
2695 }
2696
2697 void scheduleLaunchTaskBehindComplete(IBinder token) {
2698 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2699 }
2700
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002701 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2702 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002703 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2705 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002706 final int topStackNdx = stacks.size() - 1;
2707 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2708 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002709 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002710 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 }
2713
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002714 void invalidateTaskLayers() {
2715 mTaskLayersChanged = true;
2716 }
2717
2718 void rankTaskLayersIfNeeded() {
2719 if (!mTaskLayersChanged) {
2720 return;
2721 }
2722 mTaskLayersChanged = false;
2723 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2725 int baseLayer = 0;
2726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2727 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2728 }
2729 }
2730 }
2731
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002732 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2735 final int topStackNdx = stacks.size() - 1;
2736 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2737 final ActivityStack stack = stacks.get(stackNdx);
2738 stack.clearOtherAppTimeTrackers(except);
2739 }
2740 }
2741 }
2742
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2745 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002746 final int numStacks = stacks.size();
2747 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2748 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002749 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002750 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002751 }
2752 }
2753
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002754 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2755 // Examine all activities currently running in the process.
2756 TaskRecord firstTask = null;
2757 // Tasks is non-null only if two or more tasks are found.
2758 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002759 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2760 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002761 ActivityRecord r = app.activities.get(i);
2762 // First, if we find an activity that is in the process of being destroyed,
2763 // then we just aren't going to do anything for now; we want things to settle
2764 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002765 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002766 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002767 return;
2768 }
2769 // Don't consider any activies that are currently not in a state where they
2770 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002771 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2772 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002773 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002774 continue;
2775 }
2776 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002777 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002778 + " from " + r);
2779 if (firstTask == null) {
2780 firstTask = r.task;
2781 } else if (firstTask != r.task) {
2782 if (tasks == null) {
2783 tasks = new ArraySet<>();
2784 tasks.add(firstTask);
2785 }
2786 tasks.add(r.task);
2787 }
2788 }
2789 }
2790 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002791 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002792 return;
2793 }
2794 // If we have activities in multiple tasks that are in a position to be destroyed,
2795 // let's iterate through the tasks and release the oldest one.
2796 final int numDisplays = mActivityDisplays.size();
2797 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2799 // Step through all stacks starting from behind, to hit the oldest things first.
2800 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2801 final ActivityStack stack = stacks.get(stackNdx);
2802 // Try to release activities in this stack; if we manage to, we are done.
2803 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2804 return;
2805 }
2806 }
2807 }
2808 }
2809
Amith Yamasani37a40c22015-06-17 13:25:42 -07002810 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002811 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002812 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002813 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002814
Craig Mautner858d8a62013-04-23 17:08:34 -07002815 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2817 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002819 final ActivityStack stack = stacks.get(stackNdx);
2820 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002821 TaskRecord task = stack.topTask();
2822 if (task != null) {
2823 mWindowManager.moveTaskToTop(task.taskId);
2824 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002825 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002826 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002827
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002828 ActivityStack stack = getStack(restoreStackId);
2829 if (stack == null) {
2830 stack = mHomeStack;
2831 }
2832 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002833 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002834 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002835 } else {
2836 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002837 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002838 }
Craig Mautner93529a42013-10-04 15:03:13 -07002839 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002840 }
2841
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002842 /** Checks whether the userid is a profile of the current user. */
2843 boolean isCurrentProfileLocked(int userId) {
2844 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002845 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002846 }
2847
Chong Zhang45c25ce2015-08-10 22:18:26 -07002848 /** Checks whether the activity should be shown for current user. */
2849 boolean okToShowLocked(ActivityRecord r) {
2850 return r != null && (isCurrentProfileLocked(r.userId)
2851 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2852 }
2853
Craig Mautnerde4ef022013-04-07 19:01:33 -07002854 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002855 ArrayList<ActivityRecord> stops = null;
2856
2857 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002858 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2859 ActivityRecord s = mStoppingActivities.get(activityNdx);
2860 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002861 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002862 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2863 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002864 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002865 if (s.finishing) {
2866 // If this activity is finishing, it is sitting on top of
2867 // everyone else but we now know it is no longer needed...
2868 // so get rid of it. Otherwise, we need to go through the
2869 // normal flow and hide it once we determine that it is
2870 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002871 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002872 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002873 }
2874 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002875 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002876 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002877 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002878 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002879 }
2880 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002881 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002882 }
2883 }
2884
2885 return stops;
2886 }
2887
Craig Mautnercf910b02013-04-23 11:23:27 -07002888 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002889 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2890 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2891 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2892 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002893 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002894 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002895 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002896 if (r == null) Slog.e(TAG,
2897 "validateTop...: null top activity, stack=" + stack);
2898 else {
2899 final ActivityRecord pausing = stack.mPausingActivity;
2900 if (pausing != null && pausing == r) Slog.e(TAG,
2901 "validateTop...: top stack has pausing activity r=" + r
2902 + " state=" + state);
2903 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2904 "validateTop...: activity in front not resumed r=" + r
2905 + " state=" + state);
2906 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002907 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002908 final ActivityRecord resumed = stack.mResumedActivity;
2909 if (resumed != null && resumed == r) Slog.e(TAG,
2910 "validateTop...: back stack has resumed activity r=" + r
2911 + " state=" + state);
2912 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2913 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002914 }
2915 }
2916 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002917 }
2918
Craig Mautnere0570202015-05-13 13:06:11 -07002919 private String lockTaskModeToString() {
2920 switch (mLockTaskModeState) {
2921 case LOCK_TASK_MODE_LOCKED:
2922 return "LOCKED";
2923 case LOCK_TASK_MODE_PINNED:
2924 return "PINNED";
2925 case LOCK_TASK_MODE_NONE:
2926 return "NONE";
2927 default: return "unknown=" + mLockTaskModeState;
2928 }
2929 }
2930
Craig Mautner27084302013-03-25 08:05:25 -07002931 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002932 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002934 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002935 pw.print(prefix);
2936 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002937 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002938 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002939 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2940 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2941 if (packages.size() > 0) {
2942 pw.println(" mLockTaskPackages (userId:packages)=");
2943 for (int i = 0; i < packages.size(); ++i) {
2944 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2945 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2946 }
2947 }
2948 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002949 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002950
Craig Mautner20e72272013-04-01 13:45:53 -07002951 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002952 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002953 }
2954
Dianne Hackborn390517b2013-05-30 15:03:32 -07002955 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2956 boolean needSep, String prefix) {
2957 if (activity != null) {
2958 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2959 if (needSep) {
2960 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002961 }
2962 pw.print(prefix);
2963 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002964 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002965 }
2966 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002967 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002968 }
2969
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2971 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002972 boolean printed = false;
2973 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002974 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2975 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002976 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002977 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002978 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 final ActivityStack stack = stacks.get(stackNdx);
2981 StringBuilder stackHeader = new StringBuilder(128);
2982 stackHeader.append(" Stack #");
2983 stackHeader.append(stack.mStackId);
2984 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002985 stackHeader.append("\n");
2986 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2987 stackHeader.append("\n");
2988 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002989 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2990 needSep, stackHeader.toString());
2991 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2992 !dumpAll, false, dumpPackage, true,
2993 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002994
Craig Mautner4a1cb222013-12-04 16:14:06 -08002995 needSep = printed;
2996 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2997 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002998 if (pr) {
2999 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003000 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003001 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3003 " mResumedActivity: ");
3004 if (pr) {
3005 printed = true;
3006 needSep = false;
3007 }
3008 if (dumpAll) {
3009 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3010 " mLastPausedActivity: ");
3011 if (pr) {
3012 printed = true;
3013 needSep = true;
3014 }
3015 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3016 needSep, " mLastNoHistoryActivity: ");
3017 }
3018 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 }
3020 }
3021
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003022 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3023 false, dumpPackage, true, " Activities waiting to finish:", null);
3024 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3025 false, dumpPackage, true, " Activities waiting to stop:", null);
3026 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3027 false, dumpPackage, true, " Activities waiting for another to become visible:",
3028 null);
3029 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3030 false, dumpPackage, true, " Activities waiting to sleep:", null);
3031 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3032 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003033
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003034 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 }
3036
Dianne Hackborn390517b2013-05-30 15:03:32 -07003037 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003038 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003039 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003040 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003041 String innerPrefix = null;
3042 String[] args = null;
3043 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003044 for (int i=list.size()-1; i>=0; i--) {
3045 final ActivityRecord r = list.get(i);
3046 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3047 continue;
3048 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003049 if (innerPrefix == null) {
3050 innerPrefix = prefix + " ";
3051 args = new String[0];
3052 }
3053 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003054 final boolean full = !brief && (complete || !r.isInHistory());
3055 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003056 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057 needNL = false;
3058 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003059 if (header1 != null) {
3060 pw.println(header1);
3061 header1 = null;
3062 }
3063 if (header2 != null) {
3064 pw.println(header2);
3065 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003066 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003067 if (lastTask != r.task) {
3068 lastTask = r.task;
3069 pw.print(prefix);
3070 pw.print(full ? "* " : " ");
3071 pw.println(lastTask);
3072 if (full) {
3073 lastTask.dump(pw, prefix + " ");
3074 } else if (complete) {
3075 // Complete + brief == give a summary. Isn't that obvious?!?
3076 if (lastTask.intent != null) {
3077 pw.print(prefix); pw.print(" ");
3078 pw.println(lastTask.intent.toInsecureStringWithClip());
3079 }
3080 }
3081 }
3082 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3083 pw.print(" #"); pw.print(i); pw.print(": ");
3084 pw.println(r);
3085 if (full) {
3086 r.dump(pw, innerPrefix);
3087 } else if (complete) {
3088 // Complete + brief == give a summary. Isn't that obvious?!?
3089 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3090 if (r.app != null) {
3091 pw.print(innerPrefix); pw.println(r.app);
3092 }
3093 }
3094 if (client && r.app != null && r.app.thread != null) {
3095 // flush anything that is already in the PrintWriter since the thread is going
3096 // to write to the file descriptor directly
3097 pw.flush();
3098 try {
3099 TransferPipe tp = new TransferPipe();
3100 try {
3101 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3102 r.appToken, innerPrefix, args);
3103 // Short timeout, since blocking here can
3104 // deadlock with the application.
3105 tp.go(fd, 2000);
3106 } finally {
3107 tp.kill();
3108 }
3109 } catch (IOException e) {
3110 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3111 } catch (RemoteException e) {
3112 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3113 }
3114 needNL = true;
3115 }
3116 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003117 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003118 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003119
Craig Mautnerf3333272013-04-22 10:55:53 -07003120 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003121 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3122 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003123 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3124 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003125 }
3126
3127 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003128 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003129 }
3130
3131 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003132 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3133 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003134 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3135 }
3136
Craig Mautner05d29032013-05-03 13:40:13 -07003137 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003138 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3139 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3140 }
Craig Mautner05d29032013-05-03 13:40:13 -07003141 }
3142
Craig Mautner0eea92c2013-05-16 13:35:39 -07003143 void removeSleepTimeouts() {
3144 mSleepTimeout = false;
3145 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3146 }
3147
3148 final void scheduleSleepTimeout() {
3149 removeSleepTimeouts();
3150 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3151 }
3152
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 @Override
3154 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003155 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003156 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3157 }
3158
3159 @Override
3160 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003161 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3163 }
3164
3165 @Override
3166 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003167 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003168 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3169 }
3170
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003171 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003172 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003174 newDisplay = mActivityDisplays.get(displayId) == null;
3175 if (newDisplay) {
3176 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003177 if (activityDisplay.mDisplay == null) {
3178 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3179 return;
3180 }
Craig Mautner4504de52013-12-20 09:06:56 -08003181 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003182 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003183 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 }
Craig Mautner4504de52013-12-20 09:06:56 -08003185 if (newDisplay) {
3186 mWindowManager.onDisplayAdded(displayId);
3187 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003188 }
3189
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003190 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3191 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3192 return;
3193 }
3194 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3195 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3196 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3197 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3198 }
3199
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003200 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003201 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3203 if (activityDisplay != null) {
3204 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003205 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003206 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003207 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003208 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003209 }
3210 }
3211 mWindowManager.onDisplayRemoved(displayId);
3212 }
3213
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003214 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003215 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003216 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3217 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003218 // TODO: Update the bounds.
3219 }
3220 }
3221 mWindowManager.onDisplayChanged(displayId);
3222 }
3223
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003224 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 StackInfo info = new StackInfo();
3226 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3227 info.displayId = Display.DEFAULT_DISPLAY;
3228 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003229 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003230
3231 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3232 final int numTasks = tasks.size();
3233 int[] taskIds = new int[numTasks];
3234 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003235 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003236 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003237 for (int i = 0; i < numTasks; ++i) {
3238 final TaskRecord task = tasks.get(i);
3239 taskIds[i] = task.taskId;
3240 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3241 : task.realActivity != null ? task.realActivity.flattenToString()
3242 : task.getTopActivity() != null ? task.getTopActivity().packageName
3243 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003244 taskBounds[i] = new Rect();
3245 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003246 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003247 }
3248 info.taskIds = taskIds;
3249 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003250 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003251 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003252 return info;
3253 }
3254
3255 StackInfo getStackInfoLocked(int stackId) {
3256 ActivityStack stack = getStack(stackId);
3257 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003258 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003259 }
3260 return null;
3261 }
3262
3263 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003264 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003265 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3266 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003267 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003268 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003269 }
3270 }
3271 return list;
3272 }
3273
Craig Mautner15df08a2015-04-01 12:17:18 -07003274 TaskRecord getLockedTaskLocked() {
3275 final int top = mLockTaskModeTasks.size() - 1;
3276 if (top >= 0) {
3277 return mLockTaskModeTasks.get(top);
3278 }
3279 return null;
3280 }
3281
3282 boolean isLockedTask(TaskRecord task) {
3283 return mLockTaskModeTasks.contains(task);
3284 }
3285
3286 boolean isLastLockedTask(TaskRecord task) {
3287 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3288 }
3289
3290 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003291 if (!mLockTaskModeTasks.remove(task)) {
3292 return;
3293 }
3294 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3295 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003296 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003297 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3298 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003299 final Message lockTaskMsg = Message.obtain();
3300 lockTaskMsg.arg1 = task.userId;
3301 lockTaskMsg.what = LOCK_TASK_END_MSG;
3302 mHandler.sendMessage(lockTaskMsg);
3303 }
3304 }
3305
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003306 void showNonResizeableDockToastIfNeeded(
3307 TaskRecord task, int preferredStackId, int actualStackId) {
3308 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3309 return;
3310 }
3311
Jorim Jaggi51605272016-02-24 18:06:49 -05003312 if (!task.canGoInDockedStack() || task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003313 // Display warning toast if we tried to put a non-dockable task in the docked stack or
Jorim Jaggi51605272016-02-24 18:06:49 -05003314 // the task was forced to be resizable by the system.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003315 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3316 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003317 }
3318
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003319 void showLockTaskToast() {
3320 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003321 }
3322
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003323 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3324 if (mLockTaskModeTasks.contains(task)) {
3325 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3326 }
3327 }
3328
Craig Mautner432f64e2015-05-20 14:59:57 -07003329 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3330 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003331 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003332 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003333 final TaskRecord lockedTask = getLockedTaskLocked();
3334 if (lockedTask != null) {
3335 removeLockedTaskLocked(lockedTask);
3336 if (!mLockTaskModeTasks.isEmpty()) {
3337 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003338 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3339 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003340 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003341 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003342 return;
3343 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003344 }
Craig Mautnere0570202015-05-13 13:06:11 -07003345 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3346 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003347 return;
3348 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003349
3350 // Should have already been checked, but do it again.
3351 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003352 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3353 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003354 return;
3355 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003356 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003357 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003358 return;
3359 }
3360
3361 if (mLockTaskModeTasks.isEmpty()) {
3362 // First locktask.
3363 final Message lockTaskMsg = Message.obtain();
3364 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3365 lockTaskMsg.arg1 = task.userId;
3366 lockTaskMsg.what = LOCK_TASK_START_MSG;
3367 lockTaskMsg.arg2 = lockTaskModeState;
3368 mHandler.sendMessage(lockTaskMsg);
3369 }
3370 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003371 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3372 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003373 mLockTaskModeTasks.remove(task);
3374 mLockTaskModeTasks.add(task);
3375
3376 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003377 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003378 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003379
3380 if (andResume) {
3381 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003382 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003383 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003384 }
3385
3386 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003387 return isLockTaskModeViolation(task, false);
3388 }
3389
3390 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3391 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003392 return false;
3393 }
3394 final int lockTaskAuth = task.mLockTaskAuth;
3395 switch (lockTaskAuth) {
3396 case LOCK_TASK_AUTH_DONT_LOCK:
3397 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003398 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003399 case LOCK_TASK_AUTH_LAUNCHABLE:
3400 case LOCK_TASK_AUTH_WHITELISTED:
3401 return false;
3402 case LOCK_TASK_AUTH_PINNABLE:
3403 // Pinnable tasks can't be launched on top of locktask tasks.
3404 return !mLockTaskModeTasks.isEmpty();
3405 default:
3406 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3407 return true;
3408 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003409 }
3410
Craig Mautner15df08a2015-04-01 12:17:18 -07003411 void onLockTaskPackagesUpdatedLocked() {
3412 boolean didSomething = false;
3413 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3414 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003415 final boolean wasWhitelisted =
3416 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3417 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003418 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003419 final boolean isWhitelisted =
3420 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3421 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3422 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003423 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003424 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3425 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003426 removeLockedTaskLocked(lockedTask);
3427 lockedTask.performClearTaskLocked();
3428 didSomething = true;
3429 }
3430 }
3431 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3432 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3433 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3434 final ActivityStack stack = stacks.get(stackNdx);
3435 stack.onLockTaskPackagesUpdatedLocked();
3436 }
3437 }
Craig Mautnere0570202015-05-13 13:06:11 -07003438 final ActivityRecord r = topRunningActivityLocked();
3439 final TaskRecord task = r != null ? r.task : null;
3440 if (mLockTaskModeTasks.isEmpty() && task != null
3441 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3442 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003443 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3444 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3445 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3446 false);
3447 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003448 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003449 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003450 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003451 }
3452 }
3453
Benjamin Franz43261142015-02-11 15:59:44 +00003454 int getLockTaskModeState() {
3455 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003456 }
3457
Jorim Jaggife89d122015-12-22 16:28:44 +01003458 void activityRelaunchedLocked(IBinder token) {
3459 mWindowManager.notifyAppRelaunchingFinished(token);
3460 }
3461
3462 void activityRelaunchingLocked(ActivityRecord r) {
3463 mWindowManager.notifyAppRelaunching(r.appToken);
3464 }
3465
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003466 void logStackState() {
3467 mActivityMetricsLogger.logWindowState();
3468 }
3469
Wale Ogunwale22e25262016-02-01 10:32:02 -08003470 void scheduleReportMultiWindowChanged(TaskRecord task) {
3471 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3472 final ActivityRecord r = task.mActivities.get(i);
3473 if (r.app != null && r.app.thread != null) {
3474 mMultiWindowModeChangedActivities.add(r);
3475 }
3476 }
3477
3478 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3479 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3480 }
3481 }
3482
3483 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3484 final ActivityStack stack = task.stack;
3485 if (prevStack == null || prevStack == stack
3486 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3487 return;
3488 }
3489
3490 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3491 final ActivityRecord r = task.mActivities.get(i);
3492 if (r.app != null && r.app.thread != null) {
3493 mPipModeChangedActivities.add(r);
3494 }
3495 }
3496
3497 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3498 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3499 }
3500 }
3501
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003502 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003503
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003504 public ActivityStackSupervisorHandler(Looper looper) {
3505 super(looper);
3506 }
3507
Craig Mautnerf3333272013-04-22 10:55:53 -07003508 void activityIdleInternal(ActivityRecord r) {
3509 synchronized (mService) {
3510 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3511 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003512 }
3513
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003514 @Override
3515 public void handleMessage(Message msg) {
3516 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003517 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3518 synchronized (mService) {
3519 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3520 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3521 r.scheduleMultiWindowChanged();
3522 }
3523 }
3524 } break;
3525 case REPORT_PIP_MODE_CHANGED_MSG: {
3526 synchronized (mService) {
3527 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3528 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3529 r.schedulePictureInPictureChanged();
3530 }
3531 }
3532 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003533 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003534 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3535 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003536 if (mService.mDidDexOpt) {
3537 mService.mDidDexOpt = false;
3538 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3539 nmsg.obj = msg.obj;
3540 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3541 return;
3542 }
3543 // We don't at this point know if the activity is fullscreen,
3544 // so we need to be conservative and assume it isn't.
3545 activityIdleInternal((ActivityRecord)msg.obj);
3546 } break;
3547 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003548 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003549 activityIdleInternal((ActivityRecord)msg.obj);
3550 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003551 case RESUME_TOP_ACTIVITY_MSG: {
3552 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003553 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003554 }
3555 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003556 case SLEEP_TIMEOUT_MSG: {
3557 synchronized (mService) {
3558 if (mService.isSleepingOrShuttingDown()) {
3559 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3560 mSleepTimeout = true;
3561 checkReadyForSleepLocked();
3562 }
3563 }
3564 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003565 case LAUNCH_TIMEOUT_MSG: {
3566 if (mService.mDidDexOpt) {
3567 mService.mDidDexOpt = false;
3568 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3569 return;
3570 }
3571 synchronized (mService) {
3572 if (mLaunchingActivity.isHeld()) {
3573 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3574 if (VALIDATE_WAKE_LOCK_CALLER
3575 && Binder.getCallingUid() != Process.myUid()) {
3576 throw new IllegalStateException("Calling must be system uid");
3577 }
3578 mLaunchingActivity.release();
3579 }
3580 }
3581 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003583 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 } break;
3585 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003586 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 } break;
3588 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003589 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003591 case CONTAINER_CALLBACK_VISIBILITY: {
3592 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003593 final IActivityContainerCallback callback = container.mCallback;
3594 if (callback != null) {
3595 try {
3596 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3597 } catch (RemoteException e) {
3598 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003599 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003600 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003601 case LOCK_TASK_START_MSG: {
3602 // When lock task starts, we disable the status bars.
3603 try {
Jason Monk62515be2014-05-21 16:06:19 -04003604 if (mLockTaskNotify == null) {
3605 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003606 }
Jason Monk62515be2014-05-21 16:06:19 -04003607 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003608 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003609 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003610 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003611 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003612 flags = StatusBarManager.DISABLE_MASK
3613 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003614 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003615 flags = StatusBarManager.DISABLE_MASK
3616 & (~StatusBarManager.DISABLE_BACK)
3617 & (~StatusBarManager.DISABLE_HOME)
3618 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003619 }
3620 getStatusBarService().disable(flags, mToken,
3621 mService.mContext.getPackageName());
3622 }
3623 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003624 if (getDevicePolicyManager() != null) {
3625 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003626 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003627 }
justinzhang5286d3f2014-05-12 17:06:01 -04003628 } catch (RemoteException ex) {
3629 throw new RuntimeException(ex);
3630 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003631 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003632 case LOCK_TASK_END_MSG: {
3633 // When lock task ends, we enable the status bars.
3634 try {
Jason Monk62515be2014-05-21 16:06:19 -04003635 if (getStatusBarService() != null) {
3636 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3637 mService.mContext.getPackageName());
3638 }
3639 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003640 if (getDevicePolicyManager() != null) {
3641 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3642 msg.arg1);
3643 }
Jason Monk62515be2014-05-21 16:06:19 -04003644 if (mLockTaskNotify == null) {
3645 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3646 }
3647 mLockTaskNotify.show(false);
3648 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003649 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003650 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003651 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003652 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003653 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003654 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003655 new LockPatternUtils(mService.mContext)
3656 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003657 }
3658 } catch (SettingNotFoundException e) {
3659 // No setting, don't lock.
3660 }
justinzhang5286d3f2014-05-12 17:06:01 -04003661 } catch (RemoteException ex) {
3662 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003663 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003664 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003665 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003666 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003667 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3668 if (mLockTaskNotify == null) {
3669 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3670 }
3671 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3672 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003673 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3674 final ActivityContainer container = (ActivityContainer) msg.obj;
3675 final IActivityContainerCallback callback = container.mCallback;
3676 if (callback != null) {
3677 try {
3678 callback.onAllActivitiesComplete(container.asBinder());
3679 } catch (RemoteException e) {
3680 }
3681 }
3682 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003683 case LAUNCH_TASK_BEHIND_COMPLETE: {
3684 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003685 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003686 if (r != null) {
3687 handleLaunchTaskBehindCompleteLocked(r);
3688 }
3689 }
3690 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003691
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003692 }
3693 }
3694 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003695
Ying Wangb081a592014-04-22 15:20:16 -07003696 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003697 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3698 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003699 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003700 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003702 ActivityRecord mParentActivity = null;
3703 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003704
Craig Mautnere3a00d72014-04-16 08:31:19 -07003705 boolean mVisible = true;
3706
Craig Mautner4a1cb222013-12-04 16:14:06 -08003707 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003708 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003709
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003710 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3711 final static int CONTAINER_STATE_NO_SURFACE = 1;
3712 final static int CONTAINER_STATE_FINISHING = 2;
3713 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3714
3715 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 synchronized (mService) {
3717 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003718 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003719 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003720 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003722 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003723
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003724 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003725 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003726 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003727 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003728 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003729 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003730 }
3731
3732 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003733 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003734 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003735 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3736 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003737 return;
3738 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003739 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 }
3741 }
3742
3743 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003744 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003745 synchronized (mService) {
3746 if (mActivityDisplay != null) {
3747 return mActivityDisplay.mDisplayId;
3748 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003749 }
3750 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 }
3752
Jeff Brownca9bc702014-02-11 14:32:56 -08003753 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003754 public int getStackId() {
3755 synchronized (mService) {
3756 return mStackId;
3757 }
3758 }
3759
3760 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003761 public boolean injectEvent(InputEvent event) {
3762 final long origId = Binder.clearCallingIdentity();
3763 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003764 synchronized (mService) {
3765 if (mActivityDisplay != null) {
3766 return mInputManagerInternal.injectInputEvent(event,
3767 mActivityDisplay.mDisplayId,
3768 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3769 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003770 }
3771 return false;
3772 } finally {
3773 Binder.restoreCallingIdentity(origId);
3774 }
3775 }
3776
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003777 @Override
3778 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003779 synchronized (mService) {
3780 if (mContainerState == CONTAINER_STATE_FINISHING) {
3781 return;
3782 }
3783 mContainerState = CONTAINER_STATE_FINISHING;
3784
Craig Mautnerd163e752014-06-13 17:18:47 -07003785 long origId = Binder.clearCallingIdentity();
3786 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003787 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003788 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003789 } finally {
3790 Binder.restoreCallingIdentity(origId);
3791 }
3792 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003793 }
3794
Craig Mautner60257702014-09-17 15:02:33 -07003795 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003796 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003797 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003798 if (mActivityDisplay != null) {
3799 mActivityDisplay.detachActivitiesLocked(mStack);
3800 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003801 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 }
3803 }
3804
3805 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003806 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003807 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003808 }
3809
3810 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003811 public final int startActivityIntentSender(IIntentSender intentSender)
3812 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003813 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3814
3815 if (!(intentSender instanceof PendingIntentRecord)) {
3816 throw new IllegalArgumentException("Bad PendingIntent object");
3817 }
3818
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003819 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003820 Binder.getCallingUid(), mCurrentUser, false,
3821 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003822
3823 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3824 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3825 pendingIntent.key.requestResolvedType);
3826
3827 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3828 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3829 }
3830
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003831 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003832 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003833 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003834 throw new SecurityException(
3835 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3836 }
3837 }
3838
Craig Mautnerdf88d732014-01-27 09:21:32 -08003839 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 public IBinder asBinder() {
3841 return this;
3842 }
3843
Craig Mautner4504de52013-12-20 09:06:56 -08003844 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003845 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003846 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003847 }
3848
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 ActivityStackSupervisor getOuter() {
3850 return ActivityStackSupervisor.this;
3851 }
3852
Craig Mautnerd163e752014-06-13 17:18:47 -07003853 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003854 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855 }
3856
Craig Mautner6985bad2014-04-21 15:22:06 -07003857 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003858 void setVisible(boolean visible) {
3859 if (mVisible != visible) {
3860 mVisible = visible;
3861 if (mCallback != null) {
3862 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3863 0 /* unused */, this).sendToTarget();
3864 }
3865 }
3866 }
3867
Craig Mautner6985bad2014-04-21 15:22:06 -07003868 void setDrawn() {
3869 }
3870
Craig Mautner1b4bf852014-05-26 15:06:32 -07003871 // You can always start a new task on a regular ActivityStack.
3872 boolean isEligibleForNewTasks() {
3873 return true;
3874 }
3875
Craig Mautnerd163e752014-06-13 17:18:47 -07003876 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003877 detachLocked();
3878 deleteActivityContainer(this);
3879 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003880 }
3881
Craig Mautner34b73df2014-01-12 21:11:08 -08003882 @Override
3883 public String toString() {
3884 return mIdString + (mActivityDisplay == null ? "N" : "A");
3885 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003886 }
3887
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003888 private class VirtualActivityContainer extends ActivityContainer {
3889 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003890 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003891
3892 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3893 super(getNextStackId());
3894 mParentActivity = parent;
3895 mCallback = callback;
3896 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003897 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003898 }
3899
3900 @Override
3901 public void setSurface(Surface surface, int width, int height, int density) {
3902 super.setSurface(surface, width, height, density);
3903
3904 synchronized (mService) {
3905 final long origId = Binder.clearCallingIdentity();
3906 try {
3907 setSurfaceLocked(surface, width, height, density);
3908 } finally {
3909 Binder.restoreCallingIdentity(origId);
3910 }
3911 }
3912 }
3913
3914 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3915 if (mContainerState == CONTAINER_STATE_FINISHING) {
3916 return;
3917 }
3918 VirtualActivityDisplay virtualActivityDisplay =
3919 (VirtualActivityDisplay) mActivityDisplay;
3920 if (virtualActivityDisplay == null) {
3921 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003922 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003923 mActivityDisplay = virtualActivityDisplay;
3924 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003925 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003926 }
3927
3928 if (mSurface != null) {
3929 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003930 }
3931
Craig Mautner6985bad2014-04-21 15:22:06 -07003932 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003933 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003934 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003935 } else {
3936 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003937 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003938 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003939 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003940 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003941 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003942
Craig Mautnerd163e752014-06-13 17:18:47 -07003943 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003944
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003945 if (DEBUG_STACK) Slog.d(TAG_STACK,
3946 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003947 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003948
Craig Mautner6985bad2014-04-21 15:22:06 -07003949 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003950 boolean isAttachedLocked() {
3951 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003952 }
3953
3954 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003955 void setDrawn() {
3956 synchronized (mService) {
3957 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003958 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003959 }
3960 }
3961
Craig Mautner1b4bf852014-05-26 15:06:32 -07003962 // Never start a new task on an ActivityView if it isn't explicitly specified.
3963 @Override
3964 boolean isEligibleForNewTasks() {
3965 return false;
3966 }
3967
Craig Mautnerd163e752014-06-13 17:18:47 -07003968 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003969 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003970 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3971 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3972 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3973 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3974 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003975 }
3976 }
3977
Craig Mautner4a1cb222013-12-04 16:14:06 -08003978 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3979 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003980 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003981 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003982 int mDisplayId;
3983 Display mDisplay;
3984 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003985
Craig Mautner4a1cb222013-12-04 16:14:06 -08003986 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3987 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003988 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989
Jose Lima4b6c6692014-08-12 17:41:12 -07003990 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003991
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003992 ActivityDisplay() {
3993 }
Craig Mautner4504de52013-12-20 09:06:56 -08003994
Craig Mautner1a70a162014-09-13 12:09:31 -07003995 // After instantiation, check that mDisplay is not null before using this. The alternative
3996 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003997 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003998 final Display display = mDisplayManager.getDisplay(displayId);
3999 if (display == null) {
4000 return;
4001 }
4002 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004003 }
4004
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004005 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004006 mDisplay = display;
4007 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004009 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004011 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004012 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004013 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4014 + " onTop=" + onTop);
4015 if (onTop) {
4016 mStacks.add(stack);
4017 } else {
4018 mStacks.add(0, stack);
4019 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004020 }
4021
Craig Mautnere0a38842013-12-16 16:14:02 -08004022 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004023 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004024 + " from displayId=" + mDisplayId);
4025 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 }
4027
Jose Lima4b6c6692014-08-12 17:41:12 -07004028 void setVisibleBehindActivity(ActivityRecord r) {
4029 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004030 }
4031
Jose Lima4b6c6692014-08-12 17:41:12 -07004032 boolean hasVisibleBehindActivity() {
4033 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004034 }
4035
Craig Mautner34b73df2014-01-12 21:11:08 -08004036 @Override
4037 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004038 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4039 }
4040 }
4041
4042 class VirtualActivityDisplay extends ActivityDisplay {
4043 VirtualDisplay mVirtualDisplay;
4044
Craig Mautner6985bad2014-04-21 15:22:06 -07004045 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004046 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004047 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4048 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4049 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4050 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004051
4052 init(mVirtualDisplay.getDisplay());
4053
4054 mWindowManager.handleDisplayAdded(mDisplayId);
4055 }
4056
4057 void setSurface(Surface surface) {
4058 if (mVirtualDisplay != null) {
4059 mVirtualDisplay.setSurface(surface);
4060 }
4061 }
4062
4063 @Override
4064 void detachActivitiesLocked(ActivityStack stack) {
4065 super.detachActivitiesLocked(stack);
4066 if (mVirtualDisplay != null) {
4067 mVirtualDisplay.release();
4068 mVirtualDisplay = null;
4069 }
4070 }
4071
4072 @Override
4073 public String toString() {
4074 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004075 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004076 }
Jose Lima58e66d62014-05-27 20:00:27 -07004077
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004078 /**
4079 * Adjust bounds to stay within stack bounds.
4080 *
4081 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4082 * that keep them unchanged, but be contained within the stack bounds.
4083 *
4084 * @param bounds Bounds to be adjusted.
4085 * @param stackBounds Bounds within which the other bounds should remain.
4086 */
4087 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4088 if (stackBounds == null || stackBounds.contains(bounds)) {
4089 return;
4090 }
4091
4092 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4093 final int maxRight = stackBounds.right
4094 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4095 int horizontalDiff = stackBounds.left - bounds.left;
4096 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4097 || (bounds.left + horizontalDiff >= maxRight)) {
4098 horizontalDiff = maxRight - bounds.left;
4099 }
4100 bounds.left += horizontalDiff;
4101 bounds.right += horizontalDiff;
4102 }
4103
4104 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4105 final int maxBottom = stackBounds.bottom
4106 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4107 int verticalDiff = stackBounds.top - bounds.top;
4108 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4109 || (bounds.top + verticalDiff >= maxBottom)) {
4110 verticalDiff = maxBottom - bounds.top;
4111 }
4112 bounds.top += verticalDiff;
4113 bounds.bottom += verticalDiff;
4114 }
4115 }
4116
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004117 ActivityStack findStackBehind(ActivityStack stack) {
4118 // TODO(multi-display): We are only looking for stacks on the default display.
4119 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4120 if (display == null) {
4121 return null;
4122 }
4123 final ArrayList<ActivityStack> stacks = display.mStacks;
4124 for (int i = stacks.size() - 1; i >= 0; i--) {
4125 if (stacks.get(i) == stack && i > 0) {
4126 return stacks.get(i - 1);
4127 }
4128 }
4129 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4130 + " in=" + stacks);
4131 }
Craig Mautner27084302013-03-25 08:05:25 -07004132}