blob: c7fc5e24669a744d9049d6327ab55a04b3ab9f2b [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;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800101import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700102import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700103
Jorim Jaggife89d122015-12-22 16:28:44 +0100104import static android.Manifest.permission.START_ANY_ACTIVITY;
105import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
106import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
107import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
108import static android.app.ActivityManager.RESIZE_MODE_FORCED;
109import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
110import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
111import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
112import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
113import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
114import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
115import static android.app.ActivityManager.StackId.HOME_STACK_ID;
116import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
117import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
119import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
120import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
121import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggi51605272016-02-24 18:06:49 -0500122import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800123import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100124import static android.content.pm.PackageManager.PERMISSION_GRANTED;
125import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
126import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
127import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
139import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
140import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
150import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
151import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800152import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100153import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
154import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
155import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
156import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
157import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
158import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
159import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
160import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
161import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
162import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
163import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
164import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
165import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
166import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
167import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
168import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
169import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
170
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800172 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700173 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700175 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700176 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700178 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700179 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700180 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700181 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800182 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184
Craig Mautnerf3333272013-04-22 10:55:53 -0700185 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700187
Craig Mautner0eea92c2013-05-16 13:35:39 -0700188 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700190
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700191 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700193
Craig Mautner05d29032013-05-03 13:40:13 -0700194 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
195 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
196 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700198 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800199 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
200 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
201 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700202 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400203 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
204 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700205 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700206 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700207 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800208 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
209 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210
Wale Ogunwale040b4702015-08-06 18:10:50 -0700211 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Jason Monk62515be2014-05-21 16:06:19 -0400213 private static final String LOCK_TASK_TAG = "Lock-to-App";
214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 // Used to indicate if an object (e.g. stack) that we are trying to get
216 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800217 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700219 // Used to indicate that windows of activities should be preserved during the resize.
220 static final boolean PRESERVE_WINDOWS = true;
221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 // Used to indicate if an object (e.g. task) should be moved/created
223 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700224 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225
226 // Used to indicate that an objects (e.g. task) removal from its container
227 // (e.g. stack) is due to it moving to another container.
228 static final boolean MOVING = true;
229
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700230 // Force the focus to change to the stack we are moving a task to..
231 static final boolean FORCE_FOCUS = true;
232
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700233 // Restore task from the saved recents if it can't be found in any live stack.
234 static final boolean RESTORE_FROM_RECENTS = true;
235
Svetoslav7008b512015-06-24 18:47:07 -0700236 // Activity actions an app cannot start if it uses a permission which is not granted.
237 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
238 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700239
Svetoslav7008b512015-06-24 18:47:07 -0700240 static {
241 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
242 Manifest.permission.CAMERA);
243 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
244 Manifest.permission.CAMERA);
245 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
246 Manifest.permission.CALL_PHONE);
247 }
248
Svet Ganov99b60432015-06-27 13:15:22 -0700249 /** Action restriction: launching the activity is not restricted. */
250 private static final int ACTIVITY_RESTRICTION_NONE = 0;
251 /** Action restriction: launching the activity is restricted by a permission. */
252 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
253 /** Action restriction: launching the activity is restricted by an app op. */
254 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700255
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700256 // The height/width divide used when fitting a task within a bounds with method
257 // {@link #fitWithinBounds}.
258 // We always want the task to to be visible in the bounds without affecting its size when
259 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
260 // the input bounds right or bottom side minus the width or height divided by this value.
261 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
262
justinzhang5286d3f2014-05-12 17:06:01 -0400263 /** Status Bar Service **/
264 private IBinder mToken = new Binder();
265 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400266 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400267
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700268 // For debugging to make sure the caller when acquiring/releasing our
269 // wake lock is the system process.
270 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800271 /** The number of distinct task ids that can be assigned to the tasks of a single user */
272 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700273
Craig Mautner27084302013-03-25 08:05:25 -0700274 final ActivityManagerService mService;
275
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800276 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800277
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700278 final ActivityStackSupervisorHandler mHandler;
279
280 /** Short cut */
281 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800282 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700284 /** Counter for next free stack ID to use for dynamic activity stacks. */
285 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700286
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800287 /**
288 * Maps the task identifier that activities are currently being started in to the userId of the
289 * task. Each time a new task is created, the entry for the userId of the task is incremented
290 */
291 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Craig Mautner2420ead2013-04-01 17:13:20 -0700293 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800294 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700295
Craig Mautnere0a38842013-12-16 16:14:02 -0800296 /** The stack containing the launcher app. Assumed to always be attached to
297 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800298 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700299
Craig Mautnere0a38842013-12-16 16:14:02 -0800300 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800301 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Craig Mautner4a1cb222013-12-04 16:14:06 -0800303 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
304 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800305 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700307
308 /** List of activities that are waiting for a new activity to become visible before completing
309 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700312 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800313 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700314
315 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800316 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318 /** List of activities that are ready to be stopped, but waiting for the next activity to
319 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800320 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
Craig Mautnerf3333272013-04-22 10:55:53 -0700322 /** List of activities that are ready to be finished, but waiting for the previous activity to
323 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700325
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700328
Wale Ogunwale22e25262016-02-01 10:32:02 -0800329 /** List of activities whose multi-window mode changed that we need to report to the
330 * application */
331 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
332
333 /** List of activities whose picture-in-picture mode changed that we need to report to the
334 * application */
335 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
336
Craig Mautnerf3333272013-04-22 10:55:53 -0700337 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700338 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700339
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700340 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700341 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700342
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
344 * is being brought in front of us. */
345 boolean mUserLeaving = false;
346
Craig Mautner0eea92c2013-05-16 13:35:39 -0700347 /** Set when we have taken too long waiting to go to sleep. */
348 boolean mSleepTimeout = false;
349
350 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700351 * We don't want to allow the device to go to sleep while in the process
352 * of launching an activity. This is primarily to allow alarm intent
353 * receivers to launch an activity and get that to run before the device
354 * goes back to sleep.
355 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700356 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700357
358 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700359 * Set when the system is going to sleep, until we have
360 * successfully paused the current activity and released our wake lock.
361 * At that point the system is allowed to actually sleep.
362 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700363 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700365 /** Stack id of the front stack when user switched, indexed by userId. */
366 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700367
Craig Mautner4504de52013-12-20 09:06:56 -0800368 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700370 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800371
372 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700373 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374
Jeff Brownca9bc702014-02-11 14:32:56 -0800375 InputManagerInternal mInputManagerInternal;
376
Craig Mautner15df08a2015-04-01 12:17:18 -0700377 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
378 * may be finished until there is only one entry left. If this is empty the system is not
379 * in lockTask mode. */
380 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000381 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700382 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
383 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000384 */
385 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400386 /**
387 * Notifies the user when entering/exiting lock-task.
388 */
389 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800390
Wale Ogunwaled046a012015-12-24 13:05:59 -0800391 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800392 boolean inResumeTopActivity;
393
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700394 // 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 -0700395 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700396 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700397
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700398 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
399 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800400 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700401
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700402 // The default minimal size that will be used if the activity doesn't specify its minimal size.
403 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800404 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700405
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700406 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
407 private boolean mTaskLayersChanged = true;
408
Jorim Jaggi275561a2016-02-23 10:11:02 -0500409 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800410
Jorim Jaggidc249c42015-12-15 14:57:31 -0800411 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
412
Wale Ogunwale39381972015-12-17 17:15:29 -0800413 static class FindTaskResult {
414 ActivityRecord r;
415 boolean matchedByRootAffinity;
416 }
417 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
418
Craig Mautneree36c772014-07-16 14:56:05 -0700419 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100420 * Used to keep track whether app visibilities got changed since the last pause. Useful to
421 * determine whether to invoke the task stack change listener after pausing.
422 */
423 boolean mAppVisibilitiesChangedSinceLastPause;
424
425 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700426 * Description of a request to start a new activity, which has been held
427 * due to app switches being disabled.
428 */
429 static class PendingActivityLaunch {
430 final ActivityRecord r;
431 final ActivityRecord sourceRecord;
432 final int startFlags;
433 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700434 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700435
436 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700437 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700438 r = _r;
439 sourceRecord = _sourceRecord;
440 startFlags = _startFlags;
441 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700442 callerApp = _callerApp;
443 }
444
445 void sendErrorResult(String message) {
446 try {
447 if (callerApp.thread != null) {
448 callerApp.thread.scheduleCrash(message);
449 }
450 } catch (RemoteException e) {
451 Slog.e(TAG, "Exception scheduling crash of failed "
452 + "activity launcher sourceRecord=" + sourceRecord, e);
453 }
Craig Mautneree36c772014-07-16 14:56:05 -0700454 }
455 }
456
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800457 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700458 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700459 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800460 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800461 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700462 }
463
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800464 void setRecentTasks(RecentTasks recentTasks) {
465 mRecentTasks = recentTasks;
466 }
467
Jeff Brown2c43c332014-06-12 22:38:59 -0700468 /**
469 * At the time when the constructor runs, the power manager has not yet been
470 * initialized. So we initialize our wakelocks afterwards.
471 */
472 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700474 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700475 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700476 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700477 }
478
justinzhang5286d3f2014-05-12 17:06:01 -0400479 // This function returns a IStatusBarService. The value is from ServiceManager.
480 // getService and is cached.
481 private IStatusBarService getStatusBarService() {
482 synchronized (mService) {
483 if (mStatusBarService == null) {
484 mStatusBarService = IStatusBarService.Stub.asInterface(
485 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
486 if (mStatusBarService == null) {
487 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
488 }
489 }
490 return mStatusBarService;
491 }
492 }
493
Jason Monk35c62a42014-06-17 10:24:47 -0400494 private IDevicePolicyManager getDevicePolicyManager() {
495 synchronized (mService) {
496 if (mDevicePolicyManager == null) {
497 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
498 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
499 if (mDevicePolicyManager == null) {
500 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
501 }
502 }
503 return mDevicePolicyManager;
504 }
505 }
506
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700507 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 synchronized (mService) {
509 mWindowManager = wm;
510
511 mDisplayManager =
512 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
513 mDisplayManager.registerDisplayListener(this, null);
514
515 Display[] displays = mDisplayManager.getDisplays();
516 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
517 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700519 if (activityDisplay.mDisplay == null) {
520 throw new IllegalStateException("Default Display does not exist");
521 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800522 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700523 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 }
525
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800526 mHomeStack = mFocusedStack = mLastFocusedStack =
527 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800528
529 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 }
Craig Mautner27084302013-03-25 08:05:25 -0700531 }
532
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200533 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700534 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200535 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700536 }
537
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700538 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700540 }
541
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800543 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700544 }
545
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700547 if (stack == null) {
548 return false;
549 }
550
Craig Mautnerdf88d732014-01-27 09:21:32 -0800551 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
552 if (parent != null) {
553 stack = parent.task.stack;
554 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800555 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700556 }
557
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700558 /** The top most stack. */
559 boolean isFrontStack(ActivityStack stack) {
560 if (stack == null) {
561 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700563
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700564 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
565 if (parent != null) {
566 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800567 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
569 }
570
Wale Ogunwaled046a012015-12-24 13:05:59 -0800571 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800572 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
573 if (!focusCandidate.isFocusable()) {
574 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
575 focusCandidate = focusCandidate.getNextFocusableStackLocked();
576 }
577
578 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800579 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800580 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800581
Wale Ogunwaled046a012015-12-24 13:05:59 -0800582 EventLogTags.writeAmFocusedStack(
583 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
584 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
585 }
586
587 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800588 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800589 // The focus activity should always be the top activity in the focused stack.
590 // There will be chaos and anarchy if it isn't...
591 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
592 }
Craig Mautnerde313752015-01-22 14:28:03 -0800593
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800594 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800595 if (r != null && r.idle) {
596 checkFinishBootingLocked();
597 }
598 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700599 }
600
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700601 void moveHomeStackToFront(String reason) {
602 mHomeStack.moveToFront(reason);
603 }
604
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700605 /** Returns true if the focus activity was adjusted to the home stack top activity. */
606 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700607 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
608 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700609 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700610 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700611
Craig Mautner84984fa2014-06-19 11:19:20 -0700612 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700613
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700614 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700615 if (top == null) {
616 return false;
617 }
618 mService.setFocusedActivityLocked(top, reason);
619 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700620 }
621
Craig Mautner299f9602015-01-26 09:47:33 -0800622 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700623 if (!mService.mBooting && !mService.mBooted) {
624 // Not ready yet!
625 return false;
626 }
627
Craig Mautner84984fa2014-06-19 11:19:20 -0700628 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
629 mWindowManager.showRecentApps();
630 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700631 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700632
Craig Mautner84984fa2014-06-19 11:19:20 -0700633 if (prev != null) {
634 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
635 }
636
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700637 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
638 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800639 final String myReason = reason + " resumeHomeStackTask";
640
Mark Lua56ea122015-10-08 13:31:01 +0800641 // Only resume home activity if isn't finishing.
642 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800643 mService.setFocusedActivityLocked(r, myReason);
644 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700645 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800646 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700647 }
648
Craig Mautner8d341ef2013-03-26 09:03:27 -0700649 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700650 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700651 }
652
653 /**
654 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
655 * @param id Id of the task we would like returned.
656 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
657 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700658 * @param stackId The stack to restore the task to (default launch stack will be used if
659 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700660 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700661 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800662 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800664 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
666 ActivityStack stack = stacks.get(stackNdx);
667 TaskRecord task = stack.taskForIdLocked(id);
668 if (task != null) {
669 return task;
670 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700671 }
672 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800673
674 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700675 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800676 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800677 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700678 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800679 return null;
680 }
681
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700682 if (!restoreFromRecents) {
683 return task;
684 }
685
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700686 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700687 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
688 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800689 return null;
690 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800692 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700693 }
694
Craig Mautner6170f732013-04-02 13:05:23 -0700695 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800698 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
700 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
701 if (r != null) {
702 return r;
703 }
Craig Mautner6170f732013-04-02 13:05:23 -0700704 }
705 }
706 return null;
707 }
708
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000709 boolean isFocusedUserLockedProfile() {
710 final int userId = mFocusedStack.topRunningActivityLocked().userId;
711 return userId != UserHandle.myUserId()
712 && mService.mUserController.shouldConfirmCredentials(userId);
713 }
714
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800715 void setNextTaskIdForUserLocked(int taskId, int userId) {
716 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
717 if (taskId > currentTaskId) {
718 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700719 }
720 }
721
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800722 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800723 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
724 // for a userId u, a taskId can only be in the range
725 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
726 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
727 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800728 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
729 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
730 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800731 candidateTaskId++;
732 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
733 // Wrap around as there will be smaller task ids that are available now.
734 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700735 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800736 if (candidateTaskId == currentTaskId) {
737 // Something wrong!
738 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
739 throw new IllegalStateException("Cannot get an available task id."
740 + " Reached limit of " + MAX_TASK_IDS_PER_USER
741 + " running tasks per user.");
742 }
743 }
744 mCurTaskIdForUser.put(userId, candidateTaskId);
745 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700746 }
747
Craig Mautnerde4ef022013-04-07 19:01:33 -0700748 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800749 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700750 if (stack == null) {
751 return null;
752 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700753 ActivityRecord resumedActivity = stack.mResumedActivity;
754 if (resumedActivity == null || resumedActivity.app == null) {
755 resumedActivity = stack.mPausingActivity;
756 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700757 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 }
759 }
760 return resumedActivity;
761 }
762
Dianne Hackbornff072722014-09-24 10:56:28 -0700763 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700764 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
767 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
769 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700770 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 continue;
772 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700773 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 if (hr != null) {
775 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
776 && processName.equals(hr.processName)) {
777 try {
George Mount2c92c972014-03-20 09:38:23 -0700778 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 didSomething = true;
780 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700781 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 Slog.w(TAG, "Exception in new application when starting activity "
783 + hr.intent.getComponent().flattenToShortString(), e);
784 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700785 }
Craig Mautner20e72272013-04-01 13:45:53 -0700786 }
Craig Mautner20e72272013-04-01 13:45:53 -0700787 }
788 }
789 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700790 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700791 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700792 }
Craig Mautner20e72272013-04-01 13:45:53 -0700793 return didSomething;
794 }
795
796 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
798 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
800 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700801 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 continue;
803 }
804 final ActivityRecord resumedActivity = stack.mResumedActivity;
805 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700806 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800807 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800808 return false;
809 }
Craig Mautner20e72272013-04-01 13:45:53 -0700810 }
811 }
812 return true;
813 }
814
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
817 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
819 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700820 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800821 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700822 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800823 return false;
824 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700825 }
826 }
827 }
828 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700829 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
831 mLastFocusedStack + " to=" + mFocusedStack);
832 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700833 return true;
834 }
835
836 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800837 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800838 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
839 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
841 final ActivityStack stack = stacks.get(stackNdx);
842 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800843 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700844 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800845 return false;
846 }
847 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700849 }
850 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800851 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700852 }
853
Craig Mautner2acc3892013-09-23 10:28:14 -0700854 /**
855 * Pause all activities in either all of the stacks or just the back stacks.
856 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700857 * @return true if any activity was paused as a result of this call.
858 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700859 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700860 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800861 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
862 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800863 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
864 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700865 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700866 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700868 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
869 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700871 }
872 }
873 return someActivityPaused;
874 }
875
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700877 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
881 final ActivityStack stack = stacks.get(stackNdx);
882 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700883 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700885 Slog.d(TAG_STATES,
886 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 pausing = false;
888 } else {
889 return false;
890 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700891 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700892 }
893 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700894 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 }
896
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700897 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
898 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500899 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
901 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
903 final ActivityStack stack = stacks.get(stackNdx);
904 if (stack.mResumedActivity != null &&
905 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700906 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800907 }
908 }
909 }
910 }
911
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800912 void cancelInitializingActivities() {
913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
914 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
915 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
916 stacks.get(stackNdx).cancelInitializingActivities();
917 }
918 }
919 }
920
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700921 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700922 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700923 }
924
925 void sendWaitingVisibleReportLocked(ActivityRecord r) {
926 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700927 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700928 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700929 if (w.who == null) {
930 changed = true;
931 w.timeout = false;
932 if (r != null) {
933 w.who = new ComponentName(r.info.packageName, r.info.name);
934 }
935 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
936 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700937 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700938 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700939 if (changed) {
940 mService.notifyAll();
941 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700942 }
943
944 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
945 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700946 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700948 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700949 if (w.who == null) {
950 changed = true;
951 w.timeout = timeout;
952 if (r != null) {
953 w.who = new ComponentName(r.info.packageName, r.info.name);
954 }
955 w.thisTime = thisTime;
956 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700957 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700958 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700959 if (changed) {
960 mService.notifyAll();
961 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700962 }
963
Craig Mautner29219d92013-04-16 20:19:12 -0700964 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800965 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700966 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700967 if (r != null) {
968 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700969 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700970
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800972 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
974 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800975 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700976 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700977 if (r != null) {
978 return r;
979 }
980 }
981 }
982 return null;
983 }
984
Dianne Hackborn09233282014-04-30 11:33:59 -0700985 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700986 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800987 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
988 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
993 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700994 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800995 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700996 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700997 }
998 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700999
1000 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1001 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1002 while (maxNum > 0) {
1003 long mostRecentActiveTime = Long.MIN_VALUE;
1004 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001005 final int numTaskLists = runningTaskLists.size();
1006 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1007 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001008 if (!stackTaskList.isEmpty()) {
1009 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1010 if (lastActiveTime > mostRecentActiveTime) {
1011 mostRecentActiveTime = lastActiveTime;
1012 selectedStackList = stackTaskList;
1013 }
1014 }
1015 }
1016 if (selectedStackList != null) {
1017 list.add(selectedStackList.remove(0));
1018 --maxNum;
1019 } else {
1020 break;
1021 }
1022 }
Craig Mautner20e72272013-04-01 13:45:53 -07001023 }
1024
Todd Kennedy7440f172015-12-09 14:31:22 -08001025 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1026 ProfilerInfo profilerInfo) {
1027 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 if (aInfo != null) {
1029 // Store the found target back into the intent, because now that
1030 // we have it we never want to do this again. For example, if the
1031 // user navigates back to this point in the history, we should
1032 // always restart the exact same activity.
1033 intent.setComponent(new ComponentName(
1034 aInfo.applicationInfo.packageName, aInfo.name));
1035
1036 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001037 if (!aInfo.processName.equals("system")) {
1038 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001039 mService.setDebugApp(aInfo.processName, true, false);
1040 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001041
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001042 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1043 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1044 }
1045
Man Caocfa78b22015-06-11 20:14:34 -07001046 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1047 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1048 }
1049
1050 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001051 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001052 }
1053 }
1054 }
1055 return aInfo;
1056 }
1057
Todd Kennedy7440f172015-12-09 14:31:22 -08001058 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001059 return resolveIntent(intent, resolvedType, userId, 0);
1060 }
1061
1062 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001063 try {
1064 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001065 PackageManager.MATCH_DEFAULT_ONLY | flags
1066 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001067 } catch (RemoteException e) {
1068 }
1069 return null;
1070 }
1071
1072 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1073 ProfilerInfo profilerInfo, int userId) {
1074 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1075 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1076 }
1077
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001078 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1079 boolean andResume, boolean checkConfig) throws RemoteException {
1080
1081 if (!allPausedActivitiesComplete()) {
1082 // While there are activities pausing we skipping starting any new activities until
1083 // pauses are complete. NOTE: that we also do this for activities that are starting in
1084 // the paused state because they will first be resumed then paused on the client side.
1085 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1086 "realStartActivityLocked: Skipping start of r=" + r
1087 + " some activities pausing...");
1088 return false;
1089 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001090
Craig Mautner2568c3a2015-03-26 14:22:34 -07001091 if (andResume) {
1092 r.startFreezingScreenLocked(app, 0);
1093 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001094
Craig Mautner2568c3a2015-03-26 14:22:34 -07001095 // schedule launch ticks to collect information about slow apps.
1096 r.startLaunchTickingLocked();
1097 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001098
1099 // Have the window manager re-evaluate the orientation of
1100 // the screen based on the new activity order. Note that
1101 // as a result of this, it can call back into the activity
1102 // manager with a new orientation. We don't care about that,
1103 // because the activity is not currently running so we are
1104 // just restarting it anyway.
1105 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001106 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001107 mService.mConfiguration,
1108 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001109 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001110 }
1111
1112 r.app = app;
1113 app.waitingToKill = null;
1114 r.launchCount++;
1115 r.lastLaunchTime = SystemClock.uptimeMillis();
1116
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001117 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001118
1119 int idx = app.activities.indexOf(r);
1120 if (idx < 0) {
1121 app.activities.add(r);
1122 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001123 mService.updateLruProcessLocked(app, true, null);
1124 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001125
Craig Mautner15df08a2015-04-01 12:17:18 -07001126 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001127 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1128 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001129 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001130 }
1131
1132 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 try {
1134 if (app.thread == null) {
1135 throw new RemoteException();
1136 }
1137 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001138 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001139 if (andResume) {
1140 results = r.results;
1141 newIntents = r.newIntents;
1142 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001143 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1144 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1145 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 if (andResume) {
1147 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1148 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001149 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001150 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001151 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001152 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001153 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001154 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001155 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001156 r.sleeping = false;
1157 r.forceNewConfig = false;
1158 mService.showAskCompatModeDialogLocked(r);
1159 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001160 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001161 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1162 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1163 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001164 final String profileFile = mService.mProfileFile;
1165 if (profileFile != null) {
1166 ParcelFileDescriptor profileFd = mService.mProfileFd;
1167 if (profileFd != null) {
1168 try {
1169 profileFd = profileFd.dup();
1170 } catch (IOException e) {
1171 if (profileFd != null) {
1172 try {
1173 profileFd.close();
1174 } catch (IOException o) {
1175 }
1176 profileFd = null;
1177 }
1178 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001179 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001180
1181 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1182 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 }
1184 }
1185 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001186
Craig Mautner2568c3a2015-03-26 14:22:34 -07001187 if (andResume) {
1188 app.hasShownUi = true;
1189 app.pendingUiClean = true;
1190 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001191 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001192 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001193 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001194 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001195 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001196 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001197
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001198 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001199 // This may be a heavy-weight process! Note that the package
1200 // manager will ensure that only activity can run in the main
1201 // process of the .apk, which is the only thing that will be
1202 // considered heavy-weight.
1203 if (app.processName.equals(app.info.packageName)) {
1204 if (mService.mHeavyWeightProcess != null
1205 && mService.mHeavyWeightProcess != app) {
1206 Slog.w(TAG, "Starting new heavy weight process " + app
1207 + " when already running "
1208 + mService.mHeavyWeightProcess);
1209 }
1210 mService.mHeavyWeightProcess = app;
1211 Message msg = mService.mHandler.obtainMessage(
1212 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1213 msg.obj = r;
1214 mService.mHandler.sendMessage(msg);
1215 }
1216 }
1217
1218 } catch (RemoteException e) {
1219 if (r.launchFailed) {
1220 // This is the second time we failed -- finish activity
1221 // and give up.
1222 Slog.e(TAG, "Second failure launching "
1223 + r.intent.getComponent().flattenToShortString()
1224 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001225 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1227 "2nd-crash", false);
1228 return false;
1229 }
1230
1231 // This is the first time we failed -- restart process and
1232 // retry.
1233 app.activities.remove(r);
1234 throw e;
1235 }
1236
1237 r.launchFailed = false;
1238 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001239 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
1241
1242 if (andResume) {
1243 // As part of the process of launching, ActivityThread also performs
1244 // a resume.
1245 stack.minimalResumeActivityLocked(r);
1246 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001247 // This activity is not starting in the resumed state... which should look like we asked
1248 // it to pause+stop (but remain visible), and it has done so and reported back the
1249 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001250 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001251 "Moving to PAUSED: " + r + " (starting in paused state)");
1252 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 }
1254
1255 // Launch the new version setup screen if needed. We do this -after-
1256 // launching the initial activity (that is, home), so that it can have
1257 // a chance to initialize itself while in the background, making the
1258 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001259 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 mService.startSetupActivityLocked();
1261 }
1262
Dianne Hackborn465fa392014-09-14 14:21:18 -07001263 // Update any services we are bound to that might care about whether
1264 // their client may have activities.
1265 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1266
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 return true;
1268 }
1269
Craig Mautnere79d42682013-04-01 19:01:53 -07001270 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001271 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001272 // Is this activity's application already running?
1273 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001274 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001275
1276 r.task.stack.setLaunchTime(r);
1277
1278 if (app != null && app.thread != null) {
1279 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001280 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1281 || !"android".equals(r.info.packageName)) {
1282 // Don't add this if it is a platform component that is marked
1283 // to run in multiple processes, because this is actually
1284 // part of the framework so doesn't make sense to track as a
1285 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001286 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1287 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001288 }
George Mount2c92c972014-03-20 09:38:23 -07001289 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001290 return;
1291 } catch (RemoteException e) {
1292 Slog.w(TAG, "Exception when starting activity "
1293 + r.intent.getComponent().flattenToShortString(), e);
1294 }
1295
1296 // If a dead object exception was thrown -- fall through to
1297 // restart the application.
1298 }
1299
1300 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001301 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001302 }
1303
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001304 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001305 String resultWho, int requestCode, int callingPid, int callingUid,
1306 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1307 ActivityRecord resultRecord, ActivityStack resultStack) {
1308 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1309 callingUid);
1310 if (startAnyPerm == PERMISSION_GRANTED) {
1311 return true;
1312 }
1313 final int componentRestriction = getComponentRestrictionForCallingPackage(
1314 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1315 final int actionRestriction = getActionRestrictionForCallingPackage(
1316 intent.getAction(), callingPackage, callingPid, callingUid);
1317 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1318 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1319 if (resultRecord != null) {
1320 resultStack.sendActivityResultLocked(-1,
1321 resultRecord, resultWho, requestCode,
1322 Activity.RESULT_CANCELED, null);
1323 }
1324 final String msg;
1325 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1326 msg = "Permission Denial: starting " + intent.toString()
1327 + " from " + callerApp + " (pid=" + callingPid
1328 + ", uid=" + callingUid + ")" + " with revoked permission "
1329 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1330 } else if (!aInfo.exported) {
1331 msg = "Permission Denial: starting " + intent.toString()
1332 + " from " + callerApp + " (pid=" + callingPid
1333 + ", uid=" + callingUid + ")"
1334 + " not exported from uid " + aInfo.applicationInfo.uid;
1335 } else {
1336 msg = "Permission Denial: starting " + intent.toString()
1337 + " from " + callerApp + " (pid=" + callingPid
1338 + ", uid=" + callingUid + ")"
1339 + " requires " + aInfo.permission;
1340 }
1341 Slog.w(TAG, msg);
1342 throw new SecurityException(msg);
1343 }
1344
1345 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1346 final String message = "Appop Denial: starting " + intent.toString()
1347 + " from " + callerApp + " (pid=" + callingPid
1348 + ", uid=" + callingUid + ")"
1349 + " requires " + AppOpsManager.permissionToOp(
1350 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1351 Slog.w(TAG, message);
1352 return false;
1353 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1354 final String message = "Appop Denial: starting " + intent.toString()
1355 + " from " + callerApp + " (pid=" + callingPid
1356 + ", uid=" + callingUid + ")"
1357 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1358 Slog.w(TAG, message);
1359 return false;
1360 }
1361 return true;
1362 }
1363
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001364 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001365 final long identity = Binder.clearCallingIdentity();
1366 try {
1367 return UserManager.get(mService.mContext).getUserInfo(userId);
1368 } finally {
1369 Binder.restoreCallingIdentity(identity);
1370 }
1371 }
1372
Svet Ganov99b60432015-06-27 13:15:22 -07001373 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001374 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001375 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1376 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001377 == PackageManager.PERMISSION_DENIED) {
1378 return ACTIVITY_RESTRICTION_PERMISSION;
1379 }
1380
Christopher Tateff7add02015-08-17 10:23:22 -07001381 if (activityInfo.permission == null) {
1382 return ACTIVITY_RESTRICTION_NONE;
1383 }
1384
Svet Ganov99b60432015-06-27 13:15:22 -07001385 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1386 if (opCode == AppOpsManager.OP_NONE) {
1387 return ACTIVITY_RESTRICTION_NONE;
1388 }
1389
1390 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1391 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001392 if (!ignoreTargetSecurity) {
1393 return ACTIVITY_RESTRICTION_APPOP;
1394 }
Svet Ganov99b60432015-06-27 13:15:22 -07001395 }
1396
1397 return ACTIVITY_RESTRICTION_NONE;
1398 }
1399
Svetoslav7008b512015-06-24 18:47:07 -07001400 private int getActionRestrictionForCallingPackage(String action,
1401 String callingPackage, int callingPid, int callingUid) {
1402 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001403 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001404 }
1405
1406 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1407 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001408 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001409 }
1410
1411 final PackageInfo packageInfo;
1412 try {
1413 packageInfo = mService.mContext.getPackageManager()
1414 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1415 } catch (PackageManager.NameNotFoundException e) {
1416 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001417 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001418 }
1419
1420 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001421 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001422 }
1423
1424 if (mService.checkPermission(permission, callingPid, callingUid) ==
1425 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001426 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001427 }
1428
1429 final int opCode = AppOpsManager.permissionToOpCode(permission);
1430 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001431 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001432 }
1433
1434 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1435 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001436 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001437 }
1438
Svet Ganov99b60432015-06-27 13:15:22 -07001439 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001440 }
1441
Wale Ogunwaled046a012015-12-24 13:05:59 -08001442 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001443 if (r == null) {
1444 // Not sure what you are trying to do, but it is not going to work...
1445 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001446 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001447 final TaskRecord task = r.task;
1448 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001449 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001450 return false;
1451 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001452 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001453 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001454 }
1455
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001456 void setLaunchSource(int uid) {
1457 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1458 }
1459
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001460 void acquireLaunchWakelock() {
1461 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1462 throw new IllegalStateException("Calling must be system uid");
1463 }
1464 mLaunchingActivity.acquire();
1465 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1466 // To be safe, don't allow the wake lock to be held for too long.
1467 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1468 }
1469 }
1470
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001471 /**
1472 * Called when the frontmost task is idle.
1473 * @return the state of mService.mBooting before this was called.
1474 */
1475 private boolean checkFinishBootingLocked() {
1476 final boolean booting = mService.mBooting;
1477 boolean enableScreen = false;
1478 mService.mBooting = false;
1479 if (!mService.mBooted) {
1480 mService.mBooted = true;
1481 enableScreen = true;
1482 }
1483 if (booting || enableScreen) {
1484 mService.postFinishBooting(booting, enableScreen);
1485 }
1486 return booting;
1487 }
1488
Craig Mautnerf3333272013-04-22 10:55:53 -07001489 // Checked.
1490 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1491 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001492 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001493
Craig Mautnerf3333272013-04-22 10:55:53 -07001494 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001495 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001496 int NS = 0;
1497 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001498 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001499 boolean activityRemoved = false;
1500
Wale Ogunwale7d701172015-03-11 15:36:30 -07001501 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001502 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001503 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1504 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001505 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1506 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001507 if (fromTimeout) {
1508 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001509 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001510
1511 // This is a hack to semi-deal with a race condition
1512 // in the client where it can be constructed with a
1513 // newer configuration from when we asked it to launch.
1514 // We'll update with whatever configuration it now says
1515 // it used to launch.
1516 if (config != null) {
1517 r.configuration = config;
1518 }
1519
1520 // We are now idle. If someone is waiting for a thumbnail from
1521 // us, we can now deliver.
1522 r.idle = true;
1523
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001524 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001525 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001526 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001527 }
1528 }
1529
1530 if (allResumedActivitiesIdle()) {
1531 if (r != null) {
1532 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001533 }
1534
1535 if (mLaunchingActivity.isHeld()) {
1536 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1537 if (VALIDATE_WAKE_LOCK_CALLER &&
1538 Binder.getCallingUid() != Process.myUid()) {
1539 throw new IllegalStateException("Calling must be system uid");
1540 }
1541 mLaunchingActivity.release();
1542 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001543 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001544 }
1545
1546 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001547 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001548 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001549 if ((NF = mFinishingActivities.size()) > 0) {
1550 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001551 mFinishingActivities.clear();
1552 }
1553
Craig Mautnerf3333272013-04-22 10:55:53 -07001554 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001555 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001556 mStartingUsers.clear();
1557 }
1558
Craig Mautnerf3333272013-04-22 10:55:53 -07001559 // Stop any activities that are scheduled to do so but have been
1560 // waiting for the next one to start.
1561 for (int i = 0; i < NS; i++) {
1562 r = stops.get(i);
1563 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001564 if (stack != null) {
1565 if (r.finishing) {
1566 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1567 } else {
1568 stack.stopActivityLocked(r);
1569 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001570 }
1571 }
1572
1573 // Finish any activities that are scheduled to do so but have been
1574 // waiting for the next one to start.
1575 for (int i = 0; i < NF; i++) {
1576 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001577 final ActivityStack stack = r.task.stack;
1578 if (stack != null) {
1579 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1580 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001581 }
1582
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001583 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001584 // Complete user switch
1585 if (startingUsers != null) {
1586 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001587 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001588 }
1589 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001590 }
1591
1592 mService.trimApplications();
1593 //dump();
1594 //mWindowManager.dump();
1595
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001597 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001598 }
1599
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001600 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001601 }
1602
Craig Mautner8e569572013-10-11 17:36:59 -07001603 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001604 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1608 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1609 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001610 }
Craig Mautner19091252013-10-05 00:03:53 -07001611 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001612 }
1613
1614 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001615 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1616 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001617 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1618 stacks.get(stackNdx).closeSystemDialogsLocked();
1619 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001620 }
1621 }
1622
Craig Mautner93529a42013-10-04 15:03:13 -07001623 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001624 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001625 }
1626
Craig Mautner8d341ef2013-03-26 09:03:27 -07001627 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001628 * Update the last used stack id for non-current user (current user's last
1629 * used stack is the focused stack)
1630 */
1631 void updateUserStackLocked(int userId, ActivityStack stack) {
1632 if (userId != mCurrentUser) {
1633 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1634 }
1635 }
1636
1637 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001638 * @return true if some activity was finished (or would have finished if doit were true).
1639 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001640 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1641 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001642 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001645 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001646 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001647 if (stack.finishDisabledPackageActivitiesLocked(
1648 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001649 didSomething = true;
1650 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001651 }
1652 }
1653 return didSomething;
1654 }
1655
Dianne Hackborna413dc02013-07-12 12:02:55 -07001656 void updatePreviousProcessLocked(ActivityRecord r) {
1657 // Now that this process has stopped, we may want to consider
1658 // it to be the previous app to try to keep around in case
1659 // the user wants to return to it.
1660
1661 // First, found out what is currently the foreground app, so that
1662 // we don't blow away the previous app if this activity is being
1663 // hosted by the process that is actually still the foreground.
1664 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001665 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1666 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001667 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1668 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001669 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001670 if (stack.mResumedActivity != null) {
1671 fgApp = stack.mResumedActivity.app;
1672 } else if (stack.mPausingActivity != null) {
1673 fgApp = stack.mPausingActivity.app;
1674 }
1675 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001676 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001677 }
1678 }
1679
1680 // Now set this one as the previous process, only if that really
1681 // makes sense to.
1682 if (r.app != null && fgApp != null && r.app != fgApp
1683 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001684 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001685 mService.mPreviousProcess = r.app;
1686 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1687 }
1688 }
1689
Wale Ogunwaled046a012015-12-24 13:05:59 -08001690 boolean resumeFocusedStackTopActivityLocked() {
1691 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001692 }
1693
Wale Ogunwaled046a012015-12-24 13:05:59 -08001694 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001695 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001696 if (targetStack != null && isFocusedStack(targetStack)) {
1697 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001698 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001699 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1700 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001701 }
1702
Adrian Roos20d7df32016-01-12 18:59:43 +01001703 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1704 TaskRecord finishedTask = null;
1705 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001706 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1707 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001708 final int numStacks = stacks.size();
1709 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1710 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001711 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1712 if (stack == focusedStack || finishedTask == null) {
1713 finishedTask = t;
1714 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001715 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001716 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001717 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001718 }
1719
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001720 void finishVoiceTask(IVoiceInteractionSession session) {
1721 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1722 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1723 final int numStacks = stacks.size();
1724 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1725 final ActivityStack stack = stacks.get(stackNdx);
1726 stack.finishVoiceTask(session);
1727 }
1728 }
1729 }
1730
Chong Zhang280d3322015-11-03 17:27:26 -08001731 void findTaskToMoveToFrontLocked(
1732 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001733 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1734 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001735 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001736 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1737 // Caller wants the home activity moved with it. To accomplish this,
1738 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001739 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001740 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001741 if (task.stack == null) {
1742 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1743 + task + " to front. Stack is null");
1744 return;
1745 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001746
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001747 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001748 int stackId = options.getLaunchStackId();
1749 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001750 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001751 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001752 if (stackId == INVALID_STACK_ID) {
1753 stackId = task.getLaunchStackId();
1754 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001755 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001756 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1757 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1758 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001759 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1760 // still need moveTaskToFrontLocked() below for any transition settings.
1761 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001762 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1763 resizeStackLocked(stackId, bounds,
1764 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
1765 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */);
1766 } else {
1767 // WM resizeTask must be done after the task is moved to the correct stack,
1768 // because Task's setBounds() also updates dim layer's bounds, but that has
1769 // dependency on the stack.
1770 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1771 false /* relayout */, false /* forced */);
1772 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001773 }
1774 }
1775
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001776 final ActivityRecord r = task.getTopActivity();
1777 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1778 r == null ? null : r.appTimeTracker, reason);
1779
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001780 if (DEBUG_STACK) Slog.d(TAG_STACK,
1781 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001782 }
1783
Wale Ogunwale854809c2015-12-27 16:18:19 -08001784 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001785 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001786 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001787 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001788 return false;
1789 }
1790 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1791 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001792 }
1793
Craig Mautner967212c2013-04-13 21:10:58 -07001794 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001795 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001796 }
1797
1798 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001799 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1800 if (activityContainer != null) {
1801 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001802 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001803 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001804 return null;
1805 }
1806 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001807 }
1808
Craig Mautner967212c2013-04-13 21:10:58 -07001809 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001810 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001811 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1812 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001813 }
1814 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001815 }
1816
Craig Mautner4a1cb222013-12-04 16:14:06 -08001817 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001818 ActivityRecord homeActivity = getHomeActivity();
1819 if (homeActivity != null) {
1820 return homeActivity.appToken;
1821 }
1822 return null;
1823 }
1824
1825 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001826 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001827 }
1828
1829 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001830 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1831 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1832 final TaskRecord task = tasks.get(taskNdx);
1833 if (task.isHomeTask()) {
1834 final ArrayList<ActivityRecord> activities = task.mActivities;
1835 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1836 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001837 if (r.isHomeActivity()
1838 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001839 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001840 }
1841 }
1842 }
1843 }
1844 return null;
1845 }
1846
Chong Zhangb15758a2015-11-17 12:12:03 -08001847 /**
1848 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1849 * the docked stack itself, or if it's side-by-side to the docked stack.
1850 */
1851 boolean isStackDockedInEffect(int stackId) {
1852 return stackId == DOCKED_STACK_ID ||
1853 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1854 }
1855
Todd Kennedyca4d8422015-01-15 15:19:22 -08001856 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001857 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001858 ActivityContainer activityContainer =
1859 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001860 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001861 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1862 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001863 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001864 return activityContainer;
1865 }
1866
Craig Mautner34b73df2014-01-12 21:11:08 -08001867 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001868 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1869 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1870 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001871 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1872 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001873 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001874 }
1875 }
1876
Craig Mautner95da1082014-02-24 17:54:35 -08001877 void deleteActivityContainer(IActivityContainer container) {
1878 ActivityContainer activityContainer = (ActivityContainer)container;
1879 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001880 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1881 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001882 final int stackId = activityContainer.mStackId;
1883 mActivityContainers.remove(stackId);
1884 mWindowManager.removeStack(stackId);
1885 }
1886 }
1887
Jorim Jaggidc249c42015-12-15 14:57:31 -08001888 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
1889 boolean preserveWindows, boolean allowResizeInDockedMode) {
1890 if (stackId == DOCKED_STACK_ID) {
1891 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1892 preserveWindows);
1893 return;
1894 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001895 final ActivityStack stack = getStack(stackId);
1896 if (stack == null) {
1897 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1898 return;
1899 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001900
Jorim Jaggidc249c42015-12-15 14:57:31 -08001901 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001902 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1903 // stack size changing so things don't get out of sync.
1904 return;
1905 }
1906
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001907 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001908 mWindowManager.deferSurfaceLayout();
1909 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001910 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
1911 ensureConfigurationAndResume(stack, stack.topRunningActivityLocked(), preserveWindows);
Jorim Jaggic4025202015-10-22 16:43:34 +02001912 } finally {
1913 mWindowManager.continueSurfaceLayout();
1914 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001915 }
1916 }
1917
Jorim Jaggidc249c42015-12-15 14:57:31 -08001918 private void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
1919 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001920 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001921
1922 mTmpBounds.clear();
1923 mTmpConfigs.clear();
1924 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001925 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggidc249c42015-12-15 14:57:31 -08001926 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001927 final TaskRecord task = tasks.get(i);
1928 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001929 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1930 // For freeform stack we don't adjust the size of the tasks to match that
1931 // of the stack, but we do try to make sure the tasks are still contained
1932 // with the bounds of the stack.
1933 tempRect2.set(task.mBounds);
1934 fitWithinBounds(tempRect2, bounds);
1935 task.updateOverrideConfiguration(tempRect2);
1936 } else {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001937 task.updateOverrideConfiguration(
Jorim Jaggi0a932142016-02-01 17:42:25 -08001938 tempTaskBounds != null ? tempTaskBounds : bounds,
1939 tempTaskInsetBounds != null ? tempTaskInsetBounds : bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001940 }
1941 }
1942
1943 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1944 mTmpBounds.put(task.taskId, task.mBounds);
1945 if (tempTaskInsetBounds != null) {
1946 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
1947 }
1948 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001949
1950 // We might trigger a configuration change. Save the current task bounds for freezing.
1951 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001952 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
1953 mTmpBounds, mTmpInsetBounds);
1954 stack.setBounds(bounds);
1955 }
1956
1957 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
1958 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08001959 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001960 return;
1961 }
1962 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
1963 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08001964 if (!updated) {
1965 resumeFocusedStackTopActivityLocked();
1966 }
1967 }
1968
1969 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
1970 Rect tempDockedTaskInsetBounds,
1971 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
1972 final ActivityStack stack = getStack(DOCKED_STACK_ID);
1973 if (stack == null) {
1974 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
1975 return;
1976 }
1977
1978 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
1979 mWindowManager.deferSurfaceLayout();
1980 try {
1981 ActivityRecord r = stack.topRunningActivityLocked();
1982 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
1983 tempDockedTaskInsetBounds);
1984
1985 if (stack.mFullscreen) {
1986 // The dock stack went fullscreen which is kinda like dismissing it.
1987 // In this case we make all other static stacks fullscreen and move all
1988 // docked stack tasks to the fullscreen stack.
1989 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
1990 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
1991 resizeStackLocked(i, null, null, null, preserveWindows,
1992 true /* allowResizeInDockedMode */);
1993 }
1994 }
1995
1996 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1997 final int count = tasks.size();
1998 for (int i = 0; i < count; i++) {
1999 moveTaskToStackLocked(tasks.get(i).taskId,
2000 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
2001 false /* animate */);
2002 }
2003
2004 // stack shouldn't contain anymore activities, so nothing to resume.
2005 r = null;
2006 } else {
2007 // Docked stacks occupy a dedicated region on screen so the size of all other
2008 // static stacks need to be adjusted so they don't overlap with the docked stack.
2009 // We get the bounds to use from window manager which has been adjusted for any
2010 // screen controls and is also the same for all stacks.
Wale Ogunwaleccb6ce22016-01-14 15:36:35 -08002011 mWindowManager.getStackDockedModeBounds(
Wale Ogunwale961f4852016-02-01 20:25:54 -08002012 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002013 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2014 if (StackId.isResizeableByDockedStack(i)) {
2015 ActivityStack otherStack = getStack(i);
2016 if (otherStack != null) {
2017 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2018 tempOtherTaskInsetBounds, preserveWindows,
2019 true /* allowResizeInDockedMode */);
2020 }
2021 }
2022 }
2023 }
2024 ensureConfigurationAndResume(stack, r, preserveWindows);
2025 } finally {
2026 mWindowManager.continueSurfaceLayout();
2027 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2028 }
2029
2030 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2031 tempDockedTaskBounds != null
2032 || tempDockedTaskInsetBounds != null
2033 || tempOtherTaskBounds != null
2034 || tempOtherTaskInsetBounds != null);
2035 }
2036
Chong Zhangf596cd52016-01-05 13:42:44 -08002037 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002038 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002039 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002040 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002041 }
2042
Chong Zhang87b21722015-09-21 15:39:51 -07002043 // If this is a forced resize, let it go through even if the bounds is not changing,
2044 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002045 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002046 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002047 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002048 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002049 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002050 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002051
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002052 if (!mWindowManager.isValidTaskId(task.taskId)) {
2053 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002054 // All we can do for now is update the bounds so it can be used when the task is
2055 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002056 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002057 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2058 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002059 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002060 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002061 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002062 }
2063
Chong Zhang6de2ae82015-09-30 18:25:21 -07002064 // Do not move the task to another stack here.
2065 // This method assumes that the task is already placed in the right stack.
2066 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002067
Chong Zhang6de2ae82015-09-30 18:25:21 -07002068 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002069
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002070 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002071 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002072 // way and the activity was kept the way it was. If it's false, it means the activity had
2073 // to be relaunched due to configuration change.
2074 boolean kept = true;
2075 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002076 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002077 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002078 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002079 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002080 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002081 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002082 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002083 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002084 }
2085 }
2086 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002087 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002088
2089 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002090 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002091 }
2092
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002093 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002094 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2095 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002096 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002097 }
2098
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002099 ActivityContainer activityContainer = new ActivityContainer(stackId);
2100 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002101 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002102 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002103 }
2104
2105 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002106 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002107 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2108 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002109 break;
2110 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002111 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002112 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002113 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002114 }
2115
Chong Zhang5dcb2752015-08-18 13:50:26 -07002116 /**
2117 * Restores a recent task to a stack
2118 * @param task The recent task to be restored.
2119 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002120 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002121 * @return true if the task has been restored successfully.
2122 */
2123 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2124 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002125 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002126 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2127 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2128 // Put it in the fullscreen stack.
2129 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002130 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002131
Wale Ogunwale706ed792015-08-02 10:29:44 -07002132 if (task.stack != null) {
2133 // Task has already been restored once. See if we need to do anything more
2134 if (task.stack.mStackId == stackId) {
2135 // Nothing else to do since it is already restored in the right stack.
2136 return true;
2137 }
2138 // Remove current stack association, so we can re-associate the task with the
2139 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002140 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002141 }
2142
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002143 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002144 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002145
2146 if (stack == null) {
2147 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002148 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2149 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002150 return false;
2151 }
2152
Wale Ogunwale5f986092015-12-04 15:35:38 -08002153 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002154 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2155 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002156 final ArrayList<ActivityRecord> activities = task.mActivities;
2157 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002158 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002159 }
2160 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002161 }
2162
Wale Ogunwale040b4702015-08-06 18:10:50 -07002163 /**
2164 * Moves the specified task record to the input stack id.
2165 * WARNING: This method performs an unchecked/raw move of the task and
2166 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002167 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002168 * @param task Task to move.
2169 * @param stackId Id of stack to move task to.
2170 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002171 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002172 * @param reason Reason the task is been moved.
2173 * @return The stack the task was moved to.
2174 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002175 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002176 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2177 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002178 final ActivityStack prevStack = task.stack;
2179 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2180 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2181 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2182 // Whenever we are moving the top activity from the front stack we want to make sure to move
2183 // the stack to the front.
2184 final boolean wasFront = isFrontStack(prevStack)
2185 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002186
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002187 final int resizeMode = task.mResizeMode;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002188
Chong Zhangd545dce2016-02-29 18:09:17 -08002189 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002190 // We don't allow moving a unresizeable task to the docked stack since the docked
2191 // stack is used for split-screen mode and will cause things like the docked divider to
2192 // show up. We instead leave the task in its current stack or move it to the fullscreen
2193 // stack if it isn't currently in a stack.
2194 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002195 Slog.w(TAG, "Can not move unresizeable task=" + task
2196 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2197 }
2198
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002199 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2200 // if a docked stack is created below which will lead to the stack we are moving from and
2201 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002202 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002203 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002204 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002205 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002206 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002207
2208 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002209 // move focus to the new stack by moving the stack to the front.
2210 stack.moveToFrontAndResumeStateIfNeeded(
2211 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002212
Wale Ogunwale040b4702015-08-06 18:10:50 -07002213 return stack;
2214 }
2215
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002216 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002217 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002218 final TaskRecord task = anyTaskForIdLocked(taskId);
2219 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002220 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002221 return;
2222 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002223
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002224 if (task.stack != null && task.stack.mStackId == stackId) {
2225 // You are already in the right stack silly...
2226 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2227 return;
2228 }
2229
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002230 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2231 throw new IllegalArgumentException("moveTaskToStack:"
2232 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2233 }
2234
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002235 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002236 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002237 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002238 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002239 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002240 // We are about to relaunch the activity because its configuration changed due to
2241 // being maximized, i.e. size change. The activity will first remove the old window
2242 // and then add a new one. This call will tell window manager about this, so it can
2243 // preserve the old window until the new one is drawn. This prevents having a gap
2244 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002245 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 // Note here we always set the replacing window first, as the flags might be needed
2247 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002248 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002249 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002250
2251 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002252 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002253 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002254 try {
2255 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002256 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002257 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002258
Wale Ogunwale961f4852016-02-01 20:25:54 -08002259 if (!animate) {
2260 stack.mNoAnimActivities.add(topActivity);
2261 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002262
Wale Ogunwale961f4852016-02-01 20:25:54 -08002263 // We might trigger a configuration change. Save the current task bounds for freezing.
2264 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2265
2266 // Make sure the task has the appropriate bounds/size for the stack it is in.
2267 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
2268 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002269 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2270 Rect bounds = task.getLaunchBounds();
2271 if (bounds == null) {
2272 stack.layoutTaskInStack(task, null);
2273 bounds = task.mBounds;
2274 }
2275 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002276 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
2277 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow);
2278 }
2279 } finally {
2280 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002281 }
2282
2283 if (mightReplaceWindow) {
2284 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2285 // window), we need to clear the replace window settings. Otherwise, we schedule a
2286 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002287 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002288 }
2289
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002290 // The task might have already been running and its visibility needs to be synchronized with
2291 // the visibility of the stack / windows.
Jorim Jaggie9098022016-01-27 19:29:40 -08002292 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002293 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002294
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002295 showNonResizeableDockToastIfNeeded(task, preferredLaunchStackId, stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002296 }
2297
Wale Ogunwale079a0042015-10-24 11:44:07 -07002298 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2299 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2300 if (stack == null) {
2301 throw new IllegalArgumentException(
2302 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2303 }
2304
2305 final ActivityRecord r = stack.topRunningActivityLocked();
2306 if (r == null) {
2307 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2308 + " in stack=" + stack);
2309 return false;
2310 }
2311
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002312 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002313 Slog.w(TAG,
2314 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002315 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002316 return false;
2317 }
2318
Wale Ogunwale480dca02016-02-06 13:58:29 -08002319 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002320 return true;
2321 }
2322
Wale Ogunwale480dca02016-02-06 13:58:29 -08002323 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2324 mWindowManager.deferSurfaceLayout();
2325 try {
2326 final TaskRecord task = r.task;
2327
2328 // Need to make sure the pinned stack exist so we can resize it below...
2329 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2330
2331 // Resize the pinned stack to match the current size of the task the activity we are
2332 // going to be moving is currently contained in. We do this to have the right starting
2333 // animation bounds for the pinned stack to the desired bounds the caller wants.
2334 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2335 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2336 true /* allowResizeInDockedMode */);
2337
2338 if (task.mActivities.size() == 1) {
2339 // There is only one activity in the task. So, we can just move the task over to
2340 // the stack without re-parenting the activity in a different task.
2341 moveTaskToStackLocked(
2342 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2343 } else {
2344 stack.moveActivityToStack(r);
2345 }
2346 } finally {
2347 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002348 }
2349
Wale Ogunwale480dca02016-02-06 13:58:29 -08002350 // The task might have already been running and its visibility needs to be synchronized
2351 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002352 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002353 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002354
Wale Ogunwale480dca02016-02-06 13:58:29 -08002355 mWindowManager.animateResizePinnedStack(bounds);
2356 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002357 }
2358
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002359 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2360 final TaskRecord task = anyTaskForIdLocked(taskId);
2361 if (task == null) {
2362 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2363 return;
2364 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002365 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2366
2367 task.updateOverrideConfigurationForStack(stack);
2368
2369 mWindowManager.positionTaskInStack(
2370 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002371 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002372 // The task might have already been running and its visibility needs to be synchronized with
2373 // the visibility of the stack / windows.
Jorim Jaggi8fa45222016-02-19 19:54:39 -08002374 stack.ensureActivityConfigurationLocked(task.topRunningActivityLocked(), 0,
2375 !PRESERVE_WINDOWS);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002376 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002377 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002378 }
2379
Craig Mautnerac6f8432013-07-17 13:24:59 -07002380 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002381 mTmpFindTaskResult.r = null;
2382 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002383 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002384 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2385 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002386 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2387 final ActivityStack stack = stacks.get(stackNdx);
2388 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002389 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002390 continue;
2391 }
2392 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002393 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2394 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002395 continue;
2396 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002397 stack.findTaskLocked(r, mTmpFindTaskResult);
2398 // It is possible to have task in multiple stacks with the same root affinity.
2399 // If the match we found was based on root affinity we keep on looking to see if
2400 // there is a better match in another stack. We eventually return the match based
2401 // on root affinity if we don't find a better match.
2402 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2403 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002404 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002405 }
2406 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002407 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2408 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002409 }
2410
2411 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002412 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2413 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002414 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2415 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2416 if (ar != null) {
2417 return ar;
2418 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 }
2420 }
2421 return null;
2422 }
2423
Craig Mautner8d341ef2013-03-26 09:03:27 -07002424 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425 scheduleSleepTimeout();
2426 if (!mGoingToSleep.isHeld()) {
2427 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002428 if (mLaunchingActivity.isHeld()) {
2429 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2430 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002431 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002432 mLaunchingActivity.release();
2433 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002434 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002435 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002436 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002437 }
2438
2439 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002440 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002441
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002442 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002443 final long endTime = System.currentTimeMillis() + timeout;
2444 while (true) {
2445 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002446 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2447 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002448 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2449 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2450 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002451 }
2452 if (cantShutdown) {
2453 long timeRemaining = endTime - System.currentTimeMillis();
2454 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002455 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002456 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002457 } catch (InterruptedException e) {
2458 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002459 } else {
2460 Slog.w(TAG, "Activity manager shutdown timed out");
2461 timedout = true;
2462 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002463 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002464 } else {
2465 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002466 }
2467 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002468
2469 // Force checkReadyForSleep to complete.
2470 mSleepTimeout = true;
2471 checkReadyForSleepLocked();
2472
Craig Mautner8d341ef2013-03-26 09:03:27 -07002473 return timedout;
2474 }
2475
2476 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002477 removeSleepTimeouts();
2478 if (mGoingToSleep.isHeld()) {
2479 mGoingToSleep.release();
2480 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002481 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2482 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002483 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2484 final ActivityStack stack = stacks.get(stackNdx);
2485 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002486 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002487 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002488 }
Craig Mautner5314a402013-09-26 12:40:16 -07002489 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002490 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002491 mGoingToSleepActivities.clear();
2492 }
2493
2494 void activitySleptLocked(ActivityRecord r) {
2495 mGoingToSleepActivities.remove(r);
2496 checkReadyForSleepLocked();
2497 }
2498
2499 void checkReadyForSleepLocked() {
2500 if (!mService.isSleepingOrShuttingDown()) {
2501 // Do not care.
2502 return;
2503 }
2504
2505 if (!mSleepTimeout) {
2506 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002507 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2508 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002509 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2510 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2511 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002512 }
2513
2514 if (mStoppingActivities.size() > 0) {
2515 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002516 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002517 + mStoppingActivities.size() + " activities");
2518 scheduleIdleLocked();
2519 dontSleep = true;
2520 }
2521
2522 if (mGoingToSleepActivities.size() > 0) {
2523 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002524 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002525 + mGoingToSleepActivities.size() + " activities");
2526 dontSleep = true;
2527 }
2528
2529 if (dontSleep) {
2530 return;
2531 }
2532 }
2533
Craig Mautnere0a38842013-12-16 16:14:02 -08002534 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2535 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002536 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2537 stacks.get(stackNdx).goToSleep();
2538 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002539 }
2540
2541 removeSleepTimeouts();
2542
2543 if (mGoingToSleep.isHeld()) {
2544 mGoingToSleep.release();
2545 }
2546 if (mService.mShuttingDown) {
2547 mService.notifyAll();
2548 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002549 }
2550
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002551 boolean reportResumedActivityLocked(ActivityRecord r) {
2552 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002553 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002554 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002555 }
2556 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002557 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002558 mWindowManager.executeAppTransition();
2559 return true;
2560 }
2561 return false;
2562 }
2563
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002565 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2566 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002567 int stackNdx = stacks.size() - 1;
2568 while (stackNdx >= 0) {
2569 stacks.get(stackNdx).handleAppCrashLocked(app);
2570 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002571 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002572 }
2573 }
2574
Jose Lima4b6c6692014-08-12 17:41:12 -07002575 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002576 final ActivityStack stack = r.task.stack;
2577 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002578 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2579 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002580 return false;
2581 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002582 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002583 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2584 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002585
2586 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002587 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002588 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002589 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002590 return true;
2591 }
2592
2593 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002594 if (visible && top.fullscreen) {
2595 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002596 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2597 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2598 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2599 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002600 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002601 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2602 // Only the activity set as currently visible behind should actively reset its
2603 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002604 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2605 "requestVisibleBehind: returning visible=" + visible
2606 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2607 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002608 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002609 }
2610
Jose Lima4b6c6692014-08-12 17:41:12 -07002611 stack.setVisibleBehindActivity(visible ? r : null);
2612 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002613 // If there is a translucent home activity, we need to force it stop being translucent,
2614 // because we can't depend on the application to necessarily perform that operation.
2615 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002616 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002617 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002618 mService.convertFromTranslucent(next.appToken);
2619 }
2620 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002621 if (top.app != null && top.app.thread != null) {
2622 // Notify the top app of the change.
2623 try {
2624 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2625 } catch (RemoteException e) {
2626 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002627 }
2628 return true;
2629 }
2630
Craig Mautnerbb742462014-07-07 15:28:55 -07002631 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2632 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2633 r.mLaunchTaskBehind = false;
2634 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002635 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002636 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002637 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002638 mWindowManager.setAppVisibility(r.appToken, false);
2639 }
2640
2641 void scheduleLaunchTaskBehindComplete(IBinder token) {
2642 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2643 }
2644
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002645 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2646 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002647 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002648 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2649 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002650 final int topStackNdx = stacks.size() - 1;
2651 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2652 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002653 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002654 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002655 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 }
2657
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002658 void invalidateTaskLayers() {
2659 mTaskLayersChanged = true;
2660 }
2661
2662 void rankTaskLayersIfNeeded() {
2663 if (!mTaskLayersChanged) {
2664 return;
2665 }
2666 mTaskLayersChanged = false;
2667 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2668 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2669 int baseLayer = 0;
2670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2671 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2672 }
2673 }
2674 }
2675
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002676 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2677 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2678 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2679 final int topStackNdx = stacks.size() - 1;
2680 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2681 final ActivityStack stack = stacks.get(stackNdx);
2682 stack.clearOtherAppTimeTrackers(except);
2683 }
2684 }
2685 }
2686
Craig Mautner8d341ef2013-03-26 09:03:27 -07002687 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002690 final int numStacks = stacks.size();
2691 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2692 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002693 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002694 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 }
2696 }
2697
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002698 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2699 // Examine all activities currently running in the process.
2700 TaskRecord firstTask = null;
2701 // Tasks is non-null only if two or more tasks are found.
2702 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002703 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2704 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002705 ActivityRecord r = app.activities.get(i);
2706 // First, if we find an activity that is in the process of being destroyed,
2707 // then we just aren't going to do anything for now; we want things to settle
2708 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002709 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002710 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002711 return;
2712 }
2713 // Don't consider any activies that are currently not in a state where they
2714 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002715 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2716 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002717 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002718 continue;
2719 }
2720 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002721 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002722 + " from " + r);
2723 if (firstTask == null) {
2724 firstTask = r.task;
2725 } else if (firstTask != r.task) {
2726 if (tasks == null) {
2727 tasks = new ArraySet<>();
2728 tasks.add(firstTask);
2729 }
2730 tasks.add(r.task);
2731 }
2732 }
2733 }
2734 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002735 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002736 return;
2737 }
2738 // If we have activities in multiple tasks that are in a position to be destroyed,
2739 // let's iterate through the tasks and release the oldest one.
2740 final int numDisplays = mActivityDisplays.size();
2741 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2742 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2743 // Step through all stacks starting from behind, to hit the oldest things first.
2744 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2745 final ActivityStack stack = stacks.get(stackNdx);
2746 // Try to release activities in this stack; if we manage to, we are done.
2747 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2748 return;
2749 }
2750 }
2751 }
2752 }
2753
Amith Yamasani37a40c22015-06-17 13:25:42 -07002754 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002755 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002756 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002757 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002758
Craig Mautner858d8a62013-04-23 17:08:34 -07002759 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002760 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2761 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002762 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 final ActivityStack stack = stacks.get(stackNdx);
2764 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002765 TaskRecord task = stack.topTask();
2766 if (task != null) {
2767 mWindowManager.moveTaskToTop(task.taskId);
2768 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002769 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002770 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002771
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002772 ActivityStack stack = getStack(restoreStackId);
2773 if (stack == null) {
2774 stack = mHomeStack;
2775 }
2776 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002777 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002778 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002779 } else {
2780 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002781 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 }
Craig Mautner93529a42013-10-04 15:03:13 -07002783 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002784 }
2785
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002786 /** Checks whether the userid is a profile of the current user. */
2787 boolean isCurrentProfileLocked(int userId) {
2788 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002789 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002790 }
2791
Chong Zhang45c25ce2015-08-10 22:18:26 -07002792 /** Checks whether the activity should be shown for current user. */
2793 boolean okToShowLocked(ActivityRecord r) {
2794 return r != null && (isCurrentProfileLocked(r.userId)
2795 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2796 }
2797
Craig Mautnerde4ef022013-04-07 19:01:33 -07002798 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002799 ArrayList<ActivityRecord> stops = null;
2800
2801 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002802 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2803 ActivityRecord s = mStoppingActivities.get(activityNdx);
2804 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002805 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002806 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2807 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002808 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002809 if (s.finishing) {
2810 // If this activity is finishing, it is sitting on top of
2811 // everyone else but we now know it is no longer needed...
2812 // so get rid of it. Otherwise, we need to go through the
2813 // normal flow and hide it once we determine that it is
2814 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002815 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002816 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002817 }
2818 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002819 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002820 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002821 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002822 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002823 }
2824 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002825 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002826 }
2827 }
2828
2829 return stops;
2830 }
2831
Craig Mautnercf910b02013-04-23 11:23:27 -07002832 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2834 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2836 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002837 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002838 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002839 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002840 if (r == null) Slog.e(TAG,
2841 "validateTop...: null top activity, stack=" + stack);
2842 else {
2843 final ActivityRecord pausing = stack.mPausingActivity;
2844 if (pausing != null && pausing == r) Slog.e(TAG,
2845 "validateTop...: top stack has pausing activity r=" + r
2846 + " state=" + state);
2847 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2848 "validateTop...: activity in front not resumed r=" + r
2849 + " state=" + state);
2850 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002851 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002852 final ActivityRecord resumed = stack.mResumedActivity;
2853 if (resumed != null && resumed == r) Slog.e(TAG,
2854 "validateTop...: back stack has resumed activity r=" + r
2855 + " state=" + state);
2856 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2857 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002858 }
2859 }
2860 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002861 }
2862
Craig Mautnere0570202015-05-13 13:06:11 -07002863 private String lockTaskModeToString() {
2864 switch (mLockTaskModeState) {
2865 case LOCK_TASK_MODE_LOCKED:
2866 return "LOCKED";
2867 case LOCK_TASK_MODE_PINNED:
2868 return "PINNED";
2869 case LOCK_TASK_MODE_NONE:
2870 return "NONE";
2871 default: return "unknown=" + mLockTaskModeState;
2872 }
2873 }
2874
Craig Mautner27084302013-03-25 08:05:25 -07002875 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002876 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002878 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002879 pw.print(prefix);
2880 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002881 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002882 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002883 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2884 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2885 if (packages.size() > 0) {
2886 pw.println(" mLockTaskPackages (userId:packages)=");
2887 for (int i = 0; i < packages.size(); ++i) {
2888 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2889 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2890 }
2891 }
2892 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002893 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894
Craig Mautner20e72272013-04-01 13:45:53 -07002895 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002896 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002897 }
2898
Dianne Hackborn390517b2013-05-30 15:03:32 -07002899 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2900 boolean needSep, String prefix) {
2901 if (activity != null) {
2902 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2903 if (needSep) {
2904 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002905 }
2906 pw.print(prefix);
2907 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002908 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002909 }
2910 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002911 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002912 }
2913
Craig Mautner8d341ef2013-03-26 09:03:27 -07002914 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2915 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002916 boolean printed = false;
2917 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002918 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2919 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002920 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002921 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002922 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002923 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 final ActivityStack stack = stacks.get(stackNdx);
2925 StringBuilder stackHeader = new StringBuilder(128);
2926 stackHeader.append(" Stack #");
2927 stackHeader.append(stack.mStackId);
2928 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002929 stackHeader.append("\n");
2930 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2931 stackHeader.append("\n");
2932 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2934 needSep, stackHeader.toString());
2935 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2936 !dumpAll, false, dumpPackage, true,
2937 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002938
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 needSep = printed;
2940 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2941 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002942 if (pr) {
2943 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002945 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2947 " mResumedActivity: ");
2948 if (pr) {
2949 printed = true;
2950 needSep = false;
2951 }
2952 if (dumpAll) {
2953 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2954 " mLastPausedActivity: ");
2955 if (pr) {
2956 printed = true;
2957 needSep = true;
2958 }
2959 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2960 needSep, " mLastNoHistoryActivity: ");
2961 }
2962 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002963 }
2964 }
2965
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002966 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2967 false, dumpPackage, true, " Activities waiting to finish:", null);
2968 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2969 false, dumpPackage, true, " Activities waiting to stop:", null);
2970 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2971 false, dumpPackage, true, " Activities waiting for another to become visible:",
2972 null);
2973 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2974 false, dumpPackage, true, " Activities waiting to sleep:", null);
2975 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2976 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002977
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002978 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002979 }
2980
Dianne Hackborn390517b2013-05-30 15:03:32 -07002981 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002982 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002983 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002984 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002985 String innerPrefix = null;
2986 String[] args = null;
2987 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 for (int i=list.size()-1; i>=0; i--) {
2989 final ActivityRecord r = list.get(i);
2990 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2991 continue;
2992 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002993 if (innerPrefix == null) {
2994 innerPrefix = prefix + " ";
2995 args = new String[0];
2996 }
2997 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002998 final boolean full = !brief && (complete || !r.isInHistory());
2999 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003000 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003001 needNL = false;
3002 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003003 if (header1 != null) {
3004 pw.println(header1);
3005 header1 = null;
3006 }
3007 if (header2 != null) {
3008 pw.println(header2);
3009 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003010 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003011 if (lastTask != r.task) {
3012 lastTask = r.task;
3013 pw.print(prefix);
3014 pw.print(full ? "* " : " ");
3015 pw.println(lastTask);
3016 if (full) {
3017 lastTask.dump(pw, prefix + " ");
3018 } else if (complete) {
3019 // Complete + brief == give a summary. Isn't that obvious?!?
3020 if (lastTask.intent != null) {
3021 pw.print(prefix); pw.print(" ");
3022 pw.println(lastTask.intent.toInsecureStringWithClip());
3023 }
3024 }
3025 }
3026 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3027 pw.print(" #"); pw.print(i); pw.print(": ");
3028 pw.println(r);
3029 if (full) {
3030 r.dump(pw, innerPrefix);
3031 } else if (complete) {
3032 // Complete + brief == give a summary. Isn't that obvious?!?
3033 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3034 if (r.app != null) {
3035 pw.print(innerPrefix); pw.println(r.app);
3036 }
3037 }
3038 if (client && r.app != null && r.app.thread != null) {
3039 // flush anything that is already in the PrintWriter since the thread is going
3040 // to write to the file descriptor directly
3041 pw.flush();
3042 try {
3043 TransferPipe tp = new TransferPipe();
3044 try {
3045 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3046 r.appToken, innerPrefix, args);
3047 // Short timeout, since blocking here can
3048 // deadlock with the application.
3049 tp.go(fd, 2000);
3050 } finally {
3051 tp.kill();
3052 }
3053 } catch (IOException e) {
3054 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3055 } catch (RemoteException e) {
3056 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3057 }
3058 needNL = true;
3059 }
3060 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003061 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003062 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003063
Craig Mautnerf3333272013-04-22 10:55:53 -07003064 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003065 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3066 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003067 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3068 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003069 }
3070
3071 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003072 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003073 }
3074
3075 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003076 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3077 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003078 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3079 }
3080
Craig Mautner05d29032013-05-03 13:40:13 -07003081 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003082 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3083 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3084 }
Craig Mautner05d29032013-05-03 13:40:13 -07003085 }
3086
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 void removeSleepTimeouts() {
3088 mSleepTimeout = false;
3089 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3090 }
3091
3092 final void scheduleSleepTimeout() {
3093 removeSleepTimeouts();
3094 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3095 }
3096
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 @Override
3098 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003099 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003100 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3101 }
3102
3103 @Override
3104 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003105 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3107 }
3108
3109 @Override
3110 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003111 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3113 }
3114
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003115 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003116 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003117 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003118 newDisplay = mActivityDisplays.get(displayId) == null;
3119 if (newDisplay) {
3120 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003121 if (activityDisplay.mDisplay == null) {
3122 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3123 return;
3124 }
Craig Mautner4504de52013-12-20 09:06:56 -08003125 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003126 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003127 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003128 }
Craig Mautner4504de52013-12-20 09:06:56 -08003129 if (newDisplay) {
3130 mWindowManager.onDisplayAdded(displayId);
3131 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003132 }
3133
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003134 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
3135 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
3136 return;
3137 }
3138 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
3139 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
3140 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
3141 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
3142 }
3143
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003144 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003145 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003146 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3147 if (activityDisplay != null) {
3148 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003149 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003150 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003151 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003152 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003153 }
3154 }
3155 mWindowManager.onDisplayRemoved(displayId);
3156 }
3157
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003158 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003160 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3161 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003162 // TODO: Update the bounds.
3163 }
3164 }
3165 mWindowManager.onDisplayChanged(displayId);
3166 }
3167
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003168 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003169 StackInfo info = new StackInfo();
3170 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3171 info.displayId = Display.DEFAULT_DISPLAY;
3172 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003173 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174
3175 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3176 final int numTasks = tasks.size();
3177 int[] taskIds = new int[numTasks];
3178 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003179 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003180 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003181 for (int i = 0; i < numTasks; ++i) {
3182 final TaskRecord task = tasks.get(i);
3183 taskIds[i] = task.taskId;
3184 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3185 : task.realActivity != null ? task.realActivity.flattenToString()
3186 : task.getTopActivity() != null ? task.getTopActivity().packageName
3187 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003188 taskBounds[i] = new Rect();
3189 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003190 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003191 }
3192 info.taskIds = taskIds;
3193 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003194 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003195 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003196 return info;
3197 }
3198
3199 StackInfo getStackInfoLocked(int stackId) {
3200 ActivityStack stack = getStack(stackId);
3201 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003202 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 }
3204 return null;
3205 }
3206
3207 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003208 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003209 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3210 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003211 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003212 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003213 }
3214 }
3215 return list;
3216 }
3217
Craig Mautner15df08a2015-04-01 12:17:18 -07003218 TaskRecord getLockedTaskLocked() {
3219 final int top = mLockTaskModeTasks.size() - 1;
3220 if (top >= 0) {
3221 return mLockTaskModeTasks.get(top);
3222 }
3223 return null;
3224 }
3225
3226 boolean isLockedTask(TaskRecord task) {
3227 return mLockTaskModeTasks.contains(task);
3228 }
3229
3230 boolean isLastLockedTask(TaskRecord task) {
3231 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3232 }
3233
3234 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003235 if (!mLockTaskModeTasks.remove(task)) {
3236 return;
3237 }
3238 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3239 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003240 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003241 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3242 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003243 final Message lockTaskMsg = Message.obtain();
3244 lockTaskMsg.arg1 = task.userId;
3245 lockTaskMsg.what = LOCK_TASK_END_MSG;
3246 mHandler.sendMessage(lockTaskMsg);
3247 }
3248 }
3249
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003250 void showNonResizeableDockToastIfNeeded(
3251 TaskRecord task, int preferredStackId, int actualStackId) {
3252 if (!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID) {
3253 return;
3254 }
3255
Jorim Jaggi51605272016-02-24 18:06:49 -05003256 if (!task.canGoInDockedStack() || task.mResizeMode == RESIZE_MODE_FORCE_RESIZEABLE) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003257 // Display warning toast if we tried to put a non-dockable task in the docked stack or
Jorim Jaggi51605272016-02-24 18:06:49 -05003258 // the task was forced to be resizable by the system.
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003259 mWindowManager.scheduleShowNonResizeableDockToast(task.taskId);
3260 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003261 }
3262
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003263 void showLockTaskToast() {
3264 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003265 }
3266
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003267 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3268 if (mLockTaskModeTasks.contains(task)) {
3269 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3270 }
3271 }
3272
Craig Mautner432f64e2015-05-20 14:59:57 -07003273 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3274 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003275 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003276 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003277 final TaskRecord lockedTask = getLockedTaskLocked();
3278 if (lockedTask != null) {
3279 removeLockedTaskLocked(lockedTask);
3280 if (!mLockTaskModeTasks.isEmpty()) {
3281 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003282 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3283 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003284 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003285 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003286 return;
3287 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003288 }
Craig Mautnere0570202015-05-13 13:06:11 -07003289 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3290 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003291 return;
3292 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003293
3294 // Should have already been checked, but do it again.
3295 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003296 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3297 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003298 return;
3299 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003300 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003301 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003302 return;
3303 }
3304
3305 if (mLockTaskModeTasks.isEmpty()) {
3306 // First locktask.
3307 final Message lockTaskMsg = Message.obtain();
3308 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3309 lockTaskMsg.arg1 = task.userId;
3310 lockTaskMsg.what = LOCK_TASK_START_MSG;
3311 lockTaskMsg.arg2 = lockTaskModeState;
3312 mHandler.sendMessage(lockTaskMsg);
3313 }
3314 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003315 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3316 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003317 mLockTaskModeTasks.remove(task);
3318 mLockTaskModeTasks.add(task);
3319
3320 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003321 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003322 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003323
3324 if (andResume) {
3325 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003326 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003327 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003328 }
3329
3330 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003331 return isLockTaskModeViolation(task, false);
3332 }
3333
3334 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3335 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003336 return false;
3337 }
3338 final int lockTaskAuth = task.mLockTaskAuth;
3339 switch (lockTaskAuth) {
3340 case LOCK_TASK_AUTH_DONT_LOCK:
3341 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003342 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003343 case LOCK_TASK_AUTH_LAUNCHABLE:
3344 case LOCK_TASK_AUTH_WHITELISTED:
3345 return false;
3346 case LOCK_TASK_AUTH_PINNABLE:
3347 // Pinnable tasks can't be launched on top of locktask tasks.
3348 return !mLockTaskModeTasks.isEmpty();
3349 default:
3350 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3351 return true;
3352 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003353 }
3354
Craig Mautner15df08a2015-04-01 12:17:18 -07003355 void onLockTaskPackagesUpdatedLocked() {
3356 boolean didSomething = false;
3357 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3358 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003359 final boolean wasWhitelisted =
3360 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3361 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003362 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003363 final boolean isWhitelisted =
3364 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3365 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3366 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003367 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003368 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3369 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003370 removeLockedTaskLocked(lockedTask);
3371 lockedTask.performClearTaskLocked();
3372 didSomething = true;
3373 }
3374 }
3375 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3376 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3377 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3378 final ActivityStack stack = stacks.get(stackNdx);
3379 stack.onLockTaskPackagesUpdatedLocked();
3380 }
3381 }
Craig Mautnere0570202015-05-13 13:06:11 -07003382 final ActivityRecord r = topRunningActivityLocked();
3383 final TaskRecord task = r != null ? r.task : null;
3384 if (mLockTaskModeTasks.isEmpty() && task != null
3385 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3386 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003387 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3388 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3389 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3390 false);
3391 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003392 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003393 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003394 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003395 }
3396 }
3397
Benjamin Franz43261142015-02-11 15:59:44 +00003398 int getLockTaskModeState() {
3399 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003400 }
3401
Jorim Jaggife89d122015-12-22 16:28:44 +01003402 void activityRelaunchedLocked(IBinder token) {
3403 mWindowManager.notifyAppRelaunchingFinished(token);
3404 }
3405
3406 void activityRelaunchingLocked(ActivityRecord r) {
3407 mWindowManager.notifyAppRelaunching(r.appToken);
3408 }
3409
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003410 void logStackState() {
3411 mActivityMetricsLogger.logWindowState();
3412 }
3413
Wale Ogunwale22e25262016-02-01 10:32:02 -08003414 void scheduleReportMultiWindowChanged(TaskRecord task) {
3415 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3416 final ActivityRecord r = task.mActivities.get(i);
3417 if (r.app != null && r.app.thread != null) {
3418 mMultiWindowModeChangedActivities.add(r);
3419 }
3420 }
3421
3422 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3423 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3424 }
3425 }
3426
3427 void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
3428 final ActivityStack stack = task.stack;
3429 if (prevStack == null || prevStack == stack
3430 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3431 return;
3432 }
3433
3434 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3435 final ActivityRecord r = task.mActivities.get(i);
3436 if (r.app != null && r.app.thread != null) {
3437 mPipModeChangedActivities.add(r);
3438 }
3439 }
3440
3441 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3442 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3443 }
3444 }
3445
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003446 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003447
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003448 public ActivityStackSupervisorHandler(Looper looper) {
3449 super(looper);
3450 }
3451
Craig Mautnerf3333272013-04-22 10:55:53 -07003452 void activityIdleInternal(ActivityRecord r) {
3453 synchronized (mService) {
3454 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3455 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003456 }
3457
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003458 @Override
3459 public void handleMessage(Message msg) {
3460 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003461 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3462 synchronized (mService) {
3463 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3464 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
3465 r.scheduleMultiWindowChanged();
3466 }
3467 }
3468 } break;
3469 case REPORT_PIP_MODE_CHANGED_MSG: {
3470 synchronized (mService) {
3471 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3472 final ActivityRecord r = mPipModeChangedActivities.remove(i);
3473 r.schedulePictureInPictureChanged();
3474 }
3475 }
3476 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003477 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003478 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3479 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003480 if (mService.mDidDexOpt) {
3481 mService.mDidDexOpt = false;
3482 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3483 nmsg.obj = msg.obj;
3484 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3485 return;
3486 }
3487 // We don't at this point know if the activity is fullscreen,
3488 // so we need to be conservative and assume it isn't.
3489 activityIdleInternal((ActivityRecord)msg.obj);
3490 } break;
3491 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003492 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003493 activityIdleInternal((ActivityRecord)msg.obj);
3494 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003495 case RESUME_TOP_ACTIVITY_MSG: {
3496 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003497 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003498 }
3499 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003500 case SLEEP_TIMEOUT_MSG: {
3501 synchronized (mService) {
3502 if (mService.isSleepingOrShuttingDown()) {
3503 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3504 mSleepTimeout = true;
3505 checkReadyForSleepLocked();
3506 }
3507 }
3508 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003509 case LAUNCH_TIMEOUT_MSG: {
3510 if (mService.mDidDexOpt) {
3511 mService.mDidDexOpt = false;
3512 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3513 return;
3514 }
3515 synchronized (mService) {
3516 if (mLaunchingActivity.isHeld()) {
3517 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3518 if (VALIDATE_WAKE_LOCK_CALLER
3519 && Binder.getCallingUid() != Process.myUid()) {
3520 throw new IllegalStateException("Calling must be system uid");
3521 }
3522 mLaunchingActivity.release();
3523 }
3524 }
3525 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003526 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003527 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003528 } break;
3529 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003530 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003531 } break;
3532 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003533 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003535 case CONTAINER_CALLBACK_VISIBILITY: {
3536 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003537 final IActivityContainerCallback callback = container.mCallback;
3538 if (callback != null) {
3539 try {
3540 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3541 } catch (RemoteException e) {
3542 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003543 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003544 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003545 case LOCK_TASK_START_MSG: {
3546 // When lock task starts, we disable the status bars.
3547 try {
Jason Monk62515be2014-05-21 16:06:19 -04003548 if (mLockTaskNotify == null) {
3549 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003550 }
Jason Monk62515be2014-05-21 16:06:19 -04003551 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003552 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003553 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003554 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003555 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003556 flags = StatusBarManager.DISABLE_MASK
3557 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003558 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003559 flags = StatusBarManager.DISABLE_MASK
3560 & (~StatusBarManager.DISABLE_BACK)
3561 & (~StatusBarManager.DISABLE_HOME)
3562 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003563 }
3564 getStatusBarService().disable(flags, mToken,
3565 mService.mContext.getPackageName());
3566 }
3567 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003568 if (getDevicePolicyManager() != null) {
3569 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003570 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003571 }
justinzhang5286d3f2014-05-12 17:06:01 -04003572 } catch (RemoteException ex) {
3573 throw new RuntimeException(ex);
3574 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003575 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003576 case LOCK_TASK_END_MSG: {
3577 // When lock task ends, we enable the status bars.
3578 try {
Jason Monk62515be2014-05-21 16:06:19 -04003579 if (getStatusBarService() != null) {
3580 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3581 mService.mContext.getPackageName());
3582 }
3583 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003584 if (getDevicePolicyManager() != null) {
3585 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3586 msg.arg1);
3587 }
Jason Monk62515be2014-05-21 16:06:19 -04003588 if (mLockTaskNotify == null) {
3589 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3590 }
3591 mLockTaskNotify.show(false);
3592 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003593 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003594 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003595 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003596 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003597 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003598 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003599 new LockPatternUtils(mService.mContext)
3600 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003601 }
3602 } catch (SettingNotFoundException e) {
3603 // No setting, don't lock.
3604 }
justinzhang5286d3f2014-05-12 17:06:01 -04003605 } catch (RemoteException ex) {
3606 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003607 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003608 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003609 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003610 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003611 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3612 if (mLockTaskNotify == null) {
3613 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3614 }
3615 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3616 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003617 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3618 final ActivityContainer container = (ActivityContainer) msg.obj;
3619 final IActivityContainerCallback callback = container.mCallback;
3620 if (callback != null) {
3621 try {
3622 callback.onAllActivitiesComplete(container.asBinder());
3623 } catch (RemoteException e) {
3624 }
3625 }
3626 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003627 case LAUNCH_TASK_BEHIND_COMPLETE: {
3628 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003629 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003630 if (r != null) {
3631 handleLaunchTaskBehindCompleteLocked(r);
3632 }
3633 }
3634 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003635
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003636 }
3637 }
3638 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003639
Ying Wangb081a592014-04-22 15:20:16 -07003640 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003641 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3642 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003643 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003644 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003645 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003646 ActivityRecord mParentActivity = null;
3647 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003648
Craig Mautnere3a00d72014-04-16 08:31:19 -07003649 boolean mVisible = true;
3650
Craig Mautner4a1cb222013-12-04 16:14:06 -08003651 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003652 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003654 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3655 final static int CONTAINER_STATE_NO_SURFACE = 1;
3656 final static int CONTAINER_STATE_FINISHING = 2;
3657 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3658
3659 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003660 synchronized (mService) {
3661 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003662 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003663 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003664 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003666 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003667
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003668 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003669 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003670 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003671 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003672 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003673 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003674 }
3675
3676 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003677 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003678 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003679 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3680 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 return;
3682 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003683 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003684 }
3685 }
3686
3687 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003688 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003689 synchronized (mService) {
3690 if (mActivityDisplay != null) {
3691 return mActivityDisplay.mDisplayId;
3692 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003693 }
3694 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003695 }
3696
Jeff Brownca9bc702014-02-11 14:32:56 -08003697 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003698 public int getStackId() {
3699 synchronized (mService) {
3700 return mStackId;
3701 }
3702 }
3703
3704 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003705 public boolean injectEvent(InputEvent event) {
3706 final long origId = Binder.clearCallingIdentity();
3707 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003708 synchronized (mService) {
3709 if (mActivityDisplay != null) {
3710 return mInputManagerInternal.injectInputEvent(event,
3711 mActivityDisplay.mDisplayId,
3712 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3713 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003714 }
3715 return false;
3716 } finally {
3717 Binder.restoreCallingIdentity(origId);
3718 }
3719 }
3720
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003721 @Override
3722 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003723 synchronized (mService) {
3724 if (mContainerState == CONTAINER_STATE_FINISHING) {
3725 return;
3726 }
3727 mContainerState = CONTAINER_STATE_FINISHING;
3728
Craig Mautnerd163e752014-06-13 17:18:47 -07003729 long origId = Binder.clearCallingIdentity();
3730 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003731 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003732 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003733 } finally {
3734 Binder.restoreCallingIdentity(origId);
3735 }
3736 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003737 }
3738
Craig Mautner60257702014-09-17 15:02:33 -07003739 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003740 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003741 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003742 if (mActivityDisplay != null) {
3743 mActivityDisplay.detachActivitiesLocked(mStack);
3744 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003745 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 }
3747 }
3748
3749 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003750 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003751 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 }
3753
3754 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003755 public final int startActivityIntentSender(IIntentSender intentSender)
3756 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003757 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3758
3759 if (!(intentSender instanceof PendingIntentRecord)) {
3760 throw new IllegalArgumentException("Bad PendingIntent object");
3761 }
3762
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003763 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003764 Binder.getCallingUid(), mCurrentUser, false,
3765 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003766
3767 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3768 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3769 pendingIntent.key.requestResolvedType);
3770
3771 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3772 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3773 }
3774
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003775 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003776 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003777 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003778 throw new SecurityException(
3779 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3780 }
3781 }
3782
Craig Mautnerdf88d732014-01-27 09:21:32 -08003783 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 public IBinder asBinder() {
3785 return this;
3786 }
3787
Craig Mautner4504de52013-12-20 09:06:56 -08003788 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003789 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003790 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003791 }
3792
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 ActivityStackSupervisor getOuter() {
3794 return ActivityStackSupervisor.this;
3795 }
3796
Craig Mautnerd163e752014-06-13 17:18:47 -07003797 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003798 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 }
3800
Craig Mautner6985bad2014-04-21 15:22:06 -07003801 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003802 void setVisible(boolean visible) {
3803 if (mVisible != visible) {
3804 mVisible = visible;
3805 if (mCallback != null) {
3806 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3807 0 /* unused */, this).sendToTarget();
3808 }
3809 }
3810 }
3811
Craig Mautner6985bad2014-04-21 15:22:06 -07003812 void setDrawn() {
3813 }
3814
Craig Mautner1b4bf852014-05-26 15:06:32 -07003815 // You can always start a new task on a regular ActivityStack.
3816 boolean isEligibleForNewTasks() {
3817 return true;
3818 }
3819
Craig Mautnerd163e752014-06-13 17:18:47 -07003820 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003821 detachLocked();
3822 deleteActivityContainer(this);
3823 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003824 }
3825
Craig Mautner34b73df2014-01-12 21:11:08 -08003826 @Override
3827 public String toString() {
3828 return mIdString + (mActivityDisplay == null ? "N" : "A");
3829 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003830 }
3831
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003832 private class VirtualActivityContainer extends ActivityContainer {
3833 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003834 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003835
3836 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3837 super(getNextStackId());
3838 mParentActivity = parent;
3839 mCallback = callback;
3840 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003841 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003842 }
3843
3844 @Override
3845 public void setSurface(Surface surface, int width, int height, int density) {
3846 super.setSurface(surface, width, height, density);
3847
3848 synchronized (mService) {
3849 final long origId = Binder.clearCallingIdentity();
3850 try {
3851 setSurfaceLocked(surface, width, height, density);
3852 } finally {
3853 Binder.restoreCallingIdentity(origId);
3854 }
3855 }
3856 }
3857
3858 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3859 if (mContainerState == CONTAINER_STATE_FINISHING) {
3860 return;
3861 }
3862 VirtualActivityDisplay virtualActivityDisplay =
3863 (VirtualActivityDisplay) mActivityDisplay;
3864 if (virtualActivityDisplay == null) {
3865 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003866 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003867 mActivityDisplay = virtualActivityDisplay;
3868 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003869 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003870 }
3871
3872 if (mSurface != null) {
3873 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003874 }
3875
Craig Mautner6985bad2014-04-21 15:22:06 -07003876 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003877 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003878 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003879 } else {
3880 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003881 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003882 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003883 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003884 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003885 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003886
Craig Mautnerd163e752014-06-13 17:18:47 -07003887 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003888
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003889 if (DEBUG_STACK) Slog.d(TAG_STACK,
3890 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003891 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003892
Craig Mautner6985bad2014-04-21 15:22:06 -07003893 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003894 boolean isAttachedLocked() {
3895 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003896 }
3897
3898 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003899 void setDrawn() {
3900 synchronized (mService) {
3901 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003902 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003903 }
3904 }
3905
Craig Mautner1b4bf852014-05-26 15:06:32 -07003906 // Never start a new task on an ActivityView if it isn't explicitly specified.
3907 @Override
3908 boolean isEligibleForNewTasks() {
3909 return false;
3910 }
3911
Craig Mautnerd163e752014-06-13 17:18:47 -07003912 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003913 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003914 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3915 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3916 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3917 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3918 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003919 }
3920 }
3921
Craig Mautner4a1cb222013-12-04 16:14:06 -08003922 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3923 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003924 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003926 int mDisplayId;
3927 Display mDisplay;
3928 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003929
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3931 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003932 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933
Jose Lima4b6c6692014-08-12 17:41:12 -07003934 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003935
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003936 ActivityDisplay() {
3937 }
Craig Mautner4504de52013-12-20 09:06:56 -08003938
Craig Mautner1a70a162014-09-13 12:09:31 -07003939 // After instantiation, check that mDisplay is not null before using this. The alternative
3940 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003941 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003942 final Display display = mDisplayManager.getDisplay(displayId);
3943 if (display == null) {
3944 return;
3945 }
3946 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003947 }
3948
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003949 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003950 mDisplay = display;
3951 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003953 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003954
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003955 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003956 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003957 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3958 + " onTop=" + onTop);
3959 if (onTop) {
3960 mStacks.add(stack);
3961 } else {
3962 mStacks.add(0, stack);
3963 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 }
3965
Craig Mautnere0a38842013-12-16 16:14:02 -08003966 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003967 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003968 + " from displayId=" + mDisplayId);
3969 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003970 }
3971
Jose Lima4b6c6692014-08-12 17:41:12 -07003972 void setVisibleBehindActivity(ActivityRecord r) {
3973 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003974 }
3975
Jose Lima4b6c6692014-08-12 17:41:12 -07003976 boolean hasVisibleBehindActivity() {
3977 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003978 }
3979
Craig Mautner34b73df2014-01-12 21:11:08 -08003980 @Override
3981 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003982 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3983 }
3984 }
3985
3986 class VirtualActivityDisplay extends ActivityDisplay {
3987 VirtualDisplay mVirtualDisplay;
3988
Craig Mautner6985bad2014-04-21 15:22:06 -07003989 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003990 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003991 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3992 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3993 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3994 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003995
3996 init(mVirtualDisplay.getDisplay());
3997
3998 mWindowManager.handleDisplayAdded(mDisplayId);
3999 }
4000
4001 void setSurface(Surface surface) {
4002 if (mVirtualDisplay != null) {
4003 mVirtualDisplay.setSurface(surface);
4004 }
4005 }
4006
4007 @Override
4008 void detachActivitiesLocked(ActivityStack stack) {
4009 super.detachActivitiesLocked(stack);
4010 if (mVirtualDisplay != null) {
4011 mVirtualDisplay.release();
4012 mVirtualDisplay = null;
4013 }
4014 }
4015
4016 @Override
4017 public String toString() {
4018 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004019 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004020 }
Jose Lima58e66d62014-05-27 20:00:27 -07004021
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004022 /**
4023 * Adjust bounds to stay within stack bounds.
4024 *
4025 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4026 * that keep them unchanged, but be contained within the stack bounds.
4027 *
4028 * @param bounds Bounds to be adjusted.
4029 * @param stackBounds Bounds within which the other bounds should remain.
4030 */
4031 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4032 if (stackBounds == null || stackBounds.contains(bounds)) {
4033 return;
4034 }
4035
4036 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4037 final int maxRight = stackBounds.right
4038 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4039 int horizontalDiff = stackBounds.left - bounds.left;
4040 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4041 || (bounds.left + horizontalDiff >= maxRight)) {
4042 horizontalDiff = maxRight - bounds.left;
4043 }
4044 bounds.left += horizontalDiff;
4045 bounds.right += horizontalDiff;
4046 }
4047
4048 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4049 final int maxBottom = stackBounds.bottom
4050 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4051 int verticalDiff = stackBounds.top - bounds.top;
4052 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4053 || (bounds.top + verticalDiff >= maxBottom)) {
4054 verticalDiff = maxBottom - bounds.top;
4055 }
4056 bounds.top += verticalDiff;
4057 bounds.bottom += verticalDiff;
4058 }
4059 }
4060
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004061 ActivityStack findStackBehind(ActivityStack stack) {
4062 // TODO(multi-display): We are only looking for stacks on the default display.
4063 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4064 if (display == null) {
4065 return null;
4066 }
4067 final ArrayList<ActivityStack> stacks = display.mStacks;
4068 for (int i = stacks.size() - 1; i >= 0; i--) {
4069 if (stacks.get(i) == stack && i > 0) {
4070 return stacks.get(i - 1);
4071 }
4072 }
4073 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4074 + " in=" + stacks);
4075 }
Craig Mautner27084302013-03-25 08:05:25 -07004076}