blob: 828cb53b9a5340ccb11ca9d60c34af8a699bae87 [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;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800206
Wale Ogunwale040b4702015-08-06 18:10:50 -0700207 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208
Jason Monk62515be2014-05-21 16:06:19 -0400209 private static final String LOCK_TASK_TAG = "Lock-to-App";
210
Wale Ogunwale040b4702015-08-06 18:10:50 -0700211 // Used to indicate if an object (e.g. stack) that we are trying to get
212 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800213 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700215 // Used to indicate that windows of activities should be preserved during the resize.
216 static final boolean PRESERVE_WINDOWS = true;
217
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218 // Used to indicate if an object (e.g. task) should be moved/created
219 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700220 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221
222 // Used to indicate that an objects (e.g. task) removal from its container
223 // (e.g. stack) is due to it moving to another container.
224 static final boolean MOVING = true;
225
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700226 // Force the focus to change to the stack we are moving a task to..
227 static final boolean FORCE_FOCUS = true;
228
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700229 // Restore task from the saved recents if it can't be found in any live stack.
230 static final boolean RESTORE_FROM_RECENTS = true;
231
Svetoslav7008b512015-06-24 18:47:07 -0700232 // Activity actions an app cannot start if it uses a permission which is not granted.
233 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
234 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700235
Svetoslav7008b512015-06-24 18:47:07 -0700236 static {
237 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
238 Manifest.permission.CAMERA);
239 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
240 Manifest.permission.CAMERA);
241 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
242 Manifest.permission.CALL_PHONE);
243 }
244
Svet Ganov99b60432015-06-27 13:15:22 -0700245 /** Action restriction: launching the activity is not restricted. */
246 private static final int ACTIVITY_RESTRICTION_NONE = 0;
247 /** Action restriction: launching the activity is restricted by a permission. */
248 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
249 /** Action restriction: launching the activity is restricted by an app op. */
250 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700251
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700252 // The height/width divide used when fitting a task within a bounds with method
253 // {@link #fitWithinBounds}.
254 // We always want the task to to be visible in the bounds without affecting its size when
255 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
256 // the input bounds right or bottom side minus the width or height divided by this value.
257 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
258
justinzhang5286d3f2014-05-12 17:06:01 -0400259 /** Status Bar Service **/
260 private IBinder mToken = new Binder();
261 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400262 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400263
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700264 // For debugging to make sure the caller when acquiring/releasing our
265 // wake lock is the system process.
266 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800267 /** The number of distinct task ids that can be assigned to the tasks of a single user */
268 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700269
Craig Mautner27084302013-03-25 08:05:25 -0700270 final ActivityManagerService mService;
271
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800272 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800273
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700274 final ActivityStackSupervisorHandler mHandler;
275
276 /** Short cut */
277 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700279
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700280 /** Counter for next free stack ID to use for dynamic activity stacks. */
281 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700282
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /**
284 * Maps the task identifier that activities are currently being started in to the userId of the
285 * task. Each time a new task is created, the entry for the userId of the task is incremented
286 */
287 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700288
Craig Mautner2420ead2013-04-01 17:13:20 -0700289 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800290 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700291
Craig Mautnere0a38842013-12-16 16:14:02 -0800292 /** The stack containing the launcher app. Assumed to always be attached to
293 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800294 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700295
Craig Mautnere0a38842013-12-16 16:14:02 -0800296 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800297 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Craig Mautner4a1cb222013-12-04 16:14:06 -0800299 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
300 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800301 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800302 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700303
304 /** List of activities that are waiting for a new activity to become visible before completing
305 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800306 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700307
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700308 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800309 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700310
311 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800312 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700313
Craig Mautnerde4ef022013-04-07 19:01:33 -0700314 /** List of activities that are ready to be stopped, but waiting for the next activity to
315 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317
Craig Mautnerf3333272013-04-22 10:55:53 -0700318 /** List of activities that are ready to be finished, but waiting for the previous activity to
319 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700321
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700324
Craig Mautnerf3333272013-04-22 10:55:53 -0700325 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700326 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700327
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700328 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700329 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700330
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
332 * is being brought in front of us. */
333 boolean mUserLeaving = false;
334
Craig Mautner0eea92c2013-05-16 13:35:39 -0700335 /** Set when we have taken too long waiting to go to sleep. */
336 boolean mSleepTimeout = false;
337
338 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700339 * We don't want to allow the device to go to sleep while in the process
340 * of launching an activity. This is primarily to allow alarm intent
341 * receivers to launch an activity and get that to run before the device
342 * goes back to sleep.
343 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700344 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700345
346 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700347 * Set when the system is going to sleep, until we have
348 * successfully paused the current activity and released our wake lock.
349 * At that point the system is allowed to actually sleep.
350 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700351 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700352
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700353 /** Stack id of the front stack when user switched, indexed by userId. */
354 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700355
Craig Mautner4504de52013-12-20 09:06:56 -0800356 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700358 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800359
360 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700361 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362
Jeff Brownca9bc702014-02-11 14:32:56 -0800363 InputManagerInternal mInputManagerInternal;
364
Craig Mautner15df08a2015-04-01 12:17:18 -0700365 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
366 * may be finished until there is only one entry left. If this is empty the system is not
367 * in lockTask mode. */
368 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000369 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700370 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
371 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000372 */
373 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400374 /**
375 * Notifies the user when entering/exiting lock-task.
376 */
377 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800378
Wale Ogunwaled046a012015-12-24 13:05:59 -0800379 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800380 boolean inResumeTopActivity;
381
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700382 // 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 -0700383 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700384 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700385
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700386 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
387 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800388 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700389
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700390 // The default minimal size that will be used if the activity doesn't specify its minimal size.
391 // It will be calculated when the default display gets added.
392 private int mDefaultMinimalSizeOfResizeableTask = -1;
393
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700394 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
395 private boolean mTaskLayersChanged = true;
396
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800397 private final ActivityMetricsLogger mActivityMetricsLogger;
398
Jorim Jaggidc249c42015-12-15 14:57:31 -0800399 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
400
Wale Ogunwale39381972015-12-17 17:15:29 -0800401 static class FindTaskResult {
402 ActivityRecord r;
403 boolean matchedByRootAffinity;
404 }
405 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
406
Craig Mautneree36c772014-07-16 14:56:05 -0700407 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100408 * Used to keep track whether app visibilities got changed since the last pause. Useful to
409 * determine whether to invoke the task stack change listener after pausing.
410 */
411 boolean mAppVisibilitiesChangedSinceLastPause;
412
413 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700414 * Description of a request to start a new activity, which has been held
415 * due to app switches being disabled.
416 */
417 static class PendingActivityLaunch {
418 final ActivityRecord r;
419 final ActivityRecord sourceRecord;
420 final int startFlags;
421 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700422 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700423
424 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700425 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700426 r = _r;
427 sourceRecord = _sourceRecord;
428 startFlags = _startFlags;
429 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700430 callerApp = _callerApp;
431 }
432
433 void sendErrorResult(String message) {
434 try {
435 if (callerApp.thread != null) {
436 callerApp.thread.scheduleCrash(message);
437 }
438 } catch (RemoteException e) {
439 Slog.e(TAG, "Exception scheduling crash of failed "
440 + "activity launcher sourceRecord=" + sourceRecord, e);
441 }
Craig Mautneree36c772014-07-16 14:56:05 -0700442 }
443 }
444
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800445 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700446 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700447 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800448 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800449 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700450 }
451
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800452 void setRecentTasks(RecentTasks recentTasks) {
453 mRecentTasks = recentTasks;
454 }
455
Jeff Brown2c43c332014-06-12 22:38:59 -0700456 /**
457 * At the time when the constructor runs, the power manager has not yet been
458 * initialized. So we initialize our wakelocks afterwards.
459 */
460 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700462 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700463 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700464 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700465 }
466
justinzhang5286d3f2014-05-12 17:06:01 -0400467 // This function returns a IStatusBarService. The value is from ServiceManager.
468 // getService and is cached.
469 private IStatusBarService getStatusBarService() {
470 synchronized (mService) {
471 if (mStatusBarService == null) {
472 mStatusBarService = IStatusBarService.Stub.asInterface(
473 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
474 if (mStatusBarService == null) {
475 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
476 }
477 }
478 return mStatusBarService;
479 }
480 }
481
Jason Monk35c62a42014-06-17 10:24:47 -0400482 private IDevicePolicyManager getDevicePolicyManager() {
483 synchronized (mService) {
484 if (mDevicePolicyManager == null) {
485 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
486 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
487 if (mDevicePolicyManager == null) {
488 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
489 }
490 }
491 return mDevicePolicyManager;
492 }
493 }
494
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700495 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 synchronized (mService) {
497 mWindowManager = wm;
498
499 mDisplayManager =
500 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
501 mDisplayManager.registerDisplayListener(this, null);
502
503 Display[] displays = mDisplayManager.getDisplays();
504 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
505 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800506 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700507 if (activityDisplay.mDisplay == null) {
508 throw new IllegalStateException("Default Display does not exist");
509 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700511 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 }
513
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800514 mHomeStack = mFocusedStack = mLastFocusedStack =
515 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800516
517 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 }
Craig Mautner27084302013-03-25 08:05:25 -0700519 }
520
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200521 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700522 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200523 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700524 }
525
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700526 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700528 }
529
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700532 }
533
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700534 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700535 if (stack == null) {
536 return false;
537 }
538
Craig Mautnerdf88d732014-01-27 09:21:32 -0800539 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
540 if (parent != null) {
541 stack = parent.task.stack;
542 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800543 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700544 }
545
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 /** The top most stack. */
547 boolean isFrontStack(ActivityStack stack) {
548 if (stack == null) {
549 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700551
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700552 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
553 if (parent != null) {
554 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800555 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
557 }
558
Wale Ogunwaled046a012015-12-24 13:05:59 -0800559 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800560 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
561 if (!focusCandidate.isFocusable()) {
562 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
563 focusCandidate = focusCandidate.getNextFocusableStackLocked();
564 }
565
566 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800567 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800568 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800569
Wale Ogunwaled046a012015-12-24 13:05:59 -0800570 EventLogTags.writeAmFocusedStack(
571 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
572 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
573 }
574
575 final ActivityRecord r = topRunningActivityLocked();
576 if (mService.mFocusedActivity != r) {
577 // The focus activity should always be the top activity in the focused stack.
578 // There will be chaos and anarchy if it isn't...
579 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
580 }
Craig Mautnerde313752015-01-22 14:28:03 -0800581
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800582 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800583 if (r != null && r.idle) {
584 checkFinishBootingLocked();
585 }
586 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700587 }
588
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700589 void moveHomeStackToFront(String reason) {
590 mHomeStack.moveToFront(reason);
591 }
592
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700593 /** Returns true if the focus activity was adjusted to the home stack top activity. */
594 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700595 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
596 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700597 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700598 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700599
Craig Mautner84984fa2014-06-19 11:19:20 -0700600 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700601
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700602 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700603 if (top == null) {
604 return false;
605 }
606 mService.setFocusedActivityLocked(top, reason);
607 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700608 }
609
Craig Mautner299f9602015-01-26 09:47:33 -0800610 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700611 if (!mService.mBooting && !mService.mBooted) {
612 // Not ready yet!
613 return false;
614 }
615
Craig Mautner84984fa2014-06-19 11:19:20 -0700616 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
617 mWindowManager.showRecentApps();
618 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700619 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700620
Craig Mautner84984fa2014-06-19 11:19:20 -0700621 if (prev != null) {
622 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
623 }
624
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700625 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
626 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800627 final String myReason = reason + " resumeHomeStackTask";
628
Mark Lua56ea122015-10-08 13:31:01 +0800629 // Only resume home activity if isn't finishing.
630 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800631 mService.setFocusedActivityLocked(r, myReason);
632 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700633 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800634 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700635 }
636
Craig Mautner8d341ef2013-03-26 09:03:27 -0700637 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700638 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700639 }
640
641 /**
642 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
643 * @param id Id of the task we would like returned.
644 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
645 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700646 * @param stackId The stack to restore the task to (default launch stack will be used if
647 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700648 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700649 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800650 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800652 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
654 ActivityStack stack = stacks.get(stackNdx);
655 TaskRecord task = stack.taskForIdLocked(id);
656 if (task != null) {
657 return task;
658 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700659 }
660 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800661
662 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700663 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800664 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800665 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800667 return null;
668 }
669
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700670 if (!restoreFromRecents) {
671 return task;
672 }
673
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700674 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700675 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
676 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800677 return null;
678 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700679 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800680 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700681 }
682
Craig Mautner6170f732013-04-02 13:05:23 -0700683 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
689 if (r != null) {
690 return r;
691 }
Craig Mautner6170f732013-04-02 13:05:23 -0700692 }
693 }
694 return null;
695 }
696
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000697 boolean isFocusedUserLockedProfile() {
698 final int userId = mFocusedStack.topRunningActivityLocked().userId;
699 return userId != UserHandle.myUserId()
700 && mService.mUserController.shouldConfirmCredentials(userId);
701 }
702
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800703 void setNextTaskIdForUserLocked(int taskId, int userId) {
704 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
705 if (taskId > currentTaskId) {
706 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700707 }
708 }
709
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800710 int getNextTaskIdForUserLocked(int userId) {
711 mRecentTasks.loadUserRecentsLocked(userId);
712 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
713 // for a userId u, a taskId can only be in the range
714 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
715 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
716 int candidateTaskId = currentTaskId;
717 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
718 INVALID_STACK_ID) != null) {
719 candidateTaskId++;
720 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
721 // Wrap around as there will be smaller task ids that are available now.
722 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700723 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800724 if (candidateTaskId == currentTaskId) {
725 // Something wrong!
726 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
727 throw new IllegalStateException("Cannot get an available task id."
728 + " Reached limit of " + MAX_TASK_IDS_PER_USER
729 + " running tasks per user.");
730 }
731 }
732 mCurTaskIdForUser.put(userId, candidateTaskId);
733 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700734 }
735
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800737 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700738 if (stack == null) {
739 return null;
740 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700741 ActivityRecord resumedActivity = stack.mResumedActivity;
742 if (resumedActivity == null || resumedActivity.app == null) {
743 resumedActivity = stack.mPausingActivity;
744 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700745 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700746 }
747 }
748 return resumedActivity;
749 }
750
Dianne Hackbornff072722014-09-24 10:56:28 -0700751 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700752 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
755 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
757 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700758 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 continue;
760 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700761 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 if (hr != null) {
763 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
764 && processName.equals(hr.processName)) {
765 try {
George Mount2c92c972014-03-20 09:38:23 -0700766 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 didSomething = true;
768 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700769 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 Slog.w(TAG, "Exception in new application when starting activity "
771 + hr.intent.getComponent().flattenToShortString(), e);
772 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700773 }
Craig Mautner20e72272013-04-01 13:45:53 -0700774 }
Craig Mautner20e72272013-04-01 13:45:53 -0700775 }
776 }
777 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700778 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700779 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700780 }
Craig Mautner20e72272013-04-01 13:45:53 -0700781 return didSomething;
782 }
783
784 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
786 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700789 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 continue;
791 }
792 final ActivityRecord resumedActivity = stack.mResumedActivity;
793 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700794 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800795 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 return false;
797 }
Craig Mautner20e72272013-04-01 13:45:53 -0700798 }
799 }
800 return true;
801 }
802
Craig Mautnerde4ef022013-04-07 19:01:33 -0700803 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
805 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
807 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700808 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700810 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 return false;
812 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700813 }
814 }
815 }
816 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700817 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
819 mLastFocusedStack + " to=" + mFocusedStack);
820 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700821 return true;
822 }
823
824 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800825 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800826 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
827 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
829 final ActivityStack stack = stacks.get(stackNdx);
830 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800831 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700832 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800833 return false;
834 }
835 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700837 }
838 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800839 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700840 }
841
Craig Mautner2acc3892013-09-23 10:28:14 -0700842 /**
843 * Pause all activities in either all of the stacks or just the back stacks.
844 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700845 * @return true if any activity was paused as a result of this call.
846 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700847 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700848 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800849 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
850 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
852 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700853 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700854 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700856 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
857 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700859 }
860 }
861 return someActivityPaused;
862 }
863
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700865 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800866 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
867 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
869 final ActivityStack stack = stacks.get(stackNdx);
870 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700871 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700873 Slog.d(TAG_STATES,
874 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 pausing = false;
876 } else {
877 return false;
878 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700879 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700880 }
881 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700882 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700883 }
884
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700885 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
886 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500887 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
889 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
891 final ActivityStack stack = stacks.get(stackNdx);
892 if (stack.mResumedActivity != null &&
893 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700894 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800895 }
896 }
897 }
898 }
899
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700900 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700901 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700902 }
903
904 void sendWaitingVisibleReportLocked(ActivityRecord r) {
905 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700906 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700907 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700908 if (w.who == null) {
909 changed = true;
910 w.timeout = false;
911 if (r != null) {
912 w.who = new ComponentName(r.info.packageName, r.info.name);
913 }
914 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
915 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700916 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700917 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700918 if (changed) {
919 mService.notifyAll();
920 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700921 }
922
923 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
924 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700925 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700926 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700927 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700928 if (w.who == null) {
929 changed = true;
930 w.timeout = timeout;
931 if (r != null) {
932 w.who = new ComponentName(r.info.packageName, r.info.name);
933 }
934 w.thisTime = thisTime;
935 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (changed) {
939 mService.notifyAll();
940 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700941 }
942
Craig Mautner29219d92013-04-16 20:19:12 -0700943 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800944 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700945 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700946 if (r != null) {
947 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700948 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700949
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800951 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
953 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800954 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700955 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700956 if (r != null) {
957 return r;
958 }
959 }
960 }
961 return null;
962 }
963
Dianne Hackborn09233282014-04-30 11:33:59 -0700964 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700965 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
967 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
972 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700973 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700975 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700976 }
977 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700978
979 // The lists are already sorted from most recent to oldest. Just pull the most recent off
980 // each list and add it to list. Stop when all lists are empty or maxNum reached.
981 while (maxNum > 0) {
982 long mostRecentActiveTime = Long.MIN_VALUE;
983 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 final int numTaskLists = runningTaskLists.size();
985 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
986 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700987 if (!stackTaskList.isEmpty()) {
988 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
989 if (lastActiveTime > mostRecentActiveTime) {
990 mostRecentActiveTime = lastActiveTime;
991 selectedStackList = stackTaskList;
992 }
993 }
994 }
995 if (selectedStackList != null) {
996 list.add(selectedStackList.remove(0));
997 --maxNum;
998 } else {
999 break;
1000 }
1001 }
Craig Mautner20e72272013-04-01 13:45:53 -07001002 }
1003
Todd Kennedy7440f172015-12-09 14:31:22 -08001004 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1005 ProfilerInfo profilerInfo) {
1006 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 if (aInfo != null) {
1008 // Store the found target back into the intent, because now that
1009 // we have it we never want to do this again. For example, if the
1010 // user navigates back to this point in the history, we should
1011 // always restart the exact same activity.
1012 intent.setComponent(new ComponentName(
1013 aInfo.applicationInfo.packageName, aInfo.name));
1014
1015 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001016 if (!aInfo.processName.equals("system")) {
1017 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 mService.setDebugApp(aInfo.processName, true, false);
1019 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020
Man Caocfa78b22015-06-11 20:14:34 -07001021 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1022 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1023 }
1024
1025 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001026 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 }
1028 }
1029 }
1030 return aInfo;
1031 }
1032
Todd Kennedy7440f172015-12-09 14:31:22 -08001033 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1034 try {
1035 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1036 PackageManager.MATCH_DEFAULT_ONLY
1037 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1038 } catch (RemoteException e) {
1039 }
1040 return null;
1041 }
1042
1043 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1044 ProfilerInfo profilerInfo, int userId) {
1045 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1046 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1047 }
1048
Craig Mautner2420ead2013-04-01 17:13:20 -07001049 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001050 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001051 throws RemoteException {
1052
Craig Mautner2568c3a2015-03-26 14:22:34 -07001053 if (andResume) {
1054 r.startFreezingScreenLocked(app, 0);
1055 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001056
Craig Mautner2568c3a2015-03-26 14:22:34 -07001057 // schedule launch ticks to collect information about slow apps.
1058 r.startLaunchTickingLocked();
1059 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001060
1061 // Have the window manager re-evaluate the orientation of
1062 // the screen based on the new activity order. Note that
1063 // as a result of this, it can call back into the activity
1064 // manager with a new orientation. We don't care about that,
1065 // because the activity is not currently running so we are
1066 // just restarting it anyway.
1067 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001069 mService.mConfiguration,
1070 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001071 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001072 }
1073
1074 r.app = app;
1075 app.waitingToKill = null;
1076 r.launchCount++;
1077 r.lastLaunchTime = SystemClock.uptimeMillis();
1078
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001079 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001080
1081 int idx = app.activities.indexOf(r);
1082 if (idx < 0) {
1083 app.activities.add(r);
1084 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001085 mService.updateLruProcessLocked(app, true, null);
1086 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001087
Craig Mautner15df08a2015-04-01 12:17:18 -07001088 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001089 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1090 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001091 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001092 }
1093
1094 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001095 try {
1096 if (app.thread == null) {
1097 throw new RemoteException();
1098 }
1099 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001100 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001101 if (andResume) {
1102 results = r.results;
1103 newIntents = r.newIntents;
1104 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001105 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1106 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1107 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001108 if (andResume) {
1109 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1110 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001111 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001113 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001114 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001117 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001118 r.sleeping = false;
1119 r.forceNewConfig = false;
1120 mService.showAskCompatModeDialogLocked(r);
1121 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001122 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001123 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1124 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1125 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001126 final String profileFile = mService.mProfileFile;
1127 if (profileFile != null) {
1128 ParcelFileDescriptor profileFd = mService.mProfileFd;
1129 if (profileFd != null) {
1130 try {
1131 profileFd = profileFd.dup();
1132 } catch (IOException e) {
1133 if (profileFd != null) {
1134 try {
1135 profileFd.close();
1136 } catch (IOException o) {
1137 }
1138 profileFd = null;
1139 }
1140 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001141 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001142
1143 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1144 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 }
1146 }
1147 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001148
Craig Mautner2568c3a2015-03-26 14:22:34 -07001149 if (andResume) {
1150 app.hasShownUi = true;
1151 app.pendingUiClean = true;
1152 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001153 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001155 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001156 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001157 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001158 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001159
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001160 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 // This may be a heavy-weight process! Note that the package
1162 // manager will ensure that only activity can run in the main
1163 // process of the .apk, which is the only thing that will be
1164 // considered heavy-weight.
1165 if (app.processName.equals(app.info.packageName)) {
1166 if (mService.mHeavyWeightProcess != null
1167 && mService.mHeavyWeightProcess != app) {
1168 Slog.w(TAG, "Starting new heavy weight process " + app
1169 + " when already running "
1170 + mService.mHeavyWeightProcess);
1171 }
1172 mService.mHeavyWeightProcess = app;
1173 Message msg = mService.mHandler.obtainMessage(
1174 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1175 msg.obj = r;
1176 mService.mHandler.sendMessage(msg);
1177 }
1178 }
1179
1180 } catch (RemoteException e) {
1181 if (r.launchFailed) {
1182 // This is the second time we failed -- finish activity
1183 // and give up.
1184 Slog.e(TAG, "Second failure launching "
1185 + r.intent.getComponent().flattenToShortString()
1186 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001187 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1189 "2nd-crash", false);
1190 return false;
1191 }
1192
1193 // This is the first time we failed -- restart process and
1194 // retry.
1195 app.activities.remove(r);
1196 throw e;
1197 }
1198
1199 r.launchFailed = false;
1200 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001201 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 }
1203
1204 if (andResume) {
1205 // As part of the process of launching, ActivityThread also performs
1206 // a resume.
1207 stack.minimalResumeActivityLocked(r);
1208 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001209 // This activity is not starting in the resumed state... which should look like we asked
1210 // it to pause+stop (but remain visible), and it has done so and reported back the
1211 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001212 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001213 "Moving to PAUSED: " + r + " (starting in paused state)");
1214 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 }
1216
1217 // Launch the new version setup screen if needed. We do this -after-
1218 // launching the initial activity (that is, home), so that it can have
1219 // a chance to initialize itself while in the background, making the
1220 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001221 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 mService.startSetupActivityLocked();
1223 }
1224
Dianne Hackborn465fa392014-09-14 14:21:18 -07001225 // Update any services we are bound to that might care about whether
1226 // their client may have activities.
1227 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1228
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 return true;
1230 }
1231
Craig Mautnere79d42682013-04-01 19:01:53 -07001232 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001233 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001234 // Is this activity's application already running?
1235 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001236 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001237
1238 r.task.stack.setLaunchTime(r);
1239
1240 if (app != null && app.thread != null) {
1241 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001242 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1243 || !"android".equals(r.info.packageName)) {
1244 // Don't add this if it is a platform component that is marked
1245 // to run in multiple processes, because this is actually
1246 // part of the framework so doesn't make sense to track as a
1247 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001248 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1249 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001250 }
George Mount2c92c972014-03-20 09:38:23 -07001251 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001252 return;
1253 } catch (RemoteException e) {
1254 Slog.w(TAG, "Exception when starting activity "
1255 + r.intent.getComponent().flattenToShortString(), e);
1256 }
1257
1258 // If a dead object exception was thrown -- fall through to
1259 // restart the application.
1260 }
1261
1262 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001263 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001264 }
1265
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001266 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001267 String resultWho, int requestCode, int callingPid, int callingUid,
1268 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1269 ActivityRecord resultRecord, ActivityStack resultStack) {
1270 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1271 callingUid);
1272 if (startAnyPerm == PERMISSION_GRANTED) {
1273 return true;
1274 }
1275 final int componentRestriction = getComponentRestrictionForCallingPackage(
1276 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1277 final int actionRestriction = getActionRestrictionForCallingPackage(
1278 intent.getAction(), callingPackage, callingPid, callingUid);
1279 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1280 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1281 if (resultRecord != null) {
1282 resultStack.sendActivityResultLocked(-1,
1283 resultRecord, resultWho, requestCode,
1284 Activity.RESULT_CANCELED, null);
1285 }
1286 final String msg;
1287 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1288 msg = "Permission Denial: starting " + intent.toString()
1289 + " from " + callerApp + " (pid=" + callingPid
1290 + ", uid=" + callingUid + ")" + " with revoked permission "
1291 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1292 } else if (!aInfo.exported) {
1293 msg = "Permission Denial: starting " + intent.toString()
1294 + " from " + callerApp + " (pid=" + callingPid
1295 + ", uid=" + callingUid + ")"
1296 + " not exported from uid " + aInfo.applicationInfo.uid;
1297 } else {
1298 msg = "Permission Denial: starting " + intent.toString()
1299 + " from " + callerApp + " (pid=" + callingPid
1300 + ", uid=" + callingUid + ")"
1301 + " requires " + aInfo.permission;
1302 }
1303 Slog.w(TAG, msg);
1304 throw new SecurityException(msg);
1305 }
1306
1307 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1308 final String message = "Appop Denial: starting " + intent.toString()
1309 + " from " + callerApp + " (pid=" + callingPid
1310 + ", uid=" + callingUid + ")"
1311 + " requires " + AppOpsManager.permissionToOp(
1312 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1313 Slog.w(TAG, message);
1314 return false;
1315 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1316 final String message = "Appop Denial: starting " + intent.toString()
1317 + " from " + callerApp + " (pid=" + callingPid
1318 + ", uid=" + callingUid + ")"
1319 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1320 Slog.w(TAG, message);
1321 return false;
1322 }
1323 return true;
1324 }
1325
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001326 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001327 final long identity = Binder.clearCallingIdentity();
1328 try {
1329 return UserManager.get(mService.mContext).getUserInfo(userId);
1330 } finally {
1331 Binder.restoreCallingIdentity(identity);
1332 }
1333 }
1334
Svet Ganov99b60432015-06-27 13:15:22 -07001335 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001336 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001337 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1338 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001339 == PackageManager.PERMISSION_DENIED) {
1340 return ACTIVITY_RESTRICTION_PERMISSION;
1341 }
1342
Christopher Tateff7add02015-08-17 10:23:22 -07001343 if (activityInfo.permission == null) {
1344 return ACTIVITY_RESTRICTION_NONE;
1345 }
1346
Svet Ganov99b60432015-06-27 13:15:22 -07001347 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1348 if (opCode == AppOpsManager.OP_NONE) {
1349 return ACTIVITY_RESTRICTION_NONE;
1350 }
1351
1352 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1353 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001354 if (!ignoreTargetSecurity) {
1355 return ACTIVITY_RESTRICTION_APPOP;
1356 }
Svet Ganov99b60432015-06-27 13:15:22 -07001357 }
1358
1359 return ACTIVITY_RESTRICTION_NONE;
1360 }
1361
Svetoslav7008b512015-06-24 18:47:07 -07001362 private int getActionRestrictionForCallingPackage(String action,
1363 String callingPackage, int callingPid, int callingUid) {
1364 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001365 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001366 }
1367
1368 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1369 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001370 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001371 }
1372
1373 final PackageInfo packageInfo;
1374 try {
1375 packageInfo = mService.mContext.getPackageManager()
1376 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1377 } catch (PackageManager.NameNotFoundException e) {
1378 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001379 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001380 }
1381
1382 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001383 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001384 }
1385
1386 if (mService.checkPermission(permission, callingPid, callingUid) ==
1387 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001388 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001389 }
1390
1391 final int opCode = AppOpsManager.permissionToOpCode(permission);
1392 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001393 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001394 }
1395
1396 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1397 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001398 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001399 }
1400
Svet Ganov99b60432015-06-27 13:15:22 -07001401 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001402 }
1403
Wale Ogunwaled046a012015-12-24 13:05:59 -08001404 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001405 if (r == null) {
1406 // Not sure what you are trying to do, but it is not going to work...
1407 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001408 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001409 final TaskRecord task = r.task;
1410 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001411 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001412 return false;
1413 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001414 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001415 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001416 }
1417
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001418 void setLaunchSource(int uid) {
1419 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1420 }
1421
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001422 void acquireLaunchWakelock() {
1423 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1424 throw new IllegalStateException("Calling must be system uid");
1425 }
1426 mLaunchingActivity.acquire();
1427 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1428 // To be safe, don't allow the wake lock to be held for too long.
1429 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1430 }
1431 }
1432
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001433 /**
1434 * Called when the frontmost task is idle.
1435 * @return the state of mService.mBooting before this was called.
1436 */
1437 private boolean checkFinishBootingLocked() {
1438 final boolean booting = mService.mBooting;
1439 boolean enableScreen = false;
1440 mService.mBooting = false;
1441 if (!mService.mBooted) {
1442 mService.mBooted = true;
1443 enableScreen = true;
1444 }
1445 if (booting || enableScreen) {
1446 mService.postFinishBooting(booting, enableScreen);
1447 }
1448 return booting;
1449 }
1450
Craig Mautnerf3333272013-04-22 10:55:53 -07001451 // Checked.
1452 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1453 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001454 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001455
Craig Mautnerf3333272013-04-22 10:55:53 -07001456 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001457 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001458 int NS = 0;
1459 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001460 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001461 boolean activityRemoved = false;
1462
Wale Ogunwale7d701172015-03-11 15:36:30 -07001463 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001464 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001465 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1466 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001467 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1468 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001469 if (fromTimeout) {
1470 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001471 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001472
1473 // This is a hack to semi-deal with a race condition
1474 // in the client where it can be constructed with a
1475 // newer configuration from when we asked it to launch.
1476 // We'll update with whatever configuration it now says
1477 // it used to launch.
1478 if (config != null) {
1479 r.configuration = config;
1480 }
1481
1482 // We are now idle. If someone is waiting for a thumbnail from
1483 // us, we can now deliver.
1484 r.idle = true;
1485
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001486 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001487 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001488 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001489 }
1490 }
1491
1492 if (allResumedActivitiesIdle()) {
1493 if (r != null) {
1494 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001495 }
1496
1497 if (mLaunchingActivity.isHeld()) {
1498 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1499 if (VALIDATE_WAKE_LOCK_CALLER &&
1500 Binder.getCallingUid() != Process.myUid()) {
1501 throw new IllegalStateException("Calling must be system uid");
1502 }
1503 mLaunchingActivity.release();
1504 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001505 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001506 }
1507
1508 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001509 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001510 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001511 if ((NF = mFinishingActivities.size()) > 0) {
1512 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001513 mFinishingActivities.clear();
1514 }
1515
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001517 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 mStartingUsers.clear();
1519 }
1520
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 // Stop any activities that are scheduled to do so but have been
1522 // waiting for the next one to start.
1523 for (int i = 0; i < NS; i++) {
1524 r = stops.get(i);
1525 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001526 if (stack != null) {
1527 if (r.finishing) {
1528 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1529 } else {
1530 stack.stopActivityLocked(r);
1531 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001532 }
1533 }
1534
1535 // Finish any activities that are scheduled to do so but have been
1536 // waiting for the next one to start.
1537 for (int i = 0; i < NF; i++) {
1538 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001539 final ActivityStack stack = r.task.stack;
1540 if (stack != null) {
1541 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1542 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001543 }
1544
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001545 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001546 // Complete user switch
1547 if (startingUsers != null) {
1548 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001549 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001550 }
1551 }
1552 // Complete starting up of background users
1553 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001554 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001555 mStartingBackgroundUsers.clear();
1556 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001557 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001558 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001559 }
1560 }
1561
1562 mService.trimApplications();
1563 //dump();
1564 //mWindowManager.dump();
1565
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001567 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001568 }
1569
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001570 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001571 }
1572
Craig Mautner8e569572013-10-11 17:36:59 -07001573 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001574 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001575 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1576 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001577 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1578 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1579 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001580 }
Craig Mautner19091252013-10-05 00:03:53 -07001581 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001582 }
1583
1584 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001585 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1586 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1588 stacks.get(stackNdx).closeSystemDialogsLocked();
1589 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001590 }
1591 }
1592
Craig Mautner93529a42013-10-04 15:03:13 -07001593 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001594 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001595 }
1596
Craig Mautner8d341ef2013-03-26 09:03:27 -07001597 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001598 * Update the last used stack id for non-current user (current user's last
1599 * used stack is the focused stack)
1600 */
1601 void updateUserStackLocked(int userId, ActivityStack stack) {
1602 if (userId != mCurrentUser) {
1603 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1604 }
1605 }
1606
1607 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001608 * @return true if some activity was finished (or would have finished if doit were true).
1609 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001610 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1611 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001612 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001616 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001617 if (stack.finishDisabledPackageActivitiesLocked(
1618 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001619 didSomething = true;
1620 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001621 }
1622 }
1623 return didSomething;
1624 }
1625
Dianne Hackborna413dc02013-07-12 12:02:55 -07001626 void updatePreviousProcessLocked(ActivityRecord r) {
1627 // Now that this process has stopped, we may want to consider
1628 // it to be the previous app to try to keep around in case
1629 // the user wants to return to it.
1630
1631 // First, found out what is currently the foreground app, so that
1632 // we don't blow away the previous app if this activity is being
1633 // hosted by the process that is actually still the foreground.
1634 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001635 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1636 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001637 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1638 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001639 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001640 if (stack.mResumedActivity != null) {
1641 fgApp = stack.mResumedActivity.app;
1642 } else if (stack.mPausingActivity != null) {
1643 fgApp = stack.mPausingActivity.app;
1644 }
1645 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001646 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001647 }
1648 }
1649
1650 // Now set this one as the previous process, only if that really
1651 // makes sense to.
1652 if (r.app != null && fgApp != null && r.app != fgApp
1653 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001654 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001655 mService.mPreviousProcess = r.app;
1656 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1657 }
1658 }
1659
Wale Ogunwaled046a012015-12-24 13:05:59 -08001660 boolean resumeFocusedStackTopActivityLocked() {
1661 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001662 }
1663
Wale Ogunwaled046a012015-12-24 13:05:59 -08001664 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001665 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001666 if (targetStack != null && isFocusedStack(targetStack)) {
1667 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001668 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001669 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1670 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001671 }
1672
Adrian Roos20d7df32016-01-12 18:59:43 +01001673 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1674 TaskRecord finishedTask = null;
1675 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1677 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001678 final int numStacks = stacks.size();
1679 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1680 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001681 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1682 if (stack == focusedStack || finishedTask == null) {
1683 finishedTask = t;
1684 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001685 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001686 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001687 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001688 }
1689
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001690 void finishVoiceTask(IVoiceInteractionSession session) {
1691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1692 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1693 final int numStacks = stacks.size();
1694 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1695 final ActivityStack stack = stacks.get(stackNdx);
1696 stack.finishVoiceTask(session);
1697 }
1698 }
1699 }
1700
Chong Zhang280d3322015-11-03 17:27:26 -08001701 void findTaskToMoveToFrontLocked(
1702 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001703 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1704 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001705 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001706 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1707 // Caller wants the home activity moved with it. To accomplish this,
1708 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001709 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001710 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001711 if (task.stack == null) {
1712 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1713 + task + " to front. Stack is null");
1714 return;
1715 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001716
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001717 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001718 int stackId = options.getLaunchStackId();
1719 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001720 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001721 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001722 if (stackId == INVALID_STACK_ID) {
1723 stackId = task.getLaunchStackId();
1724 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001725 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001726 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1727 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1728 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001729 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1730 // still need moveTaskToFrontLocked() below for any transition settings.
1731 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001732 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1733 resizeStackLocked(stackId, bounds,
1734 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1735 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1736 } else {
1737 // WM resizeTask must be done after the task is moved to the correct stack,
1738 // because Task's setBounds() also updates dim layer's bounds, but that has
1739 // dependency on the stack.
1740 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1741 false /* relayout */, false /* forced */);
1742 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001743 }
1744 }
1745
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001746 final ActivityRecord r = task.getTopActivity();
1747 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1748 r == null ? null : r.appTimeTracker, reason);
1749
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001750 if (DEBUG_STACK) Slog.d(TAG_STACK,
1751 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001752 }
1753
Wale Ogunwale854809c2015-12-27 16:18:19 -08001754 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001755 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001756 // window management or is launching into the pinned stack.
1757 if (!options.hasLaunchBounds()) {
1758 return false;
1759 }
1760 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1761 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001762 }
1763
Craig Mautner967212c2013-04-13 21:10:58 -07001764 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001765 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001766 }
1767
1768 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001769 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1770 if (activityContainer != null) {
1771 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001772 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001773 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001774 return null;
1775 }
1776 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001777 }
1778
Craig Mautner967212c2013-04-13 21:10:58 -07001779 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001780 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1782 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001783 }
1784 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001785 }
1786
Craig Mautner4a1cb222013-12-04 16:14:06 -08001787 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001788 ActivityRecord homeActivity = getHomeActivity();
1789 if (homeActivity != null) {
1790 return homeActivity.appToken;
1791 }
1792 return null;
1793 }
1794
1795 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001796 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001797 }
1798
1799 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001800 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1801 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1802 final TaskRecord task = tasks.get(taskNdx);
1803 if (task.isHomeTask()) {
1804 final ArrayList<ActivityRecord> activities = task.mActivities;
1805 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1806 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001807 if (r.isHomeActivity()
1808 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001809 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001810 }
1811 }
1812 }
1813 }
1814 return null;
1815 }
1816
Chong Zhangb15758a2015-11-17 12:12:03 -08001817 /**
1818 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1819 * the docked stack itself, or if it's side-by-side to the docked stack.
1820 */
1821 boolean isStackDockedInEffect(int stackId) {
1822 return stackId == DOCKED_STACK_ID ||
1823 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1824 }
1825
Todd Kennedyca4d8422015-01-15 15:19:22 -08001826 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001827 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001828 ActivityContainer activityContainer =
1829 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001830 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001831 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1832 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001833 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001834 return activityContainer;
1835 }
1836
Craig Mautner34b73df2014-01-12 21:11:08 -08001837 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001838 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1839 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1840 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001841 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1842 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001843 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001844 }
1845 }
1846
Craig Mautner95da1082014-02-24 17:54:35 -08001847 void deleteActivityContainer(IActivityContainer container) {
1848 ActivityContainer activityContainer = (ActivityContainer)container;
1849 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001850 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1851 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001852 final int stackId = activityContainer.mStackId;
1853 mActivityContainers.remove(stackId);
1854 mWindowManager.removeStack(stackId);
1855 }
1856 }
1857
Jorim Jaggidc249c42015-12-15 14:57:31 -08001858 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1859 boolean preserveWindows, boolean allowResizeInDockedMode) {
1860 if (stackId == DOCKED_STACK_ID) {
1861 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1862 preserveWindows);
1863 return;
1864 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001865 final ActivityStack stack = getStack(stackId);
1866 if (stack == null) {
1867 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1868 return;
1869 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001870
Jorim Jaggidc249c42015-12-15 14:57:31 -08001871 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001872 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1873 // stack size changing so things don't get out of sync.
1874 return;
1875 }
1876
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001877 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001878 mWindowManager.deferSurfaceLayout();
1879 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001880 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1881 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001882 } finally {
1883 mWindowManager.continueSurfaceLayout();
1884 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001885 }
1886 }
1887
Jorim Jaggidc249c42015-12-15 14:57:31 -08001888 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1889 Rect tempTaskInsetBounds) {
1890 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1891 // The bounds passed in corresponds to the fullscreen bounds which we normally
1892 // represent with null. Go ahead and set it to null so that all tasks configuration
1893 // can have the right fullscreen state.
1894 bounds = null;
1895 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001896 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001897
1898 mTmpBounds.clear();
1899 mTmpConfigs.clear();
1900 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001901 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001902 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001903 final TaskRecord task = tasks.get(i);
1904 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001905 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1906 // For freeform stack we don't adjust the size of the tasks to match that
1907 // of the stack, but we do try to make sure the tasks are still contained
1908 // with the bounds of the stack.
1909 tempRect2.set(task.mBounds);
1910 fitWithinBounds(tempRect2, bounds);
1911 task.updateOverrideConfiguration(tempRect2);
1912 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001913 task.updateOverrideConfiguration(
1914 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001915 }
1916 }
1917
1918 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1919 mTmpBounds.put(task.taskId, task.mBounds);
1920 if (tempTaskInsetBounds != null) {
1921 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1922 }
1923 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001924
1925 // We might trigger a configuration change. Save the current task bounds for freezing.
1926 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001927 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1928 mTmpBounds, mTmpInsetBounds);
1929 stack.setBounds(bounds);
1930 }
1931
1932 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1933 boolean preserveWindows) {
1934 if (r == null) {
1935 return;
1936 }
1937 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1938 preserveWindows);
1939 // And we need to make sure at this point that all other activities
1940 // are made visible with the correct configuration.
1941 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1942 if (!updated) {
1943 resumeFocusedStackTopActivityLocked();
1944 }
1945 }
1946
1947 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1948 Rect tempDockedTaskInsetBounds,
1949 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1950 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1951 if (stack == null) {
1952 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1953 return;
1954 }
1955
1956 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1957 mWindowManager.deferSurfaceLayout();
1958 try {
1959 ActivityRecord r = stack.topRunningActivityLocked();
1960 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1961 tempDockedTaskInsetBounds);
1962
1963 if (stack.mFullscreen) {
1964 // The dock stack went fullscreen which is kinda like dismissing it.
1965 // In this case we make all other static stacks fullscreen and move all
1966 // docked stack tasks to the fullscreen stack.
1967 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1968 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1969 resizeStackLocked(i, null, null, null, preserveWindows,
1970 true /* allowResizeInDockedMode */);
1971 }
1972 }
1973
1974 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1975 final int count = tasks.size();
1976 for (int i = 0; i < count; i++) {
1977 moveTaskToStackLocked(tasks.get(i).taskId,
1978 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1979 false /* animate */);
1980 }
1981
1982 // stack shouldn't contain anymore activities, so nothing to resume.
1983 r = null;
1984 } else {
1985 // Docked stacks occupy a dedicated region on screen so the size of all other
1986 // static stacks need to be adjusted so they don't overlap with the docked stack.
1987 // We get the bounds to use from window manager which has been adjusted for any
1988 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08001989 mWindowManager.getStackDockedModeBounds(
1990 HOME_STACK_ID, tempRect, true /* ignoreVisibilityOnKeyguardShowing */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001991 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1992 if (StackId.isResizeableByDockedStack(i)) {
1993 ActivityStack otherStack = getStack(i);
1994 if (otherStack != null) {
1995 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1996 tempOtherTaskInsetBounds, preserveWindows,
1997 true /* allowResizeInDockedMode */);
1998 }
1999 }
2000 }
2001 }
2002 ensureConfigurationAndResume(stack, r, preserveWindows);
2003 } finally {
2004 mWindowManager.continueSurfaceLayout();
2005 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2006 }
2007
2008 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2009 tempDockedTaskBounds != null
2010 || tempDockedTaskInsetBounds != null
2011 || tempOtherTaskBounds != null
2012 || tempOtherTaskInsetBounds != null);
2013 }
2014
Chong Zhangf596cd52016-01-05 13:42:44 -08002015 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002016 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002017 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002018 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002019 }
2020
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002021 adjustForMinimalTaskDimensions(task, bounds);
2022
Chong Zhang87b21722015-09-21 15:39:51 -07002023 // If this is a forced resize, let it go through even if the bounds is not changing,
2024 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002025 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002026 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002027 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002028 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002029 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002030 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002031
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002032 if (!mWindowManager.isValidTaskId(task.taskId)) {
2033 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002034 // All we can do for now is update the bounds so it can be used when the task is
2035 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002036 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002037 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2038 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002039 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002040 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002041 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002042 }
2043
Chong Zhang6de2ae82015-09-30 18:25:21 -07002044 // Do not move the task to another stack here.
2045 // This method assumes that the task is already placed in the right stack.
2046 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002047
Chong Zhang6de2ae82015-09-30 18:25:21 -07002048 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002049
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002050 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002051 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002052 // way and the activity was kept the way it was. If it's false, it means the activity had
2053 // to be relaunched due to configuration change.
2054 boolean kept = true;
2055 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002056 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002057 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002058 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002059 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002060 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002061 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002062 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002063 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002064 }
2065 }
2066 }
Chong Zhang87b21722015-09-21 15:39:51 -07002067 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002068
2069 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002070 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002071 }
2072
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002073 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2074 if (bounds == null) {
2075 return;
2076 }
2077 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2078 : task.mMinimalSize;
2079 final boolean adjustWidth = minimalSize > bounds.width();
2080 final boolean adjustHeight = minimalSize > bounds.height();
2081 if (!(adjustWidth || adjustHeight)) {
2082 return;
2083 }
2084 Rect taskBounds = task.mBounds;
2085 if (adjustWidth) {
2086 if (taskBounds != null && bounds.right == taskBounds.right) {
2087 bounds.left = bounds.right - minimalSize;
2088 } else {
2089 // Either left bounds match, or neither match, or the previous bounds were
2090 // fullscreen and we default to keeping left.
2091 bounds.right = bounds.left + minimalSize;
2092 }
2093 }
2094 if (adjustHeight) {
2095 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2096 bounds.top = bounds.bottom - minimalSize;
2097 } else {
2098 // Either top bounds match, or neither match, or the previous bounds were
2099 // fullscreen and we default to keeping top.
2100 bounds.bottom = bounds.top + minimalSize;
2101 }
2102 }
2103 }
2104
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002105 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002106 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2107 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002108 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002109 }
2110
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002111 ActivityContainer activityContainer = new ActivityContainer(stackId);
2112 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002113 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002114 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002115 }
2116
2117 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002118 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002119 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2120 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002121 break;
2122 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002123 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002124 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002125 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002126 }
2127
Chong Zhang5dcb2752015-08-18 13:50:26 -07002128 /**
2129 * Restores a recent task to a stack
2130 * @param task The recent task to be restored.
2131 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002132 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002133 * @return true if the task has been restored successfully.
2134 */
2135 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2136 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002137 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002138 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2139 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2140 // Put it in the fullscreen stack.
2141 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002142 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002143
Wale Ogunwale706ed792015-08-02 10:29:44 -07002144 if (task.stack != null) {
2145 // Task has already been restored once. See if we need to do anything more
2146 if (task.stack.mStackId == stackId) {
2147 // Nothing else to do since it is already restored in the right stack.
2148 return true;
2149 }
2150 // Remove current stack association, so we can re-associate the task with the
2151 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002152 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002153 }
2154
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002155 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002156 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002157
2158 if (stack == null) {
2159 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002160 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2161 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002162 return false;
2163 }
2164
Wale Ogunwale5f986092015-12-04 15:35:38 -08002165 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002166 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2167 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002168 final ArrayList<ActivityRecord> activities = task.mActivities;
2169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002170 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002171 }
2172 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002173 }
2174
Wale Ogunwale040b4702015-08-06 18:10:50 -07002175 /**
2176 * Moves the specified task record to the input stack id.
2177 * WARNING: This method performs an unchecked/raw move of the task and
2178 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002179 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002180 * @param task Task to move.
2181 * @param stackId Id of stack to move task to.
2182 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002183 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002184 * @param reason Reason the task is been moved.
2185 * @return The stack the task was moved to.
2186 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002187 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002188 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2189 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002190 final ActivityStack prevStack = task.stack;
2191 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2192 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2193 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2194 // Whenever we are moving the top activity from the front stack we want to make sure to move
2195 // the stack to the front.
2196 final boolean wasFront = isFrontStack(prevStack)
2197 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002198
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002199 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002200
2201 if (stackId == DOCKED_STACK_ID && resizeMode == RESIZE_MODE_UNRESIZEABLE) {
2202 // We don't allow moving a unresizeable task to the docked stack since the docked
2203 // stack is used for split-screen mode and will cause things like the docked divider to
2204 // show up. We instead leave the task in its current stack or move it to the fullscreen
2205 // stack if it isn't currently in a stack.
2206 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2207 // TODO: display toast that activity doesn't support multi-window mode.
2208 Slog.w(TAG, "Can not move unresizeable task=" + task
2209 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2210 }
2211
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002212 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2213 // if a docked stack is created below which will lead to the stack we are moving from and
2214 // its resizeable tasks being resized.
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002215 task.mResizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002216 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002217 task.mResizeMode = resizeMode;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002218 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002219 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002220
2221 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002222 // move focus to the new stack by moving the stack to the front.
2223 stack.moveToFrontAndResumeStateIfNeeded(
2224 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002225
Wale Ogunwale040b4702015-08-06 18:10:50 -07002226 return stack;
2227 }
2228
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002229 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002230 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002231 final TaskRecord task = anyTaskForIdLocked(taskId);
2232 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002233 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002234 return;
2235 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002236
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002237 if (task.stack != null && task.stack.mStackId == stackId) {
2238 // You are already in the right stack silly...
2239 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2240 return;
2241 }
2242
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002243 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002244 final boolean mightReplaceWindow =
2245 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2246 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002247 // We are about to relaunch the activity because its configuration changed due to
2248 // being maximized, i.e. size change. The activity will first remove the old window
2249 // and then add a new one. This call will tell window manager about this, so it can
2250 // preserve the old window until the new one is drawn. This prevents having a gap
2251 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002252 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002253 // Note here we always set the replacing window first, as the flags might be needed
2254 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002255 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002256 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002257 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2258 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale513346d2016-01-27 10:55:01 -08002259 stackId = stack.mStackId;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002260
Jorim Jaggi55387522015-11-24 18:21:10 -08002261 if (!animate) {
2262 stack.mNoAnimActivities.add(topActivity);
2263 }
2264
Chong Zhangf596cd52016-01-05 13:42:44 -08002265 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002266 // Make sure the task has the appropriate bounds/size for the stack it is in.
2267 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002268 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002269 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2270 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002271 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002272 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002273 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002274 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2275 }
2276
2277 if (mightReplaceWindow) {
2278 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2279 // window), we need to clear the replace window settings. Otherwise, we schedule a
2280 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002281 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002282 }
2283
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002284 // The task might have already been running and its visibility needs to be synchronized with
2285 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002286 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002287 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002288
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002289 if (!task.isResizeable() && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002290 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002291 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002292 }
2293
Wale Ogunwale079a0042015-10-24 11:44:07 -07002294 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2295 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2296 if (stack == null) {
2297 throw new IllegalArgumentException(
2298 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2299 }
2300
2301 final ActivityRecord r = stack.topRunningActivityLocked();
2302 if (r == null) {
2303 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2304 + " in stack=" + stack);
2305 return false;
2306 }
2307
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002308 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002309 Slog.w(TAG,
2310 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002311 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002312 return false;
2313 }
2314
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002315 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", null);
2316 mWindowManager.animateResizePinnedStack(bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002317 return true;
2318 }
2319
2320 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002321 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002322 if (task.mActivities.size() == 1) {
2323 // 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 -08002324 // stack without re-parenting the activity in a different task.
2325 moveTaskToStackLocked(
2326 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002327 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002328 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2329 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002330 }
2331
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002332 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002333 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2334 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002335 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002336
2337 // The task might have already been running and its visibility needs to be synchronized with
2338 // the visibility of the stack / windows.
2339 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002340 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002341
2342 if (stackId == PINNED_STACK_ID) {
2343 mService.notifyActivityPinnedLocked();
2344 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002345 }
2346
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002347 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2348 final TaskRecord task = anyTaskForIdLocked(taskId);
2349 if (task == null) {
2350 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2351 return;
2352 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002353 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2354
2355 task.updateOverrideConfigurationForStack(stack);
2356
2357 mWindowManager.positionTaskInStack(
2358 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002359 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002360 // The task might have already been running and its visibility needs to be synchronized with
2361 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002362 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002363 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002364 }
2365
Craig Mautnerac6f8432013-07-17 13:24:59 -07002366 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002367 mTmpFindTaskResult.r = null;
2368 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002369 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002370 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2371 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002372 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2373 final ActivityStack stack = stacks.get(stackNdx);
2374 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002375 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002376 continue;
2377 }
2378 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002379 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2380 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002381 continue;
2382 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002383 stack.findTaskLocked(r, mTmpFindTaskResult);
2384 // It is possible to have task in multiple stacks with the same root affinity.
2385 // If the match we found was based on root affinity we keep on looking to see if
2386 // there is a better match in another stack. We eventually return the match based
2387 // on root affinity if we don't find a better match.
2388 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2389 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002390 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002391 }
2392 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002393 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2394 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002395 }
2396
2397 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002398 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2399 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2401 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2402 if (ar != null) {
2403 return ar;
2404 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002405 }
2406 }
2407 return null;
2408 }
2409
Craig Mautner8d341ef2013-03-26 09:03:27 -07002410 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002411 scheduleSleepTimeout();
2412 if (!mGoingToSleep.isHeld()) {
2413 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002414 if (mLaunchingActivity.isHeld()) {
2415 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2416 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002417 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002418 mLaunchingActivity.release();
2419 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002420 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002421 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002422 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002423 }
2424
2425 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002426 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002427
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002428 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002429 final long endTime = System.currentTimeMillis() + timeout;
2430 while (true) {
2431 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002432 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2433 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002434 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2435 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2436 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002437 }
2438 if (cantShutdown) {
2439 long timeRemaining = endTime - System.currentTimeMillis();
2440 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002441 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002442 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002443 } catch (InterruptedException e) {
2444 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002445 } else {
2446 Slog.w(TAG, "Activity manager shutdown timed out");
2447 timedout = true;
2448 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002449 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002450 } else {
2451 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002452 }
2453 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002454
2455 // Force checkReadyForSleep to complete.
2456 mSleepTimeout = true;
2457 checkReadyForSleepLocked();
2458
Craig Mautner8d341ef2013-03-26 09:03:27 -07002459 return timedout;
2460 }
2461
2462 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002463 removeSleepTimeouts();
2464 if (mGoingToSleep.isHeld()) {
2465 mGoingToSleep.release();
2466 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002467 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2468 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002469 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2470 final ActivityStack stack = stacks.get(stackNdx);
2471 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002472 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002473 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002474 }
Craig Mautner5314a402013-09-26 12:40:16 -07002475 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002476 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002477 mGoingToSleepActivities.clear();
2478 }
2479
2480 void activitySleptLocked(ActivityRecord r) {
2481 mGoingToSleepActivities.remove(r);
2482 checkReadyForSleepLocked();
2483 }
2484
2485 void checkReadyForSleepLocked() {
2486 if (!mService.isSleepingOrShuttingDown()) {
2487 // Do not care.
2488 return;
2489 }
2490
2491 if (!mSleepTimeout) {
2492 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2496 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2497 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002498 }
2499
2500 if (mStoppingActivities.size() > 0) {
2501 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002502 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002503 + mStoppingActivities.size() + " activities");
2504 scheduleIdleLocked();
2505 dontSleep = true;
2506 }
2507
2508 if (mGoingToSleepActivities.size() > 0) {
2509 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002510 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002511 + mGoingToSleepActivities.size() + " activities");
2512 dontSleep = true;
2513 }
2514
2515 if (dontSleep) {
2516 return;
2517 }
2518 }
2519
Craig Mautnere0a38842013-12-16 16:14:02 -08002520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002522 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2523 stacks.get(stackNdx).goToSleep();
2524 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002525 }
2526
2527 removeSleepTimeouts();
2528
2529 if (mGoingToSleep.isHeld()) {
2530 mGoingToSleep.release();
2531 }
2532 if (mService.mShuttingDown) {
2533 mService.notifyAll();
2534 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002535 }
2536
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002537 boolean reportResumedActivityLocked(ActivityRecord r) {
2538 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002539 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002540 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002541 }
2542 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002543 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002544 mWindowManager.executeAppTransition();
2545 return true;
2546 }
2547 return false;
2548 }
2549
Craig Mautner8d341ef2013-03-26 09:03:27 -07002550 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002551 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2552 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002553 int stackNdx = stacks.size() - 1;
2554 while (stackNdx >= 0) {
2555 stacks.get(stackNdx).handleAppCrashLocked(app);
2556 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002557 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002558 }
2559 }
2560
Jose Lima4b6c6692014-08-12 17:41:12 -07002561 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002562 final ActivityStack stack = r.task.stack;
2563 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002564 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2565 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002566 return false;
2567 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002568 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002569 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2570 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002571
2572 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002573 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002574 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002575 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002576 return true;
2577 }
2578
2579 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002580 if (visible && top.fullscreen) {
2581 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002582 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2583 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2584 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2585 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002586 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002587 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2588 // Only the activity set as currently visible behind should actively reset its
2589 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002590 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2591 "requestVisibleBehind: returning visible=" + visible
2592 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2593 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002594 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002595 }
2596
Jose Lima4b6c6692014-08-12 17:41:12 -07002597 stack.setVisibleBehindActivity(visible ? r : null);
2598 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002599 // Make the activity immediately above r opaque.
2600 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2601 if (next != null) {
2602 mService.convertFromTranslucent(next.appToken);
2603 }
2604 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002605 if (top.app != null && top.app.thread != null) {
2606 // Notify the top app of the change.
2607 try {
2608 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2609 } catch (RemoteException e) {
2610 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002611 }
2612 return true;
2613 }
2614
Craig Mautnerbb742462014-07-07 15:28:55 -07002615 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2616 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2617 r.mLaunchTaskBehind = false;
2618 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002619 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002620 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002621 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002622 mWindowManager.setAppVisibility(r.appToken, false);
2623 }
2624
2625 void scheduleLaunchTaskBehindComplete(IBinder token) {
2626 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2627 }
2628
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002629 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2630 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002631 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002632 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2633 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002634 final int topStackNdx = stacks.size() - 1;
2635 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2636 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002637 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002638 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002640 }
2641
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002642 void invalidateTaskLayers() {
2643 mTaskLayersChanged = true;
2644 }
2645
2646 void rankTaskLayersIfNeeded() {
2647 if (!mTaskLayersChanged) {
2648 return;
2649 }
2650 mTaskLayersChanged = false;
2651 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2652 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2653 int baseLayer = 0;
2654 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2655 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2656 }
2657 }
2658 }
2659
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002660 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2661 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2662 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2663 final int topStackNdx = stacks.size() - 1;
2664 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2665 final ActivityStack stack = stacks.get(stackNdx);
2666 stack.clearOtherAppTimeTrackers(except);
2667 }
2668 }
2669 }
2670
Craig Mautner8d341ef2013-03-26 09:03:27 -07002671 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2673 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002674 final int numStacks = stacks.size();
2675 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2676 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002677 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
2680 }
2681
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002682 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2683 // Examine all activities currently running in the process.
2684 TaskRecord firstTask = null;
2685 // Tasks is non-null only if two or more tasks are found.
2686 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002687 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2688 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002689 ActivityRecord r = app.activities.get(i);
2690 // First, if we find an activity that is in the process of being destroyed,
2691 // then we just aren't going to do anything for now; we want things to settle
2692 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002693 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002694 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002695 return;
2696 }
2697 // Don't consider any activies that are currently not in a state where they
2698 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002699 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2700 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002701 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002702 continue;
2703 }
2704 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002705 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002706 + " from " + r);
2707 if (firstTask == null) {
2708 firstTask = r.task;
2709 } else if (firstTask != r.task) {
2710 if (tasks == null) {
2711 tasks = new ArraySet<>();
2712 tasks.add(firstTask);
2713 }
2714 tasks.add(r.task);
2715 }
2716 }
2717 }
2718 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002719 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002720 return;
2721 }
2722 // If we have activities in multiple tasks that are in a position to be destroyed,
2723 // let's iterate through the tasks and release the oldest one.
2724 final int numDisplays = mActivityDisplays.size();
2725 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2727 // Step through all stacks starting from behind, to hit the oldest things first.
2728 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2729 final ActivityStack stack = stacks.get(stackNdx);
2730 // Try to release activities in this stack; if we manage to, we are done.
2731 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2732 return;
2733 }
2734 }
2735 }
2736 }
2737
Amith Yamasani37a40c22015-06-17 13:25:42 -07002738 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002739 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002740 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002741 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002742
Craig Mautner858d8a62013-04-23 17:08:34 -07002743 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2745 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002746 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002747 final ActivityStack stack = stacks.get(stackNdx);
2748 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002749 TaskRecord task = stack.topTask();
2750 if (task != null) {
2751 mWindowManager.moveTaskToTop(task.taskId);
2752 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002753 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002754 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002755
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002756 ActivityStack stack = getStack(restoreStackId);
2757 if (stack == null) {
2758 stack = mHomeStack;
2759 }
2760 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002761 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002762 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 } else {
2764 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002765 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002766 }
Craig Mautner93529a42013-10-04 15:03:13 -07002767 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002768 }
2769
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002770 /**
2771 * Add background users to send boot completed events to.
2772 * @param userId The user being started in the background
2773 * @param uss The state object for the user.
2774 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002775 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002776 mStartingBackgroundUsers.add(uss);
2777 }
2778
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002779 /** Checks whether the userid is a profile of the current user. */
2780 boolean isCurrentProfileLocked(int userId) {
2781 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002782 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002783 }
2784
Chong Zhang45c25ce2015-08-10 22:18:26 -07002785 /** Checks whether the activity should be shown for current user. */
2786 boolean okToShowLocked(ActivityRecord r) {
2787 return r != null && (isCurrentProfileLocked(r.userId)
2788 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2789 }
2790
Craig Mautnerde4ef022013-04-07 19:01:33 -07002791 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002792 ArrayList<ActivityRecord> stops = null;
2793
2794 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002795 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2796 ActivityRecord s = mStoppingActivities.get(activityNdx);
2797 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002798 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002799 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2800 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002801 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002802 if (s.finishing) {
2803 // If this activity is finishing, it is sitting on top of
2804 // everyone else but we now know it is no longer needed...
2805 // so get rid of it. Otherwise, we need to go through the
2806 // normal flow and hide it once we determine that it is
2807 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002808 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002809 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002810 }
2811 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002812 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002813 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002814 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002815 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002816 }
2817 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002818 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002819 }
2820 }
2821
2822 return stops;
2823 }
2824
Craig Mautnercf910b02013-04-23 11:23:27 -07002825 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002826 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2827 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2828 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2829 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002830 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002832 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 if (r == null) Slog.e(TAG,
2834 "validateTop...: null top activity, stack=" + stack);
2835 else {
2836 final ActivityRecord pausing = stack.mPausingActivity;
2837 if (pausing != null && pausing == r) Slog.e(TAG,
2838 "validateTop...: top stack has pausing activity r=" + r
2839 + " state=" + state);
2840 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2841 "validateTop...: activity in front not resumed r=" + r
2842 + " state=" + state);
2843 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002844 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002845 final ActivityRecord resumed = stack.mResumedActivity;
2846 if (resumed != null && resumed == r) Slog.e(TAG,
2847 "validateTop...: back stack has resumed activity r=" + r
2848 + " state=" + state);
2849 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2850 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002851 }
2852 }
2853 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002854 }
2855
Craig Mautnere0570202015-05-13 13:06:11 -07002856 private String lockTaskModeToString() {
2857 switch (mLockTaskModeState) {
2858 case LOCK_TASK_MODE_LOCKED:
2859 return "LOCKED";
2860 case LOCK_TASK_MODE_PINNED:
2861 return "PINNED";
2862 case LOCK_TASK_MODE_NONE:
2863 return "NONE";
2864 default: return "unknown=" + mLockTaskModeState;
2865 }
2866 }
2867
Craig Mautner27084302013-03-25 08:05:25 -07002868 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002869 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002870 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002871 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002872 pw.print(prefix);
2873 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002874 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002875 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002876 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2877 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2878 if (packages.size() > 0) {
2879 pw.println(" mLockTaskPackages (userId:packages)=");
2880 for (int i = 0; i < packages.size(); ++i) {
2881 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2882 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2883 }
2884 }
2885 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002886 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887
Craig Mautner20e72272013-04-01 13:45:53 -07002888 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002889 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002890 }
2891
Dianne Hackborn390517b2013-05-30 15:03:32 -07002892 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2893 boolean needSep, String prefix) {
2894 if (activity != null) {
2895 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2896 if (needSep) {
2897 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002898 }
2899 pw.print(prefix);
2900 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002901 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002902 }
2903 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002904 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002905 }
2906
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2908 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002909 boolean printed = false;
2910 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002911 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2912 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002913 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002914 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002915 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 final ActivityStack stack = stacks.get(stackNdx);
2918 StringBuilder stackHeader = new StringBuilder(128);
2919 stackHeader.append(" Stack #");
2920 stackHeader.append(stack.mStackId);
2921 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002922 stackHeader.append("\n");
2923 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2924 stackHeader.append("\n");
2925 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2927 needSep, stackHeader.toString());
2928 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2929 !dumpAll, false, dumpPackage, true,
2930 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931
Craig Mautner4a1cb222013-12-04 16:14:06 -08002932 needSep = printed;
2933 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2934 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002935 if (pr) {
2936 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002938 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2940 " mResumedActivity: ");
2941 if (pr) {
2942 printed = true;
2943 needSep = false;
2944 }
2945 if (dumpAll) {
2946 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2947 " mLastPausedActivity: ");
2948 if (pr) {
2949 printed = true;
2950 needSep = true;
2951 }
2952 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2953 needSep, " mLastNoHistoryActivity: ");
2954 }
2955 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
2957 }
2958
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002959 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2960 false, dumpPackage, true, " Activities waiting to finish:", null);
2961 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2962 false, dumpPackage, true, " Activities waiting to stop:", null);
2963 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2964 false, dumpPackage, true, " Activities waiting for another to become visible:",
2965 null);
2966 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2967 false, dumpPackage, true, " Activities waiting to sleep:", null);
2968 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2969 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002970
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002971 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002972 }
2973
Dianne Hackborn390517b2013-05-30 15:03:32 -07002974 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002976 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002978 String innerPrefix = null;
2979 String[] args = null;
2980 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002981 for (int i=list.size()-1; i>=0; i--) {
2982 final ActivityRecord r = list.get(i);
2983 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2984 continue;
2985 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002986 if (innerPrefix == null) {
2987 innerPrefix = prefix + " ";
2988 args = new String[0];
2989 }
2990 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002991 final boolean full = !brief && (complete || !r.isInHistory());
2992 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002993 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002994 needNL = false;
2995 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002996 if (header1 != null) {
2997 pw.println(header1);
2998 header1 = null;
2999 }
3000 if (header2 != null) {
3001 pw.println(header2);
3002 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003003 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003004 if (lastTask != r.task) {
3005 lastTask = r.task;
3006 pw.print(prefix);
3007 pw.print(full ? "* " : " ");
3008 pw.println(lastTask);
3009 if (full) {
3010 lastTask.dump(pw, prefix + " ");
3011 } else if (complete) {
3012 // Complete + brief == give a summary. Isn't that obvious?!?
3013 if (lastTask.intent != null) {
3014 pw.print(prefix); pw.print(" ");
3015 pw.println(lastTask.intent.toInsecureStringWithClip());
3016 }
3017 }
3018 }
3019 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3020 pw.print(" #"); pw.print(i); pw.print(": ");
3021 pw.println(r);
3022 if (full) {
3023 r.dump(pw, innerPrefix);
3024 } else if (complete) {
3025 // Complete + brief == give a summary. Isn't that obvious?!?
3026 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3027 if (r.app != null) {
3028 pw.print(innerPrefix); pw.println(r.app);
3029 }
3030 }
3031 if (client && r.app != null && r.app.thread != null) {
3032 // flush anything that is already in the PrintWriter since the thread is going
3033 // to write to the file descriptor directly
3034 pw.flush();
3035 try {
3036 TransferPipe tp = new TransferPipe();
3037 try {
3038 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3039 r.appToken, innerPrefix, args);
3040 // Short timeout, since blocking here can
3041 // deadlock with the application.
3042 tp.go(fd, 2000);
3043 } finally {
3044 tp.kill();
3045 }
3046 } catch (IOException e) {
3047 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3048 } catch (RemoteException e) {
3049 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3050 }
3051 needNL = true;
3052 }
3053 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003054 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003055 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003056
Craig Mautnerf3333272013-04-22 10:55:53 -07003057 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003058 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3059 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003060 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3061 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003062 }
3063
3064 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003065 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003066 }
3067
3068 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003069 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3070 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003071 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3072 }
3073
Craig Mautner05d29032013-05-03 13:40:13 -07003074 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003075 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3076 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3077 }
Craig Mautner05d29032013-05-03 13:40:13 -07003078 }
3079
Craig Mautner0eea92c2013-05-16 13:35:39 -07003080 void removeSleepTimeouts() {
3081 mSleepTimeout = false;
3082 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3083 }
3084
3085 final void scheduleSleepTimeout() {
3086 removeSleepTimeouts();
3087 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3088 }
3089
Craig Mautner4a1cb222013-12-04 16:14:06 -08003090 @Override
3091 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003092 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3094 }
3095
3096 @Override
3097 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003098 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003099 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3100 }
3101
3102 @Override
3103 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003104 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003105 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3106 }
3107
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003108 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003109 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003110 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003111 newDisplay = mActivityDisplays.get(displayId) == null;
3112 if (newDisplay) {
3113 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003114 if (activityDisplay.mDisplay == null) {
3115 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3116 return;
3117 }
Craig Mautner4504de52013-12-20 09:06:56 -08003118 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003119 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003120 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003121 }
Craig Mautner4504de52013-12-20 09:06:56 -08003122 if (newDisplay) {
3123 mWindowManager.onDisplayAdded(displayId);
3124 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003125 }
3126
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003127 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3128 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3129 return;
3130 }
3131 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3132 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3133 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3134 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3135 }
3136
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003137 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003138 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003139 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3140 if (activityDisplay != null) {
3141 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003142 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003143 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003145 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 }
3147 }
3148 mWindowManager.onDisplayRemoved(displayId);
3149 }
3150
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003151 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003152 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003153 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3154 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 // TODO: Update the bounds.
3156 }
3157 }
3158 mWindowManager.onDisplayChanged(displayId);
3159 }
3160
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003161 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 StackInfo info = new StackInfo();
3163 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3164 info.displayId = Display.DEFAULT_DISPLAY;
3165 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003166 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167
3168 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3169 final int numTasks = tasks.size();
3170 int[] taskIds = new int[numTasks];
3171 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003172 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003173 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 for (int i = 0; i < numTasks; ++i) {
3175 final TaskRecord task = tasks.get(i);
3176 taskIds[i] = task.taskId;
3177 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3178 : task.realActivity != null ? task.realActivity.flattenToString()
3179 : task.getTopActivity() != null ? task.getTopActivity().packageName
3180 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003181 taskBounds[i] = new Rect();
3182 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003183 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 }
3185 info.taskIds = taskIds;
3186 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003187 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003188 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189 return info;
3190 }
3191
3192 StackInfo getStackInfoLocked(int stackId) {
3193 ActivityStack stack = getStack(stackId);
3194 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003195 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003196 }
3197 return null;
3198 }
3199
3200 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003201 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3203 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003205 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 }
3207 }
3208 return list;
3209 }
3210
Craig Mautner15df08a2015-04-01 12:17:18 -07003211 TaskRecord getLockedTaskLocked() {
3212 final int top = mLockTaskModeTasks.size() - 1;
3213 if (top >= 0) {
3214 return mLockTaskModeTasks.get(top);
3215 }
3216 return null;
3217 }
3218
3219 boolean isLockedTask(TaskRecord task) {
3220 return mLockTaskModeTasks.contains(task);
3221 }
3222
3223 boolean isLastLockedTask(TaskRecord task) {
3224 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3225 }
3226
3227 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003228 if (!mLockTaskModeTasks.remove(task)) {
3229 return;
3230 }
3231 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3232 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003233 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003234 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3235 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003236 final Message lockTaskMsg = Message.obtain();
3237 lockTaskMsg.arg1 = task.userId;
3238 lockTaskMsg.what = LOCK_TASK_END_MSG;
3239 mHandler.sendMessage(lockTaskMsg);
3240 }
3241 }
3242
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003243 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003244 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003245 }
3246
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003247 void showLockTaskToast() {
3248 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003249 }
3250
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003251 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3252 if (mLockTaskModeTasks.contains(task)) {
3253 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3254 }
3255 }
3256
Craig Mautner432f64e2015-05-20 14:59:57 -07003257 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3258 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003259 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003260 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003261 final TaskRecord lockedTask = getLockedTaskLocked();
3262 if (lockedTask != null) {
3263 removeLockedTaskLocked(lockedTask);
3264 if (!mLockTaskModeTasks.isEmpty()) {
3265 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003266 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3267 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003268 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003269 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003270 return;
3271 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003272 }
Craig Mautnere0570202015-05-13 13:06:11 -07003273 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3274 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003275 return;
3276 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003277
3278 // Should have already been checked, but do it again.
3279 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003280 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3281 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003282 return;
3283 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003284 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003285 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003286 return;
3287 }
3288
3289 if (mLockTaskModeTasks.isEmpty()) {
3290 // First locktask.
3291 final Message lockTaskMsg = Message.obtain();
3292 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3293 lockTaskMsg.arg1 = task.userId;
3294 lockTaskMsg.what = LOCK_TASK_START_MSG;
3295 lockTaskMsg.arg2 = lockTaskModeState;
3296 mHandler.sendMessage(lockTaskMsg);
3297 }
3298 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003299 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3300 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003301 mLockTaskModeTasks.remove(task);
3302 mLockTaskModeTasks.add(task);
3303
3304 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003305 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003306 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003307
3308 if (andResume) {
3309 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003310 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003311 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003312 }
3313
3314 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003315 return isLockTaskModeViolation(task, false);
3316 }
3317
3318 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3319 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003320 return false;
3321 }
3322 final int lockTaskAuth = task.mLockTaskAuth;
3323 switch (lockTaskAuth) {
3324 case LOCK_TASK_AUTH_DONT_LOCK:
3325 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003326 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003327 case LOCK_TASK_AUTH_LAUNCHABLE:
3328 case LOCK_TASK_AUTH_WHITELISTED:
3329 return false;
3330 case LOCK_TASK_AUTH_PINNABLE:
3331 // Pinnable tasks can't be launched on top of locktask tasks.
3332 return !mLockTaskModeTasks.isEmpty();
3333 default:
3334 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3335 return true;
3336 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003337 }
3338
Craig Mautner15df08a2015-04-01 12:17:18 -07003339 void onLockTaskPackagesUpdatedLocked() {
3340 boolean didSomething = false;
3341 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3342 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003343 final boolean wasWhitelisted =
3344 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3345 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003346 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003347 final boolean isWhitelisted =
3348 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3349 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3350 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003351 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003352 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3353 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003354 removeLockedTaskLocked(lockedTask);
3355 lockedTask.performClearTaskLocked();
3356 didSomething = true;
3357 }
3358 }
3359 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3360 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3361 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3362 final ActivityStack stack = stacks.get(stackNdx);
3363 stack.onLockTaskPackagesUpdatedLocked();
3364 }
3365 }
Craig Mautnere0570202015-05-13 13:06:11 -07003366 final ActivityRecord r = topRunningActivityLocked();
3367 final TaskRecord task = r != null ? r.task : null;
3368 if (mLockTaskModeTasks.isEmpty() && task != null
3369 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3370 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003371 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3372 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3373 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3374 false);
3375 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003376 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003377 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003378 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003379 }
3380 }
3381
Benjamin Franz43261142015-02-11 15:59:44 +00003382 int getLockTaskModeState() {
3383 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003384 }
3385
Jorim Jaggife89d122015-12-22 16:28:44 +01003386 void activityRelaunchedLocked(IBinder token) {
3387 mWindowManager.notifyAppRelaunchingFinished(token);
3388 }
3389
3390 void activityRelaunchingLocked(ActivityRecord r) {
3391 mWindowManager.notifyAppRelaunching(r.appToken);
3392 }
3393
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003394 void logStackState() {
3395 mActivityMetricsLogger.logWindowState();
3396 }
3397
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003398 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003399
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003400 public ActivityStackSupervisorHandler(Looper looper) {
3401 super(looper);
3402 }
3403
Craig Mautnerf3333272013-04-22 10:55:53 -07003404 void activityIdleInternal(ActivityRecord r) {
3405 synchronized (mService) {
3406 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3407 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003408 }
3409
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003410 @Override
3411 public void handleMessage(Message msg) {
3412 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003413 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003414 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3415 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003416 if (mService.mDidDexOpt) {
3417 mService.mDidDexOpt = false;
3418 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3419 nmsg.obj = msg.obj;
3420 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3421 return;
3422 }
3423 // We don't at this point know if the activity is fullscreen,
3424 // so we need to be conservative and assume it isn't.
3425 activityIdleInternal((ActivityRecord)msg.obj);
3426 } break;
3427 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003428 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003429 activityIdleInternal((ActivityRecord)msg.obj);
3430 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003431 case RESUME_TOP_ACTIVITY_MSG: {
3432 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003433 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003434 }
3435 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003436 case SLEEP_TIMEOUT_MSG: {
3437 synchronized (mService) {
3438 if (mService.isSleepingOrShuttingDown()) {
3439 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3440 mSleepTimeout = true;
3441 checkReadyForSleepLocked();
3442 }
3443 }
3444 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003445 case LAUNCH_TIMEOUT_MSG: {
3446 if (mService.mDidDexOpt) {
3447 mService.mDidDexOpt = false;
3448 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3449 return;
3450 }
3451 synchronized (mService) {
3452 if (mLaunchingActivity.isHeld()) {
3453 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3454 if (VALIDATE_WAKE_LOCK_CALLER
3455 && Binder.getCallingUid() != Process.myUid()) {
3456 throw new IllegalStateException("Calling must be system uid");
3457 }
3458 mLaunchingActivity.release();
3459 }
3460 }
3461 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003463 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 } break;
3465 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003466 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 } break;
3468 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003469 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003471 case CONTAINER_CALLBACK_VISIBILITY: {
3472 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003473 final IActivityContainerCallback callback = container.mCallback;
3474 if (callback != null) {
3475 try {
3476 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3477 } catch (RemoteException e) {
3478 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003479 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003480 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003481 case LOCK_TASK_START_MSG: {
3482 // When lock task starts, we disable the status bars.
3483 try {
Jason Monk62515be2014-05-21 16:06:19 -04003484 if (mLockTaskNotify == null) {
3485 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003486 }
Jason Monk62515be2014-05-21 16:06:19 -04003487 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003488 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003489 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003490 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003491 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003492 flags = StatusBarManager.DISABLE_MASK
3493 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003494 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003495 flags = StatusBarManager.DISABLE_MASK
3496 & (~StatusBarManager.DISABLE_BACK)
3497 & (~StatusBarManager.DISABLE_HOME)
3498 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003499 }
3500 getStatusBarService().disable(flags, mToken,
3501 mService.mContext.getPackageName());
3502 }
3503 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003504 if (getDevicePolicyManager() != null) {
3505 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003506 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003507 }
justinzhang5286d3f2014-05-12 17:06:01 -04003508 } catch (RemoteException ex) {
3509 throw new RuntimeException(ex);
3510 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003511 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003512 case LOCK_TASK_END_MSG: {
3513 // When lock task ends, we enable the status bars.
3514 try {
Jason Monk62515be2014-05-21 16:06:19 -04003515 if (getStatusBarService() != null) {
3516 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3517 mService.mContext.getPackageName());
3518 }
3519 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003520 if (getDevicePolicyManager() != null) {
3521 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3522 msg.arg1);
3523 }
Jason Monk62515be2014-05-21 16:06:19 -04003524 if (mLockTaskNotify == null) {
3525 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3526 }
3527 mLockTaskNotify.show(false);
3528 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003529 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003530 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003531 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003532 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003533 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003534 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003535 new LockPatternUtils(mService.mContext)
3536 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003537 }
3538 } catch (SettingNotFoundException e) {
3539 // No setting, don't lock.
3540 }
justinzhang5286d3f2014-05-12 17:06:01 -04003541 } catch (RemoteException ex) {
3542 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003543 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003544 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003545 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003546 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003547 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3548 if (mLockTaskNotify == null) {
3549 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3550 }
3551 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3552 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003553 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3554 final ActivityContainer container = (ActivityContainer) msg.obj;
3555 final IActivityContainerCallback callback = container.mCallback;
3556 if (callback != null) {
3557 try {
3558 callback.onAllActivitiesComplete(container.asBinder());
3559 } catch (RemoteException e) {
3560 }
3561 }
3562 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003563 case LAUNCH_TASK_BEHIND_COMPLETE: {
3564 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003565 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003566 if (r != null) {
3567 handleLaunchTaskBehindCompleteLocked(r);
3568 }
3569 }
3570 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003571
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003572 }
3573 }
3574 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003575
Ying Wangb081a592014-04-22 15:20:16 -07003576 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003577 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3578 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003580 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003581 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003582 ActivityRecord mParentActivity = null;
3583 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003584
Craig Mautnere3a00d72014-04-16 08:31:19 -07003585 boolean mVisible = true;
3586
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003588 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003590 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3591 final static int CONTAINER_STATE_NO_SURFACE = 1;
3592 final static int CONTAINER_STATE_FINISHING = 2;
3593 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3594
3595 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 synchronized (mService) {
3597 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003598 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003599 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003600 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003602 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003603
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003604 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003605 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003606 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003607 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003608 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003609 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610 }
3611
3612 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003613 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003614 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003615 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3616 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003617 return;
3618 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003619 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
3621 }
3622
3623 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003624 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003625 synchronized (mService) {
3626 if (mActivityDisplay != null) {
3627 return mActivityDisplay.mDisplayId;
3628 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003629 }
3630 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003631 }
3632
Jeff Brownca9bc702014-02-11 14:32:56 -08003633 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003634 public int getStackId() {
3635 synchronized (mService) {
3636 return mStackId;
3637 }
3638 }
3639
3640 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003641 public boolean injectEvent(InputEvent event) {
3642 final long origId = Binder.clearCallingIdentity();
3643 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003644 synchronized (mService) {
3645 if (mActivityDisplay != null) {
3646 return mInputManagerInternal.injectInputEvent(event,
3647 mActivityDisplay.mDisplayId,
3648 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3649 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003650 }
3651 return false;
3652 } finally {
3653 Binder.restoreCallingIdentity(origId);
3654 }
3655 }
3656
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003657 @Override
3658 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003659 synchronized (mService) {
3660 if (mContainerState == CONTAINER_STATE_FINISHING) {
3661 return;
3662 }
3663 mContainerState = CONTAINER_STATE_FINISHING;
3664
Craig Mautnerd163e752014-06-13 17:18:47 -07003665 long origId = Binder.clearCallingIdentity();
3666 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003667 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003668 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003669 } finally {
3670 Binder.restoreCallingIdentity(origId);
3671 }
3672 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003673 }
3674
Craig Mautner60257702014-09-17 15:02:33 -07003675 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003676 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003677 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003678 if (mActivityDisplay != null) {
3679 mActivityDisplay.detachActivitiesLocked(mStack);
3680 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003681 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003682 }
3683 }
3684
3685 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003686 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003687 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003688 }
3689
3690 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003691 public final int startActivityIntentSender(IIntentSender intentSender)
3692 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003693 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3694
3695 if (!(intentSender instanceof PendingIntentRecord)) {
3696 throw new IllegalArgumentException("Bad PendingIntent object");
3697 }
3698
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003699 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003700 Binder.getCallingUid(), mCurrentUser, false,
3701 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003702
3703 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3704 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3705 pendingIntent.key.requestResolvedType);
3706
3707 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3708 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3709 }
3710
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003711 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003712 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003713 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003714 throw new SecurityException(
3715 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3716 }
3717 }
3718
Craig Mautnerdf88d732014-01-27 09:21:32 -08003719 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003720 public IBinder asBinder() {
3721 return this;
3722 }
3723
Craig Mautner4504de52013-12-20 09:06:56 -08003724 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003725 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003726 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003727 }
3728
Craig Mautner4a1cb222013-12-04 16:14:06 -08003729 ActivityStackSupervisor getOuter() {
3730 return ActivityStackSupervisor.this;
3731 }
3732
Craig Mautnerd163e752014-06-13 17:18:47 -07003733 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003734 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003735 }
3736
Craig Mautner6985bad2014-04-21 15:22:06 -07003737 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003738 void setVisible(boolean visible) {
3739 if (mVisible != visible) {
3740 mVisible = visible;
3741 if (mCallback != null) {
3742 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3743 0 /* unused */, this).sendToTarget();
3744 }
3745 }
3746 }
3747
Craig Mautner6985bad2014-04-21 15:22:06 -07003748 void setDrawn() {
3749 }
3750
Craig Mautner1b4bf852014-05-26 15:06:32 -07003751 // You can always start a new task on a regular ActivityStack.
3752 boolean isEligibleForNewTasks() {
3753 return true;
3754 }
3755
Craig Mautnerd163e752014-06-13 17:18:47 -07003756 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003757 detachLocked();
3758 deleteActivityContainer(this);
3759 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003760 }
3761
Craig Mautner34b73df2014-01-12 21:11:08 -08003762 @Override
3763 public String toString() {
3764 return mIdString + (mActivityDisplay == null ? "N" : "A");
3765 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003766 }
3767
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003768 private class VirtualActivityContainer extends ActivityContainer {
3769 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003770 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003771
3772 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3773 super(getNextStackId());
3774 mParentActivity = parent;
3775 mCallback = callback;
3776 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003777 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003778 }
3779
3780 @Override
3781 public void setSurface(Surface surface, int width, int height, int density) {
3782 super.setSurface(surface, width, height, density);
3783
3784 synchronized (mService) {
3785 final long origId = Binder.clearCallingIdentity();
3786 try {
3787 setSurfaceLocked(surface, width, height, density);
3788 } finally {
3789 Binder.restoreCallingIdentity(origId);
3790 }
3791 }
3792 }
3793
3794 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3795 if (mContainerState == CONTAINER_STATE_FINISHING) {
3796 return;
3797 }
3798 VirtualActivityDisplay virtualActivityDisplay =
3799 (VirtualActivityDisplay) mActivityDisplay;
3800 if (virtualActivityDisplay == null) {
3801 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003802 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003803 mActivityDisplay = virtualActivityDisplay;
3804 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003805 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003806 }
3807
3808 if (mSurface != null) {
3809 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003810 }
3811
Craig Mautner6985bad2014-04-21 15:22:06 -07003812 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003813 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003814 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003815 } else {
3816 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003817 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003818 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003819 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003820 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003821 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003822
Craig Mautnerd163e752014-06-13 17:18:47 -07003823 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003824
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003825 if (DEBUG_STACK) Slog.d(TAG_STACK,
3826 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003827 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003828
Craig Mautner6985bad2014-04-21 15:22:06 -07003829 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003830 boolean isAttachedLocked() {
3831 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003832 }
3833
3834 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003835 void setDrawn() {
3836 synchronized (mService) {
3837 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003838 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003839 }
3840 }
3841
Craig Mautner1b4bf852014-05-26 15:06:32 -07003842 // Never start a new task on an ActivityView if it isn't explicitly specified.
3843 @Override
3844 boolean isEligibleForNewTasks() {
3845 return false;
3846 }
3847
Craig Mautnerd163e752014-06-13 17:18:47 -07003848 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003849 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003850 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3851 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3852 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3853 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3854 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003855 }
3856 }
3857
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3859 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003860 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003861 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003862 int mDisplayId;
3863 Display mDisplay;
3864 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003865
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3867 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003868 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869
Jose Lima4b6c6692014-08-12 17:41:12 -07003870 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003871
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003872 ActivityDisplay() {
3873 }
Craig Mautner4504de52013-12-20 09:06:56 -08003874
Craig Mautner1a70a162014-09-13 12:09:31 -07003875 // After instantiation, check that mDisplay is not null before using this. The alternative
3876 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003877 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003878 final Display display = mDisplayManager.getDisplay(displayId);
3879 if (display == null) {
3880 return;
3881 }
3882 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003883 }
3884
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003885 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003886 mDisplay = display;
3887 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003889 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003891 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003892 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003893 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3894 + " onTop=" + onTop);
3895 if (onTop) {
3896 mStacks.add(stack);
3897 } else {
3898 mStacks.add(0, stack);
3899 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003900 }
3901
Craig Mautnere0a38842013-12-16 16:14:02 -08003902 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003903 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003904 + " from displayId=" + mDisplayId);
3905 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 }
3907
Jose Lima4b6c6692014-08-12 17:41:12 -07003908 void setVisibleBehindActivity(ActivityRecord r) {
3909 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003910 }
3911
Jose Lima4b6c6692014-08-12 17:41:12 -07003912 boolean hasVisibleBehindActivity() {
3913 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003914 }
3915
Craig Mautner34b73df2014-01-12 21:11:08 -08003916 @Override
3917 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003918 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3919 }
3920 }
3921
3922 class VirtualActivityDisplay extends ActivityDisplay {
3923 VirtualDisplay mVirtualDisplay;
3924
Craig Mautner6985bad2014-04-21 15:22:06 -07003925 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003926 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003927 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3928 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3929 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3930 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003931
3932 init(mVirtualDisplay.getDisplay());
3933
3934 mWindowManager.handleDisplayAdded(mDisplayId);
3935 }
3936
3937 void setSurface(Surface surface) {
3938 if (mVirtualDisplay != null) {
3939 mVirtualDisplay.setSurface(surface);
3940 }
3941 }
3942
3943 @Override
3944 void detachActivitiesLocked(ActivityStack stack) {
3945 super.detachActivitiesLocked(stack);
3946 if (mVirtualDisplay != null) {
3947 mVirtualDisplay.release();
3948 mVirtualDisplay = null;
3949 }
3950 }
3951
3952 @Override
3953 public String toString() {
3954 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003955 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003956 }
Jose Lima58e66d62014-05-27 20:00:27 -07003957
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003958 /**
3959 * Adjust bounds to stay within stack bounds.
3960 *
3961 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3962 * that keep them unchanged, but be contained within the stack bounds.
3963 *
3964 * @param bounds Bounds to be adjusted.
3965 * @param stackBounds Bounds within which the other bounds should remain.
3966 */
3967 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3968 if (stackBounds == null || stackBounds.contains(bounds)) {
3969 return;
3970 }
3971
3972 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3973 final int maxRight = stackBounds.right
3974 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3975 int horizontalDiff = stackBounds.left - bounds.left;
3976 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3977 || (bounds.left + horizontalDiff >= maxRight)) {
3978 horizontalDiff = maxRight - bounds.left;
3979 }
3980 bounds.left += horizontalDiff;
3981 bounds.right += horizontalDiff;
3982 }
3983
3984 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3985 final int maxBottom = stackBounds.bottom
3986 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3987 int verticalDiff = stackBounds.top - bounds.top;
3988 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3989 || (bounds.top + verticalDiff >= maxBottom)) {
3990 verticalDiff = maxBottom - bounds.top;
3991 }
3992 bounds.top += verticalDiff;
3993 bounds.bottom += verticalDiff;
3994 }
3995 }
3996
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003997 ActivityStack findStackBehind(ActivityStack stack) {
3998 // TODO(multi-display): We are only looking for stacks on the default display.
3999 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4000 if (display == null) {
4001 return null;
4002 }
4003 final ArrayList<ActivityStack> stacks = display.mStacks;
4004 for (int i = stacks.size() - 1; i >= 0; i--) {
4005 if (stacks.get(i) == stack && i > 0) {
4006 return stacks.get(i - 1);
4007 }
4008 }
4009 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4010 + " in=" + stacks);
4011 }
Craig Mautner27084302013-03-25 08:05:25 -07004012}