blob: 3496238f905f087ee2cdf2d3311bdd5f5b558e90 [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;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800122import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100123import static android.content.pm.PackageManager.PERMISSION_GRANTED;
124import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
125import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
138import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
149import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
150import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
151import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
152import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
153import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
154import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
155import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
156import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
157import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
158import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
159import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
160import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
161import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
162import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
163import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
164import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
166import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
168
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800170 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700171 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700172 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700173 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700174 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800180 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182
Craig Mautnerf3333272013-04-22 10:55:53 -0700183 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700185
Craig Mautner0eea92c2013-05-16 13:35:39 -0700186 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700188
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700191
Craig Mautner05d29032013-05-03 13:40:13 -0700192 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
193 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
194 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700195 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700196 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800197 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
198 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
199 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700200 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400201 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
202 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700203 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700204 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700205 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800206 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
207 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800208
Wale Ogunwale040b4702015-08-06 18:10:50 -0700209 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210
Jason Monk62515be2014-05-21 16:06:19 -0400211 private static final String LOCK_TASK_TAG = "Lock-to-App";
212
Wale Ogunwale040b4702015-08-06 18:10:50 -0700213 // Used to indicate if an object (e.g. stack) that we are trying to get
214 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800215 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700217 // Used to indicate that windows of activities should be preserved during the resize.
218 static final boolean PRESERVE_WINDOWS = true;
219
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220 // Used to indicate if an object (e.g. task) should be moved/created
221 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700222 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223
224 // Used to indicate that an objects (e.g. task) removal from its container
225 // (e.g. stack) is due to it moving to another container.
226 static final boolean MOVING = true;
227
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700228 // Force the focus to change to the stack we are moving a task to..
229 static final boolean FORCE_FOCUS = true;
230
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700231 // Restore task from the saved recents if it can't be found in any live stack.
232 static final boolean RESTORE_FROM_RECENTS = true;
233
Svetoslav7008b512015-06-24 18:47:07 -0700234 // Activity actions an app cannot start if it uses a permission which is not granted.
235 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
236 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700237
Svetoslav7008b512015-06-24 18:47:07 -0700238 static {
239 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
240 Manifest.permission.CAMERA);
241 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
242 Manifest.permission.CAMERA);
243 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
244 Manifest.permission.CALL_PHONE);
245 }
246
Svet Ganov99b60432015-06-27 13:15:22 -0700247 /** Action restriction: launching the activity is not restricted. */
248 private static final int ACTIVITY_RESTRICTION_NONE = 0;
249 /** Action restriction: launching the activity is restricted by a permission. */
250 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
251 /** Action restriction: launching the activity is restricted by an app op. */
252 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700253
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700254 // The height/width divide used when fitting a task within a bounds with method
255 // {@link #fitWithinBounds}.
256 // We always want the task to to be visible in the bounds without affecting its size when
257 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
258 // the input bounds right or bottom side minus the width or height divided by this value.
259 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
260
justinzhang5286d3f2014-05-12 17:06:01 -0400261 /** Status Bar Service **/
262 private IBinder mToken = new Binder();
263 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400264 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400265
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700266 // For debugging to make sure the caller when acquiring/releasing our
267 // wake lock is the system process.
268 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800269 /** The number of distinct task ids that can be assigned to the tasks of a single user */
270 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700271
Craig Mautner27084302013-03-25 08:05:25 -0700272 final ActivityManagerService mService;
273
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800274 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800275
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700276 final ActivityStackSupervisorHandler mHandler;
277
278 /** Short cut */
279 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800280 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700281
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700282 /** Counter for next free stack ID to use for dynamic activity stacks. */
283 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700284
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800285 /**
286 * Maps the task identifier that activities are currently being started in to the userId of the
287 * task. Each time a new task is created, the entry for the userId of the task is incremented
288 */
289 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700290
Craig Mautner2420ead2013-04-01 17:13:20 -0700291 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800292 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700293
Craig Mautnere0a38842013-12-16 16:14:02 -0800294 /** The stack containing the launcher app. Assumed to always be attached to
295 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800296 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700297
Craig Mautnere0a38842013-12-16 16:14:02 -0800298 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800299 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Craig Mautner4a1cb222013-12-04 16:14:06 -0800301 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
302 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800303 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800304 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700305
306 /** List of activities that are waiting for a new activity to become visible before completing
307 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800308 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700309
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700310 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800311 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700312
313 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315
Craig Mautnerde4ef022013-04-07 19:01:33 -0700316 /** List of activities that are ready to be stopped, but waiting for the next activity to
317 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
Craig Mautnerf3333272013-04-22 10:55:53 -0700320 /** List of activities that are ready to be finished, but waiting for the previous activity to
321 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700323
Craig Mautner0eea92c2013-05-16 13:35:39 -0700324 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800325 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326
Wale Ogunwale22e25262016-02-01 10:32:02 -0800327 /** List of activities whose multi-window mode changed that we need to report to the
328 * application */
329 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
330
331 /** List of activities whose picture-in-picture mode changed that we need to report to the
332 * application */
333 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
334
Craig Mautnerf3333272013-04-22 10:55:53 -0700335 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700336 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700337
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700338 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700339 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700340
Craig Mautnerde4ef022013-04-07 19:01:33 -0700341 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
342 * is being brought in front of us. */
343 boolean mUserLeaving = false;
344
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345 /** Set when we have taken too long waiting to go to sleep. */
346 boolean mSleepTimeout = false;
347
348 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700349 * We don't want to allow the device to go to sleep while in the process
350 * of launching an activity. This is primarily to allow alarm intent
351 * receivers to launch an activity and get that to run before the device
352 * goes back to sleep.
353 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700354 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700355
356 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700357 * Set when the system is going to sleep, until we have
358 * successfully paused the current activity and released our wake lock.
359 * At that point the system is allowed to actually sleep.
360 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700361 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700362
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700363 /** Stack id of the front stack when user switched, indexed by userId. */
364 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700365
Craig Mautner4504de52013-12-20 09:06:56 -0800366 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800367 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700368 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369
370 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700371 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800372
Jeff Brownca9bc702014-02-11 14:32:56 -0800373 InputManagerInternal mInputManagerInternal;
374
Craig Mautner15df08a2015-04-01 12:17:18 -0700375 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
376 * may be finished until there is only one entry left. If this is empty the system is not
377 * in lockTask mode. */
378 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000379 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700380 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
381 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000382 */
383 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400384 /**
385 * Notifies the user when entering/exiting lock-task.
386 */
387 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800388
Wale Ogunwaled046a012015-12-24 13:05:59 -0800389 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800390 boolean inResumeTopActivity;
391
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700392 // 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 -0700393 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700394 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700395
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700396 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
397 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800398 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700399
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700400 // The default minimal size that will be used if the activity doesn't specify its minimal size.
401 // It will be calculated when the default display gets added.
402 private int mDefaultMinimalSizeOfResizeableTask = -1;
403
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700404 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
405 private boolean mTaskLayersChanged = true;
406
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800407 private final ActivityMetricsLogger mActivityMetricsLogger;
408
Jorim Jaggidc249c42015-12-15 14:57:31 -0800409 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
410
Wale Ogunwale39381972015-12-17 17:15:29 -0800411 static class FindTaskResult {
412 ActivityRecord r;
413 boolean matchedByRootAffinity;
414 }
415 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
416
Craig Mautneree36c772014-07-16 14:56:05 -0700417 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100418 * Used to keep track whether app visibilities got changed since the last pause. Useful to
419 * determine whether to invoke the task stack change listener after pausing.
420 */
421 boolean mAppVisibilitiesChangedSinceLastPause;
422
423 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700424 * Description of a request to start a new activity, which has been held
425 * due to app switches being disabled.
426 */
427 static class PendingActivityLaunch {
428 final ActivityRecord r;
429 final ActivityRecord sourceRecord;
430 final int startFlags;
431 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700432 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700433
434 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700435 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700436 r = _r;
437 sourceRecord = _sourceRecord;
438 startFlags = _startFlags;
439 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700440 callerApp = _callerApp;
441 }
442
443 void sendErrorResult(String message) {
444 try {
445 if (callerApp.thread != null) {
446 callerApp.thread.scheduleCrash(message);
447 }
448 } catch (RemoteException e) {
449 Slog.e(TAG, "Exception scheduling crash of failed "
450 + "activity launcher sourceRecord=" + sourceRecord, e);
451 }
Craig Mautneree36c772014-07-16 14:56:05 -0700452 }
453 }
454
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800455 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700456 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700457 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800458 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800459 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700460 }
461
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800462 void setRecentTasks(RecentTasks recentTasks) {
463 mRecentTasks = recentTasks;
464 }
465
Jeff Brown2c43c332014-06-12 22:38:59 -0700466 /**
467 * At the time when the constructor runs, the power manager has not yet been
468 * initialized. So we initialize our wakelocks afterwards.
469 */
470 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800471 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700472 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700473 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700474 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700475 }
476
justinzhang5286d3f2014-05-12 17:06:01 -0400477 // This function returns a IStatusBarService. The value is from ServiceManager.
478 // getService and is cached.
479 private IStatusBarService getStatusBarService() {
480 synchronized (mService) {
481 if (mStatusBarService == null) {
482 mStatusBarService = IStatusBarService.Stub.asInterface(
483 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
484 if (mStatusBarService == null) {
485 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
486 }
487 }
488 return mStatusBarService;
489 }
490 }
491
Jason Monk35c62a42014-06-17 10:24:47 -0400492 private IDevicePolicyManager getDevicePolicyManager() {
493 synchronized (mService) {
494 if (mDevicePolicyManager == null) {
495 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
496 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
497 if (mDevicePolicyManager == null) {
498 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
499 }
500 }
501 return mDevicePolicyManager;
502 }
503 }
504
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700505 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800506 synchronized (mService) {
507 mWindowManager = wm;
508
509 mDisplayManager =
510 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
511 mDisplayManager.registerDisplayListener(this, null);
512
513 Display[] displays = mDisplayManager.getDisplays();
514 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
515 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800516 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700517 if (activityDisplay.mDisplay == null) {
518 throw new IllegalStateException("Default Display does not exist");
519 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800520 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700521 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 }
523
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800524 mHomeStack = mFocusedStack = mLastFocusedStack =
525 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800526
527 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800528 }
Craig Mautner27084302013-03-25 08:05:25 -0700529 }
530
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200531 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700532 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200533 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700534 }
535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700536 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800537 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700538 }
539
Craig Mautnerde4ef022013-04-07 19:01:33 -0700540 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800541 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700542 }
543
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700544 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700545 if (stack == null) {
546 return false;
547 }
548
Craig Mautnerdf88d732014-01-27 09:21:32 -0800549 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
550 if (parent != null) {
551 stack = parent.task.stack;
552 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800553 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700554 }
555
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 /** The top most stack. */
557 boolean isFrontStack(ActivityStack stack) {
558 if (stack == null) {
559 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700561
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700562 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
563 if (parent != null) {
564 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800565 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
567 }
568
Wale Ogunwaled046a012015-12-24 13:05:59 -0800569 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800570 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
571 if (!focusCandidate.isFocusable()) {
572 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
573 focusCandidate = focusCandidate.getNextFocusableStackLocked();
574 }
575
576 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800577 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800578 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800579
Wale Ogunwaled046a012015-12-24 13:05:59 -0800580 EventLogTags.writeAmFocusedStack(
581 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
582 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
583 }
584
585 final ActivityRecord r = topRunningActivityLocked();
586 if (mService.mFocusedActivity != r) {
587 // The focus activity should always be the top activity in the focused stack.
588 // There will be chaos and anarchy if it isn't...
589 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
590 }
Craig Mautnerde313752015-01-22 14:28:03 -0800591
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800592 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800593 if (r != null && r.idle) {
594 checkFinishBootingLocked();
595 }
596 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700597 }
598
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700599 void moveHomeStackToFront(String reason) {
600 mHomeStack.moveToFront(reason);
601 }
602
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700603 /** Returns true if the focus activity was adjusted to the home stack top activity. */
604 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700605 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
606 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700607 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700608 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700609
Craig Mautner84984fa2014-06-19 11:19:20 -0700610 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700611
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700612 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700613 if (top == null) {
614 return false;
615 }
616 mService.setFocusedActivityLocked(top, reason);
617 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700618 }
619
Craig Mautner299f9602015-01-26 09:47:33 -0800620 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700621 if (!mService.mBooting && !mService.mBooted) {
622 // Not ready yet!
623 return false;
624 }
625
Craig Mautner84984fa2014-06-19 11:19:20 -0700626 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
627 mWindowManager.showRecentApps();
628 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700629 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700630
Craig Mautner84984fa2014-06-19 11:19:20 -0700631 if (prev != null) {
632 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
633 }
634
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700635 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
636 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800637 final String myReason = reason + " resumeHomeStackTask";
638
Mark Lua56ea122015-10-08 13:31:01 +0800639 // Only resume home activity if isn't finishing.
640 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800641 mService.setFocusedActivityLocked(r, myReason);
642 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700643 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800644 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700645 }
646
Craig Mautner8d341ef2013-03-26 09:03:27 -0700647 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700648 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700649 }
650
651 /**
652 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
653 * @param id Id of the task we would like returned.
654 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
655 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700656 * @param stackId The stack to restore the task to (default launch stack will be used if
657 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700658 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700659 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800662 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
664 ActivityStack stack = stacks.get(stackNdx);
665 TaskRecord task = stack.taskForIdLocked(id);
666 if (task != null) {
667 return task;
668 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700669 }
670 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800671
672 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700673 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800674 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800675 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700676 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800677 return null;
678 }
679
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700680 if (!restoreFromRecents) {
681 return task;
682 }
683
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700684 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
686 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800687 return null;
688 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700689 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800690 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700691 }
692
Craig Mautner6170f732013-04-02 13:05:23 -0700693 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800694 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
698 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
699 if (r != null) {
700 return r;
701 }
Craig Mautner6170f732013-04-02 13:05:23 -0700702 }
703 }
704 return null;
705 }
706
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000707 boolean isFocusedUserLockedProfile() {
708 final int userId = mFocusedStack.topRunningActivityLocked().userId;
709 return userId != UserHandle.myUserId()
710 && mService.mUserController.shouldConfirmCredentials(userId);
711 }
712
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800713 void setNextTaskIdForUserLocked(int taskId, int userId) {
714 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
715 if (taskId > currentTaskId) {
716 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700717 }
718 }
719
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800720 int getNextTaskIdForUserLocked(int userId) {
721 mRecentTasks.loadUserRecentsLocked(userId);
722 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
723 // for a userId u, a taskId can only be in the range
724 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
725 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
726 int candidateTaskId = currentTaskId;
727 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
728 INVALID_STACK_ID) != null) {
729 candidateTaskId++;
730 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
731 // Wrap around as there will be smaller task ids that are available now.
732 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700733 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800734 if (candidateTaskId == currentTaskId) {
735 // Something wrong!
736 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
737 throw new IllegalStateException("Cannot get an available task id."
738 + " Reached limit of " + MAX_TASK_IDS_PER_USER
739 + " running tasks per user.");
740 }
741 }
742 mCurTaskIdForUser.put(userId, candidateTaskId);
743 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 }
745
Craig Mautnerde4ef022013-04-07 19:01:33 -0700746 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800747 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700748 if (stack == null) {
749 return null;
750 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700751 ActivityRecord resumedActivity = stack.mResumedActivity;
752 if (resumedActivity == null || resumedActivity.app == null) {
753 resumedActivity = stack.mPausingActivity;
754 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700755 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 }
757 }
758 return resumedActivity;
759 }
760
Dianne Hackbornff072722014-09-24 10:56:28 -0700761 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700762 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
765 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
767 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700768 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 continue;
770 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700771 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800772 if (hr != null) {
773 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
774 && processName.equals(hr.processName)) {
775 try {
George Mount2c92c972014-03-20 09:38:23 -0700776 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 didSomething = true;
778 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700779 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 Slog.w(TAG, "Exception in new application when starting activity "
781 + hr.intent.getComponent().flattenToShortString(), e);
782 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700783 }
Craig Mautner20e72272013-04-01 13:45:53 -0700784 }
Craig Mautner20e72272013-04-01 13:45:53 -0700785 }
786 }
787 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700788 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700789 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700790 }
Craig Mautner20e72272013-04-01 13:45:53 -0700791 return didSomething;
792 }
793
794 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
796 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
798 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700799 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800800 continue;
801 }
802 final ActivityRecord resumedActivity = stack.mResumedActivity;
803 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700804 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800805 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 return false;
807 }
Craig Mautner20e72272013-04-01 13:45:53 -0700808 }
809 }
810 return true;
811 }
812
Craig Mautnerde4ef022013-04-07 19:01:33 -0700813 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800814 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
815 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
817 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700818 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800819 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700820 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 return false;
822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700823 }
824 }
825 }
826 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700827 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
829 mLastFocusedStack + " to=" + mFocusedStack);
830 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700831 return true;
832 }
833
834 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800835 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800836 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
837 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
839 final ActivityStack stack = stacks.get(stackNdx);
840 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800841 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700842 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800843 return false;
844 }
845 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800846 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700847 }
848 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800849 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700850 }
851
Craig Mautner2acc3892013-09-23 10:28:14 -0700852 /**
853 * Pause all activities in either all of the stacks or just the back stacks.
854 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700855 * @return true if any activity was paused as a result of this call.
856 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700857 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700858 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800859 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
862 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700863 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700864 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700866 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
867 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700869 }
870 }
871 return someActivityPaused;
872 }
873
Craig Mautnerde4ef022013-04-07 19:01:33 -0700874 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700875 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800876 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
877 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
879 final ActivityStack stack = stacks.get(stackNdx);
880 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700881 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700883 Slog.d(TAG_STATES,
884 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 pausing = false;
886 } else {
887 return false;
888 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700889 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700890 }
891 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700892 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700893 }
894
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700895 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
896 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500897 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
899 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
900 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
901 final ActivityStack stack = stacks.get(stackNdx);
902 if (stack.mResumedActivity != null &&
903 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700904 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800905 }
906 }
907 }
908 }
909
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700910 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700911 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700912 }
913
914 void sendWaitingVisibleReportLocked(ActivityRecord r) {
915 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700916 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700917 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700918 if (w.who == null) {
919 changed = true;
920 w.timeout = false;
921 if (r != null) {
922 w.who = new ComponentName(r.info.packageName, r.info.name);
923 }
924 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
925 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700926 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700927 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700928 if (changed) {
929 mService.notifyAll();
930 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700931 }
932
933 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
934 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700935 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700937 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (w.who == null) {
939 changed = true;
940 w.timeout = timeout;
941 if (r != null) {
942 w.who = new ComponentName(r.info.packageName, r.info.name);
943 }
944 w.thisTime = thisTime;
945 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700948 if (changed) {
949 mService.notifyAll();
950 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700951 }
952
Craig Mautner29219d92013-04-16 20:19:12 -0700953 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800954 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700955 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700956 if (r != null) {
957 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700958 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700959
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800961 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
963 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800964 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700965 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700966 if (r != null) {
967 return r;
968 }
969 }
970 }
971 return null;
972 }
973
Dianne Hackborn09233282014-04-30 11:33:59 -0700974 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700975 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
977 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800978 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
982 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700983 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700985 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700986 }
987 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700988
989 // The lists are already sorted from most recent to oldest. Just pull the most recent off
990 // each list and add it to list. Stop when all lists are empty or maxNum reached.
991 while (maxNum > 0) {
992 long mostRecentActiveTime = Long.MIN_VALUE;
993 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 final int numTaskLists = runningTaskLists.size();
995 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
996 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700997 if (!stackTaskList.isEmpty()) {
998 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
999 if (lastActiveTime > mostRecentActiveTime) {
1000 mostRecentActiveTime = lastActiveTime;
1001 selectedStackList = stackTaskList;
1002 }
1003 }
1004 }
1005 if (selectedStackList != null) {
1006 list.add(selectedStackList.remove(0));
1007 --maxNum;
1008 } else {
1009 break;
1010 }
1011 }
Craig Mautner20e72272013-04-01 13:45:53 -07001012 }
1013
Todd Kennedy7440f172015-12-09 14:31:22 -08001014 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1015 ProfilerInfo profilerInfo) {
1016 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 if (aInfo != null) {
1018 // Store the found target back into the intent, because now that
1019 // we have it we never want to do this again. For example, if the
1020 // user navigates back to this point in the history, we should
1021 // always restart the exact same activity.
1022 intent.setComponent(new ComponentName(
1023 aInfo.applicationInfo.packageName, aInfo.name));
1024
1025 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001026 if (!aInfo.processName.equals("system")) {
1027 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 mService.setDebugApp(aInfo.processName, true, false);
1029 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001030
Man Caocfa78b22015-06-11 20:14:34 -07001031 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1032 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1033 }
1034
1035 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001036 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 }
1038 }
1039 }
1040 return aInfo;
1041 }
1042
Todd Kennedy7440f172015-12-09 14:31:22 -08001043 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1044 try {
1045 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1046 PackageManager.MATCH_DEFAULT_ONLY
1047 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1048 } catch (RemoteException e) {
1049 }
1050 return null;
1051 }
1052
1053 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1054 ProfilerInfo profilerInfo, int userId) {
1055 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1056 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1057 }
1058
Craig Mautner2420ead2013-04-01 17:13:20 -07001059 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001060 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001061 throws RemoteException {
1062
Craig Mautner2568c3a2015-03-26 14:22:34 -07001063 if (andResume) {
1064 r.startFreezingScreenLocked(app, 0);
1065 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001066
Craig Mautner2568c3a2015-03-26 14:22:34 -07001067 // schedule launch ticks to collect information about slow apps.
1068 r.startLaunchTickingLocked();
1069 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001070
1071 // Have the window manager re-evaluate the orientation of
1072 // the screen based on the new activity order. Note that
1073 // as a result of this, it can call back into the activity
1074 // manager with a new orientation. We don't care about that,
1075 // because the activity is not currently running so we are
1076 // just restarting it anyway.
1077 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001079 mService.mConfiguration,
1080 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001081 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001082 }
1083
1084 r.app = app;
1085 app.waitingToKill = null;
1086 r.launchCount++;
1087 r.lastLaunchTime = SystemClock.uptimeMillis();
1088
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001089 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001090
1091 int idx = app.activities.indexOf(r);
1092 if (idx < 0) {
1093 app.activities.add(r);
1094 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001095 mService.updateLruProcessLocked(app, true, null);
1096 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001097
Craig Mautner15df08a2015-04-01 12:17:18 -07001098 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001099 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1100 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001101 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001102 }
1103
1104 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001105 try {
1106 if (app.thread == null) {
1107 throw new RemoteException();
1108 }
1109 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001110 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001111 if (andResume) {
1112 results = r.results;
1113 newIntents = r.newIntents;
1114 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001115 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1116 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1117 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001118 if (andResume) {
1119 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1120 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001121 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001122 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001123 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001124 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001125 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001126 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001127 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001128 r.sleeping = false;
1129 r.forceNewConfig = false;
1130 mService.showAskCompatModeDialogLocked(r);
1131 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001132 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1134 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1135 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001136 final String profileFile = mService.mProfileFile;
1137 if (profileFile != null) {
1138 ParcelFileDescriptor profileFd = mService.mProfileFd;
1139 if (profileFd != null) {
1140 try {
1141 profileFd = profileFd.dup();
1142 } catch (IOException e) {
1143 if (profileFd != null) {
1144 try {
1145 profileFd.close();
1146 } catch (IOException o) {
1147 }
1148 profileFd = null;
1149 }
1150 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001151 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001152
1153 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1154 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 }
1156 }
1157 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001158
Craig Mautner2568c3a2015-03-26 14:22:34 -07001159 if (andResume) {
1160 app.hasShownUi = true;
1161 app.pendingUiClean = true;
1162 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001163 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001165 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001166 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001167 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001168 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001169
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001170 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001171 // This may be a heavy-weight process! Note that the package
1172 // manager will ensure that only activity can run in the main
1173 // process of the .apk, which is the only thing that will be
1174 // considered heavy-weight.
1175 if (app.processName.equals(app.info.packageName)) {
1176 if (mService.mHeavyWeightProcess != null
1177 && mService.mHeavyWeightProcess != app) {
1178 Slog.w(TAG, "Starting new heavy weight process " + app
1179 + " when already running "
1180 + mService.mHeavyWeightProcess);
1181 }
1182 mService.mHeavyWeightProcess = app;
1183 Message msg = mService.mHandler.obtainMessage(
1184 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1185 msg.obj = r;
1186 mService.mHandler.sendMessage(msg);
1187 }
1188 }
1189
1190 } catch (RemoteException e) {
1191 if (r.launchFailed) {
1192 // This is the second time we failed -- finish activity
1193 // and give up.
1194 Slog.e(TAG, "Second failure launching "
1195 + r.intent.getComponent().flattenToShortString()
1196 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001197 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1199 "2nd-crash", false);
1200 return false;
1201 }
1202
1203 // This is the first time we failed -- restart process and
1204 // retry.
1205 app.activities.remove(r);
1206 throw e;
1207 }
1208
1209 r.launchFailed = false;
1210 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001211 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001212 }
1213
1214 if (andResume) {
1215 // As part of the process of launching, ActivityThread also performs
1216 // a resume.
1217 stack.minimalResumeActivityLocked(r);
1218 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001219 // This activity is not starting in the resumed state... which should look like we asked
1220 // it to pause+stop (but remain visible), and it has done so and reported back the
1221 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001222 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001223 "Moving to PAUSED: " + r + " (starting in paused state)");
1224 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 }
1226
1227 // Launch the new version setup screen if needed. We do this -after-
1228 // launching the initial activity (that is, home), so that it can have
1229 // a chance to initialize itself while in the background, making the
1230 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001231 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 mService.startSetupActivityLocked();
1233 }
1234
Dianne Hackborn465fa392014-09-14 14:21:18 -07001235 // Update any services we are bound to that might care about whether
1236 // their client may have activities.
1237 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1238
Craig Mautner2420ead2013-04-01 17:13:20 -07001239 return true;
1240 }
1241
Craig Mautnere79d42682013-04-01 19:01:53 -07001242 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001243 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001244 // Is this activity's application already running?
1245 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001246 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001247
1248 r.task.stack.setLaunchTime(r);
1249
1250 if (app != null && app.thread != null) {
1251 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001252 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1253 || !"android".equals(r.info.packageName)) {
1254 // Don't add this if it is a platform component that is marked
1255 // to run in multiple processes, because this is actually
1256 // part of the framework so doesn't make sense to track as a
1257 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001258 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1259 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001260 }
George Mount2c92c972014-03-20 09:38:23 -07001261 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001262 return;
1263 } catch (RemoteException e) {
1264 Slog.w(TAG, "Exception when starting activity "
1265 + r.intent.getComponent().flattenToShortString(), e);
1266 }
1267
1268 // If a dead object exception was thrown -- fall through to
1269 // restart the application.
1270 }
1271
1272 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001273 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001274 }
1275
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001276 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001277 String resultWho, int requestCode, int callingPid, int callingUid,
1278 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1279 ActivityRecord resultRecord, ActivityStack resultStack) {
1280 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1281 callingUid);
1282 if (startAnyPerm == PERMISSION_GRANTED) {
1283 return true;
1284 }
1285 final int componentRestriction = getComponentRestrictionForCallingPackage(
1286 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1287 final int actionRestriction = getActionRestrictionForCallingPackage(
1288 intent.getAction(), callingPackage, callingPid, callingUid);
1289 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1290 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1291 if (resultRecord != null) {
1292 resultStack.sendActivityResultLocked(-1,
1293 resultRecord, resultWho, requestCode,
1294 Activity.RESULT_CANCELED, null);
1295 }
1296 final String msg;
1297 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1298 msg = "Permission Denial: starting " + intent.toString()
1299 + " from " + callerApp + " (pid=" + callingPid
1300 + ", uid=" + callingUid + ")" + " with revoked permission "
1301 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1302 } else if (!aInfo.exported) {
1303 msg = "Permission Denial: starting " + intent.toString()
1304 + " from " + callerApp + " (pid=" + callingPid
1305 + ", uid=" + callingUid + ")"
1306 + " not exported from uid " + aInfo.applicationInfo.uid;
1307 } else {
1308 msg = "Permission Denial: starting " + intent.toString()
1309 + " from " + callerApp + " (pid=" + callingPid
1310 + ", uid=" + callingUid + ")"
1311 + " requires " + aInfo.permission;
1312 }
1313 Slog.w(TAG, msg);
1314 throw new SecurityException(msg);
1315 }
1316
1317 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1318 final String message = "Appop Denial: starting " + intent.toString()
1319 + " from " + callerApp + " (pid=" + callingPid
1320 + ", uid=" + callingUid + ")"
1321 + " requires " + AppOpsManager.permissionToOp(
1322 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1323 Slog.w(TAG, message);
1324 return false;
1325 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1326 final String message = "Appop Denial: starting " + intent.toString()
1327 + " from " + callerApp + " (pid=" + callingPid
1328 + ", uid=" + callingUid + ")"
1329 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1330 Slog.w(TAG, message);
1331 return false;
1332 }
1333 return true;
1334 }
1335
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001336 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001337 final long identity = Binder.clearCallingIdentity();
1338 try {
1339 return UserManager.get(mService.mContext).getUserInfo(userId);
1340 } finally {
1341 Binder.restoreCallingIdentity(identity);
1342 }
1343 }
1344
Svet Ganov99b60432015-06-27 13:15:22 -07001345 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001346 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001347 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1348 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001349 == PackageManager.PERMISSION_DENIED) {
1350 return ACTIVITY_RESTRICTION_PERMISSION;
1351 }
1352
Christopher Tateff7add02015-08-17 10:23:22 -07001353 if (activityInfo.permission == null) {
1354 return ACTIVITY_RESTRICTION_NONE;
1355 }
1356
Svet Ganov99b60432015-06-27 13:15:22 -07001357 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1358 if (opCode == AppOpsManager.OP_NONE) {
1359 return ACTIVITY_RESTRICTION_NONE;
1360 }
1361
1362 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1363 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001364 if (!ignoreTargetSecurity) {
1365 return ACTIVITY_RESTRICTION_APPOP;
1366 }
Svet Ganov99b60432015-06-27 13:15:22 -07001367 }
1368
1369 return ACTIVITY_RESTRICTION_NONE;
1370 }
1371
Svetoslav7008b512015-06-24 18:47:07 -07001372 private int getActionRestrictionForCallingPackage(String action,
1373 String callingPackage, int callingPid, int callingUid) {
1374 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001375 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001376 }
1377
1378 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1379 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001380 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001381 }
1382
1383 final PackageInfo packageInfo;
1384 try {
1385 packageInfo = mService.mContext.getPackageManager()
1386 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1387 } catch (PackageManager.NameNotFoundException e) {
1388 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001389 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001390 }
1391
1392 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001393 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001394 }
1395
1396 if (mService.checkPermission(permission, callingPid, callingUid) ==
1397 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001398 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001399 }
1400
1401 final int opCode = AppOpsManager.permissionToOpCode(permission);
1402 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001403 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001404 }
1405
1406 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1407 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001408 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001409 }
1410
Svet Ganov99b60432015-06-27 13:15:22 -07001411 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001412 }
1413
Wale Ogunwaled046a012015-12-24 13:05:59 -08001414 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001415 if (r == null) {
1416 // Not sure what you are trying to do, but it is not going to work...
1417 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001418 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001419 final TaskRecord task = r.task;
1420 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001421 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001422 return false;
1423 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001424 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001425 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001426 }
1427
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001428 void setLaunchSource(int uid) {
1429 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1430 }
1431
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001432 void acquireLaunchWakelock() {
1433 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1434 throw new IllegalStateException("Calling must be system uid");
1435 }
1436 mLaunchingActivity.acquire();
1437 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1438 // To be safe, don't allow the wake lock to be held for too long.
1439 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1440 }
1441 }
1442
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001443 /**
1444 * Called when the frontmost task is idle.
1445 * @return the state of mService.mBooting before this was called.
1446 */
1447 private boolean checkFinishBootingLocked() {
1448 final boolean booting = mService.mBooting;
1449 boolean enableScreen = false;
1450 mService.mBooting = false;
1451 if (!mService.mBooted) {
1452 mService.mBooted = true;
1453 enableScreen = true;
1454 }
1455 if (booting || enableScreen) {
1456 mService.postFinishBooting(booting, enableScreen);
1457 }
1458 return booting;
1459 }
1460
Craig Mautnerf3333272013-04-22 10:55:53 -07001461 // Checked.
1462 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1463 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001464 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001465
Craig Mautnerf3333272013-04-22 10:55:53 -07001466 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001467 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001468 int NS = 0;
1469 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001470 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001471 boolean activityRemoved = false;
1472
Wale Ogunwale7d701172015-03-11 15:36:30 -07001473 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001474 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001475 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1476 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001477 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1478 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001479 if (fromTimeout) {
1480 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001481 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001482
1483 // This is a hack to semi-deal with a race condition
1484 // in the client where it can be constructed with a
1485 // newer configuration from when we asked it to launch.
1486 // We'll update with whatever configuration it now says
1487 // it used to launch.
1488 if (config != null) {
1489 r.configuration = config;
1490 }
1491
1492 // We are now idle. If someone is waiting for a thumbnail from
1493 // us, we can now deliver.
1494 r.idle = true;
1495
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001496 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001497 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001498 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001499 }
1500 }
1501
1502 if (allResumedActivitiesIdle()) {
1503 if (r != null) {
1504 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001505 }
1506
1507 if (mLaunchingActivity.isHeld()) {
1508 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1509 if (VALIDATE_WAKE_LOCK_CALLER &&
1510 Binder.getCallingUid() != Process.myUid()) {
1511 throw new IllegalStateException("Calling must be system uid");
1512 }
1513 mLaunchingActivity.release();
1514 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001515 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 }
1517
1518 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001519 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001521 if ((NF = mFinishingActivities.size()) > 0) {
1522 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001523 mFinishingActivities.clear();
1524 }
1525
Craig Mautnerf3333272013-04-22 10:55:53 -07001526 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001527 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001528 mStartingUsers.clear();
1529 }
1530
Craig Mautnerf3333272013-04-22 10:55:53 -07001531 // Stop any activities that are scheduled to do so but have been
1532 // waiting for the next one to start.
1533 for (int i = 0; i < NS; i++) {
1534 r = stops.get(i);
1535 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001536 if (stack != null) {
1537 if (r.finishing) {
1538 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1539 } else {
1540 stack.stopActivityLocked(r);
1541 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001542 }
1543 }
1544
1545 // Finish any activities that are scheduled to do so but have been
1546 // waiting for the next one to start.
1547 for (int i = 0; i < NF; i++) {
1548 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001549 final ActivityStack stack = r.task.stack;
1550 if (stack != null) {
1551 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1552 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001553 }
1554
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001555 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001556 // Complete user switch
1557 if (startingUsers != null) {
1558 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001559 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001560 }
1561 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001562 }
1563
1564 mService.trimApplications();
1565 //dump();
1566 //mWindowManager.dump();
1567
Craig Mautnerf3333272013-04-22 10:55:53 -07001568 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001569 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 }
1571
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001572 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001573 }
1574
Craig Mautner8e569572013-10-11 17:36:59 -07001575 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001576 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001577 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1578 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001579 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1580 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1581 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001582 }
Craig Mautner19091252013-10-05 00:03:53 -07001583 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001584 }
1585
1586 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001587 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1588 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001589 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1590 stacks.get(stackNdx).closeSystemDialogsLocked();
1591 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001592 }
1593 }
1594
Craig Mautner93529a42013-10-04 15:03:13 -07001595 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001596 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001597 }
1598
Craig Mautner8d341ef2013-03-26 09:03:27 -07001599 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001600 * Update the last used stack id for non-current user (current user's last
1601 * used stack is the focused stack)
1602 */
1603 void updateUserStackLocked(int userId, ActivityStack stack) {
1604 if (userId != mCurrentUser) {
1605 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1606 }
1607 }
1608
1609 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001610 * @return true if some activity was finished (or would have finished if doit were true).
1611 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001612 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1613 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001614 boolean didSomething = false;
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;
louis_chang8f0555a2015-10-27 10:45:53 +08001617 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001618 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001619 if (stack.finishDisabledPackageActivitiesLocked(
1620 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001621 didSomething = true;
1622 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001623 }
1624 }
1625 return didSomething;
1626 }
1627
Dianne Hackborna413dc02013-07-12 12:02:55 -07001628 void updatePreviousProcessLocked(ActivityRecord r) {
1629 // Now that this process has stopped, we may want to consider
1630 // it to be the previous app to try to keep around in case
1631 // the user wants to return to it.
1632
1633 // First, found out what is currently the foreground app, so that
1634 // we don't blow away the previous app if this activity is being
1635 // hosted by the process that is actually still the foreground.
1636 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1638 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1640 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001641 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001642 if (stack.mResumedActivity != null) {
1643 fgApp = stack.mResumedActivity.app;
1644 } else if (stack.mPausingActivity != null) {
1645 fgApp = stack.mPausingActivity.app;
1646 }
1647 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001648 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001649 }
1650 }
1651
1652 // Now set this one as the previous process, only if that really
1653 // makes sense to.
1654 if (r.app != null && fgApp != null && r.app != fgApp
1655 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001656 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001657 mService.mPreviousProcess = r.app;
1658 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1659 }
1660 }
1661
Wale Ogunwaled046a012015-12-24 13:05:59 -08001662 boolean resumeFocusedStackTopActivityLocked() {
1663 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001664 }
1665
Wale Ogunwaled046a012015-12-24 13:05:59 -08001666 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001667 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001668 if (targetStack != null && isFocusedStack(targetStack)) {
1669 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001670 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001671 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1672 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001673 }
1674
Adrian Roos20d7df32016-01-12 18:59:43 +01001675 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1676 TaskRecord finishedTask = null;
1677 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001678 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1679 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001680 final int numStacks = stacks.size();
1681 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1682 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001683 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1684 if (stack == focusedStack || finishedTask == null) {
1685 finishedTask = t;
1686 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001687 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001688 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001689 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001690 }
1691
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001692 void finishVoiceTask(IVoiceInteractionSession session) {
1693 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1694 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1695 final int numStacks = stacks.size();
1696 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1697 final ActivityStack stack = stacks.get(stackNdx);
1698 stack.finishVoiceTask(session);
1699 }
1700 }
1701 }
1702
Chong Zhang280d3322015-11-03 17:27:26 -08001703 void findTaskToMoveToFrontLocked(
1704 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001705 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1706 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001707 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001708 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1709 // Caller wants the home activity moved with it. To accomplish this,
1710 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001711 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001712 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001713 if (task.stack == null) {
1714 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1715 + task + " to front. Stack is null");
1716 return;
1717 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001718
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001719 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001720 int stackId = options.getLaunchStackId();
1721 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001722 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001723 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001724 if (stackId == INVALID_STACK_ID) {
1725 stackId = task.getLaunchStackId();
1726 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001727 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001728 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1729 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1730 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001731 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1732 // still need moveTaskToFrontLocked() below for any transition settings.
1733 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001734 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1735 resizeStackLocked(stackId, bounds,
1736 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1737 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1738 } else {
1739 // WM resizeTask must be done after the task is moved to the correct stack,
1740 // because Task's setBounds() also updates dim layer's bounds, but that has
1741 // dependency on the stack.
1742 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1743 false /* relayout */, false /* forced */);
1744 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001745 }
1746 }
1747
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001748 final ActivityRecord r = task.getTopActivity();
1749 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1750 r == null ? null : r.appTimeTracker, reason);
1751
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001752 if (DEBUG_STACK) Slog.d(TAG_STACK,
1753 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001754 }
1755
Wale Ogunwale854809c2015-12-27 16:18:19 -08001756 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001757 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001758 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001759 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001760 return false;
1761 }
1762 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1763 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001764 }
1765
Craig Mautner967212c2013-04-13 21:10:58 -07001766 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001767 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001768 }
1769
1770 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001771 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1772 if (activityContainer != null) {
1773 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001774 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001775 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001776 return null;
1777 }
1778 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001779 }
1780
Craig Mautner967212c2013-04-13 21:10:58 -07001781 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001782 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001783 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1784 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001785 }
1786 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001787 }
1788
Craig Mautner4a1cb222013-12-04 16:14:06 -08001789 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001790 ActivityRecord homeActivity = getHomeActivity();
1791 if (homeActivity != null) {
1792 return homeActivity.appToken;
1793 }
1794 return null;
1795 }
1796
1797 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001798 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001799 }
1800
1801 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001802 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1803 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1804 final TaskRecord task = tasks.get(taskNdx);
1805 if (task.isHomeTask()) {
1806 final ArrayList<ActivityRecord> activities = task.mActivities;
1807 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1808 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001809 if (r.isHomeActivity()
1810 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001811 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001812 }
1813 }
1814 }
1815 }
1816 return null;
1817 }
1818
Chong Zhangb15758a2015-11-17 12:12:03 -08001819 /**
1820 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1821 * the docked stack itself, or if it's side-by-side to the docked stack.
1822 */
1823 boolean isStackDockedInEffect(int stackId) {
1824 return stackId == DOCKED_STACK_ID ||
1825 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1826 }
1827
Todd Kennedyca4d8422015-01-15 15:19:22 -08001828 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001829 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001830 ActivityContainer activityContainer =
1831 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001832 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001833 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1834 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001835 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001836 return activityContainer;
1837 }
1838
Craig Mautner34b73df2014-01-12 21:11:08 -08001839 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001840 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1841 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1842 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001843 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1844 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001845 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001846 }
1847 }
1848
Craig Mautner95da1082014-02-24 17:54:35 -08001849 void deleteActivityContainer(IActivityContainer container) {
1850 ActivityContainer activityContainer = (ActivityContainer)container;
1851 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001852 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1853 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001854 final int stackId = activityContainer.mStackId;
1855 mActivityContainers.remove(stackId);
1856 mWindowManager.removeStack(stackId);
1857 }
1858 }
1859
Jorim Jaggidc249c42015-12-15 14:57:31 -08001860 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1861 boolean preserveWindows, boolean allowResizeInDockedMode) {
1862 if (stackId == DOCKED_STACK_ID) {
1863 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1864 preserveWindows);
1865 return;
1866 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001867 final ActivityStack stack = getStack(stackId);
1868 if (stack == null) {
1869 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1870 return;
1871 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001872
Jorim Jaggidc249c42015-12-15 14:57:31 -08001873 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001874 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1875 // stack size changing so things don't get out of sync.
1876 return;
1877 }
1878
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001879 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001880 mWindowManager.deferSurfaceLayout();
1881 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001882 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1883 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001884 } finally {
1885 mWindowManager.continueSurfaceLayout();
1886 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001887 }
1888 }
1889
Jorim Jaggidc249c42015-12-15 14:57:31 -08001890 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1891 Rect tempTaskInsetBounds) {
1892 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1893 // The bounds passed in corresponds to the fullscreen bounds which we normally
1894 // represent with null. Go ahead and set it to null so that all tasks configuration
1895 // can have the right fullscreen state.
1896 bounds = null;
1897 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001898 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001899
1900 mTmpBounds.clear();
1901 mTmpConfigs.clear();
1902 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001903 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001904 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001905 final TaskRecord task = tasks.get(i);
1906 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001907 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1908 // For freeform stack we don't adjust the size of the tasks to match that
1909 // of the stack, but we do try to make sure the tasks are still contained
1910 // with the bounds of the stack.
1911 tempRect2.set(task.mBounds);
1912 fitWithinBounds(tempRect2, bounds);
1913 task.updateOverrideConfiguration(tempRect2);
1914 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001915 task.updateOverrideConfiguration(
1916 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001917 }
1918 }
1919
1920 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1921 mTmpBounds.put(task.taskId, task.mBounds);
1922 if (tempTaskInsetBounds != null) {
1923 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1924 }
1925 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001926
1927 // We might trigger a configuration change. Save the current task bounds for freezing.
1928 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001929 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1930 mTmpBounds, mTmpInsetBounds);
1931 stack.setBounds(bounds);
1932 }
1933
1934 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1935 boolean preserveWindows) {
1936 if (r == null) {
1937 return;
1938 }
1939 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1940 preserveWindows);
1941 // And we need to make sure at this point that all other activities
1942 // are made visible with the correct configuration.
1943 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1944 if (!updated) {
1945 resumeFocusedStackTopActivityLocked();
1946 }
1947 }
1948
1949 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1950 Rect tempDockedTaskInsetBounds,
1951 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1952 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1953 if (stack == null) {
1954 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1955 return;
1956 }
1957
1958 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1959 mWindowManager.deferSurfaceLayout();
1960 try {
1961 ActivityRecord r = stack.topRunningActivityLocked();
1962 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1963 tempDockedTaskInsetBounds);
1964
1965 if (stack.mFullscreen) {
1966 // The dock stack went fullscreen which is kinda like dismissing it.
1967 // In this case we make all other static stacks fullscreen and move all
1968 // docked stack tasks to the fullscreen stack.
1969 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1970 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1971 resizeStackLocked(i, null, null, null, preserveWindows,
1972 true /* allowResizeInDockedMode */);
1973 }
1974 }
1975
1976 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1977 final int count = tasks.size();
1978 for (int i = 0; i < count; i++) {
1979 moveTaskToStackLocked(tasks.get(i).taskId,
1980 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1981 false /* animate */);
1982 }
1983
1984 // stack shouldn't contain anymore activities, so nothing to resume.
1985 r = null;
1986 } else {
1987 // Docked stacks occupy a dedicated region on screen so the size of all other
1988 // static stacks need to be adjusted so they don't overlap with the docked stack.
1989 // We get the bounds to use from window manager which has been adjusted for any
1990 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08001991 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08001992 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001993 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1994 if (StackId.isResizeableByDockedStack(i)) {
1995 ActivityStack otherStack = getStack(i);
1996 if (otherStack != null) {
1997 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1998 tempOtherTaskInsetBounds, preserveWindows,
1999 true /* allowResizeInDockedMode */);
2000 }
2001 }
2002 }
2003 }
2004 ensureConfigurationAndResume(stack, r, preserveWindows);
2005 } finally {
2006 mWindowManager.continueSurfaceLayout();
2007 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2008 }
2009
2010 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2011 tempDockedTaskBounds != null
2012 || tempDockedTaskInsetBounds != null
2013 || tempOtherTaskBounds != null
2014 || tempOtherTaskInsetBounds != null);
2015 }
2016
Chong Zhangf596cd52016-01-05 13:42:44 -08002017 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002018 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002019 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002020 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002021 }
2022
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002023 adjustForMinimalTaskDimensions(task, bounds);
2024
Chong Zhang87b21722015-09-21 15:39:51 -07002025 // If this is a forced resize, let it go through even if the bounds is not changing,
2026 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002027 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002028 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002029 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002030 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002031 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002032 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002033
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002034 if (!mWindowManager.isValidTaskId(task.taskId)) {
2035 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002036 // All we can do for now is update the bounds so it can be used when the task is
2037 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002038 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002039 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2040 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002041 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002042 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002043 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002044 }
2045
Chong Zhang6de2ae82015-09-30 18:25:21 -07002046 // Do not move the task to another stack here.
2047 // This method assumes that the task is already placed in the right stack.
2048 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002049
Chong Zhang6de2ae82015-09-30 18:25:21 -07002050 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002051
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002052 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002053 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002054 // way and the activity was kept the way it was. If it's false, it means the activity had
2055 // to be relaunched due to configuration change.
2056 boolean kept = true;
2057 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002058 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002059 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002060 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002061 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002062 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002063 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002064 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002065 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002066 }
2067 }
2068 }
Chong Zhang87b21722015-09-21 15:39:51 -07002069 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002070
2071 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002072 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002073 }
2074
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002075 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2076 if (bounds == null) {
2077 return;
2078 }
2079 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2080 : task.mMinimalSize;
2081 final boolean adjustWidth = minimalSize > bounds.width();
2082 final boolean adjustHeight = minimalSize > bounds.height();
2083 if (!(adjustWidth || adjustHeight)) {
2084 return;
2085 }
2086 Rect taskBounds = task.mBounds;
2087 if (adjustWidth) {
2088 if (taskBounds != null && bounds.right == taskBounds.right) {
2089 bounds.left = bounds.right - minimalSize;
2090 } else {
2091 // Either left bounds match, or neither match, or the previous bounds were
2092 // fullscreen and we default to keeping left.
2093 bounds.right = bounds.left + minimalSize;
2094 }
2095 }
2096 if (adjustHeight) {
2097 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2098 bounds.top = bounds.bottom - minimalSize;
2099 } else {
2100 // Either top bounds match, or neither match, or the previous bounds were
2101 // fullscreen and we default to keeping top.
2102 bounds.bottom = bounds.top + minimalSize;
2103 }
2104 }
2105 }
2106
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002107 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002108 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2109 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002110 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002111 }
2112
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002113 ActivityContainer activityContainer = new ActivityContainer(stackId);
2114 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002115 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002116 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002117 }
2118
2119 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002120 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002121 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2122 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002123 break;
2124 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002125 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002126 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002127 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002128 }
2129
Chong Zhang5dcb2752015-08-18 13:50:26 -07002130 /**
2131 * Restores a recent task to a stack
2132 * @param task The recent task to be restored.
2133 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002134 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002135 * @return true if the task has been restored successfully.
2136 */
2137 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2138 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002139 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002140 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2141 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2142 // Put it in the fullscreen stack.
2143 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002144 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002145
Wale Ogunwale706ed792015-08-02 10:29:44 -07002146 if (task.stack != null) {
2147 // Task has already been restored once. See if we need to do anything more
2148 if (task.stack.mStackId == stackId) {
2149 // Nothing else to do since it is already restored in the right stack.
2150 return true;
2151 }
2152 // Remove current stack association, so we can re-associate the task with the
2153 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002154 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002155 }
2156
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002157 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002158 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002159
2160 if (stack == null) {
2161 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002162 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2163 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002164 return false;
2165 }
2166
Wale Ogunwale5f986092015-12-04 15:35:38 -08002167 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002168 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2169 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002170 final ArrayList<ActivityRecord> activities = task.mActivities;
2171 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002172 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002173 }
2174 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002175 }
2176
Wale Ogunwale040b4702015-08-06 18:10:50 -07002177 /**
2178 * Moves the specified task record to the input stack id.
2179 * WARNING: This method performs an unchecked/raw move of the task and
2180 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002181 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002182 * @param task Task to move.
2183 * @param stackId Id of stack to move task to.
2184 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002185 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002186 * @param reason Reason the task is been moved.
2187 * @return The stack the task was moved to.
2188 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002189 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002190 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2191 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002192 final ActivityStack prevStack = task.stack;
2193 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2194 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2195 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2196 // Whenever we are moving the top activity from the front stack we want to make sure to move
2197 // the stack to the front.
2198 final boolean wasFront = isFrontStack(prevStack)
2199 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002200
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002201 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002202
2203 if (stackId == DOCKED_STACK_ID && resizeMode == RESIZE_MODE_UNRESIZEABLE) {
2204 // We don't allow moving a unresizeable task to the docked stack since the docked
2205 // stack is used for split-screen mode and will cause things like the docked divider to
2206 // show up. We instead leave the task in its current stack or move it to the fullscreen
2207 // stack if it isn't currently in a stack.
2208 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002209 Slog.w(TAG, "Can not move unresizeable task=" + task
2210 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2211 }
2212
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002213 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2214 // if a docked stack is created below which will lead to the stack we are moving from and
2215 // its resizeable tasks being resized.
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002216 task.mResizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002217 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002218 task.mResizeMode = resizeMode;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002219 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002220 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002221
2222 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002223 // move focus to the new stack by moving the stack to the front.
2224 stack.moveToFrontAndResumeStateIfNeeded(
2225 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002226
Wale Ogunwale040b4702015-08-06 18:10:50 -07002227 return stack;
2228 }
2229
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002230 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002231 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002232 final TaskRecord task = anyTaskForIdLocked(taskId);
2233 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002234 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002235 return;
2236 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002237
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002238 if (task.stack != null && task.stack.mStackId == stackId) {
2239 // You are already in the right stack silly...
2240 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2241 return;
2242 }
2243
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002244 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002245 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002247 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002248 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002249 // We are about to relaunch the activity because its configuration changed due to
2250 // being maximized, i.e. size change. The activity will first remove the old window
2251 // and then add a new one. This call will tell window manager about this, so it can
2252 // preserve the old window until the new one is drawn. This prevents having a gap
2253 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002254 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002255 // Note here we always set the replacing window first, as the flags might be needed
2256 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002257 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002258 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002259
2260 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002261 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002262 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002263 try {
2264 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2265 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
2266 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002267
Wale Ogunwale961f4852016-02-01 20:25:54 -08002268 if (!animate) {
2269 stack.mNoAnimActivities.add(topActivity);
2270 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002271
Wale Ogunwale961f4852016-02-01 20:25:54 -08002272 // We might trigger a configuration change. Save the current task bounds for freezing.
2273 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2274
2275 // Make sure the task has the appropriate bounds/size for the stack it is in.
2276 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
2277 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2278 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2279 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
2280 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2281 RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2282 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
2283 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2284 }
2285 } finally {
2286 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002287 }
2288
2289 if (mightReplaceWindow) {
2290 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2291 // window), we need to clear the replace window settings. Otherwise, we schedule a
2292 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002293 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002294 }
2295
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002296 // The task might have already been running and its visibility needs to be synchronized with
2297 // the visibility of the stack / windows.
Jorim Jaggie9098022016-01-27 19:29:40 -08002298 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002299 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002300
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002301 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002302 }
2303
Wale Ogunwale079a0042015-10-24 11:44:07 -07002304 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2305 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2306 if (stack == null) {
2307 throw new IllegalArgumentException(
2308 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2309 }
2310
2311 final ActivityRecord r = stack.topRunningActivityLocked();
2312 if (r == null) {
2313 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2314 + " in stack=" + stack);
2315 return false;
2316 }
2317
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002318 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002319 Slog.w(TAG,
2320 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002321 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002322 return false;
2323 }
2324
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002325 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", null);
2326 mWindowManager.animateResizePinnedStack(bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002327 return true;
2328 }
2329
2330 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002331 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002332 if (task.mActivities.size() == 1) {
2333 // There is only one activity in the task. So, we can just move the task over to the
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002334 // stack without re-parenting the activity in a different task.
2335 moveTaskToStackLocked(
2336 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002337 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002338 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2339 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002340 }
2341
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002342 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002343 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2344 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002345 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002346
2347 // The task might have already been running and its visibility needs to be synchronized with
2348 // the visibility of the stack / windows.
2349 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002350 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002351
2352 if (stackId == PINNED_STACK_ID) {
2353 mService.notifyActivityPinnedLocked();
2354 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002355 }
2356
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002357 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2358 final TaskRecord task = anyTaskForIdLocked(taskId);
2359 if (task == null) {
2360 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2361 return;
2362 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002363 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2364
2365 task.updateOverrideConfigurationForStack(stack);
2366
2367 mWindowManager.positionTaskInStack(
2368 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002369 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002370 // The task might have already been running and its visibility needs to be synchronized with
2371 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002372 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002373 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002374 }
2375
Craig Mautnerac6f8432013-07-17 13:24:59 -07002376 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002377 mTmpFindTaskResult.r = null;
2378 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002379 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002380 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2381 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002382 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2383 final ActivityStack stack = stacks.get(stackNdx);
2384 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002385 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002386 continue;
2387 }
2388 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002389 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2390 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002391 continue;
2392 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002393 stack.findTaskLocked(r, mTmpFindTaskResult);
2394 // It is possible to have task in multiple stacks with the same root affinity.
2395 // If the match we found was based on root affinity we keep on looking to see if
2396 // there is a better match in another stack. We eventually return the match based
2397 // on root affinity if we don't find a better match.
2398 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2399 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002400 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 }
2402 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002403 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2404 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002405 }
2406
2407 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002410 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2411 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2412 if (ar != null) {
2413 return ar;
2414 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002415 }
2416 }
2417 return null;
2418 }
2419
Craig Mautner8d341ef2013-03-26 09:03:27 -07002420 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002421 scheduleSleepTimeout();
2422 if (!mGoingToSleep.isHeld()) {
2423 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002424 if (mLaunchingActivity.isHeld()) {
2425 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2426 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002427 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002428 mLaunchingActivity.release();
2429 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002430 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002431 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002432 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002433 }
2434
2435 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002436 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002437
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002438 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002439 final long endTime = System.currentTimeMillis() + timeout;
2440 while (true) {
2441 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002444 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2445 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2446 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002447 }
2448 if (cantShutdown) {
2449 long timeRemaining = endTime - System.currentTimeMillis();
2450 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002451 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002452 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002453 } catch (InterruptedException e) {
2454 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002455 } else {
2456 Slog.w(TAG, "Activity manager shutdown timed out");
2457 timedout = true;
2458 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002459 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002460 } else {
2461 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002462 }
2463 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002464
2465 // Force checkReadyForSleep to complete.
2466 mSleepTimeout = true;
2467 checkReadyForSleepLocked();
2468
Craig Mautner8d341ef2013-03-26 09:03:27 -07002469 return timedout;
2470 }
2471
2472 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002473 removeSleepTimeouts();
2474 if (mGoingToSleep.isHeld()) {
2475 mGoingToSleep.release();
2476 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002477 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2478 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002479 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2480 final ActivityStack stack = stacks.get(stackNdx);
2481 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002482 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002483 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002484 }
Craig Mautner5314a402013-09-26 12:40:16 -07002485 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002487 mGoingToSleepActivities.clear();
2488 }
2489
2490 void activitySleptLocked(ActivityRecord r) {
2491 mGoingToSleepActivities.remove(r);
2492 checkReadyForSleepLocked();
2493 }
2494
2495 void checkReadyForSleepLocked() {
2496 if (!mService.isSleepingOrShuttingDown()) {
2497 // Do not care.
2498 return;
2499 }
2500
2501 if (!mSleepTimeout) {
2502 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002503 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2504 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002505 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2506 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2507 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002508 }
2509
2510 if (mStoppingActivities.size() > 0) {
2511 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002512 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002513 + mStoppingActivities.size() + " activities");
2514 scheduleIdleLocked();
2515 dontSleep = true;
2516 }
2517
2518 if (mGoingToSleepActivities.size() > 0) {
2519 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002520 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002521 + mGoingToSleepActivities.size() + " activities");
2522 dontSleep = true;
2523 }
2524
2525 if (dontSleep) {
2526 return;
2527 }
2528 }
2529
Craig Mautnere0a38842013-12-16 16:14:02 -08002530 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2531 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2533 stacks.get(stackNdx).goToSleep();
2534 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002535 }
2536
2537 removeSleepTimeouts();
2538
2539 if (mGoingToSleep.isHeld()) {
2540 mGoingToSleep.release();
2541 }
2542 if (mService.mShuttingDown) {
2543 mService.notifyAll();
2544 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002545 }
2546
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002547 boolean reportResumedActivityLocked(ActivityRecord r) {
2548 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002549 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002550 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002551 }
2552 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002553 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002554 mWindowManager.executeAppTransition();
2555 return true;
2556 }
2557 return false;
2558 }
2559
Craig Mautner8d341ef2013-03-26 09:03:27 -07002560 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002561 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2562 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002563 int stackNdx = stacks.size() - 1;
2564 while (stackNdx >= 0) {
2565 stacks.get(stackNdx).handleAppCrashLocked(app);
2566 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002567 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 }
2569 }
2570
Jose Lima4b6c6692014-08-12 17:41:12 -07002571 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002572 final ActivityStack stack = r.task.stack;
2573 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002574 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2575 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002576 return false;
2577 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002578 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002579 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2580 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002581
2582 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002583 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002584 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002585 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002586 return true;
2587 }
2588
2589 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002590 if (visible && top.fullscreen) {
2591 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002592 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2593 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2594 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2595 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002596 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002597 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2598 // Only the activity set as currently visible behind should actively reset its
2599 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002600 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2601 "requestVisibleBehind: returning visible=" + visible
2602 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2603 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002604 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002605 }
2606
Jose Lima4b6c6692014-08-12 17:41:12 -07002607 stack.setVisibleBehindActivity(visible ? r : null);
2608 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002609 // If there is a translucent home activity, we need to force it stop being translucent,
2610 // because we can't depend on the application to necessarily perform that operation.
2611 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002612 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002613 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002614 mService.convertFromTranslucent(next.appToken);
2615 }
2616 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002617 if (top.app != null && top.app.thread != null) {
2618 // Notify the top app of the change.
2619 try {
2620 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2621 } catch (RemoteException e) {
2622 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002623 }
2624 return true;
2625 }
2626
Craig Mautnerbb742462014-07-07 15:28:55 -07002627 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2628 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2629 r.mLaunchTaskBehind = false;
2630 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002631 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002632 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002633 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002634 mWindowManager.setAppVisibility(r.appToken, false);
2635 }
2636
2637 void scheduleLaunchTaskBehindComplete(IBinder token) {
2638 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2639 }
2640
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002641 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2642 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002643 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 final int topStackNdx = stacks.size() - 1;
2647 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2648 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002649 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002651 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 }
2653
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002654 void invalidateTaskLayers() {
2655 mTaskLayersChanged = true;
2656 }
2657
2658 void rankTaskLayersIfNeeded() {
2659 if (!mTaskLayersChanged) {
2660 return;
2661 }
2662 mTaskLayersChanged = false;
2663 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2664 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2665 int baseLayer = 0;
2666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2667 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2668 }
2669 }
2670 }
2671
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002672 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2674 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2675 final int topStackNdx = stacks.size() - 1;
2676 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2677 final ActivityStack stack = stacks.get(stackNdx);
2678 stack.clearOtherAppTimeTrackers(except);
2679 }
2680 }
2681 }
2682
Craig Mautner8d341ef2013-03-26 09:03:27 -07002683 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2685 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002686 final int numStacks = stacks.size();
2687 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2688 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002689 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002690 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 }
2692 }
2693
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002694 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2695 // Examine all activities currently running in the process.
2696 TaskRecord firstTask = null;
2697 // Tasks is non-null only if two or more tasks are found.
2698 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002699 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2700 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002701 ActivityRecord r = app.activities.get(i);
2702 // First, if we find an activity that is in the process of being destroyed,
2703 // then we just aren't going to do anything for now; we want things to settle
2704 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002705 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002706 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002707 return;
2708 }
2709 // Don't consider any activies that are currently not in a state where they
2710 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002711 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2712 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002713 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002714 continue;
2715 }
2716 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002717 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002718 + " from " + r);
2719 if (firstTask == null) {
2720 firstTask = r.task;
2721 } else if (firstTask != r.task) {
2722 if (tasks == null) {
2723 tasks = new ArraySet<>();
2724 tasks.add(firstTask);
2725 }
2726 tasks.add(r.task);
2727 }
2728 }
2729 }
2730 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002731 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002732 return;
2733 }
2734 // If we have activities in multiple tasks that are in a position to be destroyed,
2735 // let's iterate through the tasks and release the oldest one.
2736 final int numDisplays = mActivityDisplays.size();
2737 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2739 // Step through all stacks starting from behind, to hit the oldest things first.
2740 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2741 final ActivityStack stack = stacks.get(stackNdx);
2742 // Try to release activities in this stack; if we manage to, we are done.
2743 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2744 return;
2745 }
2746 }
2747 }
2748 }
2749
Amith Yamasani37a40c22015-06-17 13:25:42 -07002750 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002751 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002752 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002753 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002754
Craig Mautner858d8a62013-04-23 17:08:34 -07002755 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2757 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 final ActivityStack stack = stacks.get(stackNdx);
2760 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002761 TaskRecord task = stack.topTask();
2762 if (task != null) {
2763 mWindowManager.moveTaskToTop(task.taskId);
2764 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002765 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002766 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002767
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002768 ActivityStack stack = getStack(restoreStackId);
2769 if (stack == null) {
2770 stack = mHomeStack;
2771 }
2772 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002774 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002775 } else {
2776 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002777 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002778 }
Craig Mautner93529a42013-10-04 15:03:13 -07002779 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002780 }
2781
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002782 /** Checks whether the userid is a profile of the current user. */
2783 boolean isCurrentProfileLocked(int userId) {
2784 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002785 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002786 }
2787
Chong Zhang45c25ce2015-08-10 22:18:26 -07002788 /** Checks whether the activity should be shown for current user. */
2789 boolean okToShowLocked(ActivityRecord r) {
2790 return r != null && (isCurrentProfileLocked(r.userId)
2791 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2792 }
2793
Craig Mautnerde4ef022013-04-07 19:01:33 -07002794 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795 ArrayList<ActivityRecord> stops = null;
2796
2797 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002798 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2799 ActivityRecord s = mStoppingActivities.get(activityNdx);
2800 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002801 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002802 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2803 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002804 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002805 if (s.finishing) {
2806 // If this activity is finishing, it is sitting on top of
2807 // everyone else but we now know it is no longer needed...
2808 // so get rid of it. Otherwise, we need to go through the
2809 // normal flow and hide it once we determine that it is
2810 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002811 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002812 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002813 }
2814 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002815 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002816 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002817 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002818 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002819 }
2820 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002821 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002822 }
2823 }
2824
2825 return stops;
2826 }
2827
Craig Mautnercf910b02013-04-23 11:23:27 -07002828 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2830 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2831 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2832 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002833 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002834 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002835 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002836 if (r == null) Slog.e(TAG,
2837 "validateTop...: null top activity, stack=" + stack);
2838 else {
2839 final ActivityRecord pausing = stack.mPausingActivity;
2840 if (pausing != null && pausing == r) Slog.e(TAG,
2841 "validateTop...: top stack has pausing activity r=" + r
2842 + " state=" + state);
2843 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2844 "validateTop...: activity in front not resumed r=" + r
2845 + " state=" + state);
2846 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002847 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002848 final ActivityRecord resumed = stack.mResumedActivity;
2849 if (resumed != null && resumed == r) Slog.e(TAG,
2850 "validateTop...: back stack has resumed activity r=" + r
2851 + " state=" + state);
2852 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2853 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002854 }
2855 }
2856 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002857 }
2858
Craig Mautnere0570202015-05-13 13:06:11 -07002859 private String lockTaskModeToString() {
2860 switch (mLockTaskModeState) {
2861 case LOCK_TASK_MODE_LOCKED:
2862 return "LOCKED";
2863 case LOCK_TASK_MODE_PINNED:
2864 return "PINNED";
2865 case LOCK_TASK_MODE_NONE:
2866 return "NONE";
2867 default: return "unknown=" + mLockTaskModeState;
2868 }
2869 }
2870
Craig Mautner27084302013-03-25 08:05:25 -07002871 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002872 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002873 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002874 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002875 pw.print(prefix);
2876 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002877 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002878 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002879 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2880 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2881 if (packages.size() > 0) {
2882 pw.println(" mLockTaskPackages (userId:packages)=");
2883 for (int i = 0; i < packages.size(); ++i) {
2884 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2885 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2886 }
2887 }
2888 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002889 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002890
Craig Mautner20e72272013-04-01 13:45:53 -07002891 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002892 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002893 }
2894
Dianne Hackborn390517b2013-05-30 15:03:32 -07002895 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2896 boolean needSep, String prefix) {
2897 if (activity != null) {
2898 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2899 if (needSep) {
2900 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002901 }
2902 pw.print(prefix);
2903 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002904 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002905 }
2906 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002907 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002908 }
2909
Craig Mautner8d341ef2013-03-26 09:03:27 -07002910 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2911 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002912 boolean printed = false;
2913 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002914 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2915 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002916 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002917 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002918 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002919 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 final ActivityStack stack = stacks.get(stackNdx);
2921 StringBuilder stackHeader = new StringBuilder(128);
2922 stackHeader.append(" Stack #");
2923 stackHeader.append(stack.mStackId);
2924 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002925 stackHeader.append("\n");
2926 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2927 stackHeader.append("\n");
2928 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2930 needSep, stackHeader.toString());
2931 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2932 !dumpAll, false, dumpPackage, true,
2933 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002934
Craig Mautner4a1cb222013-12-04 16:14:06 -08002935 needSep = printed;
2936 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2937 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002938 if (pr) {
2939 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002940 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002941 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2943 " mResumedActivity: ");
2944 if (pr) {
2945 printed = true;
2946 needSep = false;
2947 }
2948 if (dumpAll) {
2949 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2950 " mLastPausedActivity: ");
2951 if (pr) {
2952 printed = true;
2953 needSep = true;
2954 }
2955 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2956 needSep, " mLastNoHistoryActivity: ");
2957 }
2958 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 }
2960 }
2961
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002962 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2963 false, dumpPackage, true, " Activities waiting to finish:", null);
2964 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2965 false, dumpPackage, true, " Activities waiting to stop:", null);
2966 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2967 false, dumpPackage, true, " Activities waiting for another to become visible:",
2968 null);
2969 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2970 false, dumpPackage, true, " Activities waiting to sleep:", null);
2971 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2972 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002973
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002974 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 }
2976
Dianne Hackborn390517b2013-05-30 15:03:32 -07002977 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002979 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002980 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002981 String innerPrefix = null;
2982 String[] args = null;
2983 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002984 for (int i=list.size()-1; i>=0; i--) {
2985 final ActivityRecord r = list.get(i);
2986 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2987 continue;
2988 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002989 if (innerPrefix == null) {
2990 innerPrefix = prefix + " ";
2991 args = new String[0];
2992 }
2993 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002994 final boolean full = !brief && (complete || !r.isInHistory());
2995 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002996 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002997 needNL = false;
2998 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002999 if (header1 != null) {
3000 pw.println(header1);
3001 header1 = null;
3002 }
3003 if (header2 != null) {
3004 pw.println(header2);
3005 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003007 if (lastTask != r.task) {
3008 lastTask = r.task;
3009 pw.print(prefix);
3010 pw.print(full ? "* " : " ");
3011 pw.println(lastTask);
3012 if (full) {
3013 lastTask.dump(pw, prefix + " ");
3014 } else if (complete) {
3015 // Complete + brief == give a summary. Isn't that obvious?!?
3016 if (lastTask.intent != null) {
3017 pw.print(prefix); pw.print(" ");
3018 pw.println(lastTask.intent.toInsecureStringWithClip());
3019 }
3020 }
3021 }
3022 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3023 pw.print(" #"); pw.print(i); pw.print(": ");
3024 pw.println(r);
3025 if (full) {
3026 r.dump(pw, innerPrefix);
3027 } else if (complete) {
3028 // Complete + brief == give a summary. Isn't that obvious?!?
3029 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3030 if (r.app != null) {
3031 pw.print(innerPrefix); pw.println(r.app);
3032 }
3033 }
3034 if (client && r.app != null && r.app.thread != null) {
3035 // flush anything that is already in the PrintWriter since the thread is going
3036 // to write to the file descriptor directly
3037 pw.flush();
3038 try {
3039 TransferPipe tp = new TransferPipe();
3040 try {
3041 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3042 r.appToken, innerPrefix, args);
3043 // Short timeout, since blocking here can
3044 // deadlock with the application.
3045 tp.go(fd, 2000);
3046 } finally {
3047 tp.kill();
3048 }
3049 } catch (IOException e) {
3050 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3051 } catch (RemoteException e) {
3052 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3053 }
3054 needNL = true;
3055 }
3056 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003057 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059
Craig Mautnerf3333272013-04-22 10:55:53 -07003060 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003061 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3062 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003063 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3064 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003065 }
3066
3067 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003068 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003069 }
3070
3071 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003072 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3073 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003074 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3075 }
3076
Craig Mautner05d29032013-05-03 13:40:13 -07003077 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003078 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3079 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3080 }
Craig Mautner05d29032013-05-03 13:40:13 -07003081 }
3082
Craig Mautner0eea92c2013-05-16 13:35:39 -07003083 void removeSleepTimeouts() {
3084 mSleepTimeout = false;
3085 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3086 }
3087
3088 final void scheduleSleepTimeout() {
3089 removeSleepTimeouts();
3090 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3091 }
3092
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 @Override
3094 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003095 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003096 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3097 }
3098
3099 @Override
3100 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003101 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003102 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3103 }
3104
3105 @Override
3106 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003107 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003108 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3109 }
3110
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003111 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003112 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003113 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003114 newDisplay = mActivityDisplays.get(displayId) == null;
3115 if (newDisplay) {
3116 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003117 if (activityDisplay.mDisplay == null) {
3118 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3119 return;
3120 }
Craig Mautner4504de52013-12-20 09:06:56 -08003121 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003122 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003123 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003124 }
Craig Mautner4504de52013-12-20 09:06:56 -08003125 if (newDisplay) {
3126 mWindowManager.onDisplayAdded(displayId);
3127 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 }
3129
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003130 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3131 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3132 return;
3133 }
3134 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3135 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3136 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3137 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3138 }
3139
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003140 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003141 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003142 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3143 if (activityDisplay != null) {
3144 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003145 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003146 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003147 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003148 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003149 }
3150 }
3151 mWindowManager.onDisplayRemoved(displayId);
3152 }
3153
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003154 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003156 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3157 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003158 // TODO: Update the bounds.
3159 }
3160 }
3161 mWindowManager.onDisplayChanged(displayId);
3162 }
3163
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003164 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003165 StackInfo info = new StackInfo();
3166 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3167 info.displayId = Display.DEFAULT_DISPLAY;
3168 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003169 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003170
3171 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3172 final int numTasks = tasks.size();
3173 int[] taskIds = new int[numTasks];
3174 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003175 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003176 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003177 for (int i = 0; i < numTasks; ++i) {
3178 final TaskRecord task = tasks.get(i);
3179 taskIds[i] = task.taskId;
3180 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3181 : task.realActivity != null ? task.realActivity.flattenToString()
3182 : task.getTopActivity() != null ? task.getTopActivity().packageName
3183 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003184 taskBounds[i] = new Rect();
3185 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003186 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 }
3188 info.taskIds = taskIds;
3189 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003190 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003191 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003192 return info;
3193 }
3194
3195 StackInfo getStackInfoLocked(int stackId) {
3196 ActivityStack stack = getStack(stackId);
3197 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003198 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003199 }
3200 return null;
3201 }
3202
3203 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003204 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003205 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3206 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003207 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003208 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003209 }
3210 }
3211 return list;
3212 }
3213
Craig Mautner15df08a2015-04-01 12:17:18 -07003214 TaskRecord getLockedTaskLocked() {
3215 final int top = mLockTaskModeTasks.size() - 1;
3216 if (top >= 0) {
3217 return mLockTaskModeTasks.get(top);
3218 }
3219 return null;
3220 }
3221
3222 boolean isLockedTask(TaskRecord task) {
3223 return mLockTaskModeTasks.contains(task);
3224 }
3225
3226 boolean isLastLockedTask(TaskRecord task) {
3227 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3228 }
3229
3230 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003231 if (!mLockTaskModeTasks.remove(task)) {
3232 return;
3233 }
3234 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3235 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003236 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003237 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3238 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003239 final Message lockTaskMsg = Message.obtain();
3240 lockTaskMsg.arg1 = task.userId;
3241 lockTaskMsg.what = LOCK_TASK_END_MSG;
3242 mHandler.sendMessage(lockTaskMsg);
3243 }
3244 }
3245
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003246 void showNonResizeableDockToastIfNeeded(
3247 TaskRecord task, int preferredStackId, int actualStackId) {
3248 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3249 return;
3250 }
3251
3252 if (!task.canGoInDockedStack() || task.inCropWindowsResizeMode()) {
3253 // Display warning toast if we tried to put a non-dockable task in the docked stack or
3254 // the task is running in cropped window mode.
3255 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3256 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003257 }
3258
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003259 void showLockTaskToast() {
3260 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003261 }
3262
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003263 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3264 if (mLockTaskModeTasks.contains(task)) {
3265 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3266 }
3267 }
3268
Craig Mautner432f64e2015-05-20 14:59:57 -07003269 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3270 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003271 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003272 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003273 final TaskRecord lockedTask = getLockedTaskLocked();
3274 if (lockedTask != null) {
3275 removeLockedTaskLocked(lockedTask);
3276 if (!mLockTaskModeTasks.isEmpty()) {
3277 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003278 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3279 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003280 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003281 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003282 return;
3283 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003284 }
Craig Mautnere0570202015-05-13 13:06:11 -07003285 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3286 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003287 return;
3288 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003289
3290 // Should have already been checked, but do it again.
3291 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003292 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3293 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003294 return;
3295 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003296 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003297 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003298 return;
3299 }
3300
3301 if (mLockTaskModeTasks.isEmpty()) {
3302 // First locktask.
3303 final Message lockTaskMsg = Message.obtain();
3304 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3305 lockTaskMsg.arg1 = task.userId;
3306 lockTaskMsg.what = LOCK_TASK_START_MSG;
3307 lockTaskMsg.arg2 = lockTaskModeState;
3308 mHandler.sendMessage(lockTaskMsg);
3309 }
3310 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003311 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3312 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003313 mLockTaskModeTasks.remove(task);
3314 mLockTaskModeTasks.add(task);
3315
3316 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003317 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003318 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003319
3320 if (andResume) {
3321 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003322 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003323 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003324 }
3325
3326 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003327 return isLockTaskModeViolation(task, false);
3328 }
3329
3330 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3331 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003332 return false;
3333 }
3334 final int lockTaskAuth = task.mLockTaskAuth;
3335 switch (lockTaskAuth) {
3336 case LOCK_TASK_AUTH_DONT_LOCK:
3337 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003338 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003339 case LOCK_TASK_AUTH_LAUNCHABLE:
3340 case LOCK_TASK_AUTH_WHITELISTED:
3341 return false;
3342 case LOCK_TASK_AUTH_PINNABLE:
3343 // Pinnable tasks can't be launched on top of locktask tasks.
3344 return !mLockTaskModeTasks.isEmpty();
3345 default:
3346 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3347 return true;
3348 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003349 }
3350
Craig Mautner15df08a2015-04-01 12:17:18 -07003351 void onLockTaskPackagesUpdatedLocked() {
3352 boolean didSomething = false;
3353 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3354 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003355 final boolean wasWhitelisted =
3356 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3357 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003358 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003359 final boolean isWhitelisted =
3360 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3361 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3362 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003363 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003364 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3365 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003366 removeLockedTaskLocked(lockedTask);
3367 lockedTask.performClearTaskLocked();
3368 didSomething = true;
3369 }
3370 }
3371 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3372 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3373 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3374 final ActivityStack stack = stacks.get(stackNdx);
3375 stack.onLockTaskPackagesUpdatedLocked();
3376 }
3377 }
Craig Mautnere0570202015-05-13 13:06:11 -07003378 final ActivityRecord r = topRunningActivityLocked();
3379 final TaskRecord task = r != null ? r.task : null;
3380 if (mLockTaskModeTasks.isEmpty() && task != null
3381 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3382 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003383 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3384 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3385 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3386 false);
3387 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003388 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003389 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003390 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003391 }
3392 }
3393
Benjamin Franz43261142015-02-11 15:59:44 +00003394 int getLockTaskModeState() {
3395 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003396 }
3397
Jorim Jaggife89d122015-12-22 16:28:44 +01003398 void activityRelaunchedLocked(IBinder token) {
3399 mWindowManager.notifyAppRelaunchingFinished(token);
3400 }
3401
3402 void activityRelaunchingLocked(ActivityRecord r) {
3403 mWindowManager.notifyAppRelaunching(r.appToken);
3404 }
3405
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003406 void logStackState() {
3407 mActivityMetricsLogger.logWindowState();
3408 }
3409
Wale Ogunwale22e25262016-02-01 10:32:02 -08003410 void scheduleReportMultiWindowChanged(TaskRecord task) {
3411 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3412 final ActivityRecord r = task.mActivities.get(i);
3413 if (r.app != null && r.app.thread != null) {
3414 mMultiWindowModeChangedActivities.add(r);
3415 }
3416 }
3417
3418 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3419 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3420 }
3421 }
3422
3423 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3424 final ActivityStack stack = task.stack;
3425 if (prevStack == null || prevStack == stack
3426 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3427 return;
3428 }
3429
3430 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3431 final ActivityRecord r = task.mActivities.get(i);
3432 if (r.app != null && r.app.thread != null) {
3433 mPipModeChangedActivities.add(r);
3434 }
3435 }
3436
3437 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3438 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3439 }
3440 }
3441
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003442 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003443
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003444 public ActivityStackSupervisorHandler(Looper looper) {
3445 super(looper);
3446 }
3447
Craig Mautnerf3333272013-04-22 10:55:53 -07003448 void activityIdleInternal(ActivityRecord r) {
3449 synchronized (mService) {
3450 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3451 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003452 }
3453
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003454 @Override
3455 public void handleMessage(Message msg) {
3456 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003457 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3458 synchronized (mService) {
3459 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3460 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3461 r.scheduleMultiWindowChanged();
3462 }
3463 }
3464 } break;
3465 case REPORT_PIP_MODE_CHANGED_MSG: {
3466 synchronized (mService) {
3467 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3468 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3469 r.schedulePictureInPictureChanged();
3470 }
3471 }
3472 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003473 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003474 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3475 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003476 if (mService.mDidDexOpt) {
3477 mService.mDidDexOpt = false;
3478 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3479 nmsg.obj = msg.obj;
3480 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3481 return;
3482 }
3483 // We don't at this point know if the activity is fullscreen,
3484 // so we need to be conservative and assume it isn't.
3485 activityIdleInternal((ActivityRecord)msg.obj);
3486 } break;
3487 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003488 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003489 activityIdleInternal((ActivityRecord)msg.obj);
3490 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003491 case RESUME_TOP_ACTIVITY_MSG: {
3492 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003493 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003494 }
3495 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003496 case SLEEP_TIMEOUT_MSG: {
3497 synchronized (mService) {
3498 if (mService.isSleepingOrShuttingDown()) {
3499 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3500 mSleepTimeout = true;
3501 checkReadyForSleepLocked();
3502 }
3503 }
3504 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003505 case LAUNCH_TIMEOUT_MSG: {
3506 if (mService.mDidDexOpt) {
3507 mService.mDidDexOpt = false;
3508 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3509 return;
3510 }
3511 synchronized (mService) {
3512 if (mLaunchingActivity.isHeld()) {
3513 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3514 if (VALIDATE_WAKE_LOCK_CALLER
3515 && Binder.getCallingUid() != Process.myUid()) {
3516 throw new IllegalStateException("Calling must be system uid");
3517 }
3518 mLaunchingActivity.release();
3519 }
3520 }
3521 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003523 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 } break;
3525 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003526 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 } break;
3528 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003529 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003531 case CONTAINER_CALLBACK_VISIBILITY: {
3532 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003533 final IActivityContainerCallback callback = container.mCallback;
3534 if (callback != null) {
3535 try {
3536 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3537 } catch (RemoteException e) {
3538 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003539 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003540 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003541 case LOCK_TASK_START_MSG: {
3542 // When lock task starts, we disable the status bars.
3543 try {
Jason Monk62515be2014-05-21 16:06:19 -04003544 if (mLockTaskNotify == null) {
3545 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003546 }
Jason Monk62515be2014-05-21 16:06:19 -04003547 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003548 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003549 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003550 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003551 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003552 flags = StatusBarManager.DISABLE_MASK
3553 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003554 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003555 flags = StatusBarManager.DISABLE_MASK
3556 & (~StatusBarManager.DISABLE_BACK)
3557 & (~StatusBarManager.DISABLE_HOME)
3558 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003559 }
3560 getStatusBarService().disable(flags, mToken,
3561 mService.mContext.getPackageName());
3562 }
3563 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003564 if (getDevicePolicyManager() != null) {
3565 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003566 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003567 }
justinzhang5286d3f2014-05-12 17:06:01 -04003568 } catch (RemoteException ex) {
3569 throw new RuntimeException(ex);
3570 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003571 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003572 case LOCK_TASK_END_MSG: {
3573 // When lock task ends, we enable the status bars.
3574 try {
Jason Monk62515be2014-05-21 16:06:19 -04003575 if (getStatusBarService() != null) {
3576 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3577 mService.mContext.getPackageName());
3578 }
3579 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003580 if (getDevicePolicyManager() != null) {
3581 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3582 msg.arg1);
3583 }
Jason Monk62515be2014-05-21 16:06:19 -04003584 if (mLockTaskNotify == null) {
3585 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3586 }
3587 mLockTaskNotify.show(false);
3588 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003589 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003590 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003591 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003592 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003593 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003594 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003595 new LockPatternUtils(mService.mContext)
3596 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003597 }
3598 } catch (SettingNotFoundException e) {
3599 // No setting, don't lock.
3600 }
justinzhang5286d3f2014-05-12 17:06:01 -04003601 } catch (RemoteException ex) {
3602 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003603 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003604 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003605 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003606 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003607 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3608 if (mLockTaskNotify == null) {
3609 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3610 }
3611 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3612 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003613 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3614 final ActivityContainer container = (ActivityContainer) msg.obj;
3615 final IActivityContainerCallback callback = container.mCallback;
3616 if (callback != null) {
3617 try {
3618 callback.onAllActivitiesComplete(container.asBinder());
3619 } catch (RemoteException e) {
3620 }
3621 }
3622 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003623 case LAUNCH_TASK_BEHIND_COMPLETE: {
3624 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003625 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003626 if (r != null) {
3627 handleLaunchTaskBehindCompleteLocked(r);
3628 }
3629 }
3630 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003631
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003632 }
3633 }
3634 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003635
Ying Wangb081a592014-04-22 15:20:16 -07003636 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003637 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3638 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003640 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003641 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003642 ActivityRecord mParentActivity = null;
3643 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003644
Craig Mautnere3a00d72014-04-16 08:31:19 -07003645 boolean mVisible = true;
3646
Craig Mautner4a1cb222013-12-04 16:14:06 -08003647 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003648 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003650 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3651 final static int CONTAINER_STATE_NO_SURFACE = 1;
3652 final static int CONTAINER_STATE_FINISHING = 2;
3653 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3654
3655 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003656 synchronized (mService) {
3657 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003658 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003659 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003660 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003661 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003662 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003663
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003664 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003665 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003666 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003667 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003668 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003669 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003670 }
3671
3672 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003673 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003674 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003675 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3676 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003677 return;
3678 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003679 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680 }
3681 }
3682
3683 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003684 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003685 synchronized (mService) {
3686 if (mActivityDisplay != null) {
3687 return mActivityDisplay.mDisplayId;
3688 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003689 }
3690 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003691 }
3692
Jeff Brownca9bc702014-02-11 14:32:56 -08003693 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003694 public int getStackId() {
3695 synchronized (mService) {
3696 return mStackId;
3697 }
3698 }
3699
3700 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003701 public boolean injectEvent(InputEvent event) {
3702 final long origId = Binder.clearCallingIdentity();
3703 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003704 synchronized (mService) {
3705 if (mActivityDisplay != null) {
3706 return mInputManagerInternal.injectInputEvent(event,
3707 mActivityDisplay.mDisplayId,
3708 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3709 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003710 }
3711 return false;
3712 } finally {
3713 Binder.restoreCallingIdentity(origId);
3714 }
3715 }
3716
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003717 @Override
3718 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003719 synchronized (mService) {
3720 if (mContainerState == CONTAINER_STATE_FINISHING) {
3721 return;
3722 }
3723 mContainerState = CONTAINER_STATE_FINISHING;
3724
Craig Mautnerd163e752014-06-13 17:18:47 -07003725 long origId = Binder.clearCallingIdentity();
3726 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003727 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003728 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003729 } finally {
3730 Binder.restoreCallingIdentity(origId);
3731 }
3732 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003733 }
3734
Craig Mautner60257702014-09-17 15:02:33 -07003735 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003736 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003737 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003738 if (mActivityDisplay != null) {
3739 mActivityDisplay.detachActivitiesLocked(mStack);
3740 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003741 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 }
3743 }
3744
3745 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003746 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003747 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748 }
3749
3750 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003751 public final int startActivityIntentSender(IIntentSender intentSender)
3752 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003753 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3754
3755 if (!(intentSender instanceof PendingIntentRecord)) {
3756 throw new IllegalArgumentException("Bad PendingIntent object");
3757 }
3758
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003759 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003760 Binder.getCallingUid(), mCurrentUser, false,
3761 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003762
3763 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3764 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3765 pendingIntent.key.requestResolvedType);
3766
3767 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3768 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3769 }
3770
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003771 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003772 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003773 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003774 throw new SecurityException(
3775 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3776 }
3777 }
3778
Craig Mautnerdf88d732014-01-27 09:21:32 -08003779 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 public IBinder asBinder() {
3781 return this;
3782 }
3783
Craig Mautner4504de52013-12-20 09:06:56 -08003784 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003785 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003786 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003787 }
3788
Craig Mautner4a1cb222013-12-04 16:14:06 -08003789 ActivityStackSupervisor getOuter() {
3790 return ActivityStackSupervisor.this;
3791 }
3792
Craig Mautnerd163e752014-06-13 17:18:47 -07003793 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003794 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 }
3796
Craig Mautner6985bad2014-04-21 15:22:06 -07003797 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003798 void setVisible(boolean visible) {
3799 if (mVisible != visible) {
3800 mVisible = visible;
3801 if (mCallback != null) {
3802 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3803 0 /* unused */, this).sendToTarget();
3804 }
3805 }
3806 }
3807
Craig Mautner6985bad2014-04-21 15:22:06 -07003808 void setDrawn() {
3809 }
3810
Craig Mautner1b4bf852014-05-26 15:06:32 -07003811 // You can always start a new task on a regular ActivityStack.
3812 boolean isEligibleForNewTasks() {
3813 return true;
3814 }
3815
Craig Mautnerd163e752014-06-13 17:18:47 -07003816 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003817 detachLocked();
3818 deleteActivityContainer(this);
3819 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003820 }
3821
Craig Mautner34b73df2014-01-12 21:11:08 -08003822 @Override
3823 public String toString() {
3824 return mIdString + (mActivityDisplay == null ? "N" : "A");
3825 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003826 }
3827
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003828 private class VirtualActivityContainer extends ActivityContainer {
3829 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003830 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003831
3832 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3833 super(getNextStackId());
3834 mParentActivity = parent;
3835 mCallback = callback;
3836 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003837 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003838 }
3839
3840 @Override
3841 public void setSurface(Surface surface, int width, int height, int density) {
3842 super.setSurface(surface, width, height, density);
3843
3844 synchronized (mService) {
3845 final long origId = Binder.clearCallingIdentity();
3846 try {
3847 setSurfaceLocked(surface, width, height, density);
3848 } finally {
3849 Binder.restoreCallingIdentity(origId);
3850 }
3851 }
3852 }
3853
3854 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3855 if (mContainerState == CONTAINER_STATE_FINISHING) {
3856 return;
3857 }
3858 VirtualActivityDisplay virtualActivityDisplay =
3859 (VirtualActivityDisplay) mActivityDisplay;
3860 if (virtualActivityDisplay == null) {
3861 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003862 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003863 mActivityDisplay = virtualActivityDisplay;
3864 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003865 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003866 }
3867
3868 if (mSurface != null) {
3869 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003870 }
3871
Craig Mautner6985bad2014-04-21 15:22:06 -07003872 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003873 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003874 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003875 } else {
3876 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003877 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003878 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003879 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003880 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003881 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003882
Craig Mautnerd163e752014-06-13 17:18:47 -07003883 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003884
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003885 if (DEBUG_STACK) Slog.d(TAG_STACK,
3886 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003887 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003888
Craig Mautner6985bad2014-04-21 15:22:06 -07003889 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003890 boolean isAttachedLocked() {
3891 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003892 }
3893
3894 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003895 void setDrawn() {
3896 synchronized (mService) {
3897 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003898 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003899 }
3900 }
3901
Craig Mautner1b4bf852014-05-26 15:06:32 -07003902 // Never start a new task on an ActivityView if it isn't explicitly specified.
3903 @Override
3904 boolean isEligibleForNewTasks() {
3905 return false;
3906 }
3907
Craig Mautnerd163e752014-06-13 17:18:47 -07003908 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003909 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003910 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3911 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3912 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3913 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3914 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003915 }
3916 }
3917
Craig Mautner4a1cb222013-12-04 16:14:06 -08003918 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3919 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003920 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003922 int mDisplayId;
3923 Display mDisplay;
3924 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003925
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3927 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003928 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929
Jose Lima4b6c6692014-08-12 17:41:12 -07003930 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003931
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003932 ActivityDisplay() {
3933 }
Craig Mautner4504de52013-12-20 09:06:56 -08003934
Craig Mautner1a70a162014-09-13 12:09:31 -07003935 // After instantiation, check that mDisplay is not null before using this. The alternative
3936 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003937 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003938 final Display display = mDisplayManager.getDisplay(displayId);
3939 if (display == null) {
3940 return;
3941 }
3942 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003943 }
3944
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003946 mDisplay = display;
3947 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003949 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003950
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003951 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003952 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003953 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3954 + " onTop=" + onTop);
3955 if (onTop) {
3956 mStacks.add(stack);
3957 } else {
3958 mStacks.add(0, stack);
3959 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 }
3961
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003963 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003964 + " from displayId=" + mDisplayId);
3965 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 }
3967
Jose Lima4b6c6692014-08-12 17:41:12 -07003968 void setVisibleBehindActivity(ActivityRecord r) {
3969 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003970 }
3971
Jose Lima4b6c6692014-08-12 17:41:12 -07003972 boolean hasVisibleBehindActivity() {
3973 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003974 }
3975
Craig Mautner34b73df2014-01-12 21:11:08 -08003976 @Override
3977 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003978 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3979 }
3980 }
3981
3982 class VirtualActivityDisplay extends ActivityDisplay {
3983 VirtualDisplay mVirtualDisplay;
3984
Craig Mautner6985bad2014-04-21 15:22:06 -07003985 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003986 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003987 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3988 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3989 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3990 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003991
3992 init(mVirtualDisplay.getDisplay());
3993
3994 mWindowManager.handleDisplayAdded(mDisplayId);
3995 }
3996
3997 void setSurface(Surface surface) {
3998 if (mVirtualDisplay != null) {
3999 mVirtualDisplay.setSurface(surface);
4000 }
4001 }
4002
4003 @Override
4004 void detachActivitiesLocked(ActivityStack stack) {
4005 super.detachActivitiesLocked(stack);
4006 if (mVirtualDisplay != null) {
4007 mVirtualDisplay.release();
4008 mVirtualDisplay = null;
4009 }
4010 }
4011
4012 @Override
4013 public String toString() {
4014 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004015 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004016 }
Jose Lima58e66d62014-05-27 20:00:27 -07004017
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004018 /**
4019 * Adjust bounds to stay within stack bounds.
4020 *
4021 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4022 * that keep them unchanged, but be contained within the stack bounds.
4023 *
4024 * @param bounds Bounds to be adjusted.
4025 * @param stackBounds Bounds within which the other bounds should remain.
4026 */
4027 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4028 if (stackBounds == null || stackBounds.contains(bounds)) {
4029 return;
4030 }
4031
4032 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4033 final int maxRight = stackBounds.right
4034 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4035 int horizontalDiff = stackBounds.left - bounds.left;
4036 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4037 || (bounds.left + horizontalDiff >= maxRight)) {
4038 horizontalDiff = maxRight - bounds.left;
4039 }
4040 bounds.left += horizontalDiff;
4041 bounds.right += horizontalDiff;
4042 }
4043
4044 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4045 final int maxBottom = stackBounds.bottom
4046 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4047 int verticalDiff = stackBounds.top - bounds.top;
4048 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4049 || (bounds.top + verticalDiff >= maxBottom)) {
4050 verticalDiff = maxBottom - bounds.top;
4051 }
4052 bounds.top += verticalDiff;
4053 bounds.bottom += verticalDiff;
4054 }
4055 }
4056
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004057 ActivityStack findStackBehind(ActivityStack stack) {
4058 // TODO(multi-display): We are only looking for stacks on the default display.
4059 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4060 if (display == null) {
4061 return null;
4062 }
4063 final ArrayList<ActivityStack> stacks = display.mStacks;
4064 for (int i = stacks.size() - 1; i >= 0; i--) {
4065 if (stacks.get(i) == stack && i > 0) {
4066 return stacks.get(i - 1);
4067 }
4068 }
4069 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4070 + " in=" + stacks);
4071 }
Craig Mautner27084302013-03-25 08:05:25 -07004072}