blob: e86ba22c5bd2998cab0ca8a8214a762e7565ede8 [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;
Jorim Jaggi51605272016-02-24 18:06:49 -0500126import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100127import static android.content.pm.PackageManager.PERMISSION_GRANTED;
128import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
131import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
132import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
133import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
134import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
135import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
136import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
137import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
138import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
139import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
140import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
141import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
142import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
153import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
154import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800155import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100156import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700157import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100158import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100159import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
160import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
162import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
163import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
164import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
165import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
166import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
167import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
168import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
169import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700170import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Tony Mak853304c2016-04-18 15:17:41 +0100171import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Winsond46b7272016-04-20 11:54:27 -0700172import static com.android.server.am.ActivityStack.STACK_VISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100173import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
177import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100178import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100179
Craig Mautner4a1cb222013-12-04 16:14:06 -0800180public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800181 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700184 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800191 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193
Craig Mautnerf3333272013-04-22 10:55:53 -0700194 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700196
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700200 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202
Craig Mautner05d29032013-05-03 13:40:13 -0700203 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
204 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
205 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800208 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
209 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
210 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700211 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400212 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
213 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700214 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700215 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700216 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800217 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
218 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700221
Jason Monk62515be2014-05-21 16:06:19 -0400222 private static final String LOCK_TASK_TAG = "Lock-to-App";
223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 // Used to indicate if an object (e.g. stack) that we are trying to get
225 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800226 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700228 // Used to indicate that windows of activities should be preserved during the resize.
229 static final boolean PRESERVE_WINDOWS = true;
230
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231 // Used to indicate if an object (e.g. task) should be moved/created
232 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700233 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234
235 // Used to indicate that an objects (e.g. task) removal from its container
236 // (e.g. stack) is due to it moving to another container.
237 static final boolean MOVING = true;
238
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700239 // Force the focus to change to the stack we are moving a task to..
240 static final boolean FORCE_FOCUS = true;
241
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700242 // Restore task from the saved recents if it can't be found in any live stack.
243 static final boolean RESTORE_FROM_RECENTS = true;
244
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700245 // Don't execute any calls to resume.
246 static final boolean DEFER_RESUME = true;
247
Svetoslav7008b512015-06-24 18:47:07 -0700248 // Activity actions an app cannot start if it uses a permission which is not granted.
249 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
250 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700251
Svetoslav7008b512015-06-24 18:47:07 -0700252 static {
253 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
254 Manifest.permission.CAMERA);
255 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
256 Manifest.permission.CAMERA);
257 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
258 Manifest.permission.CALL_PHONE);
259 }
260
Svet Ganov99b60432015-06-27 13:15:22 -0700261 /** Action restriction: launching the activity is not restricted. */
262 private static final int ACTIVITY_RESTRICTION_NONE = 0;
263 /** Action restriction: launching the activity is restricted by a permission. */
264 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
265 /** Action restriction: launching the activity is restricted by an app op. */
266 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700267
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700268 // The height/width divide used when fitting a task within a bounds with method
269 // {@link #fitWithinBounds}.
270 // We always want the task to to be visible in the bounds without affecting its size when
271 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
272 // the input bounds right or bottom side minus the width or height divided by this value.
273 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
274
justinzhang5286d3f2014-05-12 17:06:01 -0400275 /** Status Bar Service **/
276 private IBinder mToken = new Binder();
277 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400278 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400279
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280 // For debugging to make sure the caller when acquiring/releasing our
281 // wake lock is the system process.
282 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /** The number of distinct task ids that can be assigned to the tasks of a single user */
284 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner27084302013-03-25 08:05:25 -0700286 final ActivityManagerService mService;
287
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700296 /** Counter for next free stack ID to use for dynamic activity stacks. */
297 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700298
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800299 /**
300 * Maps the task identifier that activities are currently being started in to the userId of the
301 * task. Each time a new task is created, the entry for the userId of the task is incremented
302 */
303 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700304
Craig Mautner2420ead2013-04-01 17:13:20 -0700305 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack containing the launcher app. Assumed to always be attached to
309 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800313 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700314
Craig Mautner4a1cb222013-12-04 16:14:06 -0800315 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
316 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800318 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
320 /** List of activities that are waiting for a new activity to become visible before completing
321 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800322 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800325 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700326
327 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330 /** List of activities that are ready to be stopped, but waiting for the next activity to
331 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333
Craig Mautnerf3333272013-04-22 10:55:53 -0700334 /** List of activities that are ready to be finished, but waiting for the previous activity to
335 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700337
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700340
Wale Ogunwale22e25262016-02-01 10:32:02 -0800341 /** List of activities whose multi-window mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
344
345 /** List of activities whose picture-in-picture mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
348
Craig Mautnerf3333272013-04-22 10:55:53 -0700349 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700350 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700351
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
353 * is being brought in front of us. */
354 boolean mUserLeaving = false;
355
Craig Mautner0eea92c2013-05-16 13:35:39 -0700356 /** Set when we have taken too long waiting to go to sleep. */
357 boolean mSleepTimeout = false;
358
359 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 * We don't want to allow the device to go to sleep while in the process
361 * of launching an activity. This is primarily to allow alarm intent
362 * receivers to launch an activity and get that to run before the device
363 * goes back to sleep.
364 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700365 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700366
367 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368 * Set when the system is going to sleep, until we have
369 * successfully paused the current activity and released our wake lock.
370 * At that point the system is allowed to actually sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700374 /** Stack id of the front stack when user switched, indexed by userId. */
375 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700376
Craig Mautner4504de52013-12-20 09:06:56 -0800377 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700379 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380
381 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383
Jeff Brownca9bc702014-02-11 14:32:56 -0800384 InputManagerInternal mInputManagerInternal;
385
Craig Mautner15df08a2015-04-01 12:17:18 -0700386 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
387 * may be finished until there is only one entry left. If this is empty the system is not
388 * in lockTask mode. */
389 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000390 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700391 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
392 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000393 */
394 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400395 /**
396 * Notifies the user when entering/exiting lock-task.
397 */
398 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700403 // 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 -0700404 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700405 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700406
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700407 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
408 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
Jorim Jaggidc249c42015-12-15 14:57:31 -0800409 private final SparseArray<Rect> mTmpInsetBounds = new SparseArray<>();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700410
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700411 // The default minimal size that will be used if the activity doesn't specify its minimal size.
412 // It will be calculated when the default display gets added.
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800413 int mDefaultMinimalSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700414
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700415 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
416 private boolean mTaskLayersChanged = true;
417
Jorim Jaggi275561a2016-02-23 10:11:02 -0500418 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800419
Jorim Jaggidc249c42015-12-15 14:57:31 -0800420 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
421
Wale Ogunwale39381972015-12-17 17:15:29 -0800422 static class FindTaskResult {
423 ActivityRecord r;
424 boolean matchedByRootAffinity;
425 }
426 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
427
Craig Mautneree36c772014-07-16 14:56:05 -0700428 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100429 * Used to keep track whether app visibilities got changed since the last pause. Useful to
430 * determine whether to invoke the task stack change listener after pausing.
431 */
432 boolean mAppVisibilitiesChangedSinceLastPause;
433
434 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100435 * Set of tasks that are in resizing mode during an app transition to fill the "void".
436 */
437 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
438
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700439
440 /**
441 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
442 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
443 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
444 * like the docked stack going empty.
445 */
446 private boolean mAllowDockedStackResize = true;
447
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100448 /**
Tony Mak853304c2016-04-18 15:17:41 +0100449 * Is dock currently minimized.
450 */
451 boolean mIsDockMinimized;
452
453 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700454 * Description of a request to start a new activity, which has been held
455 * due to app switches being disabled.
456 */
457 static class PendingActivityLaunch {
458 final ActivityRecord r;
459 final ActivityRecord sourceRecord;
460 final int startFlags;
461 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700462 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700463
464 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700465 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700466 r = _r;
467 sourceRecord = _sourceRecord;
468 startFlags = _startFlags;
469 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700470 callerApp = _callerApp;
471 }
472
473 void sendErrorResult(String message) {
474 try {
475 if (callerApp.thread != null) {
476 callerApp.thread.scheduleCrash(message);
477 }
478 } catch (RemoteException e) {
479 Slog.e(TAG, "Exception scheduling crash of failed "
480 + "activity launcher sourceRecord=" + sourceRecord, e);
481 }
Craig Mautneree36c772014-07-16 14:56:05 -0700482 }
483 }
484
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800485 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700486 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700487 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800488 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800489 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700490 }
491
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800492 void setRecentTasks(RecentTasks recentTasks) {
493 mRecentTasks = recentTasks;
494 }
495
Jeff Brown2c43c332014-06-12 22:38:59 -0700496 /**
497 * At the time when the constructor runs, the power manager has not yet been
498 * initialized. So we initialize our wakelocks afterwards.
499 */
500 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700502 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700503 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700504 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700505 }
506
justinzhang5286d3f2014-05-12 17:06:01 -0400507 // This function returns a IStatusBarService. The value is from ServiceManager.
508 // getService and is cached.
509 private IStatusBarService getStatusBarService() {
510 synchronized (mService) {
511 if (mStatusBarService == null) {
512 mStatusBarService = IStatusBarService.Stub.asInterface(
513 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
514 if (mStatusBarService == null) {
515 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
516 }
517 }
518 return mStatusBarService;
519 }
520 }
521
Jason Monk35c62a42014-06-17 10:24:47 -0400522 private IDevicePolicyManager getDevicePolicyManager() {
523 synchronized (mService) {
524 if (mDevicePolicyManager == null) {
525 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
526 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
527 if (mDevicePolicyManager == null) {
528 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
529 }
530 }
531 return mDevicePolicyManager;
532 }
533 }
534
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700535 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 synchronized (mService) {
537 mWindowManager = wm;
538
539 mDisplayManager =
540 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
541 mDisplayManager.registerDisplayListener(this, null);
542
543 Display[] displays = mDisplayManager.getDisplays();
544 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
545 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800546 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700547 if (activityDisplay.mDisplay == null) {
548 throw new IllegalStateException("Default Display does not exist");
549 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800550 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700551 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 }
553
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800554 mHomeStack = mFocusedStack = mLastFocusedStack =
555 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800556
557 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800558 }
Craig Mautner27084302013-03-25 08:05:25 -0700559 }
560
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200561 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200563 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700564 }
565
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700566 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700568 }
569
Craig Mautnerde4ef022013-04-07 19:01:33 -0700570 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800571 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700572 }
573
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700574 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700575 if (stack == null) {
576 return false;
577 }
578
Craig Mautnerdf88d732014-01-27 09:21:32 -0800579 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
580 if (parent != null) {
581 stack = parent.task.stack;
582 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800583 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700584 }
585
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700586 /** The top most stack. */
587 boolean isFrontStack(ActivityStack stack) {
588 if (stack == null) {
589 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700591
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700592 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
593 if (parent != null) {
594 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800595 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700596 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
597 }
598
Wale Ogunwaled046a012015-12-24 13:05:59 -0800599 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800600 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
601 if (!focusCandidate.isFocusable()) {
602 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
603 focusCandidate = focusCandidate.getNextFocusableStackLocked();
604 }
605
606 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800607 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800608 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800609
Wale Ogunwaled046a012015-12-24 13:05:59 -0800610 EventLogTags.writeAmFocusedStack(
611 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
612 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
613 }
614
615 final ActivityRecord r = topRunningActivityLocked();
Chong Zhang3aa28b22016-02-04 16:38:33 -0800616 if (!mService.mDoingSetFocusedActivity && mService.mFocusedActivity != r) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800617 // The focus activity should always be the top activity in the focused stack.
618 // There will be chaos and anarchy if it isn't...
619 mService.setFocusedActivityLocked(r, reason + " setFocusStack");
620 }
Craig Mautnerde313752015-01-22 14:28:03 -0800621
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800622 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800623 if (r != null && r.idle) {
624 checkFinishBootingLocked();
625 }
626 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700627 }
628
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700629 void moveHomeStackToFront(String reason) {
630 mHomeStack.moveToFront(reason);
631 }
632
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700633 /** Returns true if the focus activity was adjusted to the home stack top activity. */
634 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700635 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700636 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700637 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700638 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700639
Craig Mautner84984fa2014-06-19 11:19:20 -0700640 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700641
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700642 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700643 if (top == null) {
644 return false;
645 }
646 mService.setFocusedActivityLocked(top, reason);
647 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700648 }
649
Craig Mautner299f9602015-01-26 09:47:33 -0800650 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700651 if (!mService.mBooting && !mService.mBooted) {
652 // Not ready yet!
653 return false;
654 }
655
Craig Mautner84984fa2014-06-19 11:19:20 -0700656 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700657 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700658 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700659 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700660
Craig Mautner84984fa2014-06-19 11:19:20 -0700661 if (prev != null) {
662 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
663 }
664
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700665 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
666 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800667 final String myReason = reason + " resumeHomeStackTask";
668
Mark Lua56ea122015-10-08 13:31:01 +0800669 // Only resume home activity if isn't finishing.
670 if (r != null && !r.finishing) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800671 mService.setFocusedActivityLocked(r, myReason);
672 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700673 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800674 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700675 }
676
Craig Mautner8d341ef2013-03-26 09:03:27 -0700677 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700678 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700679 }
680
681 /**
682 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
683 * @param id Id of the task we would like returned.
684 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
685 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700686 * @param stackId The stack to restore the task to (default launch stack will be used if
687 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700688 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700689 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800690 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800691 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800692 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
694 ActivityStack stack = stacks.get(stackNdx);
695 TaskRecord task = stack.taskForIdLocked(id);
696 if (task != null) {
697 return task;
698 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700699 }
700 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800701
702 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800704 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800705 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800707 return null;
708 }
709
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700710 if (!restoreFromRecents) {
711 return task;
712 }
713
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700714 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700715 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
716 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800717 return null;
718 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700719 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800720 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700721 }
722
Craig Mautner6170f732013-04-02 13:05:23 -0700723 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800724 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
728 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
729 if (r != null) {
730 return r;
731 }
Craig Mautner6170f732013-04-02 13:05:23 -0700732 }
733 }
734 return null;
735 }
736
Tony Mak853304c2016-04-18 15:17:41 +0100737 /**
738 * TODO: Handle freefom mode.
739 * @return true when credential confirmation is needed for the user and there is any
740 * activity started by the user in any visible stack.
741 */
742 boolean isUserLockedProfile(@UserIdInt int userId) {
743 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
744 return false;
745 }
746 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
747 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
748 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
749 for (final ActivityStack activityStack : activityStacks) {
750 if (activityStack == null) {
751 continue;
752 }
753 if (activityStack.topRunningActivityLocked() == null) {
754 continue;
755 }
756 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
757 continue;
758 }
759 if (activityStack.isDockedStack() && mIsDockMinimized) {
760 continue;
761 }
762 final TaskRecord topTask = activityStack.topTask();
763 if (topTask == null) {
764 continue;
765 }
766 // To handle the case that work app is in the task but just is not the top one.
767 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
768 final ActivityRecord activityRecord = topTask.mActivities.get(i);
769 if (activityRecord.userId == userId) {
770 return true;
771 }
772 }
773 }
774 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000775 }
776
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800777 void setNextTaskIdForUserLocked(int taskId, int userId) {
778 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
779 if (taskId > currentTaskId) {
780 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700781 }
782 }
783
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800784 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
786 // for a userId u, a taskId can only be in the range
787 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
788 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
789 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800790 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
791 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
792 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800793 candidateTaskId++;
794 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
795 // Wrap around as there will be smaller task ids that are available now.
796 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700797 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800798 if (candidateTaskId == currentTaskId) {
799 // Something wrong!
800 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
801 throw new IllegalStateException("Cannot get an available task id."
802 + " Reached limit of " + MAX_TASK_IDS_PER_USER
803 + " running tasks per user.");
804 }
805 }
806 mCurTaskIdForUser.put(userId, candidateTaskId);
807 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700808 }
809
Craig Mautnerde4ef022013-04-07 19:01:33 -0700810 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800811 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700812 if (stack == null) {
813 return null;
814 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 ActivityRecord resumedActivity = stack.mResumedActivity;
816 if (resumedActivity == null || resumedActivity.app == null) {
817 resumedActivity = stack.mPausingActivity;
818 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700819 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700820 }
821 }
822 return resumedActivity;
823 }
824
Dianne Hackbornff072722014-09-24 10:56:28 -0700825 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700826 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700832 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 continue;
834 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700835 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 if (hr != null) {
837 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
838 && processName.equals(hr.processName)) {
839 try {
George Mount2c92c972014-03-20 09:38:23 -0700840 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 didSomething = true;
842 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700843 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 Slog.w(TAG, "Exception in new application when starting activity "
845 + hr.intent.getComponent().flattenToShortString(), e);
846 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700847 }
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
850 }
851 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700852 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700853 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700854 }
Craig Mautner20e72272013-04-01 13:45:53 -0700855 return didSomething;
856 }
857
858 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800859 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
862 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700863 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 continue;
865 }
866 final ActivityRecord resumedActivity = stack.mResumedActivity;
867 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700868 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800869 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 return false;
871 }
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
873 }
874 return true;
875 }
876
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
881 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700882 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700884 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 return false;
886 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 }
888 }
889 }
890 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700891 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
893 mLastFocusedStack + " to=" + mFocusedStack);
894 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 return true;
896 }
897
898 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800899 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
901 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
903 final ActivityStack stack = stacks.get(stackNdx);
904 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800905 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700906 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800907 return false;
908 }
909 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700911 }
912 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800913 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 }
915
Craig Mautner2acc3892013-09-23 10:28:14 -0700916 /**
917 * Pause all activities in either all of the stacks or just the back stacks.
918 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700919 * @return true if any activity was paused as a result of this call.
920 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700922 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
924 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
926 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700927 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700928 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700930 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
931 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700933 }
934 }
935 return someActivityPaused;
936 }
937
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700939 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
941 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
943 final ActivityStack stack = stacks.get(stackNdx);
944 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700945 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700947 Slog.d(TAG_STATES,
948 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 pausing = false;
950 } else {
951 return false;
952 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700953 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 }
955 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700956 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700957 }
958
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700959 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
960 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500961 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
963 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
965 final ActivityStack stack = stacks.get(stackNdx);
966 if (stack.mResumedActivity != null &&
967 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700968 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800969 }
970 }
971 }
972 }
973
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800974 void cancelInitializingActivities() {
975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
976 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
978 stacks.get(stackNdx).cancelInitializingActivities();
979 }
980 }
981 }
982
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700983 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700984 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700985 }
986
987 void sendWaitingVisibleReportLocked(ActivityRecord r) {
988 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700989 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700990 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700991 if (w.who == null) {
992 changed = true;
993 w.timeout = false;
994 if (r != null) {
995 w.who = new ComponentName(r.info.packageName, r.info.name);
996 }
997 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
998 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700999 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001001 if (changed) {
1002 mService.notifyAll();
1003 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001004 }
1005
1006 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1007 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001008 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001009 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001010 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001011 if (w.who == null) {
1012 changed = true;
1013 w.timeout = timeout;
1014 if (r != null) {
1015 w.who = new ComponentName(r.info.packageName, r.info.name);
1016 }
1017 w.thisTime = thisTime;
1018 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001019 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001020 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001021 if (changed) {
1022 mService.notifyAll();
1023 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001024 }
1025
Craig Mautner29219d92013-04-16 20:19:12 -07001026 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001027 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001028 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001029 if (r != null) {
1030 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001031 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001032
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001034 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001035 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1036 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001037 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001038 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001039 if (r != null) {
1040 return r;
1041 }
1042 }
1043 }
1044 return null;
1045 }
1046
Dianne Hackborn09233282014-04-30 11:33:59 -07001047 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001048 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001049 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1050 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001051 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001052 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001053 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001054 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1055 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001056 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001057 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001058 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001059 }
1060 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001061
1062 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1063 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1064 while (maxNum > 0) {
1065 long mostRecentActiveTime = Long.MIN_VALUE;
1066 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001067 final int numTaskLists = runningTaskLists.size();
1068 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1069 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001070 if (!stackTaskList.isEmpty()) {
1071 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1072 if (lastActiveTime > mostRecentActiveTime) {
1073 mostRecentActiveTime = lastActiveTime;
1074 selectedStackList = stackTaskList;
1075 }
1076 }
1077 }
1078 if (selectedStackList != null) {
1079 list.add(selectedStackList.remove(0));
1080 --maxNum;
1081 } else {
1082 break;
1083 }
1084 }
Craig Mautner20e72272013-04-01 13:45:53 -07001085 }
1086
Todd Kennedy7440f172015-12-09 14:31:22 -08001087 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1088 ProfilerInfo profilerInfo) {
1089 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 if (aInfo != null) {
1091 // Store the found target back into the intent, because now that
1092 // we have it we never want to do this again. For example, if the
1093 // user navigates back to this point in the history, we should
1094 // always restart the exact same activity.
1095 intent.setComponent(new ComponentName(
1096 aInfo.applicationInfo.packageName, aInfo.name));
1097
1098 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001099 if (!aInfo.processName.equals("system")) {
1100 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhang55c9d732016-04-26 12:24:55 -07001101 final ProcessRecord app = mService.getProcessRecordLocked(
1102 aInfo.processName, aInfo.applicationInfo.uid, true);
1103 // If the process already started, we can't wait for debugger and shouldn't
1104 // modify the debug settings.
1105 // For non-persistent debug, normally we set the debug app here, and restores
1106 // to the original at attachApplication time. However, if the app process
1107 // already exists, we won't get another attachApplication, and the debug setting
1108 // never gets restored. Furthermore, if we get two such calls back-to-back,
1109 // both mOrigDebugApp and mDebugApp will become the same app, and it won't be
1110 // cleared even if we get attachApplication after the app process is killed.
1111 if (app == null || app.thread == null) {
1112 mService.setDebugApp(aInfo.processName, true, false);
1113 } else {
1114 Slog.w(TAG, "Ignoring waitForDebugger because process already exists");
1115 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001116 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001118 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1119 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1120 }
1121
Man Caocfa78b22015-06-11 20:14:34 -07001122 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1123 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1124 }
1125
1126 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001127 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001128 }
1129 }
1130 }
1131 return aInfo;
1132 }
1133
Todd Kennedy7440f172015-12-09 14:31:22 -08001134 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001135 return resolveIntent(intent, resolvedType, userId, 0);
1136 }
1137
1138 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001139 try {
1140 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001141 PackageManager.MATCH_DEFAULT_ONLY | flags
1142 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001143 } catch (RemoteException e) {
1144 }
1145 return null;
1146 }
1147
1148 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1149 ProfilerInfo profilerInfo, int userId) {
1150 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1151 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1152 }
1153
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001154 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1155 boolean andResume, boolean checkConfig) throws RemoteException {
1156
1157 if (!allPausedActivitiesComplete()) {
1158 // While there are activities pausing we skipping starting any new activities until
1159 // pauses are complete. NOTE: that we also do this for activities that are starting in
1160 // the paused state because they will first be resumed then paused on the client side.
1161 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1162 "realStartActivityLocked: Skipping start of r=" + r
1163 + " some activities pausing...");
1164 return false;
1165 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001166
Craig Mautner2568c3a2015-03-26 14:22:34 -07001167 if (andResume) {
1168 r.startFreezingScreenLocked(app, 0);
1169 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001170
Craig Mautner2568c3a2015-03-26 14:22:34 -07001171 // schedule launch ticks to collect information about slow apps.
1172 r.startLaunchTickingLocked();
1173 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001174
1175 // Have the window manager re-evaluate the orientation of
1176 // the screen based on the new activity order. Note that
1177 // as a result of this, it can call back into the activity
1178 // manager with a new orientation. We don't care about that,
1179 // because the activity is not currently running so we are
1180 // just restarting it anyway.
1181 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001182 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 mService.mConfiguration,
1184 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001185 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 }
1187
1188 r.app = app;
1189 app.waitingToKill = null;
1190 r.launchCount++;
1191 r.lastLaunchTime = SystemClock.uptimeMillis();
1192
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001193 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001194
1195 int idx = app.activities.indexOf(r);
1196 if (idx < 0) {
1197 app.activities.add(r);
1198 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001199 mService.updateLruProcessLocked(app, true, null);
1200 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001201
Craig Mautner15df08a2015-04-01 12:17:18 -07001202 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001203 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1204 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001205 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001206 }
1207
1208 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001209 try {
1210 if (app.thread == null) {
1211 throw new RemoteException();
1212 }
1213 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001214 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (andResume) {
1216 results = r.results;
1217 newIntents = r.newIntents;
1218 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001219 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1220 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1221 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001222 if (andResume) {
1223 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1224 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001225 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001227 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001228 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001229 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001231 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1232 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 r.sleeping = false;
1234 r.forceNewConfig = false;
1235 mService.showAskCompatModeDialogLocked(r);
1236 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001237 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1239 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1240 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001241 final String profileFile = mService.mProfileFile;
1242 if (profileFile != null) {
1243 ParcelFileDescriptor profileFd = mService.mProfileFd;
1244 if (profileFd != null) {
1245 try {
1246 profileFd = profileFd.dup();
1247 } catch (IOException e) {
1248 if (profileFd != null) {
1249 try {
1250 profileFd.close();
1251 } catch (IOException o) {
1252 }
1253 profileFd = null;
1254 }
1255 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001257
1258 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1259 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 }
1261 }
1262 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001263
Craig Mautner2568c3a2015-03-26 14:22:34 -07001264 if (andResume) {
1265 app.hasShownUi = true;
1266 app.pendingUiClean = true;
1267 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001268 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001270 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001271 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001273 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001274
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001275 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 // This may be a heavy-weight process! Note that the package
1277 // manager will ensure that only activity can run in the main
1278 // process of the .apk, which is the only thing that will be
1279 // considered heavy-weight.
1280 if (app.processName.equals(app.info.packageName)) {
1281 if (mService.mHeavyWeightProcess != null
1282 && mService.mHeavyWeightProcess != app) {
1283 Slog.w(TAG, "Starting new heavy weight process " + app
1284 + " when already running "
1285 + mService.mHeavyWeightProcess);
1286 }
1287 mService.mHeavyWeightProcess = app;
1288 Message msg = mService.mHandler.obtainMessage(
1289 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1290 msg.obj = r;
1291 mService.mHandler.sendMessage(msg);
1292 }
1293 }
1294
1295 } catch (RemoteException e) {
1296 if (r.launchFailed) {
1297 // This is the second time we failed -- finish activity
1298 // and give up.
1299 Slog.e(TAG, "Second failure launching "
1300 + r.intent.getComponent().flattenToShortString()
1301 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001302 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1304 "2nd-crash", false);
1305 return false;
1306 }
1307
1308 // This is the first time we failed -- restart process and
1309 // retry.
1310 app.activities.remove(r);
1311 throw e;
1312 }
1313
1314 r.launchFailed = false;
1315 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001316 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 }
1318
1319 if (andResume) {
1320 // As part of the process of launching, ActivityThread also performs
1321 // a resume.
1322 stack.minimalResumeActivityLocked(r);
1323 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001324 // This activity is not starting in the resumed state... which should look like we asked
1325 // it to pause+stop (but remain visible), and it has done so and reported back the
1326 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001327 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001328 "Moving to PAUSED: " + r + " (starting in paused state)");
1329 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 }
1331
1332 // Launch the new version setup screen if needed. We do this -after-
1333 // launching the initial activity (that is, home), so that it can have
1334 // a chance to initialize itself while in the background, making the
1335 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001336 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001337 mService.startSetupActivityLocked();
1338 }
1339
Dianne Hackborn465fa392014-09-14 14:21:18 -07001340 // Update any services we are bound to that might care about whether
1341 // their client may have activities.
1342 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1343
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 return true;
1345 }
1346
Craig Mautnere79d42682013-04-01 19:01:53 -07001347 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001348 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001349 // Is this activity's application already running?
1350 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001351 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001352
1353 r.task.stack.setLaunchTime(r);
1354
1355 if (app != null && app.thread != null) {
1356 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001357 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1358 || !"android".equals(r.info.packageName)) {
1359 // Don't add this if it is a platform component that is marked
1360 // to run in multiple processes, because this is actually
1361 // part of the framework so doesn't make sense to track as a
1362 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001363 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1364 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001365 }
George Mount2c92c972014-03-20 09:38:23 -07001366 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001367 return;
1368 } catch (RemoteException e) {
1369 Slog.w(TAG, "Exception when starting activity "
1370 + r.intent.getComponent().flattenToShortString(), e);
1371 }
1372
1373 // If a dead object exception was thrown -- fall through to
1374 // restart the application.
1375 }
1376
1377 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001378 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001379 }
1380
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001381 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001382 String resultWho, int requestCode, int callingPid, int callingUid,
1383 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001384 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001385 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1386 callingUid);
1387 if (startAnyPerm == PERMISSION_GRANTED) {
1388 return true;
1389 }
1390 final int componentRestriction = getComponentRestrictionForCallingPackage(
1391 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1392 final int actionRestriction = getActionRestrictionForCallingPackage(
1393 intent.getAction(), callingPackage, callingPid, callingUid);
1394 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1395 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1396 if (resultRecord != null) {
1397 resultStack.sendActivityResultLocked(-1,
1398 resultRecord, resultWho, requestCode,
1399 Activity.RESULT_CANCELED, null);
1400 }
1401 final String msg;
1402 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1403 msg = "Permission Denial: starting " + intent.toString()
1404 + " from " + callerApp + " (pid=" + callingPid
1405 + ", uid=" + callingUid + ")" + " with revoked permission "
1406 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1407 } else if (!aInfo.exported) {
1408 msg = "Permission Denial: starting " + intent.toString()
1409 + " from " + callerApp + " (pid=" + callingPid
1410 + ", uid=" + callingUid + ")"
1411 + " not exported from uid " + aInfo.applicationInfo.uid;
1412 } else {
1413 msg = "Permission Denial: starting " + intent.toString()
1414 + " from " + callerApp + " (pid=" + callingPid
1415 + ", uid=" + callingUid + ")"
1416 + " requires " + aInfo.permission;
1417 }
1418 Slog.w(TAG, msg);
1419 throw new SecurityException(msg);
1420 }
1421
1422 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1423 final String message = "Appop Denial: starting " + intent.toString()
1424 + " from " + callerApp + " (pid=" + callingPid
1425 + ", uid=" + callingUid + ")"
1426 + " requires " + AppOpsManager.permissionToOp(
1427 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1428 Slog.w(TAG, message);
1429 return false;
1430 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1431 final String message = "Appop Denial: starting " + intent.toString()
1432 + " from " + callerApp + " (pid=" + callingPid
1433 + ", uid=" + callingUid + ")"
1434 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1435 Slog.w(TAG, message);
1436 return false;
1437 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001438 if (options != null && options.getLaunchTaskId() != -1) {
1439 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1440 callingPid, callingUid);
1441 if (startInTaskPerm != PERMISSION_GRANTED) {
1442 final String msg = "Permission Denial: starting " + intent.toString()
1443 + " from " + callerApp + " (pid=" + callingPid
1444 + ", uid=" + callingUid + ") with launchTaskId="
1445 + options.getLaunchTaskId();
1446 Slog.w(TAG, msg);
1447 throw new SecurityException(msg);
1448 }
1449 }
1450
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001451 return true;
1452 }
1453
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001454 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001455 final long identity = Binder.clearCallingIdentity();
1456 try {
1457 return UserManager.get(mService.mContext).getUserInfo(userId);
1458 } finally {
1459 Binder.restoreCallingIdentity(identity);
1460 }
1461 }
1462
Svet Ganov99b60432015-06-27 13:15:22 -07001463 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001464 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001465 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1466 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001467 == PackageManager.PERMISSION_DENIED) {
1468 return ACTIVITY_RESTRICTION_PERMISSION;
1469 }
1470
Christopher Tateff7add02015-08-17 10:23:22 -07001471 if (activityInfo.permission == null) {
1472 return ACTIVITY_RESTRICTION_NONE;
1473 }
1474
Svet Ganov99b60432015-06-27 13:15:22 -07001475 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1476 if (opCode == AppOpsManager.OP_NONE) {
1477 return ACTIVITY_RESTRICTION_NONE;
1478 }
1479
1480 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1481 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001482 if (!ignoreTargetSecurity) {
1483 return ACTIVITY_RESTRICTION_APPOP;
1484 }
Svet Ganov99b60432015-06-27 13:15:22 -07001485 }
1486
1487 return ACTIVITY_RESTRICTION_NONE;
1488 }
1489
Svetoslav7008b512015-06-24 18:47:07 -07001490 private int getActionRestrictionForCallingPackage(String action,
1491 String callingPackage, int callingPid, int callingUid) {
1492 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001493 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001494 }
1495
1496 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1497 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001498 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001499 }
1500
1501 final PackageInfo packageInfo;
1502 try {
1503 packageInfo = mService.mContext.getPackageManager()
1504 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1505 } catch (PackageManager.NameNotFoundException e) {
1506 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001507 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001508 }
1509
1510 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001511 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001512 }
1513
1514 if (mService.checkPermission(permission, callingPid, callingUid) ==
1515 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001516 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001517 }
1518
1519 final int opCode = AppOpsManager.permissionToOpCode(permission);
1520 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001521 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001522 }
1523
1524 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1525 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001526 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001527 }
1528
Svet Ganov99b60432015-06-27 13:15:22 -07001529 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001530 }
1531
Wale Ogunwaled046a012015-12-24 13:05:59 -08001532 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001533 if (r == null) {
1534 // Not sure what you are trying to do, but it is not going to work...
1535 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001536 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001537 final TaskRecord task = r.task;
1538 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001539 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001540 return false;
1541 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001542 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001543 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001544 }
1545
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001546 void setLaunchSource(int uid) {
1547 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1548 }
1549
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001550 void acquireLaunchWakelock() {
1551 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1552 throw new IllegalStateException("Calling must be system uid");
1553 }
1554 mLaunchingActivity.acquire();
1555 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1556 // To be safe, don't allow the wake lock to be held for too long.
1557 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1558 }
1559 }
1560
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001561 /**
1562 * Called when the frontmost task is idle.
1563 * @return the state of mService.mBooting before this was called.
1564 */
1565 private boolean checkFinishBootingLocked() {
1566 final boolean booting = mService.mBooting;
1567 boolean enableScreen = false;
1568 mService.mBooting = false;
1569 if (!mService.mBooted) {
1570 mService.mBooted = true;
1571 enableScreen = true;
1572 }
1573 if (booting || enableScreen) {
1574 mService.postFinishBooting(booting, enableScreen);
1575 }
1576 return booting;
1577 }
1578
Craig Mautnerf3333272013-04-22 10:55:53 -07001579 // Checked.
1580 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1581 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001582 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001583
Craig Mautnerf3333272013-04-22 10:55:53 -07001584 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001585 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001586 int NS = 0;
1587 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001588 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001589 boolean activityRemoved = false;
1590
Wale Ogunwale7d701172015-03-11 15:36:30 -07001591 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001593 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1594 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001595 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1596 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001597 if (fromTimeout) {
1598 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001599 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001600
1601 // This is a hack to semi-deal with a race condition
1602 // in the client where it can be constructed with a
1603 // newer configuration from when we asked it to launch.
1604 // We'll update with whatever configuration it now says
1605 // it used to launch.
1606 if (config != null) {
1607 r.configuration = config;
1608 }
1609
1610 // We are now idle. If someone is waiting for a thumbnail from
1611 // us, we can now deliver.
1612 r.idle = true;
1613
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001614 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001615 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001616 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001617 }
1618 }
1619
1620 if (allResumedActivitiesIdle()) {
1621 if (r != null) {
1622 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001623 }
1624
1625 if (mLaunchingActivity.isHeld()) {
1626 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1627 if (VALIDATE_WAKE_LOCK_CALLER &&
1628 Binder.getCallingUid() != Process.myUid()) {
1629 throw new IllegalStateException("Calling must be system uid");
1630 }
1631 mLaunchingActivity.release();
1632 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001633 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001634 }
1635
1636 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001637 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001638 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001639 if ((NF = mFinishingActivities.size()) > 0) {
1640 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001641 mFinishingActivities.clear();
1642 }
1643
Craig Mautnerf3333272013-04-22 10:55:53 -07001644 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001645 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001646 mStartingUsers.clear();
1647 }
1648
Craig Mautnerf3333272013-04-22 10:55:53 -07001649 // Stop any activities that are scheduled to do so but have been
1650 // waiting for the next one to start.
1651 for (int i = 0; i < NS; i++) {
1652 r = stops.get(i);
1653 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001654 if (stack != null) {
1655 if (r.finishing) {
1656 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1657 } else {
1658 stack.stopActivityLocked(r);
1659 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001660 }
1661 }
1662
1663 // Finish any activities that are scheduled to do so but have been
1664 // waiting for the next one to start.
1665 for (int i = 0; i < NF; i++) {
1666 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001667 final ActivityStack stack = r.task.stack;
1668 if (stack != null) {
1669 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1670 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001671 }
1672
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001673 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001674 // Complete user switch
1675 if (startingUsers != null) {
1676 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001677 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001678 }
1679 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001680 }
1681
1682 mService.trimApplications();
1683 //dump();
1684 //mWindowManager.dump();
1685
Craig Mautnerf3333272013-04-22 10:55:53 -07001686 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001687 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001688 }
1689
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001690 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001691 }
1692
Craig Mautner8e569572013-10-11 17:36:59 -07001693 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001694 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001695 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1696 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001697 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1698 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1699 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001700 }
Craig Mautner19091252013-10-05 00:03:53 -07001701 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001702 }
1703
1704 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1706 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1708 stacks.get(stackNdx).closeSystemDialogsLocked();
1709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001710 }
1711 }
1712
Craig Mautner93529a42013-10-04 15:03:13 -07001713 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001714 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001715 }
1716
Craig Mautner8d341ef2013-03-26 09:03:27 -07001717 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001718 * Update the last used stack id for non-current user (current user's last
1719 * used stack is the focused stack)
1720 */
1721 void updateUserStackLocked(int userId, ActivityStack stack) {
1722 if (userId != mCurrentUser) {
1723 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1724 }
1725 }
1726
1727 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001728 * @return true if some activity was finished (or would have finished if doit were true).
1729 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001730 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1731 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001732 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001736 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001737 if (stack.finishDisabledPackageActivitiesLocked(
1738 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001739 didSomething = true;
1740 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001741 }
1742 }
1743 return didSomething;
1744 }
1745
Dianne Hackborna413dc02013-07-12 12:02:55 -07001746 void updatePreviousProcessLocked(ActivityRecord r) {
1747 // Now that this process has stopped, we may want to consider
1748 // it to be the previous app to try to keep around in case
1749 // the user wants to return to it.
1750
1751 // First, found out what is currently the foreground app, so that
1752 // we don't blow away the previous app if this activity is being
1753 // hosted by the process that is actually still the foreground.
1754 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001755 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1756 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001757 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1758 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001759 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001760 if (stack.mResumedActivity != null) {
1761 fgApp = stack.mResumedActivity.app;
1762 } else if (stack.mPausingActivity != null) {
1763 fgApp = stack.mPausingActivity.app;
1764 }
1765 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001766 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001767 }
1768 }
1769
1770 // Now set this one as the previous process, only if that really
1771 // makes sense to.
1772 if (r.app != null && fgApp != null && r.app != fgApp
1773 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001774 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001775 mService.mPreviousProcess = r.app;
1776 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1777 }
1778 }
1779
Wale Ogunwaled046a012015-12-24 13:05:59 -08001780 boolean resumeFocusedStackTopActivityLocked() {
1781 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001782 }
1783
Wale Ogunwaled046a012015-12-24 13:05:59 -08001784 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001785 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001786 if (targetStack != null && isFocusedStack(targetStack)) {
1787 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001788 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001789 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1790 if (r == null || r.state != RESUMED) {
1791 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1792 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001793 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001794 }
1795
Todd Kennedy39bfee52016-02-24 10:28:21 -08001796 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1800 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1801 }
1802 }
1803 }
1804
Adrian Roos20d7df32016-01-12 18:59:43 +01001805 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1806 TaskRecord finishedTask = null;
1807 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001808 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1809 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001810 final int numStacks = stacks.size();
1811 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1812 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001813 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1814 if (stack == focusedStack || finishedTask == null) {
1815 finishedTask = t;
1816 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001817 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001818 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001819 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001820 }
1821
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001822 void finishVoiceTask(IVoiceInteractionSession session) {
1823 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1824 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1825 final int numStacks = stacks.size();
1826 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1827 final ActivityStack stack = stacks.get(stackNdx);
1828 stack.finishVoiceTask(session);
1829 }
1830 }
1831 }
1832
Andrii Kulianc27916642016-04-12 17:59:27 -07001833 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1834 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001835 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1836 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001837 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001838 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1839 // Caller wants the home activity moved with it. To accomplish this,
1840 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001841 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001842 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001843 if (task.stack == null) {
1844 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1845 + task + " to front. Stack is null");
1846 return;
1847 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001848
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001849 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001850 int stackId = options.getLaunchStackId();
1851 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001852 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001853 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001854 if (stackId == INVALID_STACK_ID) {
1855 stackId = task.getLaunchStackId();
1856 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001857 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001858 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1859 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1860 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001861 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1862 // still need moveTaskToFrontLocked() below for any transition settings.
1863 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001864 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1865 resizeStackLocked(stackId, bounds,
1866 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001867 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001868 } else {
1869 // WM resizeTask must be done after the task is moved to the correct stack,
1870 // because Task's setBounds() also updates dim layer's bounds, but that has
1871 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001872 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001873 false /* relayout */, false /* forced */);
1874 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001875 }
1876 }
1877
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001878 final ActivityRecord r = task.getTopActivity();
1879 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1880 r == null ? null : r.appTimeTracker, reason);
1881
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001882 if (DEBUG_STACK) Slog.d(TAG_STACK,
1883 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001884
Andrii Kulianc27916642016-04-12 17:59:27 -07001885 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1886 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001887 }
1888
Wale Ogunwale854809c2015-12-27 16:18:19 -08001889 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001890 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001891 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001892 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001893 return false;
1894 }
1895 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1896 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001897 }
1898
Craig Mautner967212c2013-04-13 21:10:58 -07001899 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001900 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001901 }
1902
1903 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001904 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1905 if (activityContainer != null) {
1906 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001907 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001908 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001909 return null;
1910 }
1911 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001912 }
1913
Craig Mautner967212c2013-04-13 21:10:58 -07001914 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001915 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1917 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001918 }
1919 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001920 }
1921
Craig Mautner4a1cb222013-12-04 16:14:06 -08001922 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001923 ActivityRecord homeActivity = getHomeActivity();
1924 if (homeActivity != null) {
1925 return homeActivity.appToken;
1926 }
1927 return null;
1928 }
1929
1930 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001931 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001932 }
1933
1934 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1936 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1937 final TaskRecord task = tasks.get(taskNdx);
1938 if (task.isHomeTask()) {
1939 final ArrayList<ActivityRecord> activities = task.mActivities;
1940 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1941 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001942 if (r.isHomeActivity()
1943 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001944 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001945 }
1946 }
1947 }
1948 }
1949 return null;
1950 }
1951
Chong Zhangb15758a2015-11-17 12:12:03 -08001952 /**
1953 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1954 * the docked stack itself, or if it's side-by-side to the docked stack.
1955 */
1956 boolean isStackDockedInEffect(int stackId) {
1957 return stackId == DOCKED_STACK_ID ||
1958 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1959 }
1960
Todd Kennedyca4d8422015-01-15 15:19:22 -08001961 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001962 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001963 ActivityContainer activityContainer =
1964 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001965 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001966 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1967 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001968 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001969 return activityContainer;
1970 }
1971
Craig Mautner34b73df2014-01-12 21:11:08 -08001972 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001973 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1974 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1975 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001976 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1977 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001978 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001979 }
1980 }
1981
Craig Mautner95da1082014-02-24 17:54:35 -08001982 void deleteActivityContainer(IActivityContainer container) {
1983 ActivityContainer activityContainer = (ActivityContainer)container;
1984 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001985 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1986 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001987 final int stackId = activityContainer.mStackId;
1988 mActivityContainers.remove(stackId);
1989 mWindowManager.removeStack(stackId);
1990 }
1991 }
1992
Jorim Jaggidc249c42015-12-15 14:57:31 -08001993 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001994 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08001995 if (stackId == DOCKED_STACK_ID) {
1996 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
1997 preserveWindows);
1998 return;
1999 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002000 final ActivityStack stack = getStack(stackId);
2001 if (stack == null) {
2002 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2003 return;
2004 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002005
Jorim Jaggidc249c42015-12-15 14:57:31 -08002006 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002007 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2008 // stack size changing so things don't get out of sync.
2009 return;
2010 }
2011
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002012 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002013 mWindowManager.deferSurfaceLayout();
2014 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002015 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002016 if (!deferResume) {
2017 ensureConfigurationAndResume(
2018 stack, stack.topRunningActivityLocked(), preserveWindows);
2019 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002020 } finally {
2021 mWindowManager.continueSurfaceLayout();
2022 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002023 }
2024 }
2025
Jorim Jaggi192086e2016-03-11 17:17:03 +01002026 void deferUpdateBounds(int stackId) {
2027 final ActivityStack stack = getStack(stackId);
2028 if (stack != null) {
2029 stack.deferUpdateBounds();
2030 }
2031 }
2032
2033 void continueUpdateBounds(int stackId) {
2034 final ActivityStack stack = getStack(stackId);
2035 if (stack != null) {
2036 stack.continueUpdateBounds();
2037 }
2038 }
2039
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002040 void notifyAppTransitionDone() {
2041 continueUpdateBounds(HOME_STACK_ID);
2042 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2043 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
2044 if (anyTaskForIdLocked(taskId) != null) {
2045 mWindowManager.setTaskDockedResizing(taskId, false);
2046 }
2047 }
2048 mResizingTasksDuringAnimation.clear();
2049 }
2050
Jorim Jaggi192086e2016-03-11 17:17:03 +01002051 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002052 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002053 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002054
Jorim Jaggi192086e2016-03-11 17:17:03 +01002055 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2056 return;
2057 }
2058
Jorim Jaggidc249c42015-12-15 14:57:31 -08002059 mTmpBounds.clear();
2060 mTmpConfigs.clear();
2061 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002062 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002063 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2064 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002065 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002066 final TaskRecord task = tasks.get(i);
2067 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002068 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2069 // For freeform stack we don't adjust the size of the tasks to match that
2070 // of the stack, but we do try to make sure the tasks are still contained
2071 // with the bounds of the stack.
2072 tempRect2.set(task.mBounds);
2073 fitWithinBounds(tempRect2, bounds);
2074 task.updateOverrideConfiguration(tempRect2);
2075 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002076 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002077 }
2078 }
2079
2080 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2081 mTmpBounds.put(task.taskId, task.mBounds);
2082 if (tempTaskInsetBounds != null) {
2083 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2084 }
2085 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002086
2087 // We might trigger a configuration change. Save the current task bounds for freezing.
2088 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002089 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2090 mTmpBounds, mTmpInsetBounds);
2091 stack.setBounds(bounds);
2092 }
2093
2094 private void ensureConfigurationAndResume(ActivityStack stack, ActivityRecord r,
2095 boolean preserveWindows) {
Jorim Jaggic3fb3142016-02-04 19:49:28 -08002096 if (r == null || !r.visible) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002097 return;
2098 }
2099 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0,
2100 preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002101 if (!updated) {
2102 resumeFocusedStackTopActivityLocked();
2103 }
2104 }
2105
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002106 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2107 final ActivityStack stack = getStack(fromStackId);
2108 if (stack == null) {
2109 return;
2110 }
2111
2112 mWindowManager.deferSurfaceLayout();
2113 try {
2114 if (fromStackId == DOCKED_STACK_ID) {
2115
2116 // We are moving all tasks from the docked stack to the fullscreen stack,
2117 // which is dismissing the docked stack, so resize all other stacks to
2118 // fullscreen here already so we don't end up with resize trashing.
2119 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2120 if (StackId.isResizeableByDockedStack(i)) {
2121 ActivityStack otherStack = getStack(i);
2122 if (otherStack != null) {
2123 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2124 true /* allowResizeInDockedMode */, DEFER_RESUME);
2125 }
2126 }
2127 }
2128
2129 // Also disable docked stack resizing since we have manually adjusted the
2130 // size of other stacks above and we don't want to trigger a docked stack
2131 // resize when we remove task from it below and it is detached from the
2132 // display because it no longer contains any tasks.
2133 mAllowDockedStackResize = false;
2134 }
2135 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2136 final int size = tasks.size();
2137 if (onTop) {
2138 for (int i = 0; i < size; i++) {
2139 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002140 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002141 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002142 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002143
2144 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2145 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002146 } else {
2147 for (int i = size - 1; i >= 0; i--) {
2148 positionTaskInStackLocked(tasks.get(i).taskId,
2149 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2150 }
2151 }
2152 } finally {
2153 mAllowDockedStackResize = true;
2154 mWindowManager.continueSurfaceLayout();
2155 }
2156 }
2157
Jorim Jaggidc249c42015-12-15 14:57:31 -08002158 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2159 Rect tempDockedTaskInsetBounds,
2160 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002161
2162 if (!mAllowDockedStackResize) {
2163 // Docked stack resize currently disabled.
2164 return;
2165 }
2166
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2168 if (stack == null) {
2169 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2170 return;
2171 }
2172
2173 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2174 mWindowManager.deferSurfaceLayout();
2175 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002176 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2177 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 ActivityRecord r = stack.topRunningActivityLocked();
2179 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2180 tempDockedTaskInsetBounds);
2181
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002182 // TODO: Checking for isAttached might not be needed as if the user passes in null
2183 // dockedBounds then they want the docked stack to be dismissed.
2184 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2185 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186 // In this case we make all other static stacks fullscreen and move all
2187 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002188 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002189
2190 // stack shouldn't contain anymore activities, so nothing to resume.
2191 r = null;
2192 } else {
2193 // Docked stacks occupy a dedicated region on screen so the size of all other
2194 // static stacks need to be adjusted so they don't overlap with the docked stack.
2195 // We get the bounds to use from window manager which has been adjusted for any
2196 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002197 mWindowManager.getStackDockedModeBounds(
2198 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002199 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002200 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2201 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2202 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002203 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002204 }
2205 }
2206 }
2207 ensureConfigurationAndResume(stack, r, preserveWindows);
2208 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002209 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002210 mWindowManager.continueSurfaceLayout();
2211 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2212 }
2213
2214 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2215 tempDockedTaskBounds != null
2216 || tempDockedTaskInsetBounds != null
2217 || tempOtherTaskBounds != null
2218 || tempOtherTaskInsetBounds != null);
2219 }
2220
Robert Carr0d00c2e2016-02-29 17:45:02 -08002221 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2222 final ActivityStack stack = getStack(PINNED_STACK_ID);
2223 if (stack == null) {
2224 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2225 return;
2226 }
2227 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2228 mWindowManager.deferSurfaceLayout();
2229 try {
2230 ActivityRecord r = stack.topRunningActivityLocked();
2231 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2232 null);
2233 ensureConfigurationAndResume(stack, r, false);
2234 } finally {
2235 mWindowManager.continueSurfaceLayout();
2236 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2237 }
2238 }
2239
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002240 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2241 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002242 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002243 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002244 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002245 }
2246
Chong Zhang87b21722015-09-21 15:39:51 -07002247 // If this is a forced resize, let it go through even if the bounds is not changing,
2248 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002249 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002250 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002251 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002252 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002253 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002254 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002255
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002256 if (!mWindowManager.isValidTaskId(task.taskId)) {
2257 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002258 // All we can do for now is update the bounds so it can be used when the task is
2259 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002260 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002261 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2262 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002263 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002264 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002265 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002266 }
2267
Chong Zhang6de2ae82015-09-30 18:25:21 -07002268 // Do not move the task to another stack here.
2269 // This method assumes that the task is already placed in the right stack.
2270 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002271
Chong Zhang6de2ae82015-09-30 18:25:21 -07002272 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002273
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002274 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002275 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002276 // way and the activity was kept the way it was. If it's false, it means the activity had
2277 // to be relaunched due to configuration change.
2278 boolean kept = true;
2279 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002280 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002281 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002282 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002283 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002284
2285 if (!deferResume) {
2286
2287 // All other activities must be made visible with their correct configuration.
2288 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2289 if (!kept) {
2290 resumeFocusedStackTopActivityLocked();
2291 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002292 }
2293 }
2294 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002295 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002296
2297 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002298 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002299 }
2300
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002301 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002302 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2303 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002304 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002305 }
2306
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002307 ActivityContainer activityContainer = new ActivityContainer(stackId);
2308 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002309 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002310 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002311 }
2312
2313 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002314 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002315 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2316 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002317 break;
2318 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002319 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002320 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002321 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002322 }
2323
Chong Zhang5dcb2752015-08-18 13:50:26 -07002324 /**
2325 * Restores a recent task to a stack
2326 * @param task The recent task to be restored.
2327 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002328 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002329 * @return true if the task has been restored successfully.
2330 */
2331 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2332 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002333 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002334 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2335 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2336 // Put it in the fullscreen stack.
2337 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002338 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002339
Wale Ogunwale706ed792015-08-02 10:29:44 -07002340 if (task.stack != null) {
2341 // Task has already been restored once. See if we need to do anything more
2342 if (task.stack.mStackId == stackId) {
2343 // Nothing else to do since it is already restored in the right stack.
2344 return true;
2345 }
2346 // Remove current stack association, so we can re-associate the task with the
2347 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002348 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002349 }
2350
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002351 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002352 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002353
2354 if (stack == null) {
2355 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002356 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2357 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002358 return false;
2359 }
2360
Wale Ogunwale5f986092015-12-04 15:35:38 -08002361 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002362 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2363 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002364 final ArrayList<ActivityRecord> activities = task.mActivities;
2365 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002366 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002367 }
2368 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002369 }
2370
Wale Ogunwale040b4702015-08-06 18:10:50 -07002371 /**
2372 * Moves the specified task record to the input stack id.
2373 * WARNING: This method performs an unchecked/raw move of the task and
2374 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002375 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002376 * @param task Task to move.
2377 * @param stackId Id of stack to move task to.
2378 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002379 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002380 * @param reason Reason the task is been moved.
2381 * @return The stack the task was moved to.
2382 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002383 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002384 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002385
2386 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2387 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2388 + "support multi-window task=" + task + " to stackId=" + stackId);
2389 }
2390
Wale Ogunwale06579d62016-04-30 15:29:06 -07002391 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002392 final ActivityStack prevStack = task.stack;
2393 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002394 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002395 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2396 // Whenever we are moving the top activity from the front stack we want to make sure to move
2397 // the stack to the front.
2398 final boolean wasFront = isFrontStack(prevStack)
2399 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002400
Chong Zhangd545dce2016-02-29 18:09:17 -08002401 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002402 // We don't allow moving a unresizeable task to the docked stack since the docked
2403 // stack is used for split-screen mode and will cause things like the docked divider to
2404 // show up. We instead leave the task in its current stack or move it to the fullscreen
2405 // stack if it isn't currently in a stack.
2406 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002407 Slog.w(TAG, "Can not move unresizeable task=" + task
2408 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2409 }
2410
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002411 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2412 // if a docked stack is created below which will lead to the stack we are moving from and
2413 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002414 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002415 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002416 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002417 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002418 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002419
2420 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002421 // move focus to the new stack by moving the stack to the front.
2422 stack.moveToFrontAndResumeStateIfNeeded(
2423 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002424
Wale Ogunwale040b4702015-08-06 18:10:50 -07002425 return stack;
2426 }
2427
Chong Zhange4fbd322016-03-01 14:44:03 -08002428 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002429 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002430 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2431 false /* deferResume */);
2432 }
2433
2434 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2435 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002436 final TaskRecord task = anyTaskForIdLocked(taskId);
2437 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002438 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002439 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002440 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002441
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002442 if (task.stack != null && task.stack.mStackId == stackId) {
2443 // You are already in the right stack silly...
2444 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002445 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002446 }
2447
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002448 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2449 throw new IllegalArgumentException("moveTaskToStack:"
2450 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2451 }
2452
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002453 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002454 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002455 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002456 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002457 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002458 // We are about to relaunch the activity because its configuration changed due to
2459 // being maximized, i.e. size change. The activity will first remove the old window
2460 // and then add a new one. This call will tell window manager about this, so it can
2461 // preserve the old window until the new one is drawn. This prevents having a gap
2462 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002463 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002464 // Note here we always set the replacing window first, as the flags might be needed
2465 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002466 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002467 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002468
2469 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002470 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002471 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002472 try {
2473 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002474 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002475 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002476
Wale Ogunwale961f4852016-02-01 20:25:54 -08002477 if (!animate) {
2478 stack.mNoAnimActivities.add(topActivity);
2479 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002480
Wale Ogunwale961f4852016-02-01 20:25:54 -08002481 // We might trigger a configuration change. Save the current task bounds for freezing.
2482 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2483
2484 // Make sure the task has the appropriate bounds/size for the stack it is in.
2485 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002486 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2487 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002488 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2489 Rect bounds = task.getLaunchBounds();
2490 if (bounds == null) {
2491 stack.layoutTaskInStack(task, null);
2492 bounds = task.mBounds;
2493 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002494 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2495 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002496 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002497 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2498 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002499 }
2500 } finally {
2501 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002502 }
2503
2504 if (mightReplaceWindow) {
2505 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2506 // window), we need to clear the replace window settings. Otherwise, we schedule a
2507 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002508 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002509 }
2510
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002511 if (!deferResume) {
2512
2513 // The task might have already been running and its visibility needs to be synchronized with
2514 // the visibility of the stack / windows.
2515 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2516 resumeFocusedStackTopActivityLocked();
2517 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002518
Jorim Jaggid53f0922016-04-06 22:16:23 -07002519 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002520
2521 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002522 }
2523
Wale Ogunwale079a0042015-10-24 11:44:07 -07002524 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2525 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2526 if (stack == null) {
2527 throw new IllegalArgumentException(
2528 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2529 }
2530
2531 final ActivityRecord r = stack.topRunningActivityLocked();
2532 if (r == null) {
2533 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2534 + " in stack=" + stack);
2535 return false;
2536 }
2537
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002538 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002539 Slog.w(TAG,
2540 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002541 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002542 return false;
2543 }
2544
Wale Ogunwale480dca02016-02-06 13:58:29 -08002545 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002546 return true;
2547 }
2548
Wale Ogunwale480dca02016-02-06 13:58:29 -08002549 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2550 mWindowManager.deferSurfaceLayout();
2551 try {
2552 final TaskRecord task = r.task;
2553
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002554 if (r == task.stack.getVisibleBehindActivity()) {
2555 // An activity can't be pinned and visible behind at the same time. Go ahead and
2556 // release it from been visible behind before pinning.
2557 requestVisibleBehindLocked(r, false);
2558 }
2559
Wale Ogunwale480dca02016-02-06 13:58:29 -08002560 // Need to make sure the pinned stack exist so we can resize it below...
2561 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2562
2563 // Resize the pinned stack to match the current size of the task the activity we are
2564 // going to be moving is currently contained in. We do this to have the right starting
2565 // animation bounds for the pinned stack to the desired bounds the caller wants.
2566 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2567 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002568 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002569
2570 if (task.mActivities.size() == 1) {
2571 // There is only one activity in the task. So, we can just move the task over to
2572 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002573 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2574 // Move the home stack forward if the task we just moved to the pinned stack
2575 // was launched from home so home should be visible behind it.
2576 moveHomeStackToFront(reason);
2577 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002578 moveTaskToStackLocked(
2579 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2580 } else {
2581 stack.moveActivityToStack(r);
2582 }
2583 } finally {
2584 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002585 }
2586
Wale Ogunwale480dca02016-02-06 13:58:29 -08002587 // The task might have already been running and its visibility needs to be synchronized
2588 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002589 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002590 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002591
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002592 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002593 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002594 }
2595
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002596 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2597 final TaskRecord task = anyTaskForIdLocked(taskId);
2598 if (task == null) {
2599 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2600 return;
2601 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002602 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2603
2604 task.updateOverrideConfigurationForStack(stack);
2605
2606 mWindowManager.positionTaskInStack(
2607 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002608 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002609 // The task might have already been running and its visibility needs to be synchronized with
2610 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002611 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002612 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002613 }
2614
Craig Mautnerac6f8432013-07-17 13:24:59 -07002615 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002616 mTmpFindTaskResult.r = null;
2617 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002618 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002619 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2620 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002621 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2622 final ActivityStack stack = stacks.get(stackNdx);
2623 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002624 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002625 continue;
2626 }
2627 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002628 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2629 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002630 continue;
2631 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002632 stack.findTaskLocked(r, mTmpFindTaskResult);
2633 // It is possible to have task in multiple stacks with the same root affinity.
2634 // If the match we found was based on root affinity we keep on looking to see if
2635 // there is a better match in another stack. We eventually return the match based
2636 // on root affinity if we don't find a better match.
2637 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2638 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002639 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002640 }
2641 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002642 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2643 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002644 }
2645
2646 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2650 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2651 if (ar != null) {
2652 return ar;
2653 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002654 }
2655 }
2656 return null;
2657 }
2658
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002660 scheduleSleepTimeout();
2661 if (!mGoingToSleep.isHeld()) {
2662 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002663 if (mLaunchingActivity.isHeld()) {
2664 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2665 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002666 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002667 mLaunchingActivity.release();
2668 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002669 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002671 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002672 }
2673
2674 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002675 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002676
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002677 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 final long endTime = System.currentTimeMillis() + timeout;
2679 while (true) {
2680 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002681 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2682 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002683 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2684 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2685 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002686 }
2687 if (cantShutdown) {
2688 long timeRemaining = endTime - System.currentTimeMillis();
2689 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002690 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002692 } catch (InterruptedException e) {
2693 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002694 } else {
2695 Slog.w(TAG, "Activity manager shutdown timed out");
2696 timedout = true;
2697 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002698 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 } else {
2700 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002701 }
2702 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002703
2704 // Force checkReadyForSleep to complete.
2705 mSleepTimeout = true;
2706 checkReadyForSleepLocked();
2707
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 return timedout;
2709 }
2710
2711 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002712 removeSleepTimeouts();
2713 if (mGoingToSleep.isHeld()) {
2714 mGoingToSleep.release();
2715 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2717 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2719 final ActivityStack stack = stacks.get(stackNdx);
2720 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002721 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002722 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002723 }
Craig Mautner5314a402013-09-26 12:40:16 -07002724 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002725 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002726 mGoingToSleepActivities.clear();
2727 }
2728
2729 void activitySleptLocked(ActivityRecord r) {
2730 mGoingToSleepActivities.remove(r);
2731 checkReadyForSleepLocked();
2732 }
2733
2734 void checkReadyForSleepLocked() {
2735 if (!mService.isSleepingOrShuttingDown()) {
2736 // Do not care.
2737 return;
2738 }
2739
2740 if (!mSleepTimeout) {
2741 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2743 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2745 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2746 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 }
2748
2749 if (mStoppingActivities.size() > 0) {
2750 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002751 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002752 + mStoppingActivities.size() + " activities");
2753 scheduleIdleLocked();
2754 dontSleep = true;
2755 }
2756
2757 if (mGoingToSleepActivities.size() > 0) {
2758 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002759 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002760 + mGoingToSleepActivities.size() + " activities");
2761 dontSleep = true;
2762 }
2763
2764 if (dontSleep) {
2765 return;
2766 }
2767 }
2768
Craig Mautnere0a38842013-12-16 16:14:02 -08002769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2770 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002771 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2772 stacks.get(stackNdx).goToSleep();
2773 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002774 }
2775
2776 removeSleepTimeouts();
2777
2778 if (mGoingToSleep.isHeld()) {
2779 mGoingToSleep.release();
2780 }
2781 if (mService.mShuttingDown) {
2782 mService.notifyAll();
2783 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 }
2785
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002786 boolean reportResumedActivityLocked(ActivityRecord r) {
2787 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002788 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002789 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002790 }
2791 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002792 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002793 mWindowManager.executeAppTransition();
2794 return true;
2795 }
2796 return false;
2797 }
2798
Craig Mautner8d341ef2013-03-26 09:03:27 -07002799 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002800 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2801 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002802 int stackNdx = stacks.size() - 1;
2803 while (stackNdx >= 0) {
2804 stacks.get(stackNdx).handleAppCrashLocked(app);
2805 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002806 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002807 }
2808 }
2809
Jose Lima4b6c6692014-08-12 17:41:12 -07002810 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002811 final ActivityStack stack = r.task.stack;
2812 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002813 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2814 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002815 return false;
2816 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002817
2818 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2819 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2820 + " visible=" + visible + " stackId=" + stack.mStackId
2821 + " can't contain visible behind activities");
2822 return false;
2823 }
2824
Jose Lima4b6c6692014-08-12 17:41:12 -07002825 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2827 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002828
2829 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002830 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002831 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002832 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002833 return true;
2834 }
2835
2836 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002837 if (visible && top.fullscreen) {
2838 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002839 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2840 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2841 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2842 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002843 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002844 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2845 // Only the activity set as currently visible behind should actively reset its
2846 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002847 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2848 "requestVisibleBehind: returning visible=" + visible
2849 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2850 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002851 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002852 }
2853
Jose Lima4b6c6692014-08-12 17:41:12 -07002854 stack.setVisibleBehindActivity(visible ? r : null);
2855 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002856 // If there is a translucent home activity, we need to force it stop being translucent,
2857 // because we can't depend on the application to necessarily perform that operation.
2858 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002859 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002860 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002861 mService.convertFromTranslucent(next.appToken);
2862 }
2863 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002864 if (top.app != null && top.app.thread != null) {
2865 // Notify the top app of the change.
2866 try {
2867 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2868 } catch (RemoteException e) {
2869 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002870 }
2871 return true;
2872 }
2873
Craig Mautnerbb742462014-07-07 15:28:55 -07002874 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2875 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002876 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002877 final ActivityStack stack = task.stack;
2878
2879 r.mLaunchTaskBehind = false;
2880 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002881 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002882 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002883 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002884
2885 // When launching tasks behind, update the last active time of the top task after the new
2886 // task has been shown briefly
2887 final ActivityRecord top = stack.topActivity();
2888 if (top != null) {
2889 top.task.touchActiveTime();
2890 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002891 }
2892
2893 void scheduleLaunchTaskBehindComplete(IBinder token) {
2894 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2895 }
2896
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002897 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2898 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002899 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2901 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002902 final int topStackNdx = stacks.size() - 1;
2903 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2904 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002905 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002906 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002908 }
2909
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002910 void invalidateTaskLayers() {
2911 mTaskLayersChanged = true;
2912 }
2913
2914 void rankTaskLayersIfNeeded() {
2915 if (!mTaskLayersChanged) {
2916 return;
2917 }
2918 mTaskLayersChanged = false;
2919 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2920 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2921 int baseLayer = 0;
2922 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2923 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2924 }
2925 }
2926 }
2927
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002928 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2929 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2930 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2931 final int topStackNdx = stacks.size() - 1;
2932 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2933 final ActivityStack stack = stacks.get(stackNdx);
2934 stack.clearOtherAppTimeTrackers(except);
2935 }
2936 }
2937 }
2938
Craig Mautner8d341ef2013-03-26 09:03:27 -07002939 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2941 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 final int numStacks = stacks.size();
2943 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2944 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002945 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002947 }
2948 }
2949
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002950 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2951 // Examine all activities currently running in the process.
2952 TaskRecord firstTask = null;
2953 // Tasks is non-null only if two or more tasks are found.
2954 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002955 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2956 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002957 ActivityRecord r = app.activities.get(i);
2958 // First, if we find an activity that is in the process of being destroyed,
2959 // then we just aren't going to do anything for now; we want things to settle
2960 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002961 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002962 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002963 return;
2964 }
2965 // Don't consider any activies that are currently not in a state where they
2966 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002967 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2968 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002969 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002970 continue;
2971 }
2972 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002973 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002974 + " from " + r);
2975 if (firstTask == null) {
2976 firstTask = r.task;
2977 } else if (firstTask != r.task) {
2978 if (tasks == null) {
2979 tasks = new ArraySet<>();
2980 tasks.add(firstTask);
2981 }
2982 tasks.add(r.task);
2983 }
2984 }
2985 }
2986 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002987 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002988 return;
2989 }
2990 // If we have activities in multiple tasks that are in a position to be destroyed,
2991 // let's iterate through the tasks and release the oldest one.
2992 final int numDisplays = mActivityDisplays.size();
2993 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2994 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2995 // Step through all stacks starting from behind, to hit the oldest things first.
2996 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2997 final ActivityStack stack = stacks.get(stackNdx);
2998 // Try to release activities in this stack; if we manage to, we are done.
2999 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3000 return;
3001 }
3002 }
3003 }
3004 }
3005
Amith Yamasani37a40c22015-06-17 13:25:42 -07003006 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003007 final int focusStackId = mFocusedStack.getStackId();
3008 // We dismiss the docked stack whenever we switch users.
3009 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3010
3011 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003012 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003013 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003014
Craig Mautner858d8a62013-04-23 17:08:34 -07003015 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3017 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003019 final ActivityStack stack = stacks.get(stackNdx);
3020 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003021 TaskRecord task = stack.topTask();
3022 if (task != null) {
3023 mWindowManager.moveTaskToTop(task.taskId);
3024 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003025 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003026 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003027
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003028 ActivityStack stack = getStack(restoreStackId);
3029 if (stack == null) {
3030 stack = mHomeStack;
3031 }
3032 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003033 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003034 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003035 } else {
3036 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003037 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003038 }
Craig Mautner93529a42013-10-04 15:03:13 -07003039 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003040 }
3041
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003042 /** Checks whether the userid is a profile of the current user. */
3043 boolean isCurrentProfileLocked(int userId) {
3044 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003045 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003046 }
3047
Chong Zhang45c25ce2015-08-10 22:18:26 -07003048 /** Checks whether the activity should be shown for current user. */
3049 boolean okToShowLocked(ActivityRecord r) {
3050 return r != null && (isCurrentProfileLocked(r.userId)
3051 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3052 }
3053
Craig Mautnerde4ef022013-04-07 19:01:33 -07003054 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003055 ArrayList<ActivityRecord> stops = null;
3056
3057 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003058 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3059 ActivityRecord s = mStoppingActivities.get(activityNdx);
3060 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003061 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003062 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3063 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003064 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003065 if (s.finishing) {
3066 // If this activity is finishing, it is sitting on top of
3067 // everyone else but we now know it is no longer needed...
3068 // so get rid of it. Otherwise, we need to go through the
3069 // normal flow and hide it once we determine that it is
3070 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003071 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003072 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003073 }
3074 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003075 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003076 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003078 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 }
3080 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003081 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 }
3083 }
3084
3085 return stops;
3086 }
3087
Craig Mautnercf910b02013-04-23 11:23:27 -07003088 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003089 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3090 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3091 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3092 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003093 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003094 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003095 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003096 if (r == null) Slog.e(TAG,
3097 "validateTop...: null top activity, stack=" + stack);
3098 else {
3099 final ActivityRecord pausing = stack.mPausingActivity;
3100 if (pausing != null && pausing == r) Slog.e(TAG,
3101 "validateTop...: top stack has pausing activity r=" + r
3102 + " state=" + state);
3103 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3104 "validateTop...: activity in front not resumed r=" + r
3105 + " state=" + state);
3106 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003107 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003108 final ActivityRecord resumed = stack.mResumedActivity;
3109 if (resumed != null && resumed == r) Slog.e(TAG,
3110 "validateTop...: back stack has resumed activity r=" + r
3111 + " state=" + state);
3112 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3113 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003114 }
3115 }
3116 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003117 }
3118
Craig Mautnere0570202015-05-13 13:06:11 -07003119 private String lockTaskModeToString() {
3120 switch (mLockTaskModeState) {
3121 case LOCK_TASK_MODE_LOCKED:
3122 return "LOCKED";
3123 case LOCK_TASK_MODE_PINNED:
3124 return "PINNED";
3125 case LOCK_TASK_MODE_NONE:
3126 return "NONE";
3127 default: return "unknown=" + mLockTaskModeState;
3128 }
3129 }
3130
Craig Mautner27084302013-03-25 08:05:25 -07003131 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003132 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003134 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003135 pw.print(prefix);
3136 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003137 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003138 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003139 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3140 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3141 if (packages.size() > 0) {
3142 pw.println(" mLockTaskPackages (userId:packages)=");
3143 for (int i = 0; i < packages.size(); ++i) {
3144 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3145 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3146 }
3147 }
3148 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003149 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003150
Craig Mautner20e72272013-04-01 13:45:53 -07003151 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003152 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003153 }
3154
Dianne Hackborn390517b2013-05-30 15:03:32 -07003155 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3156 boolean needSep, String prefix) {
3157 if (activity != null) {
3158 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3159 if (needSep) {
3160 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003161 }
3162 pw.print(prefix);
3163 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003164 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003165 }
3166 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003167 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003168 }
3169
Craig Mautner8d341ef2013-03-26 09:03:27 -07003170 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3171 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003172 boolean printed = false;
3173 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003174 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3175 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003176 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003177 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003178 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003179 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003180 final ActivityStack stack = stacks.get(stackNdx);
3181 StringBuilder stackHeader = new StringBuilder(128);
3182 stackHeader.append(" Stack #");
3183 stackHeader.append(stack.mStackId);
3184 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003185 stackHeader.append("\n");
3186 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3187 stackHeader.append("\n");
3188 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3190 needSep, stackHeader.toString());
3191 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3192 !dumpAll, false, dumpPackage, true,
3193 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003194
Craig Mautner4a1cb222013-12-04 16:14:06 -08003195 needSep = printed;
3196 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3197 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003198 if (pr) {
3199 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003200 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003201 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003202 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3203 " mResumedActivity: ");
3204 if (pr) {
3205 printed = true;
3206 needSep = false;
3207 }
3208 if (dumpAll) {
3209 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3210 " mLastPausedActivity: ");
3211 if (pr) {
3212 printed = true;
3213 needSep = true;
3214 }
3215 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3216 needSep, " mLastNoHistoryActivity: ");
3217 }
3218 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003219 }
3220 }
3221
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003222 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3223 false, dumpPackage, true, " Activities waiting to finish:", null);
3224 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3225 false, dumpPackage, true, " Activities waiting to stop:", null);
3226 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3227 false, dumpPackage, true, " Activities waiting for another to become visible:",
3228 null);
3229 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3230 false, dumpPackage, true, " Activities waiting to sleep:", null);
3231 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3232 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003233
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003234 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003235 }
3236
Dianne Hackborn390517b2013-05-30 15:03:32 -07003237 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003239 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003240 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003241 String innerPrefix = null;
3242 String[] args = null;
3243 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003244 for (int i=list.size()-1; i>=0; i--) {
3245 final ActivityRecord r = list.get(i);
3246 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3247 continue;
3248 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003249 if (innerPrefix == null) {
3250 innerPrefix = prefix + " ";
3251 args = new String[0];
3252 }
3253 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003254 final boolean full = !brief && (complete || !r.isInHistory());
3255 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003256 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003257 needNL = false;
3258 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003259 if (header1 != null) {
3260 pw.println(header1);
3261 header1 = null;
3262 }
3263 if (header2 != null) {
3264 pw.println(header2);
3265 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003266 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003267 if (lastTask != r.task) {
3268 lastTask = r.task;
3269 pw.print(prefix);
3270 pw.print(full ? "* " : " ");
3271 pw.println(lastTask);
3272 if (full) {
3273 lastTask.dump(pw, prefix + " ");
3274 } else if (complete) {
3275 // Complete + brief == give a summary. Isn't that obvious?!?
3276 if (lastTask.intent != null) {
3277 pw.print(prefix); pw.print(" ");
3278 pw.println(lastTask.intent.toInsecureStringWithClip());
3279 }
3280 }
3281 }
3282 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3283 pw.print(" #"); pw.print(i); pw.print(": ");
3284 pw.println(r);
3285 if (full) {
3286 r.dump(pw, innerPrefix);
3287 } else if (complete) {
3288 // Complete + brief == give a summary. Isn't that obvious?!?
3289 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3290 if (r.app != null) {
3291 pw.print(innerPrefix); pw.println(r.app);
3292 }
3293 }
3294 if (client && r.app != null && r.app.thread != null) {
3295 // flush anything that is already in the PrintWriter since the thread is going
3296 // to write to the file descriptor directly
3297 pw.flush();
3298 try {
3299 TransferPipe tp = new TransferPipe();
3300 try {
3301 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3302 r.appToken, innerPrefix, args);
3303 // Short timeout, since blocking here can
3304 // deadlock with the application.
3305 tp.go(fd, 2000);
3306 } finally {
3307 tp.kill();
3308 }
3309 } catch (IOException e) {
3310 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3311 } catch (RemoteException e) {
3312 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3313 }
3314 needNL = true;
3315 }
3316 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003317 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003318 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003319
Craig Mautnerf3333272013-04-22 10:55:53 -07003320 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003321 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3322 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003323 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3324 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003325 }
3326
3327 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003328 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003329 }
3330
3331 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003332 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3333 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003334 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3335 }
3336
Craig Mautner05d29032013-05-03 13:40:13 -07003337 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003338 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3339 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3340 }
Craig Mautner05d29032013-05-03 13:40:13 -07003341 }
3342
Craig Mautner0eea92c2013-05-16 13:35:39 -07003343 void removeSleepTimeouts() {
3344 mSleepTimeout = false;
3345 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3346 }
3347
3348 final void scheduleSleepTimeout() {
3349 removeSleepTimeouts();
3350 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3351 }
3352
Craig Mautner4a1cb222013-12-04 16:14:06 -08003353 @Override
3354 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003355 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003356 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3357 }
3358
3359 @Override
3360 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003361 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003362 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3363 }
3364
3365 @Override
3366 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003367 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3369 }
3370
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003371 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003372 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003374 newDisplay = mActivityDisplays.get(displayId) == null;
3375 if (newDisplay) {
3376 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003377 if (activityDisplay.mDisplay == null) {
3378 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3379 return;
3380 }
Craig Mautner4504de52013-12-20 09:06:56 -08003381 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003382 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003383 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 }
Craig Mautner4504de52013-12-20 09:06:56 -08003385 if (newDisplay) {
3386 mWindowManager.onDisplayAdded(displayId);
3387 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 }
3389
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003390 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003391 mDefaultMinimalSizeOfResizeableTask =
3392 mService.mContext.getResources().getDimensionPixelSize(
3393 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003394 }
3395
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003396 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003397 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003398 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3399 if (activityDisplay != null) {
3400 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003402 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003403 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003404 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003405 }
3406 }
3407 mWindowManager.onDisplayRemoved(displayId);
3408 }
3409
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003410 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003412 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3413 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414 // TODO: Update the bounds.
3415 }
3416 }
3417 mWindowManager.onDisplayChanged(displayId);
3418 }
3419
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003420 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003421 StackInfo info = new StackInfo();
3422 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3423 info.displayId = Display.DEFAULT_DISPLAY;
3424 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003425 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003426 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003427
3428 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3429 final int numTasks = tasks.size();
3430 int[] taskIds = new int[numTasks];
3431 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003432 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003433 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003434 for (int i = 0; i < numTasks; ++i) {
3435 final TaskRecord task = tasks.get(i);
3436 taskIds[i] = task.taskId;
3437 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3438 : task.realActivity != null ? task.realActivity.flattenToString()
3439 : task.getTopActivity() != null ? task.getTopActivity().packageName
3440 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003441 taskBounds[i] = new Rect();
3442 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003443 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003444 }
3445 info.taskIds = taskIds;
3446 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003447 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003448 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003449
3450 final ActivityRecord top = stack.topRunningActivityLocked();
3451 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003452 return info;
3453 }
3454
3455 StackInfo getStackInfoLocked(int stackId) {
3456 ActivityStack stack = getStack(stackId);
3457 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003458 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003459 }
3460 return null;
3461 }
3462
3463 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003464 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003465 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3466 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003467 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003468 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 }
3470 }
3471 return list;
3472 }
3473
Craig Mautner15df08a2015-04-01 12:17:18 -07003474 TaskRecord getLockedTaskLocked() {
3475 final int top = mLockTaskModeTasks.size() - 1;
3476 if (top >= 0) {
3477 return mLockTaskModeTasks.get(top);
3478 }
3479 return null;
3480 }
3481
3482 boolean isLockedTask(TaskRecord task) {
3483 return mLockTaskModeTasks.contains(task);
3484 }
3485
3486 boolean isLastLockedTask(TaskRecord task) {
3487 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3488 }
3489
3490 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003491 if (!mLockTaskModeTasks.remove(task)) {
3492 return;
3493 }
3494 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3495 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003496 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003497 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3498 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003499 final Message lockTaskMsg = Message.obtain();
3500 lockTaskMsg.arg1 = task.userId;
3501 lockTaskMsg.what = LOCK_TASK_END_MSG;
3502 mHandler.sendMessage(lockTaskMsg);
3503 }
3504 }
3505
Andrii Kulianc27916642016-04-12 17:59:27 -07003506 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3507 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3508 false /* forceNonResizable */);
3509 }
3510
Jorim Jaggid53f0922016-04-06 22:16:23 -07003511 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003512 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003513 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3514 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003515 return;
3516 }
3517
Jorim Jaggicd13d332016-04-27 15:40:20 -07003518 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003519 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003520 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003521 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3522
Andrii Kulianc27916642016-04-12 17:59:27 -07003523 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3524 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003525 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003526 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3527 && !topActivity.noDisplay) {
3528 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003529 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3530 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003531 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003532 }
3533
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003534 void showLockTaskToast() {
3535 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003536 }
3537
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003538 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3539 if (mLockTaskModeTasks.contains(task)) {
3540 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3541 }
3542 }
3543
Craig Mautner432f64e2015-05-20 14:59:57 -07003544 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3545 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003546 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003547 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003548 final TaskRecord lockedTask = getLockedTaskLocked();
3549 if (lockedTask != null) {
3550 removeLockedTaskLocked(lockedTask);
3551 if (!mLockTaskModeTasks.isEmpty()) {
3552 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003553 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3554 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003555 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003556 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003557 return;
3558 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003559 }
Craig Mautnere0570202015-05-13 13:06:11 -07003560 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3561 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003562 return;
3563 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003564
3565 // Should have already been checked, but do it again.
3566 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003567 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3568 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003569 return;
3570 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003571 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003572 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003573 return;
3574 }
3575
3576 if (mLockTaskModeTasks.isEmpty()) {
3577 // First locktask.
3578 final Message lockTaskMsg = Message.obtain();
3579 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3580 lockTaskMsg.arg1 = task.userId;
3581 lockTaskMsg.what = LOCK_TASK_START_MSG;
3582 lockTaskMsg.arg2 = lockTaskModeState;
3583 mHandler.sendMessage(lockTaskMsg);
3584 }
3585 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003586 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3587 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003588 mLockTaskModeTasks.remove(task);
3589 mLockTaskModeTasks.add(task);
3590
3591 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003592 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003593 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003594
3595 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003596 findTaskToMoveToFrontLocked(task, 0, null, reason,
3597 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003598 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003599 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3600 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3601 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003602 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003603 }
3604
3605 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003606 return isLockTaskModeViolation(task, false);
3607 }
3608
3609 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3610 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003611 return false;
3612 }
3613 final int lockTaskAuth = task.mLockTaskAuth;
3614 switch (lockTaskAuth) {
3615 case LOCK_TASK_AUTH_DONT_LOCK:
3616 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003617 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003618 case LOCK_TASK_AUTH_LAUNCHABLE:
3619 case LOCK_TASK_AUTH_WHITELISTED:
3620 return false;
3621 case LOCK_TASK_AUTH_PINNABLE:
3622 // Pinnable tasks can't be launched on top of locktask tasks.
3623 return !mLockTaskModeTasks.isEmpty();
3624 default:
3625 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3626 return true;
3627 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003628 }
3629
Craig Mautner15df08a2015-04-01 12:17:18 -07003630 void onLockTaskPackagesUpdatedLocked() {
3631 boolean didSomething = false;
3632 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3633 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003634 final boolean wasWhitelisted =
3635 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3636 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003637 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003638 final boolean isWhitelisted =
3639 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3640 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3641 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003642 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003643 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3644 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003645 removeLockedTaskLocked(lockedTask);
3646 lockedTask.performClearTaskLocked();
3647 didSomething = true;
3648 }
3649 }
3650 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3651 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3652 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3653 final ActivityStack stack = stacks.get(stackNdx);
3654 stack.onLockTaskPackagesUpdatedLocked();
3655 }
3656 }
Craig Mautnere0570202015-05-13 13:06:11 -07003657 final ActivityRecord r = topRunningActivityLocked();
3658 final TaskRecord task = r != null ? r.task : null;
3659 if (mLockTaskModeTasks.isEmpty() && task != null
3660 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3661 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003662 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3663 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3664 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3665 false);
3666 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003667 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003668 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003669 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003670 }
3671 }
3672
Benjamin Franz43261142015-02-11 15:59:44 +00003673 int getLockTaskModeState() {
3674 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003675 }
3676
Jorim Jaggife89d122015-12-22 16:28:44 +01003677 void activityRelaunchedLocked(IBinder token) {
3678 mWindowManager.notifyAppRelaunchingFinished(token);
3679 }
3680
3681 void activityRelaunchingLocked(ActivityRecord r) {
3682 mWindowManager.notifyAppRelaunching(r.appToken);
3683 }
3684
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003685 void logStackState() {
3686 mActivityMetricsLogger.logWindowState();
3687 }
3688
Andrii Kulian933076d2016-03-29 17:04:42 -07003689 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003690 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3691 final ActivityRecord r = task.mActivities.get(i);
3692 if (r.app != null && r.app.thread != null) {
3693 mMultiWindowModeChangedActivities.add(r);
3694 }
3695 }
3696
3697 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3698 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3699 }
3700 }
3701
Andrii Kulian933076d2016-03-29 17:04:42 -07003702 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003703 final ActivityStack stack = task.stack;
3704 if (prevStack == null || prevStack == stack
3705 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3706 return;
3707 }
3708
3709 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3710 final ActivityRecord r = task.mActivities.get(i);
3711 if (r.app != null && r.app.thread != null) {
3712 mPipModeChangedActivities.add(r);
3713 }
3714 }
3715
3716 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3717 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3718 }
3719 }
3720
Tony Mak853304c2016-04-18 15:17:41 +01003721 void setDockedStackMinimized(boolean minimized) {
3722 mIsDockMinimized = minimized;
3723 if (minimized) {
3724 // Docked stack is not visible, no need to confirm credentials for its top activity.
3725 return;
3726 }
3727 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3728 if (dockedStack == null) {
3729 return;
3730 }
3731 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3732 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3733 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3734 }
3735 }
3736
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003737 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003738
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003739 public ActivityStackSupervisorHandler(Looper looper) {
3740 super(looper);
3741 }
3742
Craig Mautnerf3333272013-04-22 10:55:53 -07003743 void activityIdleInternal(ActivityRecord r) {
3744 synchronized (mService) {
3745 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3746 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003747 }
3748
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003749 @Override
3750 public void handleMessage(Message msg) {
3751 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003752 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3753 synchronized (mService) {
3754 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3755 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003756 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003757 }
3758 }
3759 } break;
3760 case REPORT_PIP_MODE_CHANGED_MSG: {
3761 synchronized (mService) {
3762 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3763 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003764 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003765 }
3766 }
3767 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003768 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003769 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3770 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003771 if (mService.mDidDexOpt) {
3772 mService.mDidDexOpt = false;
3773 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3774 nmsg.obj = msg.obj;
3775 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3776 return;
3777 }
3778 // We don't at this point know if the activity is fullscreen,
3779 // so we need to be conservative and assume it isn't.
3780 activityIdleInternal((ActivityRecord)msg.obj);
3781 } break;
3782 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003783 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003784 activityIdleInternal((ActivityRecord)msg.obj);
3785 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003786 case RESUME_TOP_ACTIVITY_MSG: {
3787 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003788 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003789 }
3790 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003791 case SLEEP_TIMEOUT_MSG: {
3792 synchronized (mService) {
3793 if (mService.isSleepingOrShuttingDown()) {
3794 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3795 mSleepTimeout = true;
3796 checkReadyForSleepLocked();
3797 }
3798 }
3799 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003800 case LAUNCH_TIMEOUT_MSG: {
3801 if (mService.mDidDexOpt) {
3802 mService.mDidDexOpt = false;
3803 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3804 return;
3805 }
3806 synchronized (mService) {
3807 if (mLaunchingActivity.isHeld()) {
3808 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3809 if (VALIDATE_WAKE_LOCK_CALLER
3810 && Binder.getCallingUid() != Process.myUid()) {
3811 throw new IllegalStateException("Calling must be system uid");
3812 }
3813 mLaunchingActivity.release();
3814 }
3815 }
3816 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003818 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 } break;
3820 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003821 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 } break;
3823 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003824 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003826 case CONTAINER_CALLBACK_VISIBILITY: {
3827 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003828 final IActivityContainerCallback callback = container.mCallback;
3829 if (callback != null) {
3830 try {
3831 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3832 } catch (RemoteException e) {
3833 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003834 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003835 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003836 case LOCK_TASK_START_MSG: {
3837 // When lock task starts, we disable the status bars.
3838 try {
Jason Monk62515be2014-05-21 16:06:19 -04003839 if (mLockTaskNotify == null) {
3840 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003841 }
Jason Monk62515be2014-05-21 16:06:19 -04003842 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003843 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003844 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003845 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003846 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003847 flags = StatusBarManager.DISABLE_MASK
3848 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003849 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003850 flags = StatusBarManager.DISABLE_MASK
3851 & (~StatusBarManager.DISABLE_BACK)
3852 & (~StatusBarManager.DISABLE_HOME)
3853 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003854 }
3855 getStatusBarService().disable(flags, mToken,
3856 mService.mContext.getPackageName());
3857 }
3858 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003859 if (getDevicePolicyManager() != null) {
3860 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003861 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003862 }
justinzhang5286d3f2014-05-12 17:06:01 -04003863 } catch (RemoteException ex) {
3864 throw new RuntimeException(ex);
3865 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003866 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003867 case LOCK_TASK_END_MSG: {
3868 // When lock task ends, we enable the status bars.
3869 try {
Jason Monk62515be2014-05-21 16:06:19 -04003870 if (getStatusBarService() != null) {
3871 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3872 mService.mContext.getPackageName());
3873 }
3874 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003875 if (getDevicePolicyManager() != null) {
3876 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3877 msg.arg1);
3878 }
Jason Monk62515be2014-05-21 16:06:19 -04003879 if (mLockTaskNotify == null) {
3880 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3881 }
3882 mLockTaskNotify.show(false);
3883 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003884 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003885 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003886 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003887 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003888 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003889 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003890 new LockPatternUtils(mService.mContext)
3891 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003892 }
3893 } catch (SettingNotFoundException e) {
3894 // No setting, don't lock.
3895 }
justinzhang5286d3f2014-05-12 17:06:01 -04003896 } catch (RemoteException ex) {
3897 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003898 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003899 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003900 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003901 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003902 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3903 if (mLockTaskNotify == null) {
3904 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3905 }
3906 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3907 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003908 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3909 final ActivityContainer container = (ActivityContainer) msg.obj;
3910 final IActivityContainerCallback callback = container.mCallback;
3911 if (callback != null) {
3912 try {
3913 callback.onAllActivitiesComplete(container.asBinder());
3914 } catch (RemoteException e) {
3915 }
3916 }
3917 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003918 case LAUNCH_TASK_BEHIND_COMPLETE: {
3919 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003920 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003921 if (r != null) {
3922 handleLaunchTaskBehindCompleteLocked(r);
3923 }
3924 }
3925 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003926
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003927 }
3928 }
3929 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003930
Ying Wangb081a592014-04-22 15:20:16 -07003931 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003932 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3933 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003935 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003937 ActivityRecord mParentActivity = null;
3938 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003939
Craig Mautnere3a00d72014-04-16 08:31:19 -07003940 boolean mVisible = true;
3941
Craig Mautner4a1cb222013-12-04 16:14:06 -08003942 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003943 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003945 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3946 final static int CONTAINER_STATE_NO_SURFACE = 1;
3947 final static int CONTAINER_STATE_FINISHING = 2;
3948 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3949
3950 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 synchronized (mService) {
3952 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003953 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003954 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003955 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003956 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003957 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003959 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003960 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003961 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003963 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003964 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003965 }
3966
3967 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003968 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003969 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003970 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3971 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 return;
3973 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003974 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003975 }
3976 }
3977
3978 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003979 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003980 synchronized (mService) {
3981 if (mActivityDisplay != null) {
3982 return mActivityDisplay.mDisplayId;
3983 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003984 }
3985 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003986 }
3987
Jeff Brownca9bc702014-02-11 14:32:56 -08003988 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003989 public int getStackId() {
3990 synchronized (mService) {
3991 return mStackId;
3992 }
3993 }
3994
3995 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003996 public boolean injectEvent(InputEvent event) {
3997 final long origId = Binder.clearCallingIdentity();
3998 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003999 synchronized (mService) {
4000 if (mActivityDisplay != null) {
4001 return mInputManagerInternal.injectInputEvent(event,
4002 mActivityDisplay.mDisplayId,
4003 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4004 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004005 }
4006 return false;
4007 } finally {
4008 Binder.restoreCallingIdentity(origId);
4009 }
4010 }
4011
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004012 @Override
4013 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004014 synchronized (mService) {
4015 if (mContainerState == CONTAINER_STATE_FINISHING) {
4016 return;
4017 }
4018 mContainerState = CONTAINER_STATE_FINISHING;
4019
Craig Mautnerd163e752014-06-13 17:18:47 -07004020 long origId = Binder.clearCallingIdentity();
4021 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004022 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004023 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004024 } finally {
4025 Binder.restoreCallingIdentity(origId);
4026 }
4027 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004028 }
4029
Craig Mautner60257702014-09-17 15:02:33 -07004030 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004031 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004032 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004033 if (mActivityDisplay != null) {
4034 mActivityDisplay.detachActivitiesLocked(mStack);
4035 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004036 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004037 }
4038 }
4039
4040 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004041 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004042 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 }
4044
4045 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004046 public final int startActivityIntentSender(IIntentSender intentSender)
4047 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004048 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4049
4050 if (!(intentSender instanceof PendingIntentRecord)) {
4051 throw new IllegalArgumentException("Bad PendingIntent object");
4052 }
4053
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004054 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004055 Binder.getCallingUid(), mCurrentUser, false,
4056 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004057
4058 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4059 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4060 pendingIntent.key.requestResolvedType);
4061
4062 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4063 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4064 }
4065
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004066 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004067 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004068 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004069 throw new SecurityException(
4070 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4071 }
4072 }
4073
Craig Mautnerdf88d732014-01-27 09:21:32 -08004074 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075 public IBinder asBinder() {
4076 return this;
4077 }
4078
Craig Mautner4504de52013-12-20 09:06:56 -08004079 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004080 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004081 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004082 }
4083
Craig Mautner4a1cb222013-12-04 16:14:06 -08004084 ActivityStackSupervisor getOuter() {
4085 return ActivityStackSupervisor.this;
4086 }
4087
Craig Mautnerd163e752014-06-13 17:18:47 -07004088 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004089 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 }
4091
Craig Mautner6985bad2014-04-21 15:22:06 -07004092 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004093 void setVisible(boolean visible) {
4094 if (mVisible != visible) {
4095 mVisible = visible;
4096 if (mCallback != null) {
4097 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4098 0 /* unused */, this).sendToTarget();
4099 }
4100 }
4101 }
4102
Craig Mautner6985bad2014-04-21 15:22:06 -07004103 void setDrawn() {
4104 }
4105
Craig Mautner1b4bf852014-05-26 15:06:32 -07004106 // You can always start a new task on a regular ActivityStack.
4107 boolean isEligibleForNewTasks() {
4108 return true;
4109 }
4110
Craig Mautnerd163e752014-06-13 17:18:47 -07004111 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004112 detachLocked();
4113 deleteActivityContainer(this);
4114 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004115 }
4116
Craig Mautner34b73df2014-01-12 21:11:08 -08004117 @Override
4118 public String toString() {
4119 return mIdString + (mActivityDisplay == null ? "N" : "A");
4120 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004121 }
4122
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004123 private class VirtualActivityContainer extends ActivityContainer {
4124 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004125 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004126
4127 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4128 super(getNextStackId());
4129 mParentActivity = parent;
4130 mCallback = callback;
4131 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004132 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004133 }
4134
4135 @Override
4136 public void setSurface(Surface surface, int width, int height, int density) {
4137 super.setSurface(surface, width, height, density);
4138
4139 synchronized (mService) {
4140 final long origId = Binder.clearCallingIdentity();
4141 try {
4142 setSurfaceLocked(surface, width, height, density);
4143 } finally {
4144 Binder.restoreCallingIdentity(origId);
4145 }
4146 }
4147 }
4148
4149 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4150 if (mContainerState == CONTAINER_STATE_FINISHING) {
4151 return;
4152 }
4153 VirtualActivityDisplay virtualActivityDisplay =
4154 (VirtualActivityDisplay) mActivityDisplay;
4155 if (virtualActivityDisplay == null) {
4156 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004157 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004158 mActivityDisplay = virtualActivityDisplay;
4159 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004160 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004161 }
4162
4163 if (mSurface != null) {
4164 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004165 }
4166
Craig Mautner6985bad2014-04-21 15:22:06 -07004167 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004168 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004169 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004170 } else {
4171 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004172 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004173 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004174 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004175 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004176 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004177
Craig Mautnerd163e752014-06-13 17:18:47 -07004178 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004179
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004180 if (DEBUG_STACK) Slog.d(TAG_STACK,
4181 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004182 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004183
Craig Mautner6985bad2014-04-21 15:22:06 -07004184 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004185 boolean isAttachedLocked() {
4186 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004187 }
4188
4189 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004190 void setDrawn() {
4191 synchronized (mService) {
4192 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004193 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004194 }
4195 }
4196
Craig Mautner1b4bf852014-05-26 15:06:32 -07004197 // Never start a new task on an ActivityView if it isn't explicitly specified.
4198 @Override
4199 boolean isEligibleForNewTasks() {
4200 return false;
4201 }
4202
Craig Mautnerd163e752014-06-13 17:18:47 -07004203 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004204 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004205 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4206 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4207 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4208 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4209 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004210 }
4211 }
4212
Craig Mautner4a1cb222013-12-04 16:14:06 -08004213 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4214 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004215 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004217 int mDisplayId;
4218 Display mDisplay;
4219 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004220
Craig Mautner4a1cb222013-12-04 16:14:06 -08004221 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4222 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004223 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004224
Jose Lima4b6c6692014-08-12 17:41:12 -07004225 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004226
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004227 ActivityDisplay() {
4228 }
Craig Mautner4504de52013-12-20 09:06:56 -08004229
Craig Mautner1a70a162014-09-13 12:09:31 -07004230 // After instantiation, check that mDisplay is not null before using this. The alternative
4231 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004232 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004233 final Display display = mDisplayManager.getDisplay(displayId);
4234 if (display == null) {
4235 return;
4236 }
4237 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004238 }
4239
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004240 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004241 mDisplay = display;
4242 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004243 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004244 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004246 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004247 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004248 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4249 + " onTop=" + onTop);
4250 if (onTop) {
4251 mStacks.add(stack);
4252 } else {
4253 mStacks.add(0, stack);
4254 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004255 }
4256
Craig Mautnere0a38842013-12-16 16:14:02 -08004257 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004258 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004259 + " from displayId=" + mDisplayId);
4260 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 }
4262
Jose Lima4b6c6692014-08-12 17:41:12 -07004263 void setVisibleBehindActivity(ActivityRecord r) {
4264 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004265 }
4266
Jose Lima4b6c6692014-08-12 17:41:12 -07004267 boolean hasVisibleBehindActivity() {
4268 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004269 }
4270
Craig Mautner34b73df2014-01-12 21:11:08 -08004271 @Override
4272 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004273 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4274 }
4275 }
4276
4277 class VirtualActivityDisplay extends ActivityDisplay {
4278 VirtualDisplay mVirtualDisplay;
4279
Craig Mautner6985bad2014-04-21 15:22:06 -07004280 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004281 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004282 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4283 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4284 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4285 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004286
4287 init(mVirtualDisplay.getDisplay());
4288
4289 mWindowManager.handleDisplayAdded(mDisplayId);
4290 }
4291
4292 void setSurface(Surface surface) {
4293 if (mVirtualDisplay != null) {
4294 mVirtualDisplay.setSurface(surface);
4295 }
4296 }
4297
4298 @Override
4299 void detachActivitiesLocked(ActivityStack stack) {
4300 super.detachActivitiesLocked(stack);
4301 if (mVirtualDisplay != null) {
4302 mVirtualDisplay.release();
4303 mVirtualDisplay = null;
4304 }
4305 }
4306
4307 @Override
4308 public String toString() {
4309 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004310 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004311 }
Jose Lima58e66d62014-05-27 20:00:27 -07004312
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004313 /**
4314 * Adjust bounds to stay within stack bounds.
4315 *
4316 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4317 * that keep them unchanged, but be contained within the stack bounds.
4318 *
4319 * @param bounds Bounds to be adjusted.
4320 * @param stackBounds Bounds within which the other bounds should remain.
4321 */
4322 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4323 if (stackBounds == null || stackBounds.contains(bounds)) {
4324 return;
4325 }
4326
4327 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4328 final int maxRight = stackBounds.right
4329 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4330 int horizontalDiff = stackBounds.left - bounds.left;
4331 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4332 || (bounds.left + horizontalDiff >= maxRight)) {
4333 horizontalDiff = maxRight - bounds.left;
4334 }
4335 bounds.left += horizontalDiff;
4336 bounds.right += horizontalDiff;
4337 }
4338
4339 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4340 final int maxBottom = stackBounds.bottom
4341 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4342 int verticalDiff = stackBounds.top - bounds.top;
4343 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4344 || (bounds.top + verticalDiff >= maxBottom)) {
4345 verticalDiff = maxBottom - bounds.top;
4346 }
4347 bounds.top += verticalDiff;
4348 bounds.bottom += verticalDiff;
4349 }
4350 }
4351
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004352 ActivityStack findStackBehind(ActivityStack stack) {
4353 // TODO(multi-display): We are only looking for stacks on the default display.
4354 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4355 if (display == null) {
4356 return null;
4357 }
4358 final ArrayList<ActivityStack> stacks = display.mStacks;
4359 for (int i = stacks.size() - 1; i >= 0; i--) {
4360 if (stacks.get(i) == stack && i > 0) {
4361 return stacks.get(i - 1);
4362 }
4363 }
4364 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4365 + " in=" + stacks);
4366 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004367
Jorim Jaggic69bd222016-03-15 14:38:37 +01004368 /**
4369 * Puts a task into resizing mode during the next app transition.
4370 *
4371 * @param taskId the id of the task to put into resizing mode
4372 */
4373 private void setResizingDuringAnimation(int taskId) {
4374 mResizingTasksDuringAnimation.add(taskId);
4375 mWindowManager.setTaskDockedResizing(taskId, true);
4376 }
4377
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004378 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4379 final TaskRecord task;
4380 final int callingUid;
4381 final String callingPackage;
4382 final Intent intent;
4383 final int userId;
4384 final ActivityOptions activityOptions = (bOptions != null)
4385 ? new ActivityOptions(bOptions) : null;
4386 final int launchStackId = (activityOptions != null)
4387 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004388 if (launchStackId == HOME_STACK_ID) {
4389 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4390 + taskId + " can't be launch in the home stack.");
4391 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004392
4393 if (launchStackId == DOCKED_STACK_ID) {
4394 mWindowManager.setDockedStackCreateState(
4395 activityOptions.getDockCreateMode(), null /* initialBounds */);
4396
4397 // Defer updating the stack in which recents is until the app transition is done, to
4398 // not run into issues where we still need to draw the task in recents but the
4399 // docked stack is already created.
4400 deferUpdateBounds(HOME_STACK_ID);
4401 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4402 }
4403
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004404 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4405 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004406 continueUpdateBounds(HOME_STACK_ID);
4407 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004408 throw new IllegalArgumentException(
4409 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4410 }
4411
4412 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004413 if (task.stack.mStackId != launchStackId) {
4414 moveTaskToStackLocked(
4415 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4416 ANIMATE);
4417 }
4418 }
4419
4420 // If the user must confirm credentials (e.g. when first launching a work app and the
4421 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4422 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4423 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004424 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004425 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004426 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4427 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004428
4429 // If we are launching the task in the docked stack, put it into resizing mode so
4430 // the window renders full-screen with the background filling the void. Also only
4431 // call this at the end to make sure that tasks exists on the window manager side.
4432 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004433 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004434 }
4435 return ActivityManager.START_TASK_TO_FRONT;
4436 }
4437 callingUid = task.mCallingUid;
4438 callingPackage = task.mCallingPackage;
4439 intent = task.intent;
4440 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4441 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004442 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4443 null, null, 0, 0, bOptions, userId, null, task);
4444 if (launchStackId == DOCKED_STACK_ID) {
4445 setResizingDuringAnimation(task.taskId);
4446 }
4447 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004448 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004449
4450 /**
4451 * @return a list of activities which are the top ones in each visible stack. The first
4452 * entry will be the focused activity.
4453 */
4454 public List<IBinder> getTopVisibleActivities() {
4455 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4456 if (display == null) {
4457 return Collections.EMPTY_LIST;
4458 }
4459 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4460 final ArrayList<ActivityStack> stacks = display.mStacks;
4461 for (int i = stacks.size() - 1; i >= 0; i--) {
4462 ActivityStack stack = stacks.get(i);
4463 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4464 ActivityRecord top = stack.topActivity();
4465 if (top != null) {
4466 if (stack == mFocusedStack) {
4467 topActivityTokens.add(0, top.appToken);
4468 } else {
4469 topActivityTokens.add(top.appToken);
4470 }
4471 }
4472 }
4473 }
4474 return topActivityTokens;
4475 }
Craig Mautner27084302013-03-25 08:05:25 -07004476}