blob: 31712d9233a0b6ce509d502c0f6364088bd73039 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070020import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070021import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080022import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080024import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070025import android.app.ActivityOptions;
26import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070027import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080028import android.app.IActivityContainer;
29import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070030import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080031import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070032import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070033import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040034import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040035import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070037import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070039import android.content.Intent;
40import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070042import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010045import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080047import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080048import android.hardware.display.DisplayManager;
49import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080050import android.hardware.display.DisplayManagerGlobal;
51import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080052import android.hardware.input.InputManager;
53import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.os.Binder;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070055import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070059import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070061import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070062import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070063import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040064import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070066import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070067import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070068import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010069import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070070import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070071import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040072import android.provider.Settings;
73import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070074import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070075import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070076import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080079import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080081import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080083import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080085
Dianne Hackborn85d558c2014-11-04 10:31:54 -080086import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070087import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040088import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070089import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040090import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080091import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070092import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070093import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094
Craig Mautner8d341ef2013-03-26 09:03:27 -070095import java.io.FileDescriptor;
96import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070097import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -070098import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -070099import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800101import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700102import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700103
Jorim Jaggife89d122015-12-22 16:28:44 +0100104import static android.Manifest.permission.START_ANY_ACTIVITY;
105import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
106import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
107import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
108import static android.app.ActivityManager.RESIZE_MODE_FORCED;
109import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
110import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
111import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
112import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
113import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
114import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
115import static android.app.ActivityManager.StackId.HOME_STACK_ID;
116import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
117import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
119import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
120import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
121import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800122import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100123import static android.content.pm.PackageManager.PERMISSION_GRANTED;
124import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
125import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
138import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
149import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
150import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
151import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
152import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
153import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
154import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
155import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
156import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
157import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
158import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
159import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
160import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
161import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
162import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
163import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
164import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
166import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
168
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800170 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700171 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700172 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700173 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700174 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800180 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182
Craig Mautnerf3333272013-04-22 10:55:53 -0700183 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700185
Craig Mautner0eea92c2013-05-16 13:35:39 -0700186 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700188
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700191
Craig Mautner05d29032013-05-03 13:40:13 -0700192 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
193 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
194 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700195 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700196 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800197 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
198 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
199 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700200 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400201 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
202 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700203 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700204 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700205 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800206
Wale Ogunwale040b4702015-08-06 18:10:50 -0700207 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208
Jason Monk62515be2014-05-21 16:06:19 -0400209 private static final String LOCK_TASK_TAG = "Lock-to-App";
210
Wale Ogunwale040b4702015-08-06 18:10:50 -0700211 // Used to indicate if an object (e.g. stack) that we are trying to get
212 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800213 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700215 // Used to indicate that windows of activities should be preserved during the resize.
216 static final boolean PRESERVE_WINDOWS = true;
217
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218 // Used to indicate if an object (e.g. task) should be moved/created
219 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700220 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221
222 // Used to indicate that an objects (e.g. task) removal from its container
223 // (e.g. stack) is due to it moving to another container.
224 static final boolean MOVING = true;
225
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700226 // Force the focus to change to the stack we are moving a task to..
227 static final boolean FORCE_FOCUS = true;
228
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700229 // Restore task from the saved recents if it can't be found in any live stack.
230 static final boolean RESTORE_FROM_RECENTS = true;
231
Svetoslav7008b512015-06-24 18:47:07 -0700232 // Activity actions an app cannot start if it uses a permission which is not granted.
233 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
234 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700235
Svetoslav7008b512015-06-24 18:47:07 -0700236 static {
237 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
238 Manifest.permission.CAMERA);
239 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
240 Manifest.permission.CAMERA);
241 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
242 Manifest.permission.CALL_PHONE);
243 }
244
Svet Ganov99b60432015-06-27 13:15:22 -0700245 /** Action restriction: launching the activity is not restricted. */
246 private static final int ACTIVITY_RESTRICTION_NONE = 0;
247 /** Action restriction: launching the activity is restricted by a permission. */
248 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
249 /** Action restriction: launching the activity is restricted by an app op. */
250 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700251
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700252 // The height/width divide used when fitting a task within a bounds with method
253 // {@link #fitWithinBounds}.
254 // We always want the task to to be visible in the bounds without affecting its size when
255 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
256 // the input bounds right or bottom side minus the width or height divided by this value.
257 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
258
justinzhang5286d3f2014-05-12 17:06:01 -0400259 /** Status Bar Service **/
260 private IBinder mToken = new Binder();
261 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400262 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400263
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700264 // For debugging to make sure the caller when acquiring/releasing our
265 // wake lock is the system process.
266 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800267 /** The number of distinct task ids that can be assigned to the tasks of a single user */
268 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700269
Craig Mautner27084302013-03-25 08:05:25 -0700270 final ActivityManagerService mService;
271
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800272 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800273
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700274 final ActivityStackSupervisorHandler mHandler;
275
276 /** Short cut */
277 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700279
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700280 /** Counter for next free stack ID to use for dynamic activity stacks. */
281 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700282
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /**
284 * Maps the task identifier that activities are currently being started in to the userId of the
285 * task. Each time a new task is created, the entry for the userId of the task is incremented
286 */
287 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700288
Craig Mautner2420ead2013-04-01 17:13:20 -0700289 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800290 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700291
Craig Mautnere0a38842013-12-16 16:14:02 -0800292 /** The stack containing the launcher app. Assumed to always be attached to
293 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800294 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700295
Craig Mautnere0a38842013-12-16 16:14:02 -0800296 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800297 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Craig Mautner4a1cb222013-12-04 16:14:06 -0800299 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
300 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800301 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800302 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700303
304 /** List of activities that are waiting for a new activity to become visible before completing
305 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800306 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700307
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700308 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800309 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700310
311 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800312 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700313
Craig Mautnerde4ef022013-04-07 19:01:33 -0700314 /** List of activities that are ready to be stopped, but waiting for the next activity to
315 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700317
Craig Mautnerf3333272013-04-22 10:55:53 -0700318 /** List of activities that are ready to be finished, but waiting for the previous activity to
319 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700321
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800323 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700324
Craig Mautnerf3333272013-04-22 10:55:53 -0700325 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700326 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700327
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700328 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700329 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700330
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
332 * is being brought in front of us. */
333 boolean mUserLeaving = false;
334
Craig Mautner0eea92c2013-05-16 13:35:39 -0700335 /** Set when we have taken too long waiting to go to sleep. */
336 boolean mSleepTimeout = false;
337
338 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700339 * We don't want to allow the device to go to sleep while in the process
340 * of launching an activity. This is primarily to allow alarm intent
341 * receivers to launch an activity and get that to run before the device
342 * goes back to sleep.
343 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700344 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700345
346 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700347 * Set when the system is going to sleep, until we have
348 * successfully paused the current activity and released our wake lock.
349 * At that point the system is allowed to actually sleep.
350 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700351 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700352
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700353 /** Stack id of the front stack when user switched, indexed by userId. */
354 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700355
Craig Mautner4504de52013-12-20 09:06:56 -0800356 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700358 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800359
360 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700361 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362
Jeff Brownca9bc702014-02-11 14:32:56 -0800363 InputManagerInternal mInputManagerInternal;
364
Craig Mautner15df08a2015-04-01 12:17:18 -0700365 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
366 * may be finished until there is only one entry left. If this is empty the system is not
367 * in lockTask mode. */
368 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000369 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700370 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
371 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000372 */
373 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400374 /**
375 * Notifies the user when entering/exiting lock-task.
376 */
377 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800378
Wale Ogunwaled046a012015-12-24 13:05:59 -0800379 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800380 boolean inResumeTopActivity;
381
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700382 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700383 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700384 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700385
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700386 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
387 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800388 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700389
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700390 // The default minimal size that will be used if the activity doesn't specify its minimal size.
391 // It will be calculated when the default display gets added.
392 private int mDefaultMinimalSizeOfResizeableTask = -1;
393
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700394 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
395 private boolean mTaskLayersChanged = true;
396
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800397 private final ActivityMetricsLogger mActivityMetricsLogger;
398
Jorim Jaggidc249c42015-12-15 14:57:31 -0800399 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
400
Wale Ogunwale39381972015-12-17 17:15:29 -0800401 static class FindTaskResult {
402 ActivityRecord r;
403 boolean matchedByRootAffinity;
404 }
405 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
406
Craig Mautneree36c772014-07-16 14:56:05 -0700407 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100408 * Used to keep track whether app visibilities got changed since the last pause. Useful to
409 * determine whether to invoke the task stack change listener after pausing.
410 */
411 boolean mAppVisibilitiesChangedSinceLastPause;
412
413 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700414 * Description of a request to start a new activity, which has been held
415 * due to app switches being disabled.
416 */
417 static class PendingActivityLaunch {
418 final ActivityRecord r;
419 final ActivityRecord sourceRecord;
420 final int startFlags;
421 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700422 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700423
424 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700425 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700426 r = _r;
427 sourceRecord = _sourceRecord;
428 startFlags = _startFlags;
429 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700430 callerApp = _callerApp;
431 }
432
433 void sendErrorResult(String message) {
434 try {
435 if (callerApp.thread != null) {
436 callerApp.thread.scheduleCrash(message);
437 }
438 } catch (RemoteException e) {
439 Slog.e(TAG, "Exception scheduling crash of failed "
440 + "activity launcher sourceRecord=" + sourceRecord, e);
441 }
Craig Mautneree36c772014-07-16 14:56:05 -0700442 }
443 }
444
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800445 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700446 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700447 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800448 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800449 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700450 }
451
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800452 void setRecentTasks(RecentTasks recentTasks) {
453 mRecentTasks = recentTasks;
454 }
455
Jeff Brown2c43c332014-06-12 22:38:59 -0700456 /**
457 * At the time when the constructor runs, the power manager has not yet been
458 * initialized. So we initialize our wakelocks afterwards.
459 */
460 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700462 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700463 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700464 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700465 }
466
justinzhang5286d3f2014-05-12 17:06:01 -0400467 // This function returns a IStatusBarService. The value is from ServiceManager.
468 // getService and is cached.
469 private IStatusBarService getStatusBarService() {
470 synchronized (mService) {
471 if (mStatusBarService == null) {
472 mStatusBarService = IStatusBarService.Stub.asInterface(
473 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
474 if (mStatusBarService == null) {
475 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
476 }
477 }
478 return mStatusBarService;
479 }
480 }
481
Jason Monk35c62a42014-06-17 10:24:47 -0400482 private IDevicePolicyManager getDevicePolicyManager() {
483 synchronized (mService) {
484 if (mDevicePolicyManager == null) {
485 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
486 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
487 if (mDevicePolicyManager == null) {
488 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
489 }
490 }
491 return mDevicePolicyManager;
492 }
493 }
494
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700495 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 synchronized (mService) {
497 mWindowManager = wm;
498
499 mDisplayManager =
500 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
501 mDisplayManager.registerDisplayListener(this, null);
502
503 Display[] displays = mDisplayManager.getDisplays();
504 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
505 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800506 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700507 if (activityDisplay.mDisplay == null) {
508 throw new IllegalStateException("Default Display does not exist");
509 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800510 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700511 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 }
513
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800514 mHomeStack = mFocusedStack = mLastFocusedStack =
515 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800516
517 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 }
Craig Mautner27084302013-03-25 08:05:25 -0700519 }
520
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200521 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700522 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200523 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700524 }
525
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700526 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700528 }
529
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700532 }
533
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700534 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700535 if (stack == null) {
536 return false;
537 }
538
Craig Mautnerdf88d732014-01-27 09:21:32 -0800539 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
540 if (parent != null) {
541 stack = parent.task.stack;
542 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800543 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700544 }
545
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 /** The top most stack. */
547 boolean isFrontStack(ActivityStack stack) {
548 if (stack == null) {
549 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700551
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700552 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
553 if (parent != null) {
554 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800555 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700556 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
557 }
558
Wale Ogunwaled046a012015-12-24 13:05:59 -0800559 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800560 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
561 if (!focusCandidate.isFocusable()) {
562 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
563 focusCandidate = focusCandidate.getNextFocusableStackLocked();
564 }
565
566 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800567 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800568 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800569
Wale Ogunwaled046a012015-12-24 13:05:59 -0800570 EventLogTags.writeAmFocusedStack(
571 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
572 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
573 }
574
575 final ActivityRecord r = topRunningActivityLocked();
576 if (mService.mFocusedActivity != r) {
577 // The focus activity should always be the top activity in the focused stack.
578 // There will be chaos and anarchy if it isn't...
579 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
580 }
Craig Mautnerde313752015-01-22 14:28:03 -0800581
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800582 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800583 if (r != null && r.idle) {
584 checkFinishBootingLocked();
585 }
586 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700587 }
588
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700589 void moveHomeStackToFront(String reason) {
590 mHomeStack.moveToFront(reason);
591 }
592
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700593 /** Returns true if the focus activity was adjusted to the home stack top activity. */
594 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700595 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
596 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700597 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700598 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700599
Craig Mautner84984fa2014-06-19 11:19:20 -0700600 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700601
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700602 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700603 if (top == null) {
604 return false;
605 }
606 mService.setFocusedActivityLocked(top, reason);
607 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700608 }
609
Craig Mautner299f9602015-01-26 09:47:33 -0800610 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700611 if (!mService.mBooting && !mService.mBooted) {
612 // Not ready yet!
613 return false;
614 }
615
Craig Mautner84984fa2014-06-19 11:19:20 -0700616 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
617 mWindowManager.showRecentApps();
618 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700619 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700620
Craig Mautner84984fa2014-06-19 11:19:20 -0700621 if (prev != null) {
622 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
623 }
624
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700625 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
626 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800627 final String myReason = reason + " resumeHomeStackTask";
628
Mark Lua56ea122015-10-08 13:31:01 +0800629 // Only resume home activity if isn't finishing.
630 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800631 mService.setFocusedActivityLocked(r, myReason);
632 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700633 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800634 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700635 }
636
Craig Mautner8d341ef2013-03-26 09:03:27 -0700637 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700638 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700639 }
640
641 /**
642 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
643 * @param id Id of the task we would like returned.
644 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
645 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700646 * @param stackId The stack to restore the task to (default launch stack will be used if
647 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700648 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700649 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800650 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800652 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800653 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
654 ActivityStack stack = stacks.get(stackNdx);
655 TaskRecord task = stack.taskForIdLocked(id);
656 if (task != null) {
657 return task;
658 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700659 }
660 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800661
662 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700663 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800664 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800665 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800667 return null;
668 }
669
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700670 if (!restoreFromRecents) {
671 return task;
672 }
673
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700674 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700675 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
676 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800677 return null;
678 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700679 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800680 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700681 }
682
Craig Mautner6170f732013-04-02 13:05:23 -0700683 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
689 if (r != null) {
690 return r;
691 }
Craig Mautner6170f732013-04-02 13:05:23 -0700692 }
693 }
694 return null;
695 }
696
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000697 boolean isFocusedUserLockedProfile() {
698 final int userId = mFocusedStack.topRunningActivityLocked().userId;
699 return userId != UserHandle.myUserId()
700 && mService.mUserController.shouldConfirmCredentials(userId);
701 }
702
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800703 void setNextTaskIdForUserLocked(int taskId, int userId) {
704 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
705 if (taskId > currentTaskId) {
706 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700707 }
708 }
709
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800710 int getNextTaskIdForUserLocked(int userId) {
711 mRecentTasks.loadUserRecentsLocked(userId);
712 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
713 // for a userId u, a taskId can only be in the range
714 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
715 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
716 int candidateTaskId = currentTaskId;
717 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
718 INVALID_STACK_ID) != null) {
719 candidateTaskId++;
720 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
721 // Wrap around as there will be smaller task ids that are available now.
722 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700723 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800724 if (candidateTaskId == currentTaskId) {
725 // Something wrong!
726 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
727 throw new IllegalStateException("Cannot get an available task id."
728 + " Reached limit of " + MAX_TASK_IDS_PER_USER
729 + " running tasks per user.");
730 }
731 }
732 mCurTaskIdForUser.put(userId, candidateTaskId);
733 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700734 }
735
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800737 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700738 if (stack == null) {
739 return null;
740 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700741 ActivityRecord resumedActivity = stack.mResumedActivity;
742 if (resumedActivity == null || resumedActivity.app == null) {
743 resumedActivity = stack.mPausingActivity;
744 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700745 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700746 }
747 }
748 return resumedActivity;
749 }
750
Dianne Hackbornff072722014-09-24 10:56:28 -0700751 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700752 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
755 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
757 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700758 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 continue;
760 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700761 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 if (hr != null) {
763 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
764 && processName.equals(hr.processName)) {
765 try {
George Mount2c92c972014-03-20 09:38:23 -0700766 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 didSomething = true;
768 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700769 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 Slog.w(TAG, "Exception in new application when starting activity "
771 + hr.intent.getComponent().flattenToShortString(), e);
772 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700773 }
Craig Mautner20e72272013-04-01 13:45:53 -0700774 }
Craig Mautner20e72272013-04-01 13:45:53 -0700775 }
776 }
777 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700778 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700779 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700780 }
Craig Mautner20e72272013-04-01 13:45:53 -0700781 return didSomething;
782 }
783
784 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
786 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700789 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 continue;
791 }
792 final ActivityRecord resumedActivity = stack.mResumedActivity;
793 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700794 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800795 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 return false;
797 }
Craig Mautner20e72272013-04-01 13:45:53 -0700798 }
799 }
800 return true;
801 }
802
Craig Mautnerde4ef022013-04-07 19:01:33 -0700803 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
805 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
807 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700808 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700810 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800811 return false;
812 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700813 }
814 }
815 }
816 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700817 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
819 mLastFocusedStack + " to=" + mFocusedStack);
820 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700821 return true;
822 }
823
824 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800825 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800826 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
827 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800828 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
829 final ActivityStack stack = stacks.get(stackNdx);
830 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800831 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700832 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800833 return false;
834 }
835 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700837 }
838 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800839 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700840 }
841
Craig Mautner2acc3892013-09-23 10:28:14 -0700842 /**
843 * Pause all activities in either all of the stacks or just the back stacks.
844 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700845 * @return true if any activity was paused as a result of this call.
846 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700847 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700848 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800849 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
850 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
852 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700853 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700854 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700856 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
857 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700859 }
860 }
861 return someActivityPaused;
862 }
863
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700865 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800866 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
867 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
869 final ActivityStack stack = stacks.get(stackNdx);
870 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700871 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700873 Slog.d(TAG_STATES,
874 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 pausing = false;
876 } else {
877 return false;
878 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700879 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700880 }
881 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700882 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700883 }
884
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700885 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
886 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500887 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
889 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
891 final ActivityStack stack = stacks.get(stackNdx);
892 if (stack.mResumedActivity != null &&
893 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700894 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800895 }
896 }
897 }
898 }
899
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700900 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700901 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700902 }
903
904 void sendWaitingVisibleReportLocked(ActivityRecord r) {
905 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700906 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700907 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700908 if (w.who == null) {
909 changed = true;
910 w.timeout = false;
911 if (r != null) {
912 w.who = new ComponentName(r.info.packageName, r.info.name);
913 }
914 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
915 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700916 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700917 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700918 if (changed) {
919 mService.notifyAll();
920 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700921 }
922
923 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
924 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700925 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700926 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700927 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700928 if (w.who == null) {
929 changed = true;
930 w.timeout = timeout;
931 if (r != null) {
932 w.who = new ComponentName(r.info.packageName, r.info.name);
933 }
934 w.thisTime = thisTime;
935 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700936 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700938 if (changed) {
939 mService.notifyAll();
940 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700941 }
942
Craig Mautner29219d92013-04-16 20:19:12 -0700943 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800944 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700945 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700946 if (r != null) {
947 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700948 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700949
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800951 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
953 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800954 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700955 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700956 if (r != null) {
957 return r;
958 }
959 }
960 }
961 return null;
962 }
963
Dianne Hackborn09233282014-04-30 11:33:59 -0700964 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700965 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
967 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
972 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700973 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700975 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700976 }
977 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700978
979 // The lists are already sorted from most recent to oldest. Just pull the most recent off
980 // each list and add it to list. Stop when all lists are empty or maxNum reached.
981 while (maxNum > 0) {
982 long mostRecentActiveTime = Long.MIN_VALUE;
983 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 final int numTaskLists = runningTaskLists.size();
985 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
986 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700987 if (!stackTaskList.isEmpty()) {
988 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
989 if (lastActiveTime > mostRecentActiveTime) {
990 mostRecentActiveTime = lastActiveTime;
991 selectedStackList = stackTaskList;
992 }
993 }
994 }
995 if (selectedStackList != null) {
996 list.add(selectedStackList.remove(0));
997 --maxNum;
998 } else {
999 break;
1000 }
1001 }
Craig Mautner20e72272013-04-01 13:45:53 -07001002 }
1003
Todd Kennedy7440f172015-12-09 14:31:22 -08001004 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1005 ProfilerInfo profilerInfo) {
1006 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 if (aInfo != null) {
1008 // Store the found target back into the intent, because now that
1009 // we have it we never want to do this again. For example, if the
1010 // user navigates back to this point in the history, we should
1011 // always restart the exact same activity.
1012 intent.setComponent(new ComponentName(
1013 aInfo.applicationInfo.packageName, aInfo.name));
1014
1015 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001016 if (!aInfo.processName.equals("system")) {
1017 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 mService.setDebugApp(aInfo.processName, true, false);
1019 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001020
Man Caocfa78b22015-06-11 20:14:34 -07001021 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1022 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1023 }
1024
1025 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001026 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 }
1028 }
1029 }
1030 return aInfo;
1031 }
1032
Todd Kennedy7440f172015-12-09 14:31:22 -08001033 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1034 try {
1035 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1036 PackageManager.MATCH_DEFAULT_ONLY
1037 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1038 } catch (RemoteException e) {
1039 }
1040 return null;
1041 }
1042
1043 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1044 ProfilerInfo profilerInfo, int userId) {
1045 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1046 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1047 }
1048
Craig Mautner2420ead2013-04-01 17:13:20 -07001049 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001050 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001051 throws RemoteException {
1052
Craig Mautner2568c3a2015-03-26 14:22:34 -07001053 if (andResume) {
1054 r.startFreezingScreenLocked(app, 0);
1055 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001056
Craig Mautner2568c3a2015-03-26 14:22:34 -07001057 // schedule launch ticks to collect information about slow apps.
1058 r.startLaunchTickingLocked();
1059 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001060
1061 // Have the window manager re-evaluate the orientation of
1062 // the screen based on the new activity order. Note that
1063 // as a result of this, it can call back into the activity
1064 // manager with a new orientation. We don't care about that,
1065 // because the activity is not currently running so we are
1066 // just restarting it anyway.
1067 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001069 mService.mConfiguration,
1070 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001071 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001072 }
1073
1074 r.app = app;
1075 app.waitingToKill = null;
1076 r.launchCount++;
1077 r.lastLaunchTime = SystemClock.uptimeMillis();
1078
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001079 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001080
1081 int idx = app.activities.indexOf(r);
1082 if (idx < 0) {
1083 app.activities.add(r);
1084 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001085 mService.updateLruProcessLocked(app, true, null);
1086 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001087
Craig Mautner15df08a2015-04-01 12:17:18 -07001088 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001089 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1090 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001091 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001092 }
1093
1094 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001095 try {
1096 if (app.thread == null) {
1097 throw new RemoteException();
1098 }
1099 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001100 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001101 if (andResume) {
1102 results = r.results;
1103 newIntents = r.newIntents;
1104 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001105 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1106 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1107 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001108 if (andResume) {
1109 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1110 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001111 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001113 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001114 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001117 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001118 r.sleeping = false;
1119 r.forceNewConfig = false;
1120 mService.showAskCompatModeDialogLocked(r);
1121 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001122 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001123 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1124 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1125 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001126 final String profileFile = mService.mProfileFile;
1127 if (profileFile != null) {
1128 ParcelFileDescriptor profileFd = mService.mProfileFd;
1129 if (profileFd != null) {
1130 try {
1131 profileFd = profileFd.dup();
1132 } catch (IOException e) {
1133 if (profileFd != null) {
1134 try {
1135 profileFd.close();
1136 } catch (IOException o) {
1137 }
1138 profileFd = null;
1139 }
1140 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001141 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001142
1143 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1144 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001145 }
1146 }
1147 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001148
Craig Mautner2568c3a2015-03-26 14:22:34 -07001149 if (andResume) {
1150 app.hasShownUi = true;
1151 app.pendingUiClean = true;
1152 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001153 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001155 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001156 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001157 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001158 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001159
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001160 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 // This may be a heavy-weight process! Note that the package
1162 // manager will ensure that only activity can run in the main
1163 // process of the .apk, which is the only thing that will be
1164 // considered heavy-weight.
1165 if (app.processName.equals(app.info.packageName)) {
1166 if (mService.mHeavyWeightProcess != null
1167 && mService.mHeavyWeightProcess != app) {
1168 Slog.w(TAG, "Starting new heavy weight process " + app
1169 + " when already running "
1170 + mService.mHeavyWeightProcess);
1171 }
1172 mService.mHeavyWeightProcess = app;
1173 Message msg = mService.mHandler.obtainMessage(
1174 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1175 msg.obj = r;
1176 mService.mHandler.sendMessage(msg);
1177 }
1178 }
1179
1180 } catch (RemoteException e) {
1181 if (r.launchFailed) {
1182 // This is the second time we failed -- finish activity
1183 // and give up.
1184 Slog.e(TAG, "Second failure launching "
1185 + r.intent.getComponent().flattenToShortString()
1186 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001187 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1189 "2nd-crash", false);
1190 return false;
1191 }
1192
1193 // This is the first time we failed -- restart process and
1194 // retry.
1195 app.activities.remove(r);
1196 throw e;
1197 }
1198
1199 r.launchFailed = false;
1200 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001201 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 }
1203
1204 if (andResume) {
1205 // As part of the process of launching, ActivityThread also performs
1206 // a resume.
1207 stack.minimalResumeActivityLocked(r);
1208 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001209 // This activity is not starting in the resumed state... which should look like we asked
1210 // it to pause+stop (but remain visible), and it has done so and reported back the
1211 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001212 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001213 "Moving to PAUSED: " + r + " (starting in paused state)");
1214 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 }
1216
1217 // Launch the new version setup screen if needed. We do this -after-
1218 // launching the initial activity (that is, home), so that it can have
1219 // a chance to initialize itself while in the background, making the
1220 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001221 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 mService.startSetupActivityLocked();
1223 }
1224
Dianne Hackborn465fa392014-09-14 14:21:18 -07001225 // Update any services we are bound to that might care about whether
1226 // their client may have activities.
1227 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1228
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 return true;
1230 }
1231
Craig Mautnere79d42682013-04-01 19:01:53 -07001232 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001233 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001234 // Is this activity's application already running?
1235 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001236 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001237
1238 r.task.stack.setLaunchTime(r);
1239
1240 if (app != null && app.thread != null) {
1241 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001242 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1243 || !"android".equals(r.info.packageName)) {
1244 // Don't add this if it is a platform component that is marked
1245 // to run in multiple processes, because this is actually
1246 // part of the framework so doesn't make sense to track as a
1247 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001248 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1249 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001250 }
George Mount2c92c972014-03-20 09:38:23 -07001251 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001252 return;
1253 } catch (RemoteException e) {
1254 Slog.w(TAG, "Exception when starting activity "
1255 + r.intent.getComponent().flattenToShortString(), e);
1256 }
1257
1258 // If a dead object exception was thrown -- fall through to
1259 // restart the application.
1260 }
1261
1262 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001263 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001264 }
1265
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001266 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001267 String resultWho, int requestCode, int callingPid, int callingUid,
1268 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1269 ActivityRecord resultRecord, ActivityStack resultStack) {
1270 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1271 callingUid);
1272 if (startAnyPerm == PERMISSION_GRANTED) {
1273 return true;
1274 }
1275 final int componentRestriction = getComponentRestrictionForCallingPackage(
1276 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1277 final int actionRestriction = getActionRestrictionForCallingPackage(
1278 intent.getAction(), callingPackage, callingPid, callingUid);
1279 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1280 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1281 if (resultRecord != null) {
1282 resultStack.sendActivityResultLocked(-1,
1283 resultRecord, resultWho, requestCode,
1284 Activity.RESULT_CANCELED, null);
1285 }
1286 final String msg;
1287 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1288 msg = "Permission Denial: starting " + intent.toString()
1289 + " from " + callerApp + " (pid=" + callingPid
1290 + ", uid=" + callingUid + ")" + " with revoked permission "
1291 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1292 } else if (!aInfo.exported) {
1293 msg = "Permission Denial: starting " + intent.toString()
1294 + " from " + callerApp + " (pid=" + callingPid
1295 + ", uid=" + callingUid + ")"
1296 + " not exported from uid " + aInfo.applicationInfo.uid;
1297 } else {
1298 msg = "Permission Denial: starting " + intent.toString()
1299 + " from " + callerApp + " (pid=" + callingPid
1300 + ", uid=" + callingUid + ")"
1301 + " requires " + aInfo.permission;
1302 }
1303 Slog.w(TAG, msg);
1304 throw new SecurityException(msg);
1305 }
1306
1307 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1308 final String message = "Appop Denial: starting " + intent.toString()
1309 + " from " + callerApp + " (pid=" + callingPid
1310 + ", uid=" + callingUid + ")"
1311 + " requires " + AppOpsManager.permissionToOp(
1312 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1313 Slog.w(TAG, message);
1314 return false;
1315 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1316 final String message = "Appop Denial: starting " + intent.toString()
1317 + " from " + callerApp + " (pid=" + callingPid
1318 + ", uid=" + callingUid + ")"
1319 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1320 Slog.w(TAG, message);
1321 return false;
1322 }
1323 return true;
1324 }
1325
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001326 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001327 final long identity = Binder.clearCallingIdentity();
1328 try {
1329 return UserManager.get(mService.mContext).getUserInfo(userId);
1330 } finally {
1331 Binder.restoreCallingIdentity(identity);
1332 }
1333 }
1334
Svet Ganov99b60432015-06-27 13:15:22 -07001335 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001336 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001337 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1338 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001339 == PackageManager.PERMISSION_DENIED) {
1340 return ACTIVITY_RESTRICTION_PERMISSION;
1341 }
1342
Christopher Tateff7add02015-08-17 10:23:22 -07001343 if (activityInfo.permission == null) {
1344 return ACTIVITY_RESTRICTION_NONE;
1345 }
1346
Svet Ganov99b60432015-06-27 13:15:22 -07001347 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1348 if (opCode == AppOpsManager.OP_NONE) {
1349 return ACTIVITY_RESTRICTION_NONE;
1350 }
1351
1352 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1353 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001354 if (!ignoreTargetSecurity) {
1355 return ACTIVITY_RESTRICTION_APPOP;
1356 }
Svet Ganov99b60432015-06-27 13:15:22 -07001357 }
1358
1359 return ACTIVITY_RESTRICTION_NONE;
1360 }
1361
Svetoslav7008b512015-06-24 18:47:07 -07001362 private int getActionRestrictionForCallingPackage(String action,
1363 String callingPackage, int callingPid, int callingUid) {
1364 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001365 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001366 }
1367
1368 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1369 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001370 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001371 }
1372
1373 final PackageInfo packageInfo;
1374 try {
1375 packageInfo = mService.mContext.getPackageManager()
1376 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1377 } catch (PackageManager.NameNotFoundException e) {
1378 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001379 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001380 }
1381
1382 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001383 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001384 }
1385
1386 if (mService.checkPermission(permission, callingPid, callingUid) ==
1387 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001388 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001389 }
1390
1391 final int opCode = AppOpsManager.permissionToOpCode(permission);
1392 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001393 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001394 }
1395
1396 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1397 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001398 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001399 }
1400
Svet Ganov99b60432015-06-27 13:15:22 -07001401 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001402 }
1403
Wale Ogunwaled046a012015-12-24 13:05:59 -08001404 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001405 if (r == null) {
1406 // Not sure what you are trying to do, but it is not going to work...
1407 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001408 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001409 final TaskRecord task = r.task;
1410 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001411 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001412 return false;
1413 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001414 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001415 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001416 }
1417
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001418 void setLaunchSource(int uid) {
1419 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1420 }
1421
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001422 void acquireLaunchWakelock() {
1423 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1424 throw new IllegalStateException("Calling must be system uid");
1425 }
1426 mLaunchingActivity.acquire();
1427 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1428 // To be safe, don't allow the wake lock to be held for too long.
1429 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1430 }
1431 }
1432
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001433 /**
1434 * Called when the frontmost task is idle.
1435 * @return the state of mService.mBooting before this was called.
1436 */
1437 private boolean checkFinishBootingLocked() {
1438 final boolean booting = mService.mBooting;
1439 boolean enableScreen = false;
1440 mService.mBooting = false;
1441 if (!mService.mBooted) {
1442 mService.mBooted = true;
1443 enableScreen = true;
1444 }
1445 if (booting || enableScreen) {
1446 mService.postFinishBooting(booting, enableScreen);
1447 }
1448 return booting;
1449 }
1450
Craig Mautnerf3333272013-04-22 10:55:53 -07001451 // Checked.
1452 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1453 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001454 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001455
Craig Mautnerf3333272013-04-22 10:55:53 -07001456 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001457 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001458 int NS = 0;
1459 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001460 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001461 boolean activityRemoved = false;
1462
Wale Ogunwale7d701172015-03-11 15:36:30 -07001463 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001464 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001465 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1466 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001467 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1468 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001469 if (fromTimeout) {
1470 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001471 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001472
1473 // This is a hack to semi-deal with a race condition
1474 // in the client where it can be constructed with a
1475 // newer configuration from when we asked it to launch.
1476 // We'll update with whatever configuration it now says
1477 // it used to launch.
1478 if (config != null) {
1479 r.configuration = config;
1480 }
1481
1482 // We are now idle. If someone is waiting for a thumbnail from
1483 // us, we can now deliver.
1484 r.idle = true;
1485
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001486 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001487 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001488 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001489 }
1490 }
1491
1492 if (allResumedActivitiesIdle()) {
1493 if (r != null) {
1494 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001495 }
1496
1497 if (mLaunchingActivity.isHeld()) {
1498 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1499 if (VALIDATE_WAKE_LOCK_CALLER &&
1500 Binder.getCallingUid() != Process.myUid()) {
1501 throw new IllegalStateException("Calling must be system uid");
1502 }
1503 mLaunchingActivity.release();
1504 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001505 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001506 }
1507
1508 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001509 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001510 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001511 if ((NF = mFinishingActivities.size()) > 0) {
1512 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001513 mFinishingActivities.clear();
1514 }
1515
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001517 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 mStartingUsers.clear();
1519 }
1520
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 // Stop any activities that are scheduled to do so but have been
1522 // waiting for the next one to start.
1523 for (int i = 0; i < NS; i++) {
1524 r = stops.get(i);
1525 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001526 if (stack != null) {
1527 if (r.finishing) {
1528 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1529 } else {
1530 stack.stopActivityLocked(r);
1531 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001532 }
1533 }
1534
1535 // Finish any activities that are scheduled to do so but have been
1536 // waiting for the next one to start.
1537 for (int i = 0; i < NF; i++) {
1538 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001539 final ActivityStack stack = r.task.stack;
1540 if (stack != null) {
1541 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1542 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001543 }
1544
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001545 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001546 // Complete user switch
1547 if (startingUsers != null) {
1548 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001549 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001550 }
1551 }
1552 // Complete starting up of background users
1553 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001554 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001555 mStartingBackgroundUsers.clear();
1556 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001557 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001558 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001559 }
1560 }
1561
1562 mService.trimApplications();
1563 //dump();
1564 //mWindowManager.dump();
1565
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001567 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001568 }
1569
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001570 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001571 }
1572
Craig Mautner8e569572013-10-11 17:36:59 -07001573 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001574 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001575 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1576 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001577 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1578 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1579 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001580 }
Craig Mautner19091252013-10-05 00:03:53 -07001581 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001582 }
1583
1584 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001585 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1586 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1588 stacks.get(stackNdx).closeSystemDialogsLocked();
1589 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001590 }
1591 }
1592
Craig Mautner93529a42013-10-04 15:03:13 -07001593 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001594 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001595 }
1596
Craig Mautner8d341ef2013-03-26 09:03:27 -07001597 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001598 * Update the last used stack id for non-current user (current user's last
1599 * used stack is the focused stack)
1600 */
1601 void updateUserStackLocked(int userId, ActivityStack stack) {
1602 if (userId != mCurrentUser) {
1603 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1604 }
1605 }
1606
1607 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001608 * @return true if some activity was finished (or would have finished if doit were true).
1609 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001610 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1611 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001612 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001616 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001617 if (stack.finishDisabledPackageActivitiesLocked(
1618 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001619 didSomething = true;
1620 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001621 }
1622 }
1623 return didSomething;
1624 }
1625
Dianne Hackborna413dc02013-07-12 12:02:55 -07001626 void updatePreviousProcessLocked(ActivityRecord r) {
1627 // Now that this process has stopped, we may want to consider
1628 // it to be the previous app to try to keep around in case
1629 // the user wants to return to it.
1630
1631 // First, found out what is currently the foreground app, so that
1632 // we don't blow away the previous app if this activity is being
1633 // hosted by the process that is actually still the foreground.
1634 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001635 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1636 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001637 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1638 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001639 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001640 if (stack.mResumedActivity != null) {
1641 fgApp = stack.mResumedActivity.app;
1642 } else if (stack.mPausingActivity != null) {
1643 fgApp = stack.mPausingActivity.app;
1644 }
1645 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001646 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001647 }
1648 }
1649
1650 // Now set this one as the previous process, only if that really
1651 // makes sense to.
1652 if (r.app != null && fgApp != null && r.app != fgApp
1653 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001654 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001655 mService.mPreviousProcess = r.app;
1656 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1657 }
1658 }
1659
Wale Ogunwaled046a012015-12-24 13:05:59 -08001660 boolean resumeFocusedStackTopActivityLocked() {
1661 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001662 }
1663
Wale Ogunwaled046a012015-12-24 13:05:59 -08001664 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001665 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001666 if (targetStack != null && isFocusedStack(targetStack)) {
1667 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001668 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001669 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1670 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001671 }
1672
Adrian Roos20d7df32016-01-12 18:59:43 +01001673 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1674 TaskRecord finishedTask = null;
1675 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1677 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001678 final int numStacks = stacks.size();
1679 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1680 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001681 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1682 if (stack == focusedStack || finishedTask == null) {
1683 finishedTask = t;
1684 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001685 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001686 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001687 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001688 }
1689
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001690 void finishVoiceTask(IVoiceInteractionSession session) {
1691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1692 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1693 final int numStacks = stacks.size();
1694 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1695 final ActivityStack stack = stacks.get(stackNdx);
1696 stack.finishVoiceTask(session);
1697 }
1698 }
1699 }
1700
Chong Zhang280d3322015-11-03 17:27:26 -08001701 void findTaskToMoveToFrontLocked(
1702 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001703 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1704 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001705 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001706 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1707 // Caller wants the home activity moved with it. To accomplish this,
1708 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001709 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001710 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001711 if (task.stack == null) {
1712 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1713 + task + " to front. Stack is null");
1714 return;
1715 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001716
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001717 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001718 int stackId = options.getLaunchStackId();
1719 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001720 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001721 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001722 if (stackId == INVALID_STACK_ID) {
1723 stackId = task.getLaunchStackId();
1724 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001725 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001726 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1727 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1728 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001729 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1730 // still need moveTaskToFrontLocked() below for any transition settings.
1731 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001732 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1733 resizeStackLocked(stackId, bounds,
1734 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1735 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1736 } else {
1737 // WM resizeTask must be done after the task is moved to the correct stack,
1738 // because Task's setBounds() also updates dim layer's bounds, but that has
1739 // dependency on the stack.
1740 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1741 false /* relayout */, false /* forced */);
1742 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001743 }
1744 }
1745
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001746 final ActivityRecord r = task.getTopActivity();
1747 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1748 r == null ? null : r.appTimeTracker, reason);
1749
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001750 if (DEBUG_STACK) Slog.d(TAG_STACK,
1751 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001752 }
1753
Wale Ogunwale854809c2015-12-27 16:18:19 -08001754 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001755 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001756 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001757 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001758 return false;
1759 }
1760 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1761 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001762 }
1763
Craig Mautner967212c2013-04-13 21:10:58 -07001764 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001765 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001766 }
1767
1768 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001769 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1770 if (activityContainer != null) {
1771 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001772 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001773 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001774 return null;
1775 }
1776 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001777 }
1778
Craig Mautner967212c2013-04-13 21:10:58 -07001779 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001780 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1782 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001783 }
1784 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001785 }
1786
Craig Mautner4a1cb222013-12-04 16:14:06 -08001787 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001788 ActivityRecord homeActivity = getHomeActivity();
1789 if (homeActivity != null) {
1790 return homeActivity.appToken;
1791 }
1792 return null;
1793 }
1794
1795 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001796 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001797 }
1798
1799 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001800 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1801 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1802 final TaskRecord task = tasks.get(taskNdx);
1803 if (task.isHomeTask()) {
1804 final ArrayList<ActivityRecord> activities = task.mActivities;
1805 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1806 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001807 if (r.isHomeActivity()
1808 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001809 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001810 }
1811 }
1812 }
1813 }
1814 return null;
1815 }
1816
Chong Zhangb15758a2015-11-17 12:12:03 -08001817 /**
1818 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1819 * the docked stack itself, or if it's side-by-side to the docked stack.
1820 */
1821 boolean isStackDockedInEffect(int stackId) {
1822 return stackId == DOCKED_STACK_ID ||
1823 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1824 }
1825
Todd Kennedyca4d8422015-01-15 15:19:22 -08001826 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001827 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001828 ActivityContainer activityContainer =
1829 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001830 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001831 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1832 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001833 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001834 return activityContainer;
1835 }
1836
Craig Mautner34b73df2014-01-12 21:11:08 -08001837 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001838 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1839 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1840 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001841 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1842 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001843 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001844 }
1845 }
1846
Craig Mautner95da1082014-02-24 17:54:35 -08001847 void deleteActivityContainer(IActivityContainer container) {
1848 ActivityContainer activityContainer = (ActivityContainer)container;
1849 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001850 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1851 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001852 final int stackId = activityContainer.mStackId;
1853 mActivityContainers.remove(stackId);
1854 mWindowManager.removeStack(stackId);
1855 }
1856 }
1857
Jorim Jaggidc249c42015-12-15 14:57:31 -08001858 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1859 boolean preserveWindows, boolean allowResizeInDockedMode) {
1860 if (stackId == DOCKED_STACK_ID) {
1861 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1862 preserveWindows);
1863 return;
1864 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001865 final ActivityStack stack = getStack(stackId);
1866 if (stack == null) {
1867 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1868 return;
1869 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001870
Jorim Jaggidc249c42015-12-15 14:57:31 -08001871 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001872 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1873 // stack size changing so things don't get out of sync.
1874 return;
1875 }
1876
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001877 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001878 mWindowManager.deferSurfaceLayout();
1879 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001880 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1881 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001882 } finally {
1883 mWindowManager.continueSurfaceLayout();
1884 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001885 }
1886 }
1887
Jorim Jaggidc249c42015-12-15 14:57:31 -08001888 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1889 Rect tempTaskInsetBounds) {
1890 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1891 // The bounds passed in corresponds to the fullscreen bounds which we normally
1892 // represent with null. Go ahead and set it to null so that all tasks configuration
1893 // can have the right fullscreen state.
1894 bounds = null;
1895 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001896 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001897
1898 mTmpBounds.clear();
1899 mTmpConfigs.clear();
1900 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001901 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001902 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001903 final TaskRecord task = tasks.get(i);
1904 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001905 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1906 // For freeform stack we don't adjust the size of the tasks to match that
1907 // of the stack, but we do try to make sure the tasks are still contained
1908 // with the bounds of the stack.
1909 tempRect2.set(task.mBounds);
1910 fitWithinBounds(tempRect2, bounds);
1911 task.updateOverrideConfiguration(tempRect2);
1912 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001913 task.updateOverrideConfiguration(
1914 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001915 }
1916 }
1917
1918 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1919 mTmpBounds.put(task.taskId, task.mBounds);
1920 if (tempTaskInsetBounds != null) {
1921 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1922 }
1923 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001924
1925 // We might trigger a configuration change. Save the current task bounds for freezing.
1926 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001927 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1928 mTmpBounds, mTmpInsetBounds);
1929 stack.setBounds(bounds);
1930 }
1931
1932 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1933 boolean preserveWindows) {
1934 if (r == null) {
1935 return;
1936 }
1937 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1938 preserveWindows);
1939 // And we need to make sure at this point that all other activities
1940 // are made visible with the correct configuration.
1941 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1942 if (!updated) {
1943 resumeFocusedStackTopActivityLocked();
1944 }
1945 }
1946
1947 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1948 Rect tempDockedTaskInsetBounds,
1949 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1950 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1951 if (stack == null) {
1952 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1953 return;
1954 }
1955
1956 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1957 mWindowManager.deferSurfaceLayout();
1958 try {
1959 ActivityRecord r = stack.topRunningActivityLocked();
1960 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1961 tempDockedTaskInsetBounds);
1962
1963 if (stack.mFullscreen) {
1964 // The dock stack went fullscreen which is kinda like dismissing it.
1965 // In this case we make all other static stacks fullscreen and move all
1966 // docked stack tasks to the fullscreen stack.
1967 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1968 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1969 resizeStackLocked(i, null, null, null, preserveWindows,
1970 true /* allowResizeInDockedMode */);
1971 }
1972 }
1973
1974 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1975 final int count = tasks.size();
1976 for (int i = 0; i < count; i++) {
1977 moveTaskToStackLocked(tasks.get(i).taskId,
1978 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1979 false /* animate */);
1980 }
1981
1982 // stack shouldn't contain anymore activities, so nothing to resume.
1983 r = null;
1984 } else {
1985 // Docked stacks occupy a dedicated region on screen so the size of all other
1986 // static stacks need to be adjusted so they don't overlap with the docked stack.
1987 // We get the bounds to use from window manager which has been adjusted for any
1988 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08001989 mWindowManager.getStackDockedModeBounds(
1990 HOME_STACK_ID, tempRect, true /* ignoreVisibilityOnKeyguardShowing */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001991 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1992 if (StackId.isResizeableByDockedStack(i)) {
1993 ActivityStack otherStack = getStack(i);
1994 if (otherStack != null) {
1995 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1996 tempOtherTaskInsetBounds, preserveWindows,
1997 true /* allowResizeInDockedMode */);
1998 }
1999 }
2000 }
2001 }
2002 ensureConfigurationAndResume(stack, r, preserveWindows);
2003 } finally {
2004 mWindowManager.continueSurfaceLayout();
2005 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2006 }
2007
2008 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2009 tempDockedTaskBounds != null
2010 || tempDockedTaskInsetBounds != null
2011 || tempOtherTaskBounds != null
2012 || tempOtherTaskInsetBounds != null);
2013 }
2014
Chong Zhangf596cd52016-01-05 13:42:44 -08002015 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002016 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002017 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002018 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002019 }
2020
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002021 adjustForMinimalTaskDimensions(task, bounds);
2022
Chong Zhang87b21722015-09-21 15:39:51 -07002023 // If this is a forced resize, let it go through even if the bounds is not changing,
2024 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002025 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002026 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002027 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002028 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002029 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002030 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002031
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002032 if (!mWindowManager.isValidTaskId(task.taskId)) {
2033 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002034 // All we can do for now is update the bounds so it can be used when the task is
2035 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002036 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002037 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2038 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002039 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002040 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002041 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002042 }
2043
Chong Zhang6de2ae82015-09-30 18:25:21 -07002044 // Do not move the task to another stack here.
2045 // This method assumes that the task is already placed in the right stack.
2046 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002047
Chong Zhang6de2ae82015-09-30 18:25:21 -07002048 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002049
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002050 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002051 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002052 // way and the activity was kept the way it was. If it's false, it means the activity had
2053 // to be relaunched due to configuration change.
2054 boolean kept = true;
2055 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002056 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002057 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002058 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002059 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002060 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002061 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002062 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002063 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002064 }
2065 }
2066 }
Chong Zhang87b21722015-09-21 15:39:51 -07002067 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002068
2069 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002070 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002071 }
2072
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002073 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2074 if (bounds == null) {
2075 return;
2076 }
2077 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2078 : task.mMinimalSize;
2079 final boolean adjustWidth = minimalSize > bounds.width();
2080 final boolean adjustHeight = minimalSize > bounds.height();
2081 if (!(adjustWidth || adjustHeight)) {
2082 return;
2083 }
2084 Rect taskBounds = task.mBounds;
2085 if (adjustWidth) {
2086 if (taskBounds != null && bounds.right == taskBounds.right) {
2087 bounds.left = bounds.right - minimalSize;
2088 } else {
2089 // Either left bounds match, or neither match, or the previous bounds were
2090 // fullscreen and we default to keeping left.
2091 bounds.right = bounds.left + minimalSize;
2092 }
2093 }
2094 if (adjustHeight) {
2095 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2096 bounds.top = bounds.bottom - minimalSize;
2097 } else {
2098 // Either top bounds match, or neither match, or the previous bounds were
2099 // fullscreen and we default to keeping top.
2100 bounds.bottom = bounds.top + minimalSize;
2101 }
2102 }
2103 }
2104
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002105 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002106 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2107 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002108 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002109 }
2110
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002111 ActivityContainer activityContainer = new ActivityContainer(stackId);
2112 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002113 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002114 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002115 }
2116
2117 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002118 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002119 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2120 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002121 break;
2122 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002123 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002124 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002125 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002126 }
2127
Chong Zhang5dcb2752015-08-18 13:50:26 -07002128 /**
2129 * Restores a recent task to a stack
2130 * @param task The recent task to be restored.
2131 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002132 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002133 * @return true if the task has been restored successfully.
2134 */
2135 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2136 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002137 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002138 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2139 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2140 // Put it in the fullscreen stack.
2141 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002142 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002143
Wale Ogunwale706ed792015-08-02 10:29:44 -07002144 if (task.stack != null) {
2145 // Task has already been restored once. See if we need to do anything more
2146 if (task.stack.mStackId == stackId) {
2147 // Nothing else to do since it is already restored in the right stack.
2148 return true;
2149 }
2150 // Remove current stack association, so we can re-associate the task with the
2151 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002152 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002153 }
2154
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002155 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002156 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002157
2158 if (stack == null) {
2159 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002160 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2161 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002162 return false;
2163 }
2164
Wale Ogunwale5f986092015-12-04 15:35:38 -08002165 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002166 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2167 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002168 final ArrayList<ActivityRecord> activities = task.mActivities;
2169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002170 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002171 }
2172 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002173 }
2174
Wale Ogunwale040b4702015-08-06 18:10:50 -07002175 /**
2176 * Moves the specified task record to the input stack id.
2177 * WARNING: This method performs an unchecked/raw move of the task and
2178 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002179 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002180 * @param task Task to move.
2181 * @param stackId Id of stack to move task to.
2182 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002183 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002184 * @param reason Reason the task is been moved.
2185 * @return The stack the task was moved to.
2186 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002187 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002188 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2189 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002190 final ActivityStack prevStack = task.stack;
2191 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2192 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2193 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2194 // Whenever we are moving the top activity from the front stack we want to make sure to move
2195 // the stack to the front.
2196 final boolean wasFront = isFrontStack(prevStack)
2197 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002198
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002199 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002200
2201 if (stackId == DOCKED_STACK_ID && resizeMode == RESIZE_MODE_UNRESIZEABLE) {
2202 // We don't allow moving a unresizeable task to the docked stack since the docked
2203 // stack is used for split-screen mode and will cause things like the docked divider to
2204 // show up. We instead leave the task in its current stack or move it to the fullscreen
2205 // stack if it isn't currently in a stack.
2206 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002207 Slog.w(TAG, "Can not move unresizeable task=" + task
2208 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2209 }
2210
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002211 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2212 // if a docked stack is created below which will lead to the stack we are moving from and
2213 // its resizeable tasks being resized.
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002214 task.mResizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002215 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002216 task.mResizeMode = resizeMode;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002217 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002218 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002219
2220 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002221 // move focus to the new stack by moving the stack to the front.
2222 stack.moveToFrontAndResumeStateIfNeeded(
2223 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002224
Wale Ogunwale040b4702015-08-06 18:10:50 -07002225 return stack;
2226 }
2227
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002228 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002229 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002230 final TaskRecord task = anyTaskForIdLocked(taskId);
2231 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002232 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002233 return;
2234 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002235
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002236 if (task.stack != null && task.stack.mStackId == stackId) {
2237 // You are already in the right stack silly...
2238 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2239 return;
2240 }
2241
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002242 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002243 final boolean mightReplaceWindow =
2244 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2245 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002246 // We are about to relaunch the activity because its configuration changed due to
2247 // being maximized, i.e. size change. The activity will first remove the old window
2248 // and then add a new one. This call will tell window manager about this, so it can
2249 // preserve the old window until the new one is drawn. This prevents having a gap
2250 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002251 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002252 // Note here we always set the replacing window first, as the flags might be needed
2253 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002254 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002255 }
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002256 final int preferredLaunchStackId = stackId;
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002257 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2258 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale513346d2016-01-27 10:55:01 -08002259 stackId = stack.mStackId;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002260
Jorim Jaggi55387522015-11-24 18:21:10 -08002261 if (!animate) {
2262 stack.mNoAnimActivities.add(topActivity);
2263 }
2264
Chong Zhangf596cd52016-01-05 13:42:44 -08002265 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002266 // Make sure the task has the appropriate bounds/size for the stack it is in.
2267 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002268 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002269 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2270 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002271 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002272 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002273 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002274 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2275 }
2276
2277 if (mightReplaceWindow) {
2278 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2279 // window), we need to clear the replace window settings. Otherwise, we schedule a
2280 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002281 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002282 }
2283
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002284 // The task might have already been running and its visibility needs to be synchronized with
2285 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002286 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002287 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002288
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002289 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002290 }
2291
Wale Ogunwale079a0042015-10-24 11:44:07 -07002292 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2293 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2294 if (stack == null) {
2295 throw new IllegalArgumentException(
2296 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2297 }
2298
2299 final ActivityRecord r = stack.topRunningActivityLocked();
2300 if (r == null) {
2301 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2302 + " in stack=" + stack);
2303 return false;
2304 }
2305
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002306 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002307 Slog.w(TAG,
2308 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002309 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002310 return false;
2311 }
2312
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002313 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", null);
2314 mWindowManager.animateResizePinnedStack(bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002315 return true;
2316 }
2317
2318 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002319 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002320 if (task.mActivities.size() == 1) {
2321 // 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 -08002322 // stack without re-parenting the activity in a different task.
2323 moveTaskToStackLocked(
2324 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002325 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002326 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2327 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002328 }
2329
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002330 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002331 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2332 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002333 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002334
2335 // The task might have already been running and its visibility needs to be synchronized with
2336 // the visibility of the stack / windows.
2337 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002338 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002339
2340 if (stackId == PINNED_STACK_ID) {
2341 mService.notifyActivityPinnedLocked();
2342 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002343 }
2344
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002345 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2346 final TaskRecord task = anyTaskForIdLocked(taskId);
2347 if (task == null) {
2348 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2349 return;
2350 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002351 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2352
2353 task.updateOverrideConfigurationForStack(stack);
2354
2355 mWindowManager.positionTaskInStack(
2356 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002357 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002358 // The task might have already been running and its visibility needs to be synchronized with
2359 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002360 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002361 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002362 }
2363
Craig Mautnerac6f8432013-07-17 13:24:59 -07002364 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002365 mTmpFindTaskResult.r = null;
2366 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002367 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002368 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2369 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002370 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2371 final ActivityStack stack = stacks.get(stackNdx);
2372 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002373 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002374 continue;
2375 }
2376 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002377 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2378 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002379 continue;
2380 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002381 stack.findTaskLocked(r, mTmpFindTaskResult);
2382 // It is possible to have task in multiple stacks with the same root affinity.
2383 // If the match we found was based on root affinity we keep on looking to see if
2384 // there is a better match in another stack. We eventually return the match based
2385 // on root affinity if we don't find a better match.
2386 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2387 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002388 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 }
2390 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002391 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2392 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002393 }
2394
2395 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002396 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2397 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002398 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2399 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2400 if (ar != null) {
2401 return ar;
2402 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002403 }
2404 }
2405 return null;
2406 }
2407
Craig Mautner8d341ef2013-03-26 09:03:27 -07002408 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002409 scheduleSleepTimeout();
2410 if (!mGoingToSleep.isHeld()) {
2411 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002412 if (mLaunchingActivity.isHeld()) {
2413 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2414 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002415 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002416 mLaunchingActivity.release();
2417 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002418 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002419 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002420 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002421 }
2422
2423 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002424 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002426 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002427 final long endTime = System.currentTimeMillis() + timeout;
2428 while (true) {
2429 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002430 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2431 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002432 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2433 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2434 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002435 }
2436 if (cantShutdown) {
2437 long timeRemaining = endTime - System.currentTimeMillis();
2438 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002439 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002440 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002441 } catch (InterruptedException e) {
2442 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002443 } else {
2444 Slog.w(TAG, "Activity manager shutdown timed out");
2445 timedout = true;
2446 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002447 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002448 } else {
2449 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002450 }
2451 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002452
2453 // Force checkReadyForSleep to complete.
2454 mSleepTimeout = true;
2455 checkReadyForSleepLocked();
2456
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 return timedout;
2458 }
2459
2460 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002461 removeSleepTimeouts();
2462 if (mGoingToSleep.isHeld()) {
2463 mGoingToSleep.release();
2464 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002465 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2466 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002467 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2468 final ActivityStack stack = stacks.get(stackNdx);
2469 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002470 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002471 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002472 }
Craig Mautner5314a402013-09-26 12:40:16 -07002473 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002474 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002475 mGoingToSleepActivities.clear();
2476 }
2477
2478 void activitySleptLocked(ActivityRecord r) {
2479 mGoingToSleepActivities.remove(r);
2480 checkReadyForSleepLocked();
2481 }
2482
2483 void checkReadyForSleepLocked() {
2484 if (!mService.isSleepingOrShuttingDown()) {
2485 // Do not care.
2486 return;
2487 }
2488
2489 if (!mSleepTimeout) {
2490 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2494 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2495 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002496 }
2497
2498 if (mStoppingActivities.size() > 0) {
2499 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002500 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002501 + mStoppingActivities.size() + " activities");
2502 scheduleIdleLocked();
2503 dontSleep = true;
2504 }
2505
2506 if (mGoingToSleepActivities.size() > 0) {
2507 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002508 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002509 + mGoingToSleepActivities.size() + " activities");
2510 dontSleep = true;
2511 }
2512
2513 if (dontSleep) {
2514 return;
2515 }
2516 }
2517
Craig Mautnere0a38842013-12-16 16:14:02 -08002518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2519 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002520 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2521 stacks.get(stackNdx).goToSleep();
2522 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002523 }
2524
2525 removeSleepTimeouts();
2526
2527 if (mGoingToSleep.isHeld()) {
2528 mGoingToSleep.release();
2529 }
2530 if (mService.mShuttingDown) {
2531 mService.notifyAll();
2532 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 }
2534
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002535 boolean reportResumedActivityLocked(ActivityRecord r) {
2536 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002537 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002538 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002539 }
2540 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002541 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002542 mWindowManager.executeAppTransition();
2543 return true;
2544 }
2545 return false;
2546 }
2547
Craig Mautner8d341ef2013-03-26 09:03:27 -07002548 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002549 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2550 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002551 int stackNdx = stacks.size() - 1;
2552 while (stackNdx >= 0) {
2553 stacks.get(stackNdx).handleAppCrashLocked(app);
2554 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002556 }
2557 }
2558
Jose Lima4b6c6692014-08-12 17:41:12 -07002559 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002560 final ActivityStack stack = r.task.stack;
2561 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002562 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2563 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002564 return false;
2565 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002566 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002567 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2568 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002569
2570 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002571 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002572 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002573 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002574 return true;
2575 }
2576
2577 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002578 if (visible && top.fullscreen) {
2579 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002580 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2581 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2582 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2583 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002584 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002585 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2586 // Only the activity set as currently visible behind should actively reset its
2587 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002588 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2589 "requestVisibleBehind: returning visible=" + visible
2590 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2591 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002592 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002593 }
2594
Jose Lima4b6c6692014-08-12 17:41:12 -07002595 stack.setVisibleBehindActivity(visible ? r : null);
2596 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002597 // Make the activity immediately above r opaque.
2598 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2599 if (next != null) {
2600 mService.convertFromTranslucent(next.appToken);
2601 }
2602 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002603 if (top.app != null && top.app.thread != null) {
2604 // Notify the top app of the change.
2605 try {
2606 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2607 } catch (RemoteException e) {
2608 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002609 }
2610 return true;
2611 }
2612
Craig Mautnerbb742462014-07-07 15:28:55 -07002613 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2614 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2615 r.mLaunchTaskBehind = false;
2616 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002617 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002618 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002619 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002620 mWindowManager.setAppVisibility(r.appToken, false);
2621 }
2622
2623 void scheduleLaunchTaskBehindComplete(IBinder token) {
2624 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2625 }
2626
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002627 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2628 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002629 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002630 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2631 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002632 final int topStackNdx = stacks.size() - 1;
2633 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2634 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002635 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002636 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002637 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002638 }
2639
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002640 void invalidateTaskLayers() {
2641 mTaskLayersChanged = true;
2642 }
2643
2644 void rankTaskLayersIfNeeded() {
2645 if (!mTaskLayersChanged) {
2646 return;
2647 }
2648 mTaskLayersChanged = false;
2649 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2650 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2651 int baseLayer = 0;
2652 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2653 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2654 }
2655 }
2656 }
2657
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002658 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2659 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2660 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2661 final int topStackNdx = stacks.size() - 1;
2662 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2663 final ActivityStack stack = stacks.get(stackNdx);
2664 stack.clearOtherAppTimeTrackers(except);
2665 }
2666 }
2667 }
2668
Craig Mautner8d341ef2013-03-26 09:03:27 -07002669 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2671 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002672 final int numStacks = stacks.size();
2673 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2674 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002675 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 }
2678 }
2679
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002680 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2681 // Examine all activities currently running in the process.
2682 TaskRecord firstTask = null;
2683 // Tasks is non-null only if two or more tasks are found.
2684 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002685 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2686 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002687 ActivityRecord r = app.activities.get(i);
2688 // First, if we find an activity that is in the process of being destroyed,
2689 // then we just aren't going to do anything for now; we want things to settle
2690 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002691 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002692 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002693 return;
2694 }
2695 // Don't consider any activies that are currently not in a state where they
2696 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002697 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2698 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002699 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002700 continue;
2701 }
2702 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002703 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002704 + " from " + r);
2705 if (firstTask == null) {
2706 firstTask = r.task;
2707 } else if (firstTask != r.task) {
2708 if (tasks == null) {
2709 tasks = new ArraySet<>();
2710 tasks.add(firstTask);
2711 }
2712 tasks.add(r.task);
2713 }
2714 }
2715 }
2716 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002717 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002718 return;
2719 }
2720 // If we have activities in multiple tasks that are in a position to be destroyed,
2721 // let's iterate through the tasks and release the oldest one.
2722 final int numDisplays = mActivityDisplays.size();
2723 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2725 // Step through all stacks starting from behind, to hit the oldest things first.
2726 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2727 final ActivityStack stack = stacks.get(stackNdx);
2728 // Try to release activities in this stack; if we manage to, we are done.
2729 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2730 return;
2731 }
2732 }
2733 }
2734 }
2735
Amith Yamasani37a40c22015-06-17 13:25:42 -07002736 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002737 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002738 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002739 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002740
Craig Mautner858d8a62013-04-23 17:08:34 -07002741 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2743 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002745 final ActivityStack stack = stacks.get(stackNdx);
2746 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002747 TaskRecord task = stack.topTask();
2748 if (task != null) {
2749 mWindowManager.moveTaskToTop(task.taskId);
2750 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002752 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002753
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002754 ActivityStack stack = getStack(restoreStackId);
2755 if (stack == null) {
2756 stack = mHomeStack;
2757 }
2758 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002760 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002761 } else {
2762 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002763 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002764 }
Craig Mautner93529a42013-10-04 15:03:13 -07002765 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002766 }
2767
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002768 /**
2769 * Add background users to send boot completed events to.
2770 * @param userId The user being started in the background
2771 * @param uss The state object for the user.
2772 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002773 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002774 mStartingBackgroundUsers.add(uss);
2775 }
2776
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002777 /** Checks whether the userid is a profile of the current user. */
2778 boolean isCurrentProfileLocked(int userId) {
2779 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002780 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002781 }
2782
Chong Zhang45c25ce2015-08-10 22:18:26 -07002783 /** Checks whether the activity should be shown for current user. */
2784 boolean okToShowLocked(ActivityRecord r) {
2785 return r != null && (isCurrentProfileLocked(r.userId)
2786 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2787 }
2788
Craig Mautnerde4ef022013-04-07 19:01:33 -07002789 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002790 ArrayList<ActivityRecord> stops = null;
2791
2792 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002793 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2794 ActivityRecord s = mStoppingActivities.get(activityNdx);
2795 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002796 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002797 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2798 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002799 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002800 if (s.finishing) {
2801 // If this activity is finishing, it is sitting on top of
2802 // everyone else but we now know it is no longer needed...
2803 // so get rid of it. Otherwise, we need to go through the
2804 // normal flow and hide it once we determine that it is
2805 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002806 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002807 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002808 }
2809 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002810 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002811 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002812 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002813 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002814 }
2815 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002816 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002817 }
2818 }
2819
2820 return stops;
2821 }
2822
Craig Mautnercf910b02013-04-23 11:23:27 -07002823 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2825 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2827 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002828 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002829 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002830 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (r == null) Slog.e(TAG,
2832 "validateTop...: null top activity, stack=" + stack);
2833 else {
2834 final ActivityRecord pausing = stack.mPausingActivity;
2835 if (pausing != null && pausing == r) Slog.e(TAG,
2836 "validateTop...: top stack has pausing activity r=" + r
2837 + " state=" + state);
2838 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2839 "validateTop...: activity in front not resumed r=" + r
2840 + " state=" + state);
2841 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002842 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002843 final ActivityRecord resumed = stack.mResumedActivity;
2844 if (resumed != null && resumed == r) Slog.e(TAG,
2845 "validateTop...: back stack has resumed activity r=" + r
2846 + " state=" + state);
2847 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2848 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002849 }
2850 }
2851 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002852 }
2853
Craig Mautnere0570202015-05-13 13:06:11 -07002854 private String lockTaskModeToString() {
2855 switch (mLockTaskModeState) {
2856 case LOCK_TASK_MODE_LOCKED:
2857 return "LOCKED";
2858 case LOCK_TASK_MODE_PINNED:
2859 return "PINNED";
2860 case LOCK_TASK_MODE_NONE:
2861 return "NONE";
2862 default: return "unknown=" + mLockTaskModeState;
2863 }
2864 }
2865
Craig Mautner27084302013-03-25 08:05:25 -07002866 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002867 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002869 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002870 pw.print(prefix);
2871 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002872 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002873 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002874 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2875 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2876 if (packages.size() > 0) {
2877 pw.println(" mLockTaskPackages (userId:packages)=");
2878 for (int i = 0; i < packages.size(); ++i) {
2879 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2880 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2881 }
2882 }
2883 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002884 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002885
Craig Mautner20e72272013-04-01 13:45:53 -07002886 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002887 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002888 }
2889
Dianne Hackborn390517b2013-05-30 15:03:32 -07002890 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2891 boolean needSep, String prefix) {
2892 if (activity != null) {
2893 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2894 if (needSep) {
2895 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002896 }
2897 pw.print(prefix);
2898 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002899 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002900 }
2901 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002902 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002903 }
2904
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2906 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002907 boolean printed = false;
2908 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002909 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2910 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002911 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002912 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002913 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002915 final ActivityStack stack = stacks.get(stackNdx);
2916 StringBuilder stackHeader = new StringBuilder(128);
2917 stackHeader.append(" Stack #");
2918 stackHeader.append(stack.mStackId);
2919 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002920 stackHeader.append("\n");
2921 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2922 stackHeader.append("\n");
2923 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2925 needSep, stackHeader.toString());
2926 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2927 !dumpAll, false, dumpPackage, true,
2928 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002929
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 needSep = printed;
2931 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2932 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002933 if (pr) {
2934 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002935 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002936 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2938 " mResumedActivity: ");
2939 if (pr) {
2940 printed = true;
2941 needSep = false;
2942 }
2943 if (dumpAll) {
2944 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2945 " mLastPausedActivity: ");
2946 if (pr) {
2947 printed = true;
2948 needSep = true;
2949 }
2950 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2951 needSep, " mLastNoHistoryActivity: ");
2952 }
2953 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002954 }
2955 }
2956
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002957 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2958 false, dumpPackage, true, " Activities waiting to finish:", null);
2959 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2960 false, dumpPackage, true, " Activities waiting to stop:", null);
2961 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2962 false, dumpPackage, true, " Activities waiting for another to become visible:",
2963 null);
2964 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2965 false, dumpPackage, true, " Activities waiting to sleep:", null);
2966 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2967 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002968
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002969 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002970 }
2971
Dianne Hackborn390517b2013-05-30 15:03:32 -07002972 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002974 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002976 String innerPrefix = null;
2977 String[] args = null;
2978 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002979 for (int i=list.size()-1; i>=0; i--) {
2980 final ActivityRecord r = list.get(i);
2981 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2982 continue;
2983 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002984 if (innerPrefix == null) {
2985 innerPrefix = prefix + " ";
2986 args = new String[0];
2987 }
2988 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002989 final boolean full = !brief && (complete || !r.isInHistory());
2990 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002991 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002992 needNL = false;
2993 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002994 if (header1 != null) {
2995 pw.println(header1);
2996 header1 = null;
2997 }
2998 if (header2 != null) {
2999 pw.println(header2);
3000 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003001 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003002 if (lastTask != r.task) {
3003 lastTask = r.task;
3004 pw.print(prefix);
3005 pw.print(full ? "* " : " ");
3006 pw.println(lastTask);
3007 if (full) {
3008 lastTask.dump(pw, prefix + " ");
3009 } else if (complete) {
3010 // Complete + brief == give a summary. Isn't that obvious?!?
3011 if (lastTask.intent != null) {
3012 pw.print(prefix); pw.print(" ");
3013 pw.println(lastTask.intent.toInsecureStringWithClip());
3014 }
3015 }
3016 }
3017 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3018 pw.print(" #"); pw.print(i); pw.print(": ");
3019 pw.println(r);
3020 if (full) {
3021 r.dump(pw, innerPrefix);
3022 } else if (complete) {
3023 // Complete + brief == give a summary. Isn't that obvious?!?
3024 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3025 if (r.app != null) {
3026 pw.print(innerPrefix); pw.println(r.app);
3027 }
3028 }
3029 if (client && r.app != null && r.app.thread != null) {
3030 // flush anything that is already in the PrintWriter since the thread is going
3031 // to write to the file descriptor directly
3032 pw.flush();
3033 try {
3034 TransferPipe tp = new TransferPipe();
3035 try {
3036 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3037 r.appToken, innerPrefix, args);
3038 // Short timeout, since blocking here can
3039 // deadlock with the application.
3040 tp.go(fd, 2000);
3041 } finally {
3042 tp.kill();
3043 }
3044 } catch (IOException e) {
3045 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3046 } catch (RemoteException e) {
3047 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3048 }
3049 needNL = true;
3050 }
3051 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003052 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003054
Craig Mautnerf3333272013-04-22 10:55:53 -07003055 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003056 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3057 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003058 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3059 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003060 }
3061
3062 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003063 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003064 }
3065
3066 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003067 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3068 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003069 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3070 }
3071
Craig Mautner05d29032013-05-03 13:40:13 -07003072 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003073 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3074 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3075 }
Craig Mautner05d29032013-05-03 13:40:13 -07003076 }
3077
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 void removeSleepTimeouts() {
3079 mSleepTimeout = false;
3080 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3081 }
3082
3083 final void scheduleSleepTimeout() {
3084 removeSleepTimeouts();
3085 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3086 }
3087
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 @Override
3089 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003090 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003091 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3092 }
3093
3094 @Override
3095 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003096 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3098 }
3099
3100 @Override
3101 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003102 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3104 }
3105
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003106 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003107 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003108 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003109 newDisplay = mActivityDisplays.get(displayId) == null;
3110 if (newDisplay) {
3111 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003112 if (activityDisplay.mDisplay == null) {
3113 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3114 return;
3115 }
Craig Mautner4504de52013-12-20 09:06:56 -08003116 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003117 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003118 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 }
Craig Mautner4504de52013-12-20 09:06:56 -08003120 if (newDisplay) {
3121 mWindowManager.onDisplayAdded(displayId);
3122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 }
3124
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003125 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3126 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3127 return;
3128 }
3129 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3130 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3131 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3132 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3133 }
3134
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003135 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003137 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3138 if (activityDisplay != null) {
3139 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003141 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003142 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003143 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 }
3145 }
3146 mWindowManager.onDisplayRemoved(displayId);
3147 }
3148
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003149 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003150 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003151 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3152 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 // TODO: Update the bounds.
3154 }
3155 }
3156 mWindowManager.onDisplayChanged(displayId);
3157 }
3158
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003159 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003160 StackInfo info = new StackInfo();
3161 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3162 info.displayId = Display.DEFAULT_DISPLAY;
3163 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003164 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003165
3166 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3167 final int numTasks = tasks.size();
3168 int[] taskIds = new int[numTasks];
3169 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003170 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003171 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003172 for (int i = 0; i < numTasks; ++i) {
3173 final TaskRecord task = tasks.get(i);
3174 taskIds[i] = task.taskId;
3175 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3176 : task.realActivity != null ? task.realActivity.flattenToString()
3177 : task.getTopActivity() != null ? task.getTopActivity().packageName
3178 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003179 taskBounds[i] = new Rect();
3180 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003181 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003182 }
3183 info.taskIds = taskIds;
3184 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003185 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003186 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 return info;
3188 }
3189
3190 StackInfo getStackInfoLocked(int stackId) {
3191 ActivityStack stack = getStack(stackId);
3192 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003193 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003194 }
3195 return null;
3196 }
3197
3198 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003199 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003200 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3201 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003202 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003203 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 }
3205 }
3206 return list;
3207 }
3208
Craig Mautner15df08a2015-04-01 12:17:18 -07003209 TaskRecord getLockedTaskLocked() {
3210 final int top = mLockTaskModeTasks.size() - 1;
3211 if (top >= 0) {
3212 return mLockTaskModeTasks.get(top);
3213 }
3214 return null;
3215 }
3216
3217 boolean isLockedTask(TaskRecord task) {
3218 return mLockTaskModeTasks.contains(task);
3219 }
3220
3221 boolean isLastLockedTask(TaskRecord task) {
3222 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3223 }
3224
3225 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003226 if (!mLockTaskModeTasks.remove(task)) {
3227 return;
3228 }
3229 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3230 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003231 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003232 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3233 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003234 final Message lockTaskMsg = Message.obtain();
3235 lockTaskMsg.arg1 = task.userId;
3236 lockTaskMsg.what = LOCK_TASK_END_MSG;
3237 mHandler.sendMessage(lockTaskMsg);
3238 }
3239 }
3240
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003241 void showNonResizeableDockToastIfNeeded(
3242 TaskRecord task, int preferredStackId, int actualStackId) {
3243 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3244 return;
3245 }
3246
3247 if (!task.canGoInDockedStack() || task.inCropWindowsResizeMode()) {
3248 // Display warning toast if we tried to put a non-dockable task in the docked stack or
3249 // the task is running in cropped window mode.
3250 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3251 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003252 }
3253
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003254 void showLockTaskToast() {
3255 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003256 }
3257
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003258 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3259 if (mLockTaskModeTasks.contains(task)) {
3260 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3261 }
3262 }
3263
Craig Mautner432f64e2015-05-20 14:59:57 -07003264 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3265 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003266 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003267 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003268 final TaskRecord lockedTask = getLockedTaskLocked();
3269 if (lockedTask != null) {
3270 removeLockedTaskLocked(lockedTask);
3271 if (!mLockTaskModeTasks.isEmpty()) {
3272 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003273 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3274 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003275 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003276 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003277 return;
3278 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003279 }
Craig Mautnere0570202015-05-13 13:06:11 -07003280 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3281 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003282 return;
3283 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003284
3285 // Should have already been checked, but do it again.
3286 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003287 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3288 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003289 return;
3290 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003291 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003292 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003293 return;
3294 }
3295
3296 if (mLockTaskModeTasks.isEmpty()) {
3297 // First locktask.
3298 final Message lockTaskMsg = Message.obtain();
3299 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3300 lockTaskMsg.arg1 = task.userId;
3301 lockTaskMsg.what = LOCK_TASK_START_MSG;
3302 lockTaskMsg.arg2 = lockTaskModeState;
3303 mHandler.sendMessage(lockTaskMsg);
3304 }
3305 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003306 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3307 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003308 mLockTaskModeTasks.remove(task);
3309 mLockTaskModeTasks.add(task);
3310
3311 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003312 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003313 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003314
3315 if (andResume) {
3316 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003317 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003318 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003319 }
3320
3321 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003322 return isLockTaskModeViolation(task, false);
3323 }
3324
3325 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3326 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003327 return false;
3328 }
3329 final int lockTaskAuth = task.mLockTaskAuth;
3330 switch (lockTaskAuth) {
3331 case LOCK_TASK_AUTH_DONT_LOCK:
3332 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003333 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003334 case LOCK_TASK_AUTH_LAUNCHABLE:
3335 case LOCK_TASK_AUTH_WHITELISTED:
3336 return false;
3337 case LOCK_TASK_AUTH_PINNABLE:
3338 // Pinnable tasks can't be launched on top of locktask tasks.
3339 return !mLockTaskModeTasks.isEmpty();
3340 default:
3341 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3342 return true;
3343 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003344 }
3345
Craig Mautner15df08a2015-04-01 12:17:18 -07003346 void onLockTaskPackagesUpdatedLocked() {
3347 boolean didSomething = false;
3348 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3349 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003350 final boolean wasWhitelisted =
3351 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3352 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003353 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003354 final boolean isWhitelisted =
3355 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3356 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3357 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003358 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003359 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3360 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003361 removeLockedTaskLocked(lockedTask);
3362 lockedTask.performClearTaskLocked();
3363 didSomething = true;
3364 }
3365 }
3366 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3367 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3368 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3369 final ActivityStack stack = stacks.get(stackNdx);
3370 stack.onLockTaskPackagesUpdatedLocked();
3371 }
3372 }
Craig Mautnere0570202015-05-13 13:06:11 -07003373 final ActivityRecord r = topRunningActivityLocked();
3374 final TaskRecord task = r != null ? r.task : null;
3375 if (mLockTaskModeTasks.isEmpty() && task != null
3376 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3377 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003378 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3379 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3380 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3381 false);
3382 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003383 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003384 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003385 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003386 }
3387 }
3388
Benjamin Franz43261142015-02-11 15:59:44 +00003389 int getLockTaskModeState() {
3390 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003391 }
3392
Jorim Jaggife89d122015-12-22 16:28:44 +01003393 void activityRelaunchedLocked(IBinder token) {
3394 mWindowManager.notifyAppRelaunchingFinished(token);
3395 }
3396
3397 void activityRelaunchingLocked(ActivityRecord r) {
3398 mWindowManager.notifyAppRelaunching(r.appToken);
3399 }
3400
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003401 void logStackState() {
3402 mActivityMetricsLogger.logWindowState();
3403 }
3404
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003405 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003406
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003407 public ActivityStackSupervisorHandler(Looper looper) {
3408 super(looper);
3409 }
3410
Craig Mautnerf3333272013-04-22 10:55:53 -07003411 void activityIdleInternal(ActivityRecord r) {
3412 synchronized (mService) {
3413 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3414 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003415 }
3416
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003417 @Override
3418 public void handleMessage(Message msg) {
3419 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003420 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003421 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3422 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003423 if (mService.mDidDexOpt) {
3424 mService.mDidDexOpt = false;
3425 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3426 nmsg.obj = msg.obj;
3427 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3428 return;
3429 }
3430 // We don't at this point know if the activity is fullscreen,
3431 // so we need to be conservative and assume it isn't.
3432 activityIdleInternal((ActivityRecord)msg.obj);
3433 } break;
3434 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003435 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003436 activityIdleInternal((ActivityRecord)msg.obj);
3437 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003438 case RESUME_TOP_ACTIVITY_MSG: {
3439 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003440 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003441 }
3442 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003443 case SLEEP_TIMEOUT_MSG: {
3444 synchronized (mService) {
3445 if (mService.isSleepingOrShuttingDown()) {
3446 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3447 mSleepTimeout = true;
3448 checkReadyForSleepLocked();
3449 }
3450 }
3451 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003452 case LAUNCH_TIMEOUT_MSG: {
3453 if (mService.mDidDexOpt) {
3454 mService.mDidDexOpt = false;
3455 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3456 return;
3457 }
3458 synchronized (mService) {
3459 if (mLaunchingActivity.isHeld()) {
3460 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3461 if (VALIDATE_WAKE_LOCK_CALLER
3462 && Binder.getCallingUid() != Process.myUid()) {
3463 throw new IllegalStateException("Calling must be system uid");
3464 }
3465 mLaunchingActivity.release();
3466 }
3467 }
3468 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003470 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 } break;
3472 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003473 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003474 } break;
3475 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003476 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003477 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003478 case CONTAINER_CALLBACK_VISIBILITY: {
3479 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003480 final IActivityContainerCallback callback = container.mCallback;
3481 if (callback != null) {
3482 try {
3483 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3484 } catch (RemoteException e) {
3485 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003486 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003487 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003488 case LOCK_TASK_START_MSG: {
3489 // When lock task starts, we disable the status bars.
3490 try {
Jason Monk62515be2014-05-21 16:06:19 -04003491 if (mLockTaskNotify == null) {
3492 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003493 }
Jason Monk62515be2014-05-21 16:06:19 -04003494 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003495 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003496 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003497 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003498 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003499 flags = StatusBarManager.DISABLE_MASK
3500 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003501 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003502 flags = StatusBarManager.DISABLE_MASK
3503 & (~StatusBarManager.DISABLE_BACK)
3504 & (~StatusBarManager.DISABLE_HOME)
3505 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003506 }
3507 getStatusBarService().disable(flags, mToken,
3508 mService.mContext.getPackageName());
3509 }
3510 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003511 if (getDevicePolicyManager() != null) {
3512 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003513 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003514 }
justinzhang5286d3f2014-05-12 17:06:01 -04003515 } catch (RemoteException ex) {
3516 throw new RuntimeException(ex);
3517 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003518 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003519 case LOCK_TASK_END_MSG: {
3520 // When lock task ends, we enable the status bars.
3521 try {
Jason Monk62515be2014-05-21 16:06:19 -04003522 if (getStatusBarService() != null) {
3523 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3524 mService.mContext.getPackageName());
3525 }
3526 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003527 if (getDevicePolicyManager() != null) {
3528 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3529 msg.arg1);
3530 }
Jason Monk62515be2014-05-21 16:06:19 -04003531 if (mLockTaskNotify == null) {
3532 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3533 }
3534 mLockTaskNotify.show(false);
3535 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003536 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003537 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003538 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003539 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003540 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003541 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003542 new LockPatternUtils(mService.mContext)
3543 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003544 }
3545 } catch (SettingNotFoundException e) {
3546 // No setting, don't lock.
3547 }
justinzhang5286d3f2014-05-12 17:06:01 -04003548 } catch (RemoteException ex) {
3549 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003550 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003551 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003552 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003553 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003554 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3555 if (mLockTaskNotify == null) {
3556 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3557 }
3558 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3559 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003560 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3561 final ActivityContainer container = (ActivityContainer) msg.obj;
3562 final IActivityContainerCallback callback = container.mCallback;
3563 if (callback != null) {
3564 try {
3565 callback.onAllActivitiesComplete(container.asBinder());
3566 } catch (RemoteException e) {
3567 }
3568 }
3569 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003570 case LAUNCH_TASK_BEHIND_COMPLETE: {
3571 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003572 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003573 if (r != null) {
3574 handleLaunchTaskBehindCompleteLocked(r);
3575 }
3576 }
3577 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003578
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003579 }
3580 }
3581 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003582
Ying Wangb081a592014-04-22 15:20:16 -07003583 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003584 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3585 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003586 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003587 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003588 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003589 ActivityRecord mParentActivity = null;
3590 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003591
Craig Mautnere3a00d72014-04-16 08:31:19 -07003592 boolean mVisible = true;
3593
Craig Mautner4a1cb222013-12-04 16:14:06 -08003594 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003595 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003597 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3598 final static int CONTAINER_STATE_NO_SURFACE = 1;
3599 final static int CONTAINER_STATE_FINISHING = 2;
3600 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3601
3602 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003603 synchronized (mService) {
3604 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003605 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003606 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003607 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003608 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003609 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003610
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003611 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003612 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003613 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003614 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003615 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003616 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003617 }
3618
3619 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003620 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003621 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003622 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3623 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003624 return;
3625 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003626 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003627 }
3628 }
3629
3630 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003631 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003632 synchronized (mService) {
3633 if (mActivityDisplay != null) {
3634 return mActivityDisplay.mDisplayId;
3635 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003636 }
3637 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003638 }
3639
Jeff Brownca9bc702014-02-11 14:32:56 -08003640 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003641 public int getStackId() {
3642 synchronized (mService) {
3643 return mStackId;
3644 }
3645 }
3646
3647 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003648 public boolean injectEvent(InputEvent event) {
3649 final long origId = Binder.clearCallingIdentity();
3650 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003651 synchronized (mService) {
3652 if (mActivityDisplay != null) {
3653 return mInputManagerInternal.injectInputEvent(event,
3654 mActivityDisplay.mDisplayId,
3655 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3656 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003657 }
3658 return false;
3659 } finally {
3660 Binder.restoreCallingIdentity(origId);
3661 }
3662 }
3663
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003664 @Override
3665 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003666 synchronized (mService) {
3667 if (mContainerState == CONTAINER_STATE_FINISHING) {
3668 return;
3669 }
3670 mContainerState = CONTAINER_STATE_FINISHING;
3671
Craig Mautnerd163e752014-06-13 17:18:47 -07003672 long origId = Binder.clearCallingIdentity();
3673 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003674 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003675 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003676 } finally {
3677 Binder.restoreCallingIdentity(origId);
3678 }
3679 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003680 }
3681
Craig Mautner60257702014-09-17 15:02:33 -07003682 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003683 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003684 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003685 if (mActivityDisplay != null) {
3686 mActivityDisplay.detachActivitiesLocked(mStack);
3687 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003688 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003689 }
3690 }
3691
3692 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003693 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003694 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003695 }
3696
3697 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003698 public final int startActivityIntentSender(IIntentSender intentSender)
3699 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003700 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3701
3702 if (!(intentSender instanceof PendingIntentRecord)) {
3703 throw new IllegalArgumentException("Bad PendingIntent object");
3704 }
3705
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003706 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003707 Binder.getCallingUid(), mCurrentUser, false,
3708 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003709
3710 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3711 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3712 pendingIntent.key.requestResolvedType);
3713
3714 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3715 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3716 }
3717
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003718 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003719 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003720 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003721 throw new SecurityException(
3722 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3723 }
3724 }
3725
Craig Mautnerdf88d732014-01-27 09:21:32 -08003726 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 public IBinder asBinder() {
3728 return this;
3729 }
3730
Craig Mautner4504de52013-12-20 09:06:56 -08003731 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003732 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003733 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003734 }
3735
Craig Mautner4a1cb222013-12-04 16:14:06 -08003736 ActivityStackSupervisor getOuter() {
3737 return ActivityStackSupervisor.this;
3738 }
3739
Craig Mautnerd163e752014-06-13 17:18:47 -07003740 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003741 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 }
3743
Craig Mautner6985bad2014-04-21 15:22:06 -07003744 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003745 void setVisible(boolean visible) {
3746 if (mVisible != visible) {
3747 mVisible = visible;
3748 if (mCallback != null) {
3749 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3750 0 /* unused */, this).sendToTarget();
3751 }
3752 }
3753 }
3754
Craig Mautner6985bad2014-04-21 15:22:06 -07003755 void setDrawn() {
3756 }
3757
Craig Mautner1b4bf852014-05-26 15:06:32 -07003758 // You can always start a new task on a regular ActivityStack.
3759 boolean isEligibleForNewTasks() {
3760 return true;
3761 }
3762
Craig Mautnerd163e752014-06-13 17:18:47 -07003763 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003764 detachLocked();
3765 deleteActivityContainer(this);
3766 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003767 }
3768
Craig Mautner34b73df2014-01-12 21:11:08 -08003769 @Override
3770 public String toString() {
3771 return mIdString + (mActivityDisplay == null ? "N" : "A");
3772 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003773 }
3774
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003775 private class VirtualActivityContainer extends ActivityContainer {
3776 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003777 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003778
3779 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3780 super(getNextStackId());
3781 mParentActivity = parent;
3782 mCallback = callback;
3783 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003784 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003785 }
3786
3787 @Override
3788 public void setSurface(Surface surface, int width, int height, int density) {
3789 super.setSurface(surface, width, height, density);
3790
3791 synchronized (mService) {
3792 final long origId = Binder.clearCallingIdentity();
3793 try {
3794 setSurfaceLocked(surface, width, height, density);
3795 } finally {
3796 Binder.restoreCallingIdentity(origId);
3797 }
3798 }
3799 }
3800
3801 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3802 if (mContainerState == CONTAINER_STATE_FINISHING) {
3803 return;
3804 }
3805 VirtualActivityDisplay virtualActivityDisplay =
3806 (VirtualActivityDisplay) mActivityDisplay;
3807 if (virtualActivityDisplay == null) {
3808 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003809 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003810 mActivityDisplay = virtualActivityDisplay;
3811 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003812 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003813 }
3814
3815 if (mSurface != null) {
3816 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003817 }
3818
Craig Mautner6985bad2014-04-21 15:22:06 -07003819 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003820 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003821 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003822 } else {
3823 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003824 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003825 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003826 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003827 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003828 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003829
Craig Mautnerd163e752014-06-13 17:18:47 -07003830 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003831
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003832 if (DEBUG_STACK) Slog.d(TAG_STACK,
3833 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003834 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003835
Craig Mautner6985bad2014-04-21 15:22:06 -07003836 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003837 boolean isAttachedLocked() {
3838 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003839 }
3840
3841 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003842 void setDrawn() {
3843 synchronized (mService) {
3844 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003845 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003846 }
3847 }
3848
Craig Mautner1b4bf852014-05-26 15:06:32 -07003849 // Never start a new task on an ActivityView if it isn't explicitly specified.
3850 @Override
3851 boolean isEligibleForNewTasks() {
3852 return false;
3853 }
3854
Craig Mautnerd163e752014-06-13 17:18:47 -07003855 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003856 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003857 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3858 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3859 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3860 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3861 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003862 }
3863 }
3864
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3866 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003867 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003868 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003869 int mDisplayId;
3870 Display mDisplay;
3871 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003872
Craig Mautner4a1cb222013-12-04 16:14:06 -08003873 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3874 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003875 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003876
Jose Lima4b6c6692014-08-12 17:41:12 -07003877 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003878
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003879 ActivityDisplay() {
3880 }
Craig Mautner4504de52013-12-20 09:06:56 -08003881
Craig Mautner1a70a162014-09-13 12:09:31 -07003882 // After instantiation, check that mDisplay is not null before using this. The alternative
3883 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003884 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003885 final Display display = mDisplayManager.getDisplay(displayId);
3886 if (display == null) {
3887 return;
3888 }
3889 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003890 }
3891
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003892 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003893 mDisplay = display;
3894 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003895 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003896 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003897
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003898 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003899 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003900 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3901 + " onTop=" + onTop);
3902 if (onTop) {
3903 mStacks.add(stack);
3904 } else {
3905 mStacks.add(0, stack);
3906 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003907 }
3908
Craig Mautnere0a38842013-12-16 16:14:02 -08003909 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003910 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003911 + " from displayId=" + mDisplayId);
3912 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 }
3914
Jose Lima4b6c6692014-08-12 17:41:12 -07003915 void setVisibleBehindActivity(ActivityRecord r) {
3916 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003917 }
3918
Jose Lima4b6c6692014-08-12 17:41:12 -07003919 boolean hasVisibleBehindActivity() {
3920 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003921 }
3922
Craig Mautner34b73df2014-01-12 21:11:08 -08003923 @Override
3924 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003925 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3926 }
3927 }
3928
3929 class VirtualActivityDisplay extends ActivityDisplay {
3930 VirtualDisplay mVirtualDisplay;
3931
Craig Mautner6985bad2014-04-21 15:22:06 -07003932 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003933 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003934 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3935 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3936 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3937 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003938
3939 init(mVirtualDisplay.getDisplay());
3940
3941 mWindowManager.handleDisplayAdded(mDisplayId);
3942 }
3943
3944 void setSurface(Surface surface) {
3945 if (mVirtualDisplay != null) {
3946 mVirtualDisplay.setSurface(surface);
3947 }
3948 }
3949
3950 @Override
3951 void detachActivitiesLocked(ActivityStack stack) {
3952 super.detachActivitiesLocked(stack);
3953 if (mVirtualDisplay != null) {
3954 mVirtualDisplay.release();
3955 mVirtualDisplay = null;
3956 }
3957 }
3958
3959 @Override
3960 public String toString() {
3961 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003962 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003963 }
Jose Lima58e66d62014-05-27 20:00:27 -07003964
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003965 /**
3966 * Adjust bounds to stay within stack bounds.
3967 *
3968 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3969 * that keep them unchanged, but be contained within the stack bounds.
3970 *
3971 * @param bounds Bounds to be adjusted.
3972 * @param stackBounds Bounds within which the other bounds should remain.
3973 */
3974 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3975 if (stackBounds == null || stackBounds.contains(bounds)) {
3976 return;
3977 }
3978
3979 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3980 final int maxRight = stackBounds.right
3981 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3982 int horizontalDiff = stackBounds.left - bounds.left;
3983 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3984 || (bounds.left + horizontalDiff >= maxRight)) {
3985 horizontalDiff = maxRight - bounds.left;
3986 }
3987 bounds.left += horizontalDiff;
3988 bounds.right += horizontalDiff;
3989 }
3990
3991 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3992 final int maxBottom = stackBounds.bottom
3993 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3994 int verticalDiff = stackBounds.top - bounds.top;
3995 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3996 || (bounds.top + verticalDiff >= maxBottom)) {
3997 verticalDiff = maxBottom - bounds.top;
3998 }
3999 bounds.top += verticalDiff;
4000 bounds.bottom += verticalDiff;
4001 }
4002 }
4003
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004004 ActivityStack findStackBehind(ActivityStack stack) {
4005 // TODO(multi-display): We are only looking for stacks on the default display.
4006 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4007 if (display == null) {
4008 return null;
4009 }
4010 final ArrayList<ActivityStack> stacks = display.mStacks;
4011 for (int i = stacks.size() - 1; i >= 0; i--) {
4012 if (stacks.get(i) == stack && i > 0) {
4013 return stacks.get(i - 1);
4014 }
4015 }
4016 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4017 + " in=" + stacks);
4018 }
Craig Mautner27084302013-03-25 08:05:25 -07004019}