blob: c634e0e6a6ac38a67e8a265f8e1791f8b8ca0712 [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 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001871
1872 // We might trigger a configuration change. Save the current task bounds for freezing.
1873 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001874 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1875 mTmpBounds, mTmpInsetBounds);
1876 stack.setBounds(bounds);
1877 }
1878
1879 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1880 boolean preserveWindows) {
1881 if (r == null) {
1882 return;
1883 }
1884 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1885 preserveWindows);
1886 // And we need to make sure at this point that all other activities
1887 // are made visible with the correct configuration.
1888 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1889 if (!updated) {
1890 resumeFocusedStackTopActivityLocked();
1891 }
1892 }
1893
1894 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1895 Rect tempDockedTaskInsetBounds,
1896 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1897 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1898 if (stack == null) {
1899 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1900 return;
1901 }
1902
1903 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1904 mWindowManager.deferSurfaceLayout();
1905 try {
1906 ActivityRecord r = stack.topRunningActivityLocked();
1907 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1908 tempDockedTaskInsetBounds);
1909
1910 if (stack.mFullscreen) {
1911 // The dock stack went fullscreen which is kinda like dismissing it.
1912 // In this case we make all other static stacks fullscreen and move all
1913 // docked stack tasks to the fullscreen stack.
1914 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1915 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1916 resizeStackLocked(i, null, null, null, preserveWindows,
1917 true /* allowResizeInDockedMode */);
1918 }
1919 }
1920
1921 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1922 final int count = tasks.size();
1923 for (int i = 0; i < count; i++) {
1924 moveTaskToStackLocked(tasks.get(i).taskId,
1925 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1926 false /* animate */);
1927 }
1928
1929 // stack shouldn't contain anymore activities, so nothing to resume.
1930 r = null;
1931 } else {
1932 // Docked stacks occupy a dedicated region on screen so the size of all other
1933 // static stacks need to be adjusted so they don't overlap with the docked stack.
1934 // We get the bounds to use from window manager which has been adjusted for any
1935 // screen controls and is also the same for all stacks.
1936 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1937 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1938 if (StackId.isResizeableByDockedStack(i)) {
1939 ActivityStack otherStack = getStack(i);
1940 if (otherStack != null) {
1941 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1942 tempOtherTaskInsetBounds, preserveWindows,
1943 true /* allowResizeInDockedMode */);
1944 }
1945 }
1946 }
1947 }
1948 ensureConfigurationAndResume(stack, r, preserveWindows);
1949 } finally {
1950 mWindowManager.continueSurfaceLayout();
1951 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1952 }
1953
1954 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
1955 tempDockedTaskBounds != null
1956 || tempDockedTaskInsetBounds != null
1957 || tempOtherTaskBounds != null
1958 || tempOtherTaskInsetBounds != null);
1959 }
1960
Chong Zhangf596cd52016-01-05 13:42:44 -08001961 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001962 if (!task.mResizeable) {
1963 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08001964 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001965 }
1966
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001967 adjustForMinimalTaskDimensions(task, bounds);
1968
Chong Zhang87b21722015-09-21 15:39:51 -07001969 // If this is a forced resize, let it go through even if the bounds is not changing,
1970 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001971 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001972 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001973 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08001974 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001975 }
1976
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001977 if (!mWindowManager.isValidTaskId(task.taskId)) {
1978 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001979 // All we can do for now is update the bounds so it can be used when the task is
1980 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001981 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001982 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1983 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001984 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001985 }
Chong Zhangf596cd52016-01-05 13:42:44 -08001986 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001987 }
1988
Chong Zhang6de2ae82015-09-30 18:25:21 -07001989 // Do not move the task to another stack here.
1990 // This method assumes that the task is already placed in the right stack.
1991 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001992
Chong Zhang6de2ae82015-09-30 18:25:21 -07001993 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07001994
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001995 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07001996 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001997 // way and the activity was kept the way it was. If it's false, it means the activity had
1998 // to be relaunched due to configuration change.
1999 boolean kept = true;
2000 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002001 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002002 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002003 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002004 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002005 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002006 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002007 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002008 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002009 }
2010 }
2011 }
Chong Zhang87b21722015-09-21 15:39:51 -07002012 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002013
2014 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002015 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002016 }
2017
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002018 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2019 if (bounds == null) {
2020 return;
2021 }
2022 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2023 : task.mMinimalSize;
2024 final boolean adjustWidth = minimalSize > bounds.width();
2025 final boolean adjustHeight = minimalSize > bounds.height();
2026 if (!(adjustWidth || adjustHeight)) {
2027 return;
2028 }
2029 Rect taskBounds = task.mBounds;
2030 if (adjustWidth) {
2031 if (taskBounds != null && bounds.right == taskBounds.right) {
2032 bounds.left = bounds.right - minimalSize;
2033 } else {
2034 // Either left bounds match, or neither match, or the previous bounds were
2035 // fullscreen and we default to keeping left.
2036 bounds.right = bounds.left + minimalSize;
2037 }
2038 }
2039 if (adjustHeight) {
2040 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2041 bounds.top = bounds.bottom - minimalSize;
2042 } else {
2043 // Either top bounds match, or neither match, or the previous bounds were
2044 // fullscreen and we default to keeping top.
2045 bounds.bottom = bounds.top + minimalSize;
2046 }
2047 }
2048 }
2049
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002050 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002051 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2052 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002053 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002054 }
2055
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002056 ActivityContainer activityContainer = new ActivityContainer(stackId);
2057 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002058 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002059 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002060 }
2061
2062 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002063 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002064 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2065 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002066 break;
2067 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002068 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002069 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002070 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002071 }
2072
Chong Zhang5dcb2752015-08-18 13:50:26 -07002073 /**
2074 * Restores a recent task to a stack
2075 * @param task The recent task to be restored.
2076 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002077 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002078 * @return true if the task has been restored successfully.
2079 */
2080 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2081 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002082 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002083 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002084 if (task.stack != null) {
2085 // Task has already been restored once. See if we need to do anything more
2086 if (task.stack.mStackId == stackId) {
2087 // Nothing else to do since it is already restored in the right stack.
2088 return true;
2089 }
2090 // Remove current stack association, so we can re-associate the task with the
2091 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002092 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002093 }
2094
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002095 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002096 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002097
2098 if (stack == null) {
2099 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002100 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2101 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002102 return false;
2103 }
2104
Wale Ogunwale5f986092015-12-04 15:35:38 -08002105 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002106 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2107 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002108 final ArrayList<ActivityRecord> activities = task.mActivities;
2109 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002110 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002111 }
2112 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002113 }
2114
Wale Ogunwale040b4702015-08-06 18:10:50 -07002115 /**
2116 * Moves the specified task record to the input stack id.
2117 * WARNING: This method performs an unchecked/raw move of the task and
2118 * can leave the system in an unstable state if used incorrectly.
2119 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2120 * to a stack.
2121 * @param task Task to move.
2122 * @param stackId Id of stack to move task to.
2123 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002124 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002125 * @param reason Reason the task is been moved.
2126 * @return The stack the task was moved to.
2127 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002128 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002129 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2130 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002131 final ActivityStack prevStack = task.stack;
2132 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2133 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2134 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2135 // Whenever we are moving the top activity from the front stack we want to make sure to move
2136 // the stack to the front.
2137 final boolean wasFront = isFrontStack(prevStack)
2138 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002139
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002140 final boolean resizeable = task.mResizeable;
2141 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2142 // if a docked stack is created below which will lead to the stack we are moving from and
2143 // its resizeable tasks being resized.
2144 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002145 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002146 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002147 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002148 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002149
2150 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002151 // move focus to the new stack by moving the stack to the front.
2152 stack.moveToFrontAndResumeStateIfNeeded(
2153 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002154
Wale Ogunwale040b4702015-08-06 18:10:50 -07002155 return stack;
2156 }
2157
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002158 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002159 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002160 final TaskRecord task = anyTaskForIdLocked(taskId);
2161 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002162 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002163 return;
2164 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002165
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002166 if (task.stack != null && task.stack.mStackId == stackId) {
2167 // You are already in the right stack silly...
2168 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2169 return;
2170 }
2171
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002172 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002173 final boolean mightReplaceWindow =
2174 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2175 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002176 // We are about to relaunch the activity because its configuration changed due to
2177 // being maximized, i.e. size change. The activity will first remove the old window
2178 // and then add a new one. This call will tell window manager about this, so it can
2179 // preserve the old window until the new one is drawn. This prevents having a gap
2180 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002181 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002182 // Note here we always set the replacing window first, as the flags might be needed
2183 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002184 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002185 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002186 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2187 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002188
Jorim Jaggi55387522015-11-24 18:21:10 -08002189 if (!animate) {
2190 stack.mNoAnimActivities.add(topActivity);
2191 }
2192
Chong Zhangf596cd52016-01-05 13:42:44 -08002193 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002194 // Make sure the task has the appropriate bounds/size for the stack it is in.
2195 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002196 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002197 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2198 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002199 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002200 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002201 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002202 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2203 }
2204
2205 if (mightReplaceWindow) {
2206 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2207 // window), we need to clear the replace window settings. Otherwise, we schedule a
2208 // timeout to remove the old window if the replacing window is not coming in time.
2209 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002210 }
2211
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002212 // The task might have already been running and its visibility needs to be synchronized with
2213 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002214 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002215 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002216
2217 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002218 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002219 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002220 }
2221
Wale Ogunwale079a0042015-10-24 11:44:07 -07002222 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2223 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2224 if (stack == null) {
2225 throw new IllegalArgumentException(
2226 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2227 }
2228
2229 final ActivityRecord r = stack.topRunningActivityLocked();
2230 if (r == null) {
2231 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2232 + " in stack=" + stack);
2233 return false;
2234 }
2235
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002236 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002237 Slog.w(TAG,
2238 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002239 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002240 return false;
2241 }
2242
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002243 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2244 return true;
2245 }
2246
2247 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002248 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002249 if (task.mActivities.size() == 1) {
2250 // 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 -08002251 // stack without re-parenting the activity in a different task.
2252 moveTaskToStackLocked(
2253 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002254 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002255 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2256 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002257 }
2258
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002259 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002260 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2261 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002262 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002263
2264 // The task might have already been running and its visibility needs to be synchronized with
2265 // the visibility of the stack / windows.
2266 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002267 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002268
2269 if (stackId == PINNED_STACK_ID) {
2270 mService.notifyActivityPinnedLocked();
2271 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002272 }
2273
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002274 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2275 final TaskRecord task = anyTaskForIdLocked(taskId);
2276 if (task == null) {
2277 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2278 return;
2279 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002280 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2281
2282 task.updateOverrideConfigurationForStack(stack);
2283
2284 mWindowManager.positionTaskInStack(
2285 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002286 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002287 // The task might have already been running and its visibility needs to be synchronized with
2288 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002289 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002290 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002291 }
2292
Craig Mautnerac6f8432013-07-17 13:24:59 -07002293 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002294 mTmpFindTaskResult.r = null;
2295 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002296 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002297 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2298 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002299 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2300 final ActivityStack stack = stacks.get(stackNdx);
2301 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002302 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002303 continue;
2304 }
2305 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002306 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2307 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002308 continue;
2309 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002310 stack.findTaskLocked(r, mTmpFindTaskResult);
2311 // It is possible to have task in multiple stacks with the same root affinity.
2312 // If the match we found was based on root affinity we keep on looking to see if
2313 // there is a better match in another stack. We eventually return the match based
2314 // on root affinity if we don't find a better match.
2315 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2316 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002317 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 }
2319 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002320 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2321 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 }
2323
2324 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002325 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2326 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002327 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2328 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2329 if (ar != null) {
2330 return ar;
2331 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002332 }
2333 }
2334 return null;
2335 }
2336
Craig Mautner8d341ef2013-03-26 09:03:27 -07002337 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002338 scheduleSleepTimeout();
2339 if (!mGoingToSleep.isHeld()) {
2340 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002341 if (mLaunchingActivity.isHeld()) {
2342 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2343 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002344 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002345 mLaunchingActivity.release();
2346 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002347 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002348 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002349 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002350 }
2351
2352 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002353 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002354
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002355 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002356 final long endTime = System.currentTimeMillis() + timeout;
2357 while (true) {
2358 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002359 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2360 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002361 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2362 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2363 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002364 }
2365 if (cantShutdown) {
2366 long timeRemaining = endTime - System.currentTimeMillis();
2367 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002368 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002369 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002370 } catch (InterruptedException e) {
2371 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002372 } else {
2373 Slog.w(TAG, "Activity manager shutdown timed out");
2374 timedout = true;
2375 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002376 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002377 } else {
2378 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002379 }
2380 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002381
2382 // Force checkReadyForSleep to complete.
2383 mSleepTimeout = true;
2384 checkReadyForSleepLocked();
2385
Craig Mautner8d341ef2013-03-26 09:03:27 -07002386 return timedout;
2387 }
2388
2389 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002390 removeSleepTimeouts();
2391 if (mGoingToSleep.isHeld()) {
2392 mGoingToSleep.release();
2393 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002394 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2395 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002396 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2397 final ActivityStack stack = stacks.get(stackNdx);
2398 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002399 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002400 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002401 }
Craig Mautner5314a402013-09-26 12:40:16 -07002402 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002403 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002404 mGoingToSleepActivities.clear();
2405 }
2406
2407 void activitySleptLocked(ActivityRecord r) {
2408 mGoingToSleepActivities.remove(r);
2409 checkReadyForSleepLocked();
2410 }
2411
2412 void checkReadyForSleepLocked() {
2413 if (!mService.isSleepingOrShuttingDown()) {
2414 // Do not care.
2415 return;
2416 }
2417
2418 if (!mSleepTimeout) {
2419 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002420 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2421 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002422 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2423 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2424 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425 }
2426
2427 if (mStoppingActivities.size() > 0) {
2428 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002429 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002430 + mStoppingActivities.size() + " activities");
2431 scheduleIdleLocked();
2432 dontSleep = true;
2433 }
2434
2435 if (mGoingToSleepActivities.size() > 0) {
2436 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002437 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002438 + mGoingToSleepActivities.size() + " activities");
2439 dontSleep = true;
2440 }
2441
2442 if (dontSleep) {
2443 return;
2444 }
2445 }
2446
Craig Mautnere0a38842013-12-16 16:14:02 -08002447 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2448 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002449 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2450 stacks.get(stackNdx).goToSleep();
2451 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002452 }
2453
2454 removeSleepTimeouts();
2455
2456 if (mGoingToSleep.isHeld()) {
2457 mGoingToSleep.release();
2458 }
2459 if (mService.mShuttingDown) {
2460 mService.notifyAll();
2461 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002462 }
2463
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002464 boolean reportResumedActivityLocked(ActivityRecord r) {
2465 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002466 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002467 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002468 }
2469 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002470 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002471 mWindowManager.executeAppTransition();
2472 return true;
2473 }
2474 return false;
2475 }
2476
Craig Mautner8d341ef2013-03-26 09:03:27 -07002477 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002478 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2479 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002480 int stackNdx = stacks.size() - 1;
2481 while (stackNdx >= 0) {
2482 stacks.get(stackNdx).handleAppCrashLocked(app);
2483 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002484 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002485 }
2486 }
2487
Jose Lima4b6c6692014-08-12 17:41:12 -07002488 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002489 final ActivityStack stack = r.task.stack;
2490 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002491 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2492 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002493 return false;
2494 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002495 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002496 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2497 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002498
2499 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002500 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002501 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002502 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002503 return true;
2504 }
2505
2506 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002507 if (visible && top.fullscreen) {
2508 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002509 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2510 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2511 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2512 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002513 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002514 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2515 // Only the activity set as currently visible behind should actively reset its
2516 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002517 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2518 "requestVisibleBehind: returning visible=" + visible
2519 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2520 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002521 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002522 }
2523
Jose Lima4b6c6692014-08-12 17:41:12 -07002524 stack.setVisibleBehindActivity(visible ? r : null);
2525 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002526 // Make the activity immediately above r opaque.
2527 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2528 if (next != null) {
2529 mService.convertFromTranslucent(next.appToken);
2530 }
2531 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002532 if (top.app != null && top.app.thread != null) {
2533 // Notify the top app of the change.
2534 try {
2535 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2536 } catch (RemoteException e) {
2537 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002538 }
2539 return true;
2540 }
2541
Craig Mautnerbb742462014-07-07 15:28:55 -07002542 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2543 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2544 r.mLaunchTaskBehind = false;
2545 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002546 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002547 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002548 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002549 mWindowManager.setAppVisibility(r.appToken, false);
2550 }
2551
2552 void scheduleLaunchTaskBehindComplete(IBinder token) {
2553 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2554 }
2555
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002556 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2557 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002558 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002559 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2560 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002561 final int topStackNdx = stacks.size() - 1;
2562 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2563 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002564 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002565 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002566 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002567 }
2568
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002569 void invalidateTaskLayers() {
2570 mTaskLayersChanged = true;
2571 }
2572
2573 void rankTaskLayersIfNeeded() {
2574 if (!mTaskLayersChanged) {
2575 return;
2576 }
2577 mTaskLayersChanged = false;
2578 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2579 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2580 int baseLayer = 0;
2581 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2582 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2583 }
2584 }
2585 }
2586
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002587 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2588 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2589 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2590 final int topStackNdx = stacks.size() - 1;
2591 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2592 final ActivityStack stack = stacks.get(stackNdx);
2593 stack.clearOtherAppTimeTrackers(except);
2594 }
2595 }
2596 }
2597
Craig Mautner8d341ef2013-03-26 09:03:27 -07002598 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002599 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2600 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002601 final int numStacks = stacks.size();
2602 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2603 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002604 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002605 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002606 }
2607 }
2608
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002609 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2610 // Examine all activities currently running in the process.
2611 TaskRecord firstTask = null;
2612 // Tasks is non-null only if two or more tasks are found.
2613 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002614 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2615 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002616 ActivityRecord r = app.activities.get(i);
2617 // First, if we find an activity that is in the process of being destroyed,
2618 // then we just aren't going to do anything for now; we want things to settle
2619 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002620 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002621 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002622 return;
2623 }
2624 // Don't consider any activies that are currently not in a state where they
2625 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002626 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2627 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002628 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002629 continue;
2630 }
2631 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002632 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002633 + " from " + r);
2634 if (firstTask == null) {
2635 firstTask = r.task;
2636 } else if (firstTask != r.task) {
2637 if (tasks == null) {
2638 tasks = new ArraySet<>();
2639 tasks.add(firstTask);
2640 }
2641 tasks.add(r.task);
2642 }
2643 }
2644 }
2645 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002646 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002647 return;
2648 }
2649 // If we have activities in multiple tasks that are in a position to be destroyed,
2650 // let's iterate through the tasks and release the oldest one.
2651 final int numDisplays = mActivityDisplays.size();
2652 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2653 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2654 // Step through all stacks starting from behind, to hit the oldest things first.
2655 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2656 final ActivityStack stack = stacks.get(stackNdx);
2657 // Try to release activities in this stack; if we manage to, we are done.
2658 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2659 return;
2660 }
2661 }
2662 }
2663 }
2664
Amith Yamasani37a40c22015-06-17 13:25:42 -07002665 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002666 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002667 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002668 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002669
Craig Mautner858d8a62013-04-23 17:08:34 -07002670 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 final ActivityStack stack = stacks.get(stackNdx);
2675 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002676 TaskRecord task = stack.topTask();
2677 if (task != null) {
2678 mWindowManager.moveTaskToTop(task.taskId);
2679 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002680 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002681 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002682
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002683 ActivityStack stack = getStack(restoreStackId);
2684 if (stack == null) {
2685 stack = mHomeStack;
2686 }
2687 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002689 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002690 } else {
2691 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002692 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002693 }
Craig Mautner93529a42013-10-04 15:03:13 -07002694 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002695 }
2696
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002697 /**
2698 * Add background users to send boot completed events to.
2699 * @param userId The user being started in the background
2700 * @param uss The state object for the user.
2701 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002702 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002703 mStartingBackgroundUsers.add(uss);
2704 }
2705
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002706 /** Checks whether the userid is a profile of the current user. */
2707 boolean isCurrentProfileLocked(int userId) {
2708 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002709 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002710 }
2711
Chong Zhang45c25ce2015-08-10 22:18:26 -07002712 /** Checks whether the activity should be shown for current user. */
2713 boolean okToShowLocked(ActivityRecord r) {
2714 return r != null && (isCurrentProfileLocked(r.userId)
2715 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2716 }
2717
Craig Mautnerde4ef022013-04-07 19:01:33 -07002718 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002719 ArrayList<ActivityRecord> stops = null;
2720
2721 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002722 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2723 ActivityRecord s = mStoppingActivities.get(activityNdx);
2724 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002725 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002726 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2727 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002728 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002729 if (s.finishing) {
2730 // If this activity is finishing, it is sitting on top of
2731 // everyone else but we now know it is no longer needed...
2732 // so get rid of it. Otherwise, we need to go through the
2733 // normal flow and hide it once we determine that it is
2734 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002735 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002736 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002737 }
2738 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002739 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002740 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002741 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002742 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002743 }
2744 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002745 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002746 }
2747 }
2748
2749 return stops;
2750 }
2751
Craig Mautnercf910b02013-04-23 11:23:27 -07002752 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2756 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002757 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002758 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002759 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002760 if (r == null) Slog.e(TAG,
2761 "validateTop...: null top activity, stack=" + stack);
2762 else {
2763 final ActivityRecord pausing = stack.mPausingActivity;
2764 if (pausing != null && pausing == r) Slog.e(TAG,
2765 "validateTop...: top stack has pausing activity r=" + r
2766 + " state=" + state);
2767 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2768 "validateTop...: activity in front not resumed r=" + r
2769 + " state=" + state);
2770 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002771 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002772 final ActivityRecord resumed = stack.mResumedActivity;
2773 if (resumed != null && resumed == r) Slog.e(TAG,
2774 "validateTop...: back stack has resumed activity r=" + r
2775 + " state=" + state);
2776 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2777 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002778 }
2779 }
2780 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002781 }
2782
Craig Mautnere0570202015-05-13 13:06:11 -07002783 private String lockTaskModeToString() {
2784 switch (mLockTaskModeState) {
2785 case LOCK_TASK_MODE_LOCKED:
2786 return "LOCKED";
2787 case LOCK_TASK_MODE_PINNED:
2788 return "PINNED";
2789 case LOCK_TASK_MODE_NONE:
2790 return "NONE";
2791 default: return "unknown=" + mLockTaskModeState;
2792 }
2793 }
2794
Craig Mautner27084302013-03-25 08:05:25 -07002795 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002796 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002797 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002798 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2799 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2800 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002801 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002802 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2803 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2804 if (packages.size() > 0) {
2805 pw.println(" mLockTaskPackages (userId:packages)=");
2806 for (int i = 0; i < packages.size(); ++i) {
2807 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2808 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2809 }
2810 }
2811 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002812 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002813
Craig Mautner20e72272013-04-01 13:45:53 -07002814 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002815 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002816 }
2817
Dianne Hackborn390517b2013-05-30 15:03:32 -07002818 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2819 boolean needSep, String prefix) {
2820 if (activity != null) {
2821 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2822 if (needSep) {
2823 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002824 }
2825 pw.print(prefix);
2826 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002827 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002828 }
2829 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002830 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002831 }
2832
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2834 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002835 boolean printed = false;
2836 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002837 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2838 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002839 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002840 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002841 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002842 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002843 final ActivityStack stack = stacks.get(stackNdx);
2844 StringBuilder stackHeader = new StringBuilder(128);
2845 stackHeader.append(" Stack #");
2846 stackHeader.append(stack.mStackId);
2847 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002848 stackHeader.append("\n");
2849 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2850 stackHeader.append("\n");
2851 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002852 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2853 needSep, stackHeader.toString());
2854 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2855 !dumpAll, false, dumpPackage, true,
2856 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002857
Craig Mautner4a1cb222013-12-04 16:14:06 -08002858 needSep = printed;
2859 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2860 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002861 if (pr) {
2862 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002863 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002864 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002865 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2866 " mResumedActivity: ");
2867 if (pr) {
2868 printed = true;
2869 needSep = false;
2870 }
2871 if (dumpAll) {
2872 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2873 " mLastPausedActivity: ");
2874 if (pr) {
2875 printed = true;
2876 needSep = true;
2877 }
2878 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2879 needSep, " mLastNoHistoryActivity: ");
2880 }
2881 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 }
2883 }
2884
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002885 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2886 false, dumpPackage, true, " Activities waiting to finish:", null);
2887 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2888 false, dumpPackage, true, " Activities waiting to stop:", null);
2889 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2890 false, dumpPackage, true, " Activities waiting for another to become visible:",
2891 null);
2892 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2893 false, dumpPackage, true, " Activities waiting to sleep:", null);
2894 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2895 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002896
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002897 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002898 }
2899
Dianne Hackborn390517b2013-05-30 15:03:32 -07002900 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002901 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002902 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002903 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002904 String innerPrefix = null;
2905 String[] args = null;
2906 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 for (int i=list.size()-1; i>=0; i--) {
2908 final ActivityRecord r = list.get(i);
2909 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2910 continue;
2911 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002912 if (innerPrefix == null) {
2913 innerPrefix = prefix + " ";
2914 args = new String[0];
2915 }
2916 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917 final boolean full = !brief && (complete || !r.isInHistory());
2918 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002919 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002920 needNL = false;
2921 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002922 if (header1 != null) {
2923 pw.println(header1);
2924 header1 = null;
2925 }
2926 if (header2 != null) {
2927 pw.println(header2);
2928 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002929 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002930 if (lastTask != r.task) {
2931 lastTask = r.task;
2932 pw.print(prefix);
2933 pw.print(full ? "* " : " ");
2934 pw.println(lastTask);
2935 if (full) {
2936 lastTask.dump(pw, prefix + " ");
2937 } else if (complete) {
2938 // Complete + brief == give a summary. Isn't that obvious?!?
2939 if (lastTask.intent != null) {
2940 pw.print(prefix); pw.print(" ");
2941 pw.println(lastTask.intent.toInsecureStringWithClip());
2942 }
2943 }
2944 }
2945 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2946 pw.print(" #"); pw.print(i); pw.print(": ");
2947 pw.println(r);
2948 if (full) {
2949 r.dump(pw, innerPrefix);
2950 } else if (complete) {
2951 // Complete + brief == give a summary. Isn't that obvious?!?
2952 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2953 if (r.app != null) {
2954 pw.print(innerPrefix); pw.println(r.app);
2955 }
2956 }
2957 if (client && r.app != null && r.app.thread != null) {
2958 // flush anything that is already in the PrintWriter since the thread is going
2959 // to write to the file descriptor directly
2960 pw.flush();
2961 try {
2962 TransferPipe tp = new TransferPipe();
2963 try {
2964 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2965 r.appToken, innerPrefix, args);
2966 // Short timeout, since blocking here can
2967 // deadlock with the application.
2968 tp.go(fd, 2000);
2969 } finally {
2970 tp.kill();
2971 }
2972 } catch (IOException e) {
2973 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2974 } catch (RemoteException e) {
2975 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2976 }
2977 needNL = true;
2978 }
2979 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002980 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002981 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002982
Craig Mautnerf3333272013-04-22 10:55:53 -07002983 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002984 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2985 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002986 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2987 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002988 }
2989
2990 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002991 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002992 }
2993
2994 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002995 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2996 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002997 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2998 }
2999
Craig Mautner05d29032013-05-03 13:40:13 -07003000 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003001 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3002 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3003 }
Craig Mautner05d29032013-05-03 13:40:13 -07003004 }
3005
Craig Mautner0eea92c2013-05-16 13:35:39 -07003006 void removeSleepTimeouts() {
3007 mSleepTimeout = false;
3008 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3009 }
3010
3011 final void scheduleSleepTimeout() {
3012 removeSleepTimeouts();
3013 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3014 }
3015
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 @Override
3017 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003018 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3020 }
3021
3022 @Override
3023 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003024 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3026 }
3027
3028 @Override
3029 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003030 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003031 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3032 }
3033
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003034 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003035 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003037 newDisplay = mActivityDisplays.get(displayId) == null;
3038 if (newDisplay) {
3039 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003040 if (activityDisplay.mDisplay == null) {
3041 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3042 return;
3043 }
Craig Mautner4504de52013-12-20 09:06:56 -08003044 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003045 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003046 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003047 }
Craig Mautner4504de52013-12-20 09:06:56 -08003048 if (newDisplay) {
3049 mWindowManager.onDisplayAdded(displayId);
3050 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003051 }
3052
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003053 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3054 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3055 return;
3056 }
3057 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3058 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3059 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3060 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3061 }
3062
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003063 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003065 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3066 if (activityDisplay != null) {
3067 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003069 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003070 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003071 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003072 }
3073 }
3074 mWindowManager.onDisplayRemoved(displayId);
3075 }
3076
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003077 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003078 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003079 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3080 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003081 // TODO: Update the bounds.
3082 }
3083 }
3084 mWindowManager.onDisplayChanged(displayId);
3085 }
3086
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003087 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 StackInfo info = new StackInfo();
3089 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3090 info.displayId = Display.DEFAULT_DISPLAY;
3091 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003092 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093
3094 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3095 final int numTasks = tasks.size();
3096 int[] taskIds = new int[numTasks];
3097 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003098 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003099 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 for (int i = 0; i < numTasks; ++i) {
3101 final TaskRecord task = tasks.get(i);
3102 taskIds[i] = task.taskId;
3103 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3104 : task.realActivity != null ? task.realActivity.flattenToString()
3105 : task.getTopActivity() != null ? task.getTopActivity().packageName
3106 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003107 taskBounds[i] = new Rect();
3108 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003109 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003110 }
3111 info.taskIds = taskIds;
3112 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003113 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003114 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 return info;
3116 }
3117
3118 StackInfo getStackInfoLocked(int stackId) {
3119 ActivityStack stack = getStack(stackId);
3120 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003121 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003122 }
3123 return null;
3124 }
3125
3126 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003127 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003128 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3129 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003130 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003131 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003132 }
3133 }
3134 return list;
3135 }
3136
Craig Mautner15df08a2015-04-01 12:17:18 -07003137 TaskRecord getLockedTaskLocked() {
3138 final int top = mLockTaskModeTasks.size() - 1;
3139 if (top >= 0) {
3140 return mLockTaskModeTasks.get(top);
3141 }
3142 return null;
3143 }
3144
3145 boolean isLockedTask(TaskRecord task) {
3146 return mLockTaskModeTasks.contains(task);
3147 }
3148
3149 boolean isLastLockedTask(TaskRecord task) {
3150 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3151 }
3152
3153 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003154 if (!mLockTaskModeTasks.remove(task)) {
3155 return;
3156 }
3157 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3158 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003159 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003160 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3161 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003162 final Message lockTaskMsg = Message.obtain();
3163 lockTaskMsg.arg1 = task.userId;
3164 lockTaskMsg.what = LOCK_TASK_END_MSG;
3165 mHandler.sendMessage(lockTaskMsg);
3166 }
3167 }
3168
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003169 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003170 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003171 }
3172
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003173 void showLockTaskToast() {
3174 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003175 }
3176
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003177 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3178 if (mLockTaskModeTasks.contains(task)) {
3179 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3180 }
3181 }
3182
Craig Mautner432f64e2015-05-20 14:59:57 -07003183 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3184 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003185 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003186 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003187 final TaskRecord lockedTask = getLockedTaskLocked();
3188 if (lockedTask != null) {
3189 removeLockedTaskLocked(lockedTask);
3190 if (!mLockTaskModeTasks.isEmpty()) {
3191 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003192 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3193 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003194 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003195 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003196 return;
3197 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003198 }
Craig Mautnere0570202015-05-13 13:06:11 -07003199 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3200 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003201 return;
3202 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003203
3204 // Should have already been checked, but do it again.
3205 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003206 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3207 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003208 return;
3209 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003210 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003211 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003212 return;
3213 }
3214
3215 if (mLockTaskModeTasks.isEmpty()) {
3216 // First locktask.
3217 final Message lockTaskMsg = Message.obtain();
3218 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3219 lockTaskMsg.arg1 = task.userId;
3220 lockTaskMsg.what = LOCK_TASK_START_MSG;
3221 lockTaskMsg.arg2 = lockTaskModeState;
3222 mHandler.sendMessage(lockTaskMsg);
3223 }
3224 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003225 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3226 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003227 mLockTaskModeTasks.remove(task);
3228 mLockTaskModeTasks.add(task);
3229
3230 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003231 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003232 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003233
3234 if (andResume) {
3235 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003236 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003237 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003238 }
3239
3240 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003241 return isLockTaskModeViolation(task, false);
3242 }
3243
3244 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3245 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003246 return false;
3247 }
3248 final int lockTaskAuth = task.mLockTaskAuth;
3249 switch (lockTaskAuth) {
3250 case LOCK_TASK_AUTH_DONT_LOCK:
3251 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003252 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003253 case LOCK_TASK_AUTH_LAUNCHABLE:
3254 case LOCK_TASK_AUTH_WHITELISTED:
3255 return false;
3256 case LOCK_TASK_AUTH_PINNABLE:
3257 // Pinnable tasks can't be launched on top of locktask tasks.
3258 return !mLockTaskModeTasks.isEmpty();
3259 default:
3260 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3261 return true;
3262 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003263 }
3264
Craig Mautner15df08a2015-04-01 12:17:18 -07003265 void onLockTaskPackagesUpdatedLocked() {
3266 boolean didSomething = false;
3267 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3268 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003269 final boolean wasWhitelisted =
3270 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3271 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003272 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003273 final boolean isWhitelisted =
3274 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3275 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3276 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003277 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003278 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3279 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003280 removeLockedTaskLocked(lockedTask);
3281 lockedTask.performClearTaskLocked();
3282 didSomething = true;
3283 }
3284 }
3285 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3286 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3287 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3288 final ActivityStack stack = stacks.get(stackNdx);
3289 stack.onLockTaskPackagesUpdatedLocked();
3290 }
3291 }
Craig Mautnere0570202015-05-13 13:06:11 -07003292 final ActivityRecord r = topRunningActivityLocked();
3293 final TaskRecord task = r != null ? r.task : null;
3294 if (mLockTaskModeTasks.isEmpty() && task != null
3295 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3296 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003297 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3298 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3299 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3300 false);
3301 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003302 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003303 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003304 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003305 }
3306 }
3307
Benjamin Franz43261142015-02-11 15:59:44 +00003308 int getLockTaskModeState() {
3309 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003310 }
3311
Jorim Jaggife89d122015-12-22 16:28:44 +01003312 void activityRelaunchedLocked(IBinder token) {
3313 mWindowManager.notifyAppRelaunchingFinished(token);
3314 }
3315
3316 void activityRelaunchingLocked(ActivityRecord r) {
3317 mWindowManager.notifyAppRelaunching(r.appToken);
3318 }
3319
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003320 void logStackState() {
3321 mActivityMetricsLogger.logWindowState();
3322 }
3323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003324 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003325
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003326 public ActivityStackSupervisorHandler(Looper looper) {
3327 super(looper);
3328 }
3329
Craig Mautnerf3333272013-04-22 10:55:53 -07003330 void activityIdleInternal(ActivityRecord r) {
3331 synchronized (mService) {
3332 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3333 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003334 }
3335
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003336 @Override
3337 public void handleMessage(Message msg) {
3338 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003339 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003340 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3341 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003342 if (mService.mDidDexOpt) {
3343 mService.mDidDexOpt = false;
3344 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3345 nmsg.obj = msg.obj;
3346 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3347 return;
3348 }
3349 // We don't at this point know if the activity is fullscreen,
3350 // so we need to be conservative and assume it isn't.
3351 activityIdleInternal((ActivityRecord)msg.obj);
3352 } break;
3353 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003354 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003355 activityIdleInternal((ActivityRecord)msg.obj);
3356 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003357 case RESUME_TOP_ACTIVITY_MSG: {
3358 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003359 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003360 }
3361 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003362 case SLEEP_TIMEOUT_MSG: {
3363 synchronized (mService) {
3364 if (mService.isSleepingOrShuttingDown()) {
3365 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3366 mSleepTimeout = true;
3367 checkReadyForSleepLocked();
3368 }
3369 }
3370 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003371 case LAUNCH_TIMEOUT_MSG: {
3372 if (mService.mDidDexOpt) {
3373 mService.mDidDexOpt = false;
3374 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3375 return;
3376 }
3377 synchronized (mService) {
3378 if (mLaunchingActivity.isHeld()) {
3379 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3380 if (VALIDATE_WAKE_LOCK_CALLER
3381 && Binder.getCallingUid() != Process.myUid()) {
3382 throw new IllegalStateException("Calling must be system uid");
3383 }
3384 mLaunchingActivity.release();
3385 }
3386 }
3387 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003389 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 } break;
3391 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003392 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 } break;
3394 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003395 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003397 case CONTAINER_CALLBACK_VISIBILITY: {
3398 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003399 final IActivityContainerCallback callback = container.mCallback;
3400 if (callback != null) {
3401 try {
3402 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3403 } catch (RemoteException e) {
3404 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003405 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003406 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003407 case LOCK_TASK_START_MSG: {
3408 // When lock task starts, we disable the status bars.
3409 try {
Jason Monk62515be2014-05-21 16:06:19 -04003410 if (mLockTaskNotify == null) {
3411 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003412 }
Jason Monk62515be2014-05-21 16:06:19 -04003413 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003414 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003415 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003416 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003417 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003418 flags = StatusBarManager.DISABLE_MASK
3419 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003420 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003421 flags = StatusBarManager.DISABLE_MASK
3422 & (~StatusBarManager.DISABLE_BACK)
3423 & (~StatusBarManager.DISABLE_HOME)
3424 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003425 }
3426 getStatusBarService().disable(flags, mToken,
3427 mService.mContext.getPackageName());
3428 }
3429 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003430 if (getDevicePolicyManager() != null) {
3431 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003432 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003433 }
justinzhang5286d3f2014-05-12 17:06:01 -04003434 } catch (RemoteException ex) {
3435 throw new RuntimeException(ex);
3436 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003437 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003438 case LOCK_TASK_END_MSG: {
3439 // When lock task ends, we enable the status bars.
3440 try {
Jason Monk62515be2014-05-21 16:06:19 -04003441 if (getStatusBarService() != null) {
3442 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3443 mService.mContext.getPackageName());
3444 }
3445 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003446 if (getDevicePolicyManager() != null) {
3447 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3448 msg.arg1);
3449 }
Jason Monk62515be2014-05-21 16:06:19 -04003450 if (mLockTaskNotify == null) {
3451 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3452 }
3453 mLockTaskNotify.show(false);
3454 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003455 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003456 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003457 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003458 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003459 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003460 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003461 new LockPatternUtils(mService.mContext)
3462 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003463 }
3464 } catch (SettingNotFoundException e) {
3465 // No setting, don't lock.
3466 }
justinzhang5286d3f2014-05-12 17:06:01 -04003467 } catch (RemoteException ex) {
3468 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003469 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003470 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003471 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003472 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003473 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3474 if (mLockTaskNotify == null) {
3475 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3476 }
3477 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3478 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003479 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3480 final ActivityContainer container = (ActivityContainer) msg.obj;
3481 final IActivityContainerCallback callback = container.mCallback;
3482 if (callback != null) {
3483 try {
3484 callback.onAllActivitiesComplete(container.asBinder());
3485 } catch (RemoteException e) {
3486 }
3487 }
3488 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003489 case LAUNCH_TASK_BEHIND_COMPLETE: {
3490 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003491 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003492 if (r != null) {
3493 handleLaunchTaskBehindCompleteLocked(r);
3494 }
3495 }
3496 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003497
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003498 }
3499 }
3500 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003501
Ying Wangb081a592014-04-22 15:20:16 -07003502 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003503 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3504 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003506 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003507 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003508 ActivityRecord mParentActivity = null;
3509 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003510
Craig Mautnere3a00d72014-04-16 08:31:19 -07003511 boolean mVisible = true;
3512
Craig Mautner4a1cb222013-12-04 16:14:06 -08003513 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003514 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003515
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003516 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3517 final static int CONTAINER_STATE_NO_SURFACE = 1;
3518 final static int CONTAINER_STATE_FINISHING = 2;
3519 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3520
3521 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 synchronized (mService) {
3523 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003524 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003525 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003526 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003528 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003529
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003530 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003531 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003532 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003533 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003534 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003535 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 }
3537
3538 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003539 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003541 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3542 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 return;
3544 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003545 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003546 }
3547 }
3548
3549 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003550 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003551 synchronized (mService) {
3552 if (mActivityDisplay != null) {
3553 return mActivityDisplay.mDisplayId;
3554 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 }
3556 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 }
3558
Jeff Brownca9bc702014-02-11 14:32:56 -08003559 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003560 public int getStackId() {
3561 synchronized (mService) {
3562 return mStackId;
3563 }
3564 }
3565
3566 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003567 public boolean injectEvent(InputEvent event) {
3568 final long origId = Binder.clearCallingIdentity();
3569 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003570 synchronized (mService) {
3571 if (mActivityDisplay != null) {
3572 return mInputManagerInternal.injectInputEvent(event,
3573 mActivityDisplay.mDisplayId,
3574 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3575 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003576 }
3577 return false;
3578 } finally {
3579 Binder.restoreCallingIdentity(origId);
3580 }
3581 }
3582
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003583 @Override
3584 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003585 synchronized (mService) {
3586 if (mContainerState == CONTAINER_STATE_FINISHING) {
3587 return;
3588 }
3589 mContainerState = CONTAINER_STATE_FINISHING;
3590
Craig Mautnerd163e752014-06-13 17:18:47 -07003591 long origId = Binder.clearCallingIdentity();
3592 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003593 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003594 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003595 } finally {
3596 Binder.restoreCallingIdentity(origId);
3597 }
3598 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003599 }
3600
Craig Mautner60257702014-09-17 15:02:33 -07003601 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003602 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003603 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003604 if (mActivityDisplay != null) {
3605 mActivityDisplay.detachActivitiesLocked(mStack);
3606 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003607 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003608 }
3609 }
3610
3611 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003612 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003613 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003614 }
3615
3616 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003617 public final int startActivityIntentSender(IIntentSender intentSender)
3618 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003619 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3620
3621 if (!(intentSender instanceof PendingIntentRecord)) {
3622 throw new IllegalArgumentException("Bad PendingIntent object");
3623 }
3624
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003625 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003626 Binder.getCallingUid(), mCurrentUser, false,
3627 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003628
3629 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3630 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3631 pendingIntent.key.requestResolvedType);
3632
3633 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3634 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3635 }
3636
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003637 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003638 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003639 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003640 throw new SecurityException(
3641 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3642 }
3643 }
3644
Craig Mautnerdf88d732014-01-27 09:21:32 -08003645 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003646 public IBinder asBinder() {
3647 return this;
3648 }
3649
Craig Mautner4504de52013-12-20 09:06:56 -08003650 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003651 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003652 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003653 }
3654
Craig Mautner4a1cb222013-12-04 16:14:06 -08003655 ActivityStackSupervisor getOuter() {
3656 return ActivityStackSupervisor.this;
3657 }
3658
Craig Mautnerd163e752014-06-13 17:18:47 -07003659 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003660 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003661 }
3662
Craig Mautner6985bad2014-04-21 15:22:06 -07003663 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003664 void setVisible(boolean visible) {
3665 if (mVisible != visible) {
3666 mVisible = visible;
3667 if (mCallback != null) {
3668 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3669 0 /* unused */, this).sendToTarget();
3670 }
3671 }
3672 }
3673
Craig Mautner6985bad2014-04-21 15:22:06 -07003674 void setDrawn() {
3675 }
3676
Craig Mautner1b4bf852014-05-26 15:06:32 -07003677 // You can always start a new task on a regular ActivityStack.
3678 boolean isEligibleForNewTasks() {
3679 return true;
3680 }
3681
Craig Mautnerd163e752014-06-13 17:18:47 -07003682 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003683 detachLocked();
3684 deleteActivityContainer(this);
3685 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003686 }
3687
Craig Mautner34b73df2014-01-12 21:11:08 -08003688 @Override
3689 public String toString() {
3690 return mIdString + (mActivityDisplay == null ? "N" : "A");
3691 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003692 }
3693
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003694 private class VirtualActivityContainer extends ActivityContainer {
3695 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003696 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003697
3698 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3699 super(getNextStackId());
3700 mParentActivity = parent;
3701 mCallback = callback;
3702 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003703 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003704 }
3705
3706 @Override
3707 public void setSurface(Surface surface, int width, int height, int density) {
3708 super.setSurface(surface, width, height, density);
3709
3710 synchronized (mService) {
3711 final long origId = Binder.clearCallingIdentity();
3712 try {
3713 setSurfaceLocked(surface, width, height, density);
3714 } finally {
3715 Binder.restoreCallingIdentity(origId);
3716 }
3717 }
3718 }
3719
3720 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3721 if (mContainerState == CONTAINER_STATE_FINISHING) {
3722 return;
3723 }
3724 VirtualActivityDisplay virtualActivityDisplay =
3725 (VirtualActivityDisplay) mActivityDisplay;
3726 if (virtualActivityDisplay == null) {
3727 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003728 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003729 mActivityDisplay = virtualActivityDisplay;
3730 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003731 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003732 }
3733
3734 if (mSurface != null) {
3735 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003736 }
3737
Craig Mautner6985bad2014-04-21 15:22:06 -07003738 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003739 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003740 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003741 } else {
3742 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003743 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003744 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003745 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003746 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003747 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003748
Craig Mautnerd163e752014-06-13 17:18:47 -07003749 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003750
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003751 if (DEBUG_STACK) Slog.d(TAG_STACK,
3752 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003753 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003754
Craig Mautner6985bad2014-04-21 15:22:06 -07003755 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003756 boolean isAttachedLocked() {
3757 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003758 }
3759
3760 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003761 void setDrawn() {
3762 synchronized (mService) {
3763 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003764 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003765 }
3766 }
3767
Craig Mautner1b4bf852014-05-26 15:06:32 -07003768 // Never start a new task on an ActivityView if it isn't explicitly specified.
3769 @Override
3770 boolean isEligibleForNewTasks() {
3771 return false;
3772 }
3773
Craig Mautnerd163e752014-06-13 17:18:47 -07003774 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003775 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003776 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3777 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3778 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3779 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3780 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003781 }
3782 }
3783
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3785 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003786 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003788 int mDisplayId;
3789 Display mDisplay;
3790 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003791
Craig Mautner4a1cb222013-12-04 16:14:06 -08003792 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3793 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003794 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795
Jose Lima4b6c6692014-08-12 17:41:12 -07003796 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003797
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003798 ActivityDisplay() {
3799 }
Craig Mautner4504de52013-12-20 09:06:56 -08003800
Craig Mautner1a70a162014-09-13 12:09:31 -07003801 // After instantiation, check that mDisplay is not null before using this. The alternative
3802 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003803 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003804 final Display display = mDisplayManager.getDisplay(displayId);
3805 if (display == null) {
3806 return;
3807 }
3808 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003809 }
3810
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003811 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003812 mDisplay = display;
3813 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003814 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003815 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003817 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003818 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003819 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3820 + " onTop=" + onTop);
3821 if (onTop) {
3822 mStacks.add(stack);
3823 } else {
3824 mStacks.add(0, stack);
3825 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 }
3827
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003829 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003830 + " from displayId=" + mDisplayId);
3831 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 }
3833
Jose Lima4b6c6692014-08-12 17:41:12 -07003834 void setVisibleBehindActivity(ActivityRecord r) {
3835 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003836 }
3837
Jose Lima4b6c6692014-08-12 17:41:12 -07003838 boolean hasVisibleBehindActivity() {
3839 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003840 }
3841
Craig Mautner34b73df2014-01-12 21:11:08 -08003842 @Override
3843 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003844 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3845 }
3846 }
3847
3848 class VirtualActivityDisplay extends ActivityDisplay {
3849 VirtualDisplay mVirtualDisplay;
3850
Craig Mautner6985bad2014-04-21 15:22:06 -07003851 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003852 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003853 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3854 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3855 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3856 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003857
3858 init(mVirtualDisplay.getDisplay());
3859
3860 mWindowManager.handleDisplayAdded(mDisplayId);
3861 }
3862
3863 void setSurface(Surface surface) {
3864 if (mVirtualDisplay != null) {
3865 mVirtualDisplay.setSurface(surface);
3866 }
3867 }
3868
3869 @Override
3870 void detachActivitiesLocked(ActivityStack stack) {
3871 super.detachActivitiesLocked(stack);
3872 if (mVirtualDisplay != null) {
3873 mVirtualDisplay.release();
3874 mVirtualDisplay = null;
3875 }
3876 }
3877
3878 @Override
3879 public String toString() {
3880 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003881 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003882 }
Jose Lima58e66d62014-05-27 20:00:27 -07003883
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003884 /**
3885 * Adjust bounds to stay within stack bounds.
3886 *
3887 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3888 * that keep them unchanged, but be contained within the stack bounds.
3889 *
3890 * @param bounds Bounds to be adjusted.
3891 * @param stackBounds Bounds within which the other bounds should remain.
3892 */
3893 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3894 if (stackBounds == null || stackBounds.contains(bounds)) {
3895 return;
3896 }
3897
3898 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3899 final int maxRight = stackBounds.right
3900 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3901 int horizontalDiff = stackBounds.left - bounds.left;
3902 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3903 || (bounds.left + horizontalDiff >= maxRight)) {
3904 horizontalDiff = maxRight - bounds.left;
3905 }
3906 bounds.left += horizontalDiff;
3907 bounds.right += horizontalDiff;
3908 }
3909
3910 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3911 final int maxBottom = stackBounds.bottom
3912 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3913 int verticalDiff = stackBounds.top - bounds.top;
3914 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3915 || (bounds.top + verticalDiff >= maxBottom)) {
3916 verticalDiff = maxBottom - bounds.top;
3917 }
3918 bounds.top += verticalDiff;
3919 bounds.bottom += verticalDiff;
3920 }
3921 }
3922
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003923 ActivityStack findStackBehind(ActivityStack stack) {
3924 // TODO(multi-display): We are only looking for stacks on the default display.
3925 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3926 if (display == null) {
3927 return null;
3928 }
3929 final ArrayList<ActivityStack> stacks = display.mStacks;
3930 for (int i = stacks.size() - 1; i >= 0; i--) {
3931 if (stacks.get(i) == stack && i > 0) {
3932 return stacks.get(i - 1);
3933 }
3934 }
3935 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3936 + " in=" + stacks);
3937 }
Craig Mautner27084302013-03-25 08:05:25 -07003938}