blob: 33244cd370c6be461e573e1f07a2e4a5aa33a3bb [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Svetoslav7008b512015-06-24 18:47:07 -070019import android.Manifest;
Tony Mak853304c2016-04-18 15:17:41 +010020import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -070021import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070022import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080023import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080025import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070026import android.app.ActivityOptions;
27import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070028import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080029import android.app.IActivityContainer;
30import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070031import android.app.IActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -080032import android.app.IActivityManager.WaitResult;
Jeff Hao1b012d32014-08-20 10:35:34 -070033import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -070034import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040035import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040036import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070037import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070038import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070040import android.content.Intent;
41import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070043import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.content.pm.PackageManager;
45import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010046import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080048import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080049import android.hardware.display.DisplayManager;
50import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080051import android.hardware.display.DisplayManagerGlobal;
52import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080053import android.hardware.input.InputManager;
54import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010056import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070057import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070058import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070061import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070063import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070064import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070065import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040066import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070068import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070069import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070070import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +010071import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070072import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070073import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040074import android.provider.Settings;
75import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070076import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070077import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070078import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070079import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080081import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080083import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080084import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080085import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080086import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -080087
Dianne Hackborn85d558c2014-11-04 10:31:54 -080088import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070089import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040090import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -070091import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -040092import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -080093import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -070094import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070095import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import java.io.FileDescriptor;
98import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -070099import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700100import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700101import java.util.Arrays;
Amith Yamasanie8222e52016-04-08 15:28:47 -0700102import java.util.Collections;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700103import java.util.List;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800104import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700105import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700106
Jorim Jaggife89d122015-12-22 16:28:44 +0100107import static android.Manifest.permission.START_ANY_ACTIVITY;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100108import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100109import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
110import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
111import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
112import static android.app.ActivityManager.RESIZE_MODE_FORCED;
113import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
114import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
115import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
116import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
117import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
118import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
119import static android.app.ActivityManager.StackId.HOME_STACK_ID;
120import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
121import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
122import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
123import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
124import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
125import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
141import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
152import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800154import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100155import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700156import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100157import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100158import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
159import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
162import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
163import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
164import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
165import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
166import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
167import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
168import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700169import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Tony Mak853304c2016-04-18 15:17:41 +0100170import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Winsond46b7272016-04-20 11:54:27 -0700171import static com.android.server.am.ActivityStack.STACK_VISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100172import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
173import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100177import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100178
Craig Mautner4a1cb222013-12-04 16:14:06 -0800179public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800180 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700183 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800190 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800192
Craig Mautnerf3333272013-04-22 10:55:53 -0700193 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700195
Craig Mautner0eea92c2013-05-16 13:35:39 -0700196 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700199 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201
Craig Mautner05d29032013-05-03 13:40:13 -0700202 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
203 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
204 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700205 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800207 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
208 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
209 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700210 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400211 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
212 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700213 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700214 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700215 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800216 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
217 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800218
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700220
Jason Monk62515be2014-05-21 16:06:19 -0400221 private static final String LOCK_TASK_TAG = "Lock-to-App";
222
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223 // Used to indicate if an object (e.g. stack) that we are trying to get
224 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800225 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700227 // Used to indicate that windows of activities should be preserved during the resize.
228 static final boolean PRESERVE_WINDOWS = true;
229
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230 // Used to indicate if an object (e.g. task) should be moved/created
231 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700232 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233
234 // Used to indicate that an objects (e.g. task) removal from its container
235 // (e.g. stack) is due to it moving to another container.
236 static final boolean MOVING = true;
237
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700238 // Force the focus to change to the stack we are moving a task to..
239 static final boolean FORCE_FOCUS = true;
240
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700241 // Restore task from the saved recents if it can't be found in any live stack.
242 static final boolean RESTORE_FROM_RECENTS = true;
243
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700244 // Don't execute any calls to resume.
245 static final boolean DEFER_RESUME = true;
246
Svetoslav7008b512015-06-24 18:47:07 -0700247 // Activity actions an app cannot start if it uses a permission which is not granted.
248 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
249 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700250
Svetoslav7008b512015-06-24 18:47:07 -0700251 static {
252 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
253 Manifest.permission.CAMERA);
254 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
255 Manifest.permission.CAMERA);
256 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
257 Manifest.permission.CALL_PHONE);
258 }
259
Svet Ganov99b60432015-06-27 13:15:22 -0700260 /** Action restriction: launching the activity is not restricted. */
261 private static final int ACTIVITY_RESTRICTION_NONE = 0;
262 /** Action restriction: launching the activity is restricted by a permission. */
263 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
264 /** Action restriction: launching the activity is restricted by an app op. */
265 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700266
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700267 // The height/width divide used when fitting a task within a bounds with method
268 // {@link #fitWithinBounds}.
269 // We always want the task to to be visible in the bounds without affecting its size when
270 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
271 // the input bounds right or bottom side minus the width or height divided by this value.
272 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
273
justinzhang5286d3f2014-05-12 17:06:01 -0400274 /** Status Bar Service **/
275 private IBinder mToken = new Binder();
276 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400277 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400278
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700279 // For debugging to make sure the caller when acquiring/releasing our
280 // wake lock is the system process.
281 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800282 /** The number of distinct task ids that can be assigned to the tasks of a single user */
283 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700284
Craig Mautner27084302013-03-25 08:05:25 -0700285 final ActivityManagerService mService;
286
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800287 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800288
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700289 final ActivityStackSupervisorHandler mHandler;
290
291 /** Short cut */
292 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800293 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700294
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700295 /** Counter for next free stack ID to use for dynamic activity stacks. */
296 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700297
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800298 /**
299 * Maps the task identifier that activities are currently being started in to the userId of the
300 * task. Each time a new task is created, the entry for the userId of the task is incremented
301 */
302 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700303
Craig Mautner2420ead2013-04-01 17:13:20 -0700304 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800305 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700306
Craig Mautnere0a38842013-12-16 16:14:02 -0800307 /** The stack containing the launcher app. Assumed to always be attached to
308 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800309 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700310
Craig Mautnere0a38842013-12-16 16:14:02 -0800311 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800312 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700313
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
315 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700318
319 /** List of activities that are waiting for a new activity to become visible before completing
320 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800321 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700322
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700323 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325
326 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800327 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329 /** List of activities that are ready to be stopped, but waiting for the next activity to
330 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800331 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700332
Craig Mautnerf3333272013-04-22 10:55:53 -0700333 /** List of activities that are ready to be finished, but waiting for the previous activity to
334 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800335 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700336
Craig Mautner0eea92c2013-05-16 13:35:39 -0700337 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339
Wale Ogunwale22e25262016-02-01 10:32:02 -0800340 /** List of activities whose multi-window mode changed that we need to report to the
341 * application */
342 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
343
344 /** List of activities whose picture-in-picture mode changed that we need to report to the
345 * application */
346 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
347
Craig Mautnerf3333272013-04-22 10:55:53 -0700348 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700349 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700350
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
352 * is being brought in front of us. */
353 boolean mUserLeaving = false;
354
Craig Mautner0eea92c2013-05-16 13:35:39 -0700355 /** Set when we have taken too long waiting to go to sleep. */
356 boolean mSleepTimeout = false;
357
358 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700359 * We don't want to allow the device to go to sleep while in the process
360 * of launching an activity. This is primarily to allow alarm intent
361 * receivers to launch an activity and get that to run before the device
362 * goes back to sleep.
363 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700364 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700365
366 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367 * Set when the system is going to sleep, until we have
368 * successfully paused the current activity and released our wake lock.
369 * At that point the system is allowed to actually sleep.
370 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700371 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700373 /** Stack id of the front stack when user switched, indexed by userId. */
374 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700375
Craig Mautner4504de52013-12-20 09:06:56 -0800376 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700378 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379
380 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700381 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382
Jeff Brownca9bc702014-02-11 14:32:56 -0800383 InputManagerInternal mInputManagerInternal;
384
Craig Mautner15df08a2015-04-01 12:17:18 -0700385 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
386 * may be finished until there is only one entry left. If this is empty the system is not
387 * in lockTask mode. */
388 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000389 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700390 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
391 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000392 */
393 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400394 /**
395 * Notifies the user when entering/exiting lock-task.
396 */
397 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800398
Wale Ogunwaled046a012015-12-24 13:05:59 -0800399 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800400 boolean inResumeTopActivity;
401
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700402 // 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 -0700403 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700404 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700405
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700406 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
407 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800408 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700409
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700410 // The default minimal size that will be used if the activity doesn't specify its minimal size.
411 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700412 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700413
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700414 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
415 private boolean mTaskLayersChanged = true;
416
Jorim Jaggi275561a2016-02-23 10:11:02 -0500417 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800418
Jorim Jaggidc249c42015-12-15 14:57:31 -0800419 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
420
Wale Ogunwale39381972015-12-17 17:15:29 -0800421 static class FindTaskResult {
422 ActivityRecord r;
423 boolean matchedByRootAffinity;
424 }
425 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
426
Craig Mautneree36c772014-07-16 14:56:05 -0700427 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100428 * Used to keep track whether app visibilities got changed since the last pause. Useful to
429 * determine whether to invoke the task stack change listener after pausing.
430 */
431 boolean mAppVisibilitiesChangedSinceLastPause;
432
433 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100434 * Set of tasks that are in resizing mode during an app transition to fill the "void".
435 */
436 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
437
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700438
439 /**
440 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
441 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
442 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
443 * like the docked stack going empty.
444 */
445 private boolean mAllowDockedStackResize = true;
446
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100447 /**
Tony Mak853304c2016-04-18 15:17:41 +0100448 * Is dock currently minimized.
449 */
450 boolean mIsDockMinimized;
451
452 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700453 * Description of a request to start a new activity, which has been held
454 * due to app switches being disabled.
455 */
456 static class PendingActivityLaunch {
457 final ActivityRecord r;
458 final ActivityRecord sourceRecord;
459 final int startFlags;
460 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700461 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700462
463 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700464 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700465 r = _r;
466 sourceRecord = _sourceRecord;
467 startFlags = _startFlags;
468 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700469 callerApp = _callerApp;
470 }
471
472 void sendErrorResult(String message) {
473 try {
474 if (callerApp.thread != null) {
475 callerApp.thread.scheduleCrash(message);
476 }
477 } catch (RemoteException e) {
478 Slog.e(TAG, "Exception scheduling crash of failed "
479 + "activity launcher sourceRecord=" + sourceRecord, e);
480 }
Craig Mautneree36c772014-07-16 14:56:05 -0700481 }
482 }
483
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800484 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700485 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700486 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800487 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800488 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700489 }
490
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800491 void setRecentTasks(RecentTasks recentTasks) {
492 mRecentTasks = recentTasks;
493 }
494
Jeff Brown2c43c332014-06-12 22:38:59 -0700495 /**
496 * At the time when the constructor runs, the power manager has not yet been
497 * initialized. So we initialize our wakelocks afterwards.
498 */
499 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800500 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700501 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700502 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700503 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700504 }
505
justinzhang5286d3f2014-05-12 17:06:01 -0400506 // This function returns a IStatusBarService. The value is from ServiceManager.
507 // getService and is cached.
508 private IStatusBarService getStatusBarService() {
509 synchronized (mService) {
510 if (mStatusBarService == null) {
511 mStatusBarService = IStatusBarService.Stub.asInterface(
512 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
513 if (mStatusBarService == null) {
514 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
515 }
516 }
517 return mStatusBarService;
518 }
519 }
520
Jason Monk35c62a42014-06-17 10:24:47 -0400521 private IDevicePolicyManager getDevicePolicyManager() {
522 synchronized (mService) {
523 if (mDevicePolicyManager == null) {
524 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
525 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
526 if (mDevicePolicyManager == null) {
527 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
528 }
529 }
530 return mDevicePolicyManager;
531 }
532 }
533
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700534 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800535 synchronized (mService) {
536 mWindowManager = wm;
537
538 mDisplayManager =
539 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
540 mDisplayManager.registerDisplayListener(this, null);
541
542 Display[] displays = mDisplayManager.getDisplays();
543 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
544 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800545 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700546 if (activityDisplay.mDisplay == null) {
547 throw new IllegalStateException("Default Display does not exist");
548 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800549 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700550 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 }
552
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800553 mHomeStack = mFocusedStack = mLastFocusedStack =
554 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800555
556 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800557 }
Craig Mautner27084302013-03-25 08:05:25 -0700558 }
559
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200560 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700561 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200562 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700563 }
564
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700565 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700567 }
568
Craig Mautnerde4ef022013-04-07 19:01:33 -0700569 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700571 }
572
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700573 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700574 if (stack == null) {
575 return false;
576 }
577
Craig Mautnerdf88d732014-01-27 09:21:32 -0800578 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
579 if (parent != null) {
580 stack = parent.task.stack;
581 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800582 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700583 }
584
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700585 /** The top most stack. */
586 boolean isFrontStack(ActivityStack stack) {
587 if (stack == null) {
588 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800589 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700590
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700591 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
592 if (parent != null) {
593 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800594 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700595 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
596 }
597
Wale Ogunwaled046a012015-12-24 13:05:59 -0800598 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800599 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
600 if (!focusCandidate.isFocusable()) {
601 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
602 focusCandidate = focusCandidate.getNextFocusableStackLocked();
603 }
604
605 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800606 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800607 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800608
Wale Ogunwaled046a012015-12-24 13:05:59 -0800609 EventLogTags.writeAmFocusedStack(
610 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
611 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
612 }
613
614 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800615 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800616 // The focus activity should always be the top activity in the focused stack.
617 // There will be chaos and anarchy if it isn't...
618 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
619 }
Craig Mautnerde313752015-01-22 14:28:03 -0800620
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800621 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800622 if (r != null && r.idle) {
623 checkFinishBootingLocked();
624 }
625 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700626 }
627
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700628 void moveHomeStackToFront(String reason) {
629 mHomeStack.moveToFront(reason);
630 }
631
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700632 /** Returns true if the focus activity was adjusted to the home stack top activity. */
633 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700634 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700635 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700636 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700637 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700638
Craig Mautner84984fa2014-06-19 11:19:20 -0700639 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700640
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700641 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700642 if (top == null) {
643 return false;
644 }
645 mService.setFocusedActivityLocked(top, reason);
646 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700647 }
648
Craig Mautner299f9602015-01-26 09:47:33 -0800649 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700650 if (!mService.mBooting && !mService.mBooted) {
651 // Not ready yet!
652 return false;
653 }
654
Craig Mautner84984fa2014-06-19 11:19:20 -0700655 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700656 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700657 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700658 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700659
Craig Mautner84984fa2014-06-19 11:19:20 -0700660 if (prev != null) {
661 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
662 }
663
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700664 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
665 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800666 final String myReason = reason + " resumeHomeStackTask";
667
Mark Lua56ea122015-10-08 13:31:01 +0800668 // Only resume home activity if isn't finishing.
669 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800670 mService.setFocusedActivityLocked(r, myReason);
671 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700672 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800673 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700674 }
675
Craig Mautner8d341ef2013-03-26 09:03:27 -0700676 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700677 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700678 }
679
680 /**
681 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
682 * @param id Id of the task we would like returned.
683 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
684 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700685 * @param stackId The stack to restore the task to (default launch stack will be used if
686 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700687 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700688 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800689 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800690 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800691 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
693 ActivityStack stack = stacks.get(stackNdx);
694 TaskRecord task = stack.taskForIdLocked(id);
695 if (task != null) {
696 return task;
697 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700698 }
699 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800700
701 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800703 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800704 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800706 return null;
707 }
708
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700709 if (!restoreFromRecents) {
710 return task;
711 }
712
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700713 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700714 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
715 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800716 return null;
717 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700718 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800719 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700720 }
721
Craig Mautner6170f732013-04-02 13:05:23 -0700722 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
727 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
728 if (r != null) {
729 return r;
730 }
Craig Mautner6170f732013-04-02 13:05:23 -0700731 }
732 }
733 return null;
734 }
735
Tony Mak853304c2016-04-18 15:17:41 +0100736 /**
737 * TODO: Handle freefom mode.
738 * @return true when credential confirmation is needed for the user and there is any
739 * activity started by the user in any visible stack.
740 */
741 boolean isUserLockedProfile(@UserIdInt int userId) {
742 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
743 return false;
744 }
745 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
746 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
747 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
748 for (final ActivityStack activityStack : activityStacks) {
749 if (activityStack == null) {
750 continue;
751 }
752 if (activityStack.topRunningActivityLocked() == null) {
753 continue;
754 }
755 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
756 continue;
757 }
758 if (activityStack.isDockedStack() && mIsDockMinimized) {
759 continue;
760 }
761 final TaskRecord topTask = activityStack.topTask();
762 if (topTask == null) {
763 continue;
764 }
765 // To handle the case that work app is in the task but just is not the top one.
766 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
767 final ActivityRecord activityRecord = topTask.mActivities.get(i);
768 if (activityRecord.userId == userId) {
769 return true;
770 }
771 }
772 }
773 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000774 }
775
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800776 void setNextTaskIdForUserLocked(int taskId, int userId) {
777 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
778 if (taskId > currentTaskId) {
779 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700780 }
781 }
782
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800783 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800784 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
785 // for a userId u, a taskId can only be in the range
786 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
787 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
788 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800789 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
790 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
791 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800792 candidateTaskId++;
793 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
794 // Wrap around as there will be smaller task ids that are available now.
795 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700796 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800797 if (candidateTaskId == currentTaskId) {
798 // Something wrong!
799 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
800 throw new IllegalStateException("Cannot get an available task id."
801 + " Reached limit of " + MAX_TASK_IDS_PER_USER
802 + " running tasks per user.");
803 }
804 }
805 mCurTaskIdForUser.put(userId, candidateTaskId);
806 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700807 }
808
Craig Mautnerde4ef022013-04-07 19:01:33 -0700809 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800810 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700811 if (stack == null) {
812 return null;
813 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700814 ActivityRecord resumedActivity = stack.mResumedActivity;
815 if (resumedActivity == null || resumedActivity.app == null) {
816 resumedActivity = stack.mPausingActivity;
817 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700818 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700819 }
820 }
821 return resumedActivity;
822 }
823
Dianne Hackbornff072722014-09-24 10:56:28 -0700824 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700825 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800826 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
828 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800829 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
830 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700831 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800832 continue;
833 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700834 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800835 if (hr != null) {
836 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
837 && processName.equals(hr.processName)) {
838 try {
George Mount2c92c972014-03-20 09:38:23 -0700839 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 didSomething = true;
841 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700842 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800843 Slog.w(TAG, "Exception in new application when starting activity "
844 + hr.intent.getComponent().flattenToShortString(), e);
845 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700846 }
Craig Mautner20e72272013-04-01 13:45:53 -0700847 }
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
849 }
850 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700851 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700852 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700853 }
Craig Mautner20e72272013-04-01 13:45:53 -0700854 return didSomething;
855 }
856
857 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800858 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
859 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800860 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
861 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700862 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800863 continue;
864 }
865 final ActivityRecord resumedActivity = stack.mResumedActivity;
866 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700867 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800868 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 return false;
870 }
Craig Mautner20e72272013-04-01 13:45:53 -0700871 }
872 }
873 return true;
874 }
875
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
878 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
880 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700881 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700883 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 return false;
885 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700886 }
887 }
888 }
889 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700890 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800891 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
892 mLastFocusedStack + " to=" + mFocusedStack);
893 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700894 return true;
895 }
896
897 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800898 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
900 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800901 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
902 final ActivityStack stack = stacks.get(stackNdx);
903 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800904 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700905 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800906 return false;
907 }
908 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700910 }
911 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800912 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 }
914
Craig Mautner2acc3892013-09-23 10:28:14 -0700915 /**
916 * Pause all activities in either all of the stacks or just the back stacks.
917 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700918 * @return true if any activity was paused as a result of this call.
919 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700921 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
923 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
925 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700926 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700927 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800928 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700929 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
930 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700932 }
933 }
934 return someActivityPaused;
935 }
936
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700938 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
940 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800941 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
942 final ActivityStack stack = stacks.get(stackNdx);
943 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700944 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700946 Slog.d(TAG_STATES,
947 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800948 pausing = false;
949 } else {
950 return false;
951 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700952 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700953 }
954 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700955 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 }
957
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700958 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
959 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500960 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800961 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
962 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
963 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
964 final ActivityStack stack = stacks.get(stackNdx);
965 if (stack.mResumedActivity != null &&
966 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700967 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800968 }
969 }
970 }
971 }
972
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800973 void cancelInitializingActivities() {
974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
975 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
977 stacks.get(stackNdx).cancelInitializingActivities();
978 }
979 }
980 }
981
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700982 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700983 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700984 }
985
986 void sendWaitingVisibleReportLocked(ActivityRecord r) {
987 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700988 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700989 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700990 if (w.who == null) {
991 changed = true;
992 w.timeout = false;
993 if (r != null) {
994 w.who = new ComponentName(r.info.packageName, r.info.name);
995 }
996 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
997 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700998 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700999 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001000 if (changed) {
1001 mService.notifyAll();
1002 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001003 }
1004
1005 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1006 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001007 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001008 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001009 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001010 if (w.who == null) {
1011 changed = true;
1012 w.timeout = timeout;
1013 if (r != null) {
1014 w.who = new ComponentName(r.info.packageName, r.info.name);
1015 }
1016 w.thisTime = thisTime;
1017 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001018 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001019 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001020 if (changed) {
1021 mService.notifyAll();
1022 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001023 }
1024
Craig Mautner29219d92013-04-16 20:19:12 -07001025 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001026 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001027 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001028 if (r != null) {
1029 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001030 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001031
Craig Mautner4a1cb222013-12-04 16:14:06 -08001032 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001033 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1035 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001036 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001037 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001038 if (r != null) {
1039 return r;
1040 }
1041 }
1042 }
1043 return null;
1044 }
1045
Dianne Hackborn09233282014-04-30 11:33:59 -07001046 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001047 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001048 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1049 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001050 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001051 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001053 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1054 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001055 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001056 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001057 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001058 }
1059 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001060
1061 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1062 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1063 while (maxNum > 0) {
1064 long mostRecentActiveTime = Long.MIN_VALUE;
1065 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001066 final int numTaskLists = runningTaskLists.size();
1067 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1068 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001069 if (!stackTaskList.isEmpty()) {
1070 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1071 if (lastActiveTime > mostRecentActiveTime) {
1072 mostRecentActiveTime = lastActiveTime;
1073 selectedStackList = stackTaskList;
1074 }
1075 }
1076 }
1077 if (selectedStackList != null) {
1078 list.add(selectedStackList.remove(0));
1079 --maxNum;
1080 } else {
1081 break;
1082 }
1083 }
Craig Mautner20e72272013-04-01 13:45:53 -07001084 }
1085
Todd Kennedy7440f172015-12-09 14:31:22 -08001086 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1087 ProfilerInfo profilerInfo) {
1088 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001089 if (aInfo != null) {
1090 // Store the found target back into the intent, because now that
1091 // we have it we never want to do this again. For example, if the
1092 // user navigates back to this point in the history, we should
1093 // always restart the exact same activity.
1094 intent.setComponent(new ComponentName(
1095 aInfo.applicationInfo.packageName, aInfo.name));
1096
1097 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001098 if (!aInfo.processName.equals("system")) {
1099 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhang55c9d732016-04-26 12:24:55 -07001100 final ProcessRecord app = mService.getProcessRecordLocked(
1101 aInfo.processName, aInfo.applicationInfo.uid, true);
1102 // If the process already started, we can't wait for debugger and shouldn't
1103 // modify the debug settings.
1104 // For non-persistent debug, normally we set the debug app here, and restores
1105 // to the original at attachApplication time. However, if the app process
1106 // already exists, we won't get another attachApplication, and the debug setting
1107 // never gets restored. Furthermore, if we get two such calls back-to-back,
1108 // both mOrigDebugApp and mDebugApp will become the same app, and it won't be
1109 // cleared even if we get attachApplication after the app process is killed.
1110 if (app == null || app.thread == null) {
1111 mService.setDebugApp(aInfo.processName, true, false);
1112 } else {
1113 Slog.w(TAG, "Ignoring waitForDebugger because process already exists");
1114 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001116
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001117 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1118 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1119 }
1120
Man Caocfa78b22015-06-11 20:14:34 -07001121 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1122 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1123 }
1124
1125 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001126 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001127 }
1128 }
1129 }
1130 return aInfo;
1131 }
1132
Todd Kennedy7440f172015-12-09 14:31:22 -08001133 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001134 return resolveIntent(intent, resolvedType, userId, 0);
1135 }
1136
1137 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001138 try {
1139 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001140 PackageManager.MATCH_DEFAULT_ONLY | flags
1141 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001142 } catch (RemoteException e) {
1143 }
1144 return null;
1145 }
1146
1147 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1148 ProfilerInfo profilerInfo, int userId) {
1149 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1150 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1151 }
1152
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001153 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1154 boolean andResume, boolean checkConfig) throws RemoteException {
1155
1156 if (!allPausedActivitiesComplete()) {
1157 // While there are activities pausing we skipping starting any new activities until
1158 // pauses are complete. NOTE: that we also do this for activities that are starting in
1159 // the paused state because they will first be resumed then paused on the client side.
1160 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1161 "realStartActivityLocked: Skipping start of r=" + r
1162 + " some activities pausing...");
1163 return false;
1164 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001165
Craig Mautner2568c3a2015-03-26 14:22:34 -07001166 if (andResume) {
1167 r.startFreezingScreenLocked(app, 0);
1168 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001169
Craig Mautner2568c3a2015-03-26 14:22:34 -07001170 // schedule launch ticks to collect information about slow apps.
1171 r.startLaunchTickingLocked();
1172 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001173
1174 // Have the window manager re-evaluate the orientation of
1175 // the screen based on the new activity order. Note that
1176 // as a result of this, it can call back into the activity
1177 // manager with a new orientation. We don't care about that,
1178 // because the activity is not currently running so we are
1179 // just restarting it anyway.
1180 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001181 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001182 mService.mConfiguration,
1183 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001184 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001185 }
1186
1187 r.app = app;
1188 app.waitingToKill = null;
1189 r.launchCount++;
1190 r.lastLaunchTime = SystemClock.uptimeMillis();
1191
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001192 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001193
1194 int idx = app.activities.indexOf(r);
1195 if (idx < 0) {
1196 app.activities.add(r);
1197 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001198 mService.updateLruProcessLocked(app, true, null);
1199 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001200
Craig Mautner15df08a2015-04-01 12:17:18 -07001201 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001202 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1203 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001204 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001205 }
1206
1207 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 try {
1209 if (app.thread == null) {
1210 throw new RemoteException();
1211 }
1212 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001213 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 if (andResume) {
1215 results = r.results;
1216 newIntents = r.newIntents;
1217 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001218 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1219 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1220 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001221 if (andResume) {
1222 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1223 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001224 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001226 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001227 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001228 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001230 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1231 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 r.sleeping = false;
1233 r.forceNewConfig = false;
1234 mService.showAskCompatModeDialogLocked(r);
1235 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1238 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1239 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001240 final String profileFile = mService.mProfileFile;
1241 if (profileFile != null) {
1242 ParcelFileDescriptor profileFd = mService.mProfileFd;
1243 if (profileFd != null) {
1244 try {
1245 profileFd = profileFd.dup();
1246 } catch (IOException e) {
1247 if (profileFd != null) {
1248 try {
1249 profileFd.close();
1250 } catch (IOException o) {
1251 }
1252 profileFd = null;
1253 }
1254 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001256
1257 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1258 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259 }
1260 }
1261 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001262
Craig Mautner2568c3a2015-03-26 14:22:34 -07001263 if (andResume) {
1264 app.hasShownUi = true;
1265 app.pendingUiClean = true;
1266 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001267 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001269 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001270 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001272 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001274 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 // This may be a heavy-weight process! Note that the package
1276 // manager will ensure that only activity can run in the main
1277 // process of the .apk, which is the only thing that will be
1278 // considered heavy-weight.
1279 if (app.processName.equals(app.info.packageName)) {
1280 if (mService.mHeavyWeightProcess != null
1281 && mService.mHeavyWeightProcess != app) {
1282 Slog.w(TAG, "Starting new heavy weight process " + app
1283 + " when already running "
1284 + mService.mHeavyWeightProcess);
1285 }
1286 mService.mHeavyWeightProcess = app;
1287 Message msg = mService.mHandler.obtainMessage(
1288 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1289 msg.obj = r;
1290 mService.mHandler.sendMessage(msg);
1291 }
1292 }
1293
1294 } catch (RemoteException e) {
1295 if (r.launchFailed) {
1296 // This is the second time we failed -- finish activity
1297 // and give up.
1298 Slog.e(TAG, "Second failure launching "
1299 + r.intent.getComponent().flattenToShortString()
1300 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001301 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1303 "2nd-crash", false);
1304 return false;
1305 }
1306
1307 // This is the first time we failed -- restart process and
1308 // retry.
1309 app.activities.remove(r);
1310 throw e;
1311 }
1312
1313 r.launchFailed = false;
1314 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001315 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 }
1317
1318 if (andResume) {
1319 // As part of the process of launching, ActivityThread also performs
1320 // a resume.
1321 stack.minimalResumeActivityLocked(r);
1322 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001323 // This activity is not starting in the resumed state... which should look like we asked
1324 // it to pause+stop (but remain visible), and it has done so and reported back the
1325 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001326 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001327 "Moving to PAUSED: " + r + " (starting in paused state)");
1328 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 }
1330
1331 // Launch the new version setup screen if needed. We do this -after-
1332 // launching the initial activity (that is, home), so that it can have
1333 // a chance to initialize itself while in the background, making the
1334 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001335 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 mService.startSetupActivityLocked();
1337 }
1338
Dianne Hackborn465fa392014-09-14 14:21:18 -07001339 // Update any services we are bound to that might care about whether
1340 // their client may have activities.
1341 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1342
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 return true;
1344 }
1345
Craig Mautnere79d42682013-04-01 19:01:53 -07001346 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001347 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001348 // Is this activity's application already running?
1349 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001350 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001351
1352 r.task.stack.setLaunchTime(r);
1353
1354 if (app != null && app.thread != null) {
1355 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001356 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1357 || !"android".equals(r.info.packageName)) {
1358 // Don't add this if it is a platform component that is marked
1359 // to run in multiple processes, because this is actually
1360 // part of the framework so doesn't make sense to track as a
1361 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001362 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1363 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001364 }
George Mount2c92c972014-03-20 09:38:23 -07001365 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001366 return;
1367 } catch (RemoteException e) {
1368 Slog.w(TAG, "Exception when starting activity "
1369 + r.intent.getComponent().flattenToShortString(), e);
1370 }
1371
1372 // If a dead object exception was thrown -- fall through to
1373 // restart the application.
1374 }
1375
1376 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001377 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001378 }
1379
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001380 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001381 String resultWho, int requestCode, int callingPid, int callingUid,
1382 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001383 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001384 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1385 callingUid);
1386 if (startAnyPerm == PERMISSION_GRANTED) {
1387 return true;
1388 }
1389 final int componentRestriction = getComponentRestrictionForCallingPackage(
1390 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1391 final int actionRestriction = getActionRestrictionForCallingPackage(
1392 intent.getAction(), callingPackage, callingPid, callingUid);
1393 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1394 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1395 if (resultRecord != null) {
1396 resultStack.sendActivityResultLocked(-1,
1397 resultRecord, resultWho, requestCode,
1398 Activity.RESULT_CANCELED, null);
1399 }
1400 final String msg;
1401 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1402 msg = "Permission Denial: starting " + intent.toString()
1403 + " from " + callerApp + " (pid=" + callingPid
1404 + ", uid=" + callingUid + ")" + " with revoked permission "
1405 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1406 } else if (!aInfo.exported) {
1407 msg = "Permission Denial: starting " + intent.toString()
1408 + " from " + callerApp + " (pid=" + callingPid
1409 + ", uid=" + callingUid + ")"
1410 + " not exported from uid " + aInfo.applicationInfo.uid;
1411 } else {
1412 msg = "Permission Denial: starting " + intent.toString()
1413 + " from " + callerApp + " (pid=" + callingPid
1414 + ", uid=" + callingUid + ")"
1415 + " requires " + aInfo.permission;
1416 }
1417 Slog.w(TAG, msg);
1418 throw new SecurityException(msg);
1419 }
1420
1421 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1422 final String message = "Appop Denial: starting " + intent.toString()
1423 + " from " + callerApp + " (pid=" + callingPid
1424 + ", uid=" + callingUid + ")"
1425 + " requires " + AppOpsManager.permissionToOp(
1426 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1427 Slog.w(TAG, message);
1428 return false;
1429 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1430 final String message = "Appop Denial: starting " + intent.toString()
1431 + " from " + callerApp + " (pid=" + callingPid
1432 + ", uid=" + callingUid + ")"
1433 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1434 Slog.w(TAG, message);
1435 return false;
1436 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001437 if (options != null && options.getLaunchTaskId() != -1) {
1438 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1439 callingPid, callingUid);
1440 if (startInTaskPerm != PERMISSION_GRANTED) {
1441 final String msg = "Permission Denial: starting " + intent.toString()
1442 + " from " + callerApp + " (pid=" + callingPid
1443 + ", uid=" + callingUid + ") with launchTaskId="
1444 + options.getLaunchTaskId();
1445 Slog.w(TAG, msg);
1446 throw new SecurityException(msg);
1447 }
1448 }
1449
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001450 return true;
1451 }
1452
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001453 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001454 final long identity = Binder.clearCallingIdentity();
1455 try {
1456 return UserManager.get(mService.mContext).getUserInfo(userId);
1457 } finally {
1458 Binder.restoreCallingIdentity(identity);
1459 }
1460 }
1461
Svet Ganov99b60432015-06-27 13:15:22 -07001462 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001463 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001464 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1465 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001466 == PackageManager.PERMISSION_DENIED) {
1467 return ACTIVITY_RESTRICTION_PERMISSION;
1468 }
1469
Christopher Tateff7add02015-08-17 10:23:22 -07001470 if (activityInfo.permission == null) {
1471 return ACTIVITY_RESTRICTION_NONE;
1472 }
1473
Svet Ganov99b60432015-06-27 13:15:22 -07001474 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1475 if (opCode == AppOpsManager.OP_NONE) {
1476 return ACTIVITY_RESTRICTION_NONE;
1477 }
1478
1479 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1480 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001481 if (!ignoreTargetSecurity) {
1482 return ACTIVITY_RESTRICTION_APPOP;
1483 }
Svet Ganov99b60432015-06-27 13:15:22 -07001484 }
1485
1486 return ACTIVITY_RESTRICTION_NONE;
1487 }
1488
Svetoslav7008b512015-06-24 18:47:07 -07001489 private int getActionRestrictionForCallingPackage(String action,
1490 String callingPackage, int callingPid, int callingUid) {
1491 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001492 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001493 }
1494
1495 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1496 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001497 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001498 }
1499
1500 final PackageInfo packageInfo;
1501 try {
1502 packageInfo = mService.mContext.getPackageManager()
1503 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1504 } catch (PackageManager.NameNotFoundException e) {
1505 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001506 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001507 }
1508
1509 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001510 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001511 }
1512
1513 if (mService.checkPermission(permission, callingPid, callingUid) ==
1514 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001515 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001516 }
1517
1518 final int opCode = AppOpsManager.permissionToOpCode(permission);
1519 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001520 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001521 }
1522
1523 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1524 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001525 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001526 }
1527
Svet Ganov99b60432015-06-27 13:15:22 -07001528 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001529 }
1530
Wale Ogunwaled046a012015-12-24 13:05:59 -08001531 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001532 if (r == null) {
1533 // Not sure what you are trying to do, but it is not going to work...
1534 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001535 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001536 final TaskRecord task = r.task;
1537 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001538 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001539 return false;
1540 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001541 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001542 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001543 }
1544
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001545 void setLaunchSource(int uid) {
1546 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1547 }
1548
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001549 void acquireLaunchWakelock() {
1550 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1551 throw new IllegalStateException("Calling must be system uid");
1552 }
1553 mLaunchingActivity.acquire();
1554 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1555 // To be safe, don't allow the wake lock to be held for too long.
1556 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1557 }
1558 }
1559
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001560 /**
1561 * Called when the frontmost task is idle.
1562 * @return the state of mService.mBooting before this was called.
1563 */
1564 private boolean checkFinishBootingLocked() {
1565 final boolean booting = mService.mBooting;
1566 boolean enableScreen = false;
1567 mService.mBooting = false;
1568 if (!mService.mBooted) {
1569 mService.mBooted = true;
1570 enableScreen = true;
1571 }
1572 if (booting || enableScreen) {
1573 mService.postFinishBooting(booting, enableScreen);
1574 }
1575 return booting;
1576 }
1577
Craig Mautnerf3333272013-04-22 10:55:53 -07001578 // Checked.
1579 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1580 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001581 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001582
Craig Mautnerf3333272013-04-22 10:55:53 -07001583 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001584 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001585 int NS = 0;
1586 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001587 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001588 boolean activityRemoved = false;
1589
Wale Ogunwale7d701172015-03-11 15:36:30 -07001590 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001591 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001592 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1593 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001594 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1595 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001596 if (fromTimeout) {
1597 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001598 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001599
1600 // This is a hack to semi-deal with a race condition
1601 // in the client where it can be constructed with a
1602 // newer configuration from when we asked it to launch.
1603 // We'll update with whatever configuration it now says
1604 // it used to launch.
1605 if (config != null) {
1606 r.configuration = config;
1607 }
1608
1609 // We are now idle. If someone is waiting for a thumbnail from
1610 // us, we can now deliver.
1611 r.idle = true;
1612
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001613 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001614 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001615 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001616 }
1617 }
1618
1619 if (allResumedActivitiesIdle()) {
1620 if (r != null) {
1621 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001622 }
1623
1624 if (mLaunchingActivity.isHeld()) {
1625 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1626 if (VALIDATE_WAKE_LOCK_CALLER &&
1627 Binder.getCallingUid() != Process.myUid()) {
1628 throw new IllegalStateException("Calling must be system uid");
1629 }
1630 mLaunchingActivity.release();
1631 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001632 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001633 }
1634
1635 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001636 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001637 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001638 if ((NF = mFinishingActivities.size()) > 0) {
1639 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001640 mFinishingActivities.clear();
1641 }
1642
Craig Mautnerf3333272013-04-22 10:55:53 -07001643 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001644 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001645 mStartingUsers.clear();
1646 }
1647
Craig Mautnerf3333272013-04-22 10:55:53 -07001648 // Stop any activities that are scheduled to do so but have been
1649 // waiting for the next one to start.
1650 for (int i = 0; i < NS; i++) {
1651 r = stops.get(i);
1652 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001653 if (stack != null) {
1654 if (r.finishing) {
1655 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1656 } else {
1657 stack.stopActivityLocked(r);
1658 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001659 }
1660 }
1661
1662 // Finish any activities that are scheduled to do so but have been
1663 // waiting for the next one to start.
1664 for (int i = 0; i < NF; i++) {
1665 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001666 final ActivityStack stack = r.task.stack;
1667 if (stack != null) {
1668 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1669 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001670 }
1671
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001672 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001673 // Complete user switch
1674 if (startingUsers != null) {
1675 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001676 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001677 }
1678 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001679 }
1680
1681 mService.trimApplications();
1682 //dump();
1683 //mWindowManager.dump();
1684
Craig Mautnerf3333272013-04-22 10:55:53 -07001685 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001686 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001687 }
1688
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001689 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001690 }
1691
Craig Mautner8e569572013-10-11 17:36:59 -07001692 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001693 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1695 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1697 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1698 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001699 }
Craig Mautner19091252013-10-05 00:03:53 -07001700 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001701 }
1702
1703 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1705 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001706 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1707 stacks.get(stackNdx).closeSystemDialogsLocked();
1708 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001709 }
1710 }
1711
Craig Mautner93529a42013-10-04 15:03:13 -07001712 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001713 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001714 }
1715
Craig Mautner8d341ef2013-03-26 09:03:27 -07001716 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001717 * Update the last used stack id for non-current user (current user's last
1718 * used stack is the focused stack)
1719 */
1720 void updateUserStackLocked(int userId, ActivityStack stack) {
1721 if (userId != mCurrentUser) {
1722 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1723 }
1724 }
1725
1726 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001727 * @return true if some activity was finished (or would have finished if doit were true).
1728 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001729 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1730 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001731 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001734 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001735 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001736 if (stack.finishDisabledPackageActivitiesLocked(
1737 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001738 didSomething = true;
1739 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001740 }
1741 }
1742 return didSomething;
1743 }
1744
Dianne Hackborna413dc02013-07-12 12:02:55 -07001745 void updatePreviousProcessLocked(ActivityRecord r) {
1746 // Now that this process has stopped, we may want to consider
1747 // it to be the previous app to try to keep around in case
1748 // the user wants to return to it.
1749
1750 // First, found out what is currently the foreground app, so that
1751 // we don't blow away the previous app if this activity is being
1752 // hosted by the process that is actually still the foreground.
1753 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1755 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1757 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001758 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001759 if (stack.mResumedActivity != null) {
1760 fgApp = stack.mResumedActivity.app;
1761 } else if (stack.mPausingActivity != null) {
1762 fgApp = stack.mPausingActivity.app;
1763 }
1764 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001765 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001766 }
1767 }
1768
1769 // Now set this one as the previous process, only if that really
1770 // makes sense to.
1771 if (r.app != null && fgApp != null && r.app != fgApp
1772 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001773 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001774 mService.mPreviousProcess = r.app;
1775 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1776 }
1777 }
1778
Wale Ogunwaled046a012015-12-24 13:05:59 -08001779 boolean resumeFocusedStackTopActivityLocked() {
1780 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001781 }
1782
Wale Ogunwaled046a012015-12-24 13:05:59 -08001783 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001784 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001785 if (targetStack != null && isFocusedStack(targetStack)) {
1786 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001787 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001788 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1789 if (r == null || r.state != RESUMED) {
1790 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1791 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001792 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001793 }
1794
Todd Kennedy39bfee52016-02-24 10:28:21 -08001795 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1796 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1797 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1798 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1799 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1800 }
1801 }
1802 }
1803
Adrian Roos20d7df32016-01-12 18:59:43 +01001804 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1805 TaskRecord finishedTask = null;
1806 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001809 final int numStacks = stacks.size();
1810 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1811 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001812 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1813 if (stack == focusedStack || finishedTask == null) {
1814 finishedTask = t;
1815 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001816 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001817 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001818 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001819 }
1820
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001821 void finishVoiceTask(IVoiceInteractionSession session) {
1822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1824 final int numStacks = stacks.size();
1825 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1826 final ActivityStack stack = stacks.get(stackNdx);
1827 stack.finishVoiceTask(session);
1828 }
1829 }
1830 }
1831
Andrii Kulianc27916642016-04-12 17:59:27 -07001832 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1833 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001834 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1835 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001836 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001837 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1838 // Caller wants the home activity moved with it. To accomplish this,
1839 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001840 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001841 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001842 if (task.stack == null) {
1843 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1844 + task + " to front. Stack is null");
1845 return;
1846 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001847
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001848 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001849 int stackId = options.getLaunchStackId();
1850 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001851 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001852 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001853 if (stackId == INVALID_STACK_ID) {
1854 stackId = task.getLaunchStackId();
1855 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001856 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001857 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1858 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1859 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001860 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1861 // still need moveTaskToFrontLocked() below for any transition settings.
1862 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001863 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1864 resizeStackLocked(stackId, bounds,
1865 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001866 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001867 } else {
1868 // WM resizeTask must be done after the task is moved to the correct stack,
1869 // because Task's setBounds() also updates dim layer's bounds, but that has
1870 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001871 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001872 false /* relayout */, false /* forced */);
1873 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001874 }
1875 }
1876
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001877 final ActivityRecord r = task.getTopActivity();
1878 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1879 r == null ? null : r.appTimeTracker, reason);
1880
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001881 if (DEBUG_STACK) Slog.d(TAG_STACK,
1882 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001883
Andrii Kulianc27916642016-04-12 17:59:27 -07001884 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1885 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001886 }
1887
Wale Ogunwale854809c2015-12-27 16:18:19 -08001888 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001889 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001890 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001891 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001892 return false;
1893 }
1894 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1895 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001896 }
1897
Craig Mautner967212c2013-04-13 21:10:58 -07001898 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001899 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001900 }
1901
1902 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001903 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1904 if (activityContainer != null) {
1905 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001906 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001907 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001908 return null;
1909 }
1910 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001911 }
1912
Craig Mautner967212c2013-04-13 21:10:58 -07001913 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001914 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001915 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1916 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001917 }
1918 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001919 }
1920
Craig Mautner4a1cb222013-12-04 16:14:06 -08001921 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001922 ActivityRecord homeActivity = getHomeActivity();
1923 if (homeActivity != null) {
1924 return homeActivity.appToken;
1925 }
1926 return null;
1927 }
1928
1929 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001930 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001931 }
1932
1933 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001934 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1935 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1936 final TaskRecord task = tasks.get(taskNdx);
1937 if (task.isHomeTask()) {
1938 final ArrayList<ActivityRecord> activities = task.mActivities;
1939 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1940 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001941 if (r.isHomeActivity()
1942 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001943 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001944 }
1945 }
1946 }
1947 }
1948 return null;
1949 }
1950
Chong Zhangb15758a2015-11-17 12:12:03 -08001951 /**
1952 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1953 * the docked stack itself, or if it's side-by-side to the docked stack.
1954 */
1955 boolean isStackDockedInEffect(int stackId) {
1956 return stackId == DOCKED_STACK_ID ||
1957 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1958 }
1959
Todd Kennedyca4d8422015-01-15 15:19:22 -08001960 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001961 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001962 ActivityContainer activityContainer =
1963 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001964 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001965 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1966 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001967 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001968 return activityContainer;
1969 }
1970
Craig Mautner34b73df2014-01-12 21:11:08 -08001971 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001972 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1973 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1974 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001975 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1976 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001977 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001978 }
1979 }
1980
Craig Mautner95da1082014-02-24 17:54:35 -08001981 void deleteActivityContainer(IActivityContainer container) {
1982 ActivityContainer activityContainer = (ActivityContainer)container;
1983 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001984 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1985 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001986 final int stackId = activityContainer.mStackId;
1987 mActivityContainers.remove(stackId);
1988 mWindowManager.removeStack(stackId);
1989 }
1990 }
1991
Jorim Jaggidc249c42015-12-15 14:57:31 -08001992 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001993 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001994 if (stackId == DOCKED_STACK_ID) {
1995 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1996 preserveWindows);
1997 return;
1998 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001999 final ActivityStack stack = getStack(stackId);
2000 if (stack == null) {
2001 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2002 return;
2003 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002004
Jorim Jaggidc249c42015-12-15 14:57:31 -08002005 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002006 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2007 // stack size changing so things don't get out of sync.
2008 return;
2009 }
2010
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002011 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002012 mWindowManager.deferSurfaceLayout();
2013 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002014 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002015 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002016 stack.ensureVisibleActivitiesConfigurationLocked(
2017 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002018 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002019 } finally {
2020 mWindowManager.continueSurfaceLayout();
2021 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002022 }
2023 }
2024
Jorim Jaggi192086e2016-03-11 17:17:03 +01002025 void deferUpdateBounds(int stackId) {
2026 final ActivityStack stack = getStack(stackId);
2027 if (stack != null) {
2028 stack.deferUpdateBounds();
2029 }
2030 }
2031
2032 void continueUpdateBounds(int stackId) {
2033 final ActivityStack stack = getStack(stackId);
2034 if (stack != null) {
2035 stack.continueUpdateBounds();
2036 }
2037 }
2038
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002039 void notifyAppTransitionDone() {
2040 continueUpdateBounds(HOME_STACK_ID);
2041 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2042 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
2043 if (anyTaskForIdLocked(taskId) != null) {
2044 mWindowManager.setTaskDockedResizing(taskId, false);
2045 }
2046 }
2047 mResizingTasksDuringAnimation.clear();
2048 }
2049
Jorim Jaggi192086e2016-03-11 17:17:03 +01002050 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002051 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002052 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002053
Jorim Jaggi192086e2016-03-11 17:17:03 +01002054 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2055 return;
2056 }
2057
Jorim Jaggidc249c42015-12-15 14:57:31 -08002058 mTmpBounds.clear();
2059 mTmpConfigs.clear();
2060 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002061 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002062 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2063 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002064 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002065 final TaskRecord task = tasks.get(i);
2066 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002067 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2068 // For freeform stack we don't adjust the size of the tasks to match that
2069 // of the stack, but we do try to make sure the tasks are still contained
2070 // with the bounds of the stack.
2071 tempRect2.set(task.mBounds);
2072 fitWithinBounds(tempRect2, bounds);
2073 task.updateOverrideConfiguration(tempRect2);
2074 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002075 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002076 }
2077 }
2078
2079 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2080 mTmpBounds.put(task.taskId, task.mBounds);
2081 if (tempTaskInsetBounds != null) {
2082 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2083 }
2084 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002085
2086 // We might trigger a configuration change. Save the current task bounds for freezing.
2087 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002088 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2089 mTmpBounds, mTmpInsetBounds);
2090 stack.setBounds(bounds);
2091 }
2092
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002093 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2094 final ActivityStack stack = getStack(fromStackId);
2095 if (stack == null) {
2096 return;
2097 }
2098
2099 mWindowManager.deferSurfaceLayout();
2100 try {
2101 if (fromStackId == DOCKED_STACK_ID) {
2102
2103 // We are moving all tasks from the docked stack to the fullscreen stack,
2104 // which is dismissing the docked stack, so resize all other stacks to
2105 // fullscreen here already so we don't end up with resize trashing.
2106 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2107 if (StackId.isResizeableByDockedStack(i)) {
2108 ActivityStack otherStack = getStack(i);
2109 if (otherStack != null) {
2110 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2111 true /* allowResizeInDockedMode */, DEFER_RESUME);
2112 }
2113 }
2114 }
2115
2116 // Also disable docked stack resizing since we have manually adjusted the
2117 // size of other stacks above and we don't want to trigger a docked stack
2118 // resize when we remove task from it below and it is detached from the
2119 // display because it no longer contains any tasks.
2120 mAllowDockedStackResize = false;
2121 }
2122 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2123 final int size = tasks.size();
2124 if (onTop) {
2125 for (int i = 0; i < size; i++) {
2126 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002127 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002128 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002129 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002130
2131 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2132 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002133 } else {
2134 for (int i = size - 1; i >= 0; i--) {
2135 positionTaskInStackLocked(tasks.get(i).taskId,
2136 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2137 }
2138 }
2139 } finally {
2140 mAllowDockedStackResize = true;
2141 mWindowManager.continueSurfaceLayout();
2142 }
2143 }
2144
Jorim Jaggidc249c42015-12-15 14:57:31 -08002145 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2146 Rect tempDockedTaskInsetBounds,
2147 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002148
2149 if (!mAllowDockedStackResize) {
2150 // Docked stack resize currently disabled.
2151 return;
2152 }
2153
Jorim Jaggidc249c42015-12-15 14:57:31 -08002154 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2155 if (stack == null) {
2156 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2157 return;
2158 }
2159
2160 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2161 mWindowManager.deferSurfaceLayout();
2162 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002163 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2164 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002165 ActivityRecord r = stack.topRunningActivityLocked();
2166 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2167 tempDockedTaskInsetBounds);
2168
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002169 // TODO: Checking for isAttached might not be needed as if the user passes in null
2170 // dockedBounds then they want the docked stack to be dismissed.
2171 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2172 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002173 // In this case we make all other static stacks fullscreen and move all
2174 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002175 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002176
2177 // stack shouldn't contain anymore activities, so nothing to resume.
2178 r = null;
2179 } else {
2180 // Docked stacks occupy a dedicated region on screen so the size of all other
2181 // static stacks need to be adjusted so they don't overlap with the docked stack.
2182 // We get the bounds to use from window manager which has been adjusted for any
2183 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002184 mWindowManager.getStackDockedModeBounds(
2185 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002187 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2188 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2189 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002190 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002191 }
2192 }
2193 }
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002194 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002195 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002196 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002197 mWindowManager.continueSurfaceLayout();
2198 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2199 }
2200
2201 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2202 tempDockedTaskBounds != null
2203 || tempDockedTaskInsetBounds != null
2204 || tempOtherTaskBounds != null
2205 || tempOtherTaskInsetBounds != null);
2206 }
2207
Robert Carr0d00c2e2016-02-29 17:45:02 -08002208 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2209 final ActivityStack stack = getStack(PINNED_STACK_ID);
2210 if (stack == null) {
2211 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2212 return;
2213 }
2214 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2215 mWindowManager.deferSurfaceLayout();
2216 try {
2217 ActivityRecord r = stack.topRunningActivityLocked();
2218 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2219 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002220 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002221 } finally {
2222 mWindowManager.continueSurfaceLayout();
2223 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2224 }
2225 }
2226
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002227 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2228 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002229 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002230 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002231 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002232 }
2233
Chong Zhang87b21722015-09-21 15:39:51 -07002234 // If this is a forced resize, let it go through even if the bounds is not changing,
2235 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002236 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002237 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002238 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002239 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002240 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002241 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002242
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002243 if (!mWindowManager.isValidTaskId(task.taskId)) {
2244 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002245 // All we can do for now is update the bounds so it can be used when the task is
2246 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002247 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002248 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2249 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002250 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002251 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002252 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002253 }
2254
Chong Zhang6de2ae82015-09-30 18:25:21 -07002255 // Do not move the task to another stack here.
2256 // This method assumes that the task is already placed in the right stack.
2257 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002258
Chong Zhang6de2ae82015-09-30 18:25:21 -07002259 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002260
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002261 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002262 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002263 // way and the activity was kept the way it was. If it's false, it means the activity had
2264 // to be relaunched due to configuration change.
2265 boolean kept = true;
2266 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002267 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002268 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002269 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002270 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002271
2272 if (!deferResume) {
2273
2274 // All other activities must be made visible with their correct configuration.
2275 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2276 if (!kept) {
2277 resumeFocusedStackTopActivityLocked();
2278 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002279 }
2280 }
2281 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002282 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002283
2284 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002285 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002286 }
2287
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002288 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002289 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2290 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002291 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002292 }
2293
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002294 ActivityContainer activityContainer = new ActivityContainer(stackId);
2295 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002296 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002297 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002298 }
2299
2300 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002301 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002302 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2303 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002304 break;
2305 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002306 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002307 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002308 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002309 }
2310
Chong Zhang5dcb2752015-08-18 13:50:26 -07002311 /**
2312 * Restores a recent task to a stack
2313 * @param task The recent task to be restored.
2314 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002315 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002316 * @return true if the task has been restored successfully.
2317 */
2318 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2319 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002320 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002321 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2322 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2323 // Put it in the fullscreen stack.
2324 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002325 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002326
Wale Ogunwale706ed792015-08-02 10:29:44 -07002327 if (task.stack != null) {
2328 // Task has already been restored once. See if we need to do anything more
2329 if (task.stack.mStackId == stackId) {
2330 // Nothing else to do since it is already restored in the right stack.
2331 return true;
2332 }
2333 // Remove current stack association, so we can re-associate the task with the
2334 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002335 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002336 }
2337
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002338 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002339 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002340
2341 if (stack == null) {
2342 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002343 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2344 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002345 return false;
2346 }
2347
Wale Ogunwale5f986092015-12-04 15:35:38 -08002348 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002349 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2350 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002351 final ArrayList<ActivityRecord> activities = task.mActivities;
2352 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002353 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002354 }
2355 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002356 }
2357
Wale Ogunwale040b4702015-08-06 18:10:50 -07002358 /**
2359 * Moves the specified task record to the input stack id.
2360 * WARNING: This method performs an unchecked/raw move of the task and
2361 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002362 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002363 * @param task Task to move.
2364 * @param stackId Id of stack to move task to.
2365 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002366 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002367 * @param reason Reason the task is been moved.
2368 * @return The stack the task was moved to.
2369 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002370 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002371 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002372
2373 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2374 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2375 + "support multi-window task=" + task + " to stackId=" + stackId);
2376 }
2377
Wale Ogunwale06579d62016-04-30 15:29:06 -07002378 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002379 final ActivityStack prevStack = task.stack;
2380 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002381 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002382 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2383 // Whenever we are moving the top activity from the front stack we want to make sure to move
2384 // the stack to the front.
2385 final boolean wasFront = isFrontStack(prevStack)
2386 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002387
Chong Zhangd545dce2016-02-29 18:09:17 -08002388 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002389 // We don't allow moving a unresizeable task to the docked stack since the docked
2390 // stack is used for split-screen mode and will cause things like the docked divider to
2391 // show up. We instead leave the task in its current stack or move it to the fullscreen
2392 // stack if it isn't currently in a stack.
2393 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002394 Slog.w(TAG, "Can not move unresizeable task=" + task
2395 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2396 }
2397
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002398 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2399 // if a docked stack is created below which will lead to the stack we are moving from and
2400 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002401 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002402 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002403 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002404 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002405 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002406
2407 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002408 // move focus to the new stack by moving the stack to the front.
2409 stack.moveToFrontAndResumeStateIfNeeded(
2410 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002411
Wale Ogunwale040b4702015-08-06 18:10:50 -07002412 return stack;
2413 }
2414
Chong Zhange4fbd322016-03-01 14:44:03 -08002415 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002416 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002417 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2418 false /* deferResume */);
2419 }
2420
2421 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2422 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002423 final TaskRecord task = anyTaskForIdLocked(taskId);
2424 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002425 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002426 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002427 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002428
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002429 if (task.stack != null && task.stack.mStackId == stackId) {
2430 // You are already in the right stack silly...
2431 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002432 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002433 }
2434
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002435 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2436 throw new IllegalArgumentException("moveTaskToStack:"
2437 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2438 }
2439
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002440 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002441 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002442 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002443 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002444 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002445 // We are about to relaunch the activity because its configuration changed due to
2446 // being maximized, i.e. size change. The activity will first remove the old window
2447 // and then add a new one. This call will tell window manager about this, so it can
2448 // preserve the old window until the new one is drawn. This prevents having a gap
2449 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002450 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002451 // Note here we always set the replacing window first, as the flags might be needed
2452 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002453 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002454 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002455
2456 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002457 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002458 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002459 try {
2460 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002461 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002462 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002463
Wale Ogunwale961f4852016-02-01 20:25:54 -08002464 if (!animate) {
2465 stack.mNoAnimActivities.add(topActivity);
2466 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002467
Wale Ogunwale961f4852016-02-01 20:25:54 -08002468 // We might trigger a configuration change. Save the current task bounds for freezing.
2469 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2470
2471 // Make sure the task has the appropriate bounds/size for the stack it is in.
2472 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002473 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2474 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002475 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2476 Rect bounds = task.getLaunchBounds();
2477 if (bounds == null) {
2478 stack.layoutTaskInStack(task, null);
2479 bounds = task.mBounds;
2480 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002481 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2482 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002483 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002484 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2485 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002486 }
2487 } finally {
2488 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002489 }
2490
2491 if (mightReplaceWindow) {
2492 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2493 // window), we need to clear the replace window settings. Otherwise, we schedule a
2494 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002495 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002496 }
2497
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002498 if (!deferResume) {
2499
2500 // The task might have already been running and its visibility needs to be synchronized with
2501 // the visibility of the stack / windows.
2502 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2503 resumeFocusedStackTopActivityLocked();
2504 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002505
Jorim Jaggid53f0922016-04-06 22:16:23 -07002506 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002507
2508 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002509 }
2510
Wale Ogunwale079a0042015-10-24 11:44:07 -07002511 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2512 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2513 if (stack == null) {
2514 throw new IllegalArgumentException(
2515 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2516 }
2517
2518 final ActivityRecord r = stack.topRunningActivityLocked();
2519 if (r == null) {
2520 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2521 + " in stack=" + stack);
2522 return false;
2523 }
2524
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002525 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002526 Slog.w(TAG,
2527 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002528 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002529 return false;
2530 }
2531
Wale Ogunwale480dca02016-02-06 13:58:29 -08002532 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002533 return true;
2534 }
2535
Wale Ogunwale480dca02016-02-06 13:58:29 -08002536 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2537 mWindowManager.deferSurfaceLayout();
2538 try {
2539 final TaskRecord task = r.task;
2540
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002541 if (r == task.stack.getVisibleBehindActivity()) {
2542 // An activity can't be pinned and visible behind at the same time. Go ahead and
2543 // release it from been visible behind before pinning.
2544 requestVisibleBehindLocked(r, false);
2545 }
2546
Wale Ogunwale480dca02016-02-06 13:58:29 -08002547 // Need to make sure the pinned stack exist so we can resize it below...
2548 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2549
2550 // Resize the pinned stack to match the current size of the task the activity we are
2551 // going to be moving is currently contained in. We do this to have the right starting
2552 // animation bounds for the pinned stack to the desired bounds the caller wants.
2553 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2554 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002555 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002556
2557 if (task.mActivities.size() == 1) {
2558 // There is only one activity in the task. So, we can just move the task over to
2559 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002560 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2561 // Move the home stack forward if the task we just moved to the pinned stack
2562 // was launched from home so home should be visible behind it.
2563 moveHomeStackToFront(reason);
2564 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002565 moveTaskToStackLocked(
2566 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2567 } else {
2568 stack.moveActivityToStack(r);
2569 }
2570 } finally {
2571 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002572 }
2573
Wale Ogunwale480dca02016-02-06 13:58:29 -08002574 // The task might have already been running and its visibility needs to be synchronized
2575 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002576 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002577 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002578
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002579 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002580 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002581 }
2582
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002583 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2584 final TaskRecord task = anyTaskForIdLocked(taskId);
2585 if (task == null) {
2586 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2587 return;
2588 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002589 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2590
2591 task.updateOverrideConfigurationForStack(stack);
2592
2593 mWindowManager.positionTaskInStack(
2594 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002595 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002596 // The task might have already been running and its visibility needs to be synchronized with
2597 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002598 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002599 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002600 }
2601
Craig Mautnerac6f8432013-07-17 13:24:59 -07002602 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002603 mTmpFindTaskResult.r = null;
2604 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002605 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
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) {
2609 final ActivityStack stack = stacks.get(stackNdx);
2610 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002611 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002612 continue;
2613 }
2614 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002615 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2616 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002617 continue;
2618 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002619 stack.findTaskLocked(r, mTmpFindTaskResult);
2620 // It is possible to have task in multiple stacks with the same root affinity.
2621 // If the match we found was based on root affinity we keep on looking to see if
2622 // there is a better match in another stack. We eventually return the match based
2623 // on root affinity if we don't find a better match.
2624 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2625 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002626 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002627 }
2628 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002629 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2630 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002631 }
2632
2633 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2637 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2638 if (ar != null) {
2639 return ar;
2640 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002641 }
2642 }
2643 return null;
2644 }
2645
Craig Mautner8d341ef2013-03-26 09:03:27 -07002646 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002647 scheduleSleepTimeout();
2648 if (!mGoingToSleep.isHeld()) {
2649 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002650 if (mLaunchingActivity.isHeld()) {
2651 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2652 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002653 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002654 mLaunchingActivity.release();
2655 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002657 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002658 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 }
2660
2661 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002662 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002663
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002664 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002665 final long endTime = System.currentTimeMillis() + timeout;
2666 while (true) {
2667 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2671 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2672 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 }
2674 if (cantShutdown) {
2675 long timeRemaining = endTime - System.currentTimeMillis();
2676 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 } catch (InterruptedException e) {
2680 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002681 } else {
2682 Slog.w(TAG, "Activity manager shutdown timed out");
2683 timedout = true;
2684 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002685 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002686 } else {
2687 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 }
2689 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002690
2691 // Force checkReadyForSleep to complete.
2692 mSleepTimeout = true;
2693 checkReadyForSleepLocked();
2694
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 return timedout;
2696 }
2697
2698 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 removeSleepTimeouts();
2700 if (mGoingToSleep.isHeld()) {
2701 mGoingToSleep.release();
2702 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2706 final ActivityStack stack = stacks.get(stackNdx);
2707 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002708 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002709 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002710 }
Craig Mautner5314a402013-09-26 12:40:16 -07002711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002713 mGoingToSleepActivities.clear();
2714 }
2715
2716 void activitySleptLocked(ActivityRecord r) {
2717 mGoingToSleepActivities.remove(r);
2718 checkReadyForSleepLocked();
2719 }
2720
2721 void checkReadyForSleepLocked() {
2722 if (!mService.isSleepingOrShuttingDown()) {
2723 // Do not care.
2724 return;
2725 }
2726
2727 if (!mSleepTimeout) {
2728 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2733 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 }
2735
2736 if (mStoppingActivities.size() > 0) {
2737 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002738 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002739 + mStoppingActivities.size() + " activities");
2740 scheduleIdleLocked();
2741 dontSleep = true;
2742 }
2743
2744 if (mGoingToSleepActivities.size() > 0) {
2745 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002746 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 + mGoingToSleepActivities.size() + " activities");
2748 dontSleep = true;
2749 }
2750
2751 if (dontSleep) {
2752 return;
2753 }
2754 }
2755
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2757 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2759 stacks.get(stackNdx).goToSleep();
2760 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002761 }
2762
2763 removeSleepTimeouts();
2764
2765 if (mGoingToSleep.isHeld()) {
2766 mGoingToSleep.release();
2767 }
2768 if (mService.mShuttingDown) {
2769 mService.notifyAll();
2770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 }
2772
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002773 boolean reportResumedActivityLocked(ActivityRecord r) {
2774 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002775 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002776 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002777 }
2778 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002779 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002780 mWindowManager.executeAppTransition();
2781 return true;
2782 }
2783 return false;
2784 }
2785
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002789 int stackNdx = stacks.size() - 1;
2790 while (stackNdx >= 0) {
2791 stacks.get(stackNdx).handleAppCrashLocked(app);
2792 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002793 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002794 }
2795 }
2796
Jose Lima4b6c6692014-08-12 17:41:12 -07002797 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002798 final ActivityStack stack = r.task.stack;
2799 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002800 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2801 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002802 return false;
2803 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002804
2805 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2806 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2807 + " visible=" + visible + " stackId=" + stack.mStackId
2808 + " can't contain visible behind activities");
2809 return false;
2810 }
2811
Jose Lima4b6c6692014-08-12 17:41:12 -07002812 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002813 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2814 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002815
2816 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002817 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002818 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002819 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002820 return true;
2821 }
2822
2823 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002824 if (visible && top.fullscreen) {
2825 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2827 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2828 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2829 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002830 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002831 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2832 // Only the activity set as currently visible behind should actively reset its
2833 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002834 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2835 "requestVisibleBehind: returning visible=" + visible
2836 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2837 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002838 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002839 }
2840
Jose Lima4b6c6692014-08-12 17:41:12 -07002841 stack.setVisibleBehindActivity(visible ? r : null);
2842 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002843 // If there is a translucent home activity, we need to force it stop being translucent,
2844 // because we can't depend on the application to necessarily perform that operation.
2845 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002846 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002847 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002848 mService.convertFromTranslucent(next.appToken);
2849 }
2850 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002851 if (top.app != null && top.app.thread != null) {
2852 // Notify the top app of the change.
2853 try {
2854 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2855 } catch (RemoteException e) {
2856 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002857 }
2858 return true;
2859 }
2860
Craig Mautnerbb742462014-07-07 15:28:55 -07002861 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2862 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002863 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002864 final ActivityStack stack = task.stack;
2865
2866 r.mLaunchTaskBehind = false;
2867 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002868 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002869 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002870 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002871
2872 // When launching tasks behind, update the last active time of the top task after the new
2873 // task has been shown briefly
2874 final ActivityRecord top = stack.topActivity();
2875 if (top != null) {
2876 top.task.touchActiveTime();
2877 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002878 }
2879
2880 void scheduleLaunchTaskBehindComplete(IBinder token) {
2881 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2882 }
2883
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002884 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2885 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002886 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2888 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002889 final int topStackNdx = stacks.size() - 1;
2890 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2891 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002892 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002893 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002895 }
2896
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002897 void invalidateTaskLayers() {
2898 mTaskLayersChanged = true;
2899 }
2900
2901 void rankTaskLayersIfNeeded() {
2902 if (!mTaskLayersChanged) {
2903 return;
2904 }
2905 mTaskLayersChanged = false;
2906 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2907 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2908 int baseLayer = 0;
2909 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2910 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2911 }
2912 }
2913 }
2914
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002915 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2917 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2918 final int topStackNdx = stacks.size() - 1;
2919 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2920 final ActivityStack stack = stacks.get(stackNdx);
2921 stack.clearOtherAppTimeTrackers(except);
2922 }
2923 }
2924 }
2925
Craig Mautner8d341ef2013-03-26 09:03:27 -07002926 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 final int numStacks = stacks.size();
2930 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2931 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002932 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002934 }
2935 }
2936
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002937 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2938 // Examine all activities currently running in the process.
2939 TaskRecord firstTask = null;
2940 // Tasks is non-null only if two or more tasks are found.
2941 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002942 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2943 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002944 ActivityRecord r = app.activities.get(i);
2945 // First, if we find an activity that is in the process of being destroyed,
2946 // then we just aren't going to do anything for now; we want things to settle
2947 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002948 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002949 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002950 return;
2951 }
2952 // Don't consider any activies that are currently not in a state where they
2953 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002954 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2955 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002956 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002957 continue;
2958 }
2959 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002960 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002961 + " from " + r);
2962 if (firstTask == null) {
2963 firstTask = r.task;
2964 } else if (firstTask != r.task) {
2965 if (tasks == null) {
2966 tasks = new ArraySet<>();
2967 tasks.add(firstTask);
2968 }
2969 tasks.add(r.task);
2970 }
2971 }
2972 }
2973 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002974 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002975 return;
2976 }
2977 // If we have activities in multiple tasks that are in a position to be destroyed,
2978 // let's iterate through the tasks and release the oldest one.
2979 final int numDisplays = mActivityDisplays.size();
2980 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2981 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2982 // Step through all stacks starting from behind, to hit the oldest things first.
2983 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2984 final ActivityStack stack = stacks.get(stackNdx);
2985 // Try to release activities in this stack; if we manage to, we are done.
2986 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2987 return;
2988 }
2989 }
2990 }
2991 }
2992
Amith Yamasani37a40c22015-06-17 13:25:42 -07002993 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07002994 final int focusStackId = mFocusedStack.getStackId();
2995 // We dismiss the docked stack whenever we switch users.
2996 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
2997
2998 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002999 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003000 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003001
Craig Mautner858d8a62013-04-23 17:08:34 -07003002 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3004 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003005 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003006 final ActivityStack stack = stacks.get(stackNdx);
3007 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003008 TaskRecord task = stack.topTask();
3009 if (task != null) {
3010 mWindowManager.moveTaskToTop(task.taskId);
3011 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003012 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003013 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003014
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003015 ActivityStack stack = getStack(restoreStackId);
3016 if (stack == null) {
3017 stack = mHomeStack;
3018 }
3019 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003020 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003021 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003022 } else {
3023 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003024 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003025 }
Craig Mautner93529a42013-10-04 15:03:13 -07003026 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003027 }
3028
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003029 /** Checks whether the userid is a profile of the current user. */
3030 boolean isCurrentProfileLocked(int userId) {
3031 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003032 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003033 }
3034
Chong Zhang45c25ce2015-08-10 22:18:26 -07003035 /** Checks whether the activity should be shown for current user. */
3036 boolean okToShowLocked(ActivityRecord r) {
3037 return r != null && (isCurrentProfileLocked(r.userId)
3038 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3039 }
3040
Craig Mautnerde4ef022013-04-07 19:01:33 -07003041 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003042 ArrayList<ActivityRecord> stops = null;
3043
3044 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003045 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3046 ActivityRecord s = mStoppingActivities.get(activityNdx);
3047 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003048 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003049 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3050 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003051 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003052 if (s.finishing) {
3053 // If this activity is finishing, it is sitting on top of
3054 // everyone else but we now know it is no longer needed...
3055 // so get rid of it. Otherwise, we need to go through the
3056 // normal flow and hide it once we determine that it is
3057 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003058 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003059 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003060 }
3061 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003062 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003063 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003065 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003066 }
3067 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003068 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003069 }
3070 }
3071
3072 return stops;
3073 }
3074
Craig Mautnercf910b02013-04-23 11:23:27 -07003075 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003076 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3077 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3078 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3079 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003080 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003081 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003082 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003083 if (r == null) Slog.e(TAG,
3084 "validateTop...: null top activity, stack=" + stack);
3085 else {
3086 final ActivityRecord pausing = stack.mPausingActivity;
3087 if (pausing != null && pausing == r) Slog.e(TAG,
3088 "validateTop...: top stack has pausing activity r=" + r
3089 + " state=" + state);
3090 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3091 "validateTop...: activity in front not resumed r=" + r
3092 + " state=" + state);
3093 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003094 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003095 final ActivityRecord resumed = stack.mResumedActivity;
3096 if (resumed != null && resumed == r) Slog.e(TAG,
3097 "validateTop...: back stack has resumed activity r=" + r
3098 + " state=" + state);
3099 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3100 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003101 }
3102 }
3103 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003104 }
3105
Craig Mautnere0570202015-05-13 13:06:11 -07003106 private String lockTaskModeToString() {
3107 switch (mLockTaskModeState) {
3108 case LOCK_TASK_MODE_LOCKED:
3109 return "LOCKED";
3110 case LOCK_TASK_MODE_PINNED:
3111 return "PINNED";
3112 case LOCK_TASK_MODE_NONE:
3113 return "NONE";
3114 default: return "unknown=" + mLockTaskModeState;
3115 }
3116 }
3117
Craig Mautner27084302013-03-25 08:05:25 -07003118 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003119 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003120 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003121 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003122 pw.print(prefix);
3123 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003124 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003125 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003126 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3127 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3128 if (packages.size() > 0) {
3129 pw.println(" mLockTaskPackages (userId:packages)=");
3130 for (int i = 0; i < packages.size(); ++i) {
3131 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3132 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3133 }
3134 }
3135 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137
Craig Mautner20e72272013-04-01 13:45:53 -07003138 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003139 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003140 }
3141
Dianne Hackborn390517b2013-05-30 15:03:32 -07003142 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3143 boolean needSep, String prefix) {
3144 if (activity != null) {
3145 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3146 if (needSep) {
3147 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003148 }
3149 pw.print(prefix);
3150 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003151 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003152 }
3153 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003154 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003155 }
3156
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3158 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003159 boolean printed = false;
3160 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003161 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3162 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003163 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003164 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003165 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003166 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167 final ActivityStack stack = stacks.get(stackNdx);
3168 StringBuilder stackHeader = new StringBuilder(128);
3169 stackHeader.append(" Stack #");
3170 stackHeader.append(stack.mStackId);
3171 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003172 stackHeader.append("\n");
3173 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3174 stackHeader.append("\n");
3175 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3177 needSep, stackHeader.toString());
3178 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3179 !dumpAll, false, dumpPackage, true,
3180 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003181
Craig Mautner4a1cb222013-12-04 16:14:06 -08003182 needSep = printed;
3183 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3184 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003185 if (pr) {
3186 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003188 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3190 " mResumedActivity: ");
3191 if (pr) {
3192 printed = true;
3193 needSep = false;
3194 }
3195 if (dumpAll) {
3196 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3197 " mLastPausedActivity: ");
3198 if (pr) {
3199 printed = true;
3200 needSep = true;
3201 }
3202 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3203 needSep, " mLastNoHistoryActivity: ");
3204 }
3205 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003206 }
3207 }
3208
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003209 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3210 false, dumpPackage, true, " Activities waiting to finish:", null);
3211 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3212 false, dumpPackage, true, " Activities waiting to stop:", null);
3213 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3214 false, dumpPackage, true, " Activities waiting for another to become visible:",
3215 null);
3216 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3217 false, dumpPackage, true, " Activities waiting to sleep:", null);
3218 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3219 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003220
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003221 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003222 }
3223
Dianne Hackborn390517b2013-05-30 15:03:32 -07003224 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003225 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003226 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003228 String innerPrefix = null;
3229 String[] args = null;
3230 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003231 for (int i=list.size()-1; i>=0; i--) {
3232 final ActivityRecord r = list.get(i);
3233 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3234 continue;
3235 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003236 if (innerPrefix == null) {
3237 innerPrefix = prefix + " ";
3238 args = new String[0];
3239 }
3240 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003241 final boolean full = !brief && (complete || !r.isInHistory());
3242 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003243 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 needNL = false;
3245 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003246 if (header1 != null) {
3247 pw.println(header1);
3248 header1 = null;
3249 }
3250 if (header2 != null) {
3251 pw.println(header2);
3252 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003253 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003254 if (lastTask != r.task) {
3255 lastTask = r.task;
3256 pw.print(prefix);
3257 pw.print(full ? "* " : " ");
3258 pw.println(lastTask);
3259 if (full) {
3260 lastTask.dump(pw, prefix + " ");
3261 } else if (complete) {
3262 // Complete + brief == give a summary. Isn't that obvious?!?
3263 if (lastTask.intent != null) {
3264 pw.print(prefix); pw.print(" ");
3265 pw.println(lastTask.intent.toInsecureStringWithClip());
3266 }
3267 }
3268 }
3269 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3270 pw.print(" #"); pw.print(i); pw.print(": ");
3271 pw.println(r);
3272 if (full) {
3273 r.dump(pw, innerPrefix);
3274 } else if (complete) {
3275 // Complete + brief == give a summary. Isn't that obvious?!?
3276 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3277 if (r.app != null) {
3278 pw.print(innerPrefix); pw.println(r.app);
3279 }
3280 }
3281 if (client && r.app != null && r.app.thread != null) {
3282 // flush anything that is already in the PrintWriter since the thread is going
3283 // to write to the file descriptor directly
3284 pw.flush();
3285 try {
3286 TransferPipe tp = new TransferPipe();
3287 try {
3288 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3289 r.appToken, innerPrefix, args);
3290 // Short timeout, since blocking here can
3291 // deadlock with the application.
3292 tp.go(fd, 2000);
3293 } finally {
3294 tp.kill();
3295 }
3296 } catch (IOException e) {
3297 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3298 } catch (RemoteException e) {
3299 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3300 }
3301 needNL = true;
3302 }
3303 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003304 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003305 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003306
Craig Mautnerf3333272013-04-22 10:55:53 -07003307 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3309 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003310 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3311 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003312 }
3313
3314 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003315 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003316 }
3317
3318 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003319 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3320 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003321 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3322 }
3323
Craig Mautner05d29032013-05-03 13:40:13 -07003324 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003325 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3326 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3327 }
Craig Mautner05d29032013-05-03 13:40:13 -07003328 }
3329
Craig Mautner0eea92c2013-05-16 13:35:39 -07003330 void removeSleepTimeouts() {
3331 mSleepTimeout = false;
3332 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3333 }
3334
3335 final void scheduleSleepTimeout() {
3336 removeSleepTimeouts();
3337 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3338 }
3339
Craig Mautner4a1cb222013-12-04 16:14:06 -08003340 @Override
3341 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003342 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003343 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3344 }
3345
3346 @Override
3347 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003348 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3350 }
3351
3352 @Override
3353 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003354 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003355 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3356 }
3357
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003358 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003359 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003360 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003361 newDisplay = mActivityDisplays.get(displayId) == null;
3362 if (newDisplay) {
3363 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003364 if (activityDisplay.mDisplay == null) {
3365 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3366 return;
3367 }
Craig Mautner4504de52013-12-20 09:06:56 -08003368 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003369 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003370 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003371 }
Craig Mautner4504de52013-12-20 09:06:56 -08003372 if (newDisplay) {
3373 mWindowManager.onDisplayAdded(displayId);
3374 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 }
3376
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003377 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003378 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003379 mService.mContext.getResources().getDimensionPixelSize(
3380 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003381 }
3382
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003383 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003385 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3386 if (activityDisplay != null) {
3387 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003389 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003391 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003392 }
3393 }
3394 mWindowManager.onDisplayRemoved(displayId);
3395 }
3396
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003397 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003398 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003399 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3400 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 // TODO: Update the bounds.
3402 }
3403 }
3404 mWindowManager.onDisplayChanged(displayId);
3405 }
3406
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003407 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003408 StackInfo info = new StackInfo();
3409 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3410 info.displayId = Display.DEFAULT_DISPLAY;
3411 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003412 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003413 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414
3415 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3416 final int numTasks = tasks.size();
3417 int[] taskIds = new int[numTasks];
3418 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003419 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003420 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 for (int i = 0; i < numTasks; ++i) {
3422 final TaskRecord task = tasks.get(i);
3423 taskIds[i] = task.taskId;
3424 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3425 : task.realActivity != null ? task.realActivity.flattenToString()
3426 : task.getTopActivity() != null ? task.getTopActivity().packageName
3427 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003428 taskBounds[i] = new Rect();
3429 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003430 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 }
3432 info.taskIds = taskIds;
3433 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003434 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003435 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003436
3437 final ActivityRecord top = stack.topRunningActivityLocked();
3438 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439 return info;
3440 }
3441
3442 StackInfo getStackInfoLocked(int stackId) {
3443 ActivityStack stack = getStack(stackId);
3444 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003445 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 }
3447 return null;
3448 }
3449
3450 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003451 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003452 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3453 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003455 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003456 }
3457 }
3458 return list;
3459 }
3460
Craig Mautner15df08a2015-04-01 12:17:18 -07003461 TaskRecord getLockedTaskLocked() {
3462 final int top = mLockTaskModeTasks.size() - 1;
3463 if (top >= 0) {
3464 return mLockTaskModeTasks.get(top);
3465 }
3466 return null;
3467 }
3468
3469 boolean isLockedTask(TaskRecord task) {
3470 return mLockTaskModeTasks.contains(task);
3471 }
3472
3473 boolean isLastLockedTask(TaskRecord task) {
3474 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3475 }
3476
3477 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003478 if (!mLockTaskModeTasks.remove(task)) {
3479 return;
3480 }
3481 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3482 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003483 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003484 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3485 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003486 final Message lockTaskMsg = Message.obtain();
3487 lockTaskMsg.arg1 = task.userId;
3488 lockTaskMsg.what = LOCK_TASK_END_MSG;
3489 mHandler.sendMessage(lockTaskMsg);
3490 }
3491 }
3492
Andrii Kulianc27916642016-04-12 17:59:27 -07003493 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3494 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3495 false /* forceNonResizable */);
3496 }
3497
Jorim Jaggid53f0922016-04-06 22:16:23 -07003498 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003499 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003500 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3501 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003502 return;
3503 }
3504
Jorim Jaggicd13d332016-04-27 15:40:20 -07003505 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003506 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003507 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003508 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3509
Andrii Kulianc27916642016-04-12 17:59:27 -07003510 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3511 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003512 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003513 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3514 && !topActivity.noDisplay) {
3515 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003516 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3517 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003518 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003519 }
3520
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003521 void showLockTaskToast() {
3522 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003523 }
3524
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003525 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3526 if (mLockTaskModeTasks.contains(task)) {
3527 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3528 }
3529 }
3530
Craig Mautner432f64e2015-05-20 14:59:57 -07003531 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3532 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003533 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003534 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003535 final TaskRecord lockedTask = getLockedTaskLocked();
3536 if (lockedTask != null) {
3537 removeLockedTaskLocked(lockedTask);
3538 if (!mLockTaskModeTasks.isEmpty()) {
3539 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003540 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3541 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003542 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003543 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003544 return;
3545 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003546 }
Craig Mautnere0570202015-05-13 13:06:11 -07003547 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3548 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003549 return;
3550 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003551
3552 // Should have already been checked, but do it again.
3553 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003554 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3555 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003556 return;
3557 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003558 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003559 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003560 return;
3561 }
3562
3563 if (mLockTaskModeTasks.isEmpty()) {
3564 // First locktask.
3565 final Message lockTaskMsg = Message.obtain();
3566 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3567 lockTaskMsg.arg1 = task.userId;
3568 lockTaskMsg.what = LOCK_TASK_START_MSG;
3569 lockTaskMsg.arg2 = lockTaskModeState;
3570 mHandler.sendMessage(lockTaskMsg);
3571 }
3572 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003573 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3574 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003575 mLockTaskModeTasks.remove(task);
3576 mLockTaskModeTasks.add(task);
3577
3578 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003579 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003580 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003581
3582 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003583 findTaskToMoveToFrontLocked(task, 0, null, reason,
3584 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003585 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003586 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3587 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3588 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003589 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003590 }
3591
3592 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003593 return isLockTaskModeViolation(task, false);
3594 }
3595
3596 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3597 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003598 return false;
3599 }
3600 final int lockTaskAuth = task.mLockTaskAuth;
3601 switch (lockTaskAuth) {
3602 case LOCK_TASK_AUTH_DONT_LOCK:
3603 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003604 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003605 case LOCK_TASK_AUTH_LAUNCHABLE:
3606 case LOCK_TASK_AUTH_WHITELISTED:
3607 return false;
3608 case LOCK_TASK_AUTH_PINNABLE:
3609 // Pinnable tasks can't be launched on top of locktask tasks.
3610 return !mLockTaskModeTasks.isEmpty();
3611 default:
3612 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3613 return true;
3614 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003615 }
3616
Craig Mautner15df08a2015-04-01 12:17:18 -07003617 void onLockTaskPackagesUpdatedLocked() {
3618 boolean didSomething = false;
3619 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3620 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003621 final boolean wasWhitelisted =
3622 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3623 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003624 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003625 final boolean isWhitelisted =
3626 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3627 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3628 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003629 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003630 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3631 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003632 removeLockedTaskLocked(lockedTask);
3633 lockedTask.performClearTaskLocked();
3634 didSomething = true;
3635 }
3636 }
3637 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3638 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3639 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3640 final ActivityStack stack = stacks.get(stackNdx);
3641 stack.onLockTaskPackagesUpdatedLocked();
3642 }
3643 }
Craig Mautnere0570202015-05-13 13:06:11 -07003644 final ActivityRecord r = topRunningActivityLocked();
3645 final TaskRecord task = r != null ? r.task : null;
3646 if (mLockTaskModeTasks.isEmpty() && task != null
3647 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3648 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003649 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3650 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3651 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3652 false);
3653 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003654 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003655 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003656 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003657 }
3658 }
3659
Benjamin Franz43261142015-02-11 15:59:44 +00003660 int getLockTaskModeState() {
3661 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003662 }
3663
Jorim Jaggife89d122015-12-22 16:28:44 +01003664 void activityRelaunchedLocked(IBinder token) {
3665 mWindowManager.notifyAppRelaunchingFinished(token);
3666 }
3667
3668 void activityRelaunchingLocked(ActivityRecord r) {
3669 mWindowManager.notifyAppRelaunching(r.appToken);
3670 }
3671
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003672 void logStackState() {
3673 mActivityMetricsLogger.logWindowState();
3674 }
3675
Andrii Kulian933076d2016-03-29 17:04:42 -07003676 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003677 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3678 final ActivityRecord r = task.mActivities.get(i);
3679 if (r.app != null && r.app.thread != null) {
3680 mMultiWindowModeChangedActivities.add(r);
3681 }
3682 }
3683
3684 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3685 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3686 }
3687 }
3688
Andrii Kulian933076d2016-03-29 17:04:42 -07003689 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003690 final ActivityStack stack = task.stack;
3691 if (prevStack == null || prevStack == stack
3692 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3693 return;
3694 }
3695
3696 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3697 final ActivityRecord r = task.mActivities.get(i);
3698 if (r.app != null && r.app.thread != null) {
3699 mPipModeChangedActivities.add(r);
3700 }
3701 }
3702
3703 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3704 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3705 }
3706 }
3707
Tony Mak853304c2016-04-18 15:17:41 +01003708 void setDockedStackMinimized(boolean minimized) {
3709 mIsDockMinimized = minimized;
3710 if (minimized) {
3711 // Docked stack is not visible, no need to confirm credentials for its top activity.
3712 return;
3713 }
3714 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3715 if (dockedStack == null) {
3716 return;
3717 }
3718 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3719 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3720 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3721 }
3722 }
3723
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003724 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003725
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003726 public ActivityStackSupervisorHandler(Looper looper) {
3727 super(looper);
3728 }
3729
Craig Mautnerf3333272013-04-22 10:55:53 -07003730 void activityIdleInternal(ActivityRecord r) {
3731 synchronized (mService) {
3732 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3733 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003734 }
3735
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003736 @Override
3737 public void handleMessage(Message msg) {
3738 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003739 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3740 synchronized (mService) {
3741 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3742 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003743 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003744 }
3745 }
3746 } break;
3747 case REPORT_PIP_MODE_CHANGED_MSG: {
3748 synchronized (mService) {
3749 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3750 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003751 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003752 }
3753 }
3754 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003755 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003756 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3757 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003758 if (mService.mDidDexOpt) {
3759 mService.mDidDexOpt = false;
3760 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3761 nmsg.obj = msg.obj;
3762 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3763 return;
3764 }
3765 // We don't at this point know if the activity is fullscreen,
3766 // so we need to be conservative and assume it isn't.
3767 activityIdleInternal((ActivityRecord)msg.obj);
3768 } break;
3769 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003770 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003771 activityIdleInternal((ActivityRecord)msg.obj);
3772 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003773 case RESUME_TOP_ACTIVITY_MSG: {
3774 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003775 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003776 }
3777 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003778 case SLEEP_TIMEOUT_MSG: {
3779 synchronized (mService) {
3780 if (mService.isSleepingOrShuttingDown()) {
3781 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3782 mSleepTimeout = true;
3783 checkReadyForSleepLocked();
3784 }
3785 }
3786 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003787 case LAUNCH_TIMEOUT_MSG: {
3788 if (mService.mDidDexOpt) {
3789 mService.mDidDexOpt = false;
3790 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3791 return;
3792 }
3793 synchronized (mService) {
3794 if (mLaunchingActivity.isHeld()) {
3795 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3796 if (VALIDATE_WAKE_LOCK_CALLER
3797 && Binder.getCallingUid() != Process.myUid()) {
3798 throw new IllegalStateException("Calling must be system uid");
3799 }
3800 mLaunchingActivity.release();
3801 }
3802 }
3803 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003804 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003805 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 } break;
3807 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003808 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 } break;
3810 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003811 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003813 case CONTAINER_CALLBACK_VISIBILITY: {
3814 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003815 final IActivityContainerCallback callback = container.mCallback;
3816 if (callback != null) {
3817 try {
3818 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3819 } catch (RemoteException e) {
3820 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003821 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003822 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003823 case LOCK_TASK_START_MSG: {
3824 // When lock task starts, we disable the status bars.
3825 try {
Jason Monk62515be2014-05-21 16:06:19 -04003826 if (mLockTaskNotify == null) {
3827 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003828 }
Jason Monk62515be2014-05-21 16:06:19 -04003829 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003830 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003831 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003832 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003833 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003834 flags = StatusBarManager.DISABLE_MASK
3835 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003836 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003837 flags = StatusBarManager.DISABLE_MASK
3838 & (~StatusBarManager.DISABLE_BACK)
3839 & (~StatusBarManager.DISABLE_HOME)
3840 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003841 }
3842 getStatusBarService().disable(flags, mToken,
3843 mService.mContext.getPackageName());
3844 }
3845 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003846 if (getDevicePolicyManager() != null) {
3847 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003848 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003849 }
justinzhang5286d3f2014-05-12 17:06:01 -04003850 } catch (RemoteException ex) {
3851 throw new RuntimeException(ex);
3852 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003853 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003854 case LOCK_TASK_END_MSG: {
3855 // When lock task ends, we enable the status bars.
3856 try {
Jason Monk62515be2014-05-21 16:06:19 -04003857 if (getStatusBarService() != null) {
3858 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3859 mService.mContext.getPackageName());
3860 }
3861 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003862 if (getDevicePolicyManager() != null) {
3863 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3864 msg.arg1);
3865 }
Jason Monk62515be2014-05-21 16:06:19 -04003866 if (mLockTaskNotify == null) {
3867 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3868 }
3869 mLockTaskNotify.show(false);
3870 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003871 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003872 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003873 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003874 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003875 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003876 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003877 new LockPatternUtils(mService.mContext)
3878 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003879 }
3880 } catch (SettingNotFoundException e) {
3881 // No setting, don't lock.
3882 }
justinzhang5286d3f2014-05-12 17:06:01 -04003883 } catch (RemoteException ex) {
3884 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003885 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003886 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003887 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003888 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003889 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3890 if (mLockTaskNotify == null) {
3891 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3892 }
3893 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3894 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003895 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3896 final ActivityContainer container = (ActivityContainer) msg.obj;
3897 final IActivityContainerCallback callback = container.mCallback;
3898 if (callback != null) {
3899 try {
3900 callback.onAllActivitiesComplete(container.asBinder());
3901 } catch (RemoteException e) {
3902 }
3903 }
3904 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003905 case LAUNCH_TASK_BEHIND_COMPLETE: {
3906 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003907 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003908 if (r != null) {
3909 handleLaunchTaskBehindCompleteLocked(r);
3910 }
3911 }
3912 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003913
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003914 }
3915 }
3916 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003917
Ying Wangb081a592014-04-22 15:20:16 -07003918 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003919 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3920 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003922 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003923 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003924 ActivityRecord mParentActivity = null;
3925 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003926
Craig Mautnere3a00d72014-04-16 08:31:19 -07003927 boolean mVisible = true;
3928
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003930 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003932 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3933 final static int CONTAINER_STATE_NO_SURFACE = 1;
3934 final static int CONTAINER_STATE_FINISHING = 2;
3935 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3936
3937 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 synchronized (mService) {
3939 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003940 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003941 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003942 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003943 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003944 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003946 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003947 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003948 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003949 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003950 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003951 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003952 }
3953
3954 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003955 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003956 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003957 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3958 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003959 return;
3960 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003961 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003962 }
3963 }
3964
3965 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003966 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003967 synchronized (mService) {
3968 if (mActivityDisplay != null) {
3969 return mActivityDisplay.mDisplayId;
3970 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003971 }
3972 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 }
3974
Jeff Brownca9bc702014-02-11 14:32:56 -08003975 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003976 public int getStackId() {
3977 synchronized (mService) {
3978 return mStackId;
3979 }
3980 }
3981
3982 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003983 public boolean injectEvent(InputEvent event) {
3984 final long origId = Binder.clearCallingIdentity();
3985 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003986 synchronized (mService) {
3987 if (mActivityDisplay != null) {
3988 return mInputManagerInternal.injectInputEvent(event,
3989 mActivityDisplay.mDisplayId,
3990 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3991 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003992 }
3993 return false;
3994 } finally {
3995 Binder.restoreCallingIdentity(origId);
3996 }
3997 }
3998
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003999 @Override
4000 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004001 synchronized (mService) {
4002 if (mContainerState == CONTAINER_STATE_FINISHING) {
4003 return;
4004 }
4005 mContainerState = CONTAINER_STATE_FINISHING;
4006
Craig Mautnerd163e752014-06-13 17:18:47 -07004007 long origId = Binder.clearCallingIdentity();
4008 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004009 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004010 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004011 } finally {
4012 Binder.restoreCallingIdentity(origId);
4013 }
4014 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004015 }
4016
Craig Mautner60257702014-09-17 15:02:33 -07004017 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004018 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004019 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004020 if (mActivityDisplay != null) {
4021 mActivityDisplay.detachActivitiesLocked(mStack);
4022 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004023 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004024 }
4025 }
4026
4027 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004028 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004029 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
4031
4032 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004033 public final int startActivityIntentSender(IIntentSender intentSender)
4034 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004035 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4036
4037 if (!(intentSender instanceof PendingIntentRecord)) {
4038 throw new IllegalArgumentException("Bad PendingIntent object");
4039 }
4040
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004041 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004042 Binder.getCallingUid(), mCurrentUser, false,
4043 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004044
4045 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4046 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4047 pendingIntent.key.requestResolvedType);
4048
4049 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4050 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4051 }
4052
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004053 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004054 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004055 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004056 throw new SecurityException(
4057 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4058 }
4059 }
4060
Craig Mautnerdf88d732014-01-27 09:21:32 -08004061 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 public IBinder asBinder() {
4063 return this;
4064 }
4065
Craig Mautner4504de52013-12-20 09:06:56 -08004066 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004067 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004068 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004069 }
4070
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 ActivityStackSupervisor getOuter() {
4072 return ActivityStackSupervisor.this;
4073 }
4074
Craig Mautnerd163e752014-06-13 17:18:47 -07004075 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004076 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 }
4078
Craig Mautner6985bad2014-04-21 15:22:06 -07004079 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004080 void setVisible(boolean visible) {
4081 if (mVisible != visible) {
4082 mVisible = visible;
4083 if (mCallback != null) {
4084 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4085 0 /* unused */, this).sendToTarget();
4086 }
4087 }
4088 }
4089
Craig Mautner6985bad2014-04-21 15:22:06 -07004090 void setDrawn() {
4091 }
4092
Craig Mautner1b4bf852014-05-26 15:06:32 -07004093 // You can always start a new task on a regular ActivityStack.
4094 boolean isEligibleForNewTasks() {
4095 return true;
4096 }
4097
Craig Mautnerd163e752014-06-13 17:18:47 -07004098 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004099 detachLocked();
4100 deleteActivityContainer(this);
4101 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004102 }
4103
Craig Mautner34b73df2014-01-12 21:11:08 -08004104 @Override
4105 public String toString() {
4106 return mIdString + (mActivityDisplay == null ? "N" : "A");
4107 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004108 }
4109
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004110 private class VirtualActivityContainer extends ActivityContainer {
4111 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004112 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004113
4114 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4115 super(getNextStackId());
4116 mParentActivity = parent;
4117 mCallback = callback;
4118 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004119 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004120 }
4121
4122 @Override
4123 public void setSurface(Surface surface, int width, int height, int density) {
4124 super.setSurface(surface, width, height, density);
4125
4126 synchronized (mService) {
4127 final long origId = Binder.clearCallingIdentity();
4128 try {
4129 setSurfaceLocked(surface, width, height, density);
4130 } finally {
4131 Binder.restoreCallingIdentity(origId);
4132 }
4133 }
4134 }
4135
4136 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4137 if (mContainerState == CONTAINER_STATE_FINISHING) {
4138 return;
4139 }
4140 VirtualActivityDisplay virtualActivityDisplay =
4141 (VirtualActivityDisplay) mActivityDisplay;
4142 if (virtualActivityDisplay == null) {
4143 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004144 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004145 mActivityDisplay = virtualActivityDisplay;
4146 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004147 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004148 }
4149
4150 if (mSurface != null) {
4151 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004152 }
4153
Craig Mautner6985bad2014-04-21 15:22:06 -07004154 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004155 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004156 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004157 } else {
4158 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004159 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004160 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004161 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004162 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004163 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004164
Craig Mautnerd163e752014-06-13 17:18:47 -07004165 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004166
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004167 if (DEBUG_STACK) Slog.d(TAG_STACK,
4168 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004169 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004170
Craig Mautner6985bad2014-04-21 15:22:06 -07004171 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004172 boolean isAttachedLocked() {
4173 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004174 }
4175
4176 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004177 void setDrawn() {
4178 synchronized (mService) {
4179 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004180 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004181 }
4182 }
4183
Craig Mautner1b4bf852014-05-26 15:06:32 -07004184 // Never start a new task on an ActivityView if it isn't explicitly specified.
4185 @Override
4186 boolean isEligibleForNewTasks() {
4187 return false;
4188 }
4189
Craig Mautnerd163e752014-06-13 17:18:47 -07004190 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004191 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004192 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4193 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4194 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4195 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4196 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004197 }
4198 }
4199
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4201 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004202 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004203 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004204 int mDisplayId;
4205 Display mDisplay;
4206 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004207
Craig Mautner4a1cb222013-12-04 16:14:06 -08004208 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4209 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004210 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004211
Jose Lima4b6c6692014-08-12 17:41:12 -07004212 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004213
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004214 ActivityDisplay() {
4215 }
Craig Mautner4504de52013-12-20 09:06:56 -08004216
Craig Mautner1a70a162014-09-13 12:09:31 -07004217 // After instantiation, check that mDisplay is not null before using this. The alternative
4218 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004219 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004220 final Display display = mDisplayManager.getDisplay(displayId);
4221 if (display == null) {
4222 return;
4223 }
4224 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004225 }
4226
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004227 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004228 mDisplay = display;
4229 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004231 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004233 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004234 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004235 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4236 + " onTop=" + onTop);
4237 if (onTop) {
4238 mStacks.add(stack);
4239 } else {
4240 mStacks.add(0, stack);
4241 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 }
4243
Craig Mautnere0a38842013-12-16 16:14:02 -08004244 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004245 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004246 + " from displayId=" + mDisplayId);
4247 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004248 }
4249
Jose Lima4b6c6692014-08-12 17:41:12 -07004250 void setVisibleBehindActivity(ActivityRecord r) {
4251 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004252 }
4253
Jose Lima4b6c6692014-08-12 17:41:12 -07004254 boolean hasVisibleBehindActivity() {
4255 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004256 }
4257
Craig Mautner34b73df2014-01-12 21:11:08 -08004258 @Override
4259 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004260 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4261 }
4262 }
4263
4264 class VirtualActivityDisplay extends ActivityDisplay {
4265 VirtualDisplay mVirtualDisplay;
4266
Craig Mautner6985bad2014-04-21 15:22:06 -07004267 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004268 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004269 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4270 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4271 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4272 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004273
4274 init(mVirtualDisplay.getDisplay());
4275
4276 mWindowManager.handleDisplayAdded(mDisplayId);
4277 }
4278
4279 void setSurface(Surface surface) {
4280 if (mVirtualDisplay != null) {
4281 mVirtualDisplay.setSurface(surface);
4282 }
4283 }
4284
4285 @Override
4286 void detachActivitiesLocked(ActivityStack stack) {
4287 super.detachActivitiesLocked(stack);
4288 if (mVirtualDisplay != null) {
4289 mVirtualDisplay.release();
4290 mVirtualDisplay = null;
4291 }
4292 }
4293
4294 @Override
4295 public String toString() {
4296 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004297 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004298 }
Jose Lima58e66d62014-05-27 20:00:27 -07004299
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004300 /**
4301 * Adjust bounds to stay within stack bounds.
4302 *
4303 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4304 * that keep them unchanged, but be contained within the stack bounds.
4305 *
4306 * @param bounds Bounds to be adjusted.
4307 * @param stackBounds Bounds within which the other bounds should remain.
4308 */
4309 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4310 if (stackBounds == null || stackBounds.contains(bounds)) {
4311 return;
4312 }
4313
4314 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4315 final int maxRight = stackBounds.right
4316 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4317 int horizontalDiff = stackBounds.left - bounds.left;
4318 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4319 || (bounds.left + horizontalDiff >= maxRight)) {
4320 horizontalDiff = maxRight - bounds.left;
4321 }
4322 bounds.left += horizontalDiff;
4323 bounds.right += horizontalDiff;
4324 }
4325
4326 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4327 final int maxBottom = stackBounds.bottom
4328 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4329 int verticalDiff = stackBounds.top - bounds.top;
4330 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4331 || (bounds.top + verticalDiff >= maxBottom)) {
4332 verticalDiff = maxBottom - bounds.top;
4333 }
4334 bounds.top += verticalDiff;
4335 bounds.bottom += verticalDiff;
4336 }
4337 }
4338
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004339 ActivityStack findStackBehind(ActivityStack stack) {
4340 // TODO(multi-display): We are only looking for stacks on the default display.
4341 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4342 if (display == null) {
4343 return null;
4344 }
4345 final ArrayList<ActivityStack> stacks = display.mStacks;
4346 for (int i = stacks.size() - 1; i >= 0; i--) {
4347 if (stacks.get(i) == stack && i > 0) {
4348 return stacks.get(i - 1);
4349 }
4350 }
4351 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4352 + " in=" + stacks);
4353 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004354
Jorim Jaggic69bd222016-03-15 14:38:37 +01004355 /**
4356 * Puts a task into resizing mode during the next app transition.
4357 *
4358 * @param taskId the id of the task to put into resizing mode
4359 */
4360 private void setResizingDuringAnimation(int taskId) {
4361 mResizingTasksDuringAnimation.add(taskId);
4362 mWindowManager.setTaskDockedResizing(taskId, true);
4363 }
4364
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004365 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4366 final TaskRecord task;
4367 final int callingUid;
4368 final String callingPackage;
4369 final Intent intent;
4370 final int userId;
4371 final ActivityOptions activityOptions = (bOptions != null)
4372 ? new ActivityOptions(bOptions) : null;
4373 final int launchStackId = (activityOptions != null)
4374 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004375 if (launchStackId == HOME_STACK_ID) {
4376 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4377 + taskId + " can't be launch in the home stack.");
4378 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004379
4380 if (launchStackId == DOCKED_STACK_ID) {
4381 mWindowManager.setDockedStackCreateState(
4382 activityOptions.getDockCreateMode(), null /* initialBounds */);
4383
4384 // Defer updating the stack in which recents is until the app transition is done, to
4385 // not run into issues where we still need to draw the task in recents but the
4386 // docked stack is already created.
4387 deferUpdateBounds(HOME_STACK_ID);
4388 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4389 }
4390
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004391 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4392 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004393 continueUpdateBounds(HOME_STACK_ID);
4394 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004395 throw new IllegalArgumentException(
4396 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4397 }
4398
4399 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004400 if (task.stack.mStackId != launchStackId) {
4401 moveTaskToStackLocked(
4402 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4403 ANIMATE);
4404 }
4405 }
4406
4407 // If the user must confirm credentials (e.g. when first launching a work app and the
4408 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4409 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4410 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004411 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004412 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004413 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4414 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004415
4416 // If we are launching the task in the docked stack, put it into resizing mode so
4417 // the window renders full-screen with the background filling the void. Also only
4418 // call this at the end to make sure that tasks exists on the window manager side.
4419 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004420 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004421 }
4422 return ActivityManager.START_TASK_TO_FRONT;
4423 }
4424 callingUid = task.mCallingUid;
4425 callingPackage = task.mCallingPackage;
4426 intent = task.intent;
4427 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4428 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004429 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4430 null, null, 0, 0, bOptions, userId, null, task);
4431 if (launchStackId == DOCKED_STACK_ID) {
4432 setResizingDuringAnimation(task.taskId);
4433 }
4434 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004435 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004436
4437 /**
4438 * @return a list of activities which are the top ones in each visible stack. The first
4439 * entry will be the focused activity.
4440 */
4441 public List<IBinder> getTopVisibleActivities() {
4442 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4443 if (display == null) {
4444 return Collections.EMPTY_LIST;
4445 }
4446 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4447 final ArrayList<ActivityStack> stacks = display.mStacks;
4448 for (int i = stacks.size() - 1; i >= 0; i--) {
4449 ActivityStack stack = stacks.get(i);
4450 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4451 ActivityRecord top = stack.topActivity();
4452 if (top != null) {
4453 if (stack == mFocusedStack) {
4454 topActivityTokens.add(0, top.appToken);
4455 } else {
4456 topActivityTokens.add(top.appToken);
4457 }
4458 }
4459 }
4460 }
4461 return topActivityTokens;
4462 }
Craig Mautner27084302013-03-25 08:05:25 -07004463}