blob: 8db2f8ff50f33c41458b3f63cba4909309e8b65d [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 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100406 * Used to keep track whether app visibilities got changed since the last pause. Useful to
407 * determine whether to invoke the task stack change listener after pausing.
408 */
409 boolean mAppVisibilitiesChangedSinceLastPause;
410
411 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700412 * Description of a request to start a new activity, which has been held
413 * due to app switches being disabled.
414 */
415 static class PendingActivityLaunch {
416 final ActivityRecord r;
417 final ActivityRecord sourceRecord;
418 final int startFlags;
419 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700420 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700421
422 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700423 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700424 r = _r;
425 sourceRecord = _sourceRecord;
426 startFlags = _startFlags;
427 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700428 callerApp = _callerApp;
429 }
430
431 void sendErrorResult(String message) {
432 try {
433 if (callerApp.thread != null) {
434 callerApp.thread.scheduleCrash(message);
435 }
436 } catch (RemoteException e) {
437 Slog.e(TAG, "Exception scheduling crash of failed "
438 + "activity launcher sourceRecord=" + sourceRecord, e);
439 }
Craig Mautneree36c772014-07-16 14:56:05 -0700440 }
441 }
442
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800443 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700444 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700445 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800446 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800447 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700448 }
449
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800450 void setRecentTasks(RecentTasks recentTasks) {
451 mRecentTasks = recentTasks;
452 }
453
Jeff Brown2c43c332014-06-12 22:38:59 -0700454 /**
455 * At the time when the constructor runs, the power manager has not yet been
456 * initialized. So we initialize our wakelocks afterwards.
457 */
458 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700460 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700461 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700462 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700463 }
464
justinzhang5286d3f2014-05-12 17:06:01 -0400465 // This function returns a IStatusBarService. The value is from ServiceManager.
466 // getService and is cached.
467 private IStatusBarService getStatusBarService() {
468 synchronized (mService) {
469 if (mStatusBarService == null) {
470 mStatusBarService = IStatusBarService.Stub.asInterface(
471 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
472 if (mStatusBarService == null) {
473 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
474 }
475 }
476 return mStatusBarService;
477 }
478 }
479
Jason Monk35c62a42014-06-17 10:24:47 -0400480 private IDevicePolicyManager getDevicePolicyManager() {
481 synchronized (mService) {
482 if (mDevicePolicyManager == null) {
483 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
484 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
485 if (mDevicePolicyManager == null) {
486 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
487 }
488 }
489 return mDevicePolicyManager;
490 }
491 }
492
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700493 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800494 synchronized (mService) {
495 mWindowManager = wm;
496
497 mDisplayManager =
498 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
499 mDisplayManager.registerDisplayListener(this, null);
500
501 Display[] displays = mDisplayManager.getDisplays();
502 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
503 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800504 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700505 if (activityDisplay.mDisplay == null) {
506 throw new IllegalStateException("Default Display does not exist");
507 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800508 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700509 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 }
511
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800512 mHomeStack = mFocusedStack = mLastFocusedStack =
513 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800514
515 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 }
Craig Mautner27084302013-03-25 08:05:25 -0700517 }
518
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200519 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700520 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200521 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700522 }
523
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700524 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800525 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700526 }
527
Craig Mautnerde4ef022013-04-07 19:01:33 -0700528 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700530 }
531
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700532 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700533 if (stack == null) {
534 return false;
535 }
536
Craig Mautnerdf88d732014-01-27 09:21:32 -0800537 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
538 if (parent != null) {
539 stack = parent.task.stack;
540 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800541 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700542 }
543
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700544 /** The top most stack. */
545 boolean isFrontStack(ActivityStack stack) {
546 if (stack == null) {
547 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800548 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700549
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700550 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
551 if (parent != null) {
552 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800553 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700554 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
555 }
556
Wale Ogunwaled046a012015-12-24 13:05:59 -0800557 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800558 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
559 if (!focusCandidate.isFocusable()) {
560 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
561 focusCandidate = focusCandidate.getNextFocusableStackLocked();
562 }
563
564 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800565 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800566 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800567
Wale Ogunwaled046a012015-12-24 13:05:59 -0800568 EventLogTags.writeAmFocusedStack(
569 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
570 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
571 }
572
573 final ActivityRecord r = topRunningActivityLocked();
574 if (mService.mFocusedActivity != r) {
575 // The focus activity should always be the top activity in the focused stack.
576 // There will be chaos and anarchy if it isn't...
577 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
578 }
Craig Mautnerde313752015-01-22 14:28:03 -0800579
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800580 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800581 if (r != null && r.idle) {
582 checkFinishBootingLocked();
583 }
584 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700585 }
586
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700587 void moveHomeStackToFront(String reason) {
588 mHomeStack.moveToFront(reason);
589 }
590
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700591 /** Returns true if the focus activity was adjusted to the home stack top activity. */
592 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700593 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
594 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700595 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700596 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700597
Craig Mautner84984fa2014-06-19 11:19:20 -0700598 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700599
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700600 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700601 if (top == null) {
602 return false;
603 }
604 mService.setFocusedActivityLocked(top, reason);
605 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700606 }
607
Craig Mautner299f9602015-01-26 09:47:33 -0800608 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700609 if (!mService.mBooting && !mService.mBooted) {
610 // Not ready yet!
611 return false;
612 }
613
Craig Mautner84984fa2014-06-19 11:19:20 -0700614 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
615 mWindowManager.showRecentApps();
616 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700617 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700618
Craig Mautner84984fa2014-06-19 11:19:20 -0700619 if (prev != null) {
620 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
621 }
622
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700623 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
624 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800625 final String myReason = reason + " resumeHomeStackTask";
626
Mark Lua56ea122015-10-08 13:31:01 +0800627 // Only resume home activity if isn't finishing.
628 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800629 mService.setFocusedActivityLocked(r, myReason);
630 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700631 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800632 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700633 }
634
Craig Mautner8d341ef2013-03-26 09:03:27 -0700635 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700636 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700637 }
638
639 /**
640 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
641 * @param id Id of the task we would like returned.
642 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
643 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700644 * @param stackId The stack to restore the task to (default launch stack will be used if
645 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700646 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700647 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800648 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800650 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
652 ActivityStack stack = stacks.get(stackNdx);
653 TaskRecord task = stack.taskForIdLocked(id);
654 if (task != null) {
655 return task;
656 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700657 }
658 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800659
660 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700661 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800662 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800663 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700664 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800665 return null;
666 }
667
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700668 if (!restoreFromRecents) {
669 return task;
670 }
671
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700672 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700673 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
674 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800675 return null;
676 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700677 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800678 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700679 }
680
Craig Mautner6170f732013-04-02 13:05:23 -0700681 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800682 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
687 if (r != null) {
688 return r;
689 }
Craig Mautner6170f732013-04-02 13:05:23 -0700690 }
691 }
692 return null;
693 }
694
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000695 boolean isFocusedUserLockedProfile() {
696 final int userId = mFocusedStack.topRunningActivityLocked().userId;
697 return userId != UserHandle.myUserId()
698 && mService.mUserController.shouldConfirmCredentials(userId);
699 }
700
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800701 void setNextTaskIdForUserLocked(int taskId, int userId) {
702 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
703 if (taskId > currentTaskId) {
704 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700705 }
706 }
707
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800708 int getNextTaskIdForUserLocked(int userId) {
709 mRecentTasks.loadUserRecentsLocked(userId);
710 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
711 // for a userId u, a taskId can only be in the range
712 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
713 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
714 int candidateTaskId = currentTaskId;
715 while (anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
716 INVALID_STACK_ID) != null) {
717 candidateTaskId++;
718 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
719 // Wrap around as there will be smaller task ids that are available now.
720 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700721 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800722 if (candidateTaskId == currentTaskId) {
723 // Something wrong!
724 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
725 throw new IllegalStateException("Cannot get an available task id."
726 + " Reached limit of " + MAX_TASK_IDS_PER_USER
727 + " running tasks per user.");
728 }
729 }
730 mCurTaskIdForUser.put(userId, candidateTaskId);
731 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700732 }
733
Craig Mautnerde4ef022013-04-07 19:01:33 -0700734 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800735 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700736 if (stack == null) {
737 return null;
738 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700739 ActivityRecord resumedActivity = stack.mResumedActivity;
740 if (resumedActivity == null || resumedActivity.app == null) {
741 resumedActivity = stack.mPausingActivity;
742 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700743 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700744 }
745 }
746 return resumedActivity;
747 }
748
Dianne Hackbornff072722014-09-24 10:56:28 -0700749 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700750 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800751 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
753 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
755 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700756 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800757 continue;
758 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700759 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 if (hr != null) {
761 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
762 && processName.equals(hr.processName)) {
763 try {
George Mount2c92c972014-03-20 09:38:23 -0700764 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 didSomething = true;
766 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700767 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 Slog.w(TAG, "Exception in new application when starting activity "
769 + hr.intent.getComponent().flattenToShortString(), e);
770 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700771 }
Craig Mautner20e72272013-04-01 13:45:53 -0700772 }
Craig Mautner20e72272013-04-01 13:45:53 -0700773 }
774 }
775 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700776 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700777 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700778 }
Craig Mautner20e72272013-04-01 13:45:53 -0700779 return didSomething;
780 }
781
782 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800783 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
784 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800785 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
786 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700787 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 continue;
789 }
790 final ActivityRecord resumedActivity = stack.mResumedActivity;
791 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700792 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800793 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 return false;
795 }
Craig Mautner20e72272013-04-01 13:45:53 -0700796 }
797 }
798 return true;
799 }
800
Craig Mautnerde4ef022013-04-07 19:01:33 -0700801 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
803 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
805 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700806 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700808 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 return false;
810 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 }
812 }
813 }
814 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700815 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
817 mLastFocusedStack + " to=" + mFocusedStack);
818 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700819 return true;
820 }
821
822 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800823 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
827 final ActivityStack stack = stacks.get(stackNdx);
828 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800829 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700830 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800831 return false;
832 }
833 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800834 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700835 }
836 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800837 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700838 }
839
Craig Mautner2acc3892013-09-23 10:28:14 -0700840 /**
841 * Pause all activities in either all of the stacks or just the back stacks.
842 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700843 * @return true if any activity was paused as a result of this call.
844 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700845 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700846 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800847 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
848 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
850 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700851 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700852 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700854 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
855 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700857 }
858 }
859 return someActivityPaused;
860 }
861
Craig Mautnerde4ef022013-04-07 19:01:33 -0700862 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700863 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800864 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
865 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
867 final ActivityStack stack = stacks.get(stackNdx);
868 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700869 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700871 Slog.d(TAG_STATES,
872 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800873 pausing = false;
874 } else {
875 return false;
876 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700877 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700878 }
879 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700880 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700881 }
882
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700883 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
884 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500885 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800886 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
887 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
888 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
889 final ActivityStack stack = stacks.get(stackNdx);
890 if (stack.mResumedActivity != null &&
891 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700892 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800893 }
894 }
895 }
896 }
897
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700898 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700899 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700900 }
901
902 void sendWaitingVisibleReportLocked(ActivityRecord r) {
903 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700904 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700905 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700906 if (w.who == null) {
907 changed = true;
908 w.timeout = false;
909 if (r != null) {
910 w.who = new ComponentName(r.info.packageName, r.info.name);
911 }
912 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
913 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700914 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700915 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700916 if (changed) {
917 mService.notifyAll();
918 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700919 }
920
921 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
922 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700923 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700924 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700925 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700926 if (w.who == null) {
927 changed = true;
928 w.timeout = timeout;
929 if (r != null) {
930 w.who = new ComponentName(r.info.packageName, r.info.name);
931 }
932 w.thisTime = thisTime;
933 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700934 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700935 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700936 if (changed) {
937 mService.notifyAll();
938 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700939 }
940
Craig Mautner29219d92013-04-16 20:19:12 -0700941 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800942 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700943 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700944 if (r != null) {
945 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700946 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700947
Craig Mautner4a1cb222013-12-04 16:14:06 -0800948 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800949 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
951 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800952 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700953 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700954 if (r != null) {
955 return r;
956 }
957 }
958 }
959 return null;
960 }
961
Dianne Hackborn09233282014-04-30 11:33:59 -0700962 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700963 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800964 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
965 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800966 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800968 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
970 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700971 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700973 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700974 }
975 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700976
977 // The lists are already sorted from most recent to oldest. Just pull the most recent off
978 // each list and add it to list. Stop when all lists are empty or maxNum reached.
979 while (maxNum > 0) {
980 long mostRecentActiveTime = Long.MIN_VALUE;
981 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800982 final int numTaskLists = runningTaskLists.size();
983 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
984 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700985 if (!stackTaskList.isEmpty()) {
986 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
987 if (lastActiveTime > mostRecentActiveTime) {
988 mostRecentActiveTime = lastActiveTime;
989 selectedStackList = stackTaskList;
990 }
991 }
992 }
993 if (selectedStackList != null) {
994 list.add(selectedStackList.remove(0));
995 --maxNum;
996 } else {
997 break;
998 }
999 }
Craig Mautner20e72272013-04-01 13:45:53 -07001000 }
1001
Todd Kennedy7440f172015-12-09 14:31:22 -08001002 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1003 ProfilerInfo profilerInfo) {
1004 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001005 if (aInfo != null) {
1006 // Store the found target back into the intent, because now that
1007 // we have it we never want to do this again. For example, if the
1008 // user navigates back to this point in the history, we should
1009 // always restart the exact same activity.
1010 intent.setComponent(new ComponentName(
1011 aInfo.applicationInfo.packageName, aInfo.name));
1012
1013 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001014 if (!aInfo.processName.equals("system")) {
1015 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 mService.setDebugApp(aInfo.processName, true, false);
1017 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018
Man Caocfa78b22015-06-11 20:14:34 -07001019 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1020 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1021 }
1022
1023 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001024 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 }
1026 }
1027 }
1028 return aInfo;
1029 }
1030
Todd Kennedy7440f172015-12-09 14:31:22 -08001031 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
1032 try {
1033 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
1034 PackageManager.MATCH_DEFAULT_ONLY
1035 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1036 } catch (RemoteException e) {
1037 }
1038 return null;
1039 }
1040
1041 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1042 ProfilerInfo profilerInfo, int userId) {
1043 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1044 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1045 }
1046
Craig Mautner2420ead2013-04-01 17:13:20 -07001047 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001048 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001049 throws RemoteException {
1050
Craig Mautner2568c3a2015-03-26 14:22:34 -07001051 if (andResume) {
1052 r.startFreezingScreenLocked(app, 0);
1053 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001054
Craig Mautner2568c3a2015-03-26 14:22:34 -07001055 // schedule launch ticks to collect information about slow apps.
1056 r.startLaunchTickingLocked();
1057 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001058
1059 // Have the window manager re-evaluate the orientation of
1060 // the screen based on the new activity order. Note that
1061 // as a result of this, it can call back into the activity
1062 // manager with a new orientation. We don't care about that,
1063 // because the activity is not currently running so we are
1064 // just restarting it anyway.
1065 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001067 mService.mConfiguration,
1068 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001069 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001070 }
1071
1072 r.app = app;
1073 app.waitingToKill = null;
1074 r.launchCount++;
1075 r.lastLaunchTime = SystemClock.uptimeMillis();
1076
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001077 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001078
1079 int idx = app.activities.indexOf(r);
1080 if (idx < 0) {
1081 app.activities.add(r);
1082 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001083 mService.updateLruProcessLocked(app, true, null);
1084 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001085
Craig Mautner15df08a2015-04-01 12:17:18 -07001086 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001087 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1088 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001089 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001090 }
1091
1092 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001093 try {
1094 if (app.thread == null) {
1095 throw new RemoteException();
1096 }
1097 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001098 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 if (andResume) {
1100 results = r.results;
1101 newIntents = r.newIntents;
1102 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001103 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1104 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1105 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001106 if (andResume) {
1107 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1108 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001109 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001110 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001111 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001112 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001113 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001114 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001115 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001116 r.sleeping = false;
1117 r.forceNewConfig = false;
1118 mService.showAskCompatModeDialogLocked(r);
1119 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001120 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001121 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1122 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1123 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001124 final String profileFile = mService.mProfileFile;
1125 if (profileFile != null) {
1126 ParcelFileDescriptor profileFd = mService.mProfileFd;
1127 if (profileFd != null) {
1128 try {
1129 profileFd = profileFd.dup();
1130 } catch (IOException e) {
1131 if (profileFd != null) {
1132 try {
1133 profileFd.close();
1134 } catch (IOException o) {
1135 }
1136 profileFd = null;
1137 }
1138 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001139 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001140
1141 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1142 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001143 }
1144 }
1145 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001146
Craig Mautner2568c3a2015-03-26 14:22:34 -07001147 if (andResume) {
1148 app.hasShownUi = true;
1149 app.pendingUiClean = true;
1150 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001151 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001152 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001153 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001154 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001155 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001156 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001157
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001158 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 // This may be a heavy-weight process! Note that the package
1160 // manager will ensure that only activity can run in the main
1161 // process of the .apk, which is the only thing that will be
1162 // considered heavy-weight.
1163 if (app.processName.equals(app.info.packageName)) {
1164 if (mService.mHeavyWeightProcess != null
1165 && mService.mHeavyWeightProcess != app) {
1166 Slog.w(TAG, "Starting new heavy weight process " + app
1167 + " when already running "
1168 + mService.mHeavyWeightProcess);
1169 }
1170 mService.mHeavyWeightProcess = app;
1171 Message msg = mService.mHandler.obtainMessage(
1172 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1173 msg.obj = r;
1174 mService.mHandler.sendMessage(msg);
1175 }
1176 }
1177
1178 } catch (RemoteException e) {
1179 if (r.launchFailed) {
1180 // This is the second time we failed -- finish activity
1181 // and give up.
1182 Slog.e(TAG, "Second failure launching "
1183 + r.intent.getComponent().flattenToShortString()
1184 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001185 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1187 "2nd-crash", false);
1188 return false;
1189 }
1190
1191 // This is the first time we failed -- restart process and
1192 // retry.
1193 app.activities.remove(r);
1194 throw e;
1195 }
1196
1197 r.launchFailed = false;
1198 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001199 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001200 }
1201
1202 if (andResume) {
1203 // As part of the process of launching, ActivityThread also performs
1204 // a resume.
1205 stack.minimalResumeActivityLocked(r);
1206 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001207 // This activity is not starting in the resumed state... which should look like we asked
1208 // it to pause+stop (but remain visible), and it has done so and reported back the
1209 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001210 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001211 "Moving to PAUSED: " + r + " (starting in paused state)");
1212 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001213 }
1214
1215 // Launch the new version setup screen if needed. We do this -after-
1216 // launching the initial activity (that is, home), so that it can have
1217 // a chance to initialize itself while in the background, making the
1218 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001219 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 mService.startSetupActivityLocked();
1221 }
1222
Dianne Hackborn465fa392014-09-14 14:21:18 -07001223 // Update any services we are bound to that might care about whether
1224 // their client may have activities.
1225 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1226
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 return true;
1228 }
1229
Craig Mautnere79d42682013-04-01 19:01:53 -07001230 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001231 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001232 // Is this activity's application already running?
1233 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001234 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001235
1236 r.task.stack.setLaunchTime(r);
1237
1238 if (app != null && app.thread != null) {
1239 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001240 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1241 || !"android".equals(r.info.packageName)) {
1242 // Don't add this if it is a platform component that is marked
1243 // to run in multiple processes, because this is actually
1244 // part of the framework so doesn't make sense to track as a
1245 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001246 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1247 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001248 }
George Mount2c92c972014-03-20 09:38:23 -07001249 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001250 return;
1251 } catch (RemoteException e) {
1252 Slog.w(TAG, "Exception when starting activity "
1253 + r.intent.getComponent().flattenToShortString(), e);
1254 }
1255
1256 // If a dead object exception was thrown -- fall through to
1257 // restart the application.
1258 }
1259
1260 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001261 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001262 }
1263
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001264 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001265 String resultWho, int requestCode, int callingPid, int callingUid,
1266 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1267 ActivityRecord resultRecord, ActivityStack resultStack) {
1268 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1269 callingUid);
1270 if (startAnyPerm == PERMISSION_GRANTED) {
1271 return true;
1272 }
1273 final int componentRestriction = getComponentRestrictionForCallingPackage(
1274 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1275 final int actionRestriction = getActionRestrictionForCallingPackage(
1276 intent.getAction(), callingPackage, callingPid, callingUid);
1277 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1278 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1279 if (resultRecord != null) {
1280 resultStack.sendActivityResultLocked(-1,
1281 resultRecord, resultWho, requestCode,
1282 Activity.RESULT_CANCELED, null);
1283 }
1284 final String msg;
1285 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1286 msg = "Permission Denial: starting " + intent.toString()
1287 + " from " + callerApp + " (pid=" + callingPid
1288 + ", uid=" + callingUid + ")" + " with revoked permission "
1289 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1290 } else if (!aInfo.exported) {
1291 msg = "Permission Denial: starting " + intent.toString()
1292 + " from " + callerApp + " (pid=" + callingPid
1293 + ", uid=" + callingUid + ")"
1294 + " not exported from uid " + aInfo.applicationInfo.uid;
1295 } else {
1296 msg = "Permission Denial: starting " + intent.toString()
1297 + " from " + callerApp + " (pid=" + callingPid
1298 + ", uid=" + callingUid + ")"
1299 + " requires " + aInfo.permission;
1300 }
1301 Slog.w(TAG, msg);
1302 throw new SecurityException(msg);
1303 }
1304
1305 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1306 final String message = "Appop Denial: starting " + intent.toString()
1307 + " from " + callerApp + " (pid=" + callingPid
1308 + ", uid=" + callingUid + ")"
1309 + " requires " + AppOpsManager.permissionToOp(
1310 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1311 Slog.w(TAG, message);
1312 return false;
1313 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1314 final String message = "Appop Denial: starting " + intent.toString()
1315 + " from " + callerApp + " (pid=" + callingPid
1316 + ", uid=" + callingUid + ")"
1317 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1318 Slog.w(TAG, message);
1319 return false;
1320 }
1321 return true;
1322 }
1323
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001324 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001325 final long identity = Binder.clearCallingIdentity();
1326 try {
1327 return UserManager.get(mService.mContext).getUserInfo(userId);
1328 } finally {
1329 Binder.restoreCallingIdentity(identity);
1330 }
1331 }
1332
Svet Ganov99b60432015-06-27 13:15:22 -07001333 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001334 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001335 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1336 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001337 == PackageManager.PERMISSION_DENIED) {
1338 return ACTIVITY_RESTRICTION_PERMISSION;
1339 }
1340
Christopher Tateff7add02015-08-17 10:23:22 -07001341 if (activityInfo.permission == null) {
1342 return ACTIVITY_RESTRICTION_NONE;
1343 }
1344
Svet Ganov99b60432015-06-27 13:15:22 -07001345 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1346 if (opCode == AppOpsManager.OP_NONE) {
1347 return ACTIVITY_RESTRICTION_NONE;
1348 }
1349
1350 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1351 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001352 if (!ignoreTargetSecurity) {
1353 return ACTIVITY_RESTRICTION_APPOP;
1354 }
Svet Ganov99b60432015-06-27 13:15:22 -07001355 }
1356
1357 return ACTIVITY_RESTRICTION_NONE;
1358 }
1359
Svetoslav7008b512015-06-24 18:47:07 -07001360 private int getActionRestrictionForCallingPackage(String action,
1361 String callingPackage, int callingPid, int callingUid) {
1362 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001363 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001364 }
1365
1366 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1367 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001368 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001369 }
1370
1371 final PackageInfo packageInfo;
1372 try {
1373 packageInfo = mService.mContext.getPackageManager()
1374 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1375 } catch (PackageManager.NameNotFoundException e) {
1376 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001377 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001378 }
1379
1380 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001381 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001382 }
1383
1384 if (mService.checkPermission(permission, callingPid, callingUid) ==
1385 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001386 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001387 }
1388
1389 final int opCode = AppOpsManager.permissionToOpCode(permission);
1390 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001391 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001392 }
1393
1394 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1395 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001396 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001397 }
1398
Svet Ganov99b60432015-06-27 13:15:22 -07001399 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001400 }
1401
Wale Ogunwaled046a012015-12-24 13:05:59 -08001402 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001403 if (r == null) {
1404 // Not sure what you are trying to do, but it is not going to work...
1405 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001406 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001407 final TaskRecord task = r.task;
1408 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001409 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001410 return false;
1411 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001412 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001413 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001414 }
1415
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001416 void setLaunchSource(int uid) {
1417 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1418 }
1419
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001420 void acquireLaunchWakelock() {
1421 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1422 throw new IllegalStateException("Calling must be system uid");
1423 }
1424 mLaunchingActivity.acquire();
1425 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1426 // To be safe, don't allow the wake lock to be held for too long.
1427 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1428 }
1429 }
1430
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001431 /**
1432 * Called when the frontmost task is idle.
1433 * @return the state of mService.mBooting before this was called.
1434 */
1435 private boolean checkFinishBootingLocked() {
1436 final boolean booting = mService.mBooting;
1437 boolean enableScreen = false;
1438 mService.mBooting = false;
1439 if (!mService.mBooted) {
1440 mService.mBooted = true;
1441 enableScreen = true;
1442 }
1443 if (booting || enableScreen) {
1444 mService.postFinishBooting(booting, enableScreen);
1445 }
1446 return booting;
1447 }
1448
Craig Mautnerf3333272013-04-22 10:55:53 -07001449 // Checked.
1450 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1451 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001452 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001453
Craig Mautnerf3333272013-04-22 10:55:53 -07001454 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001455 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001456 int NS = 0;
1457 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001458 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001459 boolean activityRemoved = false;
1460
Wale Ogunwale7d701172015-03-11 15:36:30 -07001461 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001462 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001463 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1464 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001465 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1466 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001467 if (fromTimeout) {
1468 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001469 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001470
1471 // This is a hack to semi-deal with a race condition
1472 // in the client where it can be constructed with a
1473 // newer configuration from when we asked it to launch.
1474 // We'll update with whatever configuration it now says
1475 // it used to launch.
1476 if (config != null) {
1477 r.configuration = config;
1478 }
1479
1480 // We are now idle. If someone is waiting for a thumbnail from
1481 // us, we can now deliver.
1482 r.idle = true;
1483
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001484 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001485 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001486 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001487 }
1488 }
1489
1490 if (allResumedActivitiesIdle()) {
1491 if (r != null) {
1492 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001493 }
1494
1495 if (mLaunchingActivity.isHeld()) {
1496 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1497 if (VALIDATE_WAKE_LOCK_CALLER &&
1498 Binder.getCallingUid() != Process.myUid()) {
1499 throw new IllegalStateException("Calling must be system uid");
1500 }
1501 mLaunchingActivity.release();
1502 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001503 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001504 }
1505
1506 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001507 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001508 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001509 if ((NF = mFinishingActivities.size()) > 0) {
1510 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001511 mFinishingActivities.clear();
1512 }
1513
Craig Mautnerf3333272013-04-22 10:55:53 -07001514 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001515 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001516 mStartingUsers.clear();
1517 }
1518
Craig Mautnerf3333272013-04-22 10:55:53 -07001519 // Stop any activities that are scheduled to do so but have been
1520 // waiting for the next one to start.
1521 for (int i = 0; i < NS; i++) {
1522 r = stops.get(i);
1523 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001524 if (stack != null) {
1525 if (r.finishing) {
1526 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1527 } else {
1528 stack.stopActivityLocked(r);
1529 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001530 }
1531 }
1532
1533 // Finish any activities that are scheduled to do so but have been
1534 // waiting for the next one to start.
1535 for (int i = 0; i < NF; i++) {
1536 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001537 final ActivityStack stack = r.task.stack;
1538 if (stack != null) {
1539 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1540 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001541 }
1542
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001543 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001544 // Complete user switch
1545 if (startingUsers != null) {
1546 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001547 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001548 }
1549 }
1550 // Complete starting up of background users
1551 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001552 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001553 mStartingBackgroundUsers.clear();
1554 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001555 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001556 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001557 }
1558 }
1559
1560 mService.trimApplications();
1561 //dump();
1562 //mWindowManager.dump();
1563
Craig Mautnerf3333272013-04-22 10:55:53 -07001564 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001565 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001566 }
1567
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001568 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001569 }
1570
Craig Mautner8e569572013-10-11 17:36:59 -07001571 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001572 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001573 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1574 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001575 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1576 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1577 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001578 }
Craig Mautner19091252013-10-05 00:03:53 -07001579 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001580 }
1581
1582 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001585 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1586 stacks.get(stackNdx).closeSystemDialogsLocked();
1587 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001588 }
1589 }
1590
Craig Mautner93529a42013-10-04 15:03:13 -07001591 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001592 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001593 }
1594
Craig Mautner8d341ef2013-03-26 09:03:27 -07001595 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001596 * Update the last used stack id for non-current user (current user's last
1597 * used stack is the focused stack)
1598 */
1599 void updateUserStackLocked(int userId, ActivityStack stack) {
1600 if (userId != mCurrentUser) {
1601 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1602 }
1603 }
1604
1605 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001606 * @return true if some activity was finished (or would have finished if doit were true).
1607 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001608 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1609 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001610 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001611 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1612 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001613 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001614 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001615 if (stack.finishDisabledPackageActivitiesLocked(
1616 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001617 didSomething = true;
1618 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001619 }
1620 }
1621 return didSomething;
1622 }
1623
Dianne Hackborna413dc02013-07-12 12:02:55 -07001624 void updatePreviousProcessLocked(ActivityRecord r) {
1625 // Now that this process has stopped, we may want to consider
1626 // it to be the previous app to try to keep around in case
1627 // the user wants to return to it.
1628
1629 // First, found out what is currently the foreground app, so that
1630 // we don't blow away the previous app if this activity is being
1631 // hosted by the process that is actually still the foreground.
1632 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1634 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001635 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1636 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001637 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001638 if (stack.mResumedActivity != null) {
1639 fgApp = stack.mResumedActivity.app;
1640 } else if (stack.mPausingActivity != null) {
1641 fgApp = stack.mPausingActivity.app;
1642 }
1643 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001644 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001645 }
1646 }
1647
1648 // Now set this one as the previous process, only if that really
1649 // makes sense to.
1650 if (r.app != null && fgApp != null && r.app != fgApp
1651 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001652 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001653 mService.mPreviousProcess = r.app;
1654 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1655 }
1656 }
1657
Wale Ogunwaled046a012015-12-24 13:05:59 -08001658 boolean resumeFocusedStackTopActivityLocked() {
1659 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001660 }
1661
Wale Ogunwaled046a012015-12-24 13:05:59 -08001662 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001663 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001664 if (targetStack != null && isFocusedStack(targetStack)) {
1665 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001666 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001667 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1668 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001669 }
1670
Adrian Roos20d7df32016-01-12 18:59:43 +01001671 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1672 TaskRecord finishedTask = null;
1673 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001676 final int numStacks = stacks.size();
1677 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1678 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001679 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1680 if (stack == focusedStack || finishedTask == null) {
1681 finishedTask = t;
1682 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001683 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001684 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001685 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001686 }
1687
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001688 void finishVoiceTask(IVoiceInteractionSession session) {
1689 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1690 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1691 final int numStacks = stacks.size();
1692 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1693 final ActivityStack stack = stacks.get(stackNdx);
1694 stack.finishVoiceTask(session);
1695 }
1696 }
1697 }
1698
Chong Zhang280d3322015-11-03 17:27:26 -08001699 void findTaskToMoveToFrontLocked(
1700 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001701 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1702 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001703 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001704 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1705 // Caller wants the home activity moved with it. To accomplish this,
1706 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001707 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001708 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001709 if (task.stack == null) {
1710 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1711 + task + " to front. Stack is null");
1712 return;
1713 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001714
Chong Zhang0fa656b2015-08-31 15:17:21 -07001715 if (task.mResizeable && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001716 int stackId = options.getLaunchStackId();
1717 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001718 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001719 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001720 if (stackId == INVALID_STACK_ID) {
1721 stackId = task.getLaunchStackId();
1722 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001723 if (stackId != task.stack.mStackId) {
1724 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1725 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1726 // still need moveTaskToFrontLocked() below for any transition settings.
1727 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001728 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1729 resizeStackLocked(stackId, bounds,
1730 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1731 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1732 } else {
1733 // WM resizeTask must be done after the task is moved to the correct stack,
1734 // because Task's setBounds() also updates dim layer's bounds, but that has
1735 // dependency on the stack.
1736 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1737 false /* relayout */, false /* forced */);
1738 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001739 }
1740 }
1741
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001742 final ActivityRecord r = task.getTopActivity();
1743 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1744 r == null ? null : r.appTimeTracker, reason);
1745
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001746 if (DEBUG_STACK) Slog.d(TAG_STACK,
1747 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001748 }
1749
Wale Ogunwale854809c2015-12-27 16:18:19 -08001750 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001751 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001752 // window management or is launching into the pinned stack.
1753 if (!options.hasLaunchBounds()) {
1754 return false;
1755 }
1756 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1757 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001758 }
1759
Craig Mautner967212c2013-04-13 21:10:58 -07001760 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001761 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001762 }
1763
1764 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001765 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1766 if (activityContainer != null) {
1767 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001768 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001769 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001770 return null;
1771 }
1772 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001773 }
1774
Craig Mautner967212c2013-04-13 21:10:58 -07001775 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001776 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001777 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1778 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001779 }
1780 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001781 }
1782
Craig Mautner4a1cb222013-12-04 16:14:06 -08001783 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001784 ActivityRecord homeActivity = getHomeActivity();
1785 if (homeActivity != null) {
1786 return homeActivity.appToken;
1787 }
1788 return null;
1789 }
1790
1791 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001792 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001793 }
1794
1795 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001796 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1797 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1798 final TaskRecord task = tasks.get(taskNdx);
1799 if (task.isHomeTask()) {
1800 final ArrayList<ActivityRecord> activities = task.mActivities;
1801 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1802 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001803 if (r.isHomeActivity()
1804 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001805 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001806 }
1807 }
1808 }
1809 }
1810 return null;
1811 }
1812
Chong Zhangb15758a2015-11-17 12:12:03 -08001813 /**
1814 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1815 * the docked stack itself, or if it's side-by-side to the docked stack.
1816 */
1817 boolean isStackDockedInEffect(int stackId) {
1818 return stackId == DOCKED_STACK_ID ||
1819 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1820 }
1821
Todd Kennedyca4d8422015-01-15 15:19:22 -08001822 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001823 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001824 ActivityContainer activityContainer =
1825 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001826 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001827 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1828 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001829 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001830 return activityContainer;
1831 }
1832
Craig Mautner34b73df2014-01-12 21:11:08 -08001833 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001834 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1835 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1836 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001837 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1838 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001839 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001840 }
1841 }
1842
Craig Mautner95da1082014-02-24 17:54:35 -08001843 void deleteActivityContainer(IActivityContainer container) {
1844 ActivityContainer activityContainer = (ActivityContainer)container;
1845 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001846 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1847 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001848 final int stackId = activityContainer.mStackId;
1849 mActivityContainers.remove(stackId);
1850 mWindowManager.removeStack(stackId);
1851 }
1852 }
1853
Jorim Jaggidc249c42015-12-15 14:57:31 -08001854 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1855 boolean preserveWindows, boolean allowResizeInDockedMode) {
1856 if (stackId == DOCKED_STACK_ID) {
1857 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1858 preserveWindows);
1859 return;
1860 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001861 final ActivityStack stack = getStack(stackId);
1862 if (stack == null) {
1863 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1864 return;
1865 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001866
Jorim Jaggidc249c42015-12-15 14:57:31 -08001867 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001868 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1869 // stack size changing so things don't get out of sync.
1870 return;
1871 }
1872
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001873 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001874 mWindowManager.deferSurfaceLayout();
1875 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001876 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1877 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001878 } finally {
1879 mWindowManager.continueSurfaceLayout();
1880 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001881 }
1882 }
1883
Jorim Jaggidc249c42015-12-15 14:57:31 -08001884 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1885 Rect tempTaskInsetBounds) {
1886 if (bounds != null && mWindowManager.isFullscreenBounds(stack.mStackId, bounds)) {
1887 // The bounds passed in corresponds to the fullscreen bounds which we normally
1888 // represent with null. Go ahead and set it to null so that all tasks configuration
1889 // can have the right fullscreen state.
1890 bounds = null;
1891 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001892 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001893
1894 mTmpBounds.clear();
1895 mTmpConfigs.clear();
1896 mTmpInsetBounds.clear();
1897 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1898 for (int i = tasks.size() - 1; i >= 0; i--) {
1899 TaskRecord task = tasks.get(i);
1900 if (task.mResizeable) {
1901 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1902 // For freeform stack we don't adjust the size of the tasks to match that
1903 // of the stack, but we do try to make sure the tasks are still contained
1904 // with the bounds of the stack.
1905 tempRect2.set(task.mBounds);
1906 fitWithinBounds(tempRect2, bounds);
1907 task.updateOverrideConfiguration(tempRect2);
1908 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001909 task.updateOverrideConfiguration(
1910 tempTaskBounds != null ? tempTaskBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001911 }
1912 }
1913
1914 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1915 mTmpBounds.put(task.taskId, task.mBounds);
1916 if (tempTaskInsetBounds != null) {
1917 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1918 }
1919 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001920
1921 // We might trigger a configuration change. Save the current task bounds for freezing.
1922 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001923 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1924 mTmpBounds, mTmpInsetBounds);
1925 stack.setBounds(bounds);
1926 }
1927
1928 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1929 boolean preserveWindows) {
1930 if (r == null) {
1931 return;
1932 }
1933 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1934 preserveWindows);
1935 // And we need to make sure at this point that all other activities
1936 // are made visible with the correct configuration.
1937 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1938 if (!updated) {
1939 resumeFocusedStackTopActivityLocked();
1940 }
1941 }
1942
1943 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1944 Rect tempDockedTaskInsetBounds,
1945 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1946 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1947 if (stack == null) {
1948 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1949 return;
1950 }
1951
1952 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1953 mWindowManager.deferSurfaceLayout();
1954 try {
1955 ActivityRecord r = stack.topRunningActivityLocked();
1956 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1957 tempDockedTaskInsetBounds);
1958
1959 if (stack.mFullscreen) {
1960 // The dock stack went fullscreen which is kinda like dismissing it.
1961 // In this case we make all other static stacks fullscreen and move all
1962 // docked stack tasks to the fullscreen stack.
1963 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1964 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1965 resizeStackLocked(i, null, null, null, preserveWindows,
1966 true /* allowResizeInDockedMode */);
1967 }
1968 }
1969
1970 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1971 final int count = tasks.size();
1972 for (int i = 0; i < count; i++) {
1973 moveTaskToStackLocked(tasks.get(i).taskId,
1974 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
1975 false /* animate */);
1976 }
1977
1978 // stack shouldn't contain anymore activities, so nothing to resume.
1979 r = null;
1980 } else {
1981 // Docked stacks occupy a dedicated region on screen so the size of all other
1982 // static stacks need to be adjusted so they don't overlap with the docked stack.
1983 // We get the bounds to use from window manager which has been adjusted for any
1984 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08001985 mWindowManager.getStackDockedModeBounds(
1986 HOME_STACK_ID, tempRect, true /* ignoreVisibilityOnKeyguardShowing */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001987 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1988 if (StackId.isResizeableByDockedStack(i)) {
1989 ActivityStack otherStack = getStack(i);
1990 if (otherStack != null) {
1991 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
1992 tempOtherTaskInsetBounds, preserveWindows,
1993 true /* allowResizeInDockedMode */);
1994 }
1995 }
1996 }
1997 }
1998 ensureConfigurationAndResume(stack, r, preserveWindows);
1999 } finally {
2000 mWindowManager.continueSurfaceLayout();
2001 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2002 }
2003
2004 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2005 tempDockedTaskBounds != null
2006 || tempDockedTaskInsetBounds != null
2007 || tempOtherTaskBounds != null
2008 || tempOtherTaskInsetBounds != null);
2009 }
2010
Chong Zhangf596cd52016-01-05 13:42:44 -08002011 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002012 if (!task.mResizeable) {
2013 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002014 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002015 }
2016
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002017 adjustForMinimalTaskDimensions(task, bounds);
2018
Chong Zhang87b21722015-09-21 15:39:51 -07002019 // If this is a forced resize, let it go through even if the bounds is not changing,
2020 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002021 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07002022 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002023 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002024 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002025 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002026 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002027
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002028 if (!mWindowManager.isValidTaskId(task.taskId)) {
2029 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002030 // All we can do for now is update the bounds so it can be used when the task is
2031 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002032 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002033 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2034 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002035 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002036 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002037 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002038 }
2039
Chong Zhang6de2ae82015-09-30 18:25:21 -07002040 // Do not move the task to another stack here.
2041 // This method assumes that the task is already placed in the right stack.
2042 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002043
Chong Zhang6de2ae82015-09-30 18:25:21 -07002044 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002045
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002046 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002047 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002048 // way and the activity was kept the way it was. If it's false, it means the activity had
2049 // to be relaunched due to configuration change.
2050 boolean kept = true;
2051 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002052 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002053 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002054 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002055 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002056 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002057 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002058 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002059 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002060 }
2061 }
2062 }
Chong Zhang87b21722015-09-21 15:39:51 -07002063 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002064
2065 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002066 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002067 }
2068
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002069 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
2070 if (bounds == null) {
2071 return;
2072 }
2073 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
2074 : task.mMinimalSize;
2075 final boolean adjustWidth = minimalSize > bounds.width();
2076 final boolean adjustHeight = minimalSize > bounds.height();
2077 if (!(adjustWidth || adjustHeight)) {
2078 return;
2079 }
2080 Rect taskBounds = task.mBounds;
2081 if (adjustWidth) {
2082 if (taskBounds != null && bounds.right == taskBounds.right) {
2083 bounds.left = bounds.right - minimalSize;
2084 } else {
2085 // Either left bounds match, or neither match, or the previous bounds were
2086 // fullscreen and we default to keeping left.
2087 bounds.right = bounds.left + minimalSize;
2088 }
2089 }
2090 if (adjustHeight) {
2091 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
2092 bounds.top = bounds.bottom - minimalSize;
2093 } else {
2094 // Either top bounds match, or neither match, or the previous bounds were
2095 // fullscreen and we default to keeping top.
2096 bounds.bottom = bounds.top + minimalSize;
2097 }
2098 }
2099 }
2100
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002101 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002102 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2103 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002104 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002105 }
2106
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002107 ActivityContainer activityContainer = new ActivityContainer(stackId);
2108 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002109 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002110 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002111 }
2112
2113 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002114 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002115 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2116 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002117 break;
2118 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002119 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002120 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002121 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002122 }
2123
Chong Zhang5dcb2752015-08-18 13:50:26 -07002124 /**
2125 * Restores a recent task to a stack
2126 * @param task The recent task to be restored.
2127 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002128 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002129 * @return true if the task has been restored successfully.
2130 */
2131 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2132 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002133 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002134 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002135 if (task.stack != null) {
2136 // Task has already been restored once. See if we need to do anything more
2137 if (task.stack.mStackId == stackId) {
2138 // Nothing else to do since it is already restored in the right stack.
2139 return true;
2140 }
2141 // Remove current stack association, so we can re-associate the task with the
2142 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002143 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002144 }
2145
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002146 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002147 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002148
2149 if (stack == null) {
2150 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002151 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2152 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002153 return false;
2154 }
2155
Wale Ogunwale5f986092015-12-04 15:35:38 -08002156 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002157 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2158 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002159 final ArrayList<ActivityRecord> activities = task.mActivities;
2160 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002161 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002162 }
2163 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002164 }
2165
Wale Ogunwale040b4702015-08-06 18:10:50 -07002166 /**
2167 * Moves the specified task record to the input stack id.
2168 * WARNING: This method performs an unchecked/raw move of the task and
2169 * can leave the system in an unstable state if used incorrectly.
2170 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2171 * to a stack.
2172 * @param task Task to move.
2173 * @param stackId Id of stack to move task to.
2174 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002175 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002176 * @param reason Reason the task is been moved.
2177 * @return The stack the task was moved to.
2178 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002179 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002180 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2181 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002182 final ActivityStack prevStack = task.stack;
2183 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2184 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2185 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2186 // Whenever we are moving the top activity from the front stack we want to make sure to move
2187 // the stack to the front.
2188 final boolean wasFront = isFrontStack(prevStack)
2189 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002190
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002191 final boolean resizeable = task.mResizeable;
2192 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2193 // if a docked stack is created below which will lead to the stack we are moving from and
2194 // its resizeable tasks being resized.
2195 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002196 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002197 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002198 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002199 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002200
2201 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002202 // move focus to the new stack by moving the stack to the front.
2203 stack.moveToFrontAndResumeStateIfNeeded(
2204 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002205
Wale Ogunwale040b4702015-08-06 18:10:50 -07002206 return stack;
2207 }
2208
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002209 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002210 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002211 final TaskRecord task = anyTaskForIdLocked(taskId);
2212 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002213 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002214 return;
2215 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002216
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002217 if (task.stack != null && task.stack.mStackId == stackId) {
2218 // You are already in the right stack silly...
2219 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2220 return;
2221 }
2222
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002223 final ActivityRecord topActivity = task.getTopActivity();
Chong Zhangf596cd52016-01-05 13:42:44 -08002224 final boolean mightReplaceWindow =
2225 StackId.preserveWindowOnTaskMove(stackId) && topActivity != null;
2226 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002227 // We are about to relaunch the activity because its configuration changed due to
2228 // being maximized, i.e. size change. The activity will first remove the old window
2229 // and then add a new one. This call will tell window manager about this, so it can
2230 // preserve the old window until the new one is drawn. This prevents having a gap
2231 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002232 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002233 // Note here we always set the replacing window first, as the flags might be needed
2234 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002235 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002236 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002237 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2238 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002239
Jorim Jaggi55387522015-11-24 18:21:10 -08002240 if (!animate) {
2241 stack.mNoAnimActivities.add(topActivity);
2242 }
2243
Chong Zhangf596cd52016-01-05 13:42:44 -08002244 boolean kept = true;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002245 // Make sure the task has the appropriate bounds/size for the stack it is in.
2246 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002247 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002248 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2249 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002250 kept = resizeTaskLocked(task, task.mLastNonFullscreenBounds,
Chong Zhang6de2ae82015-09-30 18:25:21 -07002251 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002252 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhangf596cd52016-01-05 13:42:44 -08002253 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
2254 }
2255
2256 if (mightReplaceWindow) {
2257 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2258 // window), we need to clear the replace window settings. Otherwise, we schedule a
2259 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski2443f2f2016-01-21 10:30:45 -08002260 // In case of the pinned stack we don't resize the task during the move, but we will
2261 // resize the stack soon after so we want to retain the replacing window.
2262 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken,
2263 !kept || stackId == PINNED_STACK_ID);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002264 }
2265
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002266 // The task might have already been running and its visibility needs to be synchronized with
2267 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002268 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002269 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002270
2271 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002272 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002273 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002274 }
2275
Wale Ogunwale079a0042015-10-24 11:44:07 -07002276 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2277 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2278 if (stack == null) {
2279 throw new IllegalArgumentException(
2280 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2281 }
2282
2283 final ActivityRecord r = stack.topRunningActivityLocked();
2284 if (r == null) {
2285 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2286 + " in stack=" + stack);
2287 return false;
2288 }
2289
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002290 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002291 Slog.w(TAG,
2292 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002293 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002294 return false;
2295 }
2296
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002297 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2298 return true;
2299 }
2300
2301 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002302 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002303 if (task.mActivities.size() == 1) {
2304 // 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 -08002305 // stack without re-parenting the activity in a different task.
2306 moveTaskToStackLocked(
2307 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002308 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002309 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2310 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002311 }
2312
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002313 if (bounds != null) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002314 resizeStackLocked(stackId, bounds, null /* tempTaskBounds */,
2315 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002316 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002317
2318 // The task might have already been running and its visibility needs to be synchronized with
2319 // the visibility of the stack / windows.
2320 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002321 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002322
2323 if (stackId == PINNED_STACK_ID) {
2324 mService.notifyActivityPinnedLocked();
2325 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002326 }
2327
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002328 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2329 final TaskRecord task = anyTaskForIdLocked(taskId);
2330 if (task == null) {
2331 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2332 return;
2333 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002334 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2335
2336 task.updateOverrideConfigurationForStack(stack);
2337
2338 mWindowManager.positionTaskInStack(
2339 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002340 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002341 // The task might have already been running and its visibility needs to be synchronized with
2342 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002343 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002344 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002345 }
2346
Craig Mautnerac6f8432013-07-17 13:24:59 -07002347 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002348 mTmpFindTaskResult.r = null;
2349 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002350 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002351 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2352 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002353 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2354 final ActivityStack stack = stacks.get(stackNdx);
2355 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002356 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002357 continue;
2358 }
2359 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002360 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2361 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002362 continue;
2363 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002364 stack.findTaskLocked(r, mTmpFindTaskResult);
2365 // It is possible to have task in multiple stacks with the same root affinity.
2366 // If the match we found was based on root affinity we keep on looking to see if
2367 // there is a better match in another stack. We eventually return the match based
2368 // on root affinity if we don't find a better match.
2369 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2370 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002371 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002372 }
2373 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002374 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2375 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 }
2377
2378 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002379 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2380 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002381 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2382 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2383 if (ar != null) {
2384 return ar;
2385 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 }
2387 }
2388 return null;
2389 }
2390
Craig Mautner8d341ef2013-03-26 09:03:27 -07002391 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002392 scheduleSleepTimeout();
2393 if (!mGoingToSleep.isHeld()) {
2394 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002395 if (mLaunchingActivity.isHeld()) {
2396 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2397 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002398 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002399 mLaunchingActivity.release();
2400 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002401 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002402 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002403 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002404 }
2405
2406 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002407 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002408
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002409 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002410 final long endTime = System.currentTimeMillis() + timeout;
2411 while (true) {
2412 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002413 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2414 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002415 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2416 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2417 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002418 }
2419 if (cantShutdown) {
2420 long timeRemaining = endTime - System.currentTimeMillis();
2421 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002422 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002423 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002424 } catch (InterruptedException e) {
2425 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002426 } else {
2427 Slog.w(TAG, "Activity manager shutdown timed out");
2428 timedout = true;
2429 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002430 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002431 } else {
2432 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002433 }
2434 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002435
2436 // Force checkReadyForSleep to complete.
2437 mSleepTimeout = true;
2438 checkReadyForSleepLocked();
2439
Craig Mautner8d341ef2013-03-26 09:03:27 -07002440 return timedout;
2441 }
2442
2443 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002444 removeSleepTimeouts();
2445 if (mGoingToSleep.isHeld()) {
2446 mGoingToSleep.release();
2447 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002448 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2449 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002450 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2451 final ActivityStack stack = stacks.get(stackNdx);
2452 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002453 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002454 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002455 }
Craig Mautner5314a402013-09-26 12:40:16 -07002456 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002458 mGoingToSleepActivities.clear();
2459 }
2460
2461 void activitySleptLocked(ActivityRecord r) {
2462 mGoingToSleepActivities.remove(r);
2463 checkReadyForSleepLocked();
2464 }
2465
2466 void checkReadyForSleepLocked() {
2467 if (!mService.isSleepingOrShuttingDown()) {
2468 // Do not care.
2469 return;
2470 }
2471
2472 if (!mSleepTimeout) {
2473 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002474 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2475 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002476 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2477 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2478 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002479 }
2480
2481 if (mStoppingActivities.size() > 0) {
2482 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002483 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002484 + mStoppingActivities.size() + " activities");
2485 scheduleIdleLocked();
2486 dontSleep = true;
2487 }
2488
2489 if (mGoingToSleepActivities.size() > 0) {
2490 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002491 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002492 + mGoingToSleepActivities.size() + " activities");
2493 dontSleep = true;
2494 }
2495
2496 if (dontSleep) {
2497 return;
2498 }
2499 }
2500
Craig Mautnere0a38842013-12-16 16:14:02 -08002501 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2502 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002503 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2504 stacks.get(stackNdx).goToSleep();
2505 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002506 }
2507
2508 removeSleepTimeouts();
2509
2510 if (mGoingToSleep.isHeld()) {
2511 mGoingToSleep.release();
2512 }
2513 if (mService.mShuttingDown) {
2514 mService.notifyAll();
2515 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002516 }
2517
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002518 boolean reportResumedActivityLocked(ActivityRecord r) {
2519 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002520 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002521 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002522 }
2523 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002524 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002525 mWindowManager.executeAppTransition();
2526 return true;
2527 }
2528 return false;
2529 }
2530
Craig Mautner8d341ef2013-03-26 09:03:27 -07002531 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002532 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2533 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002534 int stackNdx = stacks.size() - 1;
2535 while (stackNdx >= 0) {
2536 stacks.get(stackNdx).handleAppCrashLocked(app);
2537 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002538 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002539 }
2540 }
2541
Jose Lima4b6c6692014-08-12 17:41:12 -07002542 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002543 final ActivityStack stack = r.task.stack;
2544 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002545 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2546 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002547 return false;
2548 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002549 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002550 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2551 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002552
2553 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002554 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002555 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002556 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002557 return true;
2558 }
2559
2560 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002561 if (visible && top.fullscreen) {
2562 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002563 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2564 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2565 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2566 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002567 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002568 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2569 // Only the activity set as currently visible behind should actively reset its
2570 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002571 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2572 "requestVisibleBehind: returning visible=" + visible
2573 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2574 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002575 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002576 }
2577
Jose Lima4b6c6692014-08-12 17:41:12 -07002578 stack.setVisibleBehindActivity(visible ? r : null);
2579 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002580 // Make the activity immediately above r opaque.
2581 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2582 if (next != null) {
2583 mService.convertFromTranslucent(next.appToken);
2584 }
2585 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002586 if (top.app != null && top.app.thread != null) {
2587 // Notify the top app of the change.
2588 try {
2589 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2590 } catch (RemoteException e) {
2591 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002592 }
2593 return true;
2594 }
2595
Craig Mautnerbb742462014-07-07 15:28:55 -07002596 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2597 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2598 r.mLaunchTaskBehind = false;
2599 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002600 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002601 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002602 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002603 mWindowManager.setAppVisibility(r.appToken, false);
2604 }
2605
2606 void scheduleLaunchTaskBehindComplete(IBinder token) {
2607 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2608 }
2609
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002610 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2611 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002612 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 final int topStackNdx = stacks.size() - 1;
2616 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2617 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002618 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002619 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002620 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002621 }
2622
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002623 void invalidateTaskLayers() {
2624 mTaskLayersChanged = true;
2625 }
2626
2627 void rankTaskLayersIfNeeded() {
2628 if (!mTaskLayersChanged) {
2629 return;
2630 }
2631 mTaskLayersChanged = false;
2632 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2633 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2634 int baseLayer = 0;
2635 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2636 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2637 }
2638 }
2639 }
2640
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002641 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2643 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2644 final int topStackNdx = stacks.size() - 1;
2645 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2646 final ActivityStack stack = stacks.get(stackNdx);
2647 stack.clearOtherAppTimeTrackers(except);
2648 }
2649 }
2650 }
2651
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002655 final int numStacks = stacks.size();
2656 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2657 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002658 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002659 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002660 }
2661 }
2662
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002663 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2664 // Examine all activities currently running in the process.
2665 TaskRecord firstTask = null;
2666 // Tasks is non-null only if two or more tasks are found.
2667 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002668 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2669 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002670 ActivityRecord r = app.activities.get(i);
2671 // First, if we find an activity that is in the process of being destroyed,
2672 // then we just aren't going to do anything for now; we want things to settle
2673 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002674 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002675 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002676 return;
2677 }
2678 // Don't consider any activies that are currently not in a state where they
2679 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002680 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2681 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002682 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002683 continue;
2684 }
2685 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002686 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002687 + " from " + r);
2688 if (firstTask == null) {
2689 firstTask = r.task;
2690 } else if (firstTask != r.task) {
2691 if (tasks == null) {
2692 tasks = new ArraySet<>();
2693 tasks.add(firstTask);
2694 }
2695 tasks.add(r.task);
2696 }
2697 }
2698 }
2699 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002700 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002701 return;
2702 }
2703 // If we have activities in multiple tasks that are in a position to be destroyed,
2704 // let's iterate through the tasks and release the oldest one.
2705 final int numDisplays = mActivityDisplays.size();
2706 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2707 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2708 // Step through all stacks starting from behind, to hit the oldest things first.
2709 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2710 final ActivityStack stack = stacks.get(stackNdx);
2711 // Try to release activities in this stack; if we manage to, we are done.
2712 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2713 return;
2714 }
2715 }
2716 }
2717 }
2718
Amith Yamasani37a40c22015-06-17 13:25:42 -07002719 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002720 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002721 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002722 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002723
Craig Mautner858d8a62013-04-23 17:08:34 -07002724 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002728 final ActivityStack stack = stacks.get(stackNdx);
2729 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002730 TaskRecord task = stack.topTask();
2731 if (task != null) {
2732 mWindowManager.moveTaskToTop(task.taskId);
2733 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002734 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002735 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002736
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002737 ActivityStack stack = getStack(restoreStackId);
2738 if (stack == null) {
2739 stack = mHomeStack;
2740 }
2741 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002743 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 } else {
2745 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002746 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002747 }
Craig Mautner93529a42013-10-04 15:03:13 -07002748 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002749 }
2750
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002751 /**
2752 * Add background users to send boot completed events to.
2753 * @param userId The user being started in the background
2754 * @param uss The state object for the user.
2755 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002756 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002757 mStartingBackgroundUsers.add(uss);
2758 }
2759
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002760 /** Checks whether the userid is a profile of the current user. */
2761 boolean isCurrentProfileLocked(int userId) {
2762 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002763 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002764 }
2765
Chong Zhang45c25ce2015-08-10 22:18:26 -07002766 /** Checks whether the activity should be shown for current user. */
2767 boolean okToShowLocked(ActivityRecord r) {
2768 return r != null && (isCurrentProfileLocked(r.userId)
2769 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2770 }
2771
Craig Mautnerde4ef022013-04-07 19:01:33 -07002772 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002773 ArrayList<ActivityRecord> stops = null;
2774
2775 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002776 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2777 ActivityRecord s = mStoppingActivities.get(activityNdx);
2778 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002779 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002780 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2781 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002782 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002783 if (s.finishing) {
2784 // If this activity is finishing, it is sitting on top of
2785 // everyone else but we now know it is no longer needed...
2786 // so get rid of it. Otherwise, we need to go through the
2787 // normal flow and hide it once we determine that it is
2788 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002789 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002790 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002791 }
2792 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002793 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002794 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002796 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002797 }
2798 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002799 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002800 }
2801 }
2802
2803 return stops;
2804 }
2805
Craig Mautnercf910b02013-04-23 11:23:27 -07002806 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2810 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002811 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002812 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002813 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002814 if (r == null) Slog.e(TAG,
2815 "validateTop...: null top activity, stack=" + stack);
2816 else {
2817 final ActivityRecord pausing = stack.mPausingActivity;
2818 if (pausing != null && pausing == r) Slog.e(TAG,
2819 "validateTop...: top stack has pausing activity r=" + r
2820 + " state=" + state);
2821 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2822 "validateTop...: activity in front not resumed r=" + r
2823 + " state=" + state);
2824 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002825 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002826 final ActivityRecord resumed = stack.mResumedActivity;
2827 if (resumed != null && resumed == r) Slog.e(TAG,
2828 "validateTop...: back stack has resumed activity r=" + r
2829 + " state=" + state);
2830 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2831 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002832 }
2833 }
2834 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002835 }
2836
Craig Mautnere0570202015-05-13 13:06:11 -07002837 private String lockTaskModeToString() {
2838 switch (mLockTaskModeState) {
2839 case LOCK_TASK_MODE_LOCKED:
2840 return "LOCKED";
2841 case LOCK_TASK_MODE_PINNED:
2842 return "PINNED";
2843 case LOCK_TASK_MODE_NONE:
2844 return "NONE";
2845 default: return "unknown=" + mLockTaskModeState;
2846 }
2847 }
2848
Craig Mautner27084302013-03-25 08:05:25 -07002849 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002850 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002851 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002852 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002853 pw.print(prefix);
2854 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002855 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002856 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002857 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2858 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2859 if (packages.size() > 0) {
2860 pw.println(" mLockTaskPackages (userId:packages)=");
2861 for (int i = 0; i < packages.size(); ++i) {
2862 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2863 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2864 }
2865 }
2866 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002867 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002868
Craig Mautner20e72272013-04-01 13:45:53 -07002869 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002870 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002871 }
2872
Dianne Hackborn390517b2013-05-30 15:03:32 -07002873 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2874 boolean needSep, String prefix) {
2875 if (activity != null) {
2876 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2877 if (needSep) {
2878 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002879 }
2880 pw.print(prefix);
2881 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002882 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002883 }
2884 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002885 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002886 }
2887
Craig Mautner8d341ef2013-03-26 09:03:27 -07002888 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2889 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002890 boolean printed = false;
2891 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002892 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2893 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002894 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002895 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002896 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002897 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002898 final ActivityStack stack = stacks.get(stackNdx);
2899 StringBuilder stackHeader = new StringBuilder(128);
2900 stackHeader.append(" Stack #");
2901 stackHeader.append(stack.mStackId);
2902 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002903 stackHeader.append("\n");
2904 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2905 stackHeader.append("\n");
2906 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2908 needSep, stackHeader.toString());
2909 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2910 !dumpAll, false, dumpPackage, true,
2911 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002912
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 needSep = printed;
2914 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2915 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002916 if (pr) {
2917 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002919 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2921 " mResumedActivity: ");
2922 if (pr) {
2923 printed = true;
2924 needSep = false;
2925 }
2926 if (dumpAll) {
2927 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2928 " mLastPausedActivity: ");
2929 if (pr) {
2930 printed = true;
2931 needSep = true;
2932 }
2933 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2934 needSep, " mLastNoHistoryActivity: ");
2935 }
2936 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 }
2938 }
2939
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002940 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2941 false, dumpPackage, true, " Activities waiting to finish:", null);
2942 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2943 false, dumpPackage, true, " Activities waiting to stop:", null);
2944 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2945 false, dumpPackage, true, " Activities waiting for another to become visible:",
2946 null);
2947 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2948 false, dumpPackage, true, " Activities waiting to sleep:", null);
2949 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2950 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002951
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002952 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002953 }
2954
Dianne Hackborn390517b2013-05-30 15:03:32 -07002955 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002957 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002958 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002959 String innerPrefix = null;
2960 String[] args = null;
2961 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002962 for (int i=list.size()-1; i>=0; i--) {
2963 final ActivityRecord r = list.get(i);
2964 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2965 continue;
2966 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002967 if (innerPrefix == null) {
2968 innerPrefix = prefix + " ";
2969 args = new String[0];
2970 }
2971 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002972 final boolean full = !brief && (complete || !r.isInHistory());
2973 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002974 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 needNL = false;
2976 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002977 if (header1 != null) {
2978 pw.println(header1);
2979 header1 = null;
2980 }
2981 if (header2 != null) {
2982 pw.println(header2);
2983 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002984 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002985 if (lastTask != r.task) {
2986 lastTask = r.task;
2987 pw.print(prefix);
2988 pw.print(full ? "* " : " ");
2989 pw.println(lastTask);
2990 if (full) {
2991 lastTask.dump(pw, prefix + " ");
2992 } else if (complete) {
2993 // Complete + brief == give a summary. Isn't that obvious?!?
2994 if (lastTask.intent != null) {
2995 pw.print(prefix); pw.print(" ");
2996 pw.println(lastTask.intent.toInsecureStringWithClip());
2997 }
2998 }
2999 }
3000 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3001 pw.print(" #"); pw.print(i); pw.print(": ");
3002 pw.println(r);
3003 if (full) {
3004 r.dump(pw, innerPrefix);
3005 } else if (complete) {
3006 // Complete + brief == give a summary. Isn't that obvious?!?
3007 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3008 if (r.app != null) {
3009 pw.print(innerPrefix); pw.println(r.app);
3010 }
3011 }
3012 if (client && r.app != null && r.app.thread != null) {
3013 // flush anything that is already in the PrintWriter since the thread is going
3014 // to write to the file descriptor directly
3015 pw.flush();
3016 try {
3017 TransferPipe tp = new TransferPipe();
3018 try {
3019 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3020 r.appToken, innerPrefix, args);
3021 // Short timeout, since blocking here can
3022 // deadlock with the application.
3023 tp.go(fd, 2000);
3024 } finally {
3025 tp.kill();
3026 }
3027 } catch (IOException e) {
3028 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3029 } catch (RemoteException e) {
3030 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3031 }
3032 needNL = true;
3033 }
3034 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003035 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003036 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003037
Craig Mautnerf3333272013-04-22 10:55:53 -07003038 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003039 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3040 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003041 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3042 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003043 }
3044
3045 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003046 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003047 }
3048
3049 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003050 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3051 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003052 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3053 }
3054
Craig Mautner05d29032013-05-03 13:40:13 -07003055 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003056 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3057 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3058 }
Craig Mautner05d29032013-05-03 13:40:13 -07003059 }
3060
Craig Mautner0eea92c2013-05-16 13:35:39 -07003061 void removeSleepTimeouts() {
3062 mSleepTimeout = false;
3063 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3064 }
3065
3066 final void scheduleSleepTimeout() {
3067 removeSleepTimeouts();
3068 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3069 }
3070
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 @Override
3072 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003073 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003074 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3075 }
3076
3077 @Override
3078 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003079 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003080 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3081 }
3082
3083 @Override
3084 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003085 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3087 }
3088
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003089 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003090 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003091 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003092 newDisplay = mActivityDisplays.get(displayId) == null;
3093 if (newDisplay) {
3094 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003095 if (activityDisplay.mDisplay == null) {
3096 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3097 return;
3098 }
Craig Mautner4504de52013-12-20 09:06:56 -08003099 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003100 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003101 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003102 }
Craig Mautner4504de52013-12-20 09:06:56 -08003103 if (newDisplay) {
3104 mWindowManager.onDisplayAdded(displayId);
3105 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 }
3107
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003108 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3109 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3110 return;
3111 }
3112 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3113 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3114 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3115 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3116 }
3117
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003118 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003120 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3121 if (activityDisplay != null) {
3122 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003123 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003124 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003125 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003126 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003127 }
3128 }
3129 mWindowManager.onDisplayRemoved(displayId);
3130 }
3131
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003132 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003134 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3135 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 // TODO: Update the bounds.
3137 }
3138 }
3139 mWindowManager.onDisplayChanged(displayId);
3140 }
3141
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003142 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003143 StackInfo info = new StackInfo();
3144 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3145 info.displayId = Display.DEFAULT_DISPLAY;
3146 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003147 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003148
3149 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3150 final int numTasks = tasks.size();
3151 int[] taskIds = new int[numTasks];
3152 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003153 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003154 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003155 for (int i = 0; i < numTasks; ++i) {
3156 final TaskRecord task = tasks.get(i);
3157 taskIds[i] = task.taskId;
3158 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3159 : task.realActivity != null ? task.realActivity.flattenToString()
3160 : task.getTopActivity() != null ? task.getTopActivity().packageName
3161 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003162 taskBounds[i] = new Rect();
3163 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003164 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003165 }
3166 info.taskIds = taskIds;
3167 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003168 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003169 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003170 return info;
3171 }
3172
3173 StackInfo getStackInfoLocked(int stackId) {
3174 ActivityStack stack = getStack(stackId);
3175 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003176 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003177 }
3178 return null;
3179 }
3180
3181 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003182 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003183 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3184 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003185 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003186 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 }
3188 }
3189 return list;
3190 }
3191
Craig Mautner15df08a2015-04-01 12:17:18 -07003192 TaskRecord getLockedTaskLocked() {
3193 final int top = mLockTaskModeTasks.size() - 1;
3194 if (top >= 0) {
3195 return mLockTaskModeTasks.get(top);
3196 }
3197 return null;
3198 }
3199
3200 boolean isLockedTask(TaskRecord task) {
3201 return mLockTaskModeTasks.contains(task);
3202 }
3203
3204 boolean isLastLockedTask(TaskRecord task) {
3205 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3206 }
3207
3208 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003209 if (!mLockTaskModeTasks.remove(task)) {
3210 return;
3211 }
3212 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3213 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003214 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003215 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3216 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003217 final Message lockTaskMsg = Message.obtain();
3218 lockTaskMsg.arg1 = task.userId;
3219 lockTaskMsg.what = LOCK_TASK_END_MSG;
3220 mHandler.sendMessage(lockTaskMsg);
3221 }
3222 }
3223
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003224 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003225 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003226 }
3227
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003228 void showLockTaskToast() {
3229 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003230 }
3231
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003232 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3233 if (mLockTaskModeTasks.contains(task)) {
3234 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3235 }
3236 }
3237
Craig Mautner432f64e2015-05-20 14:59:57 -07003238 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3239 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003240 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003241 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003242 final TaskRecord lockedTask = getLockedTaskLocked();
3243 if (lockedTask != null) {
3244 removeLockedTaskLocked(lockedTask);
3245 if (!mLockTaskModeTasks.isEmpty()) {
3246 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003247 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3248 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003249 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003250 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003251 return;
3252 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003253 }
Craig Mautnere0570202015-05-13 13:06:11 -07003254 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3255 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003256 return;
3257 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003258
3259 // Should have already been checked, but do it again.
3260 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003261 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3262 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003263 return;
3264 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003265 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003266 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003267 return;
3268 }
3269
3270 if (mLockTaskModeTasks.isEmpty()) {
3271 // First locktask.
3272 final Message lockTaskMsg = Message.obtain();
3273 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3274 lockTaskMsg.arg1 = task.userId;
3275 lockTaskMsg.what = LOCK_TASK_START_MSG;
3276 lockTaskMsg.arg2 = lockTaskModeState;
3277 mHandler.sendMessage(lockTaskMsg);
3278 }
3279 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003280 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3281 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003282 mLockTaskModeTasks.remove(task);
3283 mLockTaskModeTasks.add(task);
3284
3285 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003286 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003287 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003288
3289 if (andResume) {
3290 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003291 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003292 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003293 }
3294
3295 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003296 return isLockTaskModeViolation(task, false);
3297 }
3298
3299 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3300 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003301 return false;
3302 }
3303 final int lockTaskAuth = task.mLockTaskAuth;
3304 switch (lockTaskAuth) {
3305 case LOCK_TASK_AUTH_DONT_LOCK:
3306 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003307 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003308 case LOCK_TASK_AUTH_LAUNCHABLE:
3309 case LOCK_TASK_AUTH_WHITELISTED:
3310 return false;
3311 case LOCK_TASK_AUTH_PINNABLE:
3312 // Pinnable tasks can't be launched on top of locktask tasks.
3313 return !mLockTaskModeTasks.isEmpty();
3314 default:
3315 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3316 return true;
3317 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003318 }
3319
Craig Mautner15df08a2015-04-01 12:17:18 -07003320 void onLockTaskPackagesUpdatedLocked() {
3321 boolean didSomething = false;
3322 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3323 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003324 final boolean wasWhitelisted =
3325 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3326 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003327 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003328 final boolean isWhitelisted =
3329 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3330 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3331 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003332 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003333 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3334 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003335 removeLockedTaskLocked(lockedTask);
3336 lockedTask.performClearTaskLocked();
3337 didSomething = true;
3338 }
3339 }
3340 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3341 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3342 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3343 final ActivityStack stack = stacks.get(stackNdx);
3344 stack.onLockTaskPackagesUpdatedLocked();
3345 }
3346 }
Craig Mautnere0570202015-05-13 13:06:11 -07003347 final ActivityRecord r = topRunningActivityLocked();
3348 final TaskRecord task = r != null ? r.task : null;
3349 if (mLockTaskModeTasks.isEmpty() && task != null
3350 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3351 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003352 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3353 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3354 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3355 false);
3356 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003357 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003358 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003359 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003360 }
3361 }
3362
Benjamin Franz43261142015-02-11 15:59:44 +00003363 int getLockTaskModeState() {
3364 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003365 }
3366
Jorim Jaggife89d122015-12-22 16:28:44 +01003367 void activityRelaunchedLocked(IBinder token) {
3368 mWindowManager.notifyAppRelaunchingFinished(token);
3369 }
3370
3371 void activityRelaunchingLocked(ActivityRecord r) {
3372 mWindowManager.notifyAppRelaunching(r.appToken);
3373 }
3374
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003375 void logStackState() {
3376 mActivityMetricsLogger.logWindowState();
3377 }
3378
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003379 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003380
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003381 public ActivityStackSupervisorHandler(Looper looper) {
3382 super(looper);
3383 }
3384
Craig Mautnerf3333272013-04-22 10:55:53 -07003385 void activityIdleInternal(ActivityRecord r) {
3386 synchronized (mService) {
3387 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3388 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003389 }
3390
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003391 @Override
3392 public void handleMessage(Message msg) {
3393 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003394 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003395 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3396 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003397 if (mService.mDidDexOpt) {
3398 mService.mDidDexOpt = false;
3399 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3400 nmsg.obj = msg.obj;
3401 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3402 return;
3403 }
3404 // We don't at this point know if the activity is fullscreen,
3405 // so we need to be conservative and assume it isn't.
3406 activityIdleInternal((ActivityRecord)msg.obj);
3407 } break;
3408 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003409 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003410 activityIdleInternal((ActivityRecord)msg.obj);
3411 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003412 case RESUME_TOP_ACTIVITY_MSG: {
3413 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003414 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003415 }
3416 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003417 case SLEEP_TIMEOUT_MSG: {
3418 synchronized (mService) {
3419 if (mService.isSleepingOrShuttingDown()) {
3420 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3421 mSleepTimeout = true;
3422 checkReadyForSleepLocked();
3423 }
3424 }
3425 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003426 case LAUNCH_TIMEOUT_MSG: {
3427 if (mService.mDidDexOpt) {
3428 mService.mDidDexOpt = false;
3429 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3430 return;
3431 }
3432 synchronized (mService) {
3433 if (mLaunchingActivity.isHeld()) {
3434 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3435 if (VALIDATE_WAKE_LOCK_CALLER
3436 && Binder.getCallingUid() != Process.myUid()) {
3437 throw new IllegalStateException("Calling must be system uid");
3438 }
3439 mLaunchingActivity.release();
3440 }
3441 }
3442 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003443 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003444 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003445 } break;
3446 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003447 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 } break;
3449 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003450 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003451 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003452 case CONTAINER_CALLBACK_VISIBILITY: {
3453 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003454 final IActivityContainerCallback callback = container.mCallback;
3455 if (callback != null) {
3456 try {
3457 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3458 } catch (RemoteException e) {
3459 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003460 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003461 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003462 case LOCK_TASK_START_MSG: {
3463 // When lock task starts, we disable the status bars.
3464 try {
Jason Monk62515be2014-05-21 16:06:19 -04003465 if (mLockTaskNotify == null) {
3466 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003467 }
Jason Monk62515be2014-05-21 16:06:19 -04003468 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003469 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003470 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003471 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003472 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003473 flags = StatusBarManager.DISABLE_MASK
3474 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003475 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003476 flags = StatusBarManager.DISABLE_MASK
3477 & (~StatusBarManager.DISABLE_BACK)
3478 & (~StatusBarManager.DISABLE_HOME)
3479 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003480 }
3481 getStatusBarService().disable(flags, mToken,
3482 mService.mContext.getPackageName());
3483 }
3484 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003485 if (getDevicePolicyManager() != null) {
3486 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003487 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003488 }
justinzhang5286d3f2014-05-12 17:06:01 -04003489 } catch (RemoteException ex) {
3490 throw new RuntimeException(ex);
3491 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003492 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003493 case LOCK_TASK_END_MSG: {
3494 // When lock task ends, we enable the status bars.
3495 try {
Jason Monk62515be2014-05-21 16:06:19 -04003496 if (getStatusBarService() != null) {
3497 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3498 mService.mContext.getPackageName());
3499 }
3500 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003501 if (getDevicePolicyManager() != null) {
3502 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3503 msg.arg1);
3504 }
Jason Monk62515be2014-05-21 16:06:19 -04003505 if (mLockTaskNotify == null) {
3506 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3507 }
3508 mLockTaskNotify.show(false);
3509 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003510 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003511 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003512 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003513 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003514 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003515 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003516 new LockPatternUtils(mService.mContext)
3517 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003518 }
3519 } catch (SettingNotFoundException e) {
3520 // No setting, don't lock.
3521 }
justinzhang5286d3f2014-05-12 17:06:01 -04003522 } catch (RemoteException ex) {
3523 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003524 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003525 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003526 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003527 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003528 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3529 if (mLockTaskNotify == null) {
3530 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3531 }
3532 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3533 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003534 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3535 final ActivityContainer container = (ActivityContainer) msg.obj;
3536 final IActivityContainerCallback callback = container.mCallback;
3537 if (callback != null) {
3538 try {
3539 callback.onAllActivitiesComplete(container.asBinder());
3540 } catch (RemoteException e) {
3541 }
3542 }
3543 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003544 case LAUNCH_TASK_BEHIND_COMPLETE: {
3545 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003546 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003547 if (r != null) {
3548 handleLaunchTaskBehindCompleteLocked(r);
3549 }
3550 }
3551 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003552
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003553 }
3554 }
3555 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003556
Ying Wangb081a592014-04-22 15:20:16 -07003557 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003558 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3559 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003561 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003562 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003563 ActivityRecord mParentActivity = null;
3564 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003565
Craig Mautnere3a00d72014-04-16 08:31:19 -07003566 boolean mVisible = true;
3567
Craig Mautner4a1cb222013-12-04 16:14:06 -08003568 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003571 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3572 final static int CONTAINER_STATE_NO_SURFACE = 1;
3573 final static int CONTAINER_STATE_FINISHING = 2;
3574 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3575
3576 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003577 synchronized (mService) {
3578 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003579 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003580 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003581 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003583 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003585 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003586 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003587 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003588 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003589 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003590 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 }
3592
3593 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003594 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003595 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003596 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3597 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 return;
3599 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003600 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003601 }
3602 }
3603
3604 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003605 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003606 synchronized (mService) {
3607 if (mActivityDisplay != null) {
3608 return mActivityDisplay.mDisplayId;
3609 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003610 }
3611 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 }
3613
Jeff Brownca9bc702014-02-11 14:32:56 -08003614 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003615 public int getStackId() {
3616 synchronized (mService) {
3617 return mStackId;
3618 }
3619 }
3620
3621 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003622 public boolean injectEvent(InputEvent event) {
3623 final long origId = Binder.clearCallingIdentity();
3624 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003625 synchronized (mService) {
3626 if (mActivityDisplay != null) {
3627 return mInputManagerInternal.injectInputEvent(event,
3628 mActivityDisplay.mDisplayId,
3629 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3630 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003631 }
3632 return false;
3633 } finally {
3634 Binder.restoreCallingIdentity(origId);
3635 }
3636 }
3637
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003638 @Override
3639 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003640 synchronized (mService) {
3641 if (mContainerState == CONTAINER_STATE_FINISHING) {
3642 return;
3643 }
3644 mContainerState = CONTAINER_STATE_FINISHING;
3645
Craig Mautnerd163e752014-06-13 17:18:47 -07003646 long origId = Binder.clearCallingIdentity();
3647 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003648 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003649 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003650 } finally {
3651 Binder.restoreCallingIdentity(origId);
3652 }
3653 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003654 }
3655
Craig Mautner60257702014-09-17 15:02:33 -07003656 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003657 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003658 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003659 if (mActivityDisplay != null) {
3660 mActivityDisplay.detachActivitiesLocked(mStack);
3661 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003662 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003663 }
3664 }
3665
3666 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003667 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003668 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003669 }
3670
3671 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003672 public final int startActivityIntentSender(IIntentSender intentSender)
3673 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003674 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3675
3676 if (!(intentSender instanceof PendingIntentRecord)) {
3677 throw new IllegalArgumentException("Bad PendingIntent object");
3678 }
3679
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003680 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003681 Binder.getCallingUid(), mCurrentUser, false,
3682 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003683
3684 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3685 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3686 pendingIntent.key.requestResolvedType);
3687
3688 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3689 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3690 }
3691
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003692 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003693 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003694 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003695 throw new SecurityException(
3696 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3697 }
3698 }
3699
Craig Mautnerdf88d732014-01-27 09:21:32 -08003700 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 public IBinder asBinder() {
3702 return this;
3703 }
3704
Craig Mautner4504de52013-12-20 09:06:56 -08003705 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003706 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003707 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003708 }
3709
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 ActivityStackSupervisor getOuter() {
3711 return ActivityStackSupervisor.this;
3712 }
3713
Craig Mautnerd163e752014-06-13 17:18:47 -07003714 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003715 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003716 }
3717
Craig Mautner6985bad2014-04-21 15:22:06 -07003718 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003719 void setVisible(boolean visible) {
3720 if (mVisible != visible) {
3721 mVisible = visible;
3722 if (mCallback != null) {
3723 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3724 0 /* unused */, this).sendToTarget();
3725 }
3726 }
3727 }
3728
Craig Mautner6985bad2014-04-21 15:22:06 -07003729 void setDrawn() {
3730 }
3731
Craig Mautner1b4bf852014-05-26 15:06:32 -07003732 // You can always start a new task on a regular ActivityStack.
3733 boolean isEligibleForNewTasks() {
3734 return true;
3735 }
3736
Craig Mautnerd163e752014-06-13 17:18:47 -07003737 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003738 detachLocked();
3739 deleteActivityContainer(this);
3740 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003741 }
3742
Craig Mautner34b73df2014-01-12 21:11:08 -08003743 @Override
3744 public String toString() {
3745 return mIdString + (mActivityDisplay == null ? "N" : "A");
3746 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003747 }
3748
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003749 private class VirtualActivityContainer extends ActivityContainer {
3750 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003751 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003752
3753 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3754 super(getNextStackId());
3755 mParentActivity = parent;
3756 mCallback = callback;
3757 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003758 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003759 }
3760
3761 @Override
3762 public void setSurface(Surface surface, int width, int height, int density) {
3763 super.setSurface(surface, width, height, density);
3764
3765 synchronized (mService) {
3766 final long origId = Binder.clearCallingIdentity();
3767 try {
3768 setSurfaceLocked(surface, width, height, density);
3769 } finally {
3770 Binder.restoreCallingIdentity(origId);
3771 }
3772 }
3773 }
3774
3775 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3776 if (mContainerState == CONTAINER_STATE_FINISHING) {
3777 return;
3778 }
3779 VirtualActivityDisplay virtualActivityDisplay =
3780 (VirtualActivityDisplay) mActivityDisplay;
3781 if (virtualActivityDisplay == null) {
3782 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003783 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003784 mActivityDisplay = virtualActivityDisplay;
3785 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003786 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003787 }
3788
3789 if (mSurface != null) {
3790 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003791 }
3792
Craig Mautner6985bad2014-04-21 15:22:06 -07003793 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003794 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003795 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003796 } else {
3797 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003798 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003799 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003800 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003801 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003802 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003803
Craig Mautnerd163e752014-06-13 17:18:47 -07003804 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003805
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003806 if (DEBUG_STACK) Slog.d(TAG_STACK,
3807 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003808 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003809
Craig Mautner6985bad2014-04-21 15:22:06 -07003810 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003811 boolean isAttachedLocked() {
3812 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003813 }
3814
3815 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003816 void setDrawn() {
3817 synchronized (mService) {
3818 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003819 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003820 }
3821 }
3822
Craig Mautner1b4bf852014-05-26 15:06:32 -07003823 // Never start a new task on an ActivityView if it isn't explicitly specified.
3824 @Override
3825 boolean isEligibleForNewTasks() {
3826 return false;
3827 }
3828
Craig Mautnerd163e752014-06-13 17:18:47 -07003829 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003830 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003831 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3832 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3833 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3834 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3835 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003836 }
3837 }
3838
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3840 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003841 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003842 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003843 int mDisplayId;
3844 Display mDisplay;
3845 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003846
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3848 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003849 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850
Jose Lima4b6c6692014-08-12 17:41:12 -07003851 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003852
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003853 ActivityDisplay() {
3854 }
Craig Mautner4504de52013-12-20 09:06:56 -08003855
Craig Mautner1a70a162014-09-13 12:09:31 -07003856 // After instantiation, check that mDisplay is not null before using this. The alternative
3857 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003858 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003859 final Display display = mDisplayManager.getDisplay(displayId);
3860 if (display == null) {
3861 return;
3862 }
3863 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003864 }
3865
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003866 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003867 mDisplay = display;
3868 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003870 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003872 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003873 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003874 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3875 + " onTop=" + onTop);
3876 if (onTop) {
3877 mStacks.add(stack);
3878 } else {
3879 mStacks.add(0, stack);
3880 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 }
3882
Craig Mautnere0a38842013-12-16 16:14:02 -08003883 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003884 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003885 + " from displayId=" + mDisplayId);
3886 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003887 }
3888
Jose Lima4b6c6692014-08-12 17:41:12 -07003889 void setVisibleBehindActivity(ActivityRecord r) {
3890 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003891 }
3892
Jose Lima4b6c6692014-08-12 17:41:12 -07003893 boolean hasVisibleBehindActivity() {
3894 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003895 }
3896
Craig Mautner34b73df2014-01-12 21:11:08 -08003897 @Override
3898 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003899 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3900 }
3901 }
3902
3903 class VirtualActivityDisplay extends ActivityDisplay {
3904 VirtualDisplay mVirtualDisplay;
3905
Craig Mautner6985bad2014-04-21 15:22:06 -07003906 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003907 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003908 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3909 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3910 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3911 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003912
3913 init(mVirtualDisplay.getDisplay());
3914
3915 mWindowManager.handleDisplayAdded(mDisplayId);
3916 }
3917
3918 void setSurface(Surface surface) {
3919 if (mVirtualDisplay != null) {
3920 mVirtualDisplay.setSurface(surface);
3921 }
3922 }
3923
3924 @Override
3925 void detachActivitiesLocked(ActivityStack stack) {
3926 super.detachActivitiesLocked(stack);
3927 if (mVirtualDisplay != null) {
3928 mVirtualDisplay.release();
3929 mVirtualDisplay = null;
3930 }
3931 }
3932
3933 @Override
3934 public String toString() {
3935 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003936 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003937 }
Jose Lima58e66d62014-05-27 20:00:27 -07003938
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003939 /**
3940 * Adjust bounds to stay within stack bounds.
3941 *
3942 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3943 * that keep them unchanged, but be contained within the stack bounds.
3944 *
3945 * @param bounds Bounds to be adjusted.
3946 * @param stackBounds Bounds within which the other bounds should remain.
3947 */
3948 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3949 if (stackBounds == null || stackBounds.contains(bounds)) {
3950 return;
3951 }
3952
3953 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3954 final int maxRight = stackBounds.right
3955 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3956 int horizontalDiff = stackBounds.left - bounds.left;
3957 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3958 || (bounds.left + horizontalDiff >= maxRight)) {
3959 horizontalDiff = maxRight - bounds.left;
3960 }
3961 bounds.left += horizontalDiff;
3962 bounds.right += horizontalDiff;
3963 }
3964
3965 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3966 final int maxBottom = stackBounds.bottom
3967 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3968 int verticalDiff = stackBounds.top - bounds.top;
3969 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3970 || (bounds.top + verticalDiff >= maxBottom)) {
3971 verticalDiff = maxBottom - bounds.top;
3972 }
3973 bounds.top += verticalDiff;
3974 bounds.bottom += verticalDiff;
3975 }
3976 }
3977
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003978 ActivityStack findStackBehind(ActivityStack stack) {
3979 // TODO(multi-display): We are only looking for stacks on the default display.
3980 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3981 if (display == null) {
3982 return null;
3983 }
3984 final ArrayList<ActivityStack> stacks = display.mStacks;
3985 for (int i = stacks.size() - 1; i >= 0; i--) {
3986 if (stacks.get(i) == stack && i > 0) {
3987 return stacks.get(i - 1);
3988 }
3989 }
3990 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3991 + " in=" + stacks);
3992 }
Craig Mautner27084302013-03-25 08:05:25 -07003993}