blob: 0b22bd4edadcf08899349cd913fa5073840d9b26 [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;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700101import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700102
Jorim Jaggife89d122015-12-22 16:28:44 +0100103import static android.Manifest.permission.START_ANY_ACTIVITY;
104import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
105import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
106import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
107import static android.app.ActivityManager.RESIZE_MODE_FORCED;
108import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
109import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
110import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
111import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
112import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
113import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
114import static android.app.ActivityManager.StackId.HOME_STACK_ID;
115import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
116import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
117import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
118import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
119import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
120import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
121import static android.content.pm.PackageManager.PERMISSION_GRANTED;
122import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
123import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
124import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
125import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
136import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
137import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
138import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
147import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
148import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
149import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
150import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
151import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
152import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
153import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
154import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
155import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
156import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
157import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
158import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
159import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
160import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
161import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
162import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
163import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
164import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
166
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800168 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700169 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700170 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700171 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700172 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700173 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800178 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800180
Craig Mautnerf3333272013-04-22 10:55:53 -0700181 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700183
Craig Mautner0eea92c2013-05-16 13:35:39 -0700184 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700186
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700187 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189
Craig Mautner05d29032013-05-03 13:40:13 -0700190 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
191 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
192 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700194 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
196 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
197 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700198 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400199 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
200 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700201 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700202 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700203 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204
Wale Ogunwale040b4702015-08-06 18:10:50 -0700205 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Jason Monk62515be2014-05-21 16:06:19 -0400207 private static final String LOCK_TASK_TAG = "Lock-to-App";
208
Wale Ogunwale040b4702015-08-06 18:10:50 -0700209 // Used to indicate if an object (e.g. stack) that we are trying to get
210 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800211 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700212
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700213 // Used to indicate that windows of activities should be preserved during the resize.
214 static final boolean PRESERVE_WINDOWS = true;
215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 // Used to indicate if an object (e.g. task) should be moved/created
217 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700218 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219
220 // Used to indicate that an objects (e.g. task) removal from its container
221 // (e.g. stack) is due to it moving to another container.
222 static final boolean MOVING = true;
223
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700224 // Force the focus to change to the stack we are moving a task to..
225 static final boolean FORCE_FOCUS = true;
226
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700227 // Restore task from the saved recents if it can't be found in any live stack.
228 static final boolean RESTORE_FROM_RECENTS = true;
229
Svetoslav7008b512015-06-24 18:47:07 -0700230 // Activity actions an app cannot start if it uses a permission which is not granted.
231 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
232 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700233
Svetoslav7008b512015-06-24 18:47:07 -0700234 static {
235 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
236 Manifest.permission.CAMERA);
237 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
238 Manifest.permission.CAMERA);
239 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
240 Manifest.permission.CALL_PHONE);
241 }
242
Svet Ganov99b60432015-06-27 13:15:22 -0700243 /** Action restriction: launching the activity is not restricted. */
244 private static final int ACTIVITY_RESTRICTION_NONE = 0;
245 /** Action restriction: launching the activity is restricted by a permission. */
246 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
247 /** Action restriction: launching the activity is restricted by an app op. */
248 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700249
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700250 // The height/width divide used when fitting a task within a bounds with method
251 // {@link #fitWithinBounds}.
252 // We always want the task to to be visible in the bounds without affecting its size when
253 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
254 // the input bounds right or bottom side minus the width or height divided by this value.
255 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
256
justinzhang5286d3f2014-05-12 17:06:01 -0400257 /** Status Bar Service **/
258 private IBinder mToken = new Binder();
259 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400260 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400261
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700262 // For debugging to make sure the caller when acquiring/releasing our
263 // wake lock is the system process.
264 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700265
Craig Mautner27084302013-03-25 08:05:25 -0700266 final ActivityManagerService mService;
267
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800268 private final RecentTasks mRecentTasks;
269
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270 final ActivityStackSupervisorHandler mHandler;
271
272 /** Short cut */
273 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700275
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700276 /** Counter for next free stack ID to use for dynamic activity stacks. */
277 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700278
279 /** Task identifier that activities are currently being started in. Incremented each time a
280 * new task is created. */
281 private int mCurTaskId = 0;
282
Craig Mautner2420ead2013-04-01 17:13:20 -0700283 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800284 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700285
Craig Mautnere0a38842013-12-16 16:14:02 -0800286 /** The stack containing the launcher app. Assumed to always be attached to
287 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800288 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700289
Craig Mautnere0a38842013-12-16 16:14:02 -0800290 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800291 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Craig Mautner4a1cb222013-12-04 16:14:06 -0800293 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
294 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800295 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700297
298 /** List of activities that are waiting for a new activity to become visible before completing
299 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800300 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700302 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800303 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700304
305 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800306 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700307
Craig Mautnerde4ef022013-04-07 19:01:33 -0700308 /** List of activities that are ready to be stopped, but waiting for the next activity to
309 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
Craig Mautnerf3333272013-04-22 10:55:53 -0700312 /** List of activities that are ready to be finished, but waiting for the previous activity to
313 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700315
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700318
Craig Mautnerf3333272013-04-22 10:55:53 -0700319 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700320 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700321
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700322 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700323 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700324
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
326 * is being brought in front of us. */
327 boolean mUserLeaving = false;
328
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 /** Set when we have taken too long waiting to go to sleep. */
330 boolean mSleepTimeout = false;
331
332 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700333 * We don't want to allow the device to go to sleep while in the process
334 * of launching an activity. This is primarily to allow alarm intent
335 * receivers to launch an activity and get that to run before the device
336 * goes back to sleep.
337 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700338 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700339
340 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341 * Set when the system is going to sleep, until we have
342 * successfully paused the current activity and released our wake lock.
343 * At that point the system is allowed to actually sleep.
344 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700345 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700347 /** Stack id of the front stack when user switched, indexed by userId. */
348 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700349
Craig Mautner4504de52013-12-20 09:06:56 -0800350 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800351 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700352 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353
354 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700355 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356
Jeff Brownca9bc702014-02-11 14:32:56 -0800357 InputManagerInternal mInputManagerInternal;
358
Craig Mautner15df08a2015-04-01 12:17:18 -0700359 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
360 * may be finished until there is only one entry left. If this is empty the system is not
361 * in lockTask mode. */
362 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000363 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700364 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
365 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000366 */
367 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400368 /**
369 * Notifies the user when entering/exiting lock-task.
370 */
371 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800372
Wale Ogunwaled046a012015-12-24 13:05:59 -0800373 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800374 boolean inResumeTopActivity;
375
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700376 // 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 -0700377 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700378 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700379
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700380 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
381 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800382 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700383
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700384 // The default minimal size that will be used if the activity doesn't specify its minimal size.
385 // It will be calculated when the default display gets added.
386 private int mDefaultMinimalSizeOfResizeableTask = -1;
387
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700388 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
389 private boolean mTaskLayersChanged = true;
390
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800391 private final ActivityMetricsLogger mActivityMetricsLogger;
392
Jorim Jaggidc249c42015-12-15 14:57:31 -0800393 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
394
Wale Ogunwale39381972015-12-17 17:15:29 -0800395 static class FindTaskResult {
396 ActivityRecord r;
397 boolean matchedByRootAffinity;
398 }
399 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
400
Craig Mautneree36c772014-07-16 14:56:05 -0700401 /**
402 * Description of a request to start a new activity, which has been held
403 * due to app switches being disabled.
404 */
405 static class PendingActivityLaunch {
406 final ActivityRecord r;
407 final ActivityRecord sourceRecord;
408 final int startFlags;
409 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700410 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700411
412 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700413 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700414 r = _r;
415 sourceRecord = _sourceRecord;
416 startFlags = _startFlags;
417 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700418 callerApp = _callerApp;
419 }
420
421 void sendErrorResult(String message) {
422 try {
423 if (callerApp.thread != null) {
424 callerApp.thread.scheduleCrash(message);
425 }
426 } catch (RemoteException e) {
427 Slog.e(TAG, "Exception scheduling crash of failed "
428 + "activity launcher sourceRecord=" + sourceRecord, e);
429 }
Craig Mautneree36c772014-07-16 14:56:05 -0700430 }
431 }
432
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800433 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700434 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800435 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700436 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800437 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800438 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700439 }
440
441 /**
442 * At the time when the constructor runs, the power manager has not yet been
443 * initialized. So we initialize our wakelocks afterwards.
444 */
445 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800446 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700447 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700448 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700449 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700450 }
451
justinzhang5286d3f2014-05-12 17:06:01 -0400452 // This function returns a IStatusBarService. The value is from ServiceManager.
453 // getService and is cached.
454 private IStatusBarService getStatusBarService() {
455 synchronized (mService) {
456 if (mStatusBarService == null) {
457 mStatusBarService = IStatusBarService.Stub.asInterface(
458 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
459 if (mStatusBarService == null) {
460 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
461 }
462 }
463 return mStatusBarService;
464 }
465 }
466
Jason Monk35c62a42014-06-17 10:24:47 -0400467 private IDevicePolicyManager getDevicePolicyManager() {
468 synchronized (mService) {
469 if (mDevicePolicyManager == null) {
470 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
471 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
472 if (mDevicePolicyManager == null) {
473 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
474 }
475 }
476 return mDevicePolicyManager;
477 }
478 }
479
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700480 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 synchronized (mService) {
482 mWindowManager = wm;
483
484 mDisplayManager =
485 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
486 mDisplayManager.registerDisplayListener(this, null);
487
488 Display[] displays = mDisplayManager.getDisplays();
489 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
490 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700492 if (activityDisplay.mDisplay == null) {
493 throw new IllegalStateException("Default Display does not exist");
494 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800495 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700496 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800497 }
498
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800499 mHomeStack = mFocusedStack = mLastFocusedStack =
500 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800501
502 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800503 }
Craig Mautner27084302013-03-25 08:05:25 -0700504 }
505
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200506 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700507 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200508 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700509 }
510
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700511 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700513 }
514
Craig Mautnerde4ef022013-04-07 19:01:33 -0700515 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700517 }
518
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700519 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700520 if (stack == null) {
521 return false;
522 }
523
Craig Mautnerdf88d732014-01-27 09:21:32 -0800524 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
525 if (parent != null) {
526 stack = parent.task.stack;
527 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800528 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700529 }
530
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700531 /** The top most stack. */
532 boolean isFrontStack(ActivityStack stack) {
533 if (stack == null) {
534 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700536
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700537 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
538 if (parent != null) {
539 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800540 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700541 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
542 }
543
Wale Ogunwaled046a012015-12-24 13:05:59 -0800544 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800545 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
546 if (!focusCandidate.isFocusable()) {
547 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
548 focusCandidate = focusCandidate.getNextFocusableStackLocked();
549 }
550
551 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800552 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800553 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800554
Wale Ogunwaled046a012015-12-24 13:05:59 -0800555 EventLogTags.writeAmFocusedStack(
556 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
557 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
558 }
559
560 final ActivityRecord r = topRunningActivityLocked();
561 if (mService.mFocusedActivity != r) {
562 // The focus activity should always be the top activity in the focused stack.
563 // There will be chaos and anarchy if it isn't...
564 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
565 }
Craig Mautnerde313752015-01-22 14:28:03 -0800566
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800567 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800568 if (r != null && r.idle) {
569 checkFinishBootingLocked();
570 }
571 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700572 }
573
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700574 void moveHomeStackToFront(String reason) {
575 mHomeStack.moveToFront(reason);
576 }
577
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700578 /** Returns true if the focus activity was adjusted to the home stack top activity. */
579 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700580 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
581 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700582 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700583 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700584
Craig Mautner84984fa2014-06-19 11:19:20 -0700585 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700586
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700587 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700588 if (top == null) {
589 return false;
590 }
591 mService.setFocusedActivityLocked(top, reason);
592 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700593 }
594
Craig Mautner299f9602015-01-26 09:47:33 -0800595 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700596 if (!mService.mBooting && !mService.mBooted) {
597 // Not ready yet!
598 return false;
599 }
600
Craig Mautner84984fa2014-06-19 11:19:20 -0700601 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
602 mWindowManager.showRecentApps();
603 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700604 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700605
Craig Mautner84984fa2014-06-19 11:19:20 -0700606 if (prev != null) {
607 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
608 }
609
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700610 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
611 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800612 final String myReason = reason + " resumeHomeStackTask";
613
Mark Lua56ea122015-10-08 13:31:01 +0800614 // Only resume home activity if isn't finishing.
615 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800616 mService.setFocusedActivityLocked(r, myReason);
617 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700618 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800619 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700620 }
621
Craig Mautner8d341ef2013-03-26 09:03:27 -0700622 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700623 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700624 }
625
626 /**
627 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
628 * @param id Id of the task we would like returned.
629 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
630 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700631 * @param stackId The stack to restore the task to (default launch stack will be used if
632 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700633 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700634 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800635 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800637 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
639 ActivityStack stack = stacks.get(stackNdx);
640 TaskRecord task = stack.taskForIdLocked(id);
641 if (task != null) {
642 return task;
643 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700644 }
645 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800646
647 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700648 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800649 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800650 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800652 return null;
653 }
654
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700655 if (!restoreFromRecents) {
656 return task;
657 }
658
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700659 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700660 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
661 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800662 return null;
663 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700664 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800665 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700666 }
667
Craig Mautner6170f732013-04-02 13:05:23 -0700668 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800669 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800671 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
673 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
674 if (r != null) {
675 return r;
676 }
Craig Mautner6170f732013-04-02 13:05:23 -0700677 }
678 }
679 return null;
680 }
681
Craig Mautneref73ee12014-04-23 11:45:37 -0700682 void setNextTaskId(int taskId) {
683 if (taskId > mCurTaskId) {
684 mCurTaskId = taskId;
685 }
686 }
687
Craig Mautner8d341ef2013-03-26 09:03:27 -0700688 int getNextTaskId() {
689 do {
690 mCurTaskId++;
691 if (mCurTaskId <= 0) {
692 mCurTaskId = 1;
693 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700694 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700695 return mCurTaskId;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800699 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700700 if (stack == null) {
701 return null;
702 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700703 ActivityRecord resumedActivity = stack.mResumedActivity;
704 if (resumedActivity == null || resumedActivity.app == null) {
705 resumedActivity = stack.mPausingActivity;
706 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700707 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700708 }
709 }
710 return resumedActivity;
711 }
712
Dianne Hackbornff072722014-09-24 10:56:28 -0700713 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700714 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
717 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
719 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700720 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 continue;
722 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700723 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 if (hr != null) {
725 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
726 && processName.equals(hr.processName)) {
727 try {
George Mount2c92c972014-03-20 09:38:23 -0700728 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 didSomething = true;
730 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700731 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 Slog.w(TAG, "Exception in new application when starting activity "
733 + hr.intent.getComponent().flattenToShortString(), e);
734 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700735 }
Craig Mautner20e72272013-04-01 13:45:53 -0700736 }
Craig Mautner20e72272013-04-01 13:45:53 -0700737 }
738 }
739 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700740 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700741 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700742 }
Craig Mautner20e72272013-04-01 13:45:53 -0700743 return didSomething;
744 }
745
746 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
750 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700751 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 continue;
753 }
754 final ActivityRecord resumedActivity = stack.mResumedActivity;
755 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700756 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800757 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 return false;
759 }
Craig Mautner20e72272013-04-01 13:45:53 -0700760 }
761 }
762 return true;
763 }
764
Craig Mautnerde4ef022013-04-07 19:01:33 -0700765 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
767 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
769 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700770 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700772 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 return false;
774 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700775 }
776 }
777 }
778 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700779 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800780 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
781 mLastFocusedStack + " to=" + mFocusedStack);
782 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700783 return true;
784 }
785
786 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800787 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800788 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
789 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
791 final ActivityStack stack = stacks.get(stackNdx);
792 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800793 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700794 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800795 return false;
796 }
797 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800798 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 }
800 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800801 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700802 }
803
Craig Mautner2acc3892013-09-23 10:28:14 -0700804 /**
805 * Pause all activities in either all of the stacks or just the back stacks.
806 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700807 * @return true if any activity was paused as a result of this call.
808 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700809 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700810 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800811 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
812 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800813 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
814 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700815 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700816 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800817 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700818 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
819 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800820 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700821 }
822 }
823 return someActivityPaused;
824 }
825
Craig Mautnerde4ef022013-04-07 19:01:33 -0700826 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700827 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
832 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700833 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700835 Slog.d(TAG_STATES,
836 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 pausing = false;
838 } else {
839 return false;
840 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700841 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700842 }
843 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700844 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700845 }
846
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700847 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
848 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500849 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800850 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
851 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
852 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
853 final ActivityStack stack = stacks.get(stackNdx);
854 if (stack.mResumedActivity != null &&
855 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700856 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800857 }
858 }
859 }
860 }
861
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700862 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700863 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700864 }
865
866 void sendWaitingVisibleReportLocked(ActivityRecord r) {
867 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700868 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700869 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700870 if (w.who == null) {
871 changed = true;
872 w.timeout = false;
873 if (r != null) {
874 w.who = new ComponentName(r.info.packageName, r.info.name);
875 }
876 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
877 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700880 if (changed) {
881 mService.notifyAll();
882 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700883 }
884
885 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
886 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700887 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700888 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700889 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700890 if (w.who == null) {
891 changed = true;
892 w.timeout = timeout;
893 if (r != null) {
894 w.who = new ComponentName(r.info.packageName, r.info.name);
895 }
896 w.thisTime = thisTime;
897 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700898 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700899 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700900 if (changed) {
901 mService.notifyAll();
902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700903 }
904
Craig Mautner29219d92013-04-16 20:19:12 -0700905 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800906 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700907 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700908 if (r != null) {
909 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700910 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700911
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800913 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
915 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800916 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700917 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700918 if (r != null) {
919 return r;
920 }
921 }
922 }
923 return null;
924 }
925
Dianne Hackborn09233282014-04-30 11:33:59 -0700926 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700927 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800928 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
929 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800930 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800932 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800933 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
934 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700935 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700937 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700938 }
939 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700940
941 // The lists are already sorted from most recent to oldest. Just pull the most recent off
942 // each list and add it to list. Stop when all lists are empty or maxNum reached.
943 while (maxNum > 0) {
944 long mostRecentActiveTime = Long.MIN_VALUE;
945 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 final int numTaskLists = runningTaskLists.size();
947 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
948 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700949 if (!stackTaskList.isEmpty()) {
950 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
951 if (lastActiveTime > mostRecentActiveTime) {
952 mostRecentActiveTime = lastActiveTime;
953 selectedStackList = stackTaskList;
954 }
955 }
956 }
957 if (selectedStackList != null) {
958 list.add(selectedStackList.remove(0));
959 --maxNum;
960 } else {
961 break;
962 }
963 }
Craig Mautner20e72272013-04-01 13:45:53 -0700964 }
965
Todd Kennedy7440f172015-12-09 14:31:22 -0800966 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
967 ProfilerInfo profilerInfo) {
968 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969 if (aInfo != null) {
970 // Store the found target back into the intent, because now that
971 // we have it we never want to do this again. For example, if the
972 // user navigates back to this point in the history, we should
973 // always restart the exact same activity.
974 intent.setComponent(new ComponentName(
975 aInfo.applicationInfo.packageName, aInfo.name));
976
977 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700978 if (!aInfo.processName.equals("system")) {
979 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 mService.setDebugApp(aInfo.processName, true, false);
981 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982
Man Caocfa78b22015-06-11 20:14:34 -0700983 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
984 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
985 }
986
987 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700988 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 }
990 }
991 }
992 return aInfo;
993 }
994
Todd Kennedy7440f172015-12-09 14:31:22 -0800995 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
996 try {
997 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
998 PackageManager.MATCH_DEFAULT_ONLY
999 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1000 } catch (RemoteException e) {
1001 }
1002 return null;
1003 }
1004
1005 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1006 ProfilerInfo profilerInfo, int userId) {
1007 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1008 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1009 }
1010
Craig Mautner2420ead2013-04-01 17:13:20 -07001011 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001012 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001013 throws RemoteException {
1014
Craig Mautner2568c3a2015-03-26 14:22:34 -07001015 if (andResume) {
1016 r.startFreezingScreenLocked(app, 0);
1017 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001018
Craig Mautner2568c3a2015-03-26 14:22:34 -07001019 // schedule launch ticks to collect information about slow apps.
1020 r.startLaunchTickingLocked();
1021 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001022
1023 // Have the window manager re-evaluate the orientation of
1024 // the screen based on the new activity order. Note that
1025 // as a result of this, it can call back into the activity
1026 // manager with a new orientation. We don't care about that,
1027 // because the activity is not currently running so we are
1028 // just restarting it anyway.
1029 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001031 mService.mConfiguration,
1032 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001033 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001034 }
1035
1036 r.app = app;
1037 app.waitingToKill = null;
1038 r.launchCount++;
1039 r.lastLaunchTime = SystemClock.uptimeMillis();
1040
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001041 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001042
1043 int idx = app.activities.indexOf(r);
1044 if (idx < 0) {
1045 app.activities.add(r);
1046 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001047 mService.updateLruProcessLocked(app, true, null);
1048 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001049
Craig Mautner15df08a2015-04-01 12:17:18 -07001050 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001051 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1052 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001053 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001054 }
1055
1056 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001057 try {
1058 if (app.thread == null) {
1059 throw new RemoteException();
1060 }
1061 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001062 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001063 if (andResume) {
1064 results = r.results;
1065 newIntents = r.newIntents;
1066 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1068 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1069 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001070 if (andResume) {
1071 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1072 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001073 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001074 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001075 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001076 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001077 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001078 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001079 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001080 r.sleeping = false;
1081 r.forceNewConfig = false;
1082 mService.showAskCompatModeDialogLocked(r);
1083 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001084 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001085 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1086 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1087 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001088 final String profileFile = mService.mProfileFile;
1089 if (profileFile != null) {
1090 ParcelFileDescriptor profileFd = mService.mProfileFd;
1091 if (profileFd != null) {
1092 try {
1093 profileFd = profileFd.dup();
1094 } catch (IOException e) {
1095 if (profileFd != null) {
1096 try {
1097 profileFd.close();
1098 } catch (IOException o) {
1099 }
1100 profileFd = null;
1101 }
1102 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001104
1105 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1106 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001107 }
1108 }
1109 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001110
Craig Mautner2568c3a2015-03-26 14:22:34 -07001111 if (andResume) {
1112 app.hasShownUi = true;
1113 app.pendingUiClean = true;
1114 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001115 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001117 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001118 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001119 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001120 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001121
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001122 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001123 // This may be a heavy-weight process! Note that the package
1124 // manager will ensure that only activity can run in the main
1125 // process of the .apk, which is the only thing that will be
1126 // considered heavy-weight.
1127 if (app.processName.equals(app.info.packageName)) {
1128 if (mService.mHeavyWeightProcess != null
1129 && mService.mHeavyWeightProcess != app) {
1130 Slog.w(TAG, "Starting new heavy weight process " + app
1131 + " when already running "
1132 + mService.mHeavyWeightProcess);
1133 }
1134 mService.mHeavyWeightProcess = app;
1135 Message msg = mService.mHandler.obtainMessage(
1136 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1137 msg.obj = r;
1138 mService.mHandler.sendMessage(msg);
1139 }
1140 }
1141
1142 } catch (RemoteException e) {
1143 if (r.launchFailed) {
1144 // This is the second time we failed -- finish activity
1145 // and give up.
1146 Slog.e(TAG, "Second failure launching "
1147 + r.intent.getComponent().flattenToShortString()
1148 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001149 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001150 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1151 "2nd-crash", false);
1152 return false;
1153 }
1154
1155 // This is the first time we failed -- restart process and
1156 // retry.
1157 app.activities.remove(r);
1158 throw e;
1159 }
1160
1161 r.launchFailed = false;
1162 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001163 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001164 }
1165
1166 if (andResume) {
1167 // As part of the process of launching, ActivityThread also performs
1168 // a resume.
1169 stack.minimalResumeActivityLocked(r);
1170 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001171 // This activity is not starting in the resumed state... which should look like we asked
1172 // it to pause+stop (but remain visible), and it has done so and reported back the
1173 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001174 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001175 "Moving to PAUSED: " + r + " (starting in paused state)");
1176 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001177 }
1178
1179 // Launch the new version setup screen if needed. We do this -after-
1180 // launching the initial activity (that is, home), so that it can have
1181 // a chance to initialize itself while in the background, making the
1182 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001183 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001184 mService.startSetupActivityLocked();
1185 }
1186
Dianne Hackborn465fa392014-09-14 14:21:18 -07001187 // Update any services we are bound to that might care about whether
1188 // their client may have activities.
1189 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1190
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 return true;
1192 }
1193
Craig Mautnere79d42682013-04-01 19:01:53 -07001194 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001195 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001196 // Is this activity's application already running?
1197 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001198 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001199
1200 r.task.stack.setLaunchTime(r);
1201
1202 if (app != null && app.thread != null) {
1203 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001204 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1205 || !"android".equals(r.info.packageName)) {
1206 // Don't add this if it is a platform component that is marked
1207 // to run in multiple processes, because this is actually
1208 // part of the framework so doesn't make sense to track as a
1209 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001210 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1211 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001212 }
George Mount2c92c972014-03-20 09:38:23 -07001213 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001214 return;
1215 } catch (RemoteException e) {
1216 Slog.w(TAG, "Exception when starting activity "
1217 + r.intent.getComponent().flattenToShortString(), e);
1218 }
1219
1220 // If a dead object exception was thrown -- fall through to
1221 // restart the application.
1222 }
1223
1224 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001225 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001226 }
1227
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001228 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001229 String resultWho, int requestCode, int callingPid, int callingUid,
1230 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1231 ActivityRecord resultRecord, ActivityStack resultStack) {
1232 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1233 callingUid);
1234 if (startAnyPerm == PERMISSION_GRANTED) {
1235 return true;
1236 }
1237 final int componentRestriction = getComponentRestrictionForCallingPackage(
1238 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1239 final int actionRestriction = getActionRestrictionForCallingPackage(
1240 intent.getAction(), callingPackage, callingPid, callingUid);
1241 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1242 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1243 if (resultRecord != null) {
1244 resultStack.sendActivityResultLocked(-1,
1245 resultRecord, resultWho, requestCode,
1246 Activity.RESULT_CANCELED, null);
1247 }
1248 final String msg;
1249 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1250 msg = "Permission Denial: starting " + intent.toString()
1251 + " from " + callerApp + " (pid=" + callingPid
1252 + ", uid=" + callingUid + ")" + " with revoked permission "
1253 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1254 } else if (!aInfo.exported) {
1255 msg = "Permission Denial: starting " + intent.toString()
1256 + " from " + callerApp + " (pid=" + callingPid
1257 + ", uid=" + callingUid + ")"
1258 + " not exported from uid " + aInfo.applicationInfo.uid;
1259 } else {
1260 msg = "Permission Denial: starting " + intent.toString()
1261 + " from " + callerApp + " (pid=" + callingPid
1262 + ", uid=" + callingUid + ")"
1263 + " requires " + aInfo.permission;
1264 }
1265 Slog.w(TAG, msg);
1266 throw new SecurityException(msg);
1267 }
1268
1269 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1270 final String message = "Appop Denial: starting " + intent.toString()
1271 + " from " + callerApp + " (pid=" + callingPid
1272 + ", uid=" + callingUid + ")"
1273 + " requires " + AppOpsManager.permissionToOp(
1274 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1275 Slog.w(TAG, message);
1276 return false;
1277 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1278 final String message = "Appop Denial: starting " + intent.toString()
1279 + " from " + callerApp + " (pid=" + callingPid
1280 + ", uid=" + callingUid + ")"
1281 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1282 Slog.w(TAG, message);
1283 return false;
1284 }
1285 return true;
1286 }
1287
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001288 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001289 final long identity = Binder.clearCallingIdentity();
1290 try {
1291 return UserManager.get(mService.mContext).getUserInfo(userId);
1292 } finally {
1293 Binder.restoreCallingIdentity(identity);
1294 }
1295 }
1296
Svet Ganov99b60432015-06-27 13:15:22 -07001297 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001298 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001299 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1300 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001301 == PackageManager.PERMISSION_DENIED) {
1302 return ACTIVITY_RESTRICTION_PERMISSION;
1303 }
1304
Christopher Tateff7add02015-08-17 10:23:22 -07001305 if (activityInfo.permission == null) {
1306 return ACTIVITY_RESTRICTION_NONE;
1307 }
1308
Svet Ganov99b60432015-06-27 13:15:22 -07001309 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1310 if (opCode == AppOpsManager.OP_NONE) {
1311 return ACTIVITY_RESTRICTION_NONE;
1312 }
1313
1314 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1315 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001316 if (!ignoreTargetSecurity) {
1317 return ACTIVITY_RESTRICTION_APPOP;
1318 }
Svet Ganov99b60432015-06-27 13:15:22 -07001319 }
1320
1321 return ACTIVITY_RESTRICTION_NONE;
1322 }
1323
Svetoslav7008b512015-06-24 18:47:07 -07001324 private int getActionRestrictionForCallingPackage(String action,
1325 String callingPackage, int callingPid, int callingUid) {
1326 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001327 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001328 }
1329
1330 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1331 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001332 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001333 }
1334
1335 final PackageInfo packageInfo;
1336 try {
1337 packageInfo = mService.mContext.getPackageManager()
1338 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1339 } catch (PackageManager.NameNotFoundException e) {
1340 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001341 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001342 }
1343
1344 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001345 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001346 }
1347
1348 if (mService.checkPermission(permission, callingPid, callingUid) ==
1349 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001350 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001351 }
1352
1353 final int opCode = AppOpsManager.permissionToOpCode(permission);
1354 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001355 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001356 }
1357
1358 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1359 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001360 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001361 }
1362
Svet Ganov99b60432015-06-27 13:15:22 -07001363 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001364 }
1365
Wale Ogunwaled046a012015-12-24 13:05:59 -08001366 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001367 if (r == null) {
1368 // Not sure what you are trying to do, but it is not going to work...
1369 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001370 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001371 final TaskRecord task = r.task;
1372 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001373 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001374 return false;
1375 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001376 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001377 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001378 }
1379
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001380 void setLaunchSource(int uid) {
1381 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1382 }
1383
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001384 void acquireLaunchWakelock() {
1385 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1386 throw new IllegalStateException("Calling must be system uid");
1387 }
1388 mLaunchingActivity.acquire();
1389 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1390 // To be safe, don't allow the wake lock to be held for too long.
1391 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1392 }
1393 }
1394
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001395 /**
1396 * Called when the frontmost task is idle.
1397 * @return the state of mService.mBooting before this was called.
1398 */
1399 private boolean checkFinishBootingLocked() {
1400 final boolean booting = mService.mBooting;
1401 boolean enableScreen = false;
1402 mService.mBooting = false;
1403 if (!mService.mBooted) {
1404 mService.mBooted = true;
1405 enableScreen = true;
1406 }
1407 if (booting || enableScreen) {
1408 mService.postFinishBooting(booting, enableScreen);
1409 }
1410 return booting;
1411 }
1412
Craig Mautnerf3333272013-04-22 10:55:53 -07001413 // Checked.
1414 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1415 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001416 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001417
Craig Mautnerf3333272013-04-22 10:55:53 -07001418 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001419 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001420 int NS = 0;
1421 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001422 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001423 boolean activityRemoved = false;
1424
Wale Ogunwale7d701172015-03-11 15:36:30 -07001425 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001426 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001427 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1428 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001429 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1430 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001431 if (fromTimeout) {
1432 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001433 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001434
1435 // This is a hack to semi-deal with a race condition
1436 // in the client where it can be constructed with a
1437 // newer configuration from when we asked it to launch.
1438 // We'll update with whatever configuration it now says
1439 // it used to launch.
1440 if (config != null) {
1441 r.configuration = config;
1442 }
1443
1444 // We are now idle. If someone is waiting for a thumbnail from
1445 // us, we can now deliver.
1446 r.idle = true;
1447
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001448 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001449 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001450 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001451 }
1452 }
1453
1454 if (allResumedActivitiesIdle()) {
1455 if (r != null) {
1456 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001457 }
1458
1459 if (mLaunchingActivity.isHeld()) {
1460 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1461 if (VALIDATE_WAKE_LOCK_CALLER &&
1462 Binder.getCallingUid() != Process.myUid()) {
1463 throw new IllegalStateException("Calling must be system uid");
1464 }
1465 mLaunchingActivity.release();
1466 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001467 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001468 }
1469
1470 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001471 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001472 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001473 if ((NF = mFinishingActivities.size()) > 0) {
1474 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001475 mFinishingActivities.clear();
1476 }
1477
Craig Mautnerf3333272013-04-22 10:55:53 -07001478 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001479 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001480 mStartingUsers.clear();
1481 }
1482
Craig Mautnerf3333272013-04-22 10:55:53 -07001483 // Stop any activities that are scheduled to do so but have been
1484 // waiting for the next one to start.
1485 for (int i = 0; i < NS; i++) {
1486 r = stops.get(i);
1487 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001488 if (stack != null) {
1489 if (r.finishing) {
1490 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1491 } else {
1492 stack.stopActivityLocked(r);
1493 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001494 }
1495 }
1496
1497 // Finish any activities that are scheduled to do so but have been
1498 // waiting for the next one to start.
1499 for (int i = 0; i < NF; i++) {
1500 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001501 final ActivityStack stack = r.task.stack;
1502 if (stack != null) {
1503 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1504 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001505 }
1506
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001507 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001508 // Complete user switch
1509 if (startingUsers != null) {
1510 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001511 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001512 }
1513 }
1514 // Complete starting up of background users
1515 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001516 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001517 mStartingBackgroundUsers.clear();
1518 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001519 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001520 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001521 }
1522 }
1523
1524 mService.trimApplications();
1525 //dump();
1526 //mWindowManager.dump();
1527
Craig Mautnerf3333272013-04-22 10:55:53 -07001528 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001529 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001530 }
1531
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001532 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001533 }
1534
Craig Mautner8e569572013-10-11 17:36:59 -07001535 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001536 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1538 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001539 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1540 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1541 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001542 }
Craig Mautner19091252013-10-05 00:03:53 -07001543 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001544 }
1545
1546 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001547 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1548 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001549 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1550 stacks.get(stackNdx).closeSystemDialogsLocked();
1551 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001552 }
1553 }
1554
Craig Mautner93529a42013-10-04 15:03:13 -07001555 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001556 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001557 }
1558
Craig Mautner8d341ef2013-03-26 09:03:27 -07001559 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001560 * Update the last used stack id for non-current user (current user's last
1561 * used stack is the focused stack)
1562 */
1563 void updateUserStackLocked(int userId, ActivityStack stack) {
1564 if (userId != mCurrentUser) {
1565 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1566 }
1567 }
1568
1569 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001570 * @return true if some activity was finished (or would have finished if doit were true).
1571 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001572 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1573 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001574 boolean didSomething = 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;
louis_chang8f0555a2015-10-27 10:45:53 +08001577 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001578 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001579 if (stack.finishDisabledPackageActivitiesLocked(
1580 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001581 didSomething = true;
1582 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001583 }
1584 }
1585 return didSomething;
1586 }
1587
Dianne Hackborna413dc02013-07-12 12:02:55 -07001588 void updatePreviousProcessLocked(ActivityRecord r) {
1589 // Now that this process has stopped, we may want to consider
1590 // it to be the previous app to try to keep around in case
1591 // the user wants to return to it.
1592
1593 // First, found out what is currently the foreground app, so that
1594 // we don't blow away the previous app if this activity is being
1595 // hosted by the process that is actually still the foreground.
1596 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001597 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1598 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001599 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1600 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001601 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001602 if (stack.mResumedActivity != null) {
1603 fgApp = stack.mResumedActivity.app;
1604 } else if (stack.mPausingActivity != null) {
1605 fgApp = stack.mPausingActivity.app;
1606 }
1607 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001608 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001609 }
1610 }
1611
1612 // Now set this one as the previous process, only if that really
1613 // makes sense to.
1614 if (r.app != null && fgApp != null && r.app != fgApp
1615 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001616 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001617 mService.mPreviousProcess = r.app;
1618 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1619 }
1620 }
1621
Wale Ogunwaled046a012015-12-24 13:05:59 -08001622 boolean resumeFocusedStackTopActivityLocked() {
1623 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001624 }
1625
Wale Ogunwaled046a012015-12-24 13:05:59 -08001626 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001627 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001628 if (targetStack != null && isFocusedStack(targetStack)) {
1629 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001630 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001631 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1632 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001633 }
1634
Todd Kennedy539db512014-12-15 09:57:55 -08001635 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001636 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1637 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001638 final int numStacks = stacks.size();
1639 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1640 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001641 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001642 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001643 }
1644 }
1645
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001646 void finishVoiceTask(IVoiceInteractionSession session) {
1647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1649 final int numStacks = stacks.size();
1650 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1651 final ActivityStack stack = stacks.get(stackNdx);
1652 stack.finishVoiceTask(session);
1653 }
1654 }
1655 }
1656
Chong Zhang280d3322015-11-03 17:27:26 -08001657 void findTaskToMoveToFrontLocked(
1658 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001659 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1660 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001661 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001662 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1663 // Caller wants the home activity moved with it. To accomplish this,
1664 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001665 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001666 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001667 if (task.stack == null) {
1668 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1669 + task + " to front. Stack is null");
1670 return;
1671 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001672
Chong Zhang0fa656b2015-08-31 15:17:21 -07001673 if (task.mResizeable && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001674 int stackId = options.getLaunchStackId();
1675 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001676 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001677 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001678 if (stackId == INVALID_STACK_ID) {
1679 stackId = task.getLaunchStackId();
1680 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001681 if (stackId != task.stack.mStackId) {
1682 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1683 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1684 // still need moveTaskToFrontLocked() below for any transition settings.
1685 }
1686 // WM resizeTask must be done after the task is moved to the correct stack,
1687 // because Task's setBounds() also updates dim layer's bounds, but that has
1688 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07001689 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1690 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001691 }
1692 }
1693
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001694 final ActivityRecord r = task.getTopActivity();
1695 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1696 r == null ? null : r.appTimeTracker, reason);
1697
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001698 if (DEBUG_STACK) Slog.d(TAG_STACK,
1699 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001700 }
1701
Wale Ogunwale854809c2015-12-27 16:18:19 -08001702 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001703 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001704 // window management or is launching into the pinned stack.
1705 if (!options.hasLaunchBounds()) {
1706 return false;
1707 }
1708 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1709 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001710 }
1711
Craig Mautner967212c2013-04-13 21:10:58 -07001712 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001713 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001714 }
1715
1716 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001717 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1718 if (activityContainer != null) {
1719 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001720 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001721 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001722 return null;
1723 }
1724 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001725 }
1726
Craig Mautner967212c2013-04-13 21:10:58 -07001727 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001728 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1730 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001731 }
1732 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001733 }
1734
Craig Mautner4a1cb222013-12-04 16:14:06 -08001735 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001736 ActivityRecord homeActivity = getHomeActivity();
1737 if (homeActivity != null) {
1738 return homeActivity.appToken;
1739 }
1740 return null;
1741 }
1742
1743 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001744 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001745 }
1746
1747 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001748 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1749 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1750 final TaskRecord task = tasks.get(taskNdx);
1751 if (task.isHomeTask()) {
1752 final ArrayList<ActivityRecord> activities = task.mActivities;
1753 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1754 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001755 if (r.isHomeActivity()
1756 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001757 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001758 }
1759 }
1760 }
1761 }
1762 return null;
1763 }
1764
Chong Zhangb15758a2015-11-17 12:12:03 -08001765 /**
1766 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1767 * the docked stack itself, or if it's side-by-side to the docked stack.
1768 */
1769 boolean isStackDockedInEffect(int stackId) {
1770 return stackId == DOCKED_STACK_ID ||
1771 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1772 }
1773
Todd Kennedyca4d8422015-01-15 15:19:22 -08001774 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001775 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001776 ActivityContainer activityContainer =
1777 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001778 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001779 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1780 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001781 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001782 return activityContainer;
1783 }
1784
Craig Mautner34b73df2014-01-12 21:11:08 -08001785 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001786 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1787 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1788 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001789 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1790 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001791 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001792 }
1793 }
1794
Craig Mautner95da1082014-02-24 17:54:35 -08001795 void deleteActivityContainer(IActivityContainer container) {
1796 ActivityContainer activityContainer = (ActivityContainer)container;
1797 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001798 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1799 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001800 final int stackId = activityContainer.mStackId;
1801 mActivityContainers.remove(stackId);
1802 mWindowManager.removeStack(stackId);
1803 }
1804 }
1805
Jorim Jaggidc249c42015-12-15 14:57:31 -08001806 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1807 boolean preserveWindows, boolean allowResizeInDockedMode) {
1808 if (stackId == DOCKED_STACK_ID) {
1809 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1810 preserveWindows);
1811 return;
1812 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001813 final ActivityStack stack = getStack(stackId);
1814 if (stack == null) {
1815 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1816 return;
1817 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001818
Jorim Jaggidc249c42015-12-15 14:57:31 -08001819 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001820 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1821 // stack size changing so things don't get out of sync.
1822 return;
1823 }
1824
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001825 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001826 mWindowManager.deferSurfaceLayout();
1827 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001828 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1829 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001830 } finally {
1831 mWindowManager.continueSurfaceLayout();
1832 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001833 }
1834 }
1835
Jorim Jaggidc249c42015-12-15 14:57:31 -08001836 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1837 Rect tempTaskInsetBounds) {
1838 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1839 // The bounds passed in corresponds to the fullscreen bounds which we normally
1840 // represent with null. Go ahead and set it to null so that all tasks configuration
1841 // can have the right fullscreen state.
1842 bounds = null;
1843 }
1844
1845 mTmpBounds.clear();
1846 mTmpConfigs.clear();
1847 mTmpInsetBounds.clear();
1848 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1849 for (int i = tasks.size() - 1; i >= 0; i--) {
1850 TaskRecord task = tasks.get(i);
1851 if (task.mResizeable) {
1852 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1853 // For freeform stack we don't adjust the size of the tasks to match that
1854 // of the stack, but we do try to make sure the tasks are still contained
1855 // with the bounds of the stack.
1856 tempRect2.set(task.mBounds);
1857 fitWithinBounds(tempRect2, bounds);
1858 task.updateOverrideConfiguration(tempRect2);
1859 } else {
1860 task.updateOverrideConfiguration(tempTaskBounds != null
1861 ? tempTaskBounds : bounds);
1862 }
1863 }
1864
1865 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1866 mTmpBounds.put(task.taskId, task.mBounds);
1867 if (tempTaskInsetBounds != null) {
1868 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1869 }
1870 }
1871 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1872 mTmpBounds, mTmpInsetBounds);
1873 stack.setBounds(bounds);
1874 }
1875
1876 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1877 boolean preserveWindows) {
1878 if (r == null) {
1879 return;
1880 }
1881 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1882 preserveWindows);
1883 // And we need to make sure at this point that all other activities
1884 // are made visible with the correct configuration.
1885 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1886 if (!updated) {
1887 resumeFocusedStackTopActivityLocked();
1888 }
1889 }
1890
1891 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1892 Rect tempDockedTaskInsetBounds,
1893 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1894 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1895 if (stack == null) {
1896 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1897 return;
1898 }
1899
1900 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1901 mWindowManager.deferSurfaceLayout();
1902 try {
1903 ActivityRecord r = stack.topRunningActivityLocked();
1904 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1905 tempDockedTaskInsetBounds);
1906
1907 if (stack.mFullscreen) {
1908 // The dock stack went fullscreen which is kinda like dismissing it.
1909 // In this case we make all other static stacks fullscreen and move all
1910 // docked stack tasks to the fullscreen stack.
1911 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1912 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1913 resizeStackLocked(i, null, null, null, preserveWindows,
1914 true /* allowResizeInDockedMode */);
1915 }
1916 }
1917
1918 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1919 final int count = tasks.size();
1920 for (int i = 0; i < count; i++) {
1921 moveTaskToStackLocked(tasks.get(i).taskId,
1922 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1923 false /* animate */);
1924 }
1925
1926 // stack shouldn't contain anymore activities, so nothing to resume.
1927 r = null;
1928 } else {
1929 // Docked stacks occupy a dedicated region on screen so the size of all other
1930 // static stacks need to be adjusted so they don't overlap with the docked stack.
1931 // We get the bounds to use from window manager which has been adjusted for any
1932 // screen controls and is also the same for all stacks.
1933 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1934 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1935 if (StackId.isResizeableByDockedStack(i)) {
1936 ActivityStack otherStack = getStack(i);
1937 if (otherStack != null) {
1938 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1939 tempOtherTaskInsetBounds, preserveWindows,
1940 true /* allowResizeInDockedMode */);
1941 }
1942 }
1943 }
1944 }
1945 ensureConfigurationAndResume(stack, r, preserveWindows);
1946 } finally {
1947 mWindowManager.continueSurfaceLayout();
1948 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1949 }
1950
1951 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
1952 tempDockedTaskBounds != null
1953 || tempDockedTaskInsetBounds != null
1954 || tempOtherTaskBounds != null
1955 || tempOtherTaskInsetBounds != null);
1956 }
1957
Chong Zhang6de2ae82015-09-30 18:25:21 -07001958 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001959 if (!task.mResizeable) {
1960 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
1961 return;
1962 }
1963
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001964 adjustForMinimalTaskDimensions(task, bounds);
1965
Chong Zhang87b21722015-09-21 15:39:51 -07001966 // If this is a forced resize, let it go through even if the bounds is not changing,
1967 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001968 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001969 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001970 // Nothing to do here...
1971 return;
1972 }
1973
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001974 if (!mWindowManager.isValidTaskId(task.taskId)) {
1975 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001976 // All we can do for now is update the bounds so it can be used when the task is
1977 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001978 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001979 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1980 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001981 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001982 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001983 return;
1984 }
1985
Chong Zhang6de2ae82015-09-30 18:25:21 -07001986 // Do not move the task to another stack here.
1987 // This method assumes that the task is already placed in the right stack.
1988 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001989
Chong Zhang6de2ae82015-09-30 18:25:21 -07001990 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07001991
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001992 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07001993 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001994 // way and the activity was kept the way it was. If it's false, it means the activity had
1995 // to be relaunched due to configuration change.
1996 boolean kept = true;
1997 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001998 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001999 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002000 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002001 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002002 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002003 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002004 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002005 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002006 }
2007 }
2008 }
Chong Zhang87b21722015-09-21 15:39:51 -07002009 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002010
2011 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08002012 }
2013
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002014 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2015 if (bounds == null) {
2016 return;
2017 }
2018 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2019 : task.mMinimalSize;
2020 final boolean adjustWidth = minimalSize > bounds.width();
2021 final boolean adjustHeight = minimalSize > bounds.height();
2022 if (!(adjustWidth || adjustHeight)) {
2023 return;
2024 }
2025 Rect taskBounds = task.mBounds;
2026 if (adjustWidth) {
2027 if (taskBounds != null && bounds.right == taskBounds.right) {
2028 bounds.left = bounds.right - minimalSize;
2029 } else {
2030 // Either left bounds match, or neither match, or the previous bounds were
2031 // fullscreen and we default to keeping left.
2032 bounds.right = bounds.left + minimalSize;
2033 }
2034 }
2035 if (adjustHeight) {
2036 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2037 bounds.top = bounds.bottom - minimalSize;
2038 } else {
2039 // Either top bounds match, or neither match, or the previous bounds were
2040 // fullscreen and we default to keeping top.
2041 bounds.bottom = bounds.top + minimalSize;
2042 }
2043 }
2044 }
2045
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002046 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002047 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2048 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002049 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002050 }
2051
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002052 ActivityContainer activityContainer = new ActivityContainer(stackId);
2053 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002054 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002055 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002056 }
2057
2058 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002059 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002060 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2061 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002062 break;
2063 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002064 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002065 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002066 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002067 }
2068
Chong Zhang5dcb2752015-08-18 13:50:26 -07002069 /**
2070 * Restores a recent task to a stack
2071 * @param task The recent task to be restored.
2072 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002073 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002074 * @return true if the task has been restored successfully.
2075 */
2076 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2077 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002078 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002079 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002080 if (task.stack != null) {
2081 // Task has already been restored once. See if we need to do anything more
2082 if (task.stack.mStackId == stackId) {
2083 // Nothing else to do since it is already restored in the right stack.
2084 return true;
2085 }
2086 // Remove current stack association, so we can re-associate the task with the
2087 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002088 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002089 }
2090
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002091 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002092 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002093
2094 if (stack == null) {
2095 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002096 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2097 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002098 return false;
2099 }
2100
Wale Ogunwale5f986092015-12-04 15:35:38 -08002101 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002102 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2103 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002104 final ArrayList<ActivityRecord> activities = task.mActivities;
2105 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002106 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002107 }
2108 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002109 }
2110
Wale Ogunwale040b4702015-08-06 18:10:50 -07002111 /**
2112 * Moves the specified task record to the input stack id.
2113 * WARNING: This method performs an unchecked/raw move of the task and
2114 * can leave the system in an unstable state if used incorrectly.
2115 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2116 * to a stack.
2117 * @param task Task to move.
2118 * @param stackId Id of stack to move task to.
2119 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002120 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002121 * @param reason Reason the task is been moved.
2122 * @return The stack the task was moved to.
2123 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002124 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002125 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2126 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002127 final ActivityStack prevStack = task.stack;
2128 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2129 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2130 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2131 // Whenever we are moving the top activity from the front stack we want to make sure to move
2132 // the stack to the front.
2133 final boolean wasFront = isFrontStack(prevStack)
2134 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002135
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002136 final boolean resizeable = task.mResizeable;
2137 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2138 // if a docked stack is created below which will lead to the stack we are moving from and
2139 // its resizeable tasks being resized.
2140 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002141 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002142 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002143 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002144 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002145
2146 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002147 // move focus to the new stack by moving the stack to the front.
2148 stack.moveToFrontAndResumeStateIfNeeded(
2149 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002150
Wale Ogunwale040b4702015-08-06 18:10:50 -07002151 return stack;
2152 }
2153
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002154 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002155 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002156 final TaskRecord task = anyTaskForIdLocked(taskId);
2157 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002158 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002159 return;
2160 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002161
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002162 if (task.stack != null && task.stack.mStackId == stackId) {
2163 // You are already in the right stack silly...
2164 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2165 return;
2166 }
2167
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002168 final ActivityRecord topActivity = task.getTopActivity();
2169 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002170 // We are about to relaunch the activity because its configuration changed due to
2171 // being maximized, i.e. size change. The activity will first remove the old window
2172 // and then add a new one. This call will tell window manager about this, so it can
2173 // preserve the old window until the new one is drawn. This prevents having a gap
2174 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002175 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002176 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002177 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002178 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2179 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002180
Jorim Jaggi55387522015-11-24 18:21:10 -08002181 if (!animate) {
2182 stack.mNoAnimActivities.add(topActivity);
2183 }
2184
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002185 // Make sure the task has the appropriate bounds/size for the stack it is in.
2186 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002187 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002188 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2189 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07002190 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2191 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002192 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002193 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002194 }
2195
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002196 // The task might have already been running and its visibility needs to be synchronized with
2197 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002198 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002199 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002200
2201 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002202 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002203 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002204 }
2205
Wale Ogunwale079a0042015-10-24 11:44:07 -07002206 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2207 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2208 if (stack == null) {
2209 throw new IllegalArgumentException(
2210 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2211 }
2212
2213 final ActivityRecord r = stack.topRunningActivityLocked();
2214 if (r == null) {
2215 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2216 + " in stack=" + stack);
2217 return false;
2218 }
2219
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002220 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002221 Slog.w(TAG,
2222 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002223 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002224 return false;
2225 }
2226
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002227 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2228 return true;
2229 }
2230
2231 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002232 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002233 if (task.mActivities.size() == 1) {
2234 // 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 -08002235 // stack without re-parenting the activity in a different task.
2236 moveTaskToStackLocked(
2237 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002238 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002239 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2240 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002241 }
2242
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002243 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002244 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2245 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002246 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002247
2248 // The task might have already been running and its visibility needs to be synchronized with
2249 // the visibility of the stack / windows.
2250 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002251 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002252
2253 if (stackId == PINNED_STACK_ID) {
2254 mService.notifyActivityPinnedLocked();
2255 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002256 }
2257
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002258 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2259 final TaskRecord task = anyTaskForIdLocked(taskId);
2260 if (task == null) {
2261 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2262 return;
2263 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002264 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2265
2266 task.updateOverrideConfigurationForStack(stack);
2267
2268 mWindowManager.positionTaskInStack(
2269 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002270 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002271 // The task might have already been running and its visibility needs to be synchronized with
2272 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002273 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002274 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002275 }
2276
Craig Mautnerac6f8432013-07-17 13:24:59 -07002277 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002278 mTmpFindTaskResult.r = null;
2279 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002280 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002281 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2282 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002283 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2284 final ActivityStack stack = stacks.get(stackNdx);
2285 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002286 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002287 continue;
2288 }
2289 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002290 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2291 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002292 continue;
2293 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002294 stack.findTaskLocked(r, mTmpFindTaskResult);
2295 // It is possible to have task in multiple stacks with the same root affinity.
2296 // If the match we found was based on root affinity we keep on looking to see if
2297 // there is a better match in another stack. We eventually return the match based
2298 // on root affinity if we don't find a better match.
2299 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2300 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002301 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002302 }
2303 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002304 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2305 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002306 }
2307
2308 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002309 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2310 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002311 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2312 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2313 if (ar != null) {
2314 return ar;
2315 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 }
2317 }
2318 return null;
2319 }
2320
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002322 scheduleSleepTimeout();
2323 if (!mGoingToSleep.isHeld()) {
2324 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002325 if (mLaunchingActivity.isHeld()) {
2326 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2327 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002328 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002329 mLaunchingActivity.release();
2330 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002331 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002332 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002333 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002334 }
2335
2336 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002337 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002338
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002339 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002340 final long endTime = System.currentTimeMillis() + timeout;
2341 while (true) {
2342 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002343 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2344 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002345 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2346 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2347 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002348 }
2349 if (cantShutdown) {
2350 long timeRemaining = endTime - System.currentTimeMillis();
2351 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002352 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002353 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002354 } catch (InterruptedException e) {
2355 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002356 } else {
2357 Slog.w(TAG, "Activity manager shutdown timed out");
2358 timedout = true;
2359 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002360 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002361 } else {
2362 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002363 }
2364 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002365
2366 // Force checkReadyForSleep to complete.
2367 mSleepTimeout = true;
2368 checkReadyForSleepLocked();
2369
Craig Mautner8d341ef2013-03-26 09:03:27 -07002370 return timedout;
2371 }
2372
2373 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002374 removeSleepTimeouts();
2375 if (mGoingToSleep.isHeld()) {
2376 mGoingToSleep.release();
2377 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002378 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2379 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002380 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2381 final ActivityStack stack = stacks.get(stackNdx);
2382 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002383 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002384 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002385 }
Craig Mautner5314a402013-09-26 12:40:16 -07002386 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002387 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002388 mGoingToSleepActivities.clear();
2389 }
2390
2391 void activitySleptLocked(ActivityRecord r) {
2392 mGoingToSleepActivities.remove(r);
2393 checkReadyForSleepLocked();
2394 }
2395
2396 void checkReadyForSleepLocked() {
2397 if (!mService.isSleepingOrShuttingDown()) {
2398 // Do not care.
2399 return;
2400 }
2401
2402 if (!mSleepTimeout) {
2403 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002406 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2407 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2408 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002409 }
2410
2411 if (mStoppingActivities.size() > 0) {
2412 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002413 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002414 + mStoppingActivities.size() + " activities");
2415 scheduleIdleLocked();
2416 dontSleep = true;
2417 }
2418
2419 if (mGoingToSleepActivities.size() > 0) {
2420 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002421 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002422 + mGoingToSleepActivities.size() + " activities");
2423 dontSleep = true;
2424 }
2425
2426 if (dontSleep) {
2427 return;
2428 }
2429 }
2430
Craig Mautnere0a38842013-12-16 16:14:02 -08002431 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2432 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002433 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2434 stacks.get(stackNdx).goToSleep();
2435 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002436 }
2437
2438 removeSleepTimeouts();
2439
2440 if (mGoingToSleep.isHeld()) {
2441 mGoingToSleep.release();
2442 }
2443 if (mService.mShuttingDown) {
2444 mService.notifyAll();
2445 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002446 }
2447
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002448 boolean reportResumedActivityLocked(ActivityRecord r) {
2449 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002450 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002451 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002452 }
2453 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002454 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002455 mWindowManager.executeAppTransition();
2456 return true;
2457 }
2458 return false;
2459 }
2460
Craig Mautner8d341ef2013-03-26 09:03:27 -07002461 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002462 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2463 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002464 int stackNdx = stacks.size() - 1;
2465 while (stackNdx >= 0) {
2466 stacks.get(stackNdx).handleAppCrashLocked(app);
2467 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002468 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002469 }
2470 }
2471
Jose Lima4b6c6692014-08-12 17:41:12 -07002472 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002473 final ActivityStack stack = r.task.stack;
2474 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002475 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2476 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002477 return false;
2478 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002479 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002480 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2481 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002482
2483 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002484 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002485 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002486 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002487 return true;
2488 }
2489
2490 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002491 if (visible && top.fullscreen) {
2492 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002493 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2494 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2495 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2496 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002497 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002498 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2499 // Only the activity set as currently visible behind should actively reset its
2500 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002501 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2502 "requestVisibleBehind: returning visible=" + visible
2503 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2504 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002505 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002506 }
2507
Jose Lima4b6c6692014-08-12 17:41:12 -07002508 stack.setVisibleBehindActivity(visible ? r : null);
2509 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002510 // Make the activity immediately above r opaque.
2511 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2512 if (next != null) {
2513 mService.convertFromTranslucent(next.appToken);
2514 }
2515 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002516 if (top.app != null && top.app.thread != null) {
2517 // Notify the top app of the change.
2518 try {
2519 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2520 } catch (RemoteException e) {
2521 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002522 }
2523 return true;
2524 }
2525
Craig Mautnerbb742462014-07-07 15:28:55 -07002526 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2527 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2528 r.mLaunchTaskBehind = false;
2529 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002530 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002531 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002532 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002533 mWindowManager.setAppVisibility(r.appToken, false);
2534 }
2535
2536 void scheduleLaunchTaskBehindComplete(IBinder token) {
2537 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2538 }
2539
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002540 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2541 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002542 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002545 final int topStackNdx = stacks.size() - 1;
2546 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2547 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002548 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002549 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002550 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002551 }
2552
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002553 void invalidateTaskLayers() {
2554 mTaskLayersChanged = true;
2555 }
2556
2557 void rankTaskLayersIfNeeded() {
2558 if (!mTaskLayersChanged) {
2559 return;
2560 }
2561 mTaskLayersChanged = false;
2562 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2563 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2564 int baseLayer = 0;
2565 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2566 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2567 }
2568 }
2569 }
2570
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002571 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2572 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2573 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2574 final int topStackNdx = stacks.size() - 1;
2575 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2576 final ActivityStack stack = stacks.get(stackNdx);
2577 stack.clearOtherAppTimeTrackers(except);
2578 }
2579 }
2580 }
2581
Craig Mautner8d341ef2013-03-26 09:03:27 -07002582 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 final int numStacks = stacks.size();
2586 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2587 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002588 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002589 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 }
2591 }
2592
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002593 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2594 // Examine all activities currently running in the process.
2595 TaskRecord firstTask = null;
2596 // Tasks is non-null only if two or more tasks are found.
2597 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002598 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2599 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002600 ActivityRecord r = app.activities.get(i);
2601 // First, if we find an activity that is in the process of being destroyed,
2602 // then we just aren't going to do anything for now; we want things to settle
2603 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002604 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002605 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002606 return;
2607 }
2608 // Don't consider any activies that are currently not in a state where they
2609 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002610 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2611 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002612 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002613 continue;
2614 }
2615 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002616 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002617 + " from " + r);
2618 if (firstTask == null) {
2619 firstTask = r.task;
2620 } else if (firstTask != r.task) {
2621 if (tasks == null) {
2622 tasks = new ArraySet<>();
2623 tasks.add(firstTask);
2624 }
2625 tasks.add(r.task);
2626 }
2627 }
2628 }
2629 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002630 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002631 return;
2632 }
2633 // If we have activities in multiple tasks that are in a position to be destroyed,
2634 // let's iterate through the tasks and release the oldest one.
2635 final int numDisplays = mActivityDisplays.size();
2636 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2637 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2638 // Step through all stacks starting from behind, to hit the oldest things first.
2639 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2640 final ActivityStack stack = stacks.get(stackNdx);
2641 // Try to release activities in this stack; if we manage to, we are done.
2642 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2643 return;
2644 }
2645 }
2646 }
2647 }
2648
Amith Yamasani37a40c22015-06-17 13:25:42 -07002649 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002650 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002651 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002652 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002653
Craig Mautner858d8a62013-04-23 17:08:34 -07002654 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2656 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002658 final ActivityStack stack = stacks.get(stackNdx);
2659 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002660 TaskRecord task = stack.topTask();
2661 if (task != null) {
2662 mWindowManager.moveTaskToTop(task.taskId);
2663 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002665 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002666
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002667 ActivityStack stack = getStack(restoreStackId);
2668 if (stack == null) {
2669 stack = mHomeStack;
2670 }
2671 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002672 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002673 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 } else {
2675 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002676 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002677 }
Craig Mautner93529a42013-10-04 15:03:13 -07002678 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002679 }
2680
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002681 /**
2682 * Add background users to send boot completed events to.
2683 * @param userId The user being started in the background
2684 * @param uss The state object for the user.
2685 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002686 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002687 mStartingBackgroundUsers.add(uss);
2688 }
2689
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002690 /** Checks whether the userid is a profile of the current user. */
2691 boolean isCurrentProfileLocked(int userId) {
2692 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002693 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002694 }
2695
Chong Zhang45c25ce2015-08-10 22:18:26 -07002696 /** Checks whether the activity should be shown for current user. */
2697 boolean okToShowLocked(ActivityRecord r) {
2698 return r != null && (isCurrentProfileLocked(r.userId)
2699 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2700 }
2701
Craig Mautnerde4ef022013-04-07 19:01:33 -07002702 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002703 ArrayList<ActivityRecord> stops = null;
2704
2705 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002706 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2707 ActivityRecord s = mStoppingActivities.get(activityNdx);
2708 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002709 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002710 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2711 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002712 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002713 if (s.finishing) {
2714 // If this activity is finishing, it is sitting on top of
2715 // everyone else but we now know it is no longer needed...
2716 // so get rid of it. Otherwise, we need to go through the
2717 // normal flow and hide it once we determine that it is
2718 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002719 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002720 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002721 }
2722 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002723 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002724 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002725 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002726 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002727 }
2728 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002729 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002730 }
2731 }
2732
2733 return stops;
2734 }
2735
Craig Mautnercf910b02013-04-23 11:23:27 -07002736 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2740 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002741 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002742 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002743 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002744 if (r == null) Slog.e(TAG,
2745 "validateTop...: null top activity, stack=" + stack);
2746 else {
2747 final ActivityRecord pausing = stack.mPausingActivity;
2748 if (pausing != null && pausing == r) Slog.e(TAG,
2749 "validateTop...: top stack has pausing activity r=" + r
2750 + " state=" + state);
2751 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2752 "validateTop...: activity in front not resumed r=" + r
2753 + " state=" + state);
2754 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002755 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002756 final ActivityRecord resumed = stack.mResumedActivity;
2757 if (resumed != null && resumed == r) Slog.e(TAG,
2758 "validateTop...: back stack has resumed activity r=" + r
2759 + " state=" + state);
2760 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2761 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002762 }
2763 }
2764 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002765 }
2766
Craig Mautnere0570202015-05-13 13:06:11 -07002767 private String lockTaskModeToString() {
2768 switch (mLockTaskModeState) {
2769 case LOCK_TASK_MODE_LOCKED:
2770 return "LOCKED";
2771 case LOCK_TASK_MODE_PINNED:
2772 return "PINNED";
2773 case LOCK_TASK_MODE_NONE:
2774 return "NONE";
2775 default: return "unknown=" + mLockTaskModeState;
2776 }
2777 }
2778
Craig Mautner27084302013-03-25 08:05:25 -07002779 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002780 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002781 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002782 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2783 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2784 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002785 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002786 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2787 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2788 if (packages.size() > 0) {
2789 pw.println(" mLockTaskPackages (userId:packages)=");
2790 for (int i = 0; i < packages.size(); ++i) {
2791 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2792 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2793 }
2794 }
2795 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002796 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002797
Craig Mautner20e72272013-04-01 13:45:53 -07002798 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002799 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002800 }
2801
Dianne Hackborn390517b2013-05-30 15:03:32 -07002802 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2803 boolean needSep, String prefix) {
2804 if (activity != null) {
2805 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2806 if (needSep) {
2807 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002808 }
2809 pw.print(prefix);
2810 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002811 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002812 }
2813 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002814 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002815 }
2816
Craig Mautner8d341ef2013-03-26 09:03:27 -07002817 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2818 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002819 boolean printed = false;
2820 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002821 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2822 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002823 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002824 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002825 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002827 final ActivityStack stack = stacks.get(stackNdx);
2828 StringBuilder stackHeader = new StringBuilder(128);
2829 stackHeader.append(" Stack #");
2830 stackHeader.append(stack.mStackId);
2831 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002832 stackHeader.append("\n");
2833 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2834 stackHeader.append("\n");
2835 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2837 needSep, stackHeader.toString());
2838 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2839 !dumpAll, false, dumpPackage, true,
2840 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002841
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 needSep = printed;
2843 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2844 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002845 if (pr) {
2846 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002847 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002848 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002849 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2850 " mResumedActivity: ");
2851 if (pr) {
2852 printed = true;
2853 needSep = false;
2854 }
2855 if (dumpAll) {
2856 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2857 " mLastPausedActivity: ");
2858 if (pr) {
2859 printed = true;
2860 needSep = true;
2861 }
2862 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2863 needSep, " mLastNoHistoryActivity: ");
2864 }
2865 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002866 }
2867 }
2868
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002869 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2870 false, dumpPackage, true, " Activities waiting to finish:", null);
2871 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2872 false, dumpPackage, true, " Activities waiting to stop:", null);
2873 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2874 false, dumpPackage, true, " Activities waiting for another to become visible:",
2875 null);
2876 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2877 false, dumpPackage, true, " Activities waiting to sleep:", null);
2878 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2879 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002880
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002881 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 }
2883
Dianne Hackborn390517b2013-05-30 15:03:32 -07002884 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002885 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002886 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002888 String innerPrefix = null;
2889 String[] args = null;
2890 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 for (int i=list.size()-1; i>=0; i--) {
2892 final ActivityRecord r = list.get(i);
2893 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2894 continue;
2895 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002896 if (innerPrefix == null) {
2897 innerPrefix = prefix + " ";
2898 args = new String[0];
2899 }
2900 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002901 final boolean full = !brief && (complete || !r.isInHistory());
2902 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002903 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002904 needNL = false;
2905 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002906 if (header1 != null) {
2907 pw.println(header1);
2908 header1 = null;
2909 }
2910 if (header2 != null) {
2911 pw.println(header2);
2912 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002913 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002914 if (lastTask != r.task) {
2915 lastTask = r.task;
2916 pw.print(prefix);
2917 pw.print(full ? "* " : " ");
2918 pw.println(lastTask);
2919 if (full) {
2920 lastTask.dump(pw, prefix + " ");
2921 } else if (complete) {
2922 // Complete + brief == give a summary. Isn't that obvious?!?
2923 if (lastTask.intent != null) {
2924 pw.print(prefix); pw.print(" ");
2925 pw.println(lastTask.intent.toInsecureStringWithClip());
2926 }
2927 }
2928 }
2929 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2930 pw.print(" #"); pw.print(i); pw.print(": ");
2931 pw.println(r);
2932 if (full) {
2933 r.dump(pw, innerPrefix);
2934 } else if (complete) {
2935 // Complete + brief == give a summary. Isn't that obvious?!?
2936 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2937 if (r.app != null) {
2938 pw.print(innerPrefix); pw.println(r.app);
2939 }
2940 }
2941 if (client && r.app != null && r.app.thread != null) {
2942 // flush anything that is already in the PrintWriter since the thread is going
2943 // to write to the file descriptor directly
2944 pw.flush();
2945 try {
2946 TransferPipe tp = new TransferPipe();
2947 try {
2948 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2949 r.appToken, innerPrefix, args);
2950 // Short timeout, since blocking here can
2951 // deadlock with the application.
2952 tp.go(fd, 2000);
2953 } finally {
2954 tp.kill();
2955 }
2956 } catch (IOException e) {
2957 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2958 } catch (RemoteException e) {
2959 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2960 }
2961 needNL = true;
2962 }
2963 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002964 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002965 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002966
Craig Mautnerf3333272013-04-22 10:55:53 -07002967 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002968 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2969 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002970 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2971 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002972 }
2973
2974 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002975 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002976 }
2977
2978 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002979 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2980 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002981 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2982 }
2983
Craig Mautner05d29032013-05-03 13:40:13 -07002984 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002985 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2986 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2987 }
Craig Mautner05d29032013-05-03 13:40:13 -07002988 }
2989
Craig Mautner0eea92c2013-05-16 13:35:39 -07002990 void removeSleepTimeouts() {
2991 mSleepTimeout = false;
2992 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2993 }
2994
2995 final void scheduleSleepTimeout() {
2996 removeSleepTimeouts();
2997 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2998 }
2999
Craig Mautner4a1cb222013-12-04 16:14:06 -08003000 @Override
3001 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003002 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3004 }
3005
3006 @Override
3007 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003008 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3010 }
3011
3012 @Override
3013 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003014 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003015 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3016 }
3017
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003018 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003019 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003020 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003021 newDisplay = mActivityDisplays.get(displayId) == null;
3022 if (newDisplay) {
3023 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003024 if (activityDisplay.mDisplay == null) {
3025 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3026 return;
3027 }
Craig Mautner4504de52013-12-20 09:06:56 -08003028 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003029 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003030 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003031 }
Craig Mautner4504de52013-12-20 09:06:56 -08003032 if (newDisplay) {
3033 mWindowManager.onDisplayAdded(displayId);
3034 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003035 }
3036
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003037 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3038 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3039 return;
3040 }
3041 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3042 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3043 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3044 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3045 }
3046
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003047 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003048 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003049 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3050 if (activityDisplay != null) {
3051 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003052 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003053 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003054 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003055 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003056 }
3057 }
3058 mWindowManager.onDisplayRemoved(displayId);
3059 }
3060
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003061 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003063 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3064 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 // TODO: Update the bounds.
3066 }
3067 }
3068 mWindowManager.onDisplayChanged(displayId);
3069 }
3070
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003071 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003072 StackInfo info = new StackInfo();
3073 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3074 info.displayId = Display.DEFAULT_DISPLAY;
3075 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003076 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003077
3078 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3079 final int numTasks = tasks.size();
3080 int[] taskIds = new int[numTasks];
3081 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003082 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003083 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003084 for (int i = 0; i < numTasks; ++i) {
3085 final TaskRecord task = tasks.get(i);
3086 taskIds[i] = task.taskId;
3087 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3088 : task.realActivity != null ? task.realActivity.flattenToString()
3089 : task.getTopActivity() != null ? task.getTopActivity().packageName
3090 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003091 taskBounds[i] = new Rect();
3092 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003093 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003094 }
3095 info.taskIds = taskIds;
3096 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003097 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003098 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003099 return info;
3100 }
3101
3102 StackInfo getStackInfoLocked(int stackId) {
3103 ActivityStack stack = getStack(stackId);
3104 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003105 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 }
3107 return null;
3108 }
3109
3110 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003111 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003112 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3113 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003114 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003115 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003116 }
3117 }
3118 return list;
3119 }
3120
Craig Mautner15df08a2015-04-01 12:17:18 -07003121 TaskRecord getLockedTaskLocked() {
3122 final int top = mLockTaskModeTasks.size() - 1;
3123 if (top >= 0) {
3124 return mLockTaskModeTasks.get(top);
3125 }
3126 return null;
3127 }
3128
3129 boolean isLockedTask(TaskRecord task) {
3130 return mLockTaskModeTasks.contains(task);
3131 }
3132
3133 boolean isLastLockedTask(TaskRecord task) {
3134 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3135 }
3136
3137 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003138 if (!mLockTaskModeTasks.remove(task)) {
3139 return;
3140 }
3141 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3142 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003143 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003144 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3145 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003146 final Message lockTaskMsg = Message.obtain();
3147 lockTaskMsg.arg1 = task.userId;
3148 lockTaskMsg.what = LOCK_TASK_END_MSG;
3149 mHandler.sendMessage(lockTaskMsg);
3150 }
3151 }
3152
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003153 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003154 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003155 }
3156
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003157 void showLockTaskToast() {
3158 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003159 }
3160
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003161 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3162 if (mLockTaskModeTasks.contains(task)) {
3163 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3164 }
3165 }
3166
Craig Mautner432f64e2015-05-20 14:59:57 -07003167 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3168 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003169 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003170 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003171 final TaskRecord lockedTask = getLockedTaskLocked();
3172 if (lockedTask != null) {
3173 removeLockedTaskLocked(lockedTask);
3174 if (!mLockTaskModeTasks.isEmpty()) {
3175 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003176 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3177 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003178 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003179 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003180 return;
3181 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003182 }
Craig Mautnere0570202015-05-13 13:06:11 -07003183 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3184 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003185 return;
3186 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003187
3188 // Should have already been checked, but do it again.
3189 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003190 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3191 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003192 return;
3193 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003194 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003195 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003196 return;
3197 }
3198
3199 if (mLockTaskModeTasks.isEmpty()) {
3200 // First locktask.
3201 final Message lockTaskMsg = Message.obtain();
3202 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3203 lockTaskMsg.arg1 = task.userId;
3204 lockTaskMsg.what = LOCK_TASK_START_MSG;
3205 lockTaskMsg.arg2 = lockTaskModeState;
3206 mHandler.sendMessage(lockTaskMsg);
3207 }
3208 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003209 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3210 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003211 mLockTaskModeTasks.remove(task);
3212 mLockTaskModeTasks.add(task);
3213
3214 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003215 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003216 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003217
3218 if (andResume) {
3219 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003220 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003221 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003222 }
3223
3224 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003225 return isLockTaskModeViolation(task, false);
3226 }
3227
3228 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3229 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003230 return false;
3231 }
3232 final int lockTaskAuth = task.mLockTaskAuth;
3233 switch (lockTaskAuth) {
3234 case LOCK_TASK_AUTH_DONT_LOCK:
3235 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003236 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003237 case LOCK_TASK_AUTH_LAUNCHABLE:
3238 case LOCK_TASK_AUTH_WHITELISTED:
3239 return false;
3240 case LOCK_TASK_AUTH_PINNABLE:
3241 // Pinnable tasks can't be launched on top of locktask tasks.
3242 return !mLockTaskModeTasks.isEmpty();
3243 default:
3244 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3245 return true;
3246 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003247 }
3248
Craig Mautner15df08a2015-04-01 12:17:18 -07003249 void onLockTaskPackagesUpdatedLocked() {
3250 boolean didSomething = false;
3251 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3252 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003253 final boolean wasWhitelisted =
3254 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3255 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003256 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003257 final boolean isWhitelisted =
3258 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3259 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3260 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003261 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003262 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3263 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003264 removeLockedTaskLocked(lockedTask);
3265 lockedTask.performClearTaskLocked();
3266 didSomething = true;
3267 }
3268 }
3269 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3270 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3271 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3272 final ActivityStack stack = stacks.get(stackNdx);
3273 stack.onLockTaskPackagesUpdatedLocked();
3274 }
3275 }
Craig Mautnere0570202015-05-13 13:06:11 -07003276 final ActivityRecord r = topRunningActivityLocked();
3277 final TaskRecord task = r != null ? r.task : null;
3278 if (mLockTaskModeTasks.isEmpty() && task != null
3279 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3280 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003281 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3282 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3283 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3284 false);
3285 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003286 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003287 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003288 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003289 }
3290 }
3291
Benjamin Franz43261142015-02-11 15:59:44 +00003292 int getLockTaskModeState() {
3293 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003294 }
3295
Jorim Jaggife89d122015-12-22 16:28:44 +01003296 void activityRelaunchedLocked(IBinder token) {
3297 mWindowManager.notifyAppRelaunchingFinished(token);
3298 }
3299
3300 void activityRelaunchingLocked(ActivityRecord r) {
3301 mWindowManager.notifyAppRelaunching(r.appToken);
3302 }
3303
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003304 void logStackState() {
3305 mActivityMetricsLogger.logWindowState();
3306 }
3307
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003308 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003309
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003310 public ActivityStackSupervisorHandler(Looper looper) {
3311 super(looper);
3312 }
3313
Craig Mautnerf3333272013-04-22 10:55:53 -07003314 void activityIdleInternal(ActivityRecord r) {
3315 synchronized (mService) {
3316 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3317 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003318 }
3319
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003320 @Override
3321 public void handleMessage(Message msg) {
3322 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003323 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003324 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3325 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003326 if (mService.mDidDexOpt) {
3327 mService.mDidDexOpt = false;
3328 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3329 nmsg.obj = msg.obj;
3330 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3331 return;
3332 }
3333 // We don't at this point know if the activity is fullscreen,
3334 // so we need to be conservative and assume it isn't.
3335 activityIdleInternal((ActivityRecord)msg.obj);
3336 } break;
3337 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003338 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003339 activityIdleInternal((ActivityRecord)msg.obj);
3340 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003341 case RESUME_TOP_ACTIVITY_MSG: {
3342 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003343 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003344 }
3345 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003346 case SLEEP_TIMEOUT_MSG: {
3347 synchronized (mService) {
3348 if (mService.isSleepingOrShuttingDown()) {
3349 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3350 mSleepTimeout = true;
3351 checkReadyForSleepLocked();
3352 }
3353 }
3354 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003355 case LAUNCH_TIMEOUT_MSG: {
3356 if (mService.mDidDexOpt) {
3357 mService.mDidDexOpt = false;
3358 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3359 return;
3360 }
3361 synchronized (mService) {
3362 if (mLaunchingActivity.isHeld()) {
3363 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3364 if (VALIDATE_WAKE_LOCK_CALLER
3365 && Binder.getCallingUid() != Process.myUid()) {
3366 throw new IllegalStateException("Calling must be system uid");
3367 }
3368 mLaunchingActivity.release();
3369 }
3370 }
3371 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003373 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003374 } break;
3375 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003376 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 } break;
3378 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003379 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003380 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003381 case CONTAINER_CALLBACK_VISIBILITY: {
3382 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003383 final IActivityContainerCallback callback = container.mCallback;
3384 if (callback != null) {
3385 try {
3386 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3387 } catch (RemoteException e) {
3388 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003389 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003390 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003391 case LOCK_TASK_START_MSG: {
3392 // When lock task starts, we disable the status bars.
3393 try {
Jason Monk62515be2014-05-21 16:06:19 -04003394 if (mLockTaskNotify == null) {
3395 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003396 }
Jason Monk62515be2014-05-21 16:06:19 -04003397 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003398 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003399 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003400 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003401 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003402 flags = StatusBarManager.DISABLE_MASK
3403 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003404 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003405 flags = StatusBarManager.DISABLE_MASK
3406 & (~StatusBarManager.DISABLE_BACK)
3407 & (~StatusBarManager.DISABLE_HOME)
3408 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003409 }
3410 getStatusBarService().disable(flags, mToken,
3411 mService.mContext.getPackageName());
3412 }
3413 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003414 if (getDevicePolicyManager() != null) {
3415 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003416 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003417 }
justinzhang5286d3f2014-05-12 17:06:01 -04003418 } catch (RemoteException ex) {
3419 throw new RuntimeException(ex);
3420 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003421 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003422 case LOCK_TASK_END_MSG: {
3423 // When lock task ends, we enable the status bars.
3424 try {
Jason Monk62515be2014-05-21 16:06:19 -04003425 if (getStatusBarService() != null) {
3426 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3427 mService.mContext.getPackageName());
3428 }
3429 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003430 if (getDevicePolicyManager() != null) {
3431 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3432 msg.arg1);
3433 }
Jason Monk62515be2014-05-21 16:06:19 -04003434 if (mLockTaskNotify == null) {
3435 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3436 }
3437 mLockTaskNotify.show(false);
3438 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003439 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003440 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003441 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003442 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003443 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003444 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003445 new LockPatternUtils(mService.mContext)
3446 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003447 }
3448 } catch (SettingNotFoundException e) {
3449 // No setting, don't lock.
3450 }
justinzhang5286d3f2014-05-12 17:06:01 -04003451 } catch (RemoteException ex) {
3452 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003453 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003454 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003455 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003456 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003457 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3458 if (mLockTaskNotify == null) {
3459 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3460 }
3461 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3462 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003463 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3464 final ActivityContainer container = (ActivityContainer) msg.obj;
3465 final IActivityContainerCallback callback = container.mCallback;
3466 if (callback != null) {
3467 try {
3468 callback.onAllActivitiesComplete(container.asBinder());
3469 } catch (RemoteException e) {
3470 }
3471 }
3472 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003473 case LAUNCH_TASK_BEHIND_COMPLETE: {
3474 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003475 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003476 if (r != null) {
3477 handleLaunchTaskBehindCompleteLocked(r);
3478 }
3479 }
3480 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003481
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003482 }
3483 }
3484 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003485
Ying Wangb081a592014-04-22 15:20:16 -07003486 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003487 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3488 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003490 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003491 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003492 ActivityRecord mParentActivity = null;
3493 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003494
Craig Mautnere3a00d72014-04-16 08:31:19 -07003495 boolean mVisible = true;
3496
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003498 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003500 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3501 final static int CONTAINER_STATE_NO_SURFACE = 1;
3502 final static int CONTAINER_STATE_FINISHING = 2;
3503 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3504
3505 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003506 synchronized (mService) {
3507 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003508 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003509 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003510 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003511 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003512 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003513
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003514 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003515 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003516 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003517 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003518 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003519 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003520 }
3521
3522 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003523 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003525 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3526 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 return;
3528 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003529 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 }
3531 }
3532
3533 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003534 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003535 synchronized (mService) {
3536 if (mActivityDisplay != null) {
3537 return mActivityDisplay.mDisplayId;
3538 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003539 }
3540 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 }
3542
Jeff Brownca9bc702014-02-11 14:32:56 -08003543 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003544 public int getStackId() {
3545 synchronized (mService) {
3546 return mStackId;
3547 }
3548 }
3549
3550 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003551 public boolean injectEvent(InputEvent event) {
3552 final long origId = Binder.clearCallingIdentity();
3553 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003554 synchronized (mService) {
3555 if (mActivityDisplay != null) {
3556 return mInputManagerInternal.injectInputEvent(event,
3557 mActivityDisplay.mDisplayId,
3558 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3559 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003560 }
3561 return false;
3562 } finally {
3563 Binder.restoreCallingIdentity(origId);
3564 }
3565 }
3566
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003567 @Override
3568 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003569 synchronized (mService) {
3570 if (mContainerState == CONTAINER_STATE_FINISHING) {
3571 return;
3572 }
3573 mContainerState = CONTAINER_STATE_FINISHING;
3574
Craig Mautnerd163e752014-06-13 17:18:47 -07003575 long origId = Binder.clearCallingIdentity();
3576 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003577 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003578 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003579 } finally {
3580 Binder.restoreCallingIdentity(origId);
3581 }
3582 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003583 }
3584
Craig Mautner60257702014-09-17 15:02:33 -07003585 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003586 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003587 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003588 if (mActivityDisplay != null) {
3589 mActivityDisplay.detachActivitiesLocked(mStack);
3590 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003591 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003592 }
3593 }
3594
3595 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003596 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003597 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 }
3599
3600 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003601 public final int startActivityIntentSender(IIntentSender intentSender)
3602 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003603 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3604
3605 if (!(intentSender instanceof PendingIntentRecord)) {
3606 throw new IllegalArgumentException("Bad PendingIntent object");
3607 }
3608
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003609 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003610 Binder.getCallingUid(), mCurrentUser, false,
3611 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003612
3613 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3614 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3615 pendingIntent.key.requestResolvedType);
3616
3617 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3618 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3619 }
3620
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003621 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003622 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003623 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003624 throw new SecurityException(
3625 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3626 }
3627 }
3628
Craig Mautnerdf88d732014-01-27 09:21:32 -08003629 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 public IBinder asBinder() {
3631 return this;
3632 }
3633
Craig Mautner4504de52013-12-20 09:06:56 -08003634 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003635 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003636 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003637 }
3638
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 ActivityStackSupervisor getOuter() {
3640 return ActivityStackSupervisor.this;
3641 }
3642
Craig Mautnerd163e752014-06-13 17:18:47 -07003643 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003644 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003645 }
3646
Craig Mautner6985bad2014-04-21 15:22:06 -07003647 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003648 void setVisible(boolean visible) {
3649 if (mVisible != visible) {
3650 mVisible = visible;
3651 if (mCallback != null) {
3652 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3653 0 /* unused */, this).sendToTarget();
3654 }
3655 }
3656 }
3657
Craig Mautner6985bad2014-04-21 15:22:06 -07003658 void setDrawn() {
3659 }
3660
Craig Mautner1b4bf852014-05-26 15:06:32 -07003661 // You can always start a new task on a regular ActivityStack.
3662 boolean isEligibleForNewTasks() {
3663 return true;
3664 }
3665
Craig Mautnerd163e752014-06-13 17:18:47 -07003666 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003667 detachLocked();
3668 deleteActivityContainer(this);
3669 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003670 }
3671
Craig Mautner34b73df2014-01-12 21:11:08 -08003672 @Override
3673 public String toString() {
3674 return mIdString + (mActivityDisplay == null ? "N" : "A");
3675 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003676 }
3677
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003678 private class VirtualActivityContainer extends ActivityContainer {
3679 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003680 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003681
3682 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3683 super(getNextStackId());
3684 mParentActivity = parent;
3685 mCallback = callback;
3686 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003687 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003688 }
3689
3690 @Override
3691 public void setSurface(Surface surface, int width, int height, int density) {
3692 super.setSurface(surface, width, height, density);
3693
3694 synchronized (mService) {
3695 final long origId = Binder.clearCallingIdentity();
3696 try {
3697 setSurfaceLocked(surface, width, height, density);
3698 } finally {
3699 Binder.restoreCallingIdentity(origId);
3700 }
3701 }
3702 }
3703
3704 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3705 if (mContainerState == CONTAINER_STATE_FINISHING) {
3706 return;
3707 }
3708 VirtualActivityDisplay virtualActivityDisplay =
3709 (VirtualActivityDisplay) mActivityDisplay;
3710 if (virtualActivityDisplay == null) {
3711 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003712 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003713 mActivityDisplay = virtualActivityDisplay;
3714 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003715 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003716 }
3717
3718 if (mSurface != null) {
3719 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003720 }
3721
Craig Mautner6985bad2014-04-21 15:22:06 -07003722 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003723 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003724 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003725 } else {
3726 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003727 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003728 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003729 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003730 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003731 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003732
Craig Mautnerd163e752014-06-13 17:18:47 -07003733 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003734
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003735 if (DEBUG_STACK) Slog.d(TAG_STACK,
3736 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003737 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003738
Craig Mautner6985bad2014-04-21 15:22:06 -07003739 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003740 boolean isAttachedLocked() {
3741 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003742 }
3743
3744 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003745 void setDrawn() {
3746 synchronized (mService) {
3747 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003748 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003749 }
3750 }
3751
Craig Mautner1b4bf852014-05-26 15:06:32 -07003752 // Never start a new task on an ActivityView if it isn't explicitly specified.
3753 @Override
3754 boolean isEligibleForNewTasks() {
3755 return false;
3756 }
3757
Craig Mautnerd163e752014-06-13 17:18:47 -07003758 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003759 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003760 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3761 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3762 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3763 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3764 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003765 }
3766 }
3767
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3769 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003770 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003772 int mDisplayId;
3773 Display mDisplay;
3774 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003775
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3777 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003778 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003779
Jose Lima4b6c6692014-08-12 17:41:12 -07003780 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003781
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003782 ActivityDisplay() {
3783 }
Craig Mautner4504de52013-12-20 09:06:56 -08003784
Craig Mautner1a70a162014-09-13 12:09:31 -07003785 // After instantiation, check that mDisplay is not null before using this. The alternative
3786 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003787 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003788 final Display display = mDisplayManager.getDisplay(displayId);
3789 if (display == null) {
3790 return;
3791 }
3792 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003793 }
3794
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003795 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003796 mDisplay = display;
3797 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003798 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003799 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003801 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003802 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003803 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3804 + " onTop=" + onTop);
3805 if (onTop) {
3806 mStacks.add(stack);
3807 } else {
3808 mStacks.add(0, stack);
3809 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003810 }
3811
Craig Mautnere0a38842013-12-16 16:14:02 -08003812 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003813 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003814 + " from displayId=" + mDisplayId);
3815 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 }
3817
Jose Lima4b6c6692014-08-12 17:41:12 -07003818 void setVisibleBehindActivity(ActivityRecord r) {
3819 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003820 }
3821
Jose Lima4b6c6692014-08-12 17:41:12 -07003822 boolean hasVisibleBehindActivity() {
3823 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003824 }
3825
Craig Mautner34b73df2014-01-12 21:11:08 -08003826 @Override
3827 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003828 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3829 }
3830 }
3831
3832 class VirtualActivityDisplay extends ActivityDisplay {
3833 VirtualDisplay mVirtualDisplay;
3834
Craig Mautner6985bad2014-04-21 15:22:06 -07003835 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003836 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003837 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3838 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3839 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3840 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003841
3842 init(mVirtualDisplay.getDisplay());
3843
3844 mWindowManager.handleDisplayAdded(mDisplayId);
3845 }
3846
3847 void setSurface(Surface surface) {
3848 if (mVirtualDisplay != null) {
3849 mVirtualDisplay.setSurface(surface);
3850 }
3851 }
3852
3853 @Override
3854 void detachActivitiesLocked(ActivityStack stack) {
3855 super.detachActivitiesLocked(stack);
3856 if (mVirtualDisplay != null) {
3857 mVirtualDisplay.release();
3858 mVirtualDisplay = null;
3859 }
3860 }
3861
3862 @Override
3863 public String toString() {
3864 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003865 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003866 }
Jose Lima58e66d62014-05-27 20:00:27 -07003867
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003868 /**
3869 * Adjust bounds to stay within stack bounds.
3870 *
3871 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3872 * that keep them unchanged, but be contained within the stack bounds.
3873 *
3874 * @param bounds Bounds to be adjusted.
3875 * @param stackBounds Bounds within which the other bounds should remain.
3876 */
3877 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3878 if (stackBounds == null || stackBounds.contains(bounds)) {
3879 return;
3880 }
3881
3882 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3883 final int maxRight = stackBounds.right
3884 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3885 int horizontalDiff = stackBounds.left - bounds.left;
3886 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3887 || (bounds.left + horizontalDiff >= maxRight)) {
3888 horizontalDiff = maxRight - bounds.left;
3889 }
3890 bounds.left += horizontalDiff;
3891 bounds.right += horizontalDiff;
3892 }
3893
3894 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3895 final int maxBottom = stackBounds.bottom
3896 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3897 int verticalDiff = stackBounds.top - bounds.top;
3898 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3899 || (bounds.top + verticalDiff >= maxBottom)) {
3900 verticalDiff = maxBottom - bounds.top;
3901 }
3902 bounds.top += verticalDiff;
3903 bounds.bottom += verticalDiff;
3904 }
3905 }
3906
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003907 ActivityStack findStackBehind(ActivityStack stack) {
3908 // TODO(multi-display): We are only looking for stacks on the default display.
3909 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3910 if (display == null) {
3911 return null;
3912 }
3913 final ArrayList<ActivityStack> stacks = display.mStacks;
3914 for (int i = stacks.size() - 1; i >= 0; i--) {
3915 if (stacks.get(i) == stack && i > 0) {
3916 return stacks.get(i - 1);
3917 }
3918 }
3919 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3920 + " in=" + stacks);
3921 }
Craig Mautner27084302013-03-25 08:05:25 -07003922}