blob: b8803bd787db38ee197d8189ca67f1d09bcd1170 [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 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100402 * Used to keep track whether app visibilities got changed since the last pause. Useful to
403 * determine whether to invoke the task stack change listener after pausing.
404 */
405 boolean mAppVisibilitiesChangedSinceLastPause;
406
407 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700408 * Description of a request to start a new activity, which has been held
409 * due to app switches being disabled.
410 */
411 static class PendingActivityLaunch {
412 final ActivityRecord r;
413 final ActivityRecord sourceRecord;
414 final int startFlags;
415 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700416 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700417
418 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700419 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700420 r = _r;
421 sourceRecord = _sourceRecord;
422 startFlags = _startFlags;
423 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700424 callerApp = _callerApp;
425 }
426
427 void sendErrorResult(String message) {
428 try {
429 if (callerApp.thread != null) {
430 callerApp.thread.scheduleCrash(message);
431 }
432 } catch (RemoteException e) {
433 Slog.e(TAG, "Exception scheduling crash of failed "
434 + "activity launcher sourceRecord=" + sourceRecord, e);
435 }
Craig Mautneree36c772014-07-16 14:56:05 -0700436 }
437 }
438
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800439 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700440 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800441 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700442 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800443 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800444 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700445 }
446
447 /**
448 * At the time when the constructor runs, the power manager has not yet been
449 * initialized. So we initialize our wakelocks afterwards.
450 */
451 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800452 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700453 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700454 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700455 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700456 }
457
justinzhang5286d3f2014-05-12 17:06:01 -0400458 // This function returns a IStatusBarService. The value is from ServiceManager.
459 // getService and is cached.
460 private IStatusBarService getStatusBarService() {
461 synchronized (mService) {
462 if (mStatusBarService == null) {
463 mStatusBarService = IStatusBarService.Stub.asInterface(
464 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
465 if (mStatusBarService == null) {
466 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
467 }
468 }
469 return mStatusBarService;
470 }
471 }
472
Jason Monk35c62a42014-06-17 10:24:47 -0400473 private IDevicePolicyManager getDevicePolicyManager() {
474 synchronized (mService) {
475 if (mDevicePolicyManager == null) {
476 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
477 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
478 if (mDevicePolicyManager == null) {
479 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
480 }
481 }
482 return mDevicePolicyManager;
483 }
484 }
485
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700486 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800487 synchronized (mService) {
488 mWindowManager = wm;
489
490 mDisplayManager =
491 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
492 mDisplayManager.registerDisplayListener(this, null);
493
494 Display[] displays = mDisplayManager.getDisplays();
495 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
496 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700498 if (activityDisplay.mDisplay == null) {
499 throw new IllegalStateException("Default Display does not exist");
500 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800501 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700502 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800503 }
504
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800505 mHomeStack = mFocusedStack = mLastFocusedStack =
506 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800507
508 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 }
Craig Mautner27084302013-03-25 08:05:25 -0700510 }
511
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200512 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700513 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200514 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700515 }
516
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700517 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800518 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700519 }
520
Craig Mautnerde4ef022013-04-07 19:01:33 -0700521 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700523 }
524
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700525 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700526 if (stack == null) {
527 return false;
528 }
529
Craig Mautnerdf88d732014-01-27 09:21:32 -0800530 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
531 if (parent != null) {
532 stack = parent.task.stack;
533 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800534 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700535 }
536
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700537 /** The top most stack. */
538 boolean isFrontStack(ActivityStack stack) {
539 if (stack == null) {
540 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800541 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700542
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700543 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
544 if (parent != null) {
545 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800546 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700547 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
548 }
549
Wale Ogunwaled046a012015-12-24 13:05:59 -0800550 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800551 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
552 if (!focusCandidate.isFocusable()) {
553 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
554 focusCandidate = focusCandidate.getNextFocusableStackLocked();
555 }
556
557 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800558 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800559 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800560
Wale Ogunwaled046a012015-12-24 13:05:59 -0800561 EventLogTags.writeAmFocusedStack(
562 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
563 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
564 }
565
566 final ActivityRecord r = topRunningActivityLocked();
567 if (mService.mFocusedActivity != r) {
568 // The focus activity should always be the top activity in the focused stack.
569 // There will be chaos and anarchy if it isn't...
570 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
571 }
Craig Mautnerde313752015-01-22 14:28:03 -0800572
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800573 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800574 if (r != null && r.idle) {
575 checkFinishBootingLocked();
576 }
577 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700578 }
579
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700580 void moveHomeStackToFront(String reason) {
581 mHomeStack.moveToFront(reason);
582 }
583
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700584 /** Returns true if the focus activity was adjusted to the home stack top activity. */
585 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700586 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
587 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700588 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700589 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700590
Craig Mautner84984fa2014-06-19 11:19:20 -0700591 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700592
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700593 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700594 if (top == null) {
595 return false;
596 }
597 mService.setFocusedActivityLocked(top, reason);
598 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700599 }
600
Craig Mautner299f9602015-01-26 09:47:33 -0800601 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700602 if (!mService.mBooting && !mService.mBooted) {
603 // Not ready yet!
604 return false;
605 }
606
Craig Mautner84984fa2014-06-19 11:19:20 -0700607 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
608 mWindowManager.showRecentApps();
609 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700610 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700611
Craig Mautner84984fa2014-06-19 11:19:20 -0700612 if (prev != null) {
613 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
614 }
615
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700616 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
617 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800618 final String myReason = reason + " resumeHomeStackTask";
619
Mark Lua56ea122015-10-08 13:31:01 +0800620 // Only resume home activity if isn't finishing.
621 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800622 mService.setFocusedActivityLocked(r, myReason);
623 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700624 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800625 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700626 }
627
Craig Mautner8d341ef2013-03-26 09:03:27 -0700628 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700629 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700630 }
631
632 /**
633 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
634 * @param id Id of the task we would like returned.
635 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
636 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700637 * @param stackId The stack to restore the task to (default launch stack will be used if
638 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700639 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700640 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800643 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
645 ActivityStack stack = stacks.get(stackNdx);
646 TaskRecord task = stack.taskForIdLocked(id);
647 if (task != null) {
648 return task;
649 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700650 }
651 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800652
653 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700654 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800655 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800656 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700657 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800658 return null;
659 }
660
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700661 if (!restoreFromRecents) {
662 return task;
663 }
664
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700665 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
667 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800668 return null;
669 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700670 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800671 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700672 }
673
Craig Mautner6170f732013-04-02 13:05:23 -0700674 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800675 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800676 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800677 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
679 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
680 if (r != null) {
681 return r;
682 }
Craig Mautner6170f732013-04-02 13:05:23 -0700683 }
684 }
685 return null;
686 }
687
Craig Mautneref73ee12014-04-23 11:45:37 -0700688 void setNextTaskId(int taskId) {
689 if (taskId > mCurTaskId) {
690 mCurTaskId = taskId;
691 }
692 }
693
Craig Mautner8d341ef2013-03-26 09:03:27 -0700694 int getNextTaskId() {
695 do {
696 mCurTaskId++;
697 if (mCurTaskId <= 0) {
698 mCurTaskId = 1;
699 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700700 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700701 return mCurTaskId;
702 }
703
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800705 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700706 if (stack == null) {
707 return null;
708 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700709 ActivityRecord resumedActivity = stack.mResumedActivity;
710 if (resumedActivity == null || resumedActivity.app == null) {
711 resumedActivity = stack.mPausingActivity;
712 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700713 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715 }
716 return resumedActivity;
717 }
718
Dianne Hackbornff072722014-09-24 10:56:28 -0700719 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700720 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700726 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 continue;
728 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700729 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 if (hr != null) {
731 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
732 && processName.equals(hr.processName)) {
733 try {
George Mount2c92c972014-03-20 09:38:23 -0700734 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 didSomething = true;
736 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700737 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 Slog.w(TAG, "Exception in new application when starting activity "
739 + hr.intent.getComponent().flattenToShortString(), e);
740 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700741 }
Craig Mautner20e72272013-04-01 13:45:53 -0700742 }
Craig Mautner20e72272013-04-01 13:45:53 -0700743 }
744 }
745 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700746 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700747 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700748 }
Craig Mautner20e72272013-04-01 13:45:53 -0700749 return didSomething;
750 }
751
752 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
754 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
756 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700757 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 continue;
759 }
760 final ActivityRecord resumedActivity = stack.mResumedActivity;
761 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700762 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800763 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 return false;
765 }
Craig Mautner20e72272013-04-01 13:45:53 -0700766 }
767 }
768 return true;
769 }
770
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800772 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
773 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
775 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700776 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700778 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 return false;
780 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700781 }
782 }
783 }
784 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700785 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
787 mLastFocusedStack + " to=" + mFocusedStack);
788 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700789 return true;
790 }
791
792 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800793 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800794 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
795 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
797 final ActivityStack stack = stacks.get(stackNdx);
798 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800799 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700800 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800801 return false;
802 }
803 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700805 }
806 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800807 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700808 }
809
Craig Mautner2acc3892013-09-23 10:28:14 -0700810 /**
811 * Pause all activities in either all of the stacks or just the back stacks.
812 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700813 * @return true if any activity was paused as a result of this call.
814 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700815 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700816 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800817 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
818 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800819 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
820 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700821 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700822 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700824 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
825 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700827 }
828 }
829 return someActivityPaused;
830 }
831
Craig Mautnerde4ef022013-04-07 19:01:33 -0700832 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700833 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
835 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
837 final ActivityStack stack = stacks.get(stackNdx);
838 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700839 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700841 Slog.d(TAG_STATES,
842 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 pausing = false;
844 } else {
845 return false;
846 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700847 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700848 }
849 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700850 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700851 }
852
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700853 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
854 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500855 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800856 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
857 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
859 final ActivityStack stack = stacks.get(stackNdx);
860 if (stack.mResumedActivity != null &&
861 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700862 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800863 }
864 }
865 }
866 }
867
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700868 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700869 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700870 }
871
872 void sendWaitingVisibleReportLocked(ActivityRecord r) {
873 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700874 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700875 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700876 if (w.who == null) {
877 changed = true;
878 w.timeout = false;
879 if (r != null) {
880 w.who = new ComponentName(r.info.packageName, r.info.name);
881 }
882 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
883 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700884 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700885 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700886 if (changed) {
887 mService.notifyAll();
888 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700889 }
890
891 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
892 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700893 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700895 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700896 if (w.who == null) {
897 changed = true;
898 w.timeout = timeout;
899 if (r != null) {
900 w.who = new ComponentName(r.info.packageName, r.info.name);
901 }
902 w.thisTime = thisTime;
903 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700904 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700905 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700906 if (changed) {
907 mService.notifyAll();
908 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700909 }
910
Craig Mautner29219d92013-04-16 20:19:12 -0700911 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800912 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700913 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700914 if (r != null) {
915 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700916 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700917
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800919 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
921 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800922 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700923 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700924 if (r != null) {
925 return r;
926 }
927 }
928 }
929 return null;
930 }
931
Dianne Hackborn09233282014-04-30 11:33:59 -0700932 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700933 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
935 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800936 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800937 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800938 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
940 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700941 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700943 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700944 }
945 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700946
947 // The lists are already sorted from most recent to oldest. Just pull the most recent off
948 // each list and add it to list. Stop when all lists are empty or maxNum reached.
949 while (maxNum > 0) {
950 long mostRecentActiveTime = Long.MIN_VALUE;
951 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 final int numTaskLists = runningTaskLists.size();
953 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
954 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700955 if (!stackTaskList.isEmpty()) {
956 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
957 if (lastActiveTime > mostRecentActiveTime) {
958 mostRecentActiveTime = lastActiveTime;
959 selectedStackList = stackTaskList;
960 }
961 }
962 }
963 if (selectedStackList != null) {
964 list.add(selectedStackList.remove(0));
965 --maxNum;
966 } else {
967 break;
968 }
969 }
Craig Mautner20e72272013-04-01 13:45:53 -0700970 }
971
Todd Kennedy7440f172015-12-09 14:31:22 -0800972 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
973 ProfilerInfo profilerInfo) {
974 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700975 if (aInfo != null) {
976 // Store the found target back into the intent, because now that
977 // we have it we never want to do this again. For example, if the
978 // user navigates back to this point in the history, we should
979 // always restart the exact same activity.
980 intent.setComponent(new ComponentName(
981 aInfo.applicationInfo.packageName, aInfo.name));
982
983 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700984 if (!aInfo.processName.equals("system")) {
985 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700986 mService.setDebugApp(aInfo.processName, true, false);
987 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700988
Man Caocfa78b22015-06-11 20:14:34 -0700989 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
990 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
991 }
992
993 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700994 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 }
996 }
997 }
998 return aInfo;
999 }
1000
Todd Kennedy7440f172015-12-09 14:31:22 -08001001 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1002 try {
1003 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1004 PackageManager.MATCH_DEFAULT_ONLY
1005 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1006 } catch (RemoteException e) {
1007 }
1008 return null;
1009 }
1010
1011 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1012 ProfilerInfo profilerInfo, int userId) {
1013 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1014 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1015 }
1016
Craig Mautner2420ead2013-04-01 17:13:20 -07001017 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001018 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001019 throws RemoteException {
1020
Craig Mautner2568c3a2015-03-26 14:22:34 -07001021 if (andResume) {
1022 r.startFreezingScreenLocked(app, 0);
1023 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001024
Craig Mautner2568c3a2015-03-26 14:22:34 -07001025 // schedule launch ticks to collect information about slow apps.
1026 r.startLaunchTickingLocked();
1027 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001028
1029 // Have the window manager re-evaluate the orientation of
1030 // the screen based on the new activity order. Note that
1031 // as a result of this, it can call back into the activity
1032 // manager with a new orientation. We don't care about that,
1033 // because the activity is not currently running so we are
1034 // just restarting it anyway.
1035 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001036 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001037 mService.mConfiguration,
1038 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001039 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001040 }
1041
1042 r.app = app;
1043 app.waitingToKill = null;
1044 r.launchCount++;
1045 r.lastLaunchTime = SystemClock.uptimeMillis();
1046
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001047 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001048
1049 int idx = app.activities.indexOf(r);
1050 if (idx < 0) {
1051 app.activities.add(r);
1052 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001053 mService.updateLruProcessLocked(app, true, null);
1054 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001055
Craig Mautner15df08a2015-04-01 12:17:18 -07001056 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001057 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1058 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001059 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001060 }
1061
1062 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001063 try {
1064 if (app.thread == null) {
1065 throw new RemoteException();
1066 }
1067 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001068 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001069 if (andResume) {
1070 results = r.results;
1071 newIntents = r.newIntents;
1072 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001073 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1074 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1075 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001076 if (andResume) {
1077 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1078 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001079 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001080 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001081 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001082 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001083 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001084 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001085 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001086 r.sleeping = false;
1087 r.forceNewConfig = false;
1088 mService.showAskCompatModeDialogLocked(r);
1089 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001090 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001091 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1092 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1093 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001094 final String profileFile = mService.mProfileFile;
1095 if (profileFile != null) {
1096 ParcelFileDescriptor profileFd = mService.mProfileFd;
1097 if (profileFd != null) {
1098 try {
1099 profileFd = profileFd.dup();
1100 } catch (IOException e) {
1101 if (profileFd != null) {
1102 try {
1103 profileFd.close();
1104 } catch (IOException o) {
1105 }
1106 profileFd = null;
1107 }
1108 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001109 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001110
1111 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1112 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001113 }
1114 }
1115 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001116
Craig Mautner2568c3a2015-03-26 14:22:34 -07001117 if (andResume) {
1118 app.hasShownUi = true;
1119 app.pendingUiClean = true;
1120 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001121 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001122 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001123 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001124 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001125 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001126 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001127
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001128 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001129 // This may be a heavy-weight process! Note that the package
1130 // manager will ensure that only activity can run in the main
1131 // process of the .apk, which is the only thing that will be
1132 // considered heavy-weight.
1133 if (app.processName.equals(app.info.packageName)) {
1134 if (mService.mHeavyWeightProcess != null
1135 && mService.mHeavyWeightProcess != app) {
1136 Slog.w(TAG, "Starting new heavy weight process " + app
1137 + " when already running "
1138 + mService.mHeavyWeightProcess);
1139 }
1140 mService.mHeavyWeightProcess = app;
1141 Message msg = mService.mHandler.obtainMessage(
1142 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1143 msg.obj = r;
1144 mService.mHandler.sendMessage(msg);
1145 }
1146 }
1147
1148 } catch (RemoteException e) {
1149 if (r.launchFailed) {
1150 // This is the second time we failed -- finish activity
1151 // and give up.
1152 Slog.e(TAG, "Second failure launching "
1153 + r.intent.getComponent().flattenToShortString()
1154 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001155 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1157 "2nd-crash", false);
1158 return false;
1159 }
1160
1161 // This is the first time we failed -- restart process and
1162 // retry.
1163 app.activities.remove(r);
1164 throw e;
1165 }
1166
1167 r.launchFailed = false;
1168 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001169 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001170 }
1171
1172 if (andResume) {
1173 // As part of the process of launching, ActivityThread also performs
1174 // a resume.
1175 stack.minimalResumeActivityLocked(r);
1176 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001177 // This activity is not starting in the resumed state... which should look like we asked
1178 // it to pause+stop (but remain visible), and it has done so and reported back the
1179 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001180 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001181 "Moving to PAUSED: " + r + " (starting in paused state)");
1182 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 }
1184
1185 // Launch the new version setup screen if needed. We do this -after-
1186 // launching the initial activity (that is, home), so that it can have
1187 // a chance to initialize itself while in the background, making the
1188 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001189 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001190 mService.startSetupActivityLocked();
1191 }
1192
Dianne Hackborn465fa392014-09-14 14:21:18 -07001193 // Update any services we are bound to that might care about whether
1194 // their client may have activities.
1195 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1196
Craig Mautner2420ead2013-04-01 17:13:20 -07001197 return true;
1198 }
1199
Craig Mautnere79d42682013-04-01 19:01:53 -07001200 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001201 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001202 // Is this activity's application already running?
1203 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001204 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001205
1206 r.task.stack.setLaunchTime(r);
1207
1208 if (app != null && app.thread != null) {
1209 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001210 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1211 || !"android".equals(r.info.packageName)) {
1212 // Don't add this if it is a platform component that is marked
1213 // to run in multiple processes, because this is actually
1214 // part of the framework so doesn't make sense to track as a
1215 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001216 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1217 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001218 }
George Mount2c92c972014-03-20 09:38:23 -07001219 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001220 return;
1221 } catch (RemoteException e) {
1222 Slog.w(TAG, "Exception when starting activity "
1223 + r.intent.getComponent().flattenToShortString(), e);
1224 }
1225
1226 // If a dead object exception was thrown -- fall through to
1227 // restart the application.
1228 }
1229
1230 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001231 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001232 }
1233
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001234 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001235 String resultWho, int requestCode, int callingPid, int callingUid,
1236 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1237 ActivityRecord resultRecord, ActivityStack resultStack) {
1238 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1239 callingUid);
1240 if (startAnyPerm == PERMISSION_GRANTED) {
1241 return true;
1242 }
1243 final int componentRestriction = getComponentRestrictionForCallingPackage(
1244 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1245 final int actionRestriction = getActionRestrictionForCallingPackage(
1246 intent.getAction(), callingPackage, callingPid, callingUid);
1247 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1248 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1249 if (resultRecord != null) {
1250 resultStack.sendActivityResultLocked(-1,
1251 resultRecord, resultWho, requestCode,
1252 Activity.RESULT_CANCELED, null);
1253 }
1254 final String msg;
1255 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1256 msg = "Permission Denial: starting " + intent.toString()
1257 + " from " + callerApp + " (pid=" + callingPid
1258 + ", uid=" + callingUid + ")" + " with revoked permission "
1259 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1260 } else if (!aInfo.exported) {
1261 msg = "Permission Denial: starting " + intent.toString()
1262 + " from " + callerApp + " (pid=" + callingPid
1263 + ", uid=" + callingUid + ")"
1264 + " not exported from uid " + aInfo.applicationInfo.uid;
1265 } else {
1266 msg = "Permission Denial: starting " + intent.toString()
1267 + " from " + callerApp + " (pid=" + callingPid
1268 + ", uid=" + callingUid + ")"
1269 + " requires " + aInfo.permission;
1270 }
1271 Slog.w(TAG, msg);
1272 throw new SecurityException(msg);
1273 }
1274
1275 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1276 final String message = "Appop Denial: starting " + intent.toString()
1277 + " from " + callerApp + " (pid=" + callingPid
1278 + ", uid=" + callingUid + ")"
1279 + " requires " + AppOpsManager.permissionToOp(
1280 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1281 Slog.w(TAG, message);
1282 return false;
1283 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1284 final String message = "Appop Denial: starting " + intent.toString()
1285 + " from " + callerApp + " (pid=" + callingPid
1286 + ", uid=" + callingUid + ")"
1287 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1288 Slog.w(TAG, message);
1289 return false;
1290 }
1291 return true;
1292 }
1293
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001294 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001295 final long identity = Binder.clearCallingIdentity();
1296 try {
1297 return UserManager.get(mService.mContext).getUserInfo(userId);
1298 } finally {
1299 Binder.restoreCallingIdentity(identity);
1300 }
1301 }
1302
Svet Ganov99b60432015-06-27 13:15:22 -07001303 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001304 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001305 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1306 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001307 == PackageManager.PERMISSION_DENIED) {
1308 return ACTIVITY_RESTRICTION_PERMISSION;
1309 }
1310
Christopher Tateff7add02015-08-17 10:23:22 -07001311 if (activityInfo.permission == null) {
1312 return ACTIVITY_RESTRICTION_NONE;
1313 }
1314
Svet Ganov99b60432015-06-27 13:15:22 -07001315 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1316 if (opCode == AppOpsManager.OP_NONE) {
1317 return ACTIVITY_RESTRICTION_NONE;
1318 }
1319
1320 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1321 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001322 if (!ignoreTargetSecurity) {
1323 return ACTIVITY_RESTRICTION_APPOP;
1324 }
Svet Ganov99b60432015-06-27 13:15:22 -07001325 }
1326
1327 return ACTIVITY_RESTRICTION_NONE;
1328 }
1329
Svetoslav7008b512015-06-24 18:47:07 -07001330 private int getActionRestrictionForCallingPackage(String action,
1331 String callingPackage, int callingPid, int callingUid) {
1332 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001333 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001334 }
1335
1336 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1337 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001338 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001339 }
1340
1341 final PackageInfo packageInfo;
1342 try {
1343 packageInfo = mService.mContext.getPackageManager()
1344 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1345 } catch (PackageManager.NameNotFoundException e) {
1346 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001347 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001348 }
1349
1350 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001351 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001352 }
1353
1354 if (mService.checkPermission(permission, callingPid, callingUid) ==
1355 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001356 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001357 }
1358
1359 final int opCode = AppOpsManager.permissionToOpCode(permission);
1360 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001361 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001362 }
1363
1364 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1365 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001366 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001367 }
1368
Svet Ganov99b60432015-06-27 13:15:22 -07001369 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001370 }
1371
Wale Ogunwaled046a012015-12-24 13:05:59 -08001372 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001373 if (r == null) {
1374 // Not sure what you are trying to do, but it is not going to work...
1375 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001376 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001377 final TaskRecord task = r.task;
1378 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001379 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001380 return false;
1381 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001382 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001383 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001384 }
1385
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001386 void setLaunchSource(int uid) {
1387 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1388 }
1389
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001390 void acquireLaunchWakelock() {
1391 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1392 throw new IllegalStateException("Calling must be system uid");
1393 }
1394 mLaunchingActivity.acquire();
1395 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1396 // To be safe, don't allow the wake lock to be held for too long.
1397 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1398 }
1399 }
1400
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001401 /**
1402 * Called when the frontmost task is idle.
1403 * @return the state of mService.mBooting before this was called.
1404 */
1405 private boolean checkFinishBootingLocked() {
1406 final boolean booting = mService.mBooting;
1407 boolean enableScreen = false;
1408 mService.mBooting = false;
1409 if (!mService.mBooted) {
1410 mService.mBooted = true;
1411 enableScreen = true;
1412 }
1413 if (booting || enableScreen) {
1414 mService.postFinishBooting(booting, enableScreen);
1415 }
1416 return booting;
1417 }
1418
Craig Mautnerf3333272013-04-22 10:55:53 -07001419 // Checked.
1420 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1421 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001422 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001423
Craig Mautnerf3333272013-04-22 10:55:53 -07001424 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001425 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001426 int NS = 0;
1427 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001428 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001429 boolean activityRemoved = false;
1430
Wale Ogunwale7d701172015-03-11 15:36:30 -07001431 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001432 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001433 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1434 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001435 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1436 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001437 if (fromTimeout) {
1438 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001439 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001440
1441 // This is a hack to semi-deal with a race condition
1442 // in the client where it can be constructed with a
1443 // newer configuration from when we asked it to launch.
1444 // We'll update with whatever configuration it now says
1445 // it used to launch.
1446 if (config != null) {
1447 r.configuration = config;
1448 }
1449
1450 // We are now idle. If someone is waiting for a thumbnail from
1451 // us, we can now deliver.
1452 r.idle = true;
1453
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001454 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001455 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001456 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001457 }
1458 }
1459
1460 if (allResumedActivitiesIdle()) {
1461 if (r != null) {
1462 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001463 }
1464
1465 if (mLaunchingActivity.isHeld()) {
1466 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1467 if (VALIDATE_WAKE_LOCK_CALLER &&
1468 Binder.getCallingUid() != Process.myUid()) {
1469 throw new IllegalStateException("Calling must be system uid");
1470 }
1471 mLaunchingActivity.release();
1472 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001473 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001474 }
1475
1476 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001477 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001478 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001479 if ((NF = mFinishingActivities.size()) > 0) {
1480 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001481 mFinishingActivities.clear();
1482 }
1483
Craig Mautnerf3333272013-04-22 10:55:53 -07001484 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001485 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001486 mStartingUsers.clear();
1487 }
1488
Craig Mautnerf3333272013-04-22 10:55:53 -07001489 // Stop any activities that are scheduled to do so but have been
1490 // waiting for the next one to start.
1491 for (int i = 0; i < NS; i++) {
1492 r = stops.get(i);
1493 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001494 if (stack != null) {
1495 if (r.finishing) {
1496 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1497 } else {
1498 stack.stopActivityLocked(r);
1499 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001500 }
1501 }
1502
1503 // Finish any activities that are scheduled to do so but have been
1504 // waiting for the next one to start.
1505 for (int i = 0; i < NF; i++) {
1506 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001507 final ActivityStack stack = r.task.stack;
1508 if (stack != null) {
1509 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1510 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 }
1512
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001513 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001514 // Complete user switch
1515 if (startingUsers != null) {
1516 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001517 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001518 }
1519 }
1520 // Complete starting up of background users
1521 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001522 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001523 mStartingBackgroundUsers.clear();
1524 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001525 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001526 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001527 }
1528 }
1529
1530 mService.trimApplications();
1531 //dump();
1532 //mWindowManager.dump();
1533
Craig Mautnerf3333272013-04-22 10:55:53 -07001534 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001535 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001536 }
1537
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001538 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001539 }
1540
Craig Mautner8e569572013-10-11 17:36:59 -07001541 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001542 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1546 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1547 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001548 }
Craig Mautner19091252013-10-05 00:03:53 -07001549 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001550 }
1551
1552 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001555 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1556 stacks.get(stackNdx).closeSystemDialogsLocked();
1557 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001558 }
1559 }
1560
Craig Mautner93529a42013-10-04 15:03:13 -07001561 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001562 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001563 }
1564
Craig Mautner8d341ef2013-03-26 09:03:27 -07001565 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001566 * Update the last used stack id for non-current user (current user's last
1567 * used stack is the focused stack)
1568 */
1569 void updateUserStackLocked(int userId, ActivityStack stack) {
1570 if (userId != mCurrentUser) {
1571 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1572 }
1573 }
1574
1575 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001576 * @return true if some activity was finished (or would have finished if doit were true).
1577 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001578 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1579 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001580 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001581 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1582 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001583 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001584 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001585 if (stack.finishDisabledPackageActivitiesLocked(
1586 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001587 didSomething = true;
1588 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001589 }
1590 }
1591 return didSomething;
1592 }
1593
Dianne Hackborna413dc02013-07-12 12:02:55 -07001594 void updatePreviousProcessLocked(ActivityRecord r) {
1595 // Now that this process has stopped, we may want to consider
1596 // it to be the previous app to try to keep around in case
1597 // the user wants to return to it.
1598
1599 // First, found out what is currently the foreground app, so that
1600 // we don't blow away the previous app if this activity is being
1601 // hosted by the process that is actually still the foreground.
1602 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1604 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1606 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001607 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001608 if (stack.mResumedActivity != null) {
1609 fgApp = stack.mResumedActivity.app;
1610 } else if (stack.mPausingActivity != null) {
1611 fgApp = stack.mPausingActivity.app;
1612 }
1613 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001614 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001615 }
1616 }
1617
1618 // Now set this one as the previous process, only if that really
1619 // makes sense to.
1620 if (r.app != null && fgApp != null && r.app != fgApp
1621 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001622 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001623 mService.mPreviousProcess = r.app;
1624 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1625 }
1626 }
1627
Wale Ogunwaled046a012015-12-24 13:05:59 -08001628 boolean resumeFocusedStackTopActivityLocked() {
1629 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001630 }
1631
Wale Ogunwaled046a012015-12-24 13:05:59 -08001632 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001633 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001634 if (targetStack != null && isFocusedStack(targetStack)) {
1635 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001636 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001637 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1638 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001639 }
1640
Todd Kennedy539db512014-12-15 09:57:55 -08001641 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1643 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001644 final int numStacks = stacks.size();
1645 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1646 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001647 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001648 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001649 }
1650 }
1651
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001652 void finishVoiceTask(IVoiceInteractionSession session) {
1653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1655 final int numStacks = stacks.size();
1656 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1657 final ActivityStack stack = stacks.get(stackNdx);
1658 stack.finishVoiceTask(session);
1659 }
1660 }
1661 }
1662
Chong Zhang280d3322015-11-03 17:27:26 -08001663 void findTaskToMoveToFrontLocked(
1664 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001665 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1666 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001667 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001668 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1669 // Caller wants the home activity moved with it. To accomplish this,
1670 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001671 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001672 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001673 if (task.stack == null) {
1674 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1675 + task + " to front. Stack is null");
1676 return;
1677 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001678
Chong Zhang0fa656b2015-08-31 15:17:21 -07001679 if (task.mResizeable && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001680 int stackId = options.getLaunchStackId();
1681 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001682 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001683 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001684 if (stackId == INVALID_STACK_ID) {
1685 stackId = task.getLaunchStackId();
1686 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001687 if (stackId != task.stack.mStackId) {
1688 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1689 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1690 // still need moveTaskToFrontLocked() below for any transition settings.
1691 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001692 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1693 resizeStackLocked(stackId, bounds,
1694 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1695 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1696 } else {
1697 // WM resizeTask must be done after the task is moved to the correct stack,
1698 // because Task's setBounds() also updates dim layer's bounds, but that has
1699 // dependency on the stack.
1700 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1701 false /* relayout */, false /* forced */);
1702 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001703 }
1704 }
1705
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001706 final ActivityRecord r = task.getTopActivity();
1707 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1708 r == null ? null : r.appTimeTracker, reason);
1709
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001710 if (DEBUG_STACK) Slog.d(TAG_STACK,
1711 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001712 }
1713
Wale Ogunwale854809c2015-12-27 16:18:19 -08001714 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001715 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001716 // window management or is launching into the pinned stack.
1717 if (!options.hasLaunchBounds()) {
1718 return false;
1719 }
1720 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1721 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001722 }
1723
Craig Mautner967212c2013-04-13 21:10:58 -07001724 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001725 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001726 }
1727
1728 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001729 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1730 if (activityContainer != null) {
1731 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001732 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001733 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001734 return null;
1735 }
1736 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001737 }
1738
Craig Mautner967212c2013-04-13 21:10:58 -07001739 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001740 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1742 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001743 }
1744 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001745 }
1746
Craig Mautner4a1cb222013-12-04 16:14:06 -08001747 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001748 ActivityRecord homeActivity = getHomeActivity();
1749 if (homeActivity != null) {
1750 return homeActivity.appToken;
1751 }
1752 return null;
1753 }
1754
1755 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001756 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001757 }
1758
1759 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001760 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1761 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1762 final TaskRecord task = tasks.get(taskNdx);
1763 if (task.isHomeTask()) {
1764 final ArrayList<ActivityRecord> activities = task.mActivities;
1765 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1766 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001767 if (r.isHomeActivity()
1768 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001769 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001770 }
1771 }
1772 }
1773 }
1774 return null;
1775 }
1776
Chong Zhangb15758a2015-11-17 12:12:03 -08001777 /**
1778 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1779 * the docked stack itself, or if it's side-by-side to the docked stack.
1780 */
1781 boolean isStackDockedInEffect(int stackId) {
1782 return stackId == DOCKED_STACK_ID ||
1783 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1784 }
1785
Todd Kennedyca4d8422015-01-15 15:19:22 -08001786 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001787 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001788 ActivityContainer activityContainer =
1789 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001790 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001791 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1792 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001793 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001794 return activityContainer;
1795 }
1796
Craig Mautner34b73df2014-01-12 21:11:08 -08001797 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001798 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1799 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1800 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001801 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1802 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001803 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001804 }
1805 }
1806
Craig Mautner95da1082014-02-24 17:54:35 -08001807 void deleteActivityContainer(IActivityContainer container) {
1808 ActivityContainer activityContainer = (ActivityContainer)container;
1809 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001810 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1811 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001812 final int stackId = activityContainer.mStackId;
1813 mActivityContainers.remove(stackId);
1814 mWindowManager.removeStack(stackId);
1815 }
1816 }
1817
Jorim Jaggidc249c42015-12-15 14:57:31 -08001818 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1819 boolean preserveWindows, boolean allowResizeInDockedMode) {
1820 if (stackId == DOCKED_STACK_ID) {
1821 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1822 preserveWindows);
1823 return;
1824 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001825 final ActivityStack stack = getStack(stackId);
1826 if (stack == null) {
1827 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1828 return;
1829 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001830
Jorim Jaggidc249c42015-12-15 14:57:31 -08001831 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001832 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1833 // stack size changing so things don't get out of sync.
1834 return;
1835 }
1836
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001837 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001838 mWindowManager.deferSurfaceLayout();
1839 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001840 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1841 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001842 } finally {
1843 mWindowManager.continueSurfaceLayout();
1844 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001845 }
1846 }
1847
Jorim Jaggidc249c42015-12-15 14:57:31 -08001848 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1849 Rect tempTaskInsetBounds) {
1850 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1851 // The bounds passed in corresponds to the fullscreen bounds which we normally
1852 // represent with null. Go ahead and set it to null so that all tasks configuration
1853 // can have the right fullscreen state.
1854 bounds = null;
1855 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001856 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001857
1858 mTmpBounds.clear();
1859 mTmpConfigs.clear();
1860 mTmpInsetBounds.clear();
1861 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1862 for (int i = tasks.size() - 1; i >= 0; i--) {
1863 TaskRecord task = tasks.get(i);
1864 if (task.mResizeable) {
1865 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1866 // For freeform stack we don't adjust the size of the tasks to match that
1867 // of the stack, but we do try to make sure the tasks are still contained
1868 // with the bounds of the stack.
1869 tempRect2.set(task.mBounds);
1870 fitWithinBounds(tempRect2, bounds);
1871 task.updateOverrideConfiguration(tempRect2);
1872 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001873 task.updateOverrideConfiguration(
1874 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001875 }
1876 }
1877
1878 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1879 mTmpBounds.put(task.taskId, task.mBounds);
1880 if (tempTaskInsetBounds != null) {
1881 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1882 }
1883 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001884
1885 // We might trigger a configuration change. Save the current task bounds for freezing.
1886 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001887 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1888 mTmpBounds, mTmpInsetBounds);
1889 stack.setBounds(bounds);
1890 }
1891
1892 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1893 boolean preserveWindows) {
1894 if (r == null) {
1895 return;
1896 }
1897 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1898 preserveWindows);
1899 // And we need to make sure at this point that all other activities
1900 // are made visible with the correct configuration.
1901 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1902 if (!updated) {
1903 resumeFocusedStackTopActivityLocked();
1904 }
1905 }
1906
1907 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1908 Rect tempDockedTaskInsetBounds,
1909 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1910 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1911 if (stack == null) {
1912 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1913 return;
1914 }
1915
1916 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1917 mWindowManager.deferSurfaceLayout();
1918 try {
1919 ActivityRecord r = stack.topRunningActivityLocked();
1920 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1921 tempDockedTaskInsetBounds);
1922
1923 if (stack.mFullscreen) {
1924 // The dock stack went fullscreen which is kinda like dismissing it.
1925 // In this case we make all other static stacks fullscreen and move all
1926 // docked stack tasks to the fullscreen stack.
1927 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1928 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1929 resizeStackLocked(i, null, null, null, preserveWindows,
1930 true /* allowResizeInDockedMode */);
1931 }
1932 }
1933
1934 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1935 final int count = tasks.size();
1936 for (int i = 0; i < count; i++) {
1937 moveTaskToStackLocked(tasks.get(i).taskId,
1938 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1939 false /* animate */);
1940 }
1941
1942 // stack shouldn't contain anymore activities, so nothing to resume.
1943 r = null;
1944 } else {
1945 // Docked stacks occupy a dedicated region on screen so the size of all other
1946 // static stacks need to be adjusted so they don't overlap with the docked stack.
1947 // We get the bounds to use from window manager which has been adjusted for any
1948 // screen controls and is also the same for all stacks.
1949 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1950 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1951 if (StackId.isResizeableByDockedStack(i)) {
1952 ActivityStack otherStack = getStack(i);
1953 if (otherStack != null) {
1954 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1955 tempOtherTaskInsetBounds, preserveWindows,
1956 true /* allowResizeInDockedMode */);
1957 }
1958 }
1959 }
1960 }
1961 ensureConfigurationAndResume(stack, r, preserveWindows);
1962 } finally {
1963 mWindowManager.continueSurfaceLayout();
1964 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1965 }
1966
1967 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
1968 tempDockedTaskBounds != null
1969 || tempDockedTaskInsetBounds != null
1970 || tempOtherTaskBounds != null
1971 || tempOtherTaskInsetBounds != null);
1972 }
1973
Chong Zhangf596cd52016-01-05 13:42:44 -08001974 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001975 if (!task.mResizeable) {
1976 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08001977 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001978 }
1979
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001980 adjustForMinimalTaskDimensions(task, bounds);
1981
Chong Zhang87b21722015-09-21 15:39:51 -07001982 // If this is a forced resize, let it go through even if the bounds is not changing,
1983 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001984 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001985 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001986 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08001987 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001988 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001989 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001990
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001991 if (!mWindowManager.isValidTaskId(task.taskId)) {
1992 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001993 // All we can do for now is update the bounds so it can be used when the task is
1994 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001995 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001996 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1997 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001998 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001999 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002000 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002001 }
2002
Chong Zhang6de2ae82015-09-30 18:25:21 -07002003 // Do not move the task to another stack here.
2004 // This method assumes that the task is already placed in the right stack.
2005 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002006
Chong Zhang6de2ae82015-09-30 18:25:21 -07002007 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002008
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002009 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002010 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002011 // way and the activity was kept the way it was. If it's false, it means the activity had
2012 // to be relaunched due to configuration change.
2013 boolean kept = true;
2014 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002015 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002016 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002017 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002018 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002019 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002020 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002021 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002022 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002023 }
2024 }
2025 }
Chong Zhang87b21722015-09-21 15:39:51 -07002026 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002027
2028 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002029 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002030 }
2031
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002032 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2033 if (bounds == null) {
2034 return;
2035 }
2036 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2037 : task.mMinimalSize;
2038 final boolean adjustWidth = minimalSize > bounds.width();
2039 final boolean adjustHeight = minimalSize > bounds.height();
2040 if (!(adjustWidth || adjustHeight)) {
2041 return;
2042 }
2043 Rect taskBounds = task.mBounds;
2044 if (adjustWidth) {
2045 if (taskBounds != null && bounds.right == taskBounds.right) {
2046 bounds.left = bounds.right - minimalSize;
2047 } else {
2048 // Either left bounds match, or neither match, or the previous bounds were
2049 // fullscreen and we default to keeping left.
2050 bounds.right = bounds.left + minimalSize;
2051 }
2052 }
2053 if (adjustHeight) {
2054 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2055 bounds.top = bounds.bottom - minimalSize;
2056 } else {
2057 // Either top bounds match, or neither match, or the previous bounds were
2058 // fullscreen and we default to keeping top.
2059 bounds.bottom = bounds.top + minimalSize;
2060 }
2061 }
2062 }
2063
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002064 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002065 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2066 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002067 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002068 }
2069
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002070 ActivityContainer activityContainer = new ActivityContainer(stackId);
2071 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002072 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002073 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002074 }
2075
2076 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002077 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002078 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2079 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002080 break;
2081 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002082 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002083 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002084 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002085 }
2086
Chong Zhang5dcb2752015-08-18 13:50:26 -07002087 /**
2088 * Restores a recent task to a stack
2089 * @param task The recent task to be restored.
2090 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002091 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002092 * @return true if the task has been restored successfully.
2093 */
2094 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2095 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002096 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002097 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002098 if (task.stack != null) {
2099 // Task has already been restored once. See if we need to do anything more
2100 if (task.stack.mStackId == stackId) {
2101 // Nothing else to do since it is already restored in the right stack.
2102 return true;
2103 }
2104 // Remove current stack association, so we can re-associate the task with the
2105 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002106 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002107 }
2108
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002109 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002110 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002111
2112 if (stack == null) {
2113 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002114 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2115 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002116 return false;
2117 }
2118
Wale Ogunwale5f986092015-12-04 15:35:38 -08002119 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002120 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2121 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002122 final ArrayList<ActivityRecord> activities = task.mActivities;
2123 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002124 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002125 }
2126 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002127 }
2128
Wale Ogunwale040b4702015-08-06 18:10:50 -07002129 /**
2130 * Moves the specified task record to the input stack id.
2131 * WARNING: This method performs an unchecked/raw move of the task and
2132 * can leave the system in an unstable state if used incorrectly.
2133 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2134 * to a stack.
2135 * @param task Task to move.
2136 * @param stackId Id of stack to move task to.
2137 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002138 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002139 * @param reason Reason the task is been moved.
2140 * @return The stack the task was moved to.
2141 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002142 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002143 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2144 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002145 final ActivityStack prevStack = task.stack;
2146 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2147 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2148 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2149 // Whenever we are moving the top activity from the front stack we want to make sure to move
2150 // the stack to the front.
2151 final boolean wasFront = isFrontStack(prevStack)
2152 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002153
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002154 final boolean resizeable = task.mResizeable;
2155 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2156 // if a docked stack is created below which will lead to the stack we are moving from and
2157 // its resizeable tasks being resized.
2158 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002159 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002160 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002161 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002162 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002163
2164 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002165 // move focus to the new stack by moving the stack to the front.
2166 stack.moveToFrontAndResumeStateIfNeeded(
2167 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002168
Wale Ogunwale040b4702015-08-06 18:10:50 -07002169 return stack;
2170 }
2171
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002172 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002173 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002174 final TaskRecord task = anyTaskForIdLocked(taskId);
2175 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002176 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002177 return;
2178 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002179
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002180 if (task.stack != null && task.stack.mStackId == stackId) {
2181 // You are already in the right stack silly...
2182 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2183 return;
2184 }
2185
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002186 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002187 final boolean mightReplaceWindow =
2188 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2189 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002190 // We are about to relaunch the activity because its configuration changed due to
2191 // being maximized, i.e. size change. The activity will first remove the old window
2192 // and then add a new one. This call will tell window manager about this, so it can
2193 // preserve the old window until the new one is drawn. This prevents having a gap
2194 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002195 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002196 // Note here we always set the replacing window first, as the flags might be needed
2197 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002198 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002199 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002200 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2201 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002202
Jorim Jaggi55387522015-11-24 18:21:10 -08002203 if (!animate) {
2204 stack.mNoAnimActivities.add(topActivity);
2205 }
2206
Chong Zhangf596cd52016-01-05 13:42:44 -08002207 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002208 // Make sure the task has the appropriate bounds/size for the stack it is in.
2209 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002210 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002211 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2212 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002213 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002214 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002215 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002216 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2217 }
2218
2219 if (mightReplaceWindow) {
2220 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2221 // window), we need to clear the replace window settings. Otherwise, we schedule a
2222 // timeout to remove the old window if the replacing window is not coming in time.
2223 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002224 }
2225
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002226 // The task might have already been running and its visibility needs to be synchronized with
2227 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002228 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002229 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002230
2231 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002232 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002233 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002234 }
2235
Wale Ogunwale079a0042015-10-24 11:44:07 -07002236 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2237 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2238 if (stack == null) {
2239 throw new IllegalArgumentException(
2240 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2241 }
2242
2243 final ActivityRecord r = stack.topRunningActivityLocked();
2244 if (r == null) {
2245 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2246 + " in stack=" + stack);
2247 return false;
2248 }
2249
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002250 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002251 Slog.w(TAG,
2252 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002253 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002254 return false;
2255 }
2256
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002257 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2258 return true;
2259 }
2260
2261 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002262 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002263 if (task.mActivities.size() == 1) {
2264 // 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 -08002265 // stack without re-parenting the activity in a different task.
2266 moveTaskToStackLocked(
2267 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002268 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002269 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2270 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002271 }
2272
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002273 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002274 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2275 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002276 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002277
2278 // The task might have already been running and its visibility needs to be synchronized with
2279 // the visibility of the stack / windows.
2280 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002281 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002282
2283 if (stackId == PINNED_STACK_ID) {
2284 mService.notifyActivityPinnedLocked();
2285 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002286 }
2287
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002288 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2289 final TaskRecord task = anyTaskForIdLocked(taskId);
2290 if (task == null) {
2291 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2292 return;
2293 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002294 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2295
2296 task.updateOverrideConfigurationForStack(stack);
2297
2298 mWindowManager.positionTaskInStack(
2299 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002300 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002301 // The task might have already been running and its visibility needs to be synchronized with
2302 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002303 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002304 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002305 }
2306
Craig Mautnerac6f8432013-07-17 13:24:59 -07002307 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002308 mTmpFindTaskResult.r = null;
2309 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002310 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002311 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2312 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002313 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2314 final ActivityStack stack = stacks.get(stackNdx);
2315 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002316 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002317 continue;
2318 }
2319 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002320 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2321 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002322 continue;
2323 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002324 stack.findTaskLocked(r, mTmpFindTaskResult);
2325 // It is possible to have task in multiple stacks with the same root affinity.
2326 // If the match we found was based on root affinity we keep on looking to see if
2327 // there is a better match in another stack. We eventually return the match based
2328 // on root affinity if we don't find a better match.
2329 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2330 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002331 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002332 }
2333 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002334 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2335 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002336 }
2337
2338 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002339 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2340 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002341 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2342 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2343 if (ar != null) {
2344 return ar;
2345 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002346 }
2347 }
2348 return null;
2349 }
2350
Craig Mautner8d341ef2013-03-26 09:03:27 -07002351 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002352 scheduleSleepTimeout();
2353 if (!mGoingToSleep.isHeld()) {
2354 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002355 if (mLaunchingActivity.isHeld()) {
2356 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2357 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002358 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002359 mLaunchingActivity.release();
2360 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002361 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002362 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002363 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002364 }
2365
2366 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002367 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002368
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002369 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002370 final long endTime = System.currentTimeMillis() + timeout;
2371 while (true) {
2372 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002373 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2374 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002375 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2376 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2377 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002378 }
2379 if (cantShutdown) {
2380 long timeRemaining = endTime - System.currentTimeMillis();
2381 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002382 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002383 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002384 } catch (InterruptedException e) {
2385 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002386 } else {
2387 Slog.w(TAG, "Activity manager shutdown timed out");
2388 timedout = true;
2389 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002390 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002391 } else {
2392 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002393 }
2394 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002395
2396 // Force checkReadyForSleep to complete.
2397 mSleepTimeout = true;
2398 checkReadyForSleepLocked();
2399
Craig Mautner8d341ef2013-03-26 09:03:27 -07002400 return timedout;
2401 }
2402
2403 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002404 removeSleepTimeouts();
2405 if (mGoingToSleep.isHeld()) {
2406 mGoingToSleep.release();
2407 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002410 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2411 final ActivityStack stack = stacks.get(stackNdx);
2412 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002413 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002414 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002415 }
Craig Mautner5314a402013-09-26 12:40:16 -07002416 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002417 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002418 mGoingToSleepActivities.clear();
2419 }
2420
2421 void activitySleptLocked(ActivityRecord r) {
2422 mGoingToSleepActivities.remove(r);
2423 checkReadyForSleepLocked();
2424 }
2425
2426 void checkReadyForSleepLocked() {
2427 if (!mService.isSleepingOrShuttingDown()) {
2428 // Do not care.
2429 return;
2430 }
2431
2432 if (!mSleepTimeout) {
2433 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002434 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2435 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002436 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2437 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2438 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002439 }
2440
2441 if (mStoppingActivities.size() > 0) {
2442 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002443 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002444 + mStoppingActivities.size() + " activities");
2445 scheduleIdleLocked();
2446 dontSleep = true;
2447 }
2448
2449 if (mGoingToSleepActivities.size() > 0) {
2450 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002451 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002452 + mGoingToSleepActivities.size() + " activities");
2453 dontSleep = true;
2454 }
2455
2456 if (dontSleep) {
2457 return;
2458 }
2459 }
2460
Craig Mautnere0a38842013-12-16 16:14:02 -08002461 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2462 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002463 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2464 stacks.get(stackNdx).goToSleep();
2465 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002466 }
2467
2468 removeSleepTimeouts();
2469
2470 if (mGoingToSleep.isHeld()) {
2471 mGoingToSleep.release();
2472 }
2473 if (mService.mShuttingDown) {
2474 mService.notifyAll();
2475 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002476 }
2477
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002478 boolean reportResumedActivityLocked(ActivityRecord r) {
2479 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002480 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002481 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002482 }
2483 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002484 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002485 mWindowManager.executeAppTransition();
2486 return true;
2487 }
2488 return false;
2489 }
2490
Craig Mautner8d341ef2013-03-26 09:03:27 -07002491 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002492 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2493 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002494 int stackNdx = stacks.size() - 1;
2495 while (stackNdx >= 0) {
2496 stacks.get(stackNdx).handleAppCrashLocked(app);
2497 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002498 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002499 }
2500 }
2501
Jose Lima4b6c6692014-08-12 17:41:12 -07002502 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002503 final ActivityStack stack = r.task.stack;
2504 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002505 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2506 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002507 return false;
2508 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002509 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002510 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2511 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002512
2513 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002514 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002515 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002516 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002517 return true;
2518 }
2519
2520 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002521 if (visible && top.fullscreen) {
2522 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002523 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2524 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2525 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2526 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002527 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002528 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2529 // Only the activity set as currently visible behind should actively reset its
2530 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002531 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2532 "requestVisibleBehind: returning visible=" + visible
2533 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2534 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002535 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002536 }
2537
Jose Lima4b6c6692014-08-12 17:41:12 -07002538 stack.setVisibleBehindActivity(visible ? r : null);
2539 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002540 // Make the activity immediately above r opaque.
2541 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2542 if (next != null) {
2543 mService.convertFromTranslucent(next.appToken);
2544 }
2545 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002546 if (top.app != null && top.app.thread != null) {
2547 // Notify the top app of the change.
2548 try {
2549 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2550 } catch (RemoteException e) {
2551 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002552 }
2553 return true;
2554 }
2555
Craig Mautnerbb742462014-07-07 15:28:55 -07002556 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2557 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2558 r.mLaunchTaskBehind = false;
2559 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002560 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002561 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002562 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002563 mWindowManager.setAppVisibility(r.appToken, false);
2564 }
2565
2566 void scheduleLaunchTaskBehindComplete(IBinder token) {
2567 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2568 }
2569
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002570 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2571 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002572 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002573 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2574 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002575 final int topStackNdx = stacks.size() - 1;
2576 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2577 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002578 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002579 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002580 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002581 }
2582
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002583 void invalidateTaskLayers() {
2584 mTaskLayersChanged = true;
2585 }
2586
2587 void rankTaskLayersIfNeeded() {
2588 if (!mTaskLayersChanged) {
2589 return;
2590 }
2591 mTaskLayersChanged = false;
2592 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2593 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2594 int baseLayer = 0;
2595 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2596 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2597 }
2598 }
2599 }
2600
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002601 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2602 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2603 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2604 final int topStackNdx = stacks.size() - 1;
2605 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2606 final ActivityStack stack = stacks.get(stackNdx);
2607 stack.clearOtherAppTimeTrackers(except);
2608 }
2609 }
2610 }
2611
Craig Mautner8d341ef2013-03-26 09:03:27 -07002612 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 final int numStacks = stacks.size();
2616 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2617 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002618 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002620 }
2621 }
2622
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002623 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2624 // Examine all activities currently running in the process.
2625 TaskRecord firstTask = null;
2626 // Tasks is non-null only if two or more tasks are found.
2627 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002628 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2629 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002630 ActivityRecord r = app.activities.get(i);
2631 // First, if we find an activity that is in the process of being destroyed,
2632 // then we just aren't going to do anything for now; we want things to settle
2633 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002634 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002635 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002636 return;
2637 }
2638 // Don't consider any activies that are currently not in a state where they
2639 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002640 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2641 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002642 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002643 continue;
2644 }
2645 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002646 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002647 + " from " + r);
2648 if (firstTask == null) {
2649 firstTask = r.task;
2650 } else if (firstTask != r.task) {
2651 if (tasks == null) {
2652 tasks = new ArraySet<>();
2653 tasks.add(firstTask);
2654 }
2655 tasks.add(r.task);
2656 }
2657 }
2658 }
2659 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002660 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002661 return;
2662 }
2663 // If we have activities in multiple tasks that are in a position to be destroyed,
2664 // let's iterate through the tasks and release the oldest one.
2665 final int numDisplays = mActivityDisplays.size();
2666 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2667 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2668 // Step through all stacks starting from behind, to hit the oldest things first.
2669 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2670 final ActivityStack stack = stacks.get(stackNdx);
2671 // Try to release activities in this stack; if we manage to, we are done.
2672 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2673 return;
2674 }
2675 }
2676 }
2677 }
2678
Amith Yamasani37a40c22015-06-17 13:25:42 -07002679 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002680 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002681 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002682 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002683
Craig Mautner858d8a62013-04-23 17:08:34 -07002684 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2686 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 final ActivityStack stack = stacks.get(stackNdx);
2689 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002690 TaskRecord task = stack.topTask();
2691 if (task != null) {
2692 mWindowManager.moveTaskToTop(task.taskId);
2693 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002694 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002695 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002696
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002697 ActivityStack stack = getStack(restoreStackId);
2698 if (stack == null) {
2699 stack = mHomeStack;
2700 }
2701 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002702 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002703 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002704 } else {
2705 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002706 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002707 }
Craig Mautner93529a42013-10-04 15:03:13 -07002708 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002709 }
2710
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002711 /**
2712 * Add background users to send boot completed events to.
2713 * @param userId The user being started in the background
2714 * @param uss The state object for the user.
2715 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002716 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002717 mStartingBackgroundUsers.add(uss);
2718 }
2719
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002720 /** Checks whether the userid is a profile of the current user. */
2721 boolean isCurrentProfileLocked(int userId) {
2722 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002723 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002724 }
2725
Chong Zhang45c25ce2015-08-10 22:18:26 -07002726 /** Checks whether the activity should be shown for current user. */
2727 boolean okToShowLocked(ActivityRecord r) {
2728 return r != null && (isCurrentProfileLocked(r.userId)
2729 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2730 }
2731
Craig Mautnerde4ef022013-04-07 19:01:33 -07002732 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002733 ArrayList<ActivityRecord> stops = null;
2734
2735 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002736 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2737 ActivityRecord s = mStoppingActivities.get(activityNdx);
2738 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002739 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002740 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2741 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002742 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002743 if (s.finishing) {
2744 // If this activity is finishing, it is sitting on top of
2745 // everyone else but we now know it is no longer needed...
2746 // so get rid of it. Otherwise, we need to go through the
2747 // normal flow and hide it once we determine that it is
2748 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002749 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002750 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002751 }
2752 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002753 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002754 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002755 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002756 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002757 }
2758 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002759 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002760 }
2761 }
2762
2763 return stops;
2764 }
2765
Craig Mautnercf910b02013-04-23 11:23:27 -07002766 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2770 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002771 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002772 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002773 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002774 if (r == null) Slog.e(TAG,
2775 "validateTop...: null top activity, stack=" + stack);
2776 else {
2777 final ActivityRecord pausing = stack.mPausingActivity;
2778 if (pausing != null && pausing == r) Slog.e(TAG,
2779 "validateTop...: top stack has pausing activity r=" + r
2780 + " state=" + state);
2781 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2782 "validateTop...: activity in front not resumed r=" + r
2783 + " state=" + state);
2784 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002785 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002786 final ActivityRecord resumed = stack.mResumedActivity;
2787 if (resumed != null && resumed == r) Slog.e(TAG,
2788 "validateTop...: back stack has resumed activity r=" + r
2789 + " state=" + state);
2790 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2791 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002792 }
2793 }
2794 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002795 }
2796
Craig Mautnere0570202015-05-13 13:06:11 -07002797 private String lockTaskModeToString() {
2798 switch (mLockTaskModeState) {
2799 case LOCK_TASK_MODE_LOCKED:
2800 return "LOCKED";
2801 case LOCK_TASK_MODE_PINNED:
2802 return "PINNED";
2803 case LOCK_TASK_MODE_NONE:
2804 return "NONE";
2805 default: return "unknown=" + mLockTaskModeState;
2806 }
2807 }
2808
Craig Mautner27084302013-03-25 08:05:25 -07002809 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002810 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002811 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002812 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2813 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2814 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002815 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002816 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2817 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2818 if (packages.size() > 0) {
2819 pw.println(" mLockTaskPackages (userId:packages)=");
2820 for (int i = 0; i < packages.size(); ++i) {
2821 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2822 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2823 }
2824 }
2825 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002826 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827
Craig Mautner20e72272013-04-01 13:45:53 -07002828 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002829 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002830 }
2831
Dianne Hackborn390517b2013-05-30 15:03:32 -07002832 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2833 boolean needSep, String prefix) {
2834 if (activity != null) {
2835 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2836 if (needSep) {
2837 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002838 }
2839 pw.print(prefix);
2840 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002841 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002842 }
2843 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002844 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002845 }
2846
Craig Mautner8d341ef2013-03-26 09:03:27 -07002847 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2848 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002849 boolean printed = false;
2850 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2852 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002853 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002854 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002855 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002856 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002857 final ActivityStack stack = stacks.get(stackNdx);
2858 StringBuilder stackHeader = new StringBuilder(128);
2859 stackHeader.append(" Stack #");
2860 stackHeader.append(stack.mStackId);
2861 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002862 stackHeader.append("\n");
2863 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2864 stackHeader.append("\n");
2865 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002866 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2867 needSep, stackHeader.toString());
2868 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2869 !dumpAll, false, dumpPackage, true,
2870 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002871
Craig Mautner4a1cb222013-12-04 16:14:06 -08002872 needSep = printed;
2873 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2874 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002875 if (pr) {
2876 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002878 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002879 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2880 " mResumedActivity: ");
2881 if (pr) {
2882 printed = true;
2883 needSep = false;
2884 }
2885 if (dumpAll) {
2886 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2887 " mLastPausedActivity: ");
2888 if (pr) {
2889 printed = true;
2890 needSep = true;
2891 }
2892 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2893 needSep, " mLastNoHistoryActivity: ");
2894 }
2895 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002896 }
2897 }
2898
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002899 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2900 false, dumpPackage, true, " Activities waiting to finish:", null);
2901 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2902 false, dumpPackage, true, " Activities waiting to stop:", null);
2903 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2904 false, dumpPackage, true, " Activities waiting for another to become visible:",
2905 null);
2906 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2907 false, dumpPackage, true, " Activities waiting to sleep:", null);
2908 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2909 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002910
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002911 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002912 }
2913
Dianne Hackborn390517b2013-05-30 15:03:32 -07002914 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002916 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002918 String innerPrefix = null;
2919 String[] args = null;
2920 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 for (int i=list.size()-1; i>=0; i--) {
2922 final ActivityRecord r = list.get(i);
2923 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2924 continue;
2925 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002926 if (innerPrefix == null) {
2927 innerPrefix = prefix + " ";
2928 args = new String[0];
2929 }
2930 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931 final boolean full = !brief && (complete || !r.isInHistory());
2932 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002933 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002934 needNL = false;
2935 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002936 if (header1 != null) {
2937 pw.println(header1);
2938 header1 = null;
2939 }
2940 if (header2 != null) {
2941 pw.println(header2);
2942 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002943 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 if (lastTask != r.task) {
2945 lastTask = r.task;
2946 pw.print(prefix);
2947 pw.print(full ? "* " : " ");
2948 pw.println(lastTask);
2949 if (full) {
2950 lastTask.dump(pw, prefix + " ");
2951 } else if (complete) {
2952 // Complete + brief == give a summary. Isn't that obvious?!?
2953 if (lastTask.intent != null) {
2954 pw.print(prefix); pw.print(" ");
2955 pw.println(lastTask.intent.toInsecureStringWithClip());
2956 }
2957 }
2958 }
2959 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2960 pw.print(" #"); pw.print(i); pw.print(": ");
2961 pw.println(r);
2962 if (full) {
2963 r.dump(pw, innerPrefix);
2964 } else if (complete) {
2965 // Complete + brief == give a summary. Isn't that obvious?!?
2966 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2967 if (r.app != null) {
2968 pw.print(innerPrefix); pw.println(r.app);
2969 }
2970 }
2971 if (client && r.app != null && r.app.thread != null) {
2972 // flush anything that is already in the PrintWriter since the thread is going
2973 // to write to the file descriptor directly
2974 pw.flush();
2975 try {
2976 TransferPipe tp = new TransferPipe();
2977 try {
2978 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2979 r.appToken, innerPrefix, args);
2980 // Short timeout, since blocking here can
2981 // deadlock with the application.
2982 tp.go(fd, 2000);
2983 } finally {
2984 tp.kill();
2985 }
2986 } catch (IOException e) {
2987 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2988 } catch (RemoteException e) {
2989 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2990 }
2991 needNL = true;
2992 }
2993 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002994 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002995 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002996
Craig Mautnerf3333272013-04-22 10:55:53 -07002997 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002998 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2999 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003000 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3001 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003002 }
3003
3004 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003005 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003006 }
3007
3008 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003009 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3010 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003011 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3012 }
3013
Craig Mautner05d29032013-05-03 13:40:13 -07003014 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003015 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3016 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3017 }
Craig Mautner05d29032013-05-03 13:40:13 -07003018 }
3019
Craig Mautner0eea92c2013-05-16 13:35:39 -07003020 void removeSleepTimeouts() {
3021 mSleepTimeout = false;
3022 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3023 }
3024
3025 final void scheduleSleepTimeout() {
3026 removeSleepTimeouts();
3027 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3028 }
3029
Craig Mautner4a1cb222013-12-04 16:14:06 -08003030 @Override
3031 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003032 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003033 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3034 }
3035
3036 @Override
3037 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003038 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003039 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3040 }
3041
3042 @Override
3043 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003044 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3046 }
3047
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003048 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003049 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003051 newDisplay = mActivityDisplays.get(displayId) == null;
3052 if (newDisplay) {
3053 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003054 if (activityDisplay.mDisplay == null) {
3055 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3056 return;
3057 }
Craig Mautner4504de52013-12-20 09:06:56 -08003058 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003059 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003060 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003061 }
Craig Mautner4504de52013-12-20 09:06:56 -08003062 if (newDisplay) {
3063 mWindowManager.onDisplayAdded(displayId);
3064 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 }
3066
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003067 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3068 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3069 return;
3070 }
3071 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3072 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3073 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3074 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3075 }
3076
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003077 private void handleDisplayRemoved(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) {
3081 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003083 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003084 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003085 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 }
3087 }
3088 mWindowManager.onDisplayRemoved(displayId);
3089 }
3090
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003091 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003092 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003093 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3094 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095 // TODO: Update the bounds.
3096 }
3097 }
3098 mWindowManager.onDisplayChanged(displayId);
3099 }
3100
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003101 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003102 StackInfo info = new StackInfo();
3103 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3104 info.displayId = Display.DEFAULT_DISPLAY;
3105 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003106 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003107
3108 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3109 final int numTasks = tasks.size();
3110 int[] taskIds = new int[numTasks];
3111 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003112 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003113 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003114 for (int i = 0; i < numTasks; ++i) {
3115 final TaskRecord task = tasks.get(i);
3116 taskIds[i] = task.taskId;
3117 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3118 : task.realActivity != null ? task.realActivity.flattenToString()
3119 : task.getTopActivity() != null ? task.getTopActivity().packageName
3120 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003121 taskBounds[i] = new Rect();
3122 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003123 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003124 }
3125 info.taskIds = taskIds;
3126 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003127 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003128 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 return info;
3130 }
3131
3132 StackInfo getStackInfoLocked(int stackId) {
3133 ActivityStack stack = getStack(stackId);
3134 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003135 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 }
3137 return null;
3138 }
3139
3140 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003141 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003142 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3143 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003144 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003145 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003146 }
3147 }
3148 return list;
3149 }
3150
Craig Mautner15df08a2015-04-01 12:17:18 -07003151 TaskRecord getLockedTaskLocked() {
3152 final int top = mLockTaskModeTasks.size() - 1;
3153 if (top >= 0) {
3154 return mLockTaskModeTasks.get(top);
3155 }
3156 return null;
3157 }
3158
3159 boolean isLockedTask(TaskRecord task) {
3160 return mLockTaskModeTasks.contains(task);
3161 }
3162
3163 boolean isLastLockedTask(TaskRecord task) {
3164 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3165 }
3166
3167 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003168 if (!mLockTaskModeTasks.remove(task)) {
3169 return;
3170 }
3171 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3172 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003173 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003174 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3175 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003176 final Message lockTaskMsg = Message.obtain();
3177 lockTaskMsg.arg1 = task.userId;
3178 lockTaskMsg.what = LOCK_TASK_END_MSG;
3179 mHandler.sendMessage(lockTaskMsg);
3180 }
3181 }
3182
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003183 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003184 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003185 }
3186
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003187 void showLockTaskToast() {
3188 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003189 }
3190
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003191 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3192 if (mLockTaskModeTasks.contains(task)) {
3193 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3194 }
3195 }
3196
Craig Mautner432f64e2015-05-20 14:59:57 -07003197 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3198 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003199 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003200 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003201 final TaskRecord lockedTask = getLockedTaskLocked();
3202 if (lockedTask != null) {
3203 removeLockedTaskLocked(lockedTask);
3204 if (!mLockTaskModeTasks.isEmpty()) {
3205 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003206 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3207 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003208 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003209 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003210 return;
3211 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003212 }
Craig Mautnere0570202015-05-13 13:06:11 -07003213 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3214 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003215 return;
3216 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003217
3218 // Should have already been checked, but do it again.
3219 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003220 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3221 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003222 return;
3223 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003224 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003225 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003226 return;
3227 }
3228
3229 if (mLockTaskModeTasks.isEmpty()) {
3230 // First locktask.
3231 final Message lockTaskMsg = Message.obtain();
3232 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3233 lockTaskMsg.arg1 = task.userId;
3234 lockTaskMsg.what = LOCK_TASK_START_MSG;
3235 lockTaskMsg.arg2 = lockTaskModeState;
3236 mHandler.sendMessage(lockTaskMsg);
3237 }
3238 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003239 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3240 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003241 mLockTaskModeTasks.remove(task);
3242 mLockTaskModeTasks.add(task);
3243
3244 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003245 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003246 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003247
3248 if (andResume) {
3249 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003250 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003251 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003252 }
3253
3254 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003255 return isLockTaskModeViolation(task, false);
3256 }
3257
3258 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3259 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003260 return false;
3261 }
3262 final int lockTaskAuth = task.mLockTaskAuth;
3263 switch (lockTaskAuth) {
3264 case LOCK_TASK_AUTH_DONT_LOCK:
3265 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003266 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003267 case LOCK_TASK_AUTH_LAUNCHABLE:
3268 case LOCK_TASK_AUTH_WHITELISTED:
3269 return false;
3270 case LOCK_TASK_AUTH_PINNABLE:
3271 // Pinnable tasks can't be launched on top of locktask tasks.
3272 return !mLockTaskModeTasks.isEmpty();
3273 default:
3274 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3275 return true;
3276 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003277 }
3278
Craig Mautner15df08a2015-04-01 12:17:18 -07003279 void onLockTaskPackagesUpdatedLocked() {
3280 boolean didSomething = false;
3281 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3282 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003283 final boolean wasWhitelisted =
3284 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3285 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003286 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003287 final boolean isWhitelisted =
3288 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3289 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3290 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003291 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003292 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3293 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003294 removeLockedTaskLocked(lockedTask);
3295 lockedTask.performClearTaskLocked();
3296 didSomething = true;
3297 }
3298 }
3299 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3300 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3301 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3302 final ActivityStack stack = stacks.get(stackNdx);
3303 stack.onLockTaskPackagesUpdatedLocked();
3304 }
3305 }
Craig Mautnere0570202015-05-13 13:06:11 -07003306 final ActivityRecord r = topRunningActivityLocked();
3307 final TaskRecord task = r != null ? r.task : null;
3308 if (mLockTaskModeTasks.isEmpty() && task != null
3309 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3310 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003311 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3312 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3313 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3314 false);
3315 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003316 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003317 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003318 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003319 }
3320 }
3321
Benjamin Franz43261142015-02-11 15:59:44 +00003322 int getLockTaskModeState() {
3323 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003324 }
3325
Jorim Jaggife89d122015-12-22 16:28:44 +01003326 void activityRelaunchedLocked(IBinder token) {
3327 mWindowManager.notifyAppRelaunchingFinished(token);
3328 }
3329
3330 void activityRelaunchingLocked(ActivityRecord r) {
3331 mWindowManager.notifyAppRelaunching(r.appToken);
3332 }
3333
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003334 void logStackState() {
3335 mActivityMetricsLogger.logWindowState();
3336 }
3337
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003338 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003340 public ActivityStackSupervisorHandler(Looper looper) {
3341 super(looper);
3342 }
3343
Craig Mautnerf3333272013-04-22 10:55:53 -07003344 void activityIdleInternal(ActivityRecord r) {
3345 synchronized (mService) {
3346 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3347 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003348 }
3349
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003350 @Override
3351 public void handleMessage(Message msg) {
3352 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003353 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003354 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3355 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003356 if (mService.mDidDexOpt) {
3357 mService.mDidDexOpt = false;
3358 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3359 nmsg.obj = msg.obj;
3360 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3361 return;
3362 }
3363 // We don't at this point know if the activity is fullscreen,
3364 // so we need to be conservative and assume it isn't.
3365 activityIdleInternal((ActivityRecord)msg.obj);
3366 } break;
3367 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003368 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003369 activityIdleInternal((ActivityRecord)msg.obj);
3370 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003371 case RESUME_TOP_ACTIVITY_MSG: {
3372 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003373 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003374 }
3375 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003376 case SLEEP_TIMEOUT_MSG: {
3377 synchronized (mService) {
3378 if (mService.isSleepingOrShuttingDown()) {
3379 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3380 mSleepTimeout = true;
3381 checkReadyForSleepLocked();
3382 }
3383 }
3384 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003385 case LAUNCH_TIMEOUT_MSG: {
3386 if (mService.mDidDexOpt) {
3387 mService.mDidDexOpt = false;
3388 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3389 return;
3390 }
3391 synchronized (mService) {
3392 if (mLaunchingActivity.isHeld()) {
3393 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3394 if (VALIDATE_WAKE_LOCK_CALLER
3395 && Binder.getCallingUid() != Process.myUid()) {
3396 throw new IllegalStateException("Calling must be system uid");
3397 }
3398 mLaunchingActivity.release();
3399 }
3400 }
3401 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003402 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003403 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003404 } break;
3405 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003406 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 } break;
3408 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003409 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003411 case CONTAINER_CALLBACK_VISIBILITY: {
3412 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003413 final IActivityContainerCallback callback = container.mCallback;
3414 if (callback != null) {
3415 try {
3416 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3417 } catch (RemoteException e) {
3418 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003419 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003420 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003421 case LOCK_TASK_START_MSG: {
3422 // When lock task starts, we disable the status bars.
3423 try {
Jason Monk62515be2014-05-21 16:06:19 -04003424 if (mLockTaskNotify == null) {
3425 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003426 }
Jason Monk62515be2014-05-21 16:06:19 -04003427 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003428 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003429 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003430 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003431 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003432 flags = StatusBarManager.DISABLE_MASK
3433 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003434 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003435 flags = StatusBarManager.DISABLE_MASK
3436 & (~StatusBarManager.DISABLE_BACK)
3437 & (~StatusBarManager.DISABLE_HOME)
3438 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003439 }
3440 getStatusBarService().disable(flags, mToken,
3441 mService.mContext.getPackageName());
3442 }
3443 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003444 if (getDevicePolicyManager() != null) {
3445 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003446 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003447 }
justinzhang5286d3f2014-05-12 17:06:01 -04003448 } catch (RemoteException ex) {
3449 throw new RuntimeException(ex);
3450 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003451 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003452 case LOCK_TASK_END_MSG: {
3453 // When lock task ends, we enable the status bars.
3454 try {
Jason Monk62515be2014-05-21 16:06:19 -04003455 if (getStatusBarService() != null) {
3456 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3457 mService.mContext.getPackageName());
3458 }
3459 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003460 if (getDevicePolicyManager() != null) {
3461 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3462 msg.arg1);
3463 }
Jason Monk62515be2014-05-21 16:06:19 -04003464 if (mLockTaskNotify == null) {
3465 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3466 }
3467 mLockTaskNotify.show(false);
3468 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003469 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003470 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003471 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003472 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003473 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003474 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003475 new LockPatternUtils(mService.mContext)
3476 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003477 }
3478 } catch (SettingNotFoundException e) {
3479 // No setting, don't lock.
3480 }
justinzhang5286d3f2014-05-12 17:06:01 -04003481 } catch (RemoteException ex) {
3482 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003483 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003484 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003485 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003486 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003487 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3488 if (mLockTaskNotify == null) {
3489 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3490 }
3491 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3492 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003493 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3494 final ActivityContainer container = (ActivityContainer) msg.obj;
3495 final IActivityContainerCallback callback = container.mCallback;
3496 if (callback != null) {
3497 try {
3498 callback.onAllActivitiesComplete(container.asBinder());
3499 } catch (RemoteException e) {
3500 }
3501 }
3502 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003503 case LAUNCH_TASK_BEHIND_COMPLETE: {
3504 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003505 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003506 if (r != null) {
3507 handleLaunchTaskBehindCompleteLocked(r);
3508 }
3509 }
3510 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003511
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003512 }
3513 }
3514 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003515
Ying Wangb081a592014-04-22 15:20:16 -07003516 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003517 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3518 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003519 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003520 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003521 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003522 ActivityRecord mParentActivity = null;
3523 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003524
Craig Mautnere3a00d72014-04-16 08:31:19 -07003525 boolean mVisible = true;
3526
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003529
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003530 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3531 final static int CONTAINER_STATE_NO_SURFACE = 1;
3532 final static int CONTAINER_STATE_FINISHING = 2;
3533 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3534
3535 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 synchronized (mService) {
3537 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003538 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003539 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003540 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003542 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003544 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003545 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003546 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003547 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003548 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003549 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003550 }
3551
3552 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003553 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003555 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3556 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003557 return;
3558 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003559 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 }
3561 }
3562
3563 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003564 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003565 synchronized (mService) {
3566 if (mActivityDisplay != null) {
3567 return mActivityDisplay.mDisplayId;
3568 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 }
3570 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 }
3572
Jeff Brownca9bc702014-02-11 14:32:56 -08003573 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003574 public int getStackId() {
3575 synchronized (mService) {
3576 return mStackId;
3577 }
3578 }
3579
3580 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003581 public boolean injectEvent(InputEvent event) {
3582 final long origId = Binder.clearCallingIdentity();
3583 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003584 synchronized (mService) {
3585 if (mActivityDisplay != null) {
3586 return mInputManagerInternal.injectInputEvent(event,
3587 mActivityDisplay.mDisplayId,
3588 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3589 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003590 }
3591 return false;
3592 } finally {
3593 Binder.restoreCallingIdentity(origId);
3594 }
3595 }
3596
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003597 @Override
3598 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003599 synchronized (mService) {
3600 if (mContainerState == CONTAINER_STATE_FINISHING) {
3601 return;
3602 }
3603 mContainerState = CONTAINER_STATE_FINISHING;
3604
Craig Mautnerd163e752014-06-13 17:18:47 -07003605 long origId = Binder.clearCallingIdentity();
3606 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003607 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003608 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003609 } finally {
3610 Binder.restoreCallingIdentity(origId);
3611 }
3612 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003613 }
3614
Craig Mautner60257702014-09-17 15:02:33 -07003615 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003616 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003617 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003618 if (mActivityDisplay != null) {
3619 mActivityDisplay.detachActivitiesLocked(mStack);
3620 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003621 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003622 }
3623 }
3624
3625 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003627 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 }
3629
3630 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003631 public final int startActivityIntentSender(IIntentSender intentSender)
3632 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003633 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3634
3635 if (!(intentSender instanceof PendingIntentRecord)) {
3636 throw new IllegalArgumentException("Bad PendingIntent object");
3637 }
3638
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003639 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003640 Binder.getCallingUid(), mCurrentUser, false,
3641 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003642
3643 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3644 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3645 pendingIntent.key.requestResolvedType);
3646
3647 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3648 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3649 }
3650
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003651 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003652 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003653 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003654 throw new SecurityException(
3655 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3656 }
3657 }
3658
Craig Mautnerdf88d732014-01-27 09:21:32 -08003659 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003660 public IBinder asBinder() {
3661 return this;
3662 }
3663
Craig Mautner4504de52013-12-20 09:06:56 -08003664 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003665 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003666 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003667 }
3668
Craig Mautner4a1cb222013-12-04 16:14:06 -08003669 ActivityStackSupervisor getOuter() {
3670 return ActivityStackSupervisor.this;
3671 }
3672
Craig Mautnerd163e752014-06-13 17:18:47 -07003673 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003674 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 }
3676
Craig Mautner6985bad2014-04-21 15:22:06 -07003677 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003678 void setVisible(boolean visible) {
3679 if (mVisible != visible) {
3680 mVisible = visible;
3681 if (mCallback != null) {
3682 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3683 0 /* unused */, this).sendToTarget();
3684 }
3685 }
3686 }
3687
Craig Mautner6985bad2014-04-21 15:22:06 -07003688 void setDrawn() {
3689 }
3690
Craig Mautner1b4bf852014-05-26 15:06:32 -07003691 // You can always start a new task on a regular ActivityStack.
3692 boolean isEligibleForNewTasks() {
3693 return true;
3694 }
3695
Craig Mautnerd163e752014-06-13 17:18:47 -07003696 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003697 detachLocked();
3698 deleteActivityContainer(this);
3699 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003700 }
3701
Craig Mautner34b73df2014-01-12 21:11:08 -08003702 @Override
3703 public String toString() {
3704 return mIdString + (mActivityDisplay == null ? "N" : "A");
3705 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003706 }
3707
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003708 private class VirtualActivityContainer extends ActivityContainer {
3709 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003710 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003711
3712 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3713 super(getNextStackId());
3714 mParentActivity = parent;
3715 mCallback = callback;
3716 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003717 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003718 }
3719
3720 @Override
3721 public void setSurface(Surface surface, int width, int height, int density) {
3722 super.setSurface(surface, width, height, density);
3723
3724 synchronized (mService) {
3725 final long origId = Binder.clearCallingIdentity();
3726 try {
3727 setSurfaceLocked(surface, width, height, density);
3728 } finally {
3729 Binder.restoreCallingIdentity(origId);
3730 }
3731 }
3732 }
3733
3734 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3735 if (mContainerState == CONTAINER_STATE_FINISHING) {
3736 return;
3737 }
3738 VirtualActivityDisplay virtualActivityDisplay =
3739 (VirtualActivityDisplay) mActivityDisplay;
3740 if (virtualActivityDisplay == null) {
3741 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003742 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003743 mActivityDisplay = virtualActivityDisplay;
3744 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003745 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003746 }
3747
3748 if (mSurface != null) {
3749 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003750 }
3751
Craig Mautner6985bad2014-04-21 15:22:06 -07003752 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003753 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003754 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003755 } else {
3756 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003757 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003758 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003759 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003760 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003761 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003762
Craig Mautnerd163e752014-06-13 17:18:47 -07003763 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003764
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003765 if (DEBUG_STACK) Slog.d(TAG_STACK,
3766 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003767 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003768
Craig Mautner6985bad2014-04-21 15:22:06 -07003769 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003770 boolean isAttachedLocked() {
3771 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003772 }
3773
3774 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003775 void setDrawn() {
3776 synchronized (mService) {
3777 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003778 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003779 }
3780 }
3781
Craig Mautner1b4bf852014-05-26 15:06:32 -07003782 // Never start a new task on an ActivityView if it isn't explicitly specified.
3783 @Override
3784 boolean isEligibleForNewTasks() {
3785 return false;
3786 }
3787
Craig Mautnerd163e752014-06-13 17:18:47 -07003788 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003789 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003790 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3791 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3792 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3793 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3794 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003795 }
3796 }
3797
Craig Mautner4a1cb222013-12-04 16:14:06 -08003798 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3799 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003800 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003801 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003802 int mDisplayId;
3803 Display mDisplay;
3804 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003805
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3807 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003808 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809
Jose Lima4b6c6692014-08-12 17:41:12 -07003810 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003811
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003812 ActivityDisplay() {
3813 }
Craig Mautner4504de52013-12-20 09:06:56 -08003814
Craig Mautner1a70a162014-09-13 12:09:31 -07003815 // After instantiation, check that mDisplay is not null before using this. The alternative
3816 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003817 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003818 final Display display = mDisplayManager.getDisplay(displayId);
3819 if (display == null) {
3820 return;
3821 }
3822 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003823 }
3824
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003825 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003826 mDisplay = display;
3827 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003829 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003831 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003832 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003833 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3834 + " onTop=" + onTop);
3835 if (onTop) {
3836 mStacks.add(stack);
3837 } else {
3838 mStacks.add(0, stack);
3839 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 }
3841
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003843 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003844 + " from displayId=" + mDisplayId);
3845 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 }
3847
Jose Lima4b6c6692014-08-12 17:41:12 -07003848 void setVisibleBehindActivity(ActivityRecord r) {
3849 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003850 }
3851
Jose Lima4b6c6692014-08-12 17:41:12 -07003852 boolean hasVisibleBehindActivity() {
3853 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003854 }
3855
Craig Mautner34b73df2014-01-12 21:11:08 -08003856 @Override
3857 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003858 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3859 }
3860 }
3861
3862 class VirtualActivityDisplay extends ActivityDisplay {
3863 VirtualDisplay mVirtualDisplay;
3864
Craig Mautner6985bad2014-04-21 15:22:06 -07003865 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003866 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003867 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3868 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3869 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3870 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003871
3872 init(mVirtualDisplay.getDisplay());
3873
3874 mWindowManager.handleDisplayAdded(mDisplayId);
3875 }
3876
3877 void setSurface(Surface surface) {
3878 if (mVirtualDisplay != null) {
3879 mVirtualDisplay.setSurface(surface);
3880 }
3881 }
3882
3883 @Override
3884 void detachActivitiesLocked(ActivityStack stack) {
3885 super.detachActivitiesLocked(stack);
3886 if (mVirtualDisplay != null) {
3887 mVirtualDisplay.release();
3888 mVirtualDisplay = null;
3889 }
3890 }
3891
3892 @Override
3893 public String toString() {
3894 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003895 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003896 }
Jose Lima58e66d62014-05-27 20:00:27 -07003897
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003898 /**
3899 * Adjust bounds to stay within stack bounds.
3900 *
3901 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3902 * that keep them unchanged, but be contained within the stack bounds.
3903 *
3904 * @param bounds Bounds to be adjusted.
3905 * @param stackBounds Bounds within which the other bounds should remain.
3906 */
3907 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3908 if (stackBounds == null || stackBounds.contains(bounds)) {
3909 return;
3910 }
3911
3912 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3913 final int maxRight = stackBounds.right
3914 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3915 int horizontalDiff = stackBounds.left - bounds.left;
3916 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3917 || (bounds.left + horizontalDiff >= maxRight)) {
3918 horizontalDiff = maxRight - bounds.left;
3919 }
3920 bounds.left += horizontalDiff;
3921 bounds.right += horizontalDiff;
3922 }
3923
3924 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3925 final int maxBottom = stackBounds.bottom
3926 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3927 int verticalDiff = stackBounds.top - bounds.top;
3928 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3929 || (bounds.top + verticalDiff >= maxBottom)) {
3930 verticalDiff = maxBottom - bounds.top;
3931 }
3932 bounds.top += verticalDiff;
3933 bounds.bottom += verticalDiff;
3934 }
3935 }
3936
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003937 ActivityStack findStackBehind(ActivityStack stack) {
3938 // TODO(multi-display): We are only looking for stacks on the default display.
3939 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3940 if (display == null) {
3941 return null;
3942 }
3943 final ArrayList<ActivityStack> stacks = display.mStacks;
3944 for (int i = stacks.size() - 1; i >= 0; i--) {
3945 if (stacks.get(i) == stack && i > 0) {
3946 return stacks.get(i - 1);
3947 }
3948 }
3949 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3950 + " in=" + stacks);
3951 }
Craig Mautner27084302013-03-25 08:05:25 -07003952}