blob: 03f5c41ca3492759a85b1c45eabeb50b7c64a381 [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;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800265 /** The number of distinct task ids that can be assigned to the tasks of a single user */
266 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700267
Craig Mautner27084302013-03-25 08:05:25 -0700268 final ActivityManagerService mService;
269
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800270 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800271
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700272 final ActivityStackSupervisorHandler mHandler;
273
274 /** Short cut */
275 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700277
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700278 /** Counter for next free stack ID to use for dynamic activity stacks. */
279 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700280
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800281 /**
282 * Maps the task identifier that activities are currently being started in to the userId of the
283 * task. Each time a new task is created, the entry for the userId of the task is incremented
284 */
285 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700286
Craig Mautner2420ead2013-04-01 17:13:20 -0700287 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800288 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700289
Craig Mautnere0a38842013-12-16 16:14:02 -0800290 /** The stack containing the launcher app. Assumed to always be attached to
291 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800292 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700293
Craig Mautnere0a38842013-12-16 16:14:02 -0800294 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800295 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700296
Craig Mautner4a1cb222013-12-04 16:14:06 -0800297 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
298 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800299 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800300 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301
302 /** List of activities that are waiting for a new activity to become visible before completing
303 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800304 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700305
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700306 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800307 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700308
309 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700311
Craig Mautnerde4ef022013-04-07 19:01:33 -0700312 /** List of activities that are ready to be stopped, but waiting for the next activity to
313 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700315
Craig Mautnerf3333272013-04-22 10:55:53 -0700316 /** List of activities that are ready to be finished, but waiting for the previous activity to
317 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700319
Craig Mautner0eea92c2013-05-16 13:35:39 -0700320 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800321 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322
Craig Mautnerf3333272013-04-22 10:55:53 -0700323 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700324 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700325
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700326 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700327 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700328
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
330 * is being brought in front of us. */
331 boolean mUserLeaving = false;
332
Craig Mautner0eea92c2013-05-16 13:35:39 -0700333 /** Set when we have taken too long waiting to go to sleep. */
334 boolean mSleepTimeout = false;
335
336 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700337 * We don't want to allow the device to go to sleep while in the process
338 * of launching an activity. This is primarily to allow alarm intent
339 * receivers to launch an activity and get that to run before the device
340 * goes back to sleep.
341 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700342 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700343
344 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345 * Set when the system is going to sleep, until we have
346 * successfully paused the current activity and released our wake lock.
347 * At that point the system is allowed to actually sleep.
348 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700349 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700350
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700351 /** Stack id of the front stack when user switched, indexed by userId. */
352 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700353
Craig Mautner4504de52013-12-20 09:06:56 -0800354 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800355 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700356 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357
358 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700359 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360
Jeff Brownca9bc702014-02-11 14:32:56 -0800361 InputManagerInternal mInputManagerInternal;
362
Craig Mautner15df08a2015-04-01 12:17:18 -0700363 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
364 * may be finished until there is only one entry left. If this is empty the system is not
365 * in lockTask mode. */
366 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000367 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700368 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
369 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000370 */
371 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400372 /**
373 * Notifies the user when entering/exiting lock-task.
374 */
375 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800376
Wale Ogunwaled046a012015-12-24 13:05:59 -0800377 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800378 boolean inResumeTopActivity;
379
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700380 // 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 -0700381 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700382 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700383
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700384 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
385 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800386 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700387
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700388 // The default minimal size that will be used if the activity doesn't specify its minimal size.
389 // It will be calculated when the default display gets added.
390 private int mDefaultMinimalSizeOfResizeableTask = -1;
391
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700392 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
393 private boolean mTaskLayersChanged = true;
394
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800395 private final ActivityMetricsLogger mActivityMetricsLogger;
396
Jorim Jaggidc249c42015-12-15 14:57:31 -0800397 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
398
Wale Ogunwale39381972015-12-17 17:15:29 -0800399 static class FindTaskResult {
400 ActivityRecord r;
401 boolean matchedByRootAffinity;
402 }
403 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
404
Craig Mautneree36c772014-07-16 14:56:05 -0700405 /**
406 * Description of a request to start a new activity, which has been held
407 * due to app switches being disabled.
408 */
409 static class PendingActivityLaunch {
410 final ActivityRecord r;
411 final ActivityRecord sourceRecord;
412 final int startFlags;
413 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700414 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700415
416 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700417 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700418 r = _r;
419 sourceRecord = _sourceRecord;
420 startFlags = _startFlags;
421 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700422 callerApp = _callerApp;
423 }
424
425 void sendErrorResult(String message) {
426 try {
427 if (callerApp.thread != null) {
428 callerApp.thread.scheduleCrash(message);
429 }
430 } catch (RemoteException e) {
431 Slog.e(TAG, "Exception scheduling crash of failed "
432 + "activity launcher sourceRecord=" + sourceRecord, e);
433 }
Craig Mautneree36c772014-07-16 14:56:05 -0700434 }
435 }
436
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800437 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700438 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700439 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800440 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800441 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700442 }
443
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800444 void setRecentTasks(RecentTasks recentTasks) {
445 mRecentTasks = recentTasks;
446 }
447
Jeff Brown2c43c332014-06-12 22:38:59 -0700448 /**
449 * At the time when the constructor runs, the power manager has not yet been
450 * initialized. So we initialize our wakelocks afterwards.
451 */
452 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700454 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700455 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700456 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700457 }
458
justinzhang5286d3f2014-05-12 17:06:01 -0400459 // This function returns a IStatusBarService. The value is from ServiceManager.
460 // getService and is cached.
461 private IStatusBarService getStatusBarService() {
462 synchronized (mService) {
463 if (mStatusBarService == null) {
464 mStatusBarService = IStatusBarService.Stub.asInterface(
465 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
466 if (mStatusBarService == null) {
467 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
468 }
469 }
470 return mStatusBarService;
471 }
472 }
473
Jason Monk35c62a42014-06-17 10:24:47 -0400474 private IDevicePolicyManager getDevicePolicyManager() {
475 synchronized (mService) {
476 if (mDevicePolicyManager == null) {
477 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
478 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
479 if (mDevicePolicyManager == null) {
480 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
481 }
482 }
483 return mDevicePolicyManager;
484 }
485 }
486
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700487 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 synchronized (mService) {
489 mWindowManager = wm;
490
491 mDisplayManager =
492 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
493 mDisplayManager.registerDisplayListener(this, null);
494
495 Display[] displays = mDisplayManager.getDisplays();
496 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
497 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800498 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700499 if (activityDisplay.mDisplay == null) {
500 throw new IllegalStateException("Default Display does not exist");
501 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800502 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700503 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 }
505
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800506 mHomeStack = mFocusedStack = mLastFocusedStack =
507 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800508
509 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 }
Craig Mautner27084302013-03-25 08:05:25 -0700511 }
512
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200513 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700514 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200515 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700516 }
517
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700518 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800519 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700520 }
521
Craig Mautnerde4ef022013-04-07 19:01:33 -0700522 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700524 }
525
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700526 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700527 if (stack == null) {
528 return false;
529 }
530
Craig Mautnerdf88d732014-01-27 09:21:32 -0800531 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
532 if (parent != null) {
533 stack = parent.task.stack;
534 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800535 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700536 }
537
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700538 /** The top most stack. */
539 boolean isFrontStack(ActivityStack stack) {
540 if (stack == null) {
541 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700543
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700544 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
545 if (parent != null) {
546 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800547 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700548 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
549 }
550
Wale Ogunwaled046a012015-12-24 13:05:59 -0800551 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800552 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
553 if (!focusCandidate.isFocusable()) {
554 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
555 focusCandidate = focusCandidate.getNextFocusableStackLocked();
556 }
557
558 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800559 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800560 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800561
Wale Ogunwaled046a012015-12-24 13:05:59 -0800562 EventLogTags.writeAmFocusedStack(
563 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
564 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
565 }
566
567 final ActivityRecord r = topRunningActivityLocked();
568 if (mService.mFocusedActivity != r) {
569 // The focus activity should always be the top activity in the focused stack.
570 // There will be chaos and anarchy if it isn't...
571 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
572 }
Craig Mautnerde313752015-01-22 14:28:03 -0800573
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800574 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800575 if (r != null && r.idle) {
576 checkFinishBootingLocked();
577 }
578 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700579 }
580
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700581 void moveHomeStackToFront(String reason) {
582 mHomeStack.moveToFront(reason);
583 }
584
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700585 /** Returns true if the focus activity was adjusted to the home stack top activity. */
586 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700587 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
588 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700589 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700590 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700591
Craig Mautner84984fa2014-06-19 11:19:20 -0700592 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700593
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700594 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700595 if (top == null) {
596 return false;
597 }
598 mService.setFocusedActivityLocked(top, reason);
599 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700600 }
601
Craig Mautner299f9602015-01-26 09:47:33 -0800602 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700603 if (!mService.mBooting && !mService.mBooted) {
604 // Not ready yet!
605 return false;
606 }
607
Craig Mautner84984fa2014-06-19 11:19:20 -0700608 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
609 mWindowManager.showRecentApps();
610 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700611 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700612
Craig Mautner84984fa2014-06-19 11:19:20 -0700613 if (prev != null) {
614 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
615 }
616
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700617 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
618 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800619 final String myReason = reason + " resumeHomeStackTask";
620
Mark Lua56ea122015-10-08 13:31:01 +0800621 // Only resume home activity if isn't finishing.
622 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800623 mService.setFocusedActivityLocked(r, myReason);
624 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700625 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800626 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700627 }
628
Craig Mautner8d341ef2013-03-26 09:03:27 -0700629 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700630 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700631 }
632
633 /**
634 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
635 * @param id Id of the task we would like returned.
636 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
637 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700638 * @param stackId The stack to restore the task to (default launch stack will be used if
639 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700640 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700641 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800642 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800644 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
646 ActivityStack stack = stacks.get(stackNdx);
647 TaskRecord task = stack.taskForIdLocked(id);
648 if (task != null) {
649 return task;
650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700651 }
652 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800653
654 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700655 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800656 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800657 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700658 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800659 return null;
660 }
661
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700662 if (!restoreFromRecents) {
663 return task;
664 }
665
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700666 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700667 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
668 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800669 return null;
670 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700671 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800672 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700673 }
674
Craig Mautner6170f732013-04-02 13:05:23 -0700675 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800676 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800678 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
680 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
681 if (r != null) {
682 return r;
683 }
Craig Mautner6170f732013-04-02 13:05:23 -0700684 }
685 }
686 return null;
687 }
688
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800689 void setNextTaskIdForUserLocked(int taskId, int userId) {
690 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
691 if (taskId > currentTaskId) {
692 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700693 }
694 }
695
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800696 int getNextTaskIdForUserLocked(int userId) {
697 mRecentTasks.loadUserRecentsLocked(userId);
698 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
699 // for a userId u, a taskId can only be in the range
700 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
701 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
702 int candidateTaskId = currentTaskId;
703 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
704 INVALID_STACK_ID) != null) {
705 candidateTaskId++;
706 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
707 // Wrap around as there will be smaller task ids that are available now.
708 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700709 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800710 if (candidateTaskId == currentTaskId) {
711 // Something wrong!
712 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
713 throw new IllegalStateException("Cannot get an available task id."
714 + " Reached limit of " + MAX_TASK_IDS_PER_USER
715 + " running tasks per user.");
716 }
717 }
718 mCurTaskIdForUser.put(userId, candidateTaskId);
719 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700720 }
721
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800723 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700724 if (stack == null) {
725 return null;
726 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700727 ActivityRecord resumedActivity = stack.mResumedActivity;
728 if (resumedActivity == null || resumedActivity.app == null) {
729 resumedActivity = stack.mPausingActivity;
730 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700731 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700732 }
733 }
734 return resumedActivity;
735 }
736
Dianne Hackbornff072722014-09-24 10:56:28 -0700737 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700738 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
741 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
743 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700744 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 continue;
746 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700747 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 if (hr != null) {
749 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
750 && processName.equals(hr.processName)) {
751 try {
George Mount2c92c972014-03-20 09:38:23 -0700752 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 didSomething = true;
754 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700755 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 Slog.w(TAG, "Exception in new application when starting activity "
757 + hr.intent.getComponent().flattenToShortString(), e);
758 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700759 }
Craig Mautner20e72272013-04-01 13:45:53 -0700760 }
Craig Mautner20e72272013-04-01 13:45:53 -0700761 }
762 }
763 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700764 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700765 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700766 }
Craig Mautner20e72272013-04-01 13:45:53 -0700767 return didSomething;
768 }
769
770 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800771 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
772 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
774 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700775 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 continue;
777 }
778 final ActivityRecord resumedActivity = stack.mResumedActivity;
779 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700780 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800781 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 return false;
783 }
Craig Mautner20e72272013-04-01 13:45:53 -0700784 }
785 }
786 return true;
787 }
788
Craig Mautnerde4ef022013-04-07 19:01:33 -0700789 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
791 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
793 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700794 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800795 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700796 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 return false;
798 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 }
800 }
801 }
802 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700803 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
805 mLastFocusedStack + " to=" + mFocusedStack);
806 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700807 return true;
808 }
809
810 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800811 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
813 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800814 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
815 final ActivityStack stack = stacks.get(stackNdx);
816 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800817 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700818 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800819 return false;
820 }
821 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700823 }
824 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800825 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700826 }
827
Craig Mautner2acc3892013-09-23 10:28:14 -0700828 /**
829 * Pause all activities in either all of the stacks or just the back stacks.
830 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700831 * @return true if any activity was paused as a result of this call.
832 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700833 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700834 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800835 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
836 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
838 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700839 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700840 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700842 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
843 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700845 }
846 }
847 return someActivityPaused;
848 }
849
Craig Mautnerde4ef022013-04-07 19:01:33 -0700850 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700851 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
853 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
855 final ActivityStack stack = stacks.get(stackNdx);
856 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700857 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700859 Slog.d(TAG_STATES,
860 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 pausing = false;
862 } else {
863 return false;
864 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700865 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700866 }
867 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700868 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700869 }
870
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700871 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
872 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500873 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800874 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
875 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
876 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
877 final ActivityStack stack = stacks.get(stackNdx);
878 if (stack.mResumedActivity != null &&
879 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700880 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800881 }
882 }
883 }
884 }
885
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700886 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700887 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700888 }
889
890 void sendWaitingVisibleReportLocked(ActivityRecord r) {
891 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700892 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700893 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700894 if (w.who == null) {
895 changed = true;
896 w.timeout = false;
897 if (r != null) {
898 w.who = new ComponentName(r.info.packageName, r.info.name);
899 }
900 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
901 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700903 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700904 if (changed) {
905 mService.notifyAll();
906 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700907 }
908
909 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
910 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700911 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700912 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700913 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700914 if (w.who == null) {
915 changed = true;
916 w.timeout = timeout;
917 if (r != null) {
918 w.who = new ComponentName(r.info.packageName, r.info.name);
919 }
920 w.thisTime = thisTime;
921 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700922 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700923 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700924 if (changed) {
925 mService.notifyAll();
926 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700927 }
928
Craig Mautner29219d92013-04-16 20:19:12 -0700929 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800930 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700931 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700932 if (r != null) {
933 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700934 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700935
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800937 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
939 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800940 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700941 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700942 if (r != null) {
943 return r;
944 }
945 }
946 }
947 return null;
948 }
949
Dianne Hackborn09233282014-04-30 11:33:59 -0700950 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700951 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
953 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800956 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
958 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700959 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700961 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700962 }
963 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700964
965 // The lists are already sorted from most recent to oldest. Just pull the most recent off
966 // each list and add it to list. Stop when all lists are empty or maxNum reached.
967 while (maxNum > 0) {
968 long mostRecentActiveTime = Long.MIN_VALUE;
969 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 final int numTaskLists = runningTaskLists.size();
971 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
972 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700973 if (!stackTaskList.isEmpty()) {
974 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
975 if (lastActiveTime > mostRecentActiveTime) {
976 mostRecentActiveTime = lastActiveTime;
977 selectedStackList = stackTaskList;
978 }
979 }
980 }
981 if (selectedStackList != null) {
982 list.add(selectedStackList.remove(0));
983 --maxNum;
984 } else {
985 break;
986 }
987 }
Craig Mautner20e72272013-04-01 13:45:53 -0700988 }
989
Todd Kennedy7440f172015-12-09 14:31:22 -0800990 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
991 ProfilerInfo profilerInfo) {
992 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700993 if (aInfo != null) {
994 // Store the found target back into the intent, because now that
995 // we have it we never want to do this again. For example, if the
996 // user navigates back to this point in the history, we should
997 // always restart the exact same activity.
998 intent.setComponent(new ComponentName(
999 aInfo.applicationInfo.packageName, aInfo.name));
1000
1001 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001002 if (!aInfo.processName.equals("system")) {
1003 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 mService.setDebugApp(aInfo.processName, true, false);
1005 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006
Man Caocfa78b22015-06-11 20:14:34 -07001007 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1008 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1009 }
1010
1011 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001012 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013 }
1014 }
1015 }
1016 return aInfo;
1017 }
1018
Todd Kennedy7440f172015-12-09 14:31:22 -08001019 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1020 try {
1021 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1022 PackageManager.MATCH_DEFAULT_ONLY
1023 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1024 } catch (RemoteException e) {
1025 }
1026 return null;
1027 }
1028
1029 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1030 ProfilerInfo profilerInfo, int userId) {
1031 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1032 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1033 }
1034
Craig Mautner2420ead2013-04-01 17:13:20 -07001035 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001036 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001037 throws RemoteException {
1038
Craig Mautner2568c3a2015-03-26 14:22:34 -07001039 if (andResume) {
1040 r.startFreezingScreenLocked(app, 0);
1041 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001042
Craig Mautner2568c3a2015-03-26 14:22:34 -07001043 // schedule launch ticks to collect information about slow apps.
1044 r.startLaunchTickingLocked();
1045 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001046
1047 // Have the window manager re-evaluate the orientation of
1048 // the screen based on the new activity order. Note that
1049 // as a result of this, it can call back into the activity
1050 // manager with a new orientation. We don't care about that,
1051 // because the activity is not currently running so we are
1052 // just restarting it anyway.
1053 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001054 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001055 mService.mConfiguration,
1056 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001057 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001058 }
1059
1060 r.app = app;
1061 app.waitingToKill = null;
1062 r.launchCount++;
1063 r.lastLaunchTime = SystemClock.uptimeMillis();
1064
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001065 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001066
1067 int idx = app.activities.indexOf(r);
1068 if (idx < 0) {
1069 app.activities.add(r);
1070 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001071 mService.updateLruProcessLocked(app, true, null);
1072 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001073
Craig Mautner15df08a2015-04-01 12:17:18 -07001074 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001075 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1076 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001077 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001078 }
1079
1080 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001081 try {
1082 if (app.thread == null) {
1083 throw new RemoteException();
1084 }
1085 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001086 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001087 if (andResume) {
1088 results = r.results;
1089 newIntents = r.newIntents;
1090 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001091 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1092 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1093 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001094 if (andResume) {
1095 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1096 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001097 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001098 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001099 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001100 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001101 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001102 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001103 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001104 r.sleeping = false;
1105 r.forceNewConfig = false;
1106 mService.showAskCompatModeDialogLocked(r);
1107 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001108 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001109 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1110 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1111 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001112 final String profileFile = mService.mProfileFile;
1113 if (profileFile != null) {
1114 ParcelFileDescriptor profileFd = mService.mProfileFd;
1115 if (profileFd != null) {
1116 try {
1117 profileFd = profileFd.dup();
1118 } catch (IOException e) {
1119 if (profileFd != null) {
1120 try {
1121 profileFd.close();
1122 } catch (IOException o) {
1123 }
1124 profileFd = null;
1125 }
1126 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001127 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001128
1129 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1130 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001131 }
1132 }
1133 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001134
Craig Mautner2568c3a2015-03-26 14:22:34 -07001135 if (andResume) {
1136 app.hasShownUi = true;
1137 app.pendingUiClean = true;
1138 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001139 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001140 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001141 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001142 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001143 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001144 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001145
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001146 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001147 // This may be a heavy-weight process! Note that the package
1148 // manager will ensure that only activity can run in the main
1149 // process of the .apk, which is the only thing that will be
1150 // considered heavy-weight.
1151 if (app.processName.equals(app.info.packageName)) {
1152 if (mService.mHeavyWeightProcess != null
1153 && mService.mHeavyWeightProcess != app) {
1154 Slog.w(TAG, "Starting new heavy weight process " + app
1155 + " when already running "
1156 + mService.mHeavyWeightProcess);
1157 }
1158 mService.mHeavyWeightProcess = app;
1159 Message msg = mService.mHandler.obtainMessage(
1160 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1161 msg.obj = r;
1162 mService.mHandler.sendMessage(msg);
1163 }
1164 }
1165
1166 } catch (RemoteException e) {
1167 if (r.launchFailed) {
1168 // This is the second time we failed -- finish activity
1169 // and give up.
1170 Slog.e(TAG, "Second failure launching "
1171 + r.intent.getComponent().flattenToShortString()
1172 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001173 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001174 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1175 "2nd-crash", false);
1176 return false;
1177 }
1178
1179 // This is the first time we failed -- restart process and
1180 // retry.
1181 app.activities.remove(r);
1182 throw e;
1183 }
1184
1185 r.launchFailed = false;
1186 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001187 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 }
1189
1190 if (andResume) {
1191 // As part of the process of launching, ActivityThread also performs
1192 // a resume.
1193 stack.minimalResumeActivityLocked(r);
1194 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001195 // This activity is not starting in the resumed state... which should look like we asked
1196 // it to pause+stop (but remain visible), and it has done so and reported back the
1197 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001198 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001199 "Moving to PAUSED: " + r + " (starting in paused state)");
1200 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 }
1202
1203 // Launch the new version setup screen if needed. We do this -after-
1204 // launching the initial activity (that is, home), so that it can have
1205 // a chance to initialize itself while in the background, making the
1206 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001207 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 mService.startSetupActivityLocked();
1209 }
1210
Dianne Hackborn465fa392014-09-14 14:21:18 -07001211 // Update any services we are bound to that might care about whether
1212 // their client may have activities.
1213 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1214
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 return true;
1216 }
1217
Craig Mautnere79d42682013-04-01 19:01:53 -07001218 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001219 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001220 // Is this activity's application already running?
1221 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001222 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001223
1224 r.task.stack.setLaunchTime(r);
1225
1226 if (app != null && app.thread != null) {
1227 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001228 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1229 || !"android".equals(r.info.packageName)) {
1230 // Don't add this if it is a platform component that is marked
1231 // to run in multiple processes, because this is actually
1232 // part of the framework so doesn't make sense to track as a
1233 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001234 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1235 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001236 }
George Mount2c92c972014-03-20 09:38:23 -07001237 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001238 return;
1239 } catch (RemoteException e) {
1240 Slog.w(TAG, "Exception when starting activity "
1241 + r.intent.getComponent().flattenToShortString(), e);
1242 }
1243
1244 // If a dead object exception was thrown -- fall through to
1245 // restart the application.
1246 }
1247
1248 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001249 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001250 }
1251
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001252 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001253 String resultWho, int requestCode, int callingPid, int callingUid,
1254 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1255 ActivityRecord resultRecord, ActivityStack resultStack) {
1256 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1257 callingUid);
1258 if (startAnyPerm == PERMISSION_GRANTED) {
1259 return true;
1260 }
1261 final int componentRestriction = getComponentRestrictionForCallingPackage(
1262 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1263 final int actionRestriction = getActionRestrictionForCallingPackage(
1264 intent.getAction(), callingPackage, callingPid, callingUid);
1265 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1266 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1267 if (resultRecord != null) {
1268 resultStack.sendActivityResultLocked(-1,
1269 resultRecord, resultWho, requestCode,
1270 Activity.RESULT_CANCELED, null);
1271 }
1272 final String msg;
1273 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1274 msg = "Permission Denial: starting " + intent.toString()
1275 + " from " + callerApp + " (pid=" + callingPid
1276 + ", uid=" + callingUid + ")" + " with revoked permission "
1277 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1278 } else if (!aInfo.exported) {
1279 msg = "Permission Denial: starting " + intent.toString()
1280 + " from " + callerApp + " (pid=" + callingPid
1281 + ", uid=" + callingUid + ")"
1282 + " not exported from uid " + aInfo.applicationInfo.uid;
1283 } else {
1284 msg = "Permission Denial: starting " + intent.toString()
1285 + " from " + callerApp + " (pid=" + callingPid
1286 + ", uid=" + callingUid + ")"
1287 + " requires " + aInfo.permission;
1288 }
1289 Slog.w(TAG, msg);
1290 throw new SecurityException(msg);
1291 }
1292
1293 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1294 final String message = "Appop Denial: starting " + intent.toString()
1295 + " from " + callerApp + " (pid=" + callingPid
1296 + ", uid=" + callingUid + ")"
1297 + " requires " + AppOpsManager.permissionToOp(
1298 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1299 Slog.w(TAG, message);
1300 return false;
1301 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1302 final String message = "Appop Denial: starting " + intent.toString()
1303 + " from " + callerApp + " (pid=" + callingPid
1304 + ", uid=" + callingUid + ")"
1305 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1306 Slog.w(TAG, message);
1307 return false;
1308 }
1309 return true;
1310 }
1311
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001312 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001313 final long identity = Binder.clearCallingIdentity();
1314 try {
1315 return UserManager.get(mService.mContext).getUserInfo(userId);
1316 } finally {
1317 Binder.restoreCallingIdentity(identity);
1318 }
1319 }
1320
Svet Ganov99b60432015-06-27 13:15:22 -07001321 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001322 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001323 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1324 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001325 == PackageManager.PERMISSION_DENIED) {
1326 return ACTIVITY_RESTRICTION_PERMISSION;
1327 }
1328
Christopher Tateff7add02015-08-17 10:23:22 -07001329 if (activityInfo.permission == null) {
1330 return ACTIVITY_RESTRICTION_NONE;
1331 }
1332
Svet Ganov99b60432015-06-27 13:15:22 -07001333 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1334 if (opCode == AppOpsManager.OP_NONE) {
1335 return ACTIVITY_RESTRICTION_NONE;
1336 }
1337
1338 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1339 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001340 if (!ignoreTargetSecurity) {
1341 return ACTIVITY_RESTRICTION_APPOP;
1342 }
Svet Ganov99b60432015-06-27 13:15:22 -07001343 }
1344
1345 return ACTIVITY_RESTRICTION_NONE;
1346 }
1347
Svetoslav7008b512015-06-24 18:47:07 -07001348 private int getActionRestrictionForCallingPackage(String action,
1349 String callingPackage, int callingPid, int callingUid) {
1350 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001351 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001352 }
1353
1354 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1355 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001356 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001357 }
1358
1359 final PackageInfo packageInfo;
1360 try {
1361 packageInfo = mService.mContext.getPackageManager()
1362 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1363 } catch (PackageManager.NameNotFoundException e) {
1364 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001365 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001366 }
1367
1368 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001369 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001370 }
1371
1372 if (mService.checkPermission(permission, callingPid, callingUid) ==
1373 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001374 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001375 }
1376
1377 final int opCode = AppOpsManager.permissionToOpCode(permission);
1378 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001379 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001380 }
1381
1382 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1383 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001384 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001385 }
1386
Svet Ganov99b60432015-06-27 13:15:22 -07001387 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001388 }
1389
Wale Ogunwaled046a012015-12-24 13:05:59 -08001390 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001391 if (r == null) {
1392 // Not sure what you are trying to do, but it is not going to work...
1393 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001394 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001395 final TaskRecord task = r.task;
1396 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001397 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001398 return false;
1399 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001400 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001401 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001402 }
1403
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001404 void setLaunchSource(int uid) {
1405 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1406 }
1407
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001408 void acquireLaunchWakelock() {
1409 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1410 throw new IllegalStateException("Calling must be system uid");
1411 }
1412 mLaunchingActivity.acquire();
1413 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1414 // To be safe, don't allow the wake lock to be held for too long.
1415 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1416 }
1417 }
1418
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001419 /**
1420 * Called when the frontmost task is idle.
1421 * @return the state of mService.mBooting before this was called.
1422 */
1423 private boolean checkFinishBootingLocked() {
1424 final boolean booting = mService.mBooting;
1425 boolean enableScreen = false;
1426 mService.mBooting = false;
1427 if (!mService.mBooted) {
1428 mService.mBooted = true;
1429 enableScreen = true;
1430 }
1431 if (booting || enableScreen) {
1432 mService.postFinishBooting(booting, enableScreen);
1433 }
1434 return booting;
1435 }
1436
Craig Mautnerf3333272013-04-22 10:55:53 -07001437 // Checked.
1438 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1439 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001440 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001441
Craig Mautnerf3333272013-04-22 10:55:53 -07001442 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001443 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001444 int NS = 0;
1445 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001446 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001447 boolean activityRemoved = false;
1448
Wale Ogunwale7d701172015-03-11 15:36:30 -07001449 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001450 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001451 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1452 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001453 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1454 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001455 if (fromTimeout) {
1456 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001457 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001458
1459 // This is a hack to semi-deal with a race condition
1460 // in the client where it can be constructed with a
1461 // newer configuration from when we asked it to launch.
1462 // We'll update with whatever configuration it now says
1463 // it used to launch.
1464 if (config != null) {
1465 r.configuration = config;
1466 }
1467
1468 // We are now idle. If someone is waiting for a thumbnail from
1469 // us, we can now deliver.
1470 r.idle = true;
1471
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001472 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001473 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001474 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001475 }
1476 }
1477
1478 if (allResumedActivitiesIdle()) {
1479 if (r != null) {
1480 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001481 }
1482
1483 if (mLaunchingActivity.isHeld()) {
1484 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1485 if (VALIDATE_WAKE_LOCK_CALLER &&
1486 Binder.getCallingUid() != Process.myUid()) {
1487 throw new IllegalStateException("Calling must be system uid");
1488 }
1489 mLaunchingActivity.release();
1490 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001491 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001492 }
1493
1494 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001495 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001496 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001497 if ((NF = mFinishingActivities.size()) > 0) {
1498 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001499 mFinishingActivities.clear();
1500 }
1501
Craig Mautnerf3333272013-04-22 10:55:53 -07001502 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001503 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001504 mStartingUsers.clear();
1505 }
1506
Craig Mautnerf3333272013-04-22 10:55:53 -07001507 // Stop any activities that are scheduled to do so but have been
1508 // waiting for the next one to start.
1509 for (int i = 0; i < NS; i++) {
1510 r = stops.get(i);
1511 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001512 if (stack != null) {
1513 if (r.finishing) {
1514 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1515 } else {
1516 stack.stopActivityLocked(r);
1517 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001518 }
1519 }
1520
1521 // Finish any activities that are scheduled to do so but have been
1522 // waiting for the next one to start.
1523 for (int i = 0; i < NF; i++) {
1524 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001525 final ActivityStack stack = r.task.stack;
1526 if (stack != null) {
1527 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1528 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001529 }
1530
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001531 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001532 // Complete user switch
1533 if (startingUsers != null) {
1534 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001535 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001536 }
1537 }
1538 // Complete starting up of background users
1539 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001540 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001541 mStartingBackgroundUsers.clear();
1542 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001543 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001544 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001545 }
1546 }
1547
1548 mService.trimApplications();
1549 //dump();
1550 //mWindowManager.dump();
1551
Craig Mautnerf3333272013-04-22 10:55:53 -07001552 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001553 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001554 }
1555
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001556 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001557 }
1558
Craig Mautner8e569572013-10-11 17:36:59 -07001559 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001560 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001561 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1562 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001563 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1564 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1565 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001566 }
Craig Mautner19091252013-10-05 00:03:53 -07001567 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001568 }
1569
1570 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1572 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001573 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1574 stacks.get(stackNdx).closeSystemDialogsLocked();
1575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001576 }
1577 }
1578
Craig Mautner93529a42013-10-04 15:03:13 -07001579 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001580 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001581 }
1582
Craig Mautner8d341ef2013-03-26 09:03:27 -07001583 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001584 * Update the last used stack id for non-current user (current user's last
1585 * used stack is the focused stack)
1586 */
1587 void updateUserStackLocked(int userId, ActivityStack stack) {
1588 if (userId != mCurrentUser) {
1589 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1590 }
1591 }
1592
1593 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001594 * @return true if some activity was finished (or would have finished if doit were true).
1595 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001596 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1597 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001598 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001599 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1600 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001601 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001602 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001603 if (stack.finishDisabledPackageActivitiesLocked(
1604 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001605 didSomething = true;
1606 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001607 }
1608 }
1609 return didSomething;
1610 }
1611
Dianne Hackborna413dc02013-07-12 12:02:55 -07001612 void updatePreviousProcessLocked(ActivityRecord r) {
1613 // Now that this process has stopped, we may want to consider
1614 // it to be the previous app to try to keep around in case
1615 // the user wants to return to it.
1616
1617 // First, found out what is currently the foreground app, so that
1618 // we don't blow away the previous app if this activity is being
1619 // hosted by the process that is actually still the foreground.
1620 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001621 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1622 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001623 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1624 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001625 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001626 if (stack.mResumedActivity != null) {
1627 fgApp = stack.mResumedActivity.app;
1628 } else if (stack.mPausingActivity != null) {
1629 fgApp = stack.mPausingActivity.app;
1630 }
1631 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001632 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001633 }
1634 }
1635
1636 // Now set this one as the previous process, only if that really
1637 // makes sense to.
1638 if (r.app != null && fgApp != null && r.app != fgApp
1639 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001640 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001641 mService.mPreviousProcess = r.app;
1642 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1643 }
1644 }
1645
Wale Ogunwaled046a012015-12-24 13:05:59 -08001646 boolean resumeFocusedStackTopActivityLocked() {
1647 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001648 }
1649
Wale Ogunwaled046a012015-12-24 13:05:59 -08001650 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001651 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001652 if (targetStack != null && isFocusedStack(targetStack)) {
1653 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001654 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001655 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1656 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001657 }
1658
Todd Kennedy539db512014-12-15 09:57:55 -08001659 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1661 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001662 final int numStacks = stacks.size();
1663 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1664 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001665 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001666 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001667 }
1668 }
1669
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001670 void finishVoiceTask(IVoiceInteractionSession session) {
1671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1673 final int numStacks = stacks.size();
1674 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1675 final ActivityStack stack = stacks.get(stackNdx);
1676 stack.finishVoiceTask(session);
1677 }
1678 }
1679 }
1680
Chong Zhang280d3322015-11-03 17:27:26 -08001681 void findTaskToMoveToFrontLocked(
1682 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001683 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1684 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001685 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001686 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1687 // Caller wants the home activity moved with it. To accomplish this,
1688 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001689 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001690 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001691 if (task.stack == null) {
1692 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1693 + task + " to front. Stack is null");
1694 return;
1695 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001696
Chong Zhang0fa656b2015-08-31 15:17:21 -07001697 if (task.mResizeable && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001698 int stackId = options.getLaunchStackId();
1699 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001700 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001701 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001702 if (stackId == INVALID_STACK_ID) {
1703 stackId = task.getLaunchStackId();
1704 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001705 if (stackId != task.stack.mStackId) {
1706 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1707 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1708 // still need moveTaskToFrontLocked() below for any transition settings.
1709 }
1710 // WM resizeTask must be done after the task is moved to the correct stack,
1711 // because Task's setBounds() also updates dim layer's bounds, but that has
1712 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07001713 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1714 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001715 }
1716 }
1717
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001718 final ActivityRecord r = task.getTopActivity();
1719 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1720 r == null ? null : r.appTimeTracker, reason);
1721
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001722 if (DEBUG_STACK) Slog.d(TAG_STACK,
1723 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001724 }
1725
Wale Ogunwale854809c2015-12-27 16:18:19 -08001726 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001727 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001728 // window management or is launching into the pinned stack.
1729 if (!options.hasLaunchBounds()) {
1730 return false;
1731 }
1732 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1733 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001734 }
1735
Craig Mautner967212c2013-04-13 21:10:58 -07001736 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001737 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001738 }
1739
1740 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001741 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1742 if (activityContainer != null) {
1743 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001744 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001745 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001746 return null;
1747 }
1748 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001749 }
1750
Craig Mautner967212c2013-04-13 21:10:58 -07001751 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001752 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1754 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001755 }
1756 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001757 }
1758
Craig Mautner4a1cb222013-12-04 16:14:06 -08001759 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001760 ActivityRecord homeActivity = getHomeActivity();
1761 if (homeActivity != null) {
1762 return homeActivity.appToken;
1763 }
1764 return null;
1765 }
1766
1767 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001768 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001769 }
1770
1771 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001772 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1773 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1774 final TaskRecord task = tasks.get(taskNdx);
1775 if (task.isHomeTask()) {
1776 final ArrayList<ActivityRecord> activities = task.mActivities;
1777 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1778 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001779 if (r.isHomeActivity()
1780 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001781 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001782 }
1783 }
1784 }
1785 }
1786 return null;
1787 }
1788
Chong Zhangb15758a2015-11-17 12:12:03 -08001789 /**
1790 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1791 * the docked stack itself, or if it's side-by-side to the docked stack.
1792 */
1793 boolean isStackDockedInEffect(int stackId) {
1794 return stackId == DOCKED_STACK_ID ||
1795 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1796 }
1797
Todd Kennedyca4d8422015-01-15 15:19:22 -08001798 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001799 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001800 ActivityContainer activityContainer =
1801 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001802 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001803 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1804 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001805 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001806 return activityContainer;
1807 }
1808
Craig Mautner34b73df2014-01-12 21:11:08 -08001809 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001810 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1811 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1812 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001813 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1814 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001815 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001816 }
1817 }
1818
Craig Mautner95da1082014-02-24 17:54:35 -08001819 void deleteActivityContainer(IActivityContainer container) {
1820 ActivityContainer activityContainer = (ActivityContainer)container;
1821 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001822 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1823 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001824 final int stackId = activityContainer.mStackId;
1825 mActivityContainers.remove(stackId);
1826 mWindowManager.removeStack(stackId);
1827 }
1828 }
1829
Jorim Jaggidc249c42015-12-15 14:57:31 -08001830 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1831 boolean preserveWindows, boolean allowResizeInDockedMode) {
1832 if (stackId == DOCKED_STACK_ID) {
1833 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1834 preserveWindows);
1835 return;
1836 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001837 final ActivityStack stack = getStack(stackId);
1838 if (stack == null) {
1839 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1840 return;
1841 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001842
Jorim Jaggidc249c42015-12-15 14:57:31 -08001843 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001844 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1845 // stack size changing so things don't get out of sync.
1846 return;
1847 }
1848
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001849 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001850 mWindowManager.deferSurfaceLayout();
1851 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001852 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1853 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001854 } finally {
1855 mWindowManager.continueSurfaceLayout();
1856 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001857 }
1858 }
1859
Jorim Jaggidc249c42015-12-15 14:57:31 -08001860 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1861 Rect tempTaskInsetBounds) {
1862 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1863 // The bounds passed in corresponds to the fullscreen bounds which we normally
1864 // represent with null. Go ahead and set it to null so that all tasks configuration
1865 // can have the right fullscreen state.
1866 bounds = null;
1867 }
1868
1869 mTmpBounds.clear();
1870 mTmpConfigs.clear();
1871 mTmpInsetBounds.clear();
1872 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1873 for (int i = tasks.size() - 1; i >= 0; i--) {
1874 TaskRecord task = tasks.get(i);
1875 if (task.mResizeable) {
1876 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1877 // For freeform stack we don't adjust the size of the tasks to match that
1878 // of the stack, but we do try to make sure the tasks are still contained
1879 // with the bounds of the stack.
1880 tempRect2.set(task.mBounds);
1881 fitWithinBounds(tempRect2, bounds);
1882 task.updateOverrideConfiguration(tempRect2);
1883 } else {
1884 task.updateOverrideConfiguration(tempTaskBounds != null
1885 ? tempTaskBounds : bounds);
1886 }
1887 }
1888
1889 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1890 mTmpBounds.put(task.taskId, task.mBounds);
1891 if (tempTaskInsetBounds != null) {
1892 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1893 }
1894 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001895
1896 // We might trigger a configuration change. Save the current task bounds for freezing.
1897 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001898 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1899 mTmpBounds, mTmpInsetBounds);
1900 stack.setBounds(bounds);
1901 }
1902
1903 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1904 boolean preserveWindows) {
1905 if (r == null) {
1906 return;
1907 }
1908 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1909 preserveWindows);
1910 // And we need to make sure at this point that all other activities
1911 // are made visible with the correct configuration.
1912 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1913 if (!updated) {
1914 resumeFocusedStackTopActivityLocked();
1915 }
1916 }
1917
1918 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1919 Rect tempDockedTaskInsetBounds,
1920 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1921 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1922 if (stack == null) {
1923 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1924 return;
1925 }
1926
1927 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1928 mWindowManager.deferSurfaceLayout();
1929 try {
1930 ActivityRecord r = stack.topRunningActivityLocked();
1931 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1932 tempDockedTaskInsetBounds);
1933
1934 if (stack.mFullscreen) {
1935 // The dock stack went fullscreen which is kinda like dismissing it.
1936 // In this case we make all other static stacks fullscreen and move all
1937 // docked stack tasks to the fullscreen stack.
1938 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1939 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1940 resizeStackLocked(i, null, null, null, preserveWindows,
1941 true /* allowResizeInDockedMode */);
1942 }
1943 }
1944
1945 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1946 final int count = tasks.size();
1947 for (int i = 0; i < count; i++) {
1948 moveTaskToStackLocked(tasks.get(i).taskId,
1949 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1950 false /* animate */);
1951 }
1952
1953 // stack shouldn't contain anymore activities, so nothing to resume.
1954 r = null;
1955 } else {
1956 // Docked stacks occupy a dedicated region on screen so the size of all other
1957 // static stacks need to be adjusted so they don't overlap with the docked stack.
1958 // We get the bounds to use from window manager which has been adjusted for any
1959 // screen controls and is also the same for all stacks.
1960 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1961 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1962 if (StackId.isResizeableByDockedStack(i)) {
1963 ActivityStack otherStack = getStack(i);
1964 if (otherStack != null) {
1965 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1966 tempOtherTaskInsetBounds, preserveWindows,
1967 true /* allowResizeInDockedMode */);
1968 }
1969 }
1970 }
1971 }
1972 ensureConfigurationAndResume(stack, r, preserveWindows);
1973 } finally {
1974 mWindowManager.continueSurfaceLayout();
1975 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1976 }
1977
1978 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
1979 tempDockedTaskBounds != null
1980 || tempDockedTaskInsetBounds != null
1981 || tempOtherTaskBounds != null
1982 || tempOtherTaskInsetBounds != null);
1983 }
1984
Chong Zhangf596cd52016-01-05 13:42:44 -08001985 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001986 if (!task.mResizeable) {
1987 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08001988 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001989 }
1990
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001991 adjustForMinimalTaskDimensions(task, bounds);
1992
Chong Zhang87b21722015-09-21 15:39:51 -07001993 // If this is a forced resize, let it go through even if the bounds is not changing,
1994 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001995 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001996 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001997 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08001998 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001999 }
2000
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002001 if (!mWindowManager.isValidTaskId(task.taskId)) {
2002 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002003 // All we can do for now is update the bounds so it can be used when the task is
2004 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002005 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002006 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2007 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002008 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002009 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002010 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002011 }
2012
Chong Zhang6de2ae82015-09-30 18:25:21 -07002013 // Do not move the task to another stack here.
2014 // This method assumes that the task is already placed in the right stack.
2015 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002016
Chong Zhang6de2ae82015-09-30 18:25:21 -07002017 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002018
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002019 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002020 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002021 // way and the activity was kept the way it was. If it's false, it means the activity had
2022 // to be relaunched due to configuration change.
2023 boolean kept = true;
2024 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002025 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002026 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002027 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002028 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002029 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002030 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002031 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002032 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002033 }
2034 }
2035 }
Chong Zhang87b21722015-09-21 15:39:51 -07002036 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002037
2038 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002039 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002040 }
2041
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002042 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2043 if (bounds == null) {
2044 return;
2045 }
2046 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2047 : task.mMinimalSize;
2048 final boolean adjustWidth = minimalSize > bounds.width();
2049 final boolean adjustHeight = minimalSize > bounds.height();
2050 if (!(adjustWidth || adjustHeight)) {
2051 return;
2052 }
2053 Rect taskBounds = task.mBounds;
2054 if (adjustWidth) {
2055 if (taskBounds != null && bounds.right == taskBounds.right) {
2056 bounds.left = bounds.right - minimalSize;
2057 } else {
2058 // Either left bounds match, or neither match, or the previous bounds were
2059 // fullscreen and we default to keeping left.
2060 bounds.right = bounds.left + minimalSize;
2061 }
2062 }
2063 if (adjustHeight) {
2064 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2065 bounds.top = bounds.bottom - minimalSize;
2066 } else {
2067 // Either top bounds match, or neither match, or the previous bounds were
2068 // fullscreen and we default to keeping top.
2069 bounds.bottom = bounds.top + minimalSize;
2070 }
2071 }
2072 }
2073
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002074 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002075 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2076 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002077 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002078 }
2079
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002080 ActivityContainer activityContainer = new ActivityContainer(stackId);
2081 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002082 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002083 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002084 }
2085
2086 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002087 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002088 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2089 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002090 break;
2091 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002092 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002093 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002094 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002095 }
2096
Chong Zhang5dcb2752015-08-18 13:50:26 -07002097 /**
2098 * Restores a recent task to a stack
2099 * @param task The recent task to be restored.
2100 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002101 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002102 * @return true if the task has been restored successfully.
2103 */
2104 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2105 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002106 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002107 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002108 if (task.stack != null) {
2109 // Task has already been restored once. See if we need to do anything more
2110 if (task.stack.mStackId == stackId) {
2111 // Nothing else to do since it is already restored in the right stack.
2112 return true;
2113 }
2114 // Remove current stack association, so we can re-associate the task with the
2115 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002116 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002117 }
2118
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002119 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002120 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002121
2122 if (stack == null) {
2123 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002124 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2125 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002126 return false;
2127 }
2128
Wale Ogunwale5f986092015-12-04 15:35:38 -08002129 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002130 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2131 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002132 final ArrayList<ActivityRecord> activities = task.mActivities;
2133 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002134 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002135 }
2136 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002137 }
2138
Wale Ogunwale040b4702015-08-06 18:10:50 -07002139 /**
2140 * Moves the specified task record to the input stack id.
2141 * WARNING: This method performs an unchecked/raw move of the task and
2142 * can leave the system in an unstable state if used incorrectly.
2143 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2144 * to a stack.
2145 * @param task Task to move.
2146 * @param stackId Id of stack to move task to.
2147 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002148 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002149 * @param reason Reason the task is been moved.
2150 * @return The stack the task was moved to.
2151 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002152 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002153 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2154 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002155 final ActivityStack prevStack = task.stack;
2156 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2157 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2158 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2159 // Whenever we are moving the top activity from the front stack we want to make sure to move
2160 // the stack to the front.
2161 final boolean wasFront = isFrontStack(prevStack)
2162 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002163
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002164 final boolean resizeable = task.mResizeable;
2165 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2166 // if a docked stack is created below which will lead to the stack we are moving from and
2167 // its resizeable tasks being resized.
2168 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002169 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002170 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002171 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002172 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002173
2174 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002175 // move focus to the new stack by moving the stack to the front.
2176 stack.moveToFrontAndResumeStateIfNeeded(
2177 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002178
Wale Ogunwale040b4702015-08-06 18:10:50 -07002179 return stack;
2180 }
2181
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002182 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002183 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002184 final TaskRecord task = anyTaskForIdLocked(taskId);
2185 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002186 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002187 return;
2188 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002189
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002190 if (task.stack != null && task.stack.mStackId == stackId) {
2191 // You are already in the right stack silly...
2192 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2193 return;
2194 }
2195
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002196 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002197 final boolean mightReplaceWindow =
2198 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2199 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002200 // We are about to relaunch the activity because its configuration changed due to
2201 // being maximized, i.e. size change. The activity will first remove the old window
2202 // and then add a new one. This call will tell window manager about this, so it can
2203 // preserve the old window until the new one is drawn. This prevents having a gap
2204 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002205 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002206 // Note here we always set the replacing window first, as the flags might be needed
2207 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002208 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002209 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002210 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2211 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002212
Jorim Jaggi55387522015-11-24 18:21:10 -08002213 if (!animate) {
2214 stack.mNoAnimActivities.add(topActivity);
2215 }
2216
Chong Zhangf596cd52016-01-05 13:42:44 -08002217 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002218 // Make sure the task has the appropriate bounds/size for the stack it is in.
2219 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002220 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002221 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2222 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002223 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002224 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002225 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002226 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2227 }
2228
2229 if (mightReplaceWindow) {
2230 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2231 // window), we need to clear the replace window settings. Otherwise, we schedule a
2232 // timeout to remove the old window if the replacing window is not coming in time.
2233 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002234 }
2235
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002236 // The task might have already been running and its visibility needs to be synchronized with
2237 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002238 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002239 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002240
2241 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002242 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002243 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002244 }
2245
Wale Ogunwale079a0042015-10-24 11:44:07 -07002246 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2247 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2248 if (stack == null) {
2249 throw new IllegalArgumentException(
2250 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2251 }
2252
2253 final ActivityRecord r = stack.topRunningActivityLocked();
2254 if (r == null) {
2255 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2256 + " in stack=" + stack);
2257 return false;
2258 }
2259
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002260 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002261 Slog.w(TAG,
2262 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002263 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002264 return false;
2265 }
2266
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002267 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2268 return true;
2269 }
2270
2271 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002272 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002273 if (task.mActivities.size() == 1) {
2274 // 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 -08002275 // stack without re-parenting the activity in a different task.
2276 moveTaskToStackLocked(
2277 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002278 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002279 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2280 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002281 }
2282
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002283 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002284 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2285 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002286 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002287
2288 // The task might have already been running and its visibility needs to be synchronized with
2289 // the visibility of the stack / windows.
2290 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002291 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002292
2293 if (stackId == PINNED_STACK_ID) {
2294 mService.notifyActivityPinnedLocked();
2295 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002296 }
2297
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002298 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2299 final TaskRecord task = anyTaskForIdLocked(taskId);
2300 if (task == null) {
2301 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2302 return;
2303 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002304 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2305
2306 task.updateOverrideConfigurationForStack(stack);
2307
2308 mWindowManager.positionTaskInStack(
2309 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002310 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002311 // The task might have already been running and its visibility needs to be synchronized with
2312 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002313 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002314 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002315 }
2316
Craig Mautnerac6f8432013-07-17 13:24:59 -07002317 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002318 mTmpFindTaskResult.r = null;
2319 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002320 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002321 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2322 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002323 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2324 final ActivityStack stack = stacks.get(stackNdx);
2325 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002326 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002327 continue;
2328 }
2329 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002330 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2331 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002332 continue;
2333 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002334 stack.findTaskLocked(r, mTmpFindTaskResult);
2335 // It is possible to have task in multiple stacks with the same root affinity.
2336 // If the match we found was based on root affinity we keep on looking to see if
2337 // there is a better match in another stack. We eventually return the match based
2338 // on root affinity if we don't find a better match.
2339 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2340 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002341 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 }
2343 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002344 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2345 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002346 }
2347
2348 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002349 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2350 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002351 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2352 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2353 if (ar != null) {
2354 return ar;
2355 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002356 }
2357 }
2358 return null;
2359 }
2360
Craig Mautner8d341ef2013-03-26 09:03:27 -07002361 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002362 scheduleSleepTimeout();
2363 if (!mGoingToSleep.isHeld()) {
2364 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002365 if (mLaunchingActivity.isHeld()) {
2366 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2367 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002368 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002369 mLaunchingActivity.release();
2370 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002371 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002372 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002373 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002374 }
2375
2376 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002377 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002378
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002379 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002380 final long endTime = System.currentTimeMillis() + timeout;
2381 while (true) {
2382 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002383 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2384 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002385 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2386 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2387 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002388 }
2389 if (cantShutdown) {
2390 long timeRemaining = endTime - System.currentTimeMillis();
2391 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002392 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002393 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002394 } catch (InterruptedException e) {
2395 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002396 } else {
2397 Slog.w(TAG, "Activity manager shutdown timed out");
2398 timedout = true;
2399 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002400 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002401 } else {
2402 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002403 }
2404 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002405
2406 // Force checkReadyForSleep to complete.
2407 mSleepTimeout = true;
2408 checkReadyForSleepLocked();
2409
Craig Mautner8d341ef2013-03-26 09:03:27 -07002410 return timedout;
2411 }
2412
2413 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002414 removeSleepTimeouts();
2415 if (mGoingToSleep.isHeld()) {
2416 mGoingToSleep.release();
2417 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002418 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2419 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002420 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2421 final ActivityStack stack = stacks.get(stackNdx);
2422 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002423 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002424 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002425 }
Craig Mautner5314a402013-09-26 12:40:16 -07002426 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002427 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002428 mGoingToSleepActivities.clear();
2429 }
2430
2431 void activitySleptLocked(ActivityRecord r) {
2432 mGoingToSleepActivities.remove(r);
2433 checkReadyForSleepLocked();
2434 }
2435
2436 void checkReadyForSleepLocked() {
2437 if (!mService.isSleepingOrShuttingDown()) {
2438 // Do not care.
2439 return;
2440 }
2441
2442 if (!mSleepTimeout) {
2443 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002444 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2445 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002446 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2447 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2448 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002449 }
2450
2451 if (mStoppingActivities.size() > 0) {
2452 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002453 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002454 + mStoppingActivities.size() + " activities");
2455 scheduleIdleLocked();
2456 dontSleep = true;
2457 }
2458
2459 if (mGoingToSleepActivities.size() > 0) {
2460 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002461 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002462 + mGoingToSleepActivities.size() + " activities");
2463 dontSleep = true;
2464 }
2465
2466 if (dontSleep) {
2467 return;
2468 }
2469 }
2470
Craig Mautnere0a38842013-12-16 16:14:02 -08002471 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2472 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002473 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2474 stacks.get(stackNdx).goToSleep();
2475 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002476 }
2477
2478 removeSleepTimeouts();
2479
2480 if (mGoingToSleep.isHeld()) {
2481 mGoingToSleep.release();
2482 }
2483 if (mService.mShuttingDown) {
2484 mService.notifyAll();
2485 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002486 }
2487
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002488 boolean reportResumedActivityLocked(ActivityRecord r) {
2489 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002490 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002491 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002492 }
2493 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002494 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002495 mWindowManager.executeAppTransition();
2496 return true;
2497 }
2498 return false;
2499 }
2500
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002502 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2503 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002504 int stackNdx = stacks.size() - 1;
2505 while (stackNdx >= 0) {
2506 stacks.get(stackNdx).handleAppCrashLocked(app);
2507 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002508 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
2510 }
2511
Jose Lima4b6c6692014-08-12 17:41:12 -07002512 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002513 final ActivityStack stack = r.task.stack;
2514 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002515 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2516 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002517 return false;
2518 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002519 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002520 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2521 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002522
2523 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002524 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002525 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002526 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002527 return true;
2528 }
2529
2530 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002531 if (visible && top.fullscreen) {
2532 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002533 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2534 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2535 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2536 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002537 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002538 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2539 // Only the activity set as currently visible behind should actively reset its
2540 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002541 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2542 "requestVisibleBehind: returning visible=" + visible
2543 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2544 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002545 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002546 }
2547
Jose Lima4b6c6692014-08-12 17:41:12 -07002548 stack.setVisibleBehindActivity(visible ? r : null);
2549 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002550 // Make the activity immediately above r opaque.
2551 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2552 if (next != null) {
2553 mService.convertFromTranslucent(next.appToken);
2554 }
2555 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002556 if (top.app != null && top.app.thread != null) {
2557 // Notify the top app of the change.
2558 try {
2559 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2560 } catch (RemoteException e) {
2561 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002562 }
2563 return true;
2564 }
2565
Craig Mautnerbb742462014-07-07 15:28:55 -07002566 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2567 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2568 r.mLaunchTaskBehind = false;
2569 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002570 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002571 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002572 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002573 mWindowManager.setAppVisibility(r.appToken, false);
2574 }
2575
2576 void scheduleLaunchTaskBehindComplete(IBinder token) {
2577 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2578 }
2579
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002580 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2581 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002582 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 final int topStackNdx = stacks.size() - 1;
2586 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2587 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002588 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002589 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002591 }
2592
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002593 void invalidateTaskLayers() {
2594 mTaskLayersChanged = true;
2595 }
2596
2597 void rankTaskLayersIfNeeded() {
2598 if (!mTaskLayersChanged) {
2599 return;
2600 }
2601 mTaskLayersChanged = false;
2602 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2603 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2604 int baseLayer = 0;
2605 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2606 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2607 }
2608 }
2609 }
2610
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002611 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2612 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2613 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2614 final int topStackNdx = stacks.size() - 1;
2615 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2616 final ActivityStack stack = stacks.get(stackNdx);
2617 stack.clearOtherAppTimeTrackers(except);
2618 }
2619 }
2620 }
2621
Craig Mautner8d341ef2013-03-26 09:03:27 -07002622 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002623 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2624 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002625 final int numStacks = stacks.size();
2626 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2627 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002628 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002629 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002630 }
2631 }
2632
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002633 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2634 // Examine all activities currently running in the process.
2635 TaskRecord firstTask = null;
2636 // Tasks is non-null only if two or more tasks are found.
2637 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002638 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2639 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002640 ActivityRecord r = app.activities.get(i);
2641 // First, if we find an activity that is in the process of being destroyed,
2642 // then we just aren't going to do anything for now; we want things to settle
2643 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002644 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002645 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002646 return;
2647 }
2648 // Don't consider any activies that are currently not in a state where they
2649 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002650 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2651 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002652 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002653 continue;
2654 }
2655 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002656 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002657 + " from " + r);
2658 if (firstTask == null) {
2659 firstTask = r.task;
2660 } else if (firstTask != r.task) {
2661 if (tasks == null) {
2662 tasks = new ArraySet<>();
2663 tasks.add(firstTask);
2664 }
2665 tasks.add(r.task);
2666 }
2667 }
2668 }
2669 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002670 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002671 return;
2672 }
2673 // If we have activities in multiple tasks that are in a position to be destroyed,
2674 // let's iterate through the tasks and release the oldest one.
2675 final int numDisplays = mActivityDisplays.size();
2676 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2677 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2678 // Step through all stacks starting from behind, to hit the oldest things first.
2679 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2680 final ActivityStack stack = stacks.get(stackNdx);
2681 // Try to release activities in this stack; if we manage to, we are done.
2682 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2683 return;
2684 }
2685 }
2686 }
2687 }
2688
Amith Yamasani37a40c22015-06-17 13:25:42 -07002689 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002690 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002691 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002692 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002693
Craig Mautner858d8a62013-04-23 17:08:34 -07002694 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002695 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2696 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002697 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002698 final ActivityStack stack = stacks.get(stackNdx);
2699 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002700 TaskRecord task = stack.topTask();
2701 if (task != null) {
2702 mWindowManager.moveTaskToTop(task.taskId);
2703 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002705 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002706
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002707 ActivityStack stack = getStack(restoreStackId);
2708 if (stack == null) {
2709 stack = mHomeStack;
2710 }
2711 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002713 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002714 } else {
2715 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002716 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002717 }
Craig Mautner93529a42013-10-04 15:03:13 -07002718 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002719 }
2720
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002721 /**
2722 * Add background users to send boot completed events to.
2723 * @param userId The user being started in the background
2724 * @param uss The state object for the user.
2725 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002726 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002727 mStartingBackgroundUsers.add(uss);
2728 }
2729
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002730 /** Checks whether the userid is a profile of the current user. */
2731 boolean isCurrentProfileLocked(int userId) {
2732 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002733 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002734 }
2735
Chong Zhang45c25ce2015-08-10 22:18:26 -07002736 /** Checks whether the activity should be shown for current user. */
2737 boolean okToShowLocked(ActivityRecord r) {
2738 return r != null && (isCurrentProfileLocked(r.userId)
2739 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2740 }
2741
Craig Mautnerde4ef022013-04-07 19:01:33 -07002742 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002743 ArrayList<ActivityRecord> stops = null;
2744
2745 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002746 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2747 ActivityRecord s = mStoppingActivities.get(activityNdx);
2748 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002749 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002750 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2751 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002752 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002753 if (s.finishing) {
2754 // If this activity is finishing, it is sitting on top of
2755 // everyone else but we now know it is no longer needed...
2756 // so get rid of it. Otherwise, we need to go through the
2757 // normal flow and hide it once we determine that it is
2758 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002759 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002760 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002761 }
2762 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002763 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002764 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002765 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002766 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002767 }
2768 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002769 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002770 }
2771 }
2772
2773 return stops;
2774 }
2775
Craig Mautnercf910b02013-04-23 11:23:27 -07002776 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002777 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2778 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2779 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2780 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002781 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002782 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002783 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002784 if (r == null) Slog.e(TAG,
2785 "validateTop...: null top activity, stack=" + stack);
2786 else {
2787 final ActivityRecord pausing = stack.mPausingActivity;
2788 if (pausing != null && pausing == r) Slog.e(TAG,
2789 "validateTop...: top stack has pausing activity r=" + r
2790 + " state=" + state);
2791 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2792 "validateTop...: activity in front not resumed r=" + r
2793 + " state=" + state);
2794 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002795 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002796 final ActivityRecord resumed = stack.mResumedActivity;
2797 if (resumed != null && resumed == r) Slog.e(TAG,
2798 "validateTop...: back stack has resumed activity r=" + r
2799 + " state=" + state);
2800 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2801 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002802 }
2803 }
2804 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002805 }
2806
Craig Mautnere0570202015-05-13 13:06:11 -07002807 private String lockTaskModeToString() {
2808 switch (mLockTaskModeState) {
2809 case LOCK_TASK_MODE_LOCKED:
2810 return "LOCKED";
2811 case LOCK_TASK_MODE_PINNED:
2812 return "PINNED";
2813 case LOCK_TASK_MODE_NONE:
2814 return "NONE";
2815 default: return "unknown=" + mLockTaskModeState;
2816 }
2817 }
2818
Craig Mautner27084302013-03-25 08:05:25 -07002819 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002820 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002822 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002823 pw.print(prefix);
2824 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002825 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002826 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002827 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2828 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2829 if (packages.size() > 0) {
2830 pw.println(" mLockTaskPackages (userId:packages)=");
2831 for (int i = 0; i < packages.size(); ++i) {
2832 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2833 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2834 }
2835 }
2836 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002837 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002838
Craig Mautner20e72272013-04-01 13:45:53 -07002839 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002840 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002841 }
2842
Dianne Hackborn390517b2013-05-30 15:03:32 -07002843 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2844 boolean needSep, String prefix) {
2845 if (activity != null) {
2846 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2847 if (needSep) {
2848 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002849 }
2850 pw.print(prefix);
2851 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002852 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002853 }
2854 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002855 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002856 }
2857
Craig Mautner8d341ef2013-03-26 09:03:27 -07002858 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2859 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002860 boolean printed = false;
2861 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002862 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2863 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002864 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002865 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002866 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002867 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002868 final ActivityStack stack = stacks.get(stackNdx);
2869 StringBuilder stackHeader = new StringBuilder(128);
2870 stackHeader.append(" Stack #");
2871 stackHeader.append(stack.mStackId);
2872 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002873 stackHeader.append("\n");
2874 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2875 stackHeader.append("\n");
2876 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2878 needSep, stackHeader.toString());
2879 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2880 !dumpAll, false, dumpPackage, true,
2881 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882
Craig Mautner4a1cb222013-12-04 16:14:06 -08002883 needSep = printed;
2884 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2885 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002886 if (pr) {
2887 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002888 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002889 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002890 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2891 " mResumedActivity: ");
2892 if (pr) {
2893 printed = true;
2894 needSep = false;
2895 }
2896 if (dumpAll) {
2897 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2898 " mLastPausedActivity: ");
2899 if (pr) {
2900 printed = true;
2901 needSep = true;
2902 }
2903 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2904 needSep, " mLastNoHistoryActivity: ");
2905 }
2906 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908 }
2909
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002910 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2911 false, dumpPackage, true, " Activities waiting to finish:", null);
2912 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2913 false, dumpPackage, true, " Activities waiting to stop:", null);
2914 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2915 false, dumpPackage, true, " Activities waiting for another to become visible:",
2916 null);
2917 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2918 false, dumpPackage, true, " Activities waiting to sleep:", null);
2919 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2920 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002921
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002922 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002923 }
2924
Dianne Hackborn390517b2013-05-30 15:03:32 -07002925 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002926 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002927 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002928 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002929 String innerPrefix = null;
2930 String[] args = null;
2931 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002932 for (int i=list.size()-1; i>=0; i--) {
2933 final ActivityRecord r = list.get(i);
2934 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2935 continue;
2936 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002937 if (innerPrefix == null) {
2938 innerPrefix = prefix + " ";
2939 args = new String[0];
2940 }
2941 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002942 final boolean full = !brief && (complete || !r.isInHistory());
2943 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002944 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 needNL = false;
2946 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002947 if (header1 != null) {
2948 pw.println(header1);
2949 header1 = null;
2950 }
2951 if (header2 != null) {
2952 pw.println(header2);
2953 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002954 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 if (lastTask != r.task) {
2956 lastTask = r.task;
2957 pw.print(prefix);
2958 pw.print(full ? "* " : " ");
2959 pw.println(lastTask);
2960 if (full) {
2961 lastTask.dump(pw, prefix + " ");
2962 } else if (complete) {
2963 // Complete + brief == give a summary. Isn't that obvious?!?
2964 if (lastTask.intent != null) {
2965 pw.print(prefix); pw.print(" ");
2966 pw.println(lastTask.intent.toInsecureStringWithClip());
2967 }
2968 }
2969 }
2970 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2971 pw.print(" #"); pw.print(i); pw.print(": ");
2972 pw.println(r);
2973 if (full) {
2974 r.dump(pw, innerPrefix);
2975 } else if (complete) {
2976 // Complete + brief == give a summary. Isn't that obvious?!?
2977 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2978 if (r.app != null) {
2979 pw.print(innerPrefix); pw.println(r.app);
2980 }
2981 }
2982 if (client && r.app != null && r.app.thread != null) {
2983 // flush anything that is already in the PrintWriter since the thread is going
2984 // to write to the file descriptor directly
2985 pw.flush();
2986 try {
2987 TransferPipe tp = new TransferPipe();
2988 try {
2989 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2990 r.appToken, innerPrefix, args);
2991 // Short timeout, since blocking here can
2992 // deadlock with the application.
2993 tp.go(fd, 2000);
2994 } finally {
2995 tp.kill();
2996 }
2997 } catch (IOException e) {
2998 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2999 } catch (RemoteException e) {
3000 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3001 }
3002 needNL = true;
3003 }
3004 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003005 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003006 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003007
Craig Mautnerf3333272013-04-22 10:55:53 -07003008 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003009 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3010 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003011 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3012 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003013 }
3014
3015 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003016 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003017 }
3018
3019 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003020 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3021 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003022 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3023 }
3024
Craig Mautner05d29032013-05-03 13:40:13 -07003025 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003026 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3027 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3028 }
Craig Mautner05d29032013-05-03 13:40:13 -07003029 }
3030
Craig Mautner0eea92c2013-05-16 13:35:39 -07003031 void removeSleepTimeouts() {
3032 mSleepTimeout = false;
3033 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3034 }
3035
3036 final void scheduleSleepTimeout() {
3037 removeSleepTimeouts();
3038 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3039 }
3040
Craig Mautner4a1cb222013-12-04 16:14:06 -08003041 @Override
3042 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003043 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003044 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3045 }
3046
3047 @Override
3048 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003049 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3051 }
3052
3053 @Override
3054 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003055 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003056 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3057 }
3058
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003059 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003060 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003061 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003062 newDisplay = mActivityDisplays.get(displayId) == null;
3063 if (newDisplay) {
3064 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003065 if (activityDisplay.mDisplay == null) {
3066 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3067 return;
3068 }
Craig Mautner4504de52013-12-20 09:06:56 -08003069 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003070 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003071 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003072 }
Craig Mautner4504de52013-12-20 09:06:56 -08003073 if (newDisplay) {
3074 mWindowManager.onDisplayAdded(displayId);
3075 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003076 }
3077
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003078 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3079 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3080 return;
3081 }
3082 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3083 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3084 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3085 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3086 }
3087
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003088 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003089 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003090 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3091 if (activityDisplay != null) {
3092 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003094 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 }
3098 }
3099 mWindowManager.onDisplayRemoved(displayId);
3100 }
3101
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003102 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003104 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3105 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 // TODO: Update the bounds.
3107 }
3108 }
3109 mWindowManager.onDisplayChanged(displayId);
3110 }
3111
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003112 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003113 StackInfo info = new StackInfo();
3114 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3115 info.displayId = Display.DEFAULT_DISPLAY;
3116 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003117 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003118
3119 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3120 final int numTasks = tasks.size();
3121 int[] taskIds = new int[numTasks];
3122 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003123 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003124 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003125 for (int i = 0; i < numTasks; ++i) {
3126 final TaskRecord task = tasks.get(i);
3127 taskIds[i] = task.taskId;
3128 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3129 : task.realActivity != null ? task.realActivity.flattenToString()
3130 : task.getTopActivity() != null ? task.getTopActivity().packageName
3131 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003132 taskBounds[i] = new Rect();
3133 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003134 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 }
3136 info.taskIds = taskIds;
3137 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003138 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003139 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140 return info;
3141 }
3142
3143 StackInfo getStackInfoLocked(int stackId) {
3144 ActivityStack stack = getStack(stackId);
3145 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003146 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003147 }
3148 return null;
3149 }
3150
3151 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003152 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003153 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3154 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003156 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003157 }
3158 }
3159 return list;
3160 }
3161
Craig Mautner15df08a2015-04-01 12:17:18 -07003162 TaskRecord getLockedTaskLocked() {
3163 final int top = mLockTaskModeTasks.size() - 1;
3164 if (top >= 0) {
3165 return mLockTaskModeTasks.get(top);
3166 }
3167 return null;
3168 }
3169
3170 boolean isLockedTask(TaskRecord task) {
3171 return mLockTaskModeTasks.contains(task);
3172 }
3173
3174 boolean isLastLockedTask(TaskRecord task) {
3175 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3176 }
3177
3178 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003179 if (!mLockTaskModeTasks.remove(task)) {
3180 return;
3181 }
3182 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3183 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003184 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003185 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3186 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003187 final Message lockTaskMsg = Message.obtain();
3188 lockTaskMsg.arg1 = task.userId;
3189 lockTaskMsg.what = LOCK_TASK_END_MSG;
3190 mHandler.sendMessage(lockTaskMsg);
3191 }
3192 }
3193
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003194 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003195 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003196 }
3197
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003198 void showLockTaskToast() {
3199 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003200 }
3201
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003202 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3203 if (mLockTaskModeTasks.contains(task)) {
3204 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3205 }
3206 }
3207
Craig Mautner432f64e2015-05-20 14:59:57 -07003208 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3209 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003210 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003211 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003212 final TaskRecord lockedTask = getLockedTaskLocked();
3213 if (lockedTask != null) {
3214 removeLockedTaskLocked(lockedTask);
3215 if (!mLockTaskModeTasks.isEmpty()) {
3216 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003217 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3218 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003219 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003220 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003221 return;
3222 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003223 }
Craig Mautnere0570202015-05-13 13:06:11 -07003224 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3225 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003226 return;
3227 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003228
3229 // Should have already been checked, but do it again.
3230 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003231 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3232 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003233 return;
3234 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003235 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003236 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003237 return;
3238 }
3239
3240 if (mLockTaskModeTasks.isEmpty()) {
3241 // First locktask.
3242 final Message lockTaskMsg = Message.obtain();
3243 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3244 lockTaskMsg.arg1 = task.userId;
3245 lockTaskMsg.what = LOCK_TASK_START_MSG;
3246 lockTaskMsg.arg2 = lockTaskModeState;
3247 mHandler.sendMessage(lockTaskMsg);
3248 }
3249 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003250 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3251 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003252 mLockTaskModeTasks.remove(task);
3253 mLockTaskModeTasks.add(task);
3254
3255 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003256 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003257 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003258
3259 if (andResume) {
3260 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003261 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003262 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003263 }
3264
3265 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003266 return isLockTaskModeViolation(task, false);
3267 }
3268
3269 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3270 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003271 return false;
3272 }
3273 final int lockTaskAuth = task.mLockTaskAuth;
3274 switch (lockTaskAuth) {
3275 case LOCK_TASK_AUTH_DONT_LOCK:
3276 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003277 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003278 case LOCK_TASK_AUTH_LAUNCHABLE:
3279 case LOCK_TASK_AUTH_WHITELISTED:
3280 return false;
3281 case LOCK_TASK_AUTH_PINNABLE:
3282 // Pinnable tasks can't be launched on top of locktask tasks.
3283 return !mLockTaskModeTasks.isEmpty();
3284 default:
3285 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3286 return true;
3287 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003288 }
3289
Craig Mautner15df08a2015-04-01 12:17:18 -07003290 void onLockTaskPackagesUpdatedLocked() {
3291 boolean didSomething = false;
3292 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3293 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003294 final boolean wasWhitelisted =
3295 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3296 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003297 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003298 final boolean isWhitelisted =
3299 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3300 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3301 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003302 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003303 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3304 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003305 removeLockedTaskLocked(lockedTask);
3306 lockedTask.performClearTaskLocked();
3307 didSomething = true;
3308 }
3309 }
3310 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3311 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3312 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3313 final ActivityStack stack = stacks.get(stackNdx);
3314 stack.onLockTaskPackagesUpdatedLocked();
3315 }
3316 }
Craig Mautnere0570202015-05-13 13:06:11 -07003317 final ActivityRecord r = topRunningActivityLocked();
3318 final TaskRecord task = r != null ? r.task : null;
3319 if (mLockTaskModeTasks.isEmpty() && task != null
3320 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3321 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003322 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3323 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3324 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3325 false);
3326 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003327 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003328 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003329 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003330 }
3331 }
3332
Benjamin Franz43261142015-02-11 15:59:44 +00003333 int getLockTaskModeState() {
3334 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003335 }
3336
Jorim Jaggife89d122015-12-22 16:28:44 +01003337 void activityRelaunchedLocked(IBinder token) {
3338 mWindowManager.notifyAppRelaunchingFinished(token);
3339 }
3340
3341 void activityRelaunchingLocked(ActivityRecord r) {
3342 mWindowManager.notifyAppRelaunching(r.appToken);
3343 }
3344
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003345 void logStackState() {
3346 mActivityMetricsLogger.logWindowState();
3347 }
3348
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003349 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003350
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003351 public ActivityStackSupervisorHandler(Looper looper) {
3352 super(looper);
3353 }
3354
Craig Mautnerf3333272013-04-22 10:55:53 -07003355 void activityIdleInternal(ActivityRecord r) {
3356 synchronized (mService) {
3357 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3358 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003359 }
3360
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003361 @Override
3362 public void handleMessage(Message msg) {
3363 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003364 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003365 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3366 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003367 if (mService.mDidDexOpt) {
3368 mService.mDidDexOpt = false;
3369 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3370 nmsg.obj = msg.obj;
3371 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3372 return;
3373 }
3374 // We don't at this point know if the activity is fullscreen,
3375 // so we need to be conservative and assume it isn't.
3376 activityIdleInternal((ActivityRecord)msg.obj);
3377 } break;
3378 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003379 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003380 activityIdleInternal((ActivityRecord)msg.obj);
3381 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003382 case RESUME_TOP_ACTIVITY_MSG: {
3383 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003384 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003385 }
3386 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003387 case SLEEP_TIMEOUT_MSG: {
3388 synchronized (mService) {
3389 if (mService.isSleepingOrShuttingDown()) {
3390 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3391 mSleepTimeout = true;
3392 checkReadyForSleepLocked();
3393 }
3394 }
3395 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003396 case LAUNCH_TIMEOUT_MSG: {
3397 if (mService.mDidDexOpt) {
3398 mService.mDidDexOpt = false;
3399 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3400 return;
3401 }
3402 synchronized (mService) {
3403 if (mLaunchingActivity.isHeld()) {
3404 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3405 if (VALIDATE_WAKE_LOCK_CALLER
3406 && Binder.getCallingUid() != Process.myUid()) {
3407 throw new IllegalStateException("Calling must be system uid");
3408 }
3409 mLaunchingActivity.release();
3410 }
3411 }
3412 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003413 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003414 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 } break;
3416 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003417 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003418 } break;
3419 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003420 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003422 case CONTAINER_CALLBACK_VISIBILITY: {
3423 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003424 final IActivityContainerCallback callback = container.mCallback;
3425 if (callback != null) {
3426 try {
3427 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3428 } catch (RemoteException e) {
3429 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003430 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003431 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003432 case LOCK_TASK_START_MSG: {
3433 // When lock task starts, we disable the status bars.
3434 try {
Jason Monk62515be2014-05-21 16:06:19 -04003435 if (mLockTaskNotify == null) {
3436 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003437 }
Jason Monk62515be2014-05-21 16:06:19 -04003438 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003439 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003440 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003441 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003442 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003443 flags = StatusBarManager.DISABLE_MASK
3444 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003445 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003446 flags = StatusBarManager.DISABLE_MASK
3447 & (~StatusBarManager.DISABLE_BACK)
3448 & (~StatusBarManager.DISABLE_HOME)
3449 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003450 }
3451 getStatusBarService().disable(flags, mToken,
3452 mService.mContext.getPackageName());
3453 }
3454 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003455 if (getDevicePolicyManager() != null) {
3456 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003457 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003458 }
justinzhang5286d3f2014-05-12 17:06:01 -04003459 } catch (RemoteException ex) {
3460 throw new RuntimeException(ex);
3461 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003462 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003463 case LOCK_TASK_END_MSG: {
3464 // When lock task ends, we enable the status bars.
3465 try {
Jason Monk62515be2014-05-21 16:06:19 -04003466 if (getStatusBarService() != null) {
3467 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3468 mService.mContext.getPackageName());
3469 }
3470 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003471 if (getDevicePolicyManager() != null) {
3472 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3473 msg.arg1);
3474 }
Jason Monk62515be2014-05-21 16:06:19 -04003475 if (mLockTaskNotify == null) {
3476 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3477 }
3478 mLockTaskNotify.show(false);
3479 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003480 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003481 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003482 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003483 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003484 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003485 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003486 new LockPatternUtils(mService.mContext)
3487 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003488 }
3489 } catch (SettingNotFoundException e) {
3490 // No setting, don't lock.
3491 }
justinzhang5286d3f2014-05-12 17:06:01 -04003492 } catch (RemoteException ex) {
3493 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003494 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003495 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003496 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003497 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003498 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3499 if (mLockTaskNotify == null) {
3500 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3501 }
3502 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3503 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003504 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3505 final ActivityContainer container = (ActivityContainer) msg.obj;
3506 final IActivityContainerCallback callback = container.mCallback;
3507 if (callback != null) {
3508 try {
3509 callback.onAllActivitiesComplete(container.asBinder());
3510 } catch (RemoteException e) {
3511 }
3512 }
3513 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003514 case LAUNCH_TASK_BEHIND_COMPLETE: {
3515 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003516 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003517 if (r != null) {
3518 handleLaunchTaskBehindCompleteLocked(r);
3519 }
3520 }
3521 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003522
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003523 }
3524 }
3525 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003526
Ying Wangb081a592014-04-22 15:20:16 -07003527 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003528 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3529 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003531 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003533 ActivityRecord mParentActivity = null;
3534 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003535
Craig Mautnere3a00d72014-04-16 08:31:19 -07003536 boolean mVisible = true;
3537
Craig Mautner4a1cb222013-12-04 16:14:06 -08003538 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003539 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003541 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3542 final static int CONTAINER_STATE_NO_SURFACE = 1;
3543 final static int CONTAINER_STATE_FINISHING = 2;
3544 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3545
3546 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003547 synchronized (mService) {
3548 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003549 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003550 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003551 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003552 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003553 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003555 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003556 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003557 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003558 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003559 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003560 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003561 }
3562
3563 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003564 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3567 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 return;
3569 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003570 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 }
3572 }
3573
3574 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003575 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003576 synchronized (mService) {
3577 if (mActivityDisplay != null) {
3578 return mActivityDisplay.mDisplayId;
3579 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003580 }
3581 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 }
3583
Jeff Brownca9bc702014-02-11 14:32:56 -08003584 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003585 public int getStackId() {
3586 synchronized (mService) {
3587 return mStackId;
3588 }
3589 }
3590
3591 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003592 public boolean injectEvent(InputEvent event) {
3593 final long origId = Binder.clearCallingIdentity();
3594 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003595 synchronized (mService) {
3596 if (mActivityDisplay != null) {
3597 return mInputManagerInternal.injectInputEvent(event,
3598 mActivityDisplay.mDisplayId,
3599 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3600 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003601 }
3602 return false;
3603 } finally {
3604 Binder.restoreCallingIdentity(origId);
3605 }
3606 }
3607
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003608 @Override
3609 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003610 synchronized (mService) {
3611 if (mContainerState == CONTAINER_STATE_FINISHING) {
3612 return;
3613 }
3614 mContainerState = CONTAINER_STATE_FINISHING;
3615
Craig Mautnerd163e752014-06-13 17:18:47 -07003616 long origId = Binder.clearCallingIdentity();
3617 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003618 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003619 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003620 } finally {
3621 Binder.restoreCallingIdentity(origId);
3622 }
3623 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003624 }
3625
Craig Mautner60257702014-09-17 15:02:33 -07003626 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003627 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003628 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003629 if (mActivityDisplay != null) {
3630 mActivityDisplay.detachActivitiesLocked(mStack);
3631 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003632 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 }
3634 }
3635
3636 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003637 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003638 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 }
3640
3641 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003642 public final int startActivityIntentSender(IIntentSender intentSender)
3643 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003644 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3645
3646 if (!(intentSender instanceof PendingIntentRecord)) {
3647 throw new IllegalArgumentException("Bad PendingIntent object");
3648 }
3649
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003650 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003651 Binder.getCallingUid(), mCurrentUser, false,
3652 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003653
3654 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3655 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3656 pendingIntent.key.requestResolvedType);
3657
3658 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3659 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3660 }
3661
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003662 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003663 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003664 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003665 throw new SecurityException(
3666 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3667 }
3668 }
3669
Craig Mautnerdf88d732014-01-27 09:21:32 -08003670 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003671 public IBinder asBinder() {
3672 return this;
3673 }
3674
Craig Mautner4504de52013-12-20 09:06:56 -08003675 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003676 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003677 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003678 }
3679
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680 ActivityStackSupervisor getOuter() {
3681 return ActivityStackSupervisor.this;
3682 }
3683
Craig Mautnerd163e752014-06-13 17:18:47 -07003684 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003685 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003686 }
3687
Craig Mautner6985bad2014-04-21 15:22:06 -07003688 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003689 void setVisible(boolean visible) {
3690 if (mVisible != visible) {
3691 mVisible = visible;
3692 if (mCallback != null) {
3693 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3694 0 /* unused */, this).sendToTarget();
3695 }
3696 }
3697 }
3698
Craig Mautner6985bad2014-04-21 15:22:06 -07003699 void setDrawn() {
3700 }
3701
Craig Mautner1b4bf852014-05-26 15:06:32 -07003702 // You can always start a new task on a regular ActivityStack.
3703 boolean isEligibleForNewTasks() {
3704 return true;
3705 }
3706
Craig Mautnerd163e752014-06-13 17:18:47 -07003707 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003708 detachLocked();
3709 deleteActivityContainer(this);
3710 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003711 }
3712
Craig Mautner34b73df2014-01-12 21:11:08 -08003713 @Override
3714 public String toString() {
3715 return mIdString + (mActivityDisplay == null ? "N" : "A");
3716 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003717 }
3718
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003719 private class VirtualActivityContainer extends ActivityContainer {
3720 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003721 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003722
3723 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3724 super(getNextStackId());
3725 mParentActivity = parent;
3726 mCallback = callback;
3727 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003728 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003729 }
3730
3731 @Override
3732 public void setSurface(Surface surface, int width, int height, int density) {
3733 super.setSurface(surface, width, height, density);
3734
3735 synchronized (mService) {
3736 final long origId = Binder.clearCallingIdentity();
3737 try {
3738 setSurfaceLocked(surface, width, height, density);
3739 } finally {
3740 Binder.restoreCallingIdentity(origId);
3741 }
3742 }
3743 }
3744
3745 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3746 if (mContainerState == CONTAINER_STATE_FINISHING) {
3747 return;
3748 }
3749 VirtualActivityDisplay virtualActivityDisplay =
3750 (VirtualActivityDisplay) mActivityDisplay;
3751 if (virtualActivityDisplay == null) {
3752 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003753 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003754 mActivityDisplay = virtualActivityDisplay;
3755 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003756 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003757 }
3758
3759 if (mSurface != null) {
3760 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003761 }
3762
Craig Mautner6985bad2014-04-21 15:22:06 -07003763 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003764 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003765 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003766 } else {
3767 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003768 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003769 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003770 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003771 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003772 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003773
Craig Mautnerd163e752014-06-13 17:18:47 -07003774 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003775
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003776 if (DEBUG_STACK) Slog.d(TAG_STACK,
3777 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003778 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003779
Craig Mautner6985bad2014-04-21 15:22:06 -07003780 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003781 boolean isAttachedLocked() {
3782 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003783 }
3784
3785 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003786 void setDrawn() {
3787 synchronized (mService) {
3788 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003789 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003790 }
3791 }
3792
Craig Mautner1b4bf852014-05-26 15:06:32 -07003793 // Never start a new task on an ActivityView if it isn't explicitly specified.
3794 @Override
3795 boolean isEligibleForNewTasks() {
3796 return false;
3797 }
3798
Craig Mautnerd163e752014-06-13 17:18:47 -07003799 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003800 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003801 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3802 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3803 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3804 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3805 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003806 }
3807 }
3808
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3810 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003811 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003813 int mDisplayId;
3814 Display mDisplay;
3815 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003816
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3818 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003819 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820
Jose Lima4b6c6692014-08-12 17:41:12 -07003821 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003822
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003823 ActivityDisplay() {
3824 }
Craig Mautner4504de52013-12-20 09:06:56 -08003825
Craig Mautner1a70a162014-09-13 12:09:31 -07003826 // After instantiation, check that mDisplay is not null before using this. The alternative
3827 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003828 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003829 final Display display = mDisplayManager.getDisplay(displayId);
3830 if (display == null) {
3831 return;
3832 }
3833 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003834 }
3835
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003836 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003837 mDisplay = display;
3838 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003840 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003842 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003843 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003844 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3845 + " onTop=" + onTop);
3846 if (onTop) {
3847 mStacks.add(stack);
3848 } else {
3849 mStacks.add(0, stack);
3850 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003851 }
3852
Craig Mautnere0a38842013-12-16 16:14:02 -08003853 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003854 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003855 + " from displayId=" + mDisplayId);
3856 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 }
3858
Jose Lima4b6c6692014-08-12 17:41:12 -07003859 void setVisibleBehindActivity(ActivityRecord r) {
3860 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003861 }
3862
Jose Lima4b6c6692014-08-12 17:41:12 -07003863 boolean hasVisibleBehindActivity() {
3864 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003865 }
3866
Craig Mautner34b73df2014-01-12 21:11:08 -08003867 @Override
3868 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003869 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3870 }
3871 }
3872
3873 class VirtualActivityDisplay extends ActivityDisplay {
3874 VirtualDisplay mVirtualDisplay;
3875
Craig Mautner6985bad2014-04-21 15:22:06 -07003876 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003877 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003878 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3879 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3880 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3881 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003882
3883 init(mVirtualDisplay.getDisplay());
3884
3885 mWindowManager.handleDisplayAdded(mDisplayId);
3886 }
3887
3888 void setSurface(Surface surface) {
3889 if (mVirtualDisplay != null) {
3890 mVirtualDisplay.setSurface(surface);
3891 }
3892 }
3893
3894 @Override
3895 void detachActivitiesLocked(ActivityStack stack) {
3896 super.detachActivitiesLocked(stack);
3897 if (mVirtualDisplay != null) {
3898 mVirtualDisplay.release();
3899 mVirtualDisplay = null;
3900 }
3901 }
3902
3903 @Override
3904 public String toString() {
3905 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003906 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003907 }
Jose Lima58e66d62014-05-27 20:00:27 -07003908
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003909 /**
3910 * Adjust bounds to stay within stack bounds.
3911 *
3912 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3913 * that keep them unchanged, but be contained within the stack bounds.
3914 *
3915 * @param bounds Bounds to be adjusted.
3916 * @param stackBounds Bounds within which the other bounds should remain.
3917 */
3918 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3919 if (stackBounds == null || stackBounds.contains(bounds)) {
3920 return;
3921 }
3922
3923 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3924 final int maxRight = stackBounds.right
3925 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3926 int horizontalDiff = stackBounds.left - bounds.left;
3927 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3928 || (bounds.left + horizontalDiff >= maxRight)) {
3929 horizontalDiff = maxRight - bounds.left;
3930 }
3931 bounds.left += horizontalDiff;
3932 bounds.right += horizontalDiff;
3933 }
3934
3935 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3936 final int maxBottom = stackBounds.bottom
3937 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3938 int verticalDiff = stackBounds.top - bounds.top;
3939 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3940 || (bounds.top + verticalDiff >= maxBottom)) {
3941 verticalDiff = maxBottom - bounds.top;
3942 }
3943 bounds.top += verticalDiff;
3944 bounds.bottom += verticalDiff;
3945 }
3946 }
3947
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003948 ActivityStack findStackBehind(ActivityStack stack) {
3949 // TODO(multi-display): We are only looking for stacks on the default display.
3950 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3951 if (display == null) {
3952 return null;
3953 }
3954 final ArrayList<ActivityStack> stacks = display.mStacks;
3955 for (int i = stacks.size() - 1; i >= 0; i--) {
3956 if (stacks.get(i) == stack && i > 0) {
3957 return stacks.get(i - 1);
3958 }
3959 }
3960 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3961 + " in=" + stacks);
3962 }
Craig Mautner27084302013-03-25 08:05:25 -07003963}