blob: 5f65b3da7f48de1381cfad495de8e0495eaa2136 [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
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001021 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1022 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1023 }
1024
Man Caocfa78b22015-06-11 20:14:34 -07001025 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1026 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1027 }
1028
1029 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001030 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 }
1032 }
1033 }
1034 return aInfo;
1035 }
1036
Todd Kennedy7440f172015-12-09 14:31:22 -08001037 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1038 try {
1039 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1040 PackageManager.MATCH_DEFAULT_ONLY
1041 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1042 } catch (RemoteException e) {
1043 }
1044 return null;
1045 }
1046
1047 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1048 ProfilerInfo profilerInfo, int userId) {
1049 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1050 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1051 }
1052
Craig Mautner2420ead2013-04-01 17:13:20 -07001053 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001054 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001055 throws RemoteException {
1056
Craig Mautner2568c3a2015-03-26 14:22:34 -07001057 if (andResume) {
1058 r.startFreezingScreenLocked(app, 0);
1059 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001060
Craig Mautner2568c3a2015-03-26 14:22:34 -07001061 // schedule launch ticks to collect information about slow apps.
1062 r.startLaunchTickingLocked();
1063 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001064
1065 // Have the window manager re-evaluate the orientation of
1066 // the screen based on the new activity order. Note that
1067 // as a result of this, it can call back into the activity
1068 // manager with a new orientation. We don't care about that,
1069 // because the activity is not currently running so we are
1070 // just restarting it anyway.
1071 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001072 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001073 mService.mConfiguration,
1074 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001075 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001076 }
1077
1078 r.app = app;
1079 app.waitingToKill = null;
1080 r.launchCount++;
1081 r.lastLaunchTime = SystemClock.uptimeMillis();
1082
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001083 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001084
1085 int idx = app.activities.indexOf(r);
1086 if (idx < 0) {
1087 app.activities.add(r);
1088 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001089 mService.updateLruProcessLocked(app, true, null);
1090 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001091
Craig Mautner15df08a2015-04-01 12:17:18 -07001092 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001093 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1094 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001095 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001096 }
1097
1098 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 try {
1100 if (app.thread == null) {
1101 throw new RemoteException();
1102 }
1103 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001104 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001105 if (andResume) {
1106 results = r.results;
1107 newIntents = r.newIntents;
1108 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001109 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1110 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1111 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 if (andResume) {
1113 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1114 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001117 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001118 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001119 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001120 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001121 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001122 r.sleeping = false;
1123 r.forceNewConfig = false;
1124 mService.showAskCompatModeDialogLocked(r);
1125 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001126 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001127 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1128 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1129 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001130 final String profileFile = mService.mProfileFile;
1131 if (profileFile != null) {
1132 ParcelFileDescriptor profileFd = mService.mProfileFd;
1133 if (profileFd != null) {
1134 try {
1135 profileFd = profileFd.dup();
1136 } catch (IOException e) {
1137 if (profileFd != null) {
1138 try {
1139 profileFd.close();
1140 } catch (IOException o) {
1141 }
1142 profileFd = null;
1143 }
1144 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001146
1147 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1148 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001149 }
1150 }
1151 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001152
Craig Mautner2568c3a2015-03-26 14:22:34 -07001153 if (andResume) {
1154 app.hasShownUi = true;
1155 app.pendingUiClean = true;
1156 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001157 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001158 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001159 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001160 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001161 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001162 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001163
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001164 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001165 // This may be a heavy-weight process! Note that the package
1166 // manager will ensure that only activity can run in the main
1167 // process of the .apk, which is the only thing that will be
1168 // considered heavy-weight.
1169 if (app.processName.equals(app.info.packageName)) {
1170 if (mService.mHeavyWeightProcess != null
1171 && mService.mHeavyWeightProcess != app) {
1172 Slog.w(TAG, "Starting new heavy weight process " + app
1173 + " when already running "
1174 + mService.mHeavyWeightProcess);
1175 }
1176 mService.mHeavyWeightProcess = app;
1177 Message msg = mService.mHandler.obtainMessage(
1178 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1179 msg.obj = r;
1180 mService.mHandler.sendMessage(msg);
1181 }
1182 }
1183
1184 } catch (RemoteException e) {
1185 if (r.launchFailed) {
1186 // This is the second time we failed -- finish activity
1187 // and give up.
1188 Slog.e(TAG, "Second failure launching "
1189 + r.intent.getComponent().flattenToShortString()
1190 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001191 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1193 "2nd-crash", false);
1194 return false;
1195 }
1196
1197 // This is the first time we failed -- restart process and
1198 // retry.
1199 app.activities.remove(r);
1200 throw e;
1201 }
1202
1203 r.launchFailed = false;
1204 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001205 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 }
1207
1208 if (andResume) {
1209 // As part of the process of launching, ActivityThread also performs
1210 // a resume.
1211 stack.minimalResumeActivityLocked(r);
1212 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001213 // This activity is not starting in the resumed state... which should look like we asked
1214 // it to pause+stop (but remain visible), and it has done so and reported back the
1215 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001216 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001217 "Moving to PAUSED: " + r + " (starting in paused state)");
1218 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001219 }
1220
1221 // Launch the new version setup screen if needed. We do this -after-
1222 // launching the initial activity (that is, home), so that it can have
1223 // a chance to initialize itself while in the background, making the
1224 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001225 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 mService.startSetupActivityLocked();
1227 }
1228
Dianne Hackborn465fa392014-09-14 14:21:18 -07001229 // Update any services we are bound to that might care about whether
1230 // their client may have activities.
1231 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1232
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 return true;
1234 }
1235
Craig Mautnere79d42682013-04-01 19:01:53 -07001236 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001237 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001238 // Is this activity's application already running?
1239 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001240 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001241
1242 r.task.stack.setLaunchTime(r);
1243
1244 if (app != null && app.thread != null) {
1245 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001246 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1247 || !"android".equals(r.info.packageName)) {
1248 // Don't add this if it is a platform component that is marked
1249 // to run in multiple processes, because this is actually
1250 // part of the framework so doesn't make sense to track as a
1251 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001252 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1253 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001254 }
George Mount2c92c972014-03-20 09:38:23 -07001255 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001256 return;
1257 } catch (RemoteException e) {
1258 Slog.w(TAG, "Exception when starting activity "
1259 + r.intent.getComponent().flattenToShortString(), e);
1260 }
1261
1262 // If a dead object exception was thrown -- fall through to
1263 // restart the application.
1264 }
1265
1266 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001267 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001268 }
1269
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001270 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001271 String resultWho, int requestCode, int callingPid, int callingUid,
1272 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1273 ActivityRecord resultRecord, ActivityStack resultStack) {
1274 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1275 callingUid);
1276 if (startAnyPerm == PERMISSION_GRANTED) {
1277 return true;
1278 }
1279 final int componentRestriction = getComponentRestrictionForCallingPackage(
1280 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1281 final int actionRestriction = getActionRestrictionForCallingPackage(
1282 intent.getAction(), callingPackage, callingPid, callingUid);
1283 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1284 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1285 if (resultRecord != null) {
1286 resultStack.sendActivityResultLocked(-1,
1287 resultRecord, resultWho, requestCode,
1288 Activity.RESULT_CANCELED, null);
1289 }
1290 final String msg;
1291 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1292 msg = "Permission Denial: starting " + intent.toString()
1293 + " from " + callerApp + " (pid=" + callingPid
1294 + ", uid=" + callingUid + ")" + " with revoked permission "
1295 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1296 } else if (!aInfo.exported) {
1297 msg = "Permission Denial: starting " + intent.toString()
1298 + " from " + callerApp + " (pid=" + callingPid
1299 + ", uid=" + callingUid + ")"
1300 + " not exported from uid " + aInfo.applicationInfo.uid;
1301 } else {
1302 msg = "Permission Denial: starting " + intent.toString()
1303 + " from " + callerApp + " (pid=" + callingPid
1304 + ", uid=" + callingUid + ")"
1305 + " requires " + aInfo.permission;
1306 }
1307 Slog.w(TAG, msg);
1308 throw new SecurityException(msg);
1309 }
1310
1311 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1312 final String message = "Appop Denial: starting " + intent.toString()
1313 + " from " + callerApp + " (pid=" + callingPid
1314 + ", uid=" + callingUid + ")"
1315 + " requires " + AppOpsManager.permissionToOp(
1316 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1317 Slog.w(TAG, message);
1318 return false;
1319 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1320 final String message = "Appop Denial: starting " + intent.toString()
1321 + " from " + callerApp + " (pid=" + callingPid
1322 + ", uid=" + callingUid + ")"
1323 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1324 Slog.w(TAG, message);
1325 return false;
1326 }
1327 return true;
1328 }
1329
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001330 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001331 final long identity = Binder.clearCallingIdentity();
1332 try {
1333 return UserManager.get(mService.mContext).getUserInfo(userId);
1334 } finally {
1335 Binder.restoreCallingIdentity(identity);
1336 }
1337 }
1338
Svet Ganov99b60432015-06-27 13:15:22 -07001339 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001340 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001341 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1342 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001343 == PackageManager.PERMISSION_DENIED) {
1344 return ACTIVITY_RESTRICTION_PERMISSION;
1345 }
1346
Christopher Tateff7add02015-08-17 10:23:22 -07001347 if (activityInfo.permission == null) {
1348 return ACTIVITY_RESTRICTION_NONE;
1349 }
1350
Svet Ganov99b60432015-06-27 13:15:22 -07001351 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1352 if (opCode == AppOpsManager.OP_NONE) {
1353 return ACTIVITY_RESTRICTION_NONE;
1354 }
1355
1356 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1357 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001358 if (!ignoreTargetSecurity) {
1359 return ACTIVITY_RESTRICTION_APPOP;
1360 }
Svet Ganov99b60432015-06-27 13:15:22 -07001361 }
1362
1363 return ACTIVITY_RESTRICTION_NONE;
1364 }
1365
Svetoslav7008b512015-06-24 18:47:07 -07001366 private int getActionRestrictionForCallingPackage(String action,
1367 String callingPackage, int callingPid, int callingUid) {
1368 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001369 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001370 }
1371
1372 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1373 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001374 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001375 }
1376
1377 final PackageInfo packageInfo;
1378 try {
1379 packageInfo = mService.mContext.getPackageManager()
1380 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1381 } catch (PackageManager.NameNotFoundException e) {
1382 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001383 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001384 }
1385
1386 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001387 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001388 }
1389
1390 if (mService.checkPermission(permission, callingPid, callingUid) ==
1391 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001392 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001393 }
1394
1395 final int opCode = AppOpsManager.permissionToOpCode(permission);
1396 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001397 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001398 }
1399
1400 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1401 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001402 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001403 }
1404
Svet Ganov99b60432015-06-27 13:15:22 -07001405 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001406 }
1407
Wale Ogunwaled046a012015-12-24 13:05:59 -08001408 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001409 if (r == null) {
1410 // Not sure what you are trying to do, but it is not going to work...
1411 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001412 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001413 final TaskRecord task = r.task;
1414 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001415 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001416 return false;
1417 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001418 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001419 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001420 }
1421
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001422 void setLaunchSource(int uid) {
1423 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1424 }
1425
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001426 void acquireLaunchWakelock() {
1427 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1428 throw new IllegalStateException("Calling must be system uid");
1429 }
1430 mLaunchingActivity.acquire();
1431 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1432 // To be safe, don't allow the wake lock to be held for too long.
1433 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1434 }
1435 }
1436
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001437 /**
1438 * Called when the frontmost task is idle.
1439 * @return the state of mService.mBooting before this was called.
1440 */
1441 private boolean checkFinishBootingLocked() {
1442 final boolean booting = mService.mBooting;
1443 boolean enableScreen = false;
1444 mService.mBooting = false;
1445 if (!mService.mBooted) {
1446 mService.mBooted = true;
1447 enableScreen = true;
1448 }
1449 if (booting || enableScreen) {
1450 mService.postFinishBooting(booting, enableScreen);
1451 }
1452 return booting;
1453 }
1454
Craig Mautnerf3333272013-04-22 10:55:53 -07001455 // Checked.
1456 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1457 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001458 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001459
Craig Mautnerf3333272013-04-22 10:55:53 -07001460 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001461 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001462 int NS = 0;
1463 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001464 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001465 boolean activityRemoved = false;
1466
Wale Ogunwale7d701172015-03-11 15:36:30 -07001467 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001468 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001469 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1470 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001471 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1472 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001473 if (fromTimeout) {
1474 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001475 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001476
1477 // This is a hack to semi-deal with a race condition
1478 // in the client where it can be constructed with a
1479 // newer configuration from when we asked it to launch.
1480 // We'll update with whatever configuration it now says
1481 // it used to launch.
1482 if (config != null) {
1483 r.configuration = config;
1484 }
1485
1486 // We are now idle. If someone is waiting for a thumbnail from
1487 // us, we can now deliver.
1488 r.idle = true;
1489
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001490 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001491 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001492 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001493 }
1494 }
1495
1496 if (allResumedActivitiesIdle()) {
1497 if (r != null) {
1498 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001499 }
1500
1501 if (mLaunchingActivity.isHeld()) {
1502 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1503 if (VALIDATE_WAKE_LOCK_CALLER &&
1504 Binder.getCallingUid() != Process.myUid()) {
1505 throw new IllegalStateException("Calling must be system uid");
1506 }
1507 mLaunchingActivity.release();
1508 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001509 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001510 }
1511
1512 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001513 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001514 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001515 if ((NF = mFinishingActivities.size()) > 0) {
1516 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001517 mFinishingActivities.clear();
1518 }
1519
Craig Mautnerf3333272013-04-22 10:55:53 -07001520 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001521 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001522 mStartingUsers.clear();
1523 }
1524
Craig Mautnerf3333272013-04-22 10:55:53 -07001525 // Stop any activities that are scheduled to do so but have been
1526 // waiting for the next one to start.
1527 for (int i = 0; i < NS; i++) {
1528 r = stops.get(i);
1529 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001530 if (stack != null) {
1531 if (r.finishing) {
1532 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1533 } else {
1534 stack.stopActivityLocked(r);
1535 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001536 }
1537 }
1538
1539 // Finish any activities that are scheduled to do so but have been
1540 // waiting for the next one to start.
1541 for (int i = 0; i < NF; i++) {
1542 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001543 final ActivityStack stack = r.task.stack;
1544 if (stack != null) {
1545 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1546 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001547 }
1548
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001549 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001550 // Complete user switch
1551 if (startingUsers != null) {
1552 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001553 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001554 }
1555 }
1556 // Complete starting up of background users
1557 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001558 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001559 mStartingBackgroundUsers.clear();
1560 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001561 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001562 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001563 }
1564 }
1565
1566 mService.trimApplications();
1567 //dump();
1568 //mWindowManager.dump();
1569
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001571 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001572 }
1573
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001574 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001575 }
1576
Craig Mautner8e569572013-10-11 17:36:59 -07001577 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001578 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1580 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1582 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1583 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001584 }
Craig Mautner19091252013-10-05 00:03:53 -07001585 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001586 }
1587
1588 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1590 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1592 stacks.get(stackNdx).closeSystemDialogsLocked();
1593 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001594 }
1595 }
1596
Craig Mautner93529a42013-10-04 15:03:13 -07001597 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001598 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001599 }
1600
Craig Mautner8d341ef2013-03-26 09:03:27 -07001601 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001602 * Update the last used stack id for non-current user (current user's last
1603 * used stack is the focused stack)
1604 */
1605 void updateUserStackLocked(int userId, ActivityStack stack) {
1606 if (userId != mCurrentUser) {
1607 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1608 }
1609 }
1610
1611 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001612 * @return true if some activity was finished (or would have finished if doit were true).
1613 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001614 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1615 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001616 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1618 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001620 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001621 if (stack.finishDisabledPackageActivitiesLocked(
1622 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001623 didSomething = true;
1624 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001625 }
1626 }
1627 return didSomething;
1628 }
1629
Dianne Hackborna413dc02013-07-12 12:02:55 -07001630 void updatePreviousProcessLocked(ActivityRecord r) {
1631 // Now that this process has stopped, we may want to consider
1632 // it to be the previous app to try to keep around in case
1633 // the user wants to return to it.
1634
1635 // First, found out what is currently the foreground app, so that
1636 // we don't blow away the previous app if this activity is being
1637 // hosted by the process that is actually still the foreground.
1638 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1640 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1642 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001643 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001644 if (stack.mResumedActivity != null) {
1645 fgApp = stack.mResumedActivity.app;
1646 } else if (stack.mPausingActivity != null) {
1647 fgApp = stack.mPausingActivity.app;
1648 }
1649 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001650 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001651 }
1652 }
1653
1654 // Now set this one as the previous process, only if that really
1655 // makes sense to.
1656 if (r.app != null && fgApp != null && r.app != fgApp
1657 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001658 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001659 mService.mPreviousProcess = r.app;
1660 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1661 }
1662 }
1663
Wale Ogunwaled046a012015-12-24 13:05:59 -08001664 boolean resumeFocusedStackTopActivityLocked() {
1665 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001666 }
1667
Wale Ogunwaled046a012015-12-24 13:05:59 -08001668 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001669 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001670 if (targetStack != null && isFocusedStack(targetStack)) {
1671 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001672 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001673 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1674 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001675 }
1676
Adrian Roos20d7df32016-01-12 18:59:43 +01001677 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1678 TaskRecord finishedTask = null;
1679 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001680 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1681 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001682 final int numStacks = stacks.size();
1683 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1684 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001685 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1686 if (stack == focusedStack || finishedTask == null) {
1687 finishedTask = t;
1688 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001689 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001690 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001691 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001692 }
1693
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001694 void finishVoiceTask(IVoiceInteractionSession session) {
1695 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1696 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1697 final int numStacks = stacks.size();
1698 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1699 final ActivityStack stack = stacks.get(stackNdx);
1700 stack.finishVoiceTask(session);
1701 }
1702 }
1703 }
1704
Chong Zhang280d3322015-11-03 17:27:26 -08001705 void findTaskToMoveToFrontLocked(
1706 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001707 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1708 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001709 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001710 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1711 // Caller wants the home activity moved with it. To accomplish this,
1712 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001713 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001714 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001715 if (task.stack == null) {
1716 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1717 + task + " to front. Stack is null");
1718 return;
1719 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001720
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001721 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001722 int stackId = options.getLaunchStackId();
1723 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001724 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001725 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001726 if (stackId == INVALID_STACK_ID) {
1727 stackId = task.getLaunchStackId();
1728 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001729 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001730 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1731 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1732 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001733 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1734 // still need moveTaskToFrontLocked() below for any transition settings.
1735 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001736 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1737 resizeStackLocked(stackId, bounds,
1738 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1739 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1740 } else {
1741 // WM resizeTask must be done after the task is moved to the correct stack,
1742 // because Task's setBounds() also updates dim layer's bounds, but that has
1743 // dependency on the stack.
1744 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1745 false /* relayout */, false /* forced */);
1746 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001747 }
1748 }
1749
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001750 final ActivityRecord r = task.getTopActivity();
1751 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1752 r == null ? null : r.appTimeTracker, reason);
1753
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001754 if (DEBUG_STACK) Slog.d(TAG_STACK,
1755 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001756 }
1757
Wale Ogunwale854809c2015-12-27 16:18:19 -08001758 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001759 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001760 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001761 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001762 return false;
1763 }
1764 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1765 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001766 }
1767
Craig Mautner967212c2013-04-13 21:10:58 -07001768 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001769 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001770 }
1771
1772 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001773 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1774 if (activityContainer != null) {
1775 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001776 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001777 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001778 return null;
1779 }
1780 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001781 }
1782
Craig Mautner967212c2013-04-13 21:10:58 -07001783 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001784 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1786 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001787 }
1788 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001789 }
1790
Craig Mautner4a1cb222013-12-04 16:14:06 -08001791 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001792 ActivityRecord homeActivity = getHomeActivity();
1793 if (homeActivity != null) {
1794 return homeActivity.appToken;
1795 }
1796 return null;
1797 }
1798
1799 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001800 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001801 }
1802
1803 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001804 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1805 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1806 final TaskRecord task = tasks.get(taskNdx);
1807 if (task.isHomeTask()) {
1808 final ArrayList<ActivityRecord> activities = task.mActivities;
1809 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1810 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001811 if (r.isHomeActivity()
1812 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001813 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001814 }
1815 }
1816 }
1817 }
1818 return null;
1819 }
1820
Chong Zhangb15758a2015-11-17 12:12:03 -08001821 /**
1822 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1823 * the docked stack itself, or if it's side-by-side to the docked stack.
1824 */
1825 boolean isStackDockedInEffect(int stackId) {
1826 return stackId == DOCKED_STACK_ID ||
1827 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1828 }
1829
Todd Kennedyca4d8422015-01-15 15:19:22 -08001830 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001831 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001832 ActivityContainer activityContainer =
1833 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001834 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001835 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1836 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001837 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001838 return activityContainer;
1839 }
1840
Craig Mautner34b73df2014-01-12 21:11:08 -08001841 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001842 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1843 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1844 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001845 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1846 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001847 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001848 }
1849 }
1850
Craig Mautner95da1082014-02-24 17:54:35 -08001851 void deleteActivityContainer(IActivityContainer container) {
1852 ActivityContainer activityContainer = (ActivityContainer)container;
1853 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001854 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1855 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001856 final int stackId = activityContainer.mStackId;
1857 mActivityContainers.remove(stackId);
1858 mWindowManager.removeStack(stackId);
1859 }
1860 }
1861
Jorim Jaggidc249c42015-12-15 14:57:31 -08001862 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1863 boolean preserveWindows, boolean allowResizeInDockedMode) {
1864 if (stackId == DOCKED_STACK_ID) {
1865 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1866 preserveWindows);
1867 return;
1868 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001869 final ActivityStack stack = getStack(stackId);
1870 if (stack == null) {
1871 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1872 return;
1873 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001874
Jorim Jaggidc249c42015-12-15 14:57:31 -08001875 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001876 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1877 // stack size changing so things don't get out of sync.
1878 return;
1879 }
1880
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001881 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001882 mWindowManager.deferSurfaceLayout();
1883 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001884 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1885 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001886 } finally {
1887 mWindowManager.continueSurfaceLayout();
1888 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001889 }
1890 }
1891
Jorim Jaggidc249c42015-12-15 14:57:31 -08001892 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1893 Rect tempTaskInsetBounds) {
1894 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1895 // The bounds passed in corresponds to the fullscreen bounds which we normally
1896 // represent with null. Go ahead and set it to null so that all tasks configuration
1897 // can have the right fullscreen state.
1898 bounds = null;
1899 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001900 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001901
1902 mTmpBounds.clear();
1903 mTmpConfigs.clear();
1904 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001905 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001906 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001907 final TaskRecord task = tasks.get(i);
1908 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001909 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1910 // For freeform stack we don't adjust the size of the tasks to match that
1911 // of the stack, but we do try to make sure the tasks are still contained
1912 // with the bounds of the stack.
1913 tempRect2.set(task.mBounds);
1914 fitWithinBounds(tempRect2, bounds);
1915 task.updateOverrideConfiguration(tempRect2);
1916 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001917 task.updateOverrideConfiguration(
1918 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001919 }
1920 }
1921
1922 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1923 mTmpBounds.put(task.taskId, task.mBounds);
1924 if (tempTaskInsetBounds != null) {
1925 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1926 }
1927 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001928
1929 // We might trigger a configuration change. Save the current task bounds for freezing.
1930 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001931 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1932 mTmpBounds, mTmpInsetBounds);
1933 stack.setBounds(bounds);
1934 }
1935
1936 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1937 boolean preserveWindows) {
1938 if (r == null) {
1939 return;
1940 }
1941 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1942 preserveWindows);
1943 // And we need to make sure at this point that all other activities
1944 // are made visible with the correct configuration.
1945 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1946 if (!updated) {
1947 resumeFocusedStackTopActivityLocked();
1948 }
1949 }
1950
1951 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1952 Rect tempDockedTaskInsetBounds,
1953 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1954 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1955 if (stack == null) {
1956 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1957 return;
1958 }
1959
1960 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1961 mWindowManager.deferSurfaceLayout();
1962 try {
1963 ActivityRecord r = stack.topRunningActivityLocked();
1964 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1965 tempDockedTaskInsetBounds);
1966
1967 if (stack.mFullscreen) {
1968 // The dock stack went fullscreen which is kinda like dismissing it.
1969 // In this case we make all other static stacks fullscreen and move all
1970 // docked stack tasks to the fullscreen stack.
1971 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1972 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1973 resizeStackLocked(i, null, null, null, preserveWindows,
1974 true /* allowResizeInDockedMode */);
1975 }
1976 }
1977
1978 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1979 final int count = tasks.size();
1980 for (int i = 0; i < count; i++) {
1981 moveTaskToStackLocked(tasks.get(i).taskId,
1982 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1983 false /* animate */);
1984 }
1985
1986 // stack shouldn't contain anymore activities, so nothing to resume.
1987 r = null;
1988 } else {
1989 // Docked stacks occupy a dedicated region on screen so the size of all other
1990 // static stacks need to be adjusted so they don't overlap with the docked stack.
1991 // We get the bounds to use from window manager which has been adjusted for any
1992 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08001993 mWindowManager.getStackDockedModeBounds(
1994 HOME_STACK_ID, tempRect, true /* ignoreVisibilityOnKeyguardShowing */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001995 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1996 if (StackId.isResizeableByDockedStack(i)) {
1997 ActivityStack otherStack = getStack(i);
1998 if (otherStack != null) {
1999 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2000 tempOtherTaskInsetBounds, preserveWindows,
2001 true /* allowResizeInDockedMode */);
2002 }
2003 }
2004 }
2005 }
2006 ensureConfigurationAndResume(stack, r, preserveWindows);
2007 } finally {
2008 mWindowManager.continueSurfaceLayout();
2009 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2010 }
2011
2012 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2013 tempDockedTaskBounds != null
2014 || tempDockedTaskInsetBounds != null
2015 || tempOtherTaskBounds != null
2016 || tempOtherTaskInsetBounds != null);
2017 }
2018
Chong Zhangf596cd52016-01-05 13:42:44 -08002019 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002020 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002021 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002022 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002023 }
2024
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002025 adjustForMinimalTaskDimensions(task, bounds);
2026
Chong Zhang87b21722015-09-21 15:39:51 -07002027 // If this is a forced resize, let it go through even if the bounds is not changing,
2028 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002029 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002030 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002031 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002032 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002033 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002034 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002035
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002036 if (!mWindowManager.isValidTaskId(task.taskId)) {
2037 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002038 // All we can do for now is update the bounds so it can be used when the task is
2039 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002040 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002041 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2042 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002043 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002044 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002045 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002046 }
2047
Chong Zhang6de2ae82015-09-30 18:25:21 -07002048 // Do not move the task to another stack here.
2049 // This method assumes that the task is already placed in the right stack.
2050 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002051
Chong Zhang6de2ae82015-09-30 18:25:21 -07002052 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002053
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002054 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002055 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002056 // way and the activity was kept the way it was. If it's false, it means the activity had
2057 // to be relaunched due to configuration change.
2058 boolean kept = true;
2059 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002060 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002061 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002062 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002063 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002064 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002065 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002066 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002067 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002068 }
2069 }
2070 }
Chong Zhang87b21722015-09-21 15:39:51 -07002071 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002072
2073 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002074 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002075 }
2076
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002077 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2078 if (bounds == null) {
2079 return;
2080 }
2081 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2082 : task.mMinimalSize;
2083 final boolean adjustWidth = minimalSize > bounds.width();
2084 final boolean adjustHeight = minimalSize > bounds.height();
2085 if (!(adjustWidth || adjustHeight)) {
2086 return;
2087 }
2088 Rect taskBounds = task.mBounds;
2089 if (adjustWidth) {
2090 if (taskBounds != null && bounds.right == taskBounds.right) {
2091 bounds.left = bounds.right - minimalSize;
2092 } else {
2093 // Either left bounds match, or neither match, or the previous bounds were
2094 // fullscreen and we default to keeping left.
2095 bounds.right = bounds.left + minimalSize;
2096 }
2097 }
2098 if (adjustHeight) {
2099 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2100 bounds.top = bounds.bottom - minimalSize;
2101 } else {
2102 // Either top bounds match, or neither match, or the previous bounds were
2103 // fullscreen and we default to keeping top.
2104 bounds.bottom = bounds.top + minimalSize;
2105 }
2106 }
2107 }
2108
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002109 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002110 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2111 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002112 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002113 }
2114
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002115 ActivityContainer activityContainer = new ActivityContainer(stackId);
2116 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002117 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002118 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002119 }
2120
2121 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002122 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002123 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2124 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002125 break;
2126 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002127 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002128 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002129 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
2131
Chong Zhang5dcb2752015-08-18 13:50:26 -07002132 /**
2133 * Restores a recent task to a stack
2134 * @param task The recent task to be restored.
2135 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002136 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002137 * @return true if the task has been restored successfully.
2138 */
2139 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2140 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002141 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002142 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2143 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2144 // Put it in the fullscreen stack.
2145 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002146 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002147
Wale Ogunwale706ed792015-08-02 10:29:44 -07002148 if (task.stack != null) {
2149 // Task has already been restored once. See if we need to do anything more
2150 if (task.stack.mStackId == stackId) {
2151 // Nothing else to do since it is already restored in the right stack.
2152 return true;
2153 }
2154 // Remove current stack association, so we can re-associate the task with the
2155 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002156 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002157 }
2158
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002159 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002160 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002161
2162 if (stack == null) {
2163 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002164 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2165 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002166 return false;
2167 }
2168
Wale Ogunwale5f986092015-12-04 15:35:38 -08002169 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002170 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2171 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002172 final ArrayList<ActivityRecord> activities = task.mActivities;
2173 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002174 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002175 }
2176 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002177 }
2178
Wale Ogunwale040b4702015-08-06 18:10:50 -07002179 /**
2180 * Moves the specified task record to the input stack id.
2181 * WARNING: This method performs an unchecked/raw move of the task and
2182 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002183 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002184 * @param task Task to move.
2185 * @param stackId Id of stack to move task to.
2186 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002187 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002188 * @param reason Reason the task is been moved.
2189 * @return The stack the task was moved to.
2190 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002191 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002192 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2193 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002194 final ActivityStack prevStack = task.stack;
2195 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2196 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2197 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2198 // Whenever we are moving the top activity from the front stack we want to make sure to move
2199 // the stack to the front.
2200 final boolean wasFront = isFrontStack(prevStack)
2201 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002202
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002203 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002204
2205 if (stackId == DOCKED_STACK_ID && resizeMode == RESIZE_MODE_UNRESIZEABLE) {
2206 // We don't allow moving a unresizeable task to the docked stack since the docked
2207 // stack is used for split-screen mode and will cause things like the docked divider to
2208 // show up. We instead leave the task in its current stack or move it to the fullscreen
2209 // stack if it isn't currently in a stack.
2210 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002211 Slog.w(TAG, "Can not move unresizeable task=" + task
2212 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2213 }
2214
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002215 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2216 // if a docked stack is created below which will lead to the stack we are moving from and
2217 // its resizeable tasks being resized.
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002218 task.mResizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002219 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002220 task.mResizeMode = resizeMode;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002221 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002222 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002223
2224 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002225 // move focus to the new stack by moving the stack to the front.
2226 stack.moveToFrontAndResumeStateIfNeeded(
2227 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002228
Wale Ogunwale040b4702015-08-06 18:10:50 -07002229 return stack;
2230 }
2231
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002232 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002233 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002234 final TaskRecord task = anyTaskForIdLocked(taskId);
2235 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002236 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002237 return;
2238 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002239
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002240 if (task.stack != null && task.stack.mStackId == stackId) {
2241 // You are already in the right stack silly...
2242 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2243 return;
2244 }
2245
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002246 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002247 final boolean mightReplaceWindow =
2248 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2249 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002250 // We are about to relaunch the activity because its configuration changed due to
2251 // being maximized, i.e. size change. The activity will first remove the old window
2252 // and then add a new one. This call will tell window manager about this, so it can
2253 // preserve the old window until the new one is drawn. This prevents having a gap
2254 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002255 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002256 // Note here we always set the replacing window first, as the flags might be needed
2257 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002258 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002259 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002260 final int preferredLaunchStackId = stackId;
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002261 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2262 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale513346d2016-01-27 10:55:01 -08002263 stackId = stack.mStackId;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002264
Jorim Jaggi55387522015-11-24 18:21:10 -08002265 if (!animate) {
2266 stack.mNoAnimActivities.add(topActivity);
2267 }
2268
Chong Zhangf596cd52016-01-05 13:42:44 -08002269 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002270 // Make sure the task has the appropriate bounds/size for the stack it is in.
2271 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002272 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002273 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2274 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002275 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002276 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002277 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002278 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2279 }
2280
2281 if (mightReplaceWindow) {
2282 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2283 // window), we need to clear the replace window settings. Otherwise, we schedule a
2284 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002285 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002286 }
2287
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002288 // The task might have already been running and its visibility needs to be synchronized with
2289 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002290 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002291 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002292
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002293 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002294 }
2295
Wale Ogunwale079a0042015-10-24 11:44:07 -07002296 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2297 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2298 if (stack == null) {
2299 throw new IllegalArgumentException(
2300 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2301 }
2302
2303 final ActivityRecord r = stack.topRunningActivityLocked();
2304 if (r == null) {
2305 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2306 + " in stack=" + stack);
2307 return false;
2308 }
2309
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002310 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002311 Slog.w(TAG,
2312 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002313 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002314 return false;
2315 }
2316
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002317 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", null);
2318 mWindowManager.animateResizePinnedStack(bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002319 return true;
2320 }
2321
2322 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002323 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002324 if (task.mActivities.size() == 1) {
2325 // 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 -08002326 // stack without re-parenting the activity in a different task.
2327 moveTaskToStackLocked(
2328 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002329 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002330 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2331 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002332 }
2333
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002334 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002335 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2336 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002337 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002338
2339 // The task might have already been running and its visibility needs to be synchronized with
2340 // the visibility of the stack / windows.
2341 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002342 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002343
2344 if (stackId == PINNED_STACK_ID) {
2345 mService.notifyActivityPinnedLocked();
2346 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002347 }
2348
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002349 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2350 final TaskRecord task = anyTaskForIdLocked(taskId);
2351 if (task == null) {
2352 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2353 return;
2354 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002355 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2356
2357 task.updateOverrideConfigurationForStack(stack);
2358
2359 mWindowManager.positionTaskInStack(
2360 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002361 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002362 // The task might have already been running and its visibility needs to be synchronized with
2363 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002364 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002365 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002366 }
2367
Craig Mautnerac6f8432013-07-17 13:24:59 -07002368 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002369 mTmpFindTaskResult.r = null;
2370 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002371 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002372 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2373 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002374 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2375 final ActivityStack stack = stacks.get(stackNdx);
2376 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002377 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002378 continue;
2379 }
2380 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002381 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2382 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002383 continue;
2384 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002385 stack.findTaskLocked(r, mTmpFindTaskResult);
2386 // It is possible to have task in multiple stacks with the same root affinity.
2387 // If the match we found was based on root affinity we keep on looking to see if
2388 // there is a better match in another stack. We eventually return the match based
2389 // on root affinity if we don't find a better match.
2390 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2391 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002392 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002393 }
2394 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002395 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2396 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002397 }
2398
2399 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002400 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2401 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002402 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2403 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2404 if (ar != null) {
2405 return ar;
2406 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002407 }
2408 }
2409 return null;
2410 }
2411
Craig Mautner8d341ef2013-03-26 09:03:27 -07002412 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002413 scheduleSleepTimeout();
2414 if (!mGoingToSleep.isHeld()) {
2415 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002416 if (mLaunchingActivity.isHeld()) {
2417 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2418 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002419 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002420 mLaunchingActivity.release();
2421 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002422 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002423 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002424 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002425 }
2426
2427 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002428 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002429
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002430 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002431 final long endTime = System.currentTimeMillis() + timeout;
2432 while (true) {
2433 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002434 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2435 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002436 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2437 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2438 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002439 }
2440 if (cantShutdown) {
2441 long timeRemaining = endTime - System.currentTimeMillis();
2442 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002443 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002444 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002445 } catch (InterruptedException e) {
2446 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002447 } else {
2448 Slog.w(TAG, "Activity manager shutdown timed out");
2449 timedout = true;
2450 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002451 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002452 } else {
2453 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002454 }
2455 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002456
2457 // Force checkReadyForSleep to complete.
2458 mSleepTimeout = true;
2459 checkReadyForSleepLocked();
2460
Craig Mautner8d341ef2013-03-26 09:03:27 -07002461 return timedout;
2462 }
2463
2464 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002465 removeSleepTimeouts();
2466 if (mGoingToSleep.isHeld()) {
2467 mGoingToSleep.release();
2468 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002471 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2472 final ActivityStack stack = stacks.get(stackNdx);
2473 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002474 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002475 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002476 }
Craig Mautner5314a402013-09-26 12:40:16 -07002477 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002478 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002479 mGoingToSleepActivities.clear();
2480 }
2481
2482 void activitySleptLocked(ActivityRecord r) {
2483 mGoingToSleepActivities.remove(r);
2484 checkReadyForSleepLocked();
2485 }
2486
2487 void checkReadyForSleepLocked() {
2488 if (!mService.isSleepingOrShuttingDown()) {
2489 // Do not care.
2490 return;
2491 }
2492
2493 if (!mSleepTimeout) {
2494 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2498 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2499 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002500 }
2501
2502 if (mStoppingActivities.size() > 0) {
2503 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002504 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002505 + mStoppingActivities.size() + " activities");
2506 scheduleIdleLocked();
2507 dontSleep = true;
2508 }
2509
2510 if (mGoingToSleepActivities.size() > 0) {
2511 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002512 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002513 + mGoingToSleepActivities.size() + " activities");
2514 dontSleep = true;
2515 }
2516
2517 if (dontSleep) {
2518 return;
2519 }
2520 }
2521
Craig Mautnere0a38842013-12-16 16:14:02 -08002522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2525 stacks.get(stackNdx).goToSleep();
2526 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002527 }
2528
2529 removeSleepTimeouts();
2530
2531 if (mGoingToSleep.isHeld()) {
2532 mGoingToSleep.release();
2533 }
2534 if (mService.mShuttingDown) {
2535 mService.notifyAll();
2536 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002537 }
2538
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002539 boolean reportResumedActivityLocked(ActivityRecord r) {
2540 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002541 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002542 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002543 }
2544 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002545 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002546 mWindowManager.executeAppTransition();
2547 return true;
2548 }
2549 return false;
2550 }
2551
Craig Mautner8d341ef2013-03-26 09:03:27 -07002552 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002555 int stackNdx = stacks.size() - 1;
2556 while (stackNdx >= 0) {
2557 stacks.get(stackNdx).handleAppCrashLocked(app);
2558 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002559 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002560 }
2561 }
2562
Jose Lima4b6c6692014-08-12 17:41:12 -07002563 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002564 final ActivityStack stack = r.task.stack;
2565 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002566 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2567 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002568 return false;
2569 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002570 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002571 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2572 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002573
2574 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002575 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002576 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002577 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002578 return true;
2579 }
2580
2581 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002582 if (visible && top.fullscreen) {
2583 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002584 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2585 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2586 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2587 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002588 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002589 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2590 // Only the activity set as currently visible behind should actively reset its
2591 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002592 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2593 "requestVisibleBehind: returning visible=" + visible
2594 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2595 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002596 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002597 }
2598
Jose Lima4b6c6692014-08-12 17:41:12 -07002599 stack.setVisibleBehindActivity(visible ? r : null);
2600 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002601 // Make the activity immediately above r opaque.
2602 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2603 if (next != null) {
2604 mService.convertFromTranslucent(next.appToken);
2605 }
2606 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002607 if (top.app != null && top.app.thread != null) {
2608 // Notify the top app of the change.
2609 try {
2610 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2611 } catch (RemoteException e) {
2612 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002613 }
2614 return true;
2615 }
2616
Craig Mautnerbb742462014-07-07 15:28:55 -07002617 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2618 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2619 r.mLaunchTaskBehind = false;
2620 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002621 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002622 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002623 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002624 mWindowManager.setAppVisibility(r.appToken, false);
2625 }
2626
2627 void scheduleLaunchTaskBehindComplete(IBinder token) {
2628 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2629 }
2630
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002631 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2632 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002633 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 final int topStackNdx = stacks.size() - 1;
2637 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2638 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002639 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002640 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002641 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002642 }
2643
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002644 void invalidateTaskLayers() {
2645 mTaskLayersChanged = true;
2646 }
2647
2648 void rankTaskLayersIfNeeded() {
2649 if (!mTaskLayersChanged) {
2650 return;
2651 }
2652 mTaskLayersChanged = false;
2653 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2655 int baseLayer = 0;
2656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2657 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2658 }
2659 }
2660 }
2661
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002662 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2664 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2665 final int topStackNdx = stacks.size() - 1;
2666 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2667 final ActivityStack stack = stacks.get(stackNdx);
2668 stack.clearOtherAppTimeTrackers(except);
2669 }
2670 }
2671 }
2672
Craig Mautner8d341ef2013-03-26 09:03:27 -07002673 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 final int numStacks = stacks.size();
2677 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2678 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002679 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 }
2682 }
2683
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002684 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2685 // Examine all activities currently running in the process.
2686 TaskRecord firstTask = null;
2687 // Tasks is non-null only if two or more tasks are found.
2688 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002689 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2690 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002691 ActivityRecord r = app.activities.get(i);
2692 // First, if we find an activity that is in the process of being destroyed,
2693 // then we just aren't going to do anything for now; we want things to settle
2694 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002695 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002696 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002697 return;
2698 }
2699 // Don't consider any activies that are currently not in a state where they
2700 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002701 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2702 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002703 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002704 continue;
2705 }
2706 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002707 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002708 + " from " + r);
2709 if (firstTask == null) {
2710 firstTask = r.task;
2711 } else if (firstTask != r.task) {
2712 if (tasks == null) {
2713 tasks = new ArraySet<>();
2714 tasks.add(firstTask);
2715 }
2716 tasks.add(r.task);
2717 }
2718 }
2719 }
2720 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002721 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002722 return;
2723 }
2724 // If we have activities in multiple tasks that are in a position to be destroyed,
2725 // let's iterate through the tasks and release the oldest one.
2726 final int numDisplays = mActivityDisplays.size();
2727 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2729 // Step through all stacks starting from behind, to hit the oldest things first.
2730 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2731 final ActivityStack stack = stacks.get(stackNdx);
2732 // Try to release activities in this stack; if we manage to, we are done.
2733 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2734 return;
2735 }
2736 }
2737 }
2738 }
2739
Amith Yamasani37a40c22015-06-17 13:25:42 -07002740 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002741 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002742 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002743 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002744
Craig Mautner858d8a62013-04-23 17:08:34 -07002745 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2747 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002749 final ActivityStack stack = stacks.get(stackNdx);
2750 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002751 TaskRecord task = stack.topTask();
2752 if (task != null) {
2753 mWindowManager.moveTaskToTop(task.taskId);
2754 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002755 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002756 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002757
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002758 ActivityStack stack = getStack(restoreStackId);
2759 if (stack == null) {
2760 stack = mHomeStack;
2761 }
2762 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002764 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002765 } else {
2766 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002767 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002768 }
Craig Mautner93529a42013-10-04 15:03:13 -07002769 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002770 }
2771
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002772 /**
2773 * Add background users to send boot completed events to.
2774 * @param userId The user being started in the background
2775 * @param uss The state object for the user.
2776 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002777 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002778 mStartingBackgroundUsers.add(uss);
2779 }
2780
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002781 /** Checks whether the userid is a profile of the current user. */
2782 boolean isCurrentProfileLocked(int userId) {
2783 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002784 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002785 }
2786
Chong Zhang45c25ce2015-08-10 22:18:26 -07002787 /** Checks whether the activity should be shown for current user. */
2788 boolean okToShowLocked(ActivityRecord r) {
2789 return r != null && (isCurrentProfileLocked(r.userId)
2790 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2791 }
2792
Craig Mautnerde4ef022013-04-07 19:01:33 -07002793 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002794 ArrayList<ActivityRecord> stops = null;
2795
2796 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002797 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2798 ActivityRecord s = mStoppingActivities.get(activityNdx);
2799 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002800 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002801 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2802 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002803 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002804 if (s.finishing) {
2805 // If this activity is finishing, it is sitting on top of
2806 // everyone else but we now know it is no longer needed...
2807 // so get rid of it. Otherwise, we need to go through the
2808 // normal flow and hide it once we determine that it is
2809 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002810 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002811 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002812 }
2813 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002814 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002815 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002816 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002817 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002818 }
2819 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002820 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002821 }
2822 }
2823
2824 return stops;
2825 }
2826
Craig Mautnercf910b02013-04-23 11:23:27 -07002827 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2829 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2831 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002832 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002834 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002835 if (r == null) Slog.e(TAG,
2836 "validateTop...: null top activity, stack=" + stack);
2837 else {
2838 final ActivityRecord pausing = stack.mPausingActivity;
2839 if (pausing != null && pausing == r) Slog.e(TAG,
2840 "validateTop...: top stack has pausing activity r=" + r
2841 + " state=" + state);
2842 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2843 "validateTop...: activity in front not resumed r=" + r
2844 + " state=" + state);
2845 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002846 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002847 final ActivityRecord resumed = stack.mResumedActivity;
2848 if (resumed != null && resumed == r) Slog.e(TAG,
2849 "validateTop...: back stack has resumed activity r=" + r
2850 + " state=" + state);
2851 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2852 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002853 }
2854 }
2855 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002856 }
2857
Craig Mautnere0570202015-05-13 13:06:11 -07002858 private String lockTaskModeToString() {
2859 switch (mLockTaskModeState) {
2860 case LOCK_TASK_MODE_LOCKED:
2861 return "LOCKED";
2862 case LOCK_TASK_MODE_PINNED:
2863 return "PINNED";
2864 case LOCK_TASK_MODE_NONE:
2865 return "NONE";
2866 default: return "unknown=" + mLockTaskModeState;
2867 }
2868 }
2869
Craig Mautner27084302013-03-25 08:05:25 -07002870 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002871 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002872 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002873 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002874 pw.print(prefix);
2875 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002876 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002877 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002878 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2879 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2880 if (packages.size() > 0) {
2881 pw.println(" mLockTaskPackages (userId:packages)=");
2882 for (int i = 0; i < packages.size(); ++i) {
2883 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2884 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2885 }
2886 }
2887 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002888 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002889
Craig Mautner20e72272013-04-01 13:45:53 -07002890 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002891 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002892 }
2893
Dianne Hackborn390517b2013-05-30 15:03:32 -07002894 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2895 boolean needSep, String prefix) {
2896 if (activity != null) {
2897 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2898 if (needSep) {
2899 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002900 }
2901 pw.print(prefix);
2902 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002903 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002904 }
2905 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002906 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002907 }
2908
Craig Mautner8d341ef2013-03-26 09:03:27 -07002909 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2910 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002911 boolean printed = false;
2912 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002913 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2914 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002915 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002916 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002917 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002918 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002919 final ActivityStack stack = stacks.get(stackNdx);
2920 StringBuilder stackHeader = new StringBuilder(128);
2921 stackHeader.append(" Stack #");
2922 stackHeader.append(stack.mStackId);
2923 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002924 stackHeader.append("\n");
2925 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2926 stackHeader.append("\n");
2927 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002928 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2929 needSep, stackHeader.toString());
2930 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2931 !dumpAll, false, dumpPackage, true,
2932 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002933
Craig Mautner4a1cb222013-12-04 16:14:06 -08002934 needSep = printed;
2935 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2936 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002937 if (pr) {
2938 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002940 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2942 " mResumedActivity: ");
2943 if (pr) {
2944 printed = true;
2945 needSep = false;
2946 }
2947 if (dumpAll) {
2948 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2949 " mLastPausedActivity: ");
2950 if (pr) {
2951 printed = true;
2952 needSep = true;
2953 }
2954 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2955 needSep, " mLastNoHistoryActivity: ");
2956 }
2957 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002958 }
2959 }
2960
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002961 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2962 false, dumpPackage, true, " Activities waiting to finish:", null);
2963 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2964 false, dumpPackage, true, " Activities waiting to stop:", null);
2965 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2966 false, dumpPackage, true, " Activities waiting for another to become visible:",
2967 null);
2968 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2969 false, dumpPackage, true, " Activities waiting to sleep:", null);
2970 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2971 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002972
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002973 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002974 }
2975
Dianne Hackborn390517b2013-05-30 15:03:32 -07002976 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002978 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002979 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002980 String innerPrefix = null;
2981 String[] args = null;
2982 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002983 for (int i=list.size()-1; i>=0; i--) {
2984 final ActivityRecord r = list.get(i);
2985 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2986 continue;
2987 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002988 if (innerPrefix == null) {
2989 innerPrefix = prefix + " ";
2990 args = new String[0];
2991 }
2992 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002993 final boolean full = !brief && (complete || !r.isInHistory());
2994 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002995 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 needNL = false;
2997 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002998 if (header1 != null) {
2999 pw.println(header1);
3000 header1 = null;
3001 }
3002 if (header2 != null) {
3003 pw.println(header2);
3004 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003005 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003006 if (lastTask != r.task) {
3007 lastTask = r.task;
3008 pw.print(prefix);
3009 pw.print(full ? "* " : " ");
3010 pw.println(lastTask);
3011 if (full) {
3012 lastTask.dump(pw, prefix + " ");
3013 } else if (complete) {
3014 // Complete + brief == give a summary. Isn't that obvious?!?
3015 if (lastTask.intent != null) {
3016 pw.print(prefix); pw.print(" ");
3017 pw.println(lastTask.intent.toInsecureStringWithClip());
3018 }
3019 }
3020 }
3021 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3022 pw.print(" #"); pw.print(i); pw.print(": ");
3023 pw.println(r);
3024 if (full) {
3025 r.dump(pw, innerPrefix);
3026 } else if (complete) {
3027 // Complete + brief == give a summary. Isn't that obvious?!?
3028 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3029 if (r.app != null) {
3030 pw.print(innerPrefix); pw.println(r.app);
3031 }
3032 }
3033 if (client && r.app != null && r.app.thread != null) {
3034 // flush anything that is already in the PrintWriter since the thread is going
3035 // to write to the file descriptor directly
3036 pw.flush();
3037 try {
3038 TransferPipe tp = new TransferPipe();
3039 try {
3040 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3041 r.appToken, innerPrefix, args);
3042 // Short timeout, since blocking here can
3043 // deadlock with the application.
3044 tp.go(fd, 2000);
3045 } finally {
3046 tp.kill();
3047 }
3048 } catch (IOException e) {
3049 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3050 } catch (RemoteException e) {
3051 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3052 }
3053 needNL = true;
3054 }
3055 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003056 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003058
Craig Mautnerf3333272013-04-22 10:55:53 -07003059 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003060 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3061 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003062 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3063 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003064 }
3065
3066 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003067 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003068 }
3069
3070 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003071 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3072 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003073 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3074 }
3075
Craig Mautner05d29032013-05-03 13:40:13 -07003076 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003077 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3078 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3079 }
Craig Mautner05d29032013-05-03 13:40:13 -07003080 }
3081
Craig Mautner0eea92c2013-05-16 13:35:39 -07003082 void removeSleepTimeouts() {
3083 mSleepTimeout = false;
3084 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3085 }
3086
3087 final void scheduleSleepTimeout() {
3088 removeSleepTimeouts();
3089 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3090 }
3091
Craig Mautner4a1cb222013-12-04 16:14:06 -08003092 @Override
3093 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003094 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3096 }
3097
3098 @Override
3099 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003100 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3102 }
3103
3104 @Override
3105 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003106 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003107 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3108 }
3109
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003110 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003111 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003113 newDisplay = mActivityDisplays.get(displayId) == null;
3114 if (newDisplay) {
3115 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003116 if (activityDisplay.mDisplay == null) {
3117 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3118 return;
3119 }
Craig Mautner4504de52013-12-20 09:06:56 -08003120 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003121 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 }
Craig Mautner4504de52013-12-20 09:06:56 -08003124 if (newDisplay) {
3125 mWindowManager.onDisplayAdded(displayId);
3126 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003127 }
3128
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003129 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3130 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3131 return;
3132 }
3133 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3134 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3135 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3136 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3137 }
3138
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003139 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003141 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3142 if (activityDisplay != null) {
3143 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003145 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003147 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003148 }
3149 }
3150 mWindowManager.onDisplayRemoved(displayId);
3151 }
3152
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003153 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003154 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003155 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3156 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003157 // TODO: Update the bounds.
3158 }
3159 }
3160 mWindowManager.onDisplayChanged(displayId);
3161 }
3162
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003163 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003164 StackInfo info = new StackInfo();
3165 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3166 info.displayId = Display.DEFAULT_DISPLAY;
3167 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003168 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169
3170 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3171 final int numTasks = tasks.size();
3172 int[] taskIds = new int[numTasks];
3173 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003174 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003175 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 for (int i = 0; i < numTasks; ++i) {
3177 final TaskRecord task = tasks.get(i);
3178 taskIds[i] = task.taskId;
3179 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3180 : task.realActivity != null ? task.realActivity.flattenToString()
3181 : task.getTopActivity() != null ? task.getTopActivity().packageName
3182 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003183 taskBounds[i] = new Rect();
3184 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003185 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003186 }
3187 info.taskIds = taskIds;
3188 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003189 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003190 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 return info;
3192 }
3193
3194 StackInfo getStackInfoLocked(int stackId) {
3195 ActivityStack stack = getStack(stackId);
3196 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003197 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003198 }
3199 return null;
3200 }
3201
3202 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003203 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3205 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003207 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003208 }
3209 }
3210 return list;
3211 }
3212
Craig Mautner15df08a2015-04-01 12:17:18 -07003213 TaskRecord getLockedTaskLocked() {
3214 final int top = mLockTaskModeTasks.size() - 1;
3215 if (top >= 0) {
3216 return mLockTaskModeTasks.get(top);
3217 }
3218 return null;
3219 }
3220
3221 boolean isLockedTask(TaskRecord task) {
3222 return mLockTaskModeTasks.contains(task);
3223 }
3224
3225 boolean isLastLockedTask(TaskRecord task) {
3226 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3227 }
3228
3229 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003230 if (!mLockTaskModeTasks.remove(task)) {
3231 return;
3232 }
3233 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3234 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003235 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003236 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3237 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003238 final Message lockTaskMsg = Message.obtain();
3239 lockTaskMsg.arg1 = task.userId;
3240 lockTaskMsg.what = LOCK_TASK_END_MSG;
3241 mHandler.sendMessage(lockTaskMsg);
3242 }
3243 }
3244
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003245 void showNonResizeableDockToastIfNeeded(
3246 TaskRecord task, int preferredStackId, int actualStackId) {
3247 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3248 return;
3249 }
3250
3251 if (!task.canGoInDockedStack() || task.inCropWindowsResizeMode()) {
3252 // Display warning toast if we tried to put a non-dockable task in the docked stack or
3253 // the task is running in cropped window mode.
3254 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3255 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003256 }
3257
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003258 void showLockTaskToast() {
3259 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003260 }
3261
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003262 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3263 if (mLockTaskModeTasks.contains(task)) {
3264 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3265 }
3266 }
3267
Craig Mautner432f64e2015-05-20 14:59:57 -07003268 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3269 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003270 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003271 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003272 final TaskRecord lockedTask = getLockedTaskLocked();
3273 if (lockedTask != null) {
3274 removeLockedTaskLocked(lockedTask);
3275 if (!mLockTaskModeTasks.isEmpty()) {
3276 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003277 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3278 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003279 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003280 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003281 return;
3282 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003283 }
Craig Mautnere0570202015-05-13 13:06:11 -07003284 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3285 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003286 return;
3287 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003288
3289 // Should have already been checked, but do it again.
3290 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003291 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3292 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003293 return;
3294 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003295 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003296 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003297 return;
3298 }
3299
3300 if (mLockTaskModeTasks.isEmpty()) {
3301 // First locktask.
3302 final Message lockTaskMsg = Message.obtain();
3303 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3304 lockTaskMsg.arg1 = task.userId;
3305 lockTaskMsg.what = LOCK_TASK_START_MSG;
3306 lockTaskMsg.arg2 = lockTaskModeState;
3307 mHandler.sendMessage(lockTaskMsg);
3308 }
3309 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003310 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3311 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003312 mLockTaskModeTasks.remove(task);
3313 mLockTaskModeTasks.add(task);
3314
3315 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003316 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003317 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003318
3319 if (andResume) {
3320 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003321 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003322 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003323 }
3324
3325 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003326 return isLockTaskModeViolation(task, false);
3327 }
3328
3329 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3330 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003331 return false;
3332 }
3333 final int lockTaskAuth = task.mLockTaskAuth;
3334 switch (lockTaskAuth) {
3335 case LOCK_TASK_AUTH_DONT_LOCK:
3336 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003337 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003338 case LOCK_TASK_AUTH_LAUNCHABLE:
3339 case LOCK_TASK_AUTH_WHITELISTED:
3340 return false;
3341 case LOCK_TASK_AUTH_PINNABLE:
3342 // Pinnable tasks can't be launched on top of locktask tasks.
3343 return !mLockTaskModeTasks.isEmpty();
3344 default:
3345 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3346 return true;
3347 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003348 }
3349
Craig Mautner15df08a2015-04-01 12:17:18 -07003350 void onLockTaskPackagesUpdatedLocked() {
3351 boolean didSomething = false;
3352 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3353 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003354 final boolean wasWhitelisted =
3355 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3356 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003357 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003358 final boolean isWhitelisted =
3359 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3360 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3361 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003362 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003363 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3364 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003365 removeLockedTaskLocked(lockedTask);
3366 lockedTask.performClearTaskLocked();
3367 didSomething = true;
3368 }
3369 }
3370 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3371 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3372 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3373 final ActivityStack stack = stacks.get(stackNdx);
3374 stack.onLockTaskPackagesUpdatedLocked();
3375 }
3376 }
Craig Mautnere0570202015-05-13 13:06:11 -07003377 final ActivityRecord r = topRunningActivityLocked();
3378 final TaskRecord task = r != null ? r.task : null;
3379 if (mLockTaskModeTasks.isEmpty() && task != null
3380 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3381 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003382 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3383 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3384 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3385 false);
3386 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003387 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003388 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003389 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003390 }
3391 }
3392
Benjamin Franz43261142015-02-11 15:59:44 +00003393 int getLockTaskModeState() {
3394 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003395 }
3396
Jorim Jaggife89d122015-12-22 16:28:44 +01003397 void activityRelaunchedLocked(IBinder token) {
3398 mWindowManager.notifyAppRelaunchingFinished(token);
3399 }
3400
3401 void activityRelaunchingLocked(ActivityRecord r) {
3402 mWindowManager.notifyAppRelaunching(r.appToken);
3403 }
3404
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003405 void logStackState() {
3406 mActivityMetricsLogger.logWindowState();
3407 }
3408
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003409 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003410
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003411 public ActivityStackSupervisorHandler(Looper looper) {
3412 super(looper);
3413 }
3414
Craig Mautnerf3333272013-04-22 10:55:53 -07003415 void activityIdleInternal(ActivityRecord r) {
3416 synchronized (mService) {
3417 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3418 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003419 }
3420
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003421 @Override
3422 public void handleMessage(Message msg) {
3423 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003424 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003425 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3426 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003427 if (mService.mDidDexOpt) {
3428 mService.mDidDexOpt = false;
3429 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3430 nmsg.obj = msg.obj;
3431 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3432 return;
3433 }
3434 // We don't at this point know if the activity is fullscreen,
3435 // so we need to be conservative and assume it isn't.
3436 activityIdleInternal((ActivityRecord)msg.obj);
3437 } break;
3438 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003439 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003440 activityIdleInternal((ActivityRecord)msg.obj);
3441 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003442 case RESUME_TOP_ACTIVITY_MSG: {
3443 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003444 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003445 }
3446 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003447 case SLEEP_TIMEOUT_MSG: {
3448 synchronized (mService) {
3449 if (mService.isSleepingOrShuttingDown()) {
3450 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3451 mSleepTimeout = true;
3452 checkReadyForSleepLocked();
3453 }
3454 }
3455 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003456 case LAUNCH_TIMEOUT_MSG: {
3457 if (mService.mDidDexOpt) {
3458 mService.mDidDexOpt = false;
3459 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3460 return;
3461 }
3462 synchronized (mService) {
3463 if (mLaunchingActivity.isHeld()) {
3464 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3465 if (VALIDATE_WAKE_LOCK_CALLER
3466 && Binder.getCallingUid() != Process.myUid()) {
3467 throw new IllegalStateException("Calling must be system uid");
3468 }
3469 mLaunchingActivity.release();
3470 }
3471 }
3472 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003474 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003475 } break;
3476 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003477 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 } break;
3479 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003480 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003482 case CONTAINER_CALLBACK_VISIBILITY: {
3483 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003484 final IActivityContainerCallback callback = container.mCallback;
3485 if (callback != null) {
3486 try {
3487 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3488 } catch (RemoteException e) {
3489 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003490 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003491 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003492 case LOCK_TASK_START_MSG: {
3493 // When lock task starts, we disable the status bars.
3494 try {
Jason Monk62515be2014-05-21 16:06:19 -04003495 if (mLockTaskNotify == null) {
3496 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003497 }
Jason Monk62515be2014-05-21 16:06:19 -04003498 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003499 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003500 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003501 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003502 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003503 flags = StatusBarManager.DISABLE_MASK
3504 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003505 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003506 flags = StatusBarManager.DISABLE_MASK
3507 & (~StatusBarManager.DISABLE_BACK)
3508 & (~StatusBarManager.DISABLE_HOME)
3509 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003510 }
3511 getStatusBarService().disable(flags, mToken,
3512 mService.mContext.getPackageName());
3513 }
3514 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003515 if (getDevicePolicyManager() != null) {
3516 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003517 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003518 }
justinzhang5286d3f2014-05-12 17:06:01 -04003519 } catch (RemoteException ex) {
3520 throw new RuntimeException(ex);
3521 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003522 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003523 case LOCK_TASK_END_MSG: {
3524 // When lock task ends, we enable the status bars.
3525 try {
Jason Monk62515be2014-05-21 16:06:19 -04003526 if (getStatusBarService() != null) {
3527 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3528 mService.mContext.getPackageName());
3529 }
3530 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003531 if (getDevicePolicyManager() != null) {
3532 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3533 msg.arg1);
3534 }
Jason Monk62515be2014-05-21 16:06:19 -04003535 if (mLockTaskNotify == null) {
3536 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3537 }
3538 mLockTaskNotify.show(false);
3539 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003540 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003541 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003542 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003543 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003544 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003545 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003546 new LockPatternUtils(mService.mContext)
3547 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003548 }
3549 } catch (SettingNotFoundException e) {
3550 // No setting, don't lock.
3551 }
justinzhang5286d3f2014-05-12 17:06:01 -04003552 } catch (RemoteException ex) {
3553 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003554 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003555 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003556 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003557 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003558 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3559 if (mLockTaskNotify == null) {
3560 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3561 }
3562 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3563 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003564 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3565 final ActivityContainer container = (ActivityContainer) msg.obj;
3566 final IActivityContainerCallback callback = container.mCallback;
3567 if (callback != null) {
3568 try {
3569 callback.onAllActivitiesComplete(container.asBinder());
3570 } catch (RemoteException e) {
3571 }
3572 }
3573 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003574 case LAUNCH_TASK_BEHIND_COMPLETE: {
3575 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003576 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003577 if (r != null) {
3578 handleLaunchTaskBehindCompleteLocked(r);
3579 }
3580 }
3581 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003582
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003583 }
3584 }
3585 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003586
Ying Wangb081a592014-04-22 15:20:16 -07003587 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003588 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3589 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003591 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003592 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003593 ActivityRecord mParentActivity = null;
3594 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003595
Craig Mautnere3a00d72014-04-16 08:31:19 -07003596 boolean mVisible = true;
3597
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003599 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003601 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3602 final static int CONTAINER_STATE_NO_SURFACE = 1;
3603 final static int CONTAINER_STATE_FINISHING = 2;
3604 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3605
3606 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003607 synchronized (mService) {
3608 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003609 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003610 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003611 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003613 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003614
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003615 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003616 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003617 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003618 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003619 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003620 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003621 }
3622
3623 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003624 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003625 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3627 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 return;
3629 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003630 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003631 }
3632 }
3633
3634 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003635 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003636 synchronized (mService) {
3637 if (mActivityDisplay != null) {
3638 return mActivityDisplay.mDisplayId;
3639 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003640 }
3641 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642 }
3643
Jeff Brownca9bc702014-02-11 14:32:56 -08003644 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003645 public int getStackId() {
3646 synchronized (mService) {
3647 return mStackId;
3648 }
3649 }
3650
3651 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003652 public boolean injectEvent(InputEvent event) {
3653 final long origId = Binder.clearCallingIdentity();
3654 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003655 synchronized (mService) {
3656 if (mActivityDisplay != null) {
3657 return mInputManagerInternal.injectInputEvent(event,
3658 mActivityDisplay.mDisplayId,
3659 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3660 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003661 }
3662 return false;
3663 } finally {
3664 Binder.restoreCallingIdentity(origId);
3665 }
3666 }
3667
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003668 @Override
3669 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003670 synchronized (mService) {
3671 if (mContainerState == CONTAINER_STATE_FINISHING) {
3672 return;
3673 }
3674 mContainerState = CONTAINER_STATE_FINISHING;
3675
Craig Mautnerd163e752014-06-13 17:18:47 -07003676 long origId = Binder.clearCallingIdentity();
3677 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003678 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003679 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003680 } finally {
3681 Binder.restoreCallingIdentity(origId);
3682 }
3683 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003684 }
3685
Craig Mautner60257702014-09-17 15:02:33 -07003686 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003687 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003688 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003689 if (mActivityDisplay != null) {
3690 mActivityDisplay.detachActivitiesLocked(mStack);
3691 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003692 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003693 }
3694 }
3695
3696 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003697 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003698 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003699 }
3700
3701 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003702 public final int startActivityIntentSender(IIntentSender intentSender)
3703 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003704 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3705
3706 if (!(intentSender instanceof PendingIntentRecord)) {
3707 throw new IllegalArgumentException("Bad PendingIntent object");
3708 }
3709
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003710 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003711 Binder.getCallingUid(), mCurrentUser, false,
3712 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003713
3714 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3715 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3716 pendingIntent.key.requestResolvedType);
3717
3718 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3719 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3720 }
3721
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003722 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003723 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003724 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003725 throw new SecurityException(
3726 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3727 }
3728 }
3729
Craig Mautnerdf88d732014-01-27 09:21:32 -08003730 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003731 public IBinder asBinder() {
3732 return this;
3733 }
3734
Craig Mautner4504de52013-12-20 09:06:56 -08003735 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003736 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003737 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003738 }
3739
Craig Mautner4a1cb222013-12-04 16:14:06 -08003740 ActivityStackSupervisor getOuter() {
3741 return ActivityStackSupervisor.this;
3742 }
3743
Craig Mautnerd163e752014-06-13 17:18:47 -07003744 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003745 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 }
3747
Craig Mautner6985bad2014-04-21 15:22:06 -07003748 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003749 void setVisible(boolean visible) {
3750 if (mVisible != visible) {
3751 mVisible = visible;
3752 if (mCallback != null) {
3753 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3754 0 /* unused */, this).sendToTarget();
3755 }
3756 }
3757 }
3758
Craig Mautner6985bad2014-04-21 15:22:06 -07003759 void setDrawn() {
3760 }
3761
Craig Mautner1b4bf852014-05-26 15:06:32 -07003762 // You can always start a new task on a regular ActivityStack.
3763 boolean isEligibleForNewTasks() {
3764 return true;
3765 }
3766
Craig Mautnerd163e752014-06-13 17:18:47 -07003767 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003768 detachLocked();
3769 deleteActivityContainer(this);
3770 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003771 }
3772
Craig Mautner34b73df2014-01-12 21:11:08 -08003773 @Override
3774 public String toString() {
3775 return mIdString + (mActivityDisplay == null ? "N" : "A");
3776 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003777 }
3778
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003779 private class VirtualActivityContainer extends ActivityContainer {
3780 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003781 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003782
3783 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3784 super(getNextStackId());
3785 mParentActivity = parent;
3786 mCallback = callback;
3787 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003788 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003789 }
3790
3791 @Override
3792 public void setSurface(Surface surface, int width, int height, int density) {
3793 super.setSurface(surface, width, height, density);
3794
3795 synchronized (mService) {
3796 final long origId = Binder.clearCallingIdentity();
3797 try {
3798 setSurfaceLocked(surface, width, height, density);
3799 } finally {
3800 Binder.restoreCallingIdentity(origId);
3801 }
3802 }
3803 }
3804
3805 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3806 if (mContainerState == CONTAINER_STATE_FINISHING) {
3807 return;
3808 }
3809 VirtualActivityDisplay virtualActivityDisplay =
3810 (VirtualActivityDisplay) mActivityDisplay;
3811 if (virtualActivityDisplay == null) {
3812 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003813 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003814 mActivityDisplay = virtualActivityDisplay;
3815 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003816 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003817 }
3818
3819 if (mSurface != null) {
3820 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003821 }
3822
Craig Mautner6985bad2014-04-21 15:22:06 -07003823 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003824 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003825 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003826 } else {
3827 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003828 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003829 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003830 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003831 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003832 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003833
Craig Mautnerd163e752014-06-13 17:18:47 -07003834 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003835
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003836 if (DEBUG_STACK) Slog.d(TAG_STACK,
3837 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003838 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003839
Craig Mautner6985bad2014-04-21 15:22:06 -07003840 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003841 boolean isAttachedLocked() {
3842 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003843 }
3844
3845 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003846 void setDrawn() {
3847 synchronized (mService) {
3848 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003849 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003850 }
3851 }
3852
Craig Mautner1b4bf852014-05-26 15:06:32 -07003853 // Never start a new task on an ActivityView if it isn't explicitly specified.
3854 @Override
3855 boolean isEligibleForNewTasks() {
3856 return false;
3857 }
3858
Craig Mautnerd163e752014-06-13 17:18:47 -07003859 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003860 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003861 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3862 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3863 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3864 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3865 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003866 }
3867 }
3868
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3870 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003871 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003873 int mDisplayId;
3874 Display mDisplay;
3875 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003876
Craig Mautner4a1cb222013-12-04 16:14:06 -08003877 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3878 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003879 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880
Jose Lima4b6c6692014-08-12 17:41:12 -07003881 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003882
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003883 ActivityDisplay() {
3884 }
Craig Mautner4504de52013-12-20 09:06:56 -08003885
Craig Mautner1a70a162014-09-13 12:09:31 -07003886 // After instantiation, check that mDisplay is not null before using this. The alternative
3887 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003888 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003889 final Display display = mDisplayManager.getDisplay(displayId);
3890 if (display == null) {
3891 return;
3892 }
3893 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003894 }
3895
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003896 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003897 mDisplay = display;
3898 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003900 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003902 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003903 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003904 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3905 + " onTop=" + onTop);
3906 if (onTop) {
3907 mStacks.add(stack);
3908 } else {
3909 mStacks.add(0, stack);
3910 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003911 }
3912
Craig Mautnere0a38842013-12-16 16:14:02 -08003913 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003914 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003915 + " from displayId=" + mDisplayId);
3916 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917 }
3918
Jose Lima4b6c6692014-08-12 17:41:12 -07003919 void setVisibleBehindActivity(ActivityRecord r) {
3920 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003921 }
3922
Jose Lima4b6c6692014-08-12 17:41:12 -07003923 boolean hasVisibleBehindActivity() {
3924 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003925 }
3926
Craig Mautner34b73df2014-01-12 21:11:08 -08003927 @Override
3928 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003929 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3930 }
3931 }
3932
3933 class VirtualActivityDisplay extends ActivityDisplay {
3934 VirtualDisplay mVirtualDisplay;
3935
Craig Mautner6985bad2014-04-21 15:22:06 -07003936 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003937 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003938 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3939 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3940 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3941 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003942
3943 init(mVirtualDisplay.getDisplay());
3944
3945 mWindowManager.handleDisplayAdded(mDisplayId);
3946 }
3947
3948 void setSurface(Surface surface) {
3949 if (mVirtualDisplay != null) {
3950 mVirtualDisplay.setSurface(surface);
3951 }
3952 }
3953
3954 @Override
3955 void detachActivitiesLocked(ActivityStack stack) {
3956 super.detachActivitiesLocked(stack);
3957 if (mVirtualDisplay != null) {
3958 mVirtualDisplay.release();
3959 mVirtualDisplay = null;
3960 }
3961 }
3962
3963 @Override
3964 public String toString() {
3965 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003966 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003967 }
Jose Lima58e66d62014-05-27 20:00:27 -07003968
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003969 /**
3970 * Adjust bounds to stay within stack bounds.
3971 *
3972 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3973 * that keep them unchanged, but be contained within the stack bounds.
3974 *
3975 * @param bounds Bounds to be adjusted.
3976 * @param stackBounds Bounds within which the other bounds should remain.
3977 */
3978 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3979 if (stackBounds == null || stackBounds.contains(bounds)) {
3980 return;
3981 }
3982
3983 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3984 final int maxRight = stackBounds.right
3985 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3986 int horizontalDiff = stackBounds.left - bounds.left;
3987 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3988 || (bounds.left + horizontalDiff >= maxRight)) {
3989 horizontalDiff = maxRight - bounds.left;
3990 }
3991 bounds.left += horizontalDiff;
3992 bounds.right += horizontalDiff;
3993 }
3994
3995 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3996 final int maxBottom = stackBounds.bottom
3997 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3998 int verticalDiff = stackBounds.top - bounds.top;
3999 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4000 || (bounds.top + verticalDiff >= maxBottom)) {
4001 verticalDiff = maxBottom - bounds.top;
4002 }
4003 bounds.top += verticalDiff;
4004 bounds.bottom += verticalDiff;
4005 }
4006 }
4007
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004008 ActivityStack findStackBehind(ActivityStack stack) {
4009 // TODO(multi-display): We are only looking for stacks on the default display.
4010 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4011 if (display == null) {
4012 return null;
4013 }
4014 final ArrayList<ActivityStack> stacks = display.mStacks;
4015 for (int i = stacks.size() - 1; i >= 0; i--) {
4016 if (stacks.get(i) == stack && i > 0) {
4017 return stacks.get(i - 1);
4018 }
4019 }
4020 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4021 + " in=" + stacks);
4022 }
Craig Mautner27084302013-03-25 08:05:25 -07004023}