blob: 4f8f6707c17827542fc4c3ad4d25feba99bea097 [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
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080020import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
21import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
22import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
23import static android.app.ActivityManager.RESIZE_MODE_FORCED;
24import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Wale Ogunwale3797c222015-10-27 14:21:58 -070025import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.HOME_STACK_ID;
31import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
32import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
33import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynskidc394902015-12-14 10:20:22 -080034import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner29219d92013-04-16 20:19:12 -070035import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070036import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070037import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070038import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080039import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080040import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080041import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080047import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080052import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080053import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080058import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080062import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
63import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
64import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Craig Mautner05d29032013-05-03 13:40:13 -070065import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080066import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070067import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
68import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080069import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
70import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
71import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
72import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
73import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
74import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
75import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
76import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Craig Mautner15df08a2015-04-01 12:17:18 -070077import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
78import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010079import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070080import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
81import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070082
Svetoslav7008b512015-06-24 18:47:07 -070083import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070084import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080086import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070087import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080088import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.app.ActivityOptions;
90import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070091import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080092import android.app.IActivityContainer;
93import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080095import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070096import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040098import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040099import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700100import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700101import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103import android.content.Intent;
104import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700105import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700106import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.content.pm.PackageManager;
108import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100109import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800111import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112import android.hardware.display.DisplayManager;
113import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800114import android.hardware.display.DisplayManagerGlobal;
115import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import android.hardware.input.InputManager;
117import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.os.Binder;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700119import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700120import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700123import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700125import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700126import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700127import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400128import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700130import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700131import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700132import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100133import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700134import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700135import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400136import android.provider.Settings;
137import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700138import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700139import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700140import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700142import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800145import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800146import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800147import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800148import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800149
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800150import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700151import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400152import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700153import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400154import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800155import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700156import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700157import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700158
Craig Mautner8d341ef2013-03-26 09:03:27 -0700159import java.io.FileDescriptor;
160import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700161import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700162import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700163import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700164import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700165import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700166
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800168 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700169 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700170 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700171 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700172 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700173 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700174 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700175 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700176 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700177 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800178 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800180
Craig Mautnerf3333272013-04-22 10:55:53 -0700181 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700183
Craig Mautner0eea92c2013-05-16 13:35:39 -0700184 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700186
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700187 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700189
Craig Mautner05d29032013-05-03 13:40:13 -0700190 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
191 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
192 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700194 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800195 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
196 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
197 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700198 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400199 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
200 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700201 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700202 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700203 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204
Wale Ogunwale040b4702015-08-06 18:10:50 -0700205 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206
Jason Monk62515be2014-05-21 16:06:19 -0400207 private static final String LOCK_TASK_TAG = "Lock-to-App";
208
Wale Ogunwale040b4702015-08-06 18:10:50 -0700209 // Used to indicate if an object (e.g. stack) that we are trying to get
210 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800211 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700212
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700213 // Used to indicate that windows of activities should be preserved during the resize.
214 static final boolean PRESERVE_WINDOWS = true;
215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 // Used to indicate if an object (e.g. task) should be moved/created
217 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700218 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219
220 // Used to indicate that an objects (e.g. task) removal from its container
221 // (e.g. stack) is due to it moving to another container.
222 static final boolean MOVING = true;
223
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700224 // Force the focus to change to the stack we are moving a task to..
225 static final boolean FORCE_FOCUS = true;
226
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700227 // Restore task from the saved recents if it can't be found in any live stack.
228 static final boolean RESTORE_FROM_RECENTS = true;
229
Svetoslav7008b512015-06-24 18:47:07 -0700230 // Activity actions an app cannot start if it uses a permission which is not granted.
231 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
232 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700233
Svetoslav7008b512015-06-24 18:47:07 -0700234 static {
235 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
236 Manifest.permission.CAMERA);
237 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
238 Manifest.permission.CAMERA);
239 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
240 Manifest.permission.CALL_PHONE);
241 }
242
Svet Ganov99b60432015-06-27 13:15:22 -0700243 /** Action restriction: launching the activity is not restricted. */
244 private static final int ACTIVITY_RESTRICTION_NONE = 0;
245 /** Action restriction: launching the activity is restricted by a permission. */
246 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
247 /** Action restriction: launching the activity is restricted by an app op. */
248 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700249
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700250 // The height/width divide used when fitting a task within a bounds with method
251 // {@link #fitWithinBounds}.
252 // We always want the task to to be visible in the bounds without affecting its size when
253 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
254 // the input bounds right or bottom side minus the width or height divided by this value.
255 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
256
justinzhang5286d3f2014-05-12 17:06:01 -0400257 /** Status Bar Service **/
258 private IBinder mToken = new Binder();
259 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400260 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400261
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700262 // For debugging to make sure the caller when acquiring/releasing our
263 // wake lock is the system process.
264 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700265
Craig Mautner27084302013-03-25 08:05:25 -0700266 final ActivityManagerService mService;
267
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800268 private final RecentTasks mRecentTasks;
269
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270 final ActivityStackSupervisorHandler mHandler;
271
272 /** Short cut */
273 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700275
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700276 /** Counter for next free stack ID to use for dynamic activity stacks. */
277 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700278
279 /** Task identifier that activities are currently being started in. Incremented each time a
280 * new task is created. */
281 private int mCurTaskId = 0;
282
Craig Mautner2420ead2013-04-01 17:13:20 -0700283 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800284 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700285
Craig Mautnere0a38842013-12-16 16:14:02 -0800286 /** The stack containing the launcher app. Assumed to always be attached to
287 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800288 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700289
Craig Mautnere0a38842013-12-16 16:14:02 -0800290 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800291 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700292
Craig Mautner4a1cb222013-12-04 16:14:06 -0800293 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
294 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800295 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700297
298 /** List of activities that are waiting for a new activity to become visible before completing
299 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800300 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700302 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800303 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700304
305 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800306 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700307
Craig Mautnerde4ef022013-04-07 19:01:33 -0700308 /** List of activities that are ready to be stopped, but waiting for the next activity to
309 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800310 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700311
Craig Mautnerf3333272013-04-22 10:55:53 -0700312 /** List of activities that are ready to be finished, but waiting for the previous activity to
313 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800314 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700315
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800317 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700318
Craig Mautnerf3333272013-04-22 10:55:53 -0700319 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700320 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700321
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700322 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700323 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700324
Craig Mautnerde4ef022013-04-07 19:01:33 -0700325 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
326 * is being brought in front of us. */
327 boolean mUserLeaving = false;
328
Craig Mautner0eea92c2013-05-16 13:35:39 -0700329 /** Set when we have taken too long waiting to go to sleep. */
330 boolean mSleepTimeout = false;
331
332 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700333 * We don't want to allow the device to go to sleep while in the process
334 * of launching an activity. This is primarily to allow alarm intent
335 * receivers to launch an activity and get that to run before the device
336 * goes back to sleep.
337 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700338 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700339
340 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700341 * Set when the system is going to sleep, until we have
342 * successfully paused the current activity and released our wake lock.
343 * At that point the system is allowed to actually sleep.
344 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700345 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700347 /** Stack id of the front stack when user switched, indexed by userId. */
348 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700349
Craig Mautner4504de52013-12-20 09:06:56 -0800350 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800351 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700352 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353
354 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700355 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356
Jeff Brownca9bc702014-02-11 14:32:56 -0800357 InputManagerInternal mInputManagerInternal;
358
Craig Mautner15df08a2015-04-01 12:17:18 -0700359 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
360 * may be finished until there is only one entry left. If this is empty the system is not
361 * in lockTask mode. */
362 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000363 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700364 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
365 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000366 */
367 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400368 /**
369 * Notifies the user when entering/exiting lock-task.
370 */
371 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800372
Wale Ogunwaled046a012015-12-24 13:05:59 -0800373 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800374 boolean inResumeTopActivity;
375
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700376 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700377 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700378 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700379
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700380 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
381 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
382
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700383 // The default minimal size that will be used if the activity doesn't specify its minimal size.
384 // It will be calculated when the default display gets added.
385 private int mDefaultMinimalSizeOfResizeableTask = -1;
386
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700387 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
388 private boolean mTaskLayersChanged = true;
389
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800390 private final ActivityMetricsLogger mActivityMetricsLogger;
391
Wale Ogunwale39381972015-12-17 17:15:29 -0800392 static class FindTaskResult {
393 ActivityRecord r;
394 boolean matchedByRootAffinity;
395 }
396 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
397
Craig Mautneree36c772014-07-16 14:56:05 -0700398 /**
399 * Description of a request to start a new activity, which has been held
400 * due to app switches being disabled.
401 */
402 static class PendingActivityLaunch {
403 final ActivityRecord r;
404 final ActivityRecord sourceRecord;
405 final int startFlags;
406 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700407 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700408
409 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700410 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700411 r = _r;
412 sourceRecord = _sourceRecord;
413 startFlags = _startFlags;
414 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700415 callerApp = _callerApp;
416 }
417
418 void sendErrorResult(String message) {
419 try {
420 if (callerApp.thread != null) {
421 callerApp.thread.scheduleCrash(message);
422 }
423 } catch (RemoteException e) {
424 Slog.e(TAG, "Exception scheduling crash of failed "
425 + "activity launcher sourceRecord=" + sourceRecord, e);
426 }
Craig Mautneree36c772014-07-16 14:56:05 -0700427 }
428 }
429
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800430 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700431 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800432 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700433 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800434 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700435 }
436
437 /**
438 * At the time when the constructor runs, the power manager has not yet been
439 * initialized. So we initialize our wakelocks afterwards.
440 */
441 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800442 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700443 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700444 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700445 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700446 }
447
justinzhang5286d3f2014-05-12 17:06:01 -0400448 // This function returns a IStatusBarService. The value is from ServiceManager.
449 // getService and is cached.
450 private IStatusBarService getStatusBarService() {
451 synchronized (mService) {
452 if (mStatusBarService == null) {
453 mStatusBarService = IStatusBarService.Stub.asInterface(
454 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
455 if (mStatusBarService == null) {
456 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
457 }
458 }
459 return mStatusBarService;
460 }
461 }
462
Jason Monk35c62a42014-06-17 10:24:47 -0400463 private IDevicePolicyManager getDevicePolicyManager() {
464 synchronized (mService) {
465 if (mDevicePolicyManager == null) {
466 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
467 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
468 if (mDevicePolicyManager == null) {
469 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
470 }
471 }
472 return mDevicePolicyManager;
473 }
474 }
475
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700476 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800477 synchronized (mService) {
478 mWindowManager = wm;
479
480 mDisplayManager =
481 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
482 mDisplayManager.registerDisplayListener(this, null);
483
484 Display[] displays = mDisplayManager.getDisplays();
485 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
486 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700488 if (activityDisplay.mDisplay == null) {
489 throw new IllegalStateException("Default Display does not exist");
490 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700492 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800493 }
494
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800495 mHomeStack = mFocusedStack = mLastFocusedStack =
496 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800497
498 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800499 }
Craig Mautner27084302013-03-25 08:05:25 -0700500 }
501
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200502 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700503 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200504 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700505 }
506
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700507 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700509 }
510
Craig Mautnerde4ef022013-04-07 19:01:33 -0700511 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700513 }
514
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700515 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700516 if (stack == null) {
517 return false;
518 }
519
Craig Mautnerdf88d732014-01-27 09:21:32 -0800520 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
521 if (parent != null) {
522 stack = parent.task.stack;
523 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800524 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700525 }
526
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700527 /** The top most stack. */
528 boolean isFrontStack(ActivityStack stack) {
529 if (stack == null) {
530 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800531 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700532
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700533 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
534 if (parent != null) {
535 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800536 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700537 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
538 }
539
Wale Ogunwaled046a012015-12-24 13:05:59 -0800540 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800541 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
542 if (!focusCandidate.isFocusable()) {
543 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
544 focusCandidate = focusCandidate.getNextFocusableStackLocked();
545 }
546
547 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800548 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800549 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800550
Wale Ogunwaled046a012015-12-24 13:05:59 -0800551 EventLogTags.writeAmFocusedStack(
552 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
553 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
554 }
555
556 final ActivityRecord r = topRunningActivityLocked();
557 if (mService.mFocusedActivity != r) {
558 // The focus activity should always be the top activity in the focused stack.
559 // There will be chaos and anarchy if it isn't...
560 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
561 }
Craig Mautnerde313752015-01-22 14:28:03 -0800562
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800563 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800564 if (r != null && r.idle) {
565 checkFinishBootingLocked();
566 }
567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700568 }
569
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700570 void moveHomeStackToFront(String reason) {
571 mHomeStack.moveToFront(reason);
572 }
573
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700574 /** Returns true if the focus activity was adjusted to the home stack top activity. */
575 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700576 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
577 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700578 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700579 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700580
Craig Mautner84984fa2014-06-19 11:19:20 -0700581 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700582
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700583 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700584 if (top == null) {
585 return false;
586 }
587 mService.setFocusedActivityLocked(top, reason);
588 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700589 }
590
Craig Mautner299f9602015-01-26 09:47:33 -0800591 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700592 if (!mService.mBooting && !mService.mBooted) {
593 // Not ready yet!
594 return false;
595 }
596
Craig Mautner84984fa2014-06-19 11:19:20 -0700597 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
598 mWindowManager.showRecentApps();
599 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700600 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700601
Craig Mautner84984fa2014-06-19 11:19:20 -0700602 if (prev != null) {
603 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
604 }
605
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700606 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
607 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 final String myReason = reason + " resumeHomeStackTask";
609
Mark Lua56ea122015-10-08 13:31:01 +0800610 // Only resume home activity if isn't finishing.
611 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800612 mService.setFocusedActivityLocked(r, myReason);
613 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700614 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800615 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700616 }
617
Craig Mautner8d341ef2013-03-26 09:03:27 -0700618 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700619 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700620 }
621
622 /**
623 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
624 * @param id Id of the task we would like returned.
625 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
626 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700627 * @param stackId The stack to restore the task to (default launch stack will be used if
628 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700629 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700630 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800631 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800632 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800633 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
635 ActivityStack stack = stacks.get(stackNdx);
636 TaskRecord task = stack.taskForIdLocked(id);
637 if (task != null) {
638 return task;
639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700640 }
641 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800642
643 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800645 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800646 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800648 return null;
649 }
650
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700651 if (!restoreFromRecents) {
652 return task;
653 }
654
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700655 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700656 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
657 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800658 return null;
659 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700660 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800661 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700662 }
663
Craig Mautner6170f732013-04-02 13:05:23 -0700664 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800665 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800667 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
669 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
670 if (r != null) {
671 return r;
672 }
Craig Mautner6170f732013-04-02 13:05:23 -0700673 }
674 }
675 return null;
676 }
677
Craig Mautneref73ee12014-04-23 11:45:37 -0700678 void setNextTaskId(int taskId) {
679 if (taskId > mCurTaskId) {
680 mCurTaskId = taskId;
681 }
682 }
683
Craig Mautner8d341ef2013-03-26 09:03:27 -0700684 int getNextTaskId() {
685 do {
686 mCurTaskId++;
687 if (mCurTaskId <= 0) {
688 mCurTaskId = 1;
689 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700690 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700691 return mCurTaskId;
692 }
693
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800695 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700696 if (stack == null) {
697 return null;
698 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700699 ActivityRecord resumedActivity = stack.mResumedActivity;
700 if (resumedActivity == null || resumedActivity.app == null) {
701 resumedActivity = stack.mPausingActivity;
702 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700703 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 }
705 }
706 return resumedActivity;
707 }
708
Dianne Hackbornff072722014-09-24 10:56:28 -0700709 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700710 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
713 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
715 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700716 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800717 continue;
718 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700719 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800720 if (hr != null) {
721 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
722 && processName.equals(hr.processName)) {
723 try {
George Mount2c92c972014-03-20 09:38:23 -0700724 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 didSomething = true;
726 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700727 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 Slog.w(TAG, "Exception in new application when starting activity "
729 + hr.intent.getComponent().flattenToShortString(), e);
730 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700731 }
Craig Mautner20e72272013-04-01 13:45:53 -0700732 }
Craig Mautner20e72272013-04-01 13:45:53 -0700733 }
734 }
735 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700736 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700737 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700738 }
Craig Mautner20e72272013-04-01 13:45:53 -0700739 return didSomething;
740 }
741
742 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
744 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
746 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700747 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 continue;
749 }
750 final ActivityRecord resumedActivity = stack.mResumedActivity;
751 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700752 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800753 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800754 return false;
755 }
Craig Mautner20e72272013-04-01 13:45:53 -0700756 }
757 }
758 return true;
759 }
760
Craig Mautnerde4ef022013-04-07 19:01:33 -0700761 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
763 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
765 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700766 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700768 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 return false;
770 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 }
772 }
773 }
774 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700775 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
777 mLastFocusedStack + " to=" + mFocusedStack);
778 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700779 return true;
780 }
781
782 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800783 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
785 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
787 final ActivityStack stack = stacks.get(stackNdx);
788 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800789 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700790 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800791 return false;
792 }
793 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700795 }
796 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800797 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 }
799
Craig Mautner2acc3892013-09-23 10:28:14 -0700800 /**
801 * Pause all activities in either all of the stacks or just the back stacks.
802 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700803 * @return true if any activity was paused as a result of this call.
804 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700805 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700806 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
808 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
810 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700811 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700812 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800813 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700814 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
815 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700817 }
818 }
819 return someActivityPaused;
820 }
821
Craig Mautnerde4ef022013-04-07 19:01:33 -0700822 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700823 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
825 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
827 final ActivityStack stack = stacks.get(stackNdx);
828 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700829 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700831 Slog.d(TAG_STATES,
832 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 pausing = false;
834 } else {
835 return false;
836 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700837 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700838 }
839 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700840 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700841 }
842
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700843 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
844 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500845 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800846 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
847 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
848 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
849 final ActivityStack stack = stacks.get(stackNdx);
850 if (stack.mResumedActivity != null &&
851 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700852 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800853 }
854 }
855 }
856 }
857
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700858 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700859 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700860 }
861
862 void sendWaitingVisibleReportLocked(ActivityRecord r) {
863 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700864 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700865 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700866 if (w.who == null) {
867 changed = true;
868 w.timeout = false;
869 if (r != null) {
870 w.who = new ComponentName(r.info.packageName, r.info.name);
871 }
872 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
873 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700874 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700875 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700876 if (changed) {
877 mService.notifyAll();
878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700879 }
880
881 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
882 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700883 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700884 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700885 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700886 if (w.who == null) {
887 changed = true;
888 w.timeout = timeout;
889 if (r != null) {
890 w.who = new ComponentName(r.info.packageName, r.info.name);
891 }
892 w.thisTime = thisTime;
893 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700895 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700896 if (changed) {
897 mService.notifyAll();
898 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700899 }
900
Craig Mautner29219d92013-04-16 20:19:12 -0700901 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800902 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700903 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700904 if (r != null) {
905 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700906 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700907
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800909 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
911 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800912 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700913 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700914 if (r != null) {
915 return r;
916 }
917 }
918 }
919 return null;
920 }
921
Dianne Hackborn09233282014-04-30 11:33:59 -0700922 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700923 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
925 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800926 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800928 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
930 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700931 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700933 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700934 }
935 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700936
937 // The lists are already sorted from most recent to oldest. Just pull the most recent off
938 // each list and add it to list. Stop when all lists are empty or maxNum reached.
939 while (maxNum > 0) {
940 long mostRecentActiveTime = Long.MIN_VALUE;
941 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 final int numTaskLists = runningTaskLists.size();
943 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
944 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700945 if (!stackTaskList.isEmpty()) {
946 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
947 if (lastActiveTime > mostRecentActiveTime) {
948 mostRecentActiveTime = lastActiveTime;
949 selectedStackList = stackTaskList;
950 }
951 }
952 }
953 if (selectedStackList != null) {
954 list.add(selectedStackList.remove(0));
955 --maxNum;
956 } else {
957 break;
958 }
959 }
Craig Mautner20e72272013-04-01 13:45:53 -0700960 }
961
Todd Kennedy7440f172015-12-09 14:31:22 -0800962 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
963 ProfilerInfo profilerInfo) {
964 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 if (aInfo != null) {
966 // Store the found target back into the intent, because now that
967 // we have it we never want to do this again. For example, if the
968 // user navigates back to this point in the history, we should
969 // always restart the exact same activity.
970 intent.setComponent(new ComponentName(
971 aInfo.applicationInfo.packageName, aInfo.name));
972
973 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700974 if (!aInfo.processName.equals("system")) {
975 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 mService.setDebugApp(aInfo.processName, true, false);
977 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978
Man Caocfa78b22015-06-11 20:14:34 -0700979 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
980 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
981 }
982
983 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700984 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 }
986 }
987 }
988 return aInfo;
989 }
990
Todd Kennedy7440f172015-12-09 14:31:22 -0800991 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
992 try {
993 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
994 PackageManager.MATCH_DEFAULT_ONLY
995 | ActivityManagerService.STOCK_PM_FLAGS, userId);
996 } catch (RemoteException e) {
997 }
998 return null;
999 }
1000
1001 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1002 ProfilerInfo profilerInfo, int userId) {
1003 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1004 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1005 }
1006
Craig Mautner2420ead2013-04-01 17:13:20 -07001007 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001008 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001009 throws RemoteException {
1010
Craig Mautner2568c3a2015-03-26 14:22:34 -07001011 if (andResume) {
1012 r.startFreezingScreenLocked(app, 0);
1013 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001014
Craig Mautner2568c3a2015-03-26 14:22:34 -07001015 // schedule launch ticks to collect information about slow apps.
1016 r.startLaunchTickingLocked();
1017 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001018
1019 // Have the window manager re-evaluate the orientation of
1020 // the screen based on the new activity order. Note that
1021 // as a result of this, it can call back into the activity
1022 // manager with a new orientation. We don't care about that,
1023 // because the activity is not currently running so we are
1024 // just restarting it anyway.
1025 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001026 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001027 mService.mConfiguration,
1028 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001029 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001030 }
1031
1032 r.app = app;
1033 app.waitingToKill = null;
1034 r.launchCount++;
1035 r.lastLaunchTime = SystemClock.uptimeMillis();
1036
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001037 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001038
1039 int idx = app.activities.indexOf(r);
1040 if (idx < 0) {
1041 app.activities.add(r);
1042 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001043 mService.updateLruProcessLocked(app, true, null);
1044 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001045
Craig Mautner15df08a2015-04-01 12:17:18 -07001046 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001047 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1048 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001049 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001050 }
1051
1052 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001053 try {
1054 if (app.thread == null) {
1055 throw new RemoteException();
1056 }
1057 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001058 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001059 if (andResume) {
1060 results = r.results;
1061 newIntents = r.newIntents;
1062 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1064 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1065 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001066 if (andResume) {
1067 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1068 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001069 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001070 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001071 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001072 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001073 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001074 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001075 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001076 r.sleeping = false;
1077 r.forceNewConfig = false;
1078 mService.showAskCompatModeDialogLocked(r);
1079 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001080 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001081 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1082 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1083 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001084 final String profileFile = mService.mProfileFile;
1085 if (profileFile != null) {
1086 ParcelFileDescriptor profileFd = mService.mProfileFd;
1087 if (profileFd != null) {
1088 try {
1089 profileFd = profileFd.dup();
1090 } catch (IOException e) {
1091 if (profileFd != null) {
1092 try {
1093 profileFd.close();
1094 } catch (IOException o) {
1095 }
1096 profileFd = null;
1097 }
1098 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001100
1101 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1102 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001103 }
1104 }
1105 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001106
Craig Mautner2568c3a2015-03-26 14:22:34 -07001107 if (andResume) {
1108 app.hasShownUi = true;
1109 app.pendingUiClean = true;
1110 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001111 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001112 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001113 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001114 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001115 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001116 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001117
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001118 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001119 // This may be a heavy-weight process! Note that the package
1120 // manager will ensure that only activity can run in the main
1121 // process of the .apk, which is the only thing that will be
1122 // considered heavy-weight.
1123 if (app.processName.equals(app.info.packageName)) {
1124 if (mService.mHeavyWeightProcess != null
1125 && mService.mHeavyWeightProcess != app) {
1126 Slog.w(TAG, "Starting new heavy weight process " + app
1127 + " when already running "
1128 + mService.mHeavyWeightProcess);
1129 }
1130 mService.mHeavyWeightProcess = app;
1131 Message msg = mService.mHandler.obtainMessage(
1132 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1133 msg.obj = r;
1134 mService.mHandler.sendMessage(msg);
1135 }
1136 }
1137
1138 } catch (RemoteException e) {
1139 if (r.launchFailed) {
1140 // This is the second time we failed -- finish activity
1141 // and give up.
1142 Slog.e(TAG, "Second failure launching "
1143 + r.intent.getComponent().flattenToShortString()
1144 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001145 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001146 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1147 "2nd-crash", false);
1148 return false;
1149 }
1150
1151 // This is the first time we failed -- restart process and
1152 // retry.
1153 app.activities.remove(r);
1154 throw e;
1155 }
1156
1157 r.launchFailed = false;
1158 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001159 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001160 }
1161
1162 if (andResume) {
1163 // As part of the process of launching, ActivityThread also performs
1164 // a resume.
1165 stack.minimalResumeActivityLocked(r);
1166 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001167 // This activity is not starting in the resumed state... which should look like we asked
1168 // it to pause+stop (but remain visible), and it has done so and reported back the
1169 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001170 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001171 "Moving to PAUSED: " + r + " (starting in paused state)");
1172 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001173 }
1174
1175 // Launch the new version setup screen if needed. We do this -after-
1176 // launching the initial activity (that is, home), so that it can have
1177 // a chance to initialize itself while in the background, making the
1178 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001179 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001180 mService.startSetupActivityLocked();
1181 }
1182
Dianne Hackborn465fa392014-09-14 14:21:18 -07001183 // Update any services we are bound to that might care about whether
1184 // their client may have activities.
1185 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1186
Craig Mautner2420ead2013-04-01 17:13:20 -07001187 return true;
1188 }
1189
Craig Mautnere79d42682013-04-01 19:01:53 -07001190 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001191 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001192 // Is this activity's application already running?
1193 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001194 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001195
1196 r.task.stack.setLaunchTime(r);
1197
1198 if (app != null && app.thread != null) {
1199 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001200 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1201 || !"android".equals(r.info.packageName)) {
1202 // Don't add this if it is a platform component that is marked
1203 // to run in multiple processes, because this is actually
1204 // part of the framework so doesn't make sense to track as a
1205 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001206 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1207 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001208 }
George Mount2c92c972014-03-20 09:38:23 -07001209 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001210 return;
1211 } catch (RemoteException e) {
1212 Slog.w(TAG, "Exception when starting activity "
1213 + r.intent.getComponent().flattenToShortString(), e);
1214 }
1215
1216 // If a dead object exception was thrown -- fall through to
1217 // restart the application.
1218 }
1219
1220 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001221 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001222 }
1223
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001224 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001225 String resultWho, int requestCode, int callingPid, int callingUid,
1226 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1227 ActivityRecord resultRecord, ActivityStack resultStack) {
1228 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1229 callingUid);
1230 if (startAnyPerm == PERMISSION_GRANTED) {
1231 return true;
1232 }
1233 final int componentRestriction = getComponentRestrictionForCallingPackage(
1234 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1235 final int actionRestriction = getActionRestrictionForCallingPackage(
1236 intent.getAction(), callingPackage, callingPid, callingUid);
1237 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1238 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1239 if (resultRecord != null) {
1240 resultStack.sendActivityResultLocked(-1,
1241 resultRecord, resultWho, requestCode,
1242 Activity.RESULT_CANCELED, null);
1243 }
1244 final String msg;
1245 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1246 msg = "Permission Denial: starting " + intent.toString()
1247 + " from " + callerApp + " (pid=" + callingPid
1248 + ", uid=" + callingUid + ")" + " with revoked permission "
1249 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1250 } else if (!aInfo.exported) {
1251 msg = "Permission Denial: starting " + intent.toString()
1252 + " from " + callerApp + " (pid=" + callingPid
1253 + ", uid=" + callingUid + ")"
1254 + " not exported from uid " + aInfo.applicationInfo.uid;
1255 } else {
1256 msg = "Permission Denial: starting " + intent.toString()
1257 + " from " + callerApp + " (pid=" + callingPid
1258 + ", uid=" + callingUid + ")"
1259 + " requires " + aInfo.permission;
1260 }
1261 Slog.w(TAG, msg);
1262 throw new SecurityException(msg);
1263 }
1264
1265 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1266 final String message = "Appop Denial: starting " + intent.toString()
1267 + " from " + callerApp + " (pid=" + callingPid
1268 + ", uid=" + callingUid + ")"
1269 + " requires " + AppOpsManager.permissionToOp(
1270 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1271 Slog.w(TAG, message);
1272 return false;
1273 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1274 final String message = "Appop Denial: starting " + intent.toString()
1275 + " from " + callerApp + " (pid=" + callingPid
1276 + ", uid=" + callingUid + ")"
1277 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1278 Slog.w(TAG, message);
1279 return false;
1280 }
1281 return true;
1282 }
1283
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001284 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001285 final long identity = Binder.clearCallingIdentity();
1286 try {
1287 return UserManager.get(mService.mContext).getUserInfo(userId);
1288 } finally {
1289 Binder.restoreCallingIdentity(identity);
1290 }
1291 }
1292
Svet Ganov99b60432015-06-27 13:15:22 -07001293 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001294 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001295 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1296 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001297 == PackageManager.PERMISSION_DENIED) {
1298 return ACTIVITY_RESTRICTION_PERMISSION;
1299 }
1300
Christopher Tateff7add02015-08-17 10:23:22 -07001301 if (activityInfo.permission == null) {
1302 return ACTIVITY_RESTRICTION_NONE;
1303 }
1304
Svet Ganov99b60432015-06-27 13:15:22 -07001305 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1306 if (opCode == AppOpsManager.OP_NONE) {
1307 return ACTIVITY_RESTRICTION_NONE;
1308 }
1309
1310 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1311 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001312 if (!ignoreTargetSecurity) {
1313 return ACTIVITY_RESTRICTION_APPOP;
1314 }
Svet Ganov99b60432015-06-27 13:15:22 -07001315 }
1316
1317 return ACTIVITY_RESTRICTION_NONE;
1318 }
1319
Svetoslav7008b512015-06-24 18:47:07 -07001320 private int getActionRestrictionForCallingPackage(String action,
1321 String callingPackage, int callingPid, int callingUid) {
1322 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001323 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001324 }
1325
1326 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1327 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001328 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001329 }
1330
1331 final PackageInfo packageInfo;
1332 try {
1333 packageInfo = mService.mContext.getPackageManager()
1334 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1335 } catch (PackageManager.NameNotFoundException e) {
1336 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001337 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001338 }
1339
1340 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001341 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001342 }
1343
1344 if (mService.checkPermission(permission, callingPid, callingUid) ==
1345 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001346 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001347 }
1348
1349 final int opCode = AppOpsManager.permissionToOpCode(permission);
1350 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001351 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001352 }
1353
1354 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1355 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001356 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001357 }
1358
Svet Ganov99b60432015-06-27 13:15:22 -07001359 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001360 }
1361
Wale Ogunwaled046a012015-12-24 13:05:59 -08001362 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001363 if (r == null) {
1364 // Not sure what you are trying to do, but it is not going to work...
1365 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001366 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001367 final TaskRecord task = r.task;
1368 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001369 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001370 return false;
1371 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001372 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001373 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001374 }
1375
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001376 void setLaunchSource(int uid) {
1377 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1378 }
1379
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001380 void acquireLaunchWakelock() {
1381 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1382 throw new IllegalStateException("Calling must be system uid");
1383 }
1384 mLaunchingActivity.acquire();
1385 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1386 // To be safe, don't allow the wake lock to be held for too long.
1387 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1388 }
1389 }
1390
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001391 /**
1392 * Called when the frontmost task is idle.
1393 * @return the state of mService.mBooting before this was called.
1394 */
1395 private boolean checkFinishBootingLocked() {
1396 final boolean booting = mService.mBooting;
1397 boolean enableScreen = false;
1398 mService.mBooting = false;
1399 if (!mService.mBooted) {
1400 mService.mBooted = true;
1401 enableScreen = true;
1402 }
1403 if (booting || enableScreen) {
1404 mService.postFinishBooting(booting, enableScreen);
1405 }
1406 return booting;
1407 }
1408
Craig Mautnerf3333272013-04-22 10:55:53 -07001409 // Checked.
1410 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1411 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001412 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001413
Craig Mautnerf3333272013-04-22 10:55:53 -07001414 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001415 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001416 int NS = 0;
1417 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001418 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001419 boolean activityRemoved = false;
1420
Wale Ogunwale7d701172015-03-11 15:36:30 -07001421 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001422 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001423 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1424 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001425 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1426 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001427 if (fromTimeout) {
1428 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001429 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001430
1431 // This is a hack to semi-deal with a race condition
1432 // in the client where it can be constructed with a
1433 // newer configuration from when we asked it to launch.
1434 // We'll update with whatever configuration it now says
1435 // it used to launch.
1436 if (config != null) {
1437 r.configuration = config;
1438 }
1439
1440 // We are now idle. If someone is waiting for a thumbnail from
1441 // us, we can now deliver.
1442 r.idle = true;
1443
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001444 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001445 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001446 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001447 }
1448 }
1449
1450 if (allResumedActivitiesIdle()) {
1451 if (r != null) {
1452 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001453 }
1454
1455 if (mLaunchingActivity.isHeld()) {
1456 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1457 if (VALIDATE_WAKE_LOCK_CALLER &&
1458 Binder.getCallingUid() != Process.myUid()) {
1459 throw new IllegalStateException("Calling must be system uid");
1460 }
1461 mLaunchingActivity.release();
1462 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001463 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001464 }
1465
1466 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001467 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001468 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001469 if ((NF = mFinishingActivities.size()) > 0) {
1470 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001471 mFinishingActivities.clear();
1472 }
1473
Craig Mautnerf3333272013-04-22 10:55:53 -07001474 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001475 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001476 mStartingUsers.clear();
1477 }
1478
Craig Mautnerf3333272013-04-22 10:55:53 -07001479 // Stop any activities that are scheduled to do so but have been
1480 // waiting for the next one to start.
1481 for (int i = 0; i < NS; i++) {
1482 r = stops.get(i);
1483 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001484 if (stack != null) {
1485 if (r.finishing) {
1486 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1487 } else {
1488 stack.stopActivityLocked(r);
1489 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001490 }
1491 }
1492
1493 // Finish any activities that are scheduled to do so but have been
1494 // waiting for the next one to start.
1495 for (int i = 0; i < NF; i++) {
1496 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001497 final ActivityStack stack = r.task.stack;
1498 if (stack != null) {
1499 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1500 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001501 }
1502
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001503 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001504 // Complete user switch
1505 if (startingUsers != null) {
1506 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001507 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001508 }
1509 }
1510 // Complete starting up of background users
1511 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07001512 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001513 mStartingBackgroundUsers.clear();
1514 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001515 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001516 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001517 }
1518 }
1519
1520 mService.trimApplications();
1521 //dump();
1522 //mWindowManager.dump();
1523
Craig Mautnerf3333272013-04-22 10:55:53 -07001524 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001525 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001526 }
1527
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001528 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001529 }
1530
Craig Mautner8e569572013-10-11 17:36:59 -07001531 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001532 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001533 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1534 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001535 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1536 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1537 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001538 }
Craig Mautner19091252013-10-05 00:03:53 -07001539 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001540 }
1541
1542 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1546 stacks.get(stackNdx).closeSystemDialogsLocked();
1547 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001548 }
1549 }
1550
Craig Mautner93529a42013-10-04 15:03:13 -07001551 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001552 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001553 }
1554
Craig Mautner8d341ef2013-03-26 09:03:27 -07001555 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001556 * Update the last used stack id for non-current user (current user's last
1557 * used stack is the focused stack)
1558 */
1559 void updateUserStackLocked(int userId, ActivityStack stack) {
1560 if (userId != mCurrentUser) {
1561 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1562 }
1563 }
1564
1565 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001566 * @return true if some activity was finished (or would have finished if doit were true).
1567 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001568 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1569 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001570 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1572 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001573 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001574 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001575 if (stack.finishDisabledPackageActivitiesLocked(
1576 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001577 didSomething = true;
1578 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001579 }
1580 }
1581 return didSomething;
1582 }
1583
Dianne Hackborna413dc02013-07-12 12:02:55 -07001584 void updatePreviousProcessLocked(ActivityRecord r) {
1585 // Now that this process has stopped, we may want to consider
1586 // it to be the previous app to try to keep around in case
1587 // the user wants to return to it.
1588
1589 // First, found out what is currently the foreground app, so that
1590 // we don't blow away the previous app if this activity is being
1591 // hosted by the process that is actually still the foreground.
1592 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001593 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1594 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001595 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1596 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001597 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001598 if (stack.mResumedActivity != null) {
1599 fgApp = stack.mResumedActivity.app;
1600 } else if (stack.mPausingActivity != null) {
1601 fgApp = stack.mPausingActivity.app;
1602 }
1603 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001604 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001605 }
1606 }
1607
1608 // Now set this one as the previous process, only if that really
1609 // makes sense to.
1610 if (r.app != null && fgApp != null && r.app != fgApp
1611 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001612 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001613 mService.mPreviousProcess = r.app;
1614 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1615 }
1616 }
1617
Wale Ogunwaled046a012015-12-24 13:05:59 -08001618 boolean resumeFocusedStackTopActivityLocked() {
1619 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001620 }
1621
Wale Ogunwaled046a012015-12-24 13:05:59 -08001622 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001623 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001624 if (targetStack != null && isFocusedStack(targetStack)) {
1625 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001626 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001627 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1628 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001629 }
1630
Todd Kennedy539db512014-12-15 09:57:55 -08001631 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001632 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1633 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001634 final int numStacks = stacks.size();
1635 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1636 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08001637 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001638 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001639 }
1640 }
1641
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001642 void finishVoiceTask(IVoiceInteractionSession session) {
1643 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1644 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1645 final int numStacks = stacks.size();
1646 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1647 final ActivityStack stack = stacks.get(stackNdx);
1648 stack.finishVoiceTask(session);
1649 }
1650 }
1651 }
1652
Chong Zhang280d3322015-11-03 17:27:26 -08001653 void findTaskToMoveToFrontLocked(
1654 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001655 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1656 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001657 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001658 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1659 // Caller wants the home activity moved with it. To accomplish this,
1660 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001661 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001662 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001663 if (task.stack == null) {
1664 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1665 + task + " to front. Stack is null");
1666 return;
1667 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001668
Chong Zhang0fa656b2015-08-31 15:17:21 -07001669 if (task.mResizeable && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001670 int stackId = options.getLaunchStackId();
1671 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001672 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001673 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001674 if (stackId == INVALID_STACK_ID) {
1675 stackId = task.getLaunchStackId();
1676 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001677 if (stackId != task.stack.mStackId) {
1678 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1679 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1680 // still need moveTaskToFrontLocked() below for any transition settings.
1681 }
1682 // WM resizeTask must be done after the task is moved to the correct stack,
1683 // because Task's setBounds() also updates dim layer's bounds, but that has
1684 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07001685 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
1686 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07001687 }
1688 }
1689
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001690 final ActivityRecord r = task.getTopActivity();
1691 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1692 r == null ? null : r.appTimeTracker, reason);
1693
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001694 if (DEBUG_STACK) Slog.d(TAG_STACK,
1695 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001696 }
1697
Wale Ogunwale854809c2015-12-27 16:18:19 -08001698 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001699 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001700 // window management or is launching into the pinned stack.
1701 if (!options.hasLaunchBounds()) {
1702 return false;
1703 }
1704 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1705 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001706 }
1707
Craig Mautner967212c2013-04-13 21:10:58 -07001708 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001709 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001710 }
1711
1712 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001713 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1714 if (activityContainer != null) {
1715 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001716 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001717 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001718 return null;
1719 }
1720 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001721 }
1722
Craig Mautner967212c2013-04-13 21:10:58 -07001723 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001724 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1726 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001727 }
1728 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001729 }
1730
Craig Mautner4a1cb222013-12-04 16:14:06 -08001731 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001732 ActivityRecord homeActivity = getHomeActivity();
1733 if (homeActivity != null) {
1734 return homeActivity.appToken;
1735 }
1736 return null;
1737 }
1738
1739 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001740 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001741 }
1742
1743 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001744 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1745 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1746 final TaskRecord task = tasks.get(taskNdx);
1747 if (task.isHomeTask()) {
1748 final ArrayList<ActivityRecord> activities = task.mActivities;
1749 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1750 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001751 if (r.isHomeActivity()
1752 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001753 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001754 }
1755 }
1756 }
1757 }
1758 return null;
1759 }
1760
Chong Zhangb15758a2015-11-17 12:12:03 -08001761 /**
1762 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1763 * the docked stack itself, or if it's side-by-side to the docked stack.
1764 */
1765 boolean isStackDockedInEffect(int stackId) {
1766 return stackId == DOCKED_STACK_ID ||
1767 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1768 }
1769
Todd Kennedyca4d8422015-01-15 15:19:22 -08001770 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001771 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001772 ActivityContainer activityContainer =
1773 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001774 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001775 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1776 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001777 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001778 return activityContainer;
1779 }
1780
Craig Mautner34b73df2014-01-12 21:11:08 -08001781 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001782 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1783 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1784 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001785 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1786 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001787 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001788 }
1789 }
1790
Craig Mautner95da1082014-02-24 17:54:35 -08001791 void deleteActivityContainer(IActivityContainer container) {
1792 ActivityContainer activityContainer = (ActivityContainer)container;
1793 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001794 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1795 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001796 final int stackId = activityContainer.mStackId;
1797 mActivityContainers.remove(stackId);
1798 mWindowManager.removeStack(stackId);
1799 }
1800 }
1801
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001802 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
1803 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001804 final ActivityStack stack = getStack(stackId);
1805 if (stack == null) {
1806 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
1807 return;
1808 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001809
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07001810 if (!allowResizeInDockedMode
1811 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
1812 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
1813 // stack size changing so things don't get out of sync.
1814 return;
1815 }
1816
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001817 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02001818 mWindowManager.deferSurfaceLayout();
1819 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08001820
1821 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
1822 // The bounds passed in corresponds to the fullscreen bounds which we normally
1823 // represent with null. Go ahead and set it to null so that all tasks configuration
1824 // can have the right fullscreen state.
1825 bounds = null;
1826 }
1827
Jorim Jaggic4025202015-10-22 16:43:34 +02001828 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001829
Jorim Jaggic4025202015-10-22 16:43:34 +02001830 mTmpBounds.clear();
1831 mTmpConfigs.clear();
1832 ArrayList<TaskRecord> tasks = stack.getAllTasks();
1833 for (int i = tasks.size() - 1; i >= 0; i--) {
1834 TaskRecord task = tasks.get(i);
1835 if (task.mResizeable) {
1836 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1837 // For freeform stack we don't adjust the size of the tasks to match that
1838 // of the stack, but we do try to make sure the tasks are still contained
1839 // with the bounds of the stack.
1840 tempRect2.set(task.mBounds);
1841 fitWithinBounds(tempRect2, bounds);
1842 task.updateOverrideConfiguration(tempRect2);
1843 } else {
1844 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001845 }
1846 }
1847
Jorim Jaggic4025202015-10-22 16:43:34 +02001848 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
1849 mTmpBounds.put(task.taskId, task.mBounds);
1850 }
1851 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
1852 if (stack.mStackId == DOCKED_STACK_ID) {
1853 // Dock stack funness...Yay!
1854 if (stack.mFullscreen) {
1855 // The dock stack went fullscreen which is kinda like dismissing it.
1856 // In this case we make all other static stacks fullscreen and move all
1857 // docked stack tasks to the fullscreen stack.
1858 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001859 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001860 resizeStackLocked(i, null, preserveWindows, true);
1861 }
1862 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001863
Jorim Jaggic4025202015-10-22 16:43:34 +02001864 final int count = tasks.size();
1865 for (int i = 0; i < count; i++) {
1866 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08001867 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08001868 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02001869 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001870
Jorim Jaggic4025202015-10-22 16:43:34 +02001871 // stack shouldn't contain anymore activities, so nothing to resume.
1872 r = null;
1873 } else {
1874 // Docked stacks occupy a dedicated region on screen so the size of all other
1875 // static stacks need to be adjusted so they don't overlap with the docked stack.
1876 // We get the bounds to use from window manager which has been adjusted for any
1877 // screen controls and is also the same for all stacks.
1878 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
1879
1880 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001881 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02001882 ActivityStack otherStack = getStack(i);
1883 if (otherStack != null) {
1884 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
1885 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001886 }
1887 }
1888 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001889 // Since we are resizing the stack, all other operations should strive to preserve
1890 // windows.
1891 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001892 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001893 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001894
Jorim Jaggic4025202015-10-22 16:43:34 +02001895 if (r != null) {
1896 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
1897 // And we need to make sure at this point that all other activities
1898 // are made visible with the correct configuration.
1899 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
1900 if (!updated) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001901 resumeFocusedStackTopActivityLocked();
Jorim Jaggic4025202015-10-22 16:43:34 +02001902 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001903 }
Jorim Jaggic4025202015-10-22 16:43:34 +02001904 } finally {
1905 mWindowManager.continueSurfaceLayout();
1906 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001907 }
1908 }
1909
Chong Zhang6de2ae82015-09-30 18:25:21 -07001910 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001911 if (!task.mResizeable) {
1912 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
1913 return;
1914 }
1915
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001916 adjustForMinimalTaskDimensions(task, bounds);
1917
Chong Zhang87b21722015-09-21 15:39:51 -07001918 // If this is a forced resize, let it go through even if the bounds is not changing,
1919 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07001920 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07001921 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001922 // Nothing to do here...
1923 return;
1924 }
1925
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001926 if (!mWindowManager.isValidTaskId(task.taskId)) {
1927 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07001928 // All we can do for now is update the bounds so it can be used when the task is
1929 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001930 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001931 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
1932 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07001933 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001934 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001935 return;
1936 }
1937
Chong Zhang6de2ae82015-09-30 18:25:21 -07001938 // Do not move the task to another stack here.
1939 // This method assumes that the task is already placed in the right stack.
1940 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001941
Chong Zhang6de2ae82015-09-30 18:25:21 -07001942 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07001943
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001944 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07001945 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001946 // way and the activity was kept the way it was. If it's false, it means the activity had
1947 // to be relaunched due to configuration change.
1948 boolean kept = true;
1949 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001950 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001951 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001952 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07001953 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001954 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001955 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001956 if (!kept) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001957 resumeFocusedStackTopActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08001958 }
1959 }
1960 }
Chong Zhang87b21722015-09-21 15:39:51 -07001961 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07001962
1963 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08001964 }
1965
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001966 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
1967 if (bounds == null) {
1968 return;
1969 }
1970 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
1971 : task.mMinimalSize;
1972 final boolean adjustWidth = minimalSize > bounds.width();
1973 final boolean adjustHeight = minimalSize > bounds.height();
1974 if (!(adjustWidth || adjustHeight)) {
1975 return;
1976 }
1977 Rect taskBounds = task.mBounds;
1978 if (adjustWidth) {
1979 if (taskBounds != null && bounds.right == taskBounds.right) {
1980 bounds.left = bounds.right - minimalSize;
1981 } else {
1982 // Either left bounds match, or neither match, or the previous bounds were
1983 // fullscreen and we default to keeping left.
1984 bounds.right = bounds.left + minimalSize;
1985 }
1986 }
1987 if (adjustHeight) {
1988 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
1989 bounds.top = bounds.bottom - minimalSize;
1990 } else {
1991 // Either top bounds match, or neither match, or the previous bounds were
1992 // fullscreen and we default to keeping top.
1993 bounds.bottom = bounds.top + minimalSize;
1994 }
1995 }
1996 }
1997
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001998 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001999 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2000 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002001 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002002 }
2003
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002004 ActivityContainer activityContainer = new ActivityContainer(stackId);
2005 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002006 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002007 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002008 }
2009
2010 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002011 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002012 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2013 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002014 break;
2015 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002016 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002017 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002018 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002019 }
2020
Chong Zhang5dcb2752015-08-18 13:50:26 -07002021 /**
2022 * Restores a recent task to a stack
2023 * @param task The recent task to be restored.
2024 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002025 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002026 * @return true if the task has been restored successfully.
2027 */
2028 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2029 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002030 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07002031 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002032 if (task.stack != null) {
2033 // Task has already been restored once. See if we need to do anything more
2034 if (task.stack.mStackId == stackId) {
2035 // Nothing else to do since it is already restored in the right stack.
2036 return true;
2037 }
2038 // Remove current stack association, so we can re-associate the task with the
2039 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002040 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002041 }
2042
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002043 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002044 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002045
2046 if (stack == null) {
2047 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002048 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2049 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002050 return false;
2051 }
2052
Wale Ogunwale5f986092015-12-04 15:35:38 -08002053 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002054 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2055 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002056 final ArrayList<ActivityRecord> activities = task.mActivities;
2057 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002058 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002059 }
2060 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002061 }
2062
Wale Ogunwale040b4702015-08-06 18:10:50 -07002063 /**
2064 * Moves the specified task record to the input stack id.
2065 * WARNING: This method performs an unchecked/raw move of the task and
2066 * can leave the system in an unstable state if used incorrectly.
2067 * Use {@link #moveTaskToStackLocked} to perform safe task movement
2068 * to a stack.
2069 * @param task Task to move.
2070 * @param stackId Id of stack to move task to.
2071 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002072 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002073 * @param reason Reason the task is been moved.
2074 * @return The stack the task was moved to.
2075 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002076 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002077 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
2078 final ActivityRecord r = task.getTopActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002079 final ActivityStack prevStack = task.stack;
2080 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
2081 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
2082 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2083 // Whenever we are moving the top activity from the front stack we want to make sure to move
2084 // the stack to the front.
2085 final boolean wasFront = isFrontStack(prevStack)
2086 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002087
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002088 final boolean resizeable = task.mResizeable;
2089 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2090 // if a docked stack is created below which will lead to the stack we are moving from and
2091 // its resizeable tasks being resized.
2092 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002093 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002094 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002095 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002096 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002097
2098 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002099 // move focus to the new stack by moving the stack to the front.
2100 stack.moveToFrontAndResumeStateIfNeeded(
2101 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002102
Wale Ogunwale040b4702015-08-06 18:10:50 -07002103 return stack;
2104 }
2105
Filip Gruszczynski90186c62015-10-26 14:07:00 -07002106 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002107 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002108 final TaskRecord task = anyTaskForIdLocked(taskId);
2109 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002110 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002111 return;
2112 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002113
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002114 if (task.stack != null && task.stack.mStackId == stackId) {
2115 // You are already in the right stack silly...
2116 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
2117 return;
2118 }
2119
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002120 final ActivityRecord topActivity = task.getTopActivity();
2121 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002122 // We are about to relaunch the activity because its configuration changed due to
2123 // being maximized, i.e. size change. The activity will first remove the old window
2124 // and then add a new one. This call will tell window manager about this, so it can
2125 // preserve the old window until the new one is drawn. This prevents having a gap
2126 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002127 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002128 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002129 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08002130 final ActivityStack stack = moveTaskToStackUncheckedLocked(
2131 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002132
Jorim Jaggi55387522015-11-24 18:21:10 -08002133 if (!animate) {
2134 stack.mNoAnimActivities.add(topActivity);
2135 }
2136
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002137 // Make sure the task has the appropriate bounds/size for the stack it is in.
2138 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002139 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002140 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
2141 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07002142 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
2143 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07002144 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002145 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002146 }
2147
Stefan Kuhne54714cd2015-05-12 13:42:18 -07002148 // The task might have already been running and its visibility needs to be synchronized with
2149 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002150 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002151 resumeFocusedStackTopActivityLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08002152
2153 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08002154 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08002155 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002156 }
2157
Wale Ogunwale079a0042015-10-24 11:44:07 -07002158 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2159 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2160 if (stack == null) {
2161 throw new IllegalArgumentException(
2162 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2163 }
2164
2165 final ActivityRecord r = stack.topRunningActivityLocked();
2166 if (r == null) {
2167 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2168 + " in stack=" + stack);
2169 return false;
2170 }
2171
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002172 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002173 Slog.w(TAG,
2174 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002175 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002176 return false;
2177 }
2178
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002179 moveActivityToStackLocked(r, PINNED_STACK_ID, "moveTopActivityToPinnedStack", bounds);
2180 return true;
2181 }
2182
2183 void moveActivityToStackLocked(ActivityRecord r, int stackId, String reason, Rect bounds) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002184 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07002185 if (task.mActivities.size() == 1) {
2186 // There is only one activity in the task. So, we can just move the task over to the
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002187 // stack without re-parenting the activity in a different task.
2188 moveTaskToStackLocked(
2189 task.taskId, stackId, ON_TOP, FORCE_FOCUS, reason, true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002190 } else {
Wale Ogunwaled88f6512015-12-06 19:39:01 -08002191 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
2192 stack.moveActivityToStack(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002193 }
2194
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002195 if (bounds != null) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002196 resizeStackLocked(stackId, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002197 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002198
2199 // The task might have already been running and its visibility needs to be synchronized with
2200 // the visibility of the stack / windows.
2201 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002202 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002203
2204 if (stackId == PINNED_STACK_ID) {
2205 mService.notifyActivityPinnedLocked();
2206 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07002207 }
2208
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002209 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2210 final TaskRecord task = anyTaskForIdLocked(taskId);
2211 if (task == null) {
2212 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2213 return;
2214 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002215 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2216
2217 task.updateOverrideConfigurationForStack(stack);
2218
2219 mWindowManager.positionTaskInStack(
2220 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002221 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002222 // The task might have already been running and its visibility needs to be synchronized with
2223 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002224 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002225 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002226 }
2227
Craig Mautnerac6f8432013-07-17 13:24:59 -07002228 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002229 mTmpFindTaskResult.r = null;
2230 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002231 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002232 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2233 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002234 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2235 final ActivityStack stack = stacks.get(stackNdx);
2236 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002237 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002238 continue;
2239 }
2240 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002241 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2242 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002243 continue;
2244 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002245 stack.findTaskLocked(r, mTmpFindTaskResult);
2246 // It is possible to have task in multiple stacks with the same root affinity.
2247 // If the match we found was based on root affinity we keep on looking to see if
2248 // there is a better match in another stack. We eventually return the match based
2249 // on root affinity if we don't find a better match.
2250 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2251 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002252 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002253 }
2254 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002255 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2256 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002257 }
2258
2259 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002260 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2261 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002262 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2263 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2264 if (ar != null) {
2265 return ar;
2266 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 }
2268 }
2269 return null;
2270 }
2271
Craig Mautner8d341ef2013-03-26 09:03:27 -07002272 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002273 scheduleSleepTimeout();
2274 if (!mGoingToSleep.isHeld()) {
2275 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002276 if (mLaunchingActivity.isHeld()) {
2277 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2278 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002279 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002280 mLaunchingActivity.release();
2281 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002282 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002283 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002284 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002285 }
2286
2287 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002288 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002289
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002290 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002291 final long endTime = System.currentTimeMillis() + timeout;
2292 while (true) {
2293 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002294 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2295 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002296 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2297 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2298 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002299 }
2300 if (cantShutdown) {
2301 long timeRemaining = endTime - System.currentTimeMillis();
2302 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002303 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002304 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002305 } catch (InterruptedException e) {
2306 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002307 } else {
2308 Slog.w(TAG, "Activity manager shutdown timed out");
2309 timedout = true;
2310 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002311 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002312 } else {
2313 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002314 }
2315 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002316
2317 // Force checkReadyForSleep to complete.
2318 mSleepTimeout = true;
2319 checkReadyForSleepLocked();
2320
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 return timedout;
2322 }
2323
2324 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002325 removeSleepTimeouts();
2326 if (mGoingToSleep.isHeld()) {
2327 mGoingToSleep.release();
2328 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002329 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2330 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002331 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2332 final ActivityStack stack = stacks.get(stackNdx);
2333 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002334 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002335 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002336 }
Craig Mautner5314a402013-09-26 12:40:16 -07002337 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002338 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002339 mGoingToSleepActivities.clear();
2340 }
2341
2342 void activitySleptLocked(ActivityRecord r) {
2343 mGoingToSleepActivities.remove(r);
2344 checkReadyForSleepLocked();
2345 }
2346
2347 void checkReadyForSleepLocked() {
2348 if (!mService.isSleepingOrShuttingDown()) {
2349 // Do not care.
2350 return;
2351 }
2352
2353 if (!mSleepTimeout) {
2354 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002355 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2356 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002357 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2358 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2359 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002360 }
2361
2362 if (mStoppingActivities.size() > 0) {
2363 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002364 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002365 + mStoppingActivities.size() + " activities");
2366 scheduleIdleLocked();
2367 dontSleep = true;
2368 }
2369
2370 if (mGoingToSleepActivities.size() > 0) {
2371 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002372 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002373 + mGoingToSleepActivities.size() + " activities");
2374 dontSleep = true;
2375 }
2376
2377 if (dontSleep) {
2378 return;
2379 }
2380 }
2381
Craig Mautnere0a38842013-12-16 16:14:02 -08002382 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2383 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002384 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2385 stacks.get(stackNdx).goToSleep();
2386 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002387 }
2388
2389 removeSleepTimeouts();
2390
2391 if (mGoingToSleep.isHeld()) {
2392 mGoingToSleep.release();
2393 }
2394 if (mService.mShuttingDown) {
2395 mService.notifyAll();
2396 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002397 }
2398
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002399 boolean reportResumedActivityLocked(ActivityRecord r) {
2400 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002401 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002402 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002403 }
2404 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002405 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002406 mWindowManager.executeAppTransition();
2407 return true;
2408 }
2409 return false;
2410 }
2411
Craig Mautner8d341ef2013-03-26 09:03:27 -07002412 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002413 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2414 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002415 int stackNdx = stacks.size() - 1;
2416 while (stackNdx >= 0) {
2417 stacks.get(stackNdx).handleAppCrashLocked(app);
2418 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002419 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002420 }
2421 }
2422
Jose Lima4b6c6692014-08-12 17:41:12 -07002423 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002424 final ActivityStack stack = r.task.stack;
2425 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002426 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2427 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002428 return false;
2429 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002430 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002431 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2432 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002433
2434 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002435 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002436 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002437 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002438 return true;
2439 }
2440
2441 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002442 if (visible && top.fullscreen) {
2443 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002444 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2445 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2446 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2447 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002448 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002449 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2450 // Only the activity set as currently visible behind should actively reset its
2451 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002452 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2453 "requestVisibleBehind: returning visible=" + visible
2454 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2455 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002456 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002457 }
2458
Jose Lima4b6c6692014-08-12 17:41:12 -07002459 stack.setVisibleBehindActivity(visible ? r : null);
2460 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002461 // Make the activity immediately above r opaque.
2462 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2463 if (next != null) {
2464 mService.convertFromTranslucent(next.appToken);
2465 }
2466 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002467 if (top.app != null && top.app.thread != null) {
2468 // Notify the top app of the change.
2469 try {
2470 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2471 } catch (RemoteException e) {
2472 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002473 }
2474 return true;
2475 }
2476
Craig Mautnerbb742462014-07-07 15:28:55 -07002477 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2478 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2479 r.mLaunchTaskBehind = false;
2480 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08002481 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002482 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002483 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002484 mWindowManager.setAppVisibility(r.appToken, false);
2485 }
2486
2487 void scheduleLaunchTaskBehindComplete(IBinder token) {
2488 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2489 }
2490
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002491 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2492 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002493 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2495 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002496 final int topStackNdx = stacks.size() - 1;
2497 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2498 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002499 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002500 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002502 }
2503
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002504 void invalidateTaskLayers() {
2505 mTaskLayersChanged = true;
2506 }
2507
2508 void rankTaskLayersIfNeeded() {
2509 if (!mTaskLayersChanged) {
2510 return;
2511 }
2512 mTaskLayersChanged = false;
2513 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2514 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2515 int baseLayer = 0;
2516 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2517 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2518 }
2519 }
2520 }
2521
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002522 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2523 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2524 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2525 final int topStackNdx = stacks.size() - 1;
2526 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2527 final ActivityStack stack = stacks.get(stackNdx);
2528 stack.clearOtherAppTimeTrackers(except);
2529 }
2530 }
2531 }
2532
Craig Mautner8d341ef2013-03-26 09:03:27 -07002533 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
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 final int numStacks = stacks.size();
2537 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2538 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002539 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002540 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002541 }
2542 }
2543
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002544 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2545 // Examine all activities currently running in the process.
2546 TaskRecord firstTask = null;
2547 // Tasks is non-null only if two or more tasks are found.
2548 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002549 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2550 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002551 ActivityRecord r = app.activities.get(i);
2552 // First, if we find an activity that is in the process of being destroyed,
2553 // then we just aren't going to do anything for now; we want things to settle
2554 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002555 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002556 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002557 return;
2558 }
2559 // Don't consider any activies that are currently not in a state where they
2560 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002561 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2562 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002563 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002564 continue;
2565 }
2566 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002567 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002568 + " from " + r);
2569 if (firstTask == null) {
2570 firstTask = r.task;
2571 } else if (firstTask != r.task) {
2572 if (tasks == null) {
2573 tasks = new ArraySet<>();
2574 tasks.add(firstTask);
2575 }
2576 tasks.add(r.task);
2577 }
2578 }
2579 }
2580 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002581 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002582 return;
2583 }
2584 // If we have activities in multiple tasks that are in a position to be destroyed,
2585 // let's iterate through the tasks and release the oldest one.
2586 final int numDisplays = mActivityDisplays.size();
2587 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2588 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2589 // Step through all stacks starting from behind, to hit the oldest things first.
2590 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2591 final ActivityStack stack = stacks.get(stackNdx);
2592 // Try to release activities in this stack; if we manage to, we are done.
2593 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2594 return;
2595 }
2596 }
2597 }
2598 }
2599
Amith Yamasani37a40c22015-06-17 13:25:42 -07002600 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002601 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002602 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002603 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002604
Craig Mautner858d8a62013-04-23 17:08:34 -07002605 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002606 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2607 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002608 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002609 final ActivityStack stack = stacks.get(stackNdx);
2610 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002611 TaskRecord task = stack.topTask();
2612 if (task != null) {
2613 mWindowManager.moveTaskToTop(task.taskId);
2614 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002615 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002616 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002617
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002618 ActivityStack stack = getStack(restoreStackId);
2619 if (stack == null) {
2620 stack = mHomeStack;
2621 }
2622 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002623 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07002624 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002625 } else {
2626 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08002627 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08002628 }
Craig Mautner93529a42013-10-04 15:03:13 -07002629 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002630 }
2631
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002632 /**
2633 * Add background users to send boot completed events to.
2634 * @param userId The user being started in the background
2635 * @param uss The state object for the user.
2636 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07002637 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002638 mStartingBackgroundUsers.add(uss);
2639 }
2640
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002641 /** Checks whether the userid is a profile of the current user. */
2642 boolean isCurrentProfileLocked(int userId) {
2643 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002644 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002645 }
2646
Chong Zhang45c25ce2015-08-10 22:18:26 -07002647 /** Checks whether the activity should be shown for current user. */
2648 boolean okToShowLocked(ActivityRecord r) {
2649 return r != null && (isCurrentProfileLocked(r.userId)
2650 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
2651 }
2652
Craig Mautnerde4ef022013-04-07 19:01:33 -07002653 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002654 ArrayList<ActivityRecord> stops = null;
2655
2656 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08002657 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2658 ActivityRecord s = mStoppingActivities.get(activityNdx);
2659 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002660 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08002661 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
2662 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002663 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002664 if (s.finishing) {
2665 // If this activity is finishing, it is sitting on top of
2666 // everyone else but we now know it is no longer needed...
2667 // so get rid of it. Otherwise, we need to go through the
2668 // normal flow and hide it once we determine that it is
2669 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002670 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002671 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002672 }
2673 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002674 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002675 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002676 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002677 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002678 }
2679 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08002680 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002681 }
2682 }
2683
2684 return stops;
2685 }
2686
Craig Mautnercf910b02013-04-23 11:23:27 -07002687 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002688 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2689 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2690 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2691 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002692 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002693 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002694 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002695 if (r == null) Slog.e(TAG,
2696 "validateTop...: null top activity, stack=" + stack);
2697 else {
2698 final ActivityRecord pausing = stack.mPausingActivity;
2699 if (pausing != null && pausing == r) Slog.e(TAG,
2700 "validateTop...: top stack has pausing activity r=" + r
2701 + " state=" + state);
2702 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
2703 "validateTop...: activity in front not resumed r=" + r
2704 + " state=" + state);
2705 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002706 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002707 final ActivityRecord resumed = stack.mResumedActivity;
2708 if (resumed != null && resumed == r) Slog.e(TAG,
2709 "validateTop...: back stack has resumed activity r=" + r
2710 + " state=" + state);
2711 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
2712 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002713 }
2714 }
2715 }
Craig Mautner76ea2242013-05-15 11:40:05 -07002716 }
2717
Craig Mautnere0570202015-05-13 13:06:11 -07002718 private String lockTaskModeToString() {
2719 switch (mLockTaskModeState) {
2720 case LOCK_TASK_MODE_LOCKED:
2721 return "LOCKED";
2722 case LOCK_TASK_MODE_PINNED:
2723 return "PINNED";
2724 case LOCK_TASK_MODE_NONE:
2725 return "NONE";
2726 default: return "unknown=" + mLockTaskModeState;
2727 }
2728 }
2729
Craig Mautner27084302013-03-25 08:05:25 -07002730 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002731 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002732 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002733 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2734 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2735 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002736 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07002737 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
2738 final SparseArray<String[]> packages = mService.mLockTaskPackages;
2739 if (packages.size() > 0) {
2740 pw.println(" mLockTaskPackages (userId:packages)=");
2741 for (int i = 0; i < packages.size(); ++i) {
2742 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
2743 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
2744 }
2745 }
2746 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07002747 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748
Craig Mautner20e72272013-04-01 13:45:53 -07002749 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002750 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002751 }
2752
Dianne Hackborn390517b2013-05-30 15:03:32 -07002753 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2754 boolean needSep, String prefix) {
2755 if (activity != null) {
2756 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2757 if (needSep) {
2758 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002759 }
2760 pw.print(prefix);
2761 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002762 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002763 }
2764 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002765 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002766 }
2767
Craig Mautner8d341ef2013-03-26 09:03:27 -07002768 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2769 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002770 boolean printed = false;
2771 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002772 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2773 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002774 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07002775 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002776 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07002777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002778 final ActivityStack stack = stacks.get(stackNdx);
2779 StringBuilder stackHeader = new StringBuilder(128);
2780 stackHeader.append(" Stack #");
2781 stackHeader.append(stack.mStackId);
2782 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002783 stackHeader.append("\n");
2784 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
2785 stackHeader.append("\n");
2786 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2788 needSep, stackHeader.toString());
2789 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2790 !dumpAll, false, dumpPackage, true,
2791 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792
Craig Mautner4a1cb222013-12-04 16:14:06 -08002793 needSep = printed;
2794 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2795 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002796 if (pr) {
2797 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002798 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002799 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002800 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2801 " mResumedActivity: ");
2802 if (pr) {
2803 printed = true;
2804 needSep = false;
2805 }
2806 if (dumpAll) {
2807 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2808 " mLastPausedActivity: ");
2809 if (pr) {
2810 printed = true;
2811 needSep = true;
2812 }
2813 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2814 needSep, " mLastNoHistoryActivity: ");
2815 }
2816 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002817 }
2818 }
2819
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002820 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2821 false, dumpPackage, true, " Activities waiting to finish:", null);
2822 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2823 false, dumpPackage, true, " Activities waiting to stop:", null);
2824 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2825 false, dumpPackage, true, " Activities waiting for another to become visible:",
2826 null);
2827 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2828 false, dumpPackage, true, " Activities waiting to sleep:", null);
2829 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2830 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002831
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002832 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 }
2834
Dianne Hackborn390517b2013-05-30 15:03:32 -07002835 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002836 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002837 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002838 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002839 String innerPrefix = null;
2840 String[] args = null;
2841 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002842 for (int i=list.size()-1; i>=0; i--) {
2843 final ActivityRecord r = list.get(i);
2844 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2845 continue;
2846 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002847 if (innerPrefix == null) {
2848 innerPrefix = prefix + " ";
2849 args = new String[0];
2850 }
2851 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002852 final boolean full = !brief && (complete || !r.isInHistory());
2853 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002854 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002855 needNL = false;
2856 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002857 if (header1 != null) {
2858 pw.println(header1);
2859 header1 = null;
2860 }
2861 if (header2 != null) {
2862 pw.println(header2);
2863 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002864 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002865 if (lastTask != r.task) {
2866 lastTask = r.task;
2867 pw.print(prefix);
2868 pw.print(full ? "* " : " ");
2869 pw.println(lastTask);
2870 if (full) {
2871 lastTask.dump(pw, prefix + " ");
2872 } else if (complete) {
2873 // Complete + brief == give a summary. Isn't that obvious?!?
2874 if (lastTask.intent != null) {
2875 pw.print(prefix); pw.print(" ");
2876 pw.println(lastTask.intent.toInsecureStringWithClip());
2877 }
2878 }
2879 }
2880 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2881 pw.print(" #"); pw.print(i); pw.print(": ");
2882 pw.println(r);
2883 if (full) {
2884 r.dump(pw, innerPrefix);
2885 } else if (complete) {
2886 // Complete + brief == give a summary. Isn't that obvious?!?
2887 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2888 if (r.app != null) {
2889 pw.print(innerPrefix); pw.println(r.app);
2890 }
2891 }
2892 if (client && r.app != null && r.app.thread != null) {
2893 // flush anything that is already in the PrintWriter since the thread is going
2894 // to write to the file descriptor directly
2895 pw.flush();
2896 try {
2897 TransferPipe tp = new TransferPipe();
2898 try {
2899 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2900 r.appToken, innerPrefix, args);
2901 // Short timeout, since blocking here can
2902 // deadlock with the application.
2903 tp.go(fd, 2000);
2904 } finally {
2905 tp.kill();
2906 }
2907 } catch (IOException e) {
2908 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2909 } catch (RemoteException e) {
2910 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2911 }
2912 needNL = true;
2913 }
2914 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002915 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002916 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002917
Craig Mautnerf3333272013-04-22 10:55:53 -07002918 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002919 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
2920 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002921 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2922 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002923 }
2924
2925 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002926 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002927 }
2928
2929 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
2931 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002932 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2933 }
2934
Craig Mautner05d29032013-05-03 13:40:13 -07002935 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002936 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2937 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2938 }
Craig Mautner05d29032013-05-03 13:40:13 -07002939 }
2940
Craig Mautner0eea92c2013-05-16 13:35:39 -07002941 void removeSleepTimeouts() {
2942 mSleepTimeout = false;
2943 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2944 }
2945
2946 final void scheduleSleepTimeout() {
2947 removeSleepTimeouts();
2948 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2949 }
2950
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 @Override
2952 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002953 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2955 }
2956
2957 @Override
2958 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002959 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002960 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2961 }
2962
2963 @Override
2964 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07002965 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2967 }
2968
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002969 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002970 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002971 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002972 newDisplay = mActivityDisplays.get(displayId) == null;
2973 if (newDisplay) {
2974 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07002975 if (activityDisplay.mDisplay == null) {
2976 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
2977 return;
2978 }
Craig Mautner4504de52013-12-20 09:06:56 -08002979 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002980 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08002981 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002982 }
Craig Mautner4504de52013-12-20 09:06:56 -08002983 if (newDisplay) {
2984 mWindowManager.onDisplayAdded(displayId);
2985 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002986 }
2987
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002988 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
2989 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
2990 return;
2991 }
2992 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
2993 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
2994 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
2995 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
2996 }
2997
Wale Ogunwalef16a2812015-04-01 11:23:15 -07002998 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002999 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003000 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3001 if (activityDisplay != null) {
3002 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003004 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003006 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003007 }
3008 }
3009 mWindowManager.onDisplayRemoved(displayId);
3010 }
3011
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003012 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003013 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3015 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 // TODO: Update the bounds.
3017 }
3018 }
3019 mWindowManager.onDisplayChanged(displayId);
3020 }
3021
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003022 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003023 StackInfo info = new StackInfo();
3024 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3025 info.displayId = Display.DEFAULT_DISPLAY;
3026 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003027 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003028
3029 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3030 final int numTasks = tasks.size();
3031 int[] taskIds = new int[numTasks];
3032 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003033 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003034 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003035 for (int i = 0; i < numTasks; ++i) {
3036 final TaskRecord task = tasks.get(i);
3037 taskIds[i] = task.taskId;
3038 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3039 : task.realActivity != null ? task.realActivity.flattenToString()
3040 : task.getTopActivity() != null ? task.getTopActivity().packageName
3041 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003042 taskBounds[i] = new Rect();
3043 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003044 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 }
3046 info.taskIds = taskIds;
3047 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003048 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003049 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 return info;
3051 }
3052
3053 StackInfo getStackInfoLocked(int stackId) {
3054 ActivityStack stack = getStack(stackId);
3055 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003056 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003057 }
3058 return null;
3059 }
3060
3061 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003062 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003063 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3064 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003066 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003067 }
3068 }
3069 return list;
3070 }
3071
Craig Mautner15df08a2015-04-01 12:17:18 -07003072 TaskRecord getLockedTaskLocked() {
3073 final int top = mLockTaskModeTasks.size() - 1;
3074 if (top >= 0) {
3075 return mLockTaskModeTasks.get(top);
3076 }
3077 return null;
3078 }
3079
3080 boolean isLockedTask(TaskRecord task) {
3081 return mLockTaskModeTasks.contains(task);
3082 }
3083
3084 boolean isLastLockedTask(TaskRecord task) {
3085 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3086 }
3087
3088 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003089 if (!mLockTaskModeTasks.remove(task)) {
3090 return;
3091 }
3092 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3093 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003094 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003095 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3096 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003097 final Message lockTaskMsg = Message.obtain();
3098 lockTaskMsg.arg1 = task.userId;
3099 lockTaskMsg.what = LOCK_TASK_END_MSG;
3100 mHandler.sendMessage(lockTaskMsg);
3101 }
3102 }
3103
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003104 void showNonResizeableDockToast(int taskId) {
Chong Zhangc806d902015-11-30 09:44:27 -08003105 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003106 }
3107
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003108 void showLockTaskToast() {
3109 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003110 }
3111
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003112 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3113 if (mLockTaskModeTasks.contains(task)) {
3114 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3115 }
3116 }
3117
Craig Mautner432f64e2015-05-20 14:59:57 -07003118 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3119 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003120 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003121 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003122 final TaskRecord lockedTask = getLockedTaskLocked();
3123 if (lockedTask != null) {
3124 removeLockedTaskLocked(lockedTask);
3125 if (!mLockTaskModeTasks.isEmpty()) {
3126 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003127 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3128 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003129 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003130 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003131 return;
3132 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003133 }
Craig Mautnere0570202015-05-13 13:06:11 -07003134 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3135 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003136 return;
3137 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003138
3139 // Should have already been checked, but do it again.
3140 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003141 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3142 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003143 return;
3144 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003145 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003146 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003147 return;
3148 }
3149
3150 if (mLockTaskModeTasks.isEmpty()) {
3151 // First locktask.
3152 final Message lockTaskMsg = Message.obtain();
3153 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3154 lockTaskMsg.arg1 = task.userId;
3155 lockTaskMsg.what = LOCK_TASK_START_MSG;
3156 lockTaskMsg.arg2 = lockTaskModeState;
3157 mHandler.sendMessage(lockTaskMsg);
3158 }
3159 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003160 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3161 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003162 mLockTaskModeTasks.remove(task);
3163 mLockTaskModeTasks.add(task);
3164
3165 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003166 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003167 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003168
3169 if (andResume) {
3170 findTaskToMoveToFrontLocked(task, 0, null, reason);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003171 resumeFocusedStackTopActivityLocked();
Craig Mautner432f64e2015-05-20 14:59:57 -07003172 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003173 }
3174
3175 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003176 return isLockTaskModeViolation(task, false);
3177 }
3178
3179 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3180 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003181 return false;
3182 }
3183 final int lockTaskAuth = task.mLockTaskAuth;
3184 switch (lockTaskAuth) {
3185 case LOCK_TASK_AUTH_DONT_LOCK:
3186 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003187 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003188 case LOCK_TASK_AUTH_LAUNCHABLE:
3189 case LOCK_TASK_AUTH_WHITELISTED:
3190 return false;
3191 case LOCK_TASK_AUTH_PINNABLE:
3192 // Pinnable tasks can't be launched on top of locktask tasks.
3193 return !mLockTaskModeTasks.isEmpty();
3194 default:
3195 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3196 return true;
3197 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003198 }
3199
Craig Mautner15df08a2015-04-01 12:17:18 -07003200 void onLockTaskPackagesUpdatedLocked() {
3201 boolean didSomething = false;
3202 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3203 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003204 final boolean wasWhitelisted =
3205 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3206 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003207 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003208 final boolean isWhitelisted =
3209 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3210 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3211 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003212 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003213 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3214 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003215 removeLockedTaskLocked(lockedTask);
3216 lockedTask.performClearTaskLocked();
3217 didSomething = true;
3218 }
3219 }
3220 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3221 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3222 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3223 final ActivityStack stack = stacks.get(stackNdx);
3224 stack.onLockTaskPackagesUpdatedLocked();
3225 }
3226 }
Craig Mautnere0570202015-05-13 13:06:11 -07003227 final ActivityRecord r = topRunningActivityLocked();
3228 final TaskRecord task = r != null ? r.task : null;
3229 if (mLockTaskModeTasks.isEmpty() && task != null
3230 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3231 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003232 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3233 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3234 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3235 false);
3236 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003237 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003238 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003239 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003240 }
3241 }
3242
Benjamin Franz43261142015-02-11 15:59:44 +00003243 int getLockTaskModeState() {
3244 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003245 }
3246
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003247 void logStackState() {
3248 mActivityMetricsLogger.logWindowState();
3249 }
3250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003251 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003252
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003253 public ActivityStackSupervisorHandler(Looper looper) {
3254 super(looper);
3255 }
3256
Craig Mautnerf3333272013-04-22 10:55:53 -07003257 void activityIdleInternal(ActivityRecord r) {
3258 synchronized (mService) {
3259 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3260 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003261 }
3262
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003263 @Override
3264 public void handleMessage(Message msg) {
3265 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003266 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003267 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3268 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003269 if (mService.mDidDexOpt) {
3270 mService.mDidDexOpt = false;
3271 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3272 nmsg.obj = msg.obj;
3273 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3274 return;
3275 }
3276 // We don't at this point know if the activity is fullscreen,
3277 // so we need to be conservative and assume it isn't.
3278 activityIdleInternal((ActivityRecord)msg.obj);
3279 } break;
3280 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003281 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003282 activityIdleInternal((ActivityRecord)msg.obj);
3283 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003284 case RESUME_TOP_ACTIVITY_MSG: {
3285 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003286 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003287 }
3288 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003289 case SLEEP_TIMEOUT_MSG: {
3290 synchronized (mService) {
3291 if (mService.isSleepingOrShuttingDown()) {
3292 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3293 mSleepTimeout = true;
3294 checkReadyForSleepLocked();
3295 }
3296 }
3297 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003298 case LAUNCH_TIMEOUT_MSG: {
3299 if (mService.mDidDexOpt) {
3300 mService.mDidDexOpt = false;
3301 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3302 return;
3303 }
3304 synchronized (mService) {
3305 if (mLaunchingActivity.isHeld()) {
3306 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3307 if (VALIDATE_WAKE_LOCK_CALLER
3308 && Binder.getCallingUid() != Process.myUid()) {
3309 throw new IllegalStateException("Calling must be system uid");
3310 }
3311 mLaunchingActivity.release();
3312 }
3313 }
3314 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003316 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003317 } break;
3318 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003319 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 } break;
3321 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003322 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003323 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003324 case CONTAINER_CALLBACK_VISIBILITY: {
3325 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003326 final IActivityContainerCallback callback = container.mCallback;
3327 if (callback != null) {
3328 try {
3329 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3330 } catch (RemoteException e) {
3331 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003332 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003333 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003334 case LOCK_TASK_START_MSG: {
3335 // When lock task starts, we disable the status bars.
3336 try {
Jason Monk62515be2014-05-21 16:06:19 -04003337 if (mLockTaskNotify == null) {
3338 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003339 }
Jason Monk62515be2014-05-21 16:06:19 -04003340 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003341 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003342 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003343 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003344 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003345 flags = StatusBarManager.DISABLE_MASK
3346 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003347 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003348 flags = StatusBarManager.DISABLE_MASK
3349 & (~StatusBarManager.DISABLE_BACK)
3350 & (~StatusBarManager.DISABLE_HOME)
3351 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003352 }
3353 getStatusBarService().disable(flags, mToken,
3354 mService.mContext.getPackageName());
3355 }
3356 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003357 if (getDevicePolicyManager() != null) {
3358 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003359 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003360 }
justinzhang5286d3f2014-05-12 17:06:01 -04003361 } catch (RemoteException ex) {
3362 throw new RuntimeException(ex);
3363 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003364 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003365 case LOCK_TASK_END_MSG: {
3366 // When lock task ends, we enable the status bars.
3367 try {
Jason Monk62515be2014-05-21 16:06:19 -04003368 if (getStatusBarService() != null) {
3369 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3370 mService.mContext.getPackageName());
3371 }
3372 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003373 if (getDevicePolicyManager() != null) {
3374 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3375 msg.arg1);
3376 }
Jason Monk62515be2014-05-21 16:06:19 -04003377 if (mLockTaskNotify == null) {
3378 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3379 }
3380 mLockTaskNotify.show(false);
3381 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003382 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003383 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003384 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003385 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003386 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003387 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003388 new LockPatternUtils(mService.mContext)
3389 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003390 }
3391 } catch (SettingNotFoundException e) {
3392 // No setting, don't lock.
3393 }
justinzhang5286d3f2014-05-12 17:06:01 -04003394 } catch (RemoteException ex) {
3395 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003396 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003397 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003398 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003399 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003400 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3401 if (mLockTaskNotify == null) {
3402 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3403 }
3404 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3405 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003406 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3407 final ActivityContainer container = (ActivityContainer) msg.obj;
3408 final IActivityContainerCallback callback = container.mCallback;
3409 if (callback != null) {
3410 try {
3411 callback.onAllActivitiesComplete(container.asBinder());
3412 } catch (RemoteException e) {
3413 }
3414 }
3415 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003416 case LAUNCH_TASK_BEHIND_COMPLETE: {
3417 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003418 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003419 if (r != null) {
3420 handleLaunchTaskBehindCompleteLocked(r);
3421 }
3422 }
3423 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003424
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003425 }
3426 }
3427 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003428
Ying Wangb081a592014-04-22 15:20:16 -07003429 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003430 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3431 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003432 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003433 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003434 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003435 ActivityRecord mParentActivity = null;
3436 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003437
Craig Mautnere3a00d72014-04-16 08:31:19 -07003438 boolean mVisible = true;
3439
Craig Mautner4a1cb222013-12-04 16:14:06 -08003440 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003441 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003443 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3444 final static int CONTAINER_STATE_NO_SURFACE = 1;
3445 final static int CONTAINER_STATE_FINISHING = 2;
3446 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3447
3448 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003449 synchronized (mService) {
3450 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003451 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003452 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003453 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003455 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003456
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003457 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003458 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003459 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003460 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003461 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003462 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003463 }
3464
3465 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003466 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003468 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3469 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 return;
3471 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003472 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003473 }
3474 }
3475
3476 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003477 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003478 synchronized (mService) {
3479 if (mActivityDisplay != null) {
3480 return mActivityDisplay.mDisplayId;
3481 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003482 }
3483 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003484 }
3485
Jeff Brownca9bc702014-02-11 14:32:56 -08003486 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003487 public int getStackId() {
3488 synchronized (mService) {
3489 return mStackId;
3490 }
3491 }
3492
3493 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003494 public boolean injectEvent(InputEvent event) {
3495 final long origId = Binder.clearCallingIdentity();
3496 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003497 synchronized (mService) {
3498 if (mActivityDisplay != null) {
3499 return mInputManagerInternal.injectInputEvent(event,
3500 mActivityDisplay.mDisplayId,
3501 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3502 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003503 }
3504 return false;
3505 } finally {
3506 Binder.restoreCallingIdentity(origId);
3507 }
3508 }
3509
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003510 @Override
3511 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003512 synchronized (mService) {
3513 if (mContainerState == CONTAINER_STATE_FINISHING) {
3514 return;
3515 }
3516 mContainerState = CONTAINER_STATE_FINISHING;
3517
Craig Mautnerd163e752014-06-13 17:18:47 -07003518 long origId = Binder.clearCallingIdentity();
3519 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003520 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003521 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003522 } finally {
3523 Binder.restoreCallingIdentity(origId);
3524 }
3525 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003526 }
3527
Craig Mautner60257702014-09-17 15:02:33 -07003528 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003529 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08003530 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 if (mActivityDisplay != null) {
3532 mActivityDisplay.detachActivitiesLocked(mStack);
3533 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003534 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 }
3536 }
3537
3538 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003539 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003540 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003541 }
3542
3543 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07003544 public final int startActivityIntentSender(IIntentSender intentSender)
3545 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003546 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3547
3548 if (!(intentSender instanceof PendingIntentRecord)) {
3549 throw new IllegalArgumentException("Bad PendingIntent object");
3550 }
3551
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07003552 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003553 Binder.getCallingUid(), mCurrentUser, false,
3554 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08003555
3556 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3557 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
3558 pendingIntent.key.requestResolvedType);
3559
3560 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
3561 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
3562 }
3563
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003564 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07003565 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003566 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003567 throw new SecurityException(
3568 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3569 }
3570 }
3571
Craig Mautnerdf88d732014-01-27 09:21:32 -08003572 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003573 public IBinder asBinder() {
3574 return this;
3575 }
3576
Craig Mautner4504de52013-12-20 09:06:56 -08003577 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003578 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003579 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003580 }
3581
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 ActivityStackSupervisor getOuter() {
3583 return ActivityStackSupervisor.this;
3584 }
3585
Craig Mautnerd163e752014-06-13 17:18:47 -07003586 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003587 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003588 }
3589
Craig Mautner6985bad2014-04-21 15:22:06 -07003590 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003591 void setVisible(boolean visible) {
3592 if (mVisible != visible) {
3593 mVisible = visible;
3594 if (mCallback != null) {
3595 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3596 0 /* unused */, this).sendToTarget();
3597 }
3598 }
3599 }
3600
Craig Mautner6985bad2014-04-21 15:22:06 -07003601 void setDrawn() {
3602 }
3603
Craig Mautner1b4bf852014-05-26 15:06:32 -07003604 // You can always start a new task on a regular ActivityStack.
3605 boolean isEligibleForNewTasks() {
3606 return true;
3607 }
3608
Craig Mautnerd163e752014-06-13 17:18:47 -07003609 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07003610 detachLocked();
3611 deleteActivityContainer(this);
3612 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003613 }
3614
Craig Mautner34b73df2014-01-12 21:11:08 -08003615 @Override
3616 public String toString() {
3617 return mIdString + (mActivityDisplay == null ? "N" : "A");
3618 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003619 }
3620
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003621 private class VirtualActivityContainer extends ActivityContainer {
3622 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003623 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003624
3625 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3626 super(getNextStackId());
3627 mParentActivity = parent;
3628 mCallback = callback;
3629 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003630 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003631 }
3632
3633 @Override
3634 public void setSurface(Surface surface, int width, int height, int density) {
3635 super.setSurface(surface, width, height, density);
3636
3637 synchronized (mService) {
3638 final long origId = Binder.clearCallingIdentity();
3639 try {
3640 setSurfaceLocked(surface, width, height, density);
3641 } finally {
3642 Binder.restoreCallingIdentity(origId);
3643 }
3644 }
3645 }
3646
3647 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3648 if (mContainerState == CONTAINER_STATE_FINISHING) {
3649 return;
3650 }
3651 VirtualActivityDisplay virtualActivityDisplay =
3652 (VirtualActivityDisplay) mActivityDisplay;
3653 if (virtualActivityDisplay == null) {
3654 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003655 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003656 mActivityDisplay = virtualActivityDisplay;
3657 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003658 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003659 }
3660
3661 if (mSurface != null) {
3662 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003663 }
3664
Craig Mautner6985bad2014-04-21 15:22:06 -07003665 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003666 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003667 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003668 } else {
3669 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003670 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003671 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003672 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003673 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003674 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003675
Craig Mautnerd163e752014-06-13 17:18:47 -07003676 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003677
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003678 if (DEBUG_STACK) Slog.d(TAG_STACK,
3679 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003680 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003681
Craig Mautner6985bad2014-04-21 15:22:06 -07003682 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003683 boolean isAttachedLocked() {
3684 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003685 }
3686
3687 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003688 void setDrawn() {
3689 synchronized (mService) {
3690 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003691 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003692 }
3693 }
3694
Craig Mautner1b4bf852014-05-26 15:06:32 -07003695 // Never start a new task on an ActivityView if it isn't explicitly specified.
3696 @Override
3697 boolean isEligibleForNewTasks() {
3698 return false;
3699 }
3700
Craig Mautnerd163e752014-06-13 17:18:47 -07003701 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003702 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003703 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3704 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3705 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3706 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3707 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003708 }
3709 }
3710
Craig Mautner4a1cb222013-12-04 16:14:06 -08003711 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3712 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003713 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003715 int mDisplayId;
3716 Display mDisplay;
3717 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003718
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3720 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08003721 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003722
Jose Lima4b6c6692014-08-12 17:41:12 -07003723 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003724
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003725 ActivityDisplay() {
3726 }
Craig Mautner4504de52013-12-20 09:06:56 -08003727
Craig Mautner1a70a162014-09-13 12:09:31 -07003728 // After instantiation, check that mDisplay is not null before using this. The alternative
3729 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003730 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003731 final Display display = mDisplayManager.getDisplay(displayId);
3732 if (display == null) {
3733 return;
3734 }
3735 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003736 }
3737
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003738 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003739 mDisplay = display;
3740 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003741 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003742 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003744 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003745 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003746 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
3747 + " onTop=" + onTop);
3748 if (onTop) {
3749 mStacks.add(stack);
3750 } else {
3751 mStacks.add(0, stack);
3752 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003753 }
3754
Craig Mautnere0a38842013-12-16 16:14:02 -08003755 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003756 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003757 + " from displayId=" + mDisplayId);
3758 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 }
3760
Jose Lima4b6c6692014-08-12 17:41:12 -07003761 void setVisibleBehindActivity(ActivityRecord r) {
3762 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003763 }
3764
Jose Lima4b6c6692014-08-12 17:41:12 -07003765 boolean hasVisibleBehindActivity() {
3766 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003767 }
3768
Craig Mautner34b73df2014-01-12 21:11:08 -08003769 @Override
3770 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003771 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3772 }
3773 }
3774
3775 class VirtualActivityDisplay extends ActivityDisplay {
3776 VirtualDisplay mVirtualDisplay;
3777
Craig Mautner6985bad2014-04-21 15:22:06 -07003778 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003779 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003780 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3781 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3782 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3783 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003784
3785 init(mVirtualDisplay.getDisplay());
3786
3787 mWindowManager.handleDisplayAdded(mDisplayId);
3788 }
3789
3790 void setSurface(Surface surface) {
3791 if (mVirtualDisplay != null) {
3792 mVirtualDisplay.setSurface(surface);
3793 }
3794 }
3795
3796 @Override
3797 void detachActivitiesLocked(ActivityStack stack) {
3798 super.detachActivitiesLocked(stack);
3799 if (mVirtualDisplay != null) {
3800 mVirtualDisplay.release();
3801 mVirtualDisplay = null;
3802 }
3803 }
3804
3805 @Override
3806 public String toString() {
3807 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003808 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003809 }
Jose Lima58e66d62014-05-27 20:00:27 -07003810
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003811 /**
3812 * Adjust bounds to stay within stack bounds.
3813 *
3814 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
3815 * that keep them unchanged, but be contained within the stack bounds.
3816 *
3817 * @param bounds Bounds to be adjusted.
3818 * @param stackBounds Bounds within which the other bounds should remain.
3819 */
3820 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
3821 if (stackBounds == null || stackBounds.contains(bounds)) {
3822 return;
3823 }
3824
3825 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
3826 final int maxRight = stackBounds.right
3827 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
3828 int horizontalDiff = stackBounds.left - bounds.left;
3829 if ((horizontalDiff < 0 && bounds.left >= maxRight)
3830 || (bounds.left + horizontalDiff >= maxRight)) {
3831 horizontalDiff = maxRight - bounds.left;
3832 }
3833 bounds.left += horizontalDiff;
3834 bounds.right += horizontalDiff;
3835 }
3836
3837 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
3838 final int maxBottom = stackBounds.bottom
3839 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
3840 int verticalDiff = stackBounds.top - bounds.top;
3841 if ((verticalDiff < 0 && bounds.top >= maxBottom)
3842 || (bounds.top + verticalDiff >= maxBottom)) {
3843 verticalDiff = maxBottom - bounds.top;
3844 }
3845 bounds.top += verticalDiff;
3846 bounds.bottom += verticalDiff;
3847 }
3848 }
3849
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08003850 ActivityStack findStackBehind(ActivityStack stack) {
3851 // TODO(multi-display): We are only looking for stacks on the default display.
3852 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
3853 if (display == null) {
3854 return null;
3855 }
3856 final ArrayList<ActivityStack> stacks = display.mStacks;
3857 for (int i = stacks.size() - 1; i >= 0; i--) {
3858 if (stacks.get(i) == stack && i > 0) {
3859 return stacks.get(i - 1);
3860 }
3861 }
3862 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
3863 + " in=" + stacks);
3864 }
Craig Mautner27084302013-03-25 08:05:25 -07003865}