blob: c559b84bd7fd4b3b8482b5dec9922f3a1a9ae1a9 [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;
Chong Zhang5022da32016-06-21 16:31:37 -0700114import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife89d122015-12-22 16:28:44 +0100115import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
116import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
117import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
118import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
119import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
120import static android.app.ActivityManager.StackId.HOME_STACK_ID;
121import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
122import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
123import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
124import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
125import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
126import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
127import 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.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700413 int mDefaultMinSizeOfResizeableTask = -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
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700784 static int nextTaskIdForUser(int taskId, int userId) {
785 int nextTaskId = taskId + 1;
786 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
787 // Wrap around as there will be smaller task ids that are available now.
788 nextTaskId -= MAX_TASK_IDS_PER_USER;
789 }
790 return nextTaskId;
791 }
792
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800793 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800794 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
795 // for a userId u, a taskId can only be in the range
796 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
797 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700798 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800799 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
800 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
801 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700802 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800803 if (candidateTaskId == currentTaskId) {
804 // Something wrong!
805 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
806 throw new IllegalStateException("Cannot get an available task id."
807 + " Reached limit of " + MAX_TASK_IDS_PER_USER
808 + " running tasks per user.");
809 }
810 }
811 mCurTaskIdForUser.put(userId, candidateTaskId);
812 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700813 }
814
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800816 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700817 if (stack == null) {
818 return null;
819 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700820 ActivityRecord resumedActivity = stack.mResumedActivity;
821 if (resumedActivity == null || resumedActivity.app == null) {
822 resumedActivity = stack.mPausingActivity;
823 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700824 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700825 }
826 }
827 return resumedActivity;
828 }
829
Dianne Hackbornff072722014-09-24 10:56:28 -0700830 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700831 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800832 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800833 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
834 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
836 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700837 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 continue;
839 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700840 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 if (hr != null) {
842 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
843 && processName.equals(hr.processName)) {
844 try {
George Mount2c92c972014-03-20 09:38:23 -0700845 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800846 didSomething = true;
847 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700848 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 Slog.w(TAG, "Exception in new application when starting activity "
850 + hr.intent.getComponent().flattenToShortString(), e);
851 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700852 }
Craig Mautner20e72272013-04-01 13:45:53 -0700853 }
Craig Mautner20e72272013-04-01 13:45:53 -0700854 }
855 }
856 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700857 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700858 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700859 }
Craig Mautner20e72272013-04-01 13:45:53 -0700860 return didSomething;
861 }
862
863 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800864 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
865 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
867 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700868 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 continue;
870 }
871 final ActivityRecord resumedActivity = stack.mResumedActivity;
872 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700873 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800874 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 return false;
876 }
Craig Mautner20e72272013-04-01 13:45:53 -0700877 }
878 }
Wei Wang65c7a152016-06-02 18:51:22 -0700879 // Send launch end powerhint when idle
880 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700881 return true;
882 }
883
Craig Mautnerde4ef022013-04-07 19:01:33 -0700884 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
886 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
888 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700889 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700891 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 return false;
893 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700894 }
895 }
896 }
897 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700898 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800899 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
900 mLastFocusedStack + " to=" + mFocusedStack);
901 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 return true;
903 }
904
905 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800906 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800907 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
908 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
910 final ActivityStack stack = stacks.get(stackNdx);
911 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800912 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700913 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800914 return false;
915 }
916 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700918 }
919 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800920 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700921 }
922
Craig Mautner2acc3892013-09-23 10:28:14 -0700923 /**
924 * Pause all activities in either all of the stacks or just the back stacks.
925 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700926 * @param resuming The resuming activity.
927 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
928 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700929 * @return true if any activity was paused as a result of this call.
930 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700931 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700932 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800933 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
934 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
936 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700937 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700938 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700940 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
941 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700943 }
944 }
945 return someActivityPaused;
946 }
947
Craig Mautnerde4ef022013-04-07 19:01:33 -0700948 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700949 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800950 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
951 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
953 final ActivityStack stack = stacks.get(stackNdx);
954 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700955 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700957 Slog.d(TAG_STATES,
958 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 pausing = false;
960 } else {
961 return false;
962 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700963 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700964 }
965 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700966 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 }
968
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700970 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500971 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800972 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
973 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
974 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
975 final ActivityStack stack = stacks.get(stackNdx);
976 if (stack.mResumedActivity != null &&
977 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700978 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800979 }
980 }
981 }
982 }
983
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800984 void cancelInitializingActivities() {
985 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
986 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
987 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
988 stacks.get(stackNdx).cancelInitializingActivities();
989 }
990 }
991 }
992
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700993 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700994 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700995 }
996
997 void sendWaitingVisibleReportLocked(ActivityRecord r) {
998 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700999 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001001 if (w.who == null) {
1002 changed = true;
1003 w.timeout = false;
1004 if (r != null) {
1005 w.who = new ComponentName(r.info.packageName, r.info.name);
1006 }
1007 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1008 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001009 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001011 if (changed) {
1012 mService.notifyAll();
1013 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001014 }
1015
Chong Zhang5022da32016-06-21 16:31:37 -07001016 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1017 boolean changed = false;
1018 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1019 WaitResult w = mWaitingActivityLaunched.remove(i);
1020 if (w.who == null) {
1021 changed = true;
1022 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1023 // the starting activity ends up moving another activity to front, and it should
1024 // wait for this new activity to become visible instead.
1025 // Do not modify other fields.
1026 w.result = START_TASK_TO_FRONT;
1027 }
1028 }
1029 if (changed) {
1030 mService.notifyAll();
1031 }
1032 }
1033
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001034 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1035 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001036 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001038 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001039 if (w.who == null) {
1040 changed = true;
1041 w.timeout = timeout;
1042 if (r != null) {
1043 w.who = new ComponentName(r.info.packageName, r.info.name);
1044 }
1045 w.thisTime = thisTime;
1046 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001047 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001049 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001050 if (changed) {
1051 mService.notifyAll();
1052 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001053 }
1054
Craig Mautner29219d92013-04-16 20:19:12 -07001055 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001056 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001057 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001058 if (r != null) {
1059 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001060 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001061
Andrii Kulian7318d632016-07-20 18:59:28 -07001062 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001063 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001064 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1065 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001066 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001067 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001068 if (r != null) {
1069 return r;
1070 }
1071 }
1072 }
1073 return null;
1074 }
1075
Dianne Hackborn09233282014-04-30 11:33:59 -07001076 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001077 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001078 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1079 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001080 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001081 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001082 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1084 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001085 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001086 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001087 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001088 }
1089 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001090
1091 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1092 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1093 while (maxNum > 0) {
1094 long mostRecentActiveTime = Long.MIN_VALUE;
1095 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001096 final int numTaskLists = runningTaskLists.size();
1097 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1098 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001099 if (!stackTaskList.isEmpty()) {
1100 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1101 if (lastActiveTime > mostRecentActiveTime) {
1102 mostRecentActiveTime = lastActiveTime;
1103 selectedStackList = stackTaskList;
1104 }
1105 }
1106 }
1107 if (selectedStackList != null) {
1108 list.add(selectedStackList.remove(0));
1109 --maxNum;
1110 } else {
1111 break;
1112 }
1113 }
Craig Mautner20e72272013-04-01 13:45:53 -07001114 }
1115
Todd Kennedy7440f172015-12-09 14:31:22 -08001116 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1117 ProfilerInfo profilerInfo) {
1118 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 if (aInfo != null) {
1120 // Store the found target back into the intent, because now that
1121 // we have it we never want to do this again. For example, if the
1122 // user navigates back to this point in the history, we should
1123 // always restart the exact same activity.
1124 intent.setComponent(new ComponentName(
1125 aInfo.applicationInfo.packageName, aInfo.name));
1126
1127 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001128 if (!aInfo.processName.equals("system")) {
1129 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001130 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001131 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001132
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001133 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1134 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1135 }
1136
Man Caocfa78b22015-06-11 20:14:34 -07001137 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1138 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1139 }
1140
1141 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001142 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001143 }
1144 }
1145 }
1146 return aInfo;
1147 }
1148
Todd Kennedy7440f172015-12-09 14:31:22 -08001149 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001150 return resolveIntent(intent, resolvedType, userId, 0);
1151 }
1152
1153 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001154 try {
1155 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001156 PackageManager.MATCH_DEFAULT_ONLY | flags
1157 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001158 } catch (RemoteException e) {
1159 }
1160 return null;
1161 }
1162
1163 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1164 ProfilerInfo profilerInfo, int userId) {
1165 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1166 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1167 }
1168
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001169 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1170 boolean andResume, boolean checkConfig) throws RemoteException {
1171
1172 if (!allPausedActivitiesComplete()) {
1173 // While there are activities pausing we skipping starting any new activities until
1174 // pauses are complete. NOTE: that we also do this for activities that are starting in
1175 // the paused state because they will first be resumed then paused on the client side.
1176 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1177 "realStartActivityLocked: Skipping start of r=" + r
1178 + " some activities pausing...");
1179 return false;
1180 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001181
Craig Mautner2568c3a2015-03-26 14:22:34 -07001182 if (andResume) {
1183 r.startFreezingScreenLocked(app, 0);
1184 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001185
Craig Mautner2568c3a2015-03-26 14:22:34 -07001186 // schedule launch ticks to collect information about slow apps.
1187 r.startLaunchTickingLocked();
1188 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001189
1190 // Have the window manager re-evaluate the orientation of
1191 // the screen based on the new activity order. Note that
1192 // as a result of this, it can call back into the activity
1193 // manager with a new orientation. We don't care about that,
1194 // because the activity is not currently running so we are
1195 // just restarting it anyway.
1196 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001197 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001198 mService.mConfiguration,
1199 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001200 // Deferring resume here because we're going to launch new activity shortly.
1201 // We don't want to perform a redundant launch of the same record while ensuring
1202 // configurations and trying to resume top activity of focused stack.
1203 mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 }
1205
1206 r.app = app;
1207 app.waitingToKill = null;
1208 r.launchCount++;
1209 r.lastLaunchTime = SystemClock.uptimeMillis();
1210
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001211 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001212
1213 int idx = app.activities.indexOf(r);
1214 if (idx < 0) {
1215 app.activities.add(r);
1216 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001217 mService.updateLruProcessLocked(app, true, null);
1218 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001219
Craig Mautner15df08a2015-04-01 12:17:18 -07001220 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001221 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1222 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001223 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001224 }
1225
1226 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 try {
1228 if (app.thread == null) {
1229 throw new RemoteException();
1230 }
1231 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001232 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 if (andResume) {
1234 results = r.results;
1235 newIntents = r.newIntents;
1236 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001237 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1238 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1239 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 if (andResume) {
1241 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1242 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001245 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001246 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001247 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001249 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1250 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 r.sleeping = false;
1252 r.forceNewConfig = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04001253 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 mService.showAskCompatModeDialogLocked(r);
1255 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001256 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001257 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1258 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1259 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001260 final String profileFile = mService.mProfileFile;
1261 if (profileFile != null) {
1262 ParcelFileDescriptor profileFd = mService.mProfileFd;
1263 if (profileFd != null) {
1264 try {
1265 profileFd = profileFd.dup();
1266 } catch (IOException e) {
1267 if (profileFd != null) {
1268 try {
1269 profileFd.close();
1270 } catch (IOException o) {
1271 }
1272 profileFd = null;
1273 }
1274 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001276
1277 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1278 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 }
1280 }
1281 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001282
Craig Mautner2568c3a2015-03-26 14:22:34 -07001283 if (andResume) {
1284 app.hasShownUi = true;
1285 app.pendingUiClean = true;
1286 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001287 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001289 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001290 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001291 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001292 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001293
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001294 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 // This may be a heavy-weight process! Note that the package
1296 // manager will ensure that only activity can run in the main
1297 // process of the .apk, which is the only thing that will be
1298 // considered heavy-weight.
1299 if (app.processName.equals(app.info.packageName)) {
1300 if (mService.mHeavyWeightProcess != null
1301 && mService.mHeavyWeightProcess != app) {
1302 Slog.w(TAG, "Starting new heavy weight process " + app
1303 + " when already running "
1304 + mService.mHeavyWeightProcess);
1305 }
1306 mService.mHeavyWeightProcess = app;
1307 Message msg = mService.mHandler.obtainMessage(
1308 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1309 msg.obj = r;
1310 mService.mHandler.sendMessage(msg);
1311 }
1312 }
1313
1314 } catch (RemoteException e) {
1315 if (r.launchFailed) {
1316 // This is the second time we failed -- finish activity
1317 // and give up.
1318 Slog.e(TAG, "Second failure launching "
1319 + r.intent.getComponent().flattenToShortString()
1320 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001321 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1323 "2nd-crash", false);
1324 return false;
1325 }
1326
1327 // This is the first time we failed -- restart process and
1328 // retry.
1329 app.activities.remove(r);
1330 throw e;
1331 }
1332
1333 r.launchFailed = false;
1334 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001335 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 }
1337
1338 if (andResume) {
1339 // As part of the process of launching, ActivityThread also performs
1340 // a resume.
1341 stack.minimalResumeActivityLocked(r);
1342 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001343 // This activity is not starting in the resumed state... which should look like we asked
1344 // it to pause+stop (but remain visible), and it has done so and reported back the
1345 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001346 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001347 "Moving to PAUSED: " + r + " (starting in paused state)");
1348 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001349 }
1350
1351 // Launch the new version setup screen if needed. We do this -after-
1352 // launching the initial activity (that is, home), so that it can have
1353 // a chance to initialize itself while in the background, making the
1354 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001355 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 mService.startSetupActivityLocked();
1357 }
1358
Dianne Hackborn465fa392014-09-14 14:21:18 -07001359 // Update any services we are bound to that might care about whether
1360 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001361 if (r.app != null) {
1362 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1363 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001364
Craig Mautner2420ead2013-04-01 17:13:20 -07001365 return true;
1366 }
1367
Craig Mautnere79d42682013-04-01 19:01:53 -07001368 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001369 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001370 // Is this activity's application already running?
1371 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001372 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001373
1374 r.task.stack.setLaunchTime(r);
1375
1376 if (app != null && app.thread != null) {
1377 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001378 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1379 || !"android".equals(r.info.packageName)) {
1380 // Don't add this if it is a platform component that is marked
1381 // to run in multiple processes, because this is actually
1382 // part of the framework so doesn't make sense to track as a
1383 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001384 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1385 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001386 }
George Mount2c92c972014-03-20 09:38:23 -07001387 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001388 return;
1389 } catch (RemoteException e) {
1390 Slog.w(TAG, "Exception when starting activity "
1391 + r.intent.getComponent().flattenToShortString(), e);
1392 }
1393
1394 // If a dead object exception was thrown -- fall through to
1395 // restart the application.
1396 }
1397
1398 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001399 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001400 }
1401
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001402 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001403 String resultWho, int requestCode, int callingPid, int callingUid,
1404 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001405 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001406 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1407 callingUid);
1408 if (startAnyPerm == PERMISSION_GRANTED) {
1409 return true;
1410 }
1411 final int componentRestriction = getComponentRestrictionForCallingPackage(
1412 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1413 final int actionRestriction = getActionRestrictionForCallingPackage(
1414 intent.getAction(), callingPackage, callingPid, callingUid);
1415 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1416 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1417 if (resultRecord != null) {
1418 resultStack.sendActivityResultLocked(-1,
1419 resultRecord, resultWho, requestCode,
1420 Activity.RESULT_CANCELED, null);
1421 }
1422 final String msg;
1423 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1424 msg = "Permission Denial: starting " + intent.toString()
1425 + " from " + callerApp + " (pid=" + callingPid
1426 + ", uid=" + callingUid + ")" + " with revoked permission "
1427 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1428 } else if (!aInfo.exported) {
1429 msg = "Permission Denial: starting " + intent.toString()
1430 + " from " + callerApp + " (pid=" + callingPid
1431 + ", uid=" + callingUid + ")"
1432 + " not exported from uid " + aInfo.applicationInfo.uid;
1433 } else {
1434 msg = "Permission Denial: starting " + intent.toString()
1435 + " from " + callerApp + " (pid=" + callingPid
1436 + ", uid=" + callingUid + ")"
1437 + " requires " + aInfo.permission;
1438 }
1439 Slog.w(TAG, msg);
1440 throw new SecurityException(msg);
1441 }
1442
1443 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1444 final String message = "Appop Denial: starting " + intent.toString()
1445 + " from " + callerApp + " (pid=" + callingPid
1446 + ", uid=" + callingUid + ")"
1447 + " requires " + AppOpsManager.permissionToOp(
1448 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1449 Slog.w(TAG, message);
1450 return false;
1451 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1452 final String message = "Appop Denial: starting " + intent.toString()
1453 + " from " + callerApp + " (pid=" + callingPid
1454 + ", uid=" + callingUid + ")"
1455 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1456 Slog.w(TAG, message);
1457 return false;
1458 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001459 if (options != null && options.getLaunchTaskId() != -1) {
1460 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1461 callingPid, callingUid);
1462 if (startInTaskPerm != PERMISSION_GRANTED) {
1463 final String msg = "Permission Denial: starting " + intent.toString()
1464 + " from " + callerApp + " (pid=" + callingPid
1465 + ", uid=" + callingUid + ") with launchTaskId="
1466 + options.getLaunchTaskId();
1467 Slog.w(TAG, msg);
1468 throw new SecurityException(msg);
1469 }
1470 }
1471
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001472 return true;
1473 }
1474
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001475 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001476 final long identity = Binder.clearCallingIdentity();
1477 try {
1478 return UserManager.get(mService.mContext).getUserInfo(userId);
1479 } finally {
1480 Binder.restoreCallingIdentity(identity);
1481 }
1482 }
1483
Svet Ganov99b60432015-06-27 13:15:22 -07001484 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001485 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001486 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1487 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001488 == PackageManager.PERMISSION_DENIED) {
1489 return ACTIVITY_RESTRICTION_PERMISSION;
1490 }
1491
Christopher Tateff7add02015-08-17 10:23:22 -07001492 if (activityInfo.permission == null) {
1493 return ACTIVITY_RESTRICTION_NONE;
1494 }
1495
Svet Ganov99b60432015-06-27 13:15:22 -07001496 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1497 if (opCode == AppOpsManager.OP_NONE) {
1498 return ACTIVITY_RESTRICTION_NONE;
1499 }
1500
1501 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1502 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001503 if (!ignoreTargetSecurity) {
1504 return ACTIVITY_RESTRICTION_APPOP;
1505 }
Svet Ganov99b60432015-06-27 13:15:22 -07001506 }
1507
1508 return ACTIVITY_RESTRICTION_NONE;
1509 }
1510
Svetoslav7008b512015-06-24 18:47:07 -07001511 private int getActionRestrictionForCallingPackage(String action,
1512 String callingPackage, int callingPid, int callingUid) {
1513 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001514 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001515 }
1516
1517 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1518 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001519 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001520 }
1521
1522 final PackageInfo packageInfo;
1523 try {
1524 packageInfo = mService.mContext.getPackageManager()
1525 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1526 } catch (PackageManager.NameNotFoundException e) {
1527 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001528 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001529 }
1530
1531 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001532 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001533 }
1534
1535 if (mService.checkPermission(permission, callingPid, callingUid) ==
1536 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001537 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001538 }
1539
1540 final int opCode = AppOpsManager.permissionToOpCode(permission);
1541 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001542 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001543 }
1544
1545 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1546 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001547 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001548 }
1549
Svet Ganov99b60432015-06-27 13:15:22 -07001550 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001551 }
1552
Wale Ogunwaled046a012015-12-24 13:05:59 -08001553 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001554 if (r == null) {
1555 // Not sure what you are trying to do, but it is not going to work...
1556 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001557 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001558 final TaskRecord task = r.task;
1559 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001560 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001561 return false;
1562 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001563 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001564 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001565 }
1566
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001567 void setLaunchSource(int uid) {
1568 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1569 }
1570
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001571 void acquireLaunchWakelock() {
1572 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1573 throw new IllegalStateException("Calling must be system uid");
1574 }
1575 mLaunchingActivity.acquire();
1576 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1577 // To be safe, don't allow the wake lock to be held for too long.
1578 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1579 }
1580 }
1581
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001582 /**
1583 * Called when the frontmost task is idle.
1584 * @return the state of mService.mBooting before this was called.
1585 */
1586 private boolean checkFinishBootingLocked() {
1587 final boolean booting = mService.mBooting;
1588 boolean enableScreen = false;
1589 mService.mBooting = false;
1590 if (!mService.mBooted) {
1591 mService.mBooted = true;
1592 enableScreen = true;
1593 }
1594 if (booting || enableScreen) {
1595 mService.postFinishBooting(booting, enableScreen);
1596 }
1597 return booting;
1598 }
1599
Craig Mautnerf3333272013-04-22 10:55:53 -07001600 // Checked.
1601 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1602 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001603 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001604
Craig Mautnerf3333272013-04-22 10:55:53 -07001605 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001606 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001607 int NS = 0;
1608 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001609 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001610 boolean activityRemoved = false;
1611
Wale Ogunwale7d701172015-03-11 15:36:30 -07001612 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001613 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001614 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1615 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001616 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1617 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001618 if (fromTimeout) {
1619 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001620 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001621
1622 // This is a hack to semi-deal with a race condition
1623 // in the client where it can be constructed with a
1624 // newer configuration from when we asked it to launch.
1625 // We'll update with whatever configuration it now says
1626 // it used to launch.
1627 if (config != null) {
1628 r.configuration = config;
1629 }
1630
1631 // We are now idle. If someone is waiting for a thumbnail from
1632 // us, we can now deliver.
1633 r.idle = true;
1634
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001635 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001636 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001637 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001638 }
1639 }
1640
1641 if (allResumedActivitiesIdle()) {
1642 if (r != null) {
1643 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001644 }
1645
1646 if (mLaunchingActivity.isHeld()) {
1647 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1648 if (VALIDATE_WAKE_LOCK_CALLER &&
1649 Binder.getCallingUid() != Process.myUid()) {
1650 throw new IllegalStateException("Calling must be system uid");
1651 }
1652 mLaunchingActivity.release();
1653 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001654 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001655 }
1656
1657 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001658 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001659 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001660 if ((NF = mFinishingActivities.size()) > 0) {
1661 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001662 mFinishingActivities.clear();
1663 }
1664
Craig Mautnerf3333272013-04-22 10:55:53 -07001665 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001666 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001667 mStartingUsers.clear();
1668 }
1669
Craig Mautnerf3333272013-04-22 10:55:53 -07001670 // Stop any activities that are scheduled to do so but have been
1671 // waiting for the next one to start.
1672 for (int i = 0; i < NS; i++) {
1673 r = stops.get(i);
1674 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001675 if (stack != null) {
1676 if (r.finishing) {
1677 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1678 } else {
1679 stack.stopActivityLocked(r);
1680 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001681 }
1682 }
1683
1684 // Finish any activities that are scheduled to do so but have been
1685 // waiting for the next one to start.
1686 for (int i = 0; i < NF; i++) {
1687 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001688 final ActivityStack stack = r.task.stack;
1689 if (stack != null) {
1690 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1691 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001692 }
1693
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001694 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001695 // Complete user switch
1696 if (startingUsers != null) {
1697 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001698 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001699 }
1700 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001701 }
1702
1703 mService.trimApplications();
1704 //dump();
1705 //mWindowManager.dump();
1706
Craig Mautnerf3333272013-04-22 10:55:53 -07001707 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001708 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001709 }
1710
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001711 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001712 }
1713
Craig Mautner8e569572013-10-11 17:36:59 -07001714 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001715 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1717 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1719 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1720 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001721 }
Craig Mautner19091252013-10-05 00:03:53 -07001722 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001723 }
1724
1725 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1729 stacks.get(stackNdx).closeSystemDialogsLocked();
1730 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001731 }
1732 }
1733
Craig Mautner93529a42013-10-04 15:03:13 -07001734 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001735 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001736 }
1737
Craig Mautner8d341ef2013-03-26 09:03:27 -07001738 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001739 * Update the last used stack id for non-current user (current user's last
1740 * used stack is the focused stack)
1741 */
1742 void updateUserStackLocked(int userId, ActivityStack stack) {
1743 if (userId != mCurrentUser) {
1744 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1745 }
1746 }
1747
1748 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001749 * @return true if some activity was finished (or would have finished if doit were true).
1750 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001751 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1752 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001753 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1755 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001757 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001758 if (stack.finishDisabledPackageActivitiesLocked(
1759 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001760 didSomething = true;
1761 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001762 }
1763 }
1764 return didSomething;
1765 }
1766
Dianne Hackborna413dc02013-07-12 12:02:55 -07001767 void updatePreviousProcessLocked(ActivityRecord r) {
1768 // Now that this process has stopped, we may want to consider
1769 // it to be the previous app to try to keep around in case
1770 // the user wants to return to it.
1771
1772 // First, found out what is currently the foreground app, so that
1773 // we don't blow away the previous app if this activity is being
1774 // hosted by the process that is actually still the foreground.
1775 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1777 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1779 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001780 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001781 if (stack.mResumedActivity != null) {
1782 fgApp = stack.mResumedActivity.app;
1783 } else if (stack.mPausingActivity != null) {
1784 fgApp = stack.mPausingActivity.app;
1785 }
1786 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001787 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001788 }
1789 }
1790
1791 // Now set this one as the previous process, only if that really
1792 // makes sense to.
1793 if (r.app != null && fgApp != null && r.app != fgApp
1794 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001795 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001796 mService.mPreviousProcess = r.app;
1797 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1798 }
1799 }
1800
Wale Ogunwaled046a012015-12-24 13:05:59 -08001801 boolean resumeFocusedStackTopActivityLocked() {
1802 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001803 }
1804
Wale Ogunwaled046a012015-12-24 13:05:59 -08001805 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001806 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001807 if (targetStack != null && isFocusedStack(targetStack)) {
1808 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001809 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001810 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1811 if (r == null || r.state != RESUMED) {
1812 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1813 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001814 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001815 }
1816
Todd Kennedy39bfee52016-02-24 10:28:21 -08001817 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1818 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1819 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1820 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1821 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1822 }
1823 }
1824 }
1825
Adrian Roos20d7df32016-01-12 18:59:43 +01001826 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1827 TaskRecord finishedTask = null;
1828 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1830 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001831 final int numStacks = stacks.size();
1832 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1833 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001834 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1835 if (stack == focusedStack || finishedTask == null) {
1836 finishedTask = t;
1837 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001838 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001839 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001840 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001841 }
1842
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001843 void finishVoiceTask(IVoiceInteractionSession session) {
1844 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1845 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1846 final int numStacks = stacks.size();
1847 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1848 final ActivityStack stack = stacks.get(stackNdx);
1849 stack.finishVoiceTask(session);
1850 }
1851 }
1852 }
1853
Andrii Kulianc27916642016-04-12 17:59:27 -07001854 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1855 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001856 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1857 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001858 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001859 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1860 // Caller wants the home activity moved with it. To accomplish this,
1861 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001862 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001863 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001864 if (task.stack == null) {
1865 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1866 + task + " to front. Stack is null");
1867 return;
1868 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001869
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001870 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001871 int stackId = options.getLaunchStackId();
1872 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001873 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001874 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001875 if (stackId == INVALID_STACK_ID) {
1876 stackId = task.getLaunchStackId();
1877 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001878 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001879 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1880 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1881 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001882 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1883 // still need moveTaskToFrontLocked() below for any transition settings.
1884 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001885 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1886 resizeStackLocked(stackId, bounds,
1887 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001888 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001889 } else {
1890 // WM resizeTask must be done after the task is moved to the correct stack,
1891 // because Task's setBounds() also updates dim layer's bounds, but that has
1892 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001893 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001894 false /* relayout */, false /* forced */);
1895 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001896 }
1897 }
1898
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001899 final ActivityRecord r = task.getTopActivity();
1900 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1901 r == null ? null : r.appTimeTracker, reason);
1902
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001903 if (DEBUG_STACK) Slog.d(TAG_STACK,
1904 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001905
Andrii Kulianc27916642016-04-12 17:59:27 -07001906 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1907 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001908 }
1909
Wale Ogunwale854809c2015-12-27 16:18:19 -08001910 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001911 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001912 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001913 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001914 return false;
1915 }
1916 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1917 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001918 }
1919
Craig Mautner967212c2013-04-13 21:10:58 -07001920 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001921 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001922 }
1923
1924 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001925 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1926 if (activityContainer != null) {
1927 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001928 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001929 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001930 return null;
1931 }
1932 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001933 }
1934
Craig Mautner967212c2013-04-13 21:10:58 -07001935 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001936 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001937 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1938 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001939 }
1940 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001941 }
1942
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001944 ActivityRecord homeActivity = getHomeActivity();
1945 if (homeActivity != null) {
1946 return homeActivity.appToken;
1947 }
1948 return null;
1949 }
1950
1951 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001952 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001953 }
1954
1955 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001956 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1957 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1958 final TaskRecord task = tasks.get(taskNdx);
1959 if (task.isHomeTask()) {
1960 final ArrayList<ActivityRecord> activities = task.mActivities;
1961 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1962 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001963 if (r.isHomeActivity()
1964 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001965 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001966 }
1967 }
1968 }
1969 }
1970 return null;
1971 }
1972
Chong Zhangb15758a2015-11-17 12:12:03 -08001973 /**
1974 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1975 * the docked stack itself, or if it's side-by-side to the docked stack.
1976 */
1977 boolean isStackDockedInEffect(int stackId) {
1978 return stackId == DOCKED_STACK_ID ||
1979 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1980 }
1981
Todd Kennedyca4d8422015-01-15 15:19:22 -08001982 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001983 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001984 ActivityContainer activityContainer =
1985 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001986 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001987 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1988 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001989 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001990 return activityContainer;
1991 }
1992
Craig Mautner34b73df2014-01-12 21:11:08 -08001993 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001994 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1995 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1996 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001997 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1998 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001999 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002000 }
2001 }
2002
Craig Mautner95da1082014-02-24 17:54:35 -08002003 void deleteActivityContainer(IActivityContainer container) {
2004 ActivityContainer activityContainer = (ActivityContainer)container;
2005 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002006 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2007 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002008 final int stackId = activityContainer.mStackId;
2009 mActivityContainers.remove(stackId);
2010 mWindowManager.removeStack(stackId);
2011 }
2012 }
2013
Jorim Jaggidc249c42015-12-15 14:57:31 -08002014 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002015 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002016 if (stackId == DOCKED_STACK_ID) {
2017 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002018 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002019 return;
2020 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002021 final ActivityStack stack = getStack(stackId);
2022 if (stack == null) {
2023 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2024 return;
2025 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002026
Jorim Jaggidc249c42015-12-15 14:57:31 -08002027 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002028 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2029 // stack size changing so things don't get out of sync.
2030 return;
2031 }
2032
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002033 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002034 mWindowManager.deferSurfaceLayout();
2035 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002036 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002037 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002038 stack.ensureVisibleActivitiesConfigurationLocked(
2039 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002040 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002041 } finally {
2042 mWindowManager.continueSurfaceLayout();
2043 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002044 }
2045 }
2046
Jorim Jaggi192086e2016-03-11 17:17:03 +01002047 void deferUpdateBounds(int stackId) {
2048 final ActivityStack stack = getStack(stackId);
2049 if (stack != null) {
2050 stack.deferUpdateBounds();
2051 }
2052 }
2053
2054 void continueUpdateBounds(int stackId) {
2055 final ActivityStack stack = getStack(stackId);
2056 if (stack != null) {
2057 stack.continueUpdateBounds();
2058 }
2059 }
2060
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002061 void notifyAppTransitionDone() {
2062 continueUpdateBounds(HOME_STACK_ID);
2063 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2064 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002065 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002066 mWindowManager.setTaskDockedResizing(taskId, false);
2067 }
2068 }
2069 mResizingTasksDuringAnimation.clear();
2070 }
2071
Jorim Jaggi192086e2016-03-11 17:17:03 +01002072 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002073 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002074 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002075
Jorim Jaggi192086e2016-03-11 17:17:03 +01002076 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2077 return;
2078 }
2079
Jorim Jaggidc249c42015-12-15 14:57:31 -08002080 mTmpBounds.clear();
2081 mTmpConfigs.clear();
2082 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002083 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002084 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2085 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002086 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002087 final TaskRecord task = tasks.get(i);
2088 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002089 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2090 // For freeform stack we don't adjust the size of the tasks to match that
2091 // of the stack, but we do try to make sure the tasks are still contained
2092 // with the bounds of the stack.
2093 tempRect2.set(task.mBounds);
2094 fitWithinBounds(tempRect2, bounds);
2095 task.updateOverrideConfiguration(tempRect2);
2096 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002097 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002098 }
2099 }
2100
2101 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2102 mTmpBounds.put(task.taskId, task.mBounds);
2103 if (tempTaskInsetBounds != null) {
2104 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2105 }
2106 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002107
2108 // We might trigger a configuration change. Save the current task bounds for freezing.
2109 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002110 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2111 mTmpBounds, mTmpInsetBounds);
2112 stack.setBounds(bounds);
2113 }
2114
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002115 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2116 final ActivityStack stack = getStack(fromStackId);
2117 if (stack == null) {
2118 return;
2119 }
2120
2121 mWindowManager.deferSurfaceLayout();
2122 try {
2123 if (fromStackId == DOCKED_STACK_ID) {
2124
2125 // We are moving all tasks from the docked stack to the fullscreen stack,
2126 // which is dismissing the docked stack, so resize all other stacks to
2127 // fullscreen here already so we don't end up with resize trashing.
2128 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2129 if (StackId.isResizeableByDockedStack(i)) {
2130 ActivityStack otherStack = getStack(i);
2131 if (otherStack != null) {
2132 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2133 true /* allowResizeInDockedMode */, DEFER_RESUME);
2134 }
2135 }
2136 }
2137
2138 // Also disable docked stack resizing since we have manually adjusted the
2139 // size of other stacks above and we don't want to trigger a docked stack
2140 // resize when we remove task from it below and it is detached from the
2141 // display because it no longer contains any tasks.
2142 mAllowDockedStackResize = false;
2143 }
2144 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2145 final int size = tasks.size();
2146 if (onTop) {
2147 for (int i = 0; i < size; i++) {
2148 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002149 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002150 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002151 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002152
2153 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2154 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002155 } else {
2156 for (int i = size - 1; i >= 0; i--) {
2157 positionTaskInStackLocked(tasks.get(i).taskId,
2158 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2159 }
2160 }
2161 } finally {
2162 mAllowDockedStackResize = true;
2163 mWindowManager.continueSurfaceLayout();
2164 }
2165 }
2166
Jorim Jaggidc249c42015-12-15 14:57:31 -08002167 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002168 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2169 boolean preserveWindows) {
2170 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2171 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2172 false /* deferResume */);
2173 }
2174
2175 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2176 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2177 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002178
2179 if (!mAllowDockedStackResize) {
2180 // Docked stack resize currently disabled.
2181 return;
2182 }
2183
Jorim Jaggidc249c42015-12-15 14:57:31 -08002184 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2185 if (stack == null) {
2186 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2187 return;
2188 }
2189
2190 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2191 mWindowManager.deferSurfaceLayout();
2192 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002193 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2194 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002195 ActivityRecord r = stack.topRunningActivityLocked();
2196 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2197 tempDockedTaskInsetBounds);
2198
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002199 // TODO: Checking for isAttached might not be needed as if the user passes in null
2200 // dockedBounds then they want the docked stack to be dismissed.
2201 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2202 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002203 // In this case we make all other static stacks fullscreen and move all
2204 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002205 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002206
2207 // stack shouldn't contain anymore activities, so nothing to resume.
2208 r = null;
2209 } else {
2210 // Docked stacks occupy a dedicated region on screen so the size of all other
2211 // static stacks need to be adjusted so they don't overlap with the docked stack.
2212 // We get the bounds to use from window manager which has been adjusted for any
2213 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002214 mWindowManager.getStackDockedModeBounds(
2215 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002216 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002217 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2218 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2219 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002220 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002221 }
2222 }
2223 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002224 if (!deferResume) {
2225 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2226 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002227 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002228 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002229 mWindowManager.continueSurfaceLayout();
2230 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2231 }
2232
2233 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2234 tempDockedTaskBounds != null
2235 || tempDockedTaskInsetBounds != null
2236 || tempOtherTaskBounds != null
2237 || tempOtherTaskInsetBounds != null);
2238 }
2239
Robert Carr0d00c2e2016-02-29 17:45:02 -08002240 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2241 final ActivityStack stack = getStack(PINNED_STACK_ID);
2242 if (stack == null) {
2243 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2244 return;
2245 }
2246 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2247 mWindowManager.deferSurfaceLayout();
2248 try {
2249 ActivityRecord r = stack.topRunningActivityLocked();
2250 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2251 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002252 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002253 } finally {
2254 mWindowManager.continueSurfaceLayout();
2255 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2256 }
2257 }
2258
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002259 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2260 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002261 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002262 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002263 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002264 }
2265
Chong Zhang87b21722015-09-21 15:39:51 -07002266 // If this is a forced resize, let it go through even if the bounds is not changing,
2267 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002268 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002269 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002270 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002271 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002272 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002273 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002274
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002275 if (!mWindowManager.isValidTaskId(task.taskId)) {
2276 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002277 // All we can do for now is update the bounds so it can be used when the task is
2278 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002279 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002280 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2281 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002282 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002283 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002284 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002285 }
2286
Chong Zhang6de2ae82015-09-30 18:25:21 -07002287 // Do not move the task to another stack here.
2288 // This method assumes that the task is already placed in the right stack.
2289 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002290
Chong Zhang6de2ae82015-09-30 18:25:21 -07002291 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002292
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002293 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002294 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002295 // way and the activity was kept the way it was. If it's false, it means the activity had
2296 // to be relaunched due to configuration change.
2297 boolean kept = true;
2298 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002299 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002300 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002301 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002302 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002303
2304 if (!deferResume) {
2305
2306 // All other activities must be made visible with their correct configuration.
2307 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2308 if (!kept) {
2309 resumeFocusedStackTopActivityLocked();
2310 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002311 }
2312 }
2313 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002314 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002315
2316 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002317 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002318 }
2319
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002320 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002321 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2322 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002323 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002324 }
2325
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002326 ActivityContainer activityContainer = new ActivityContainer(stackId);
2327 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002328 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002329 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002330 }
2331
2332 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002333 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002334 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2335 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002336 break;
2337 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002338 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002339 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002340 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002341 }
2342
Chong Zhang5dcb2752015-08-18 13:50:26 -07002343 /**
2344 * Restores a recent task to a stack
2345 * @param task The recent task to be restored.
2346 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002347 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002348 * @return true if the task has been restored successfully.
2349 */
2350 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2351 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002352 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002353 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2354 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2355 // Put it in the fullscreen stack.
2356 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002357 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002358
Wale Ogunwale706ed792015-08-02 10:29:44 -07002359 if (task.stack != null) {
2360 // Task has already been restored once. See if we need to do anything more
2361 if (task.stack.mStackId == stackId) {
2362 // Nothing else to do since it is already restored in the right stack.
2363 return true;
2364 }
2365 // Remove current stack association, so we can re-associate the task with the
2366 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002367 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002368 }
2369
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002370 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002371 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002372
2373 if (stack == null) {
2374 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002375 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2376 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002377 return false;
2378 }
2379
Wale Ogunwale5f986092015-12-04 15:35:38 -08002380 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002381 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2382 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002383 final ArrayList<ActivityRecord> activities = task.mActivities;
2384 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002385 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002386 }
2387 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002388 }
2389
Wale Ogunwale040b4702015-08-06 18:10:50 -07002390 /**
2391 * Moves the specified task record to the input stack id.
2392 * WARNING: This method performs an unchecked/raw move of the task and
2393 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002394 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002395 * @param task Task to move.
2396 * @param stackId Id of stack to move task to.
2397 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002398 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002399 * @param reason Reason the task is been moved.
2400 * @return The stack the task was moved to.
2401 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002402 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002403 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002404
2405 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2406 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2407 + "support multi-window task=" + task + " to stackId=" + stackId);
2408 }
2409
Wale Ogunwale06579d62016-04-30 15:29:06 -07002410 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002411 final ActivityStack prevStack = task.stack;
2412 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002413 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002414 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2415 // Whenever we are moving the top activity from the front stack we want to make sure to move
2416 // the stack to the front.
2417 final boolean wasFront = isFrontStack(prevStack)
2418 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002419
Chong Zhangd545dce2016-02-29 18:09:17 -08002420 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002421 // We don't allow moving a unresizeable task to the docked stack since the docked
2422 // stack is used for split-screen mode and will cause things like the docked divider to
2423 // show up. We instead leave the task in its current stack or move it to the fullscreen
2424 // stack if it isn't currently in a stack.
2425 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002426 Slog.w(TAG, "Can not move unresizeable task=" + task
2427 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2428 }
2429
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002430 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2431 // if a docked stack is created below which will lead to the stack we are moving from and
2432 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002433 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002434 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002435 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002436 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002437 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002438
2439 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002440 // move focus to the new stack by moving the stack to the front.
2441 stack.moveToFrontAndResumeStateIfNeeded(
2442 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002443
Wale Ogunwale040b4702015-08-06 18:10:50 -07002444 return stack;
2445 }
2446
Chong Zhange4fbd322016-03-01 14:44:03 -08002447 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002448 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002449 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2450 false /* deferResume */);
2451 }
2452
2453 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2454 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002455 final TaskRecord task = anyTaskForIdLocked(taskId);
2456 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002457 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002458 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002459 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002460
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002461 if (task.stack != null && task.stack.mStackId == stackId) {
2462 // You are already in the right stack silly...
2463 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002464 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002465 }
2466
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002467 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2468 throw new IllegalArgumentException("moveTaskToStack:"
2469 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2470 }
2471
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002472 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002473 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002474 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002475 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002476 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002477 // We are about to relaunch the activity because its configuration changed due to
2478 // being maximized, i.e. size change. The activity will first remove the old window
2479 // and then add a new one. This call will tell window manager about this, so it can
2480 // preserve the old window until the new one is drawn. This prevents having a gap
2481 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002482 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002483 // Note here we always set the replacing window first, as the flags might be needed
2484 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002485 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002486 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002487
2488 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002489 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002490 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002491 try {
2492 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002493 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002494 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002495
Wale Ogunwale961f4852016-02-01 20:25:54 -08002496 if (!animate) {
2497 stack.mNoAnimActivities.add(topActivity);
2498 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002499
Wale Ogunwale961f4852016-02-01 20:25:54 -08002500 // We might trigger a configuration change. Save the current task bounds for freezing.
2501 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2502
2503 // Make sure the task has the appropriate bounds/size for the stack it is in.
2504 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002505 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2506 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002507 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2508 Rect bounds = task.getLaunchBounds();
2509 if (bounds == null) {
2510 stack.layoutTaskInStack(task, null);
2511 bounds = task.mBounds;
2512 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002513 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2514 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002515 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002516 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2517 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002518 }
2519 } finally {
2520 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002521 }
2522
2523 if (mightReplaceWindow) {
2524 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2525 // window), we need to clear the replace window settings. Otherwise, we schedule a
2526 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002527 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002528 }
2529
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002530 if (!deferResume) {
2531
2532 // The task might have already been running and its visibility needs to be synchronized with
2533 // the visibility of the stack / windows.
2534 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2535 resumeFocusedStackTopActivityLocked();
2536 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002537
Jorim Jaggid53f0922016-04-06 22:16:23 -07002538 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002539
2540 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002541 }
2542
Wale Ogunwale079a0042015-10-24 11:44:07 -07002543 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2544 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2545 if (stack == null) {
2546 throw new IllegalArgumentException(
2547 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2548 }
2549
2550 final ActivityRecord r = stack.topRunningActivityLocked();
2551 if (r == null) {
2552 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2553 + " in stack=" + stack);
2554 return false;
2555 }
2556
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002557 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002558 Slog.w(TAG,
2559 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002560 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002561 return false;
2562 }
2563
Wale Ogunwale480dca02016-02-06 13:58:29 -08002564 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002565 return true;
2566 }
2567
Wale Ogunwale480dca02016-02-06 13:58:29 -08002568 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2569 mWindowManager.deferSurfaceLayout();
2570 try {
2571 final TaskRecord task = r.task;
2572
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002573 if (r == task.stack.getVisibleBehindActivity()) {
2574 // An activity can't be pinned and visible behind at the same time. Go ahead and
2575 // release it from been visible behind before pinning.
2576 requestVisibleBehindLocked(r, false);
2577 }
2578
Wale Ogunwale480dca02016-02-06 13:58:29 -08002579 // Need to make sure the pinned stack exist so we can resize it below...
2580 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2581
2582 // Resize the pinned stack to match the current size of the task the activity we are
2583 // going to be moving is currently contained in. We do this to have the right starting
2584 // animation bounds for the pinned stack to the desired bounds the caller wants.
2585 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2586 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002587 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002588
2589 if (task.mActivities.size() == 1) {
2590 // There is only one activity in the task. So, we can just move the task over to
2591 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002592 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2593 // Move the home stack forward if the task we just moved to the pinned stack
2594 // was launched from home so home should be visible behind it.
2595 moveHomeStackToFront(reason);
2596 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002597 moveTaskToStackLocked(
2598 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2599 } else {
2600 stack.moveActivityToStack(r);
2601 }
2602 } finally {
2603 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002604 }
2605
Wale Ogunwale480dca02016-02-06 13:58:29 -08002606 // The task might have already been running and its visibility needs to be synchronized
2607 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002608 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002609 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002610
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002611 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002612 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002613 }
2614
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002615 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2616 final TaskRecord task = anyTaskForIdLocked(taskId);
2617 if (task == null) {
2618 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2619 return;
2620 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002621 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2622
2623 task.updateOverrideConfigurationForStack(stack);
2624
2625 mWindowManager.positionTaskInStack(
2626 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002627 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002628 // The task might have already been running and its visibility needs to be synchronized with
2629 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002630 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002631 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002632 }
2633
Craig Mautnerac6f8432013-07-17 13:24:59 -07002634 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002635 mTmpFindTaskResult.r = null;
2636 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002637 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002638 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2639 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002640 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2641 final ActivityStack stack = stacks.get(stackNdx);
2642 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002643 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002644 continue;
2645 }
2646 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002647 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2648 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 continue;
2650 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002651 stack.findTaskLocked(r, mTmpFindTaskResult);
2652 // It is possible to have task in multiple stacks with the same root affinity.
2653 // If the match we found was based on root affinity we keep on looking to see if
2654 // there is a better match in another stack. We eventually return the match based
2655 // on root affinity if we don't find a better match.
2656 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2657 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002658 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002659 }
2660 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002661 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2662 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002663 }
2664
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002665 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2666 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002667 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2668 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002669 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002670 final ActivityRecord ar = stacks.get(stackNdx)
2671 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002672 if (ar != null) {
2673 return ar;
2674 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002675 }
2676 }
2677 return null;
2678 }
2679
Craig Mautner8d341ef2013-03-26 09:03:27 -07002680 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002681 scheduleSleepTimeout();
2682 if (!mGoingToSleep.isHeld()) {
2683 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002684 if (mLaunchingActivity.isHeld()) {
2685 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2686 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002687 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002688 mLaunchingActivity.release();
2689 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002690 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002692 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002693 }
2694
2695 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002696 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002697
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002698 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 final long endTime = System.currentTimeMillis() + timeout;
2700 while (true) {
2701 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2703 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002704 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2705 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2706 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002707 }
2708 if (cantShutdown) {
2709 long timeRemaining = endTime - System.currentTimeMillis();
2710 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002711 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002712 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002713 } catch (InterruptedException e) {
2714 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002715 } else {
2716 Slog.w(TAG, "Activity manager shutdown timed out");
2717 timedout = true;
2718 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002719 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002720 } else {
2721 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002722 }
2723 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002724
2725 // Force checkReadyForSleep to complete.
2726 mSleepTimeout = true;
2727 checkReadyForSleepLocked();
2728
Craig Mautner8d341ef2013-03-26 09:03:27 -07002729 return timedout;
2730 }
2731
2732 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002733 removeSleepTimeouts();
2734 if (mGoingToSleep.isHeld()) {
2735 mGoingToSleep.release();
2736 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2740 final ActivityStack stack = stacks.get(stackNdx);
2741 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002742 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002743 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 }
Craig Mautner5314a402013-09-26 12:40:16 -07002745 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002746 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002747 mGoingToSleepActivities.clear();
2748 }
2749
2750 void activitySleptLocked(ActivityRecord r) {
2751 mGoingToSleepActivities.remove(r);
2752 checkReadyForSleepLocked();
2753 }
2754
2755 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002756 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002757 // Do not care.
2758 return;
2759 }
2760
2761 if (!mSleepTimeout) {
2762 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2766 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2767 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002768 }
2769
2770 if (mStoppingActivities.size() > 0) {
2771 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002772 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002773 + mStoppingActivities.size() + " activities");
2774 scheduleIdleLocked();
2775 dontSleep = true;
2776 }
2777
2778 if (mGoingToSleepActivities.size() > 0) {
2779 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002780 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002781 + mGoingToSleepActivities.size() + " activities");
2782 dontSleep = true;
2783 }
2784
2785 if (dontSleep) {
2786 return;
2787 }
2788 }
2789
Wei Wang65c7a152016-06-02 18:51:22 -07002790 // Send launch end powerhint before going sleep
2791 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2792
Craig Mautnere0a38842013-12-16 16:14:02 -08002793 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2794 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002795 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2796 stacks.get(stackNdx).goToSleep();
2797 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002798 }
2799
2800 removeSleepTimeouts();
2801
2802 if (mGoingToSleep.isHeld()) {
2803 mGoingToSleep.release();
2804 }
2805 if (mService.mShuttingDown) {
2806 mService.notifyAll();
2807 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002808 }
2809
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002810 boolean reportResumedActivityLocked(ActivityRecord r) {
2811 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002812 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002813 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002814 }
2815 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002816 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002817 mWindowManager.executeAppTransition();
2818 return true;
2819 }
2820 return false;
2821 }
2822
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2825 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002826 int stackNdx = stacks.size() - 1;
2827 while (stackNdx >= 0) {
2828 stacks.get(stackNdx).handleAppCrashLocked(app);
2829 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 }
2832 }
2833
Jose Lima4b6c6692014-08-12 17:41:12 -07002834 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002835 final ActivityStack stack = r.task.stack;
2836 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2838 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002839 return false;
2840 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002841
2842 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2843 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2844 + " visible=" + visible + " stackId=" + stack.mStackId
2845 + " can't contain visible behind activities");
2846 return false;
2847 }
2848
Jose Lima4b6c6692014-08-12 17:41:12 -07002849 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002850 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2851 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002852
2853 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002854 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002855 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002856 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002857 return true;
2858 }
2859
2860 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002861 if (visible && top.fullscreen) {
2862 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002863 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2864 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2865 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2866 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002867 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002868 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2869 // Only the activity set as currently visible behind should actively reset its
2870 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002871 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2872 "requestVisibleBehind: returning visible=" + visible
2873 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2874 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002875 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002876 }
2877
Jose Lima4b6c6692014-08-12 17:41:12 -07002878 stack.setVisibleBehindActivity(visible ? r : null);
2879 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002880 // If there is a translucent home activity, we need to force it stop being translucent,
2881 // because we can't depend on the application to necessarily perform that operation.
2882 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002883 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002884 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002885 mService.convertFromTranslucent(next.appToken);
2886 }
2887 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002888 if (top.app != null && top.app.thread != null) {
2889 // Notify the top app of the change.
2890 try {
2891 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2892 } catch (RemoteException e) {
2893 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002894 }
2895 return true;
2896 }
2897
Craig Mautnerbb742462014-07-07 15:28:55 -07002898 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2899 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002900 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002901 final ActivityStack stack = task.stack;
2902
2903 r.mLaunchTaskBehind = false;
2904 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002905 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002906 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002907 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002908
2909 // When launching tasks behind, update the last active time of the top task after the new
2910 // task has been shown briefly
2911 final ActivityRecord top = stack.topActivity();
2912 if (top != null) {
2913 top.task.touchActiveTime();
2914 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002915 }
2916
2917 void scheduleLaunchTaskBehindComplete(IBinder token) {
2918 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2919 }
2920
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002921 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2922 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002923 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 final int topStackNdx = stacks.size() - 1;
2927 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2928 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002929 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002930 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002931 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002932 }
2933
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002934 void invalidateTaskLayers() {
2935 mTaskLayersChanged = true;
2936 }
2937
2938 void rankTaskLayersIfNeeded() {
2939 if (!mTaskLayersChanged) {
2940 return;
2941 }
2942 mTaskLayersChanged = false;
2943 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2945 int baseLayer = 0;
2946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2947 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2948 }
2949 }
2950 }
2951
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002952 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2954 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2955 final int topStackNdx = stacks.size() - 1;
2956 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2957 final ActivityStack stack = stacks.get(stackNdx);
2958 stack.clearOtherAppTimeTrackers(except);
2959 }
2960 }
2961 }
2962
Craig Mautner8d341ef2013-03-26 09:03:27 -07002963 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2965 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 final int numStacks = stacks.size();
2967 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2968 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002969 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002970 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
2972 }
2973
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002974 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2975 // Examine all activities currently running in the process.
2976 TaskRecord firstTask = null;
2977 // Tasks is non-null only if two or more tasks are found.
2978 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002979 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2980 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002981 ActivityRecord r = app.activities.get(i);
2982 // First, if we find an activity that is in the process of being destroyed,
2983 // then we just aren't going to do anything for now; we want things to settle
2984 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002985 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002986 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002987 return;
2988 }
2989 // Don't consider any activies that are currently not in a state where they
2990 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002991 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2992 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002993 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002994 continue;
2995 }
2996 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002997 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002998 + " from " + r);
2999 if (firstTask == null) {
3000 firstTask = r.task;
3001 } else if (firstTask != r.task) {
3002 if (tasks == null) {
3003 tasks = new ArraySet<>();
3004 tasks.add(firstTask);
3005 }
3006 tasks.add(r.task);
3007 }
3008 }
3009 }
3010 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003011 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003012 return;
3013 }
3014 // If we have activities in multiple tasks that are in a position to be destroyed,
3015 // let's iterate through the tasks and release the oldest one.
3016 final int numDisplays = mActivityDisplays.size();
3017 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3018 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3019 // Step through all stacks starting from behind, to hit the oldest things first.
3020 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3021 final ActivityStack stack = stacks.get(stackNdx);
3022 // Try to release activities in this stack; if we manage to, we are done.
3023 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3024 return;
3025 }
3026 }
3027 }
3028 }
3029
Amith Yamasani37a40c22015-06-17 13:25:42 -07003030 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003031 final int focusStackId = mFocusedStack.getStackId();
3032 // We dismiss the docked stack whenever we switch users.
3033 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3034
3035 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003036 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003037 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003038
Craig Mautner858d8a62013-04-23 17:08:34 -07003039 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003040 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3041 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003042 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003043 final ActivityStack stack = stacks.get(stackNdx);
3044 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003045 TaskRecord task = stack.topTask();
3046 if (task != null) {
3047 mWindowManager.moveTaskToTop(task.taskId);
3048 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003050 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003051
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003052 ActivityStack stack = getStack(restoreStackId);
3053 if (stack == null) {
3054 stack = mHomeStack;
3055 }
3056 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003057 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003058 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003059 } else {
3060 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003061 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003062 }
Craig Mautner93529a42013-10-04 15:03:13 -07003063 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003064 }
3065
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003066 /** Checks whether the userid is a profile of the current user. */
3067 boolean isCurrentProfileLocked(int userId) {
3068 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003069 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003070 }
3071
Chong Zhang45c25ce2015-08-10 22:18:26 -07003072 /** Checks whether the activity should be shown for current user. */
3073 boolean okToShowLocked(ActivityRecord r) {
Nicolas Prevot1219c922016-06-20 17:25:12 +01003074 return r != null && ((r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
3075 || (isCurrentProfileLocked(r.userId)
3076 && !mService.mUserController.isUserStoppingOrShuttingDownLocked(r.userId)));
Chong Zhang45c25ce2015-08-10 22:18:26 -07003077 }
3078
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003080 ArrayList<ActivityRecord> stops = null;
3081
3082 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003083 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3084 ActivityRecord s = mStoppingActivities.get(activityNdx);
Wale Ogunwale3c519302016-07-14 09:17:59 -07003085 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003086 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003087 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3088 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003089 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003090 if (s.finishing) {
3091 // If this activity is finishing, it is sitting on top of
3092 // everyone else but we now know it is no longer needed...
3093 // so get rid of it. Otherwise, we need to go through the
3094 // normal flow and hide it once we determine that it is
3095 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003096 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003097 mWindowManager.setAppVisibility(s.appToken, false);
Wale Ogunwale3c519302016-07-14 09:17:59 -07003098 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003099 }
3100 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003101 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003102 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003103 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003104 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003105 }
3106 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003107 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003108 }
3109 }
3110
3111 return stops;
3112 }
3113
Craig Mautnercf910b02013-04-23 11:23:27 -07003114 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003115 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3116 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3117 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3118 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003119 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003120 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003121 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003122 if (r == null) Slog.e(TAG,
3123 "validateTop...: null top activity, stack=" + stack);
3124 else {
3125 final ActivityRecord pausing = stack.mPausingActivity;
3126 if (pausing != null && pausing == r) Slog.e(TAG,
3127 "validateTop...: top stack has pausing activity r=" + r
3128 + " state=" + state);
3129 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3130 "validateTop...: activity in front not resumed r=" + r
3131 + " state=" + state);
3132 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003133 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003134 final ActivityRecord resumed = stack.mResumedActivity;
3135 if (resumed != null && resumed == r) Slog.e(TAG,
3136 "validateTop...: back stack has resumed activity r=" + r
3137 + " state=" + state);
3138 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3139 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003140 }
3141 }
3142 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003143 }
3144
Craig Mautnere0570202015-05-13 13:06:11 -07003145 private String lockTaskModeToString() {
3146 switch (mLockTaskModeState) {
3147 case LOCK_TASK_MODE_LOCKED:
3148 return "LOCKED";
3149 case LOCK_TASK_MODE_PINNED:
3150 return "PINNED";
3151 case LOCK_TASK_MODE_NONE:
3152 return "NONE";
3153 default: return "unknown=" + mLockTaskModeState;
3154 }
3155 }
3156
Craig Mautner27084302013-03-25 08:05:25 -07003157 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003158 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003160 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003161 pw.print(prefix);
3162 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003163 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003164 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003165 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3166 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3167 if (packages.size() > 0) {
3168 pw.println(" mLockTaskPackages (userId:packages)=");
3169 for (int i = 0; i < packages.size(); ++i) {
3170 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3171 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3172 }
3173 }
3174 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003175 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003176
Craig Mautner20e72272013-04-01 13:45:53 -07003177 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003178 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003179 }
3180
Dianne Hackborn390517b2013-05-30 15:03:32 -07003181 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3182 boolean needSep, String prefix) {
3183 if (activity != null) {
3184 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3185 if (needSep) {
3186 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003187 }
3188 pw.print(prefix);
3189 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003190 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003191 }
3192 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003193 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003194 }
3195
Craig Mautner8d341ef2013-03-26 09:03:27 -07003196 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3197 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003198 boolean printed = false;
3199 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003200 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3201 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003202 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003203 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003205 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003206 final ActivityStack stack = stacks.get(stackNdx);
3207 StringBuilder stackHeader = new StringBuilder(128);
3208 stackHeader.append(" Stack #");
3209 stackHeader.append(stack.mStackId);
3210 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003211 stackHeader.append("\n");
3212 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3213 stackHeader.append("\n");
3214 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003215 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3216 needSep, stackHeader.toString());
3217 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3218 !dumpAll, false, dumpPackage, true,
3219 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003220
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 needSep = printed;
3222 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3223 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003224 if (pr) {
3225 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003226 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003227 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3229 " mResumedActivity: ");
3230 if (pr) {
3231 printed = true;
3232 needSep = false;
3233 }
3234 if (dumpAll) {
3235 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3236 " mLastPausedActivity: ");
3237 if (pr) {
3238 printed = true;
3239 needSep = true;
3240 }
3241 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3242 needSep, " mLastNoHistoryActivity: ");
3243 }
3244 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003245 }
3246 }
3247
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003248 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3249 false, dumpPackage, true, " Activities waiting to finish:", null);
3250 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3251 false, dumpPackage, true, " Activities waiting to stop:", null);
3252 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3253 false, dumpPackage, true, " Activities waiting for another to become visible:",
3254 null);
3255 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3256 false, dumpPackage, true, " Activities waiting to sleep:", null);
3257 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3258 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003259
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003260 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003261 }
3262
Dianne Hackborn390517b2013-05-30 15:03:32 -07003263 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003264 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003265 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003266 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003267 String innerPrefix = null;
3268 String[] args = null;
3269 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003270 for (int i=list.size()-1; i>=0; i--) {
3271 final ActivityRecord r = list.get(i);
3272 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3273 continue;
3274 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003275 if (innerPrefix == null) {
3276 innerPrefix = prefix + " ";
3277 args = new String[0];
3278 }
3279 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003280 final boolean full = !brief && (complete || !r.isInHistory());
3281 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003282 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003283 needNL = false;
3284 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003285 if (header1 != null) {
3286 pw.println(header1);
3287 header1 = null;
3288 }
3289 if (header2 != null) {
3290 pw.println(header2);
3291 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003292 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 if (lastTask != r.task) {
3294 lastTask = r.task;
3295 pw.print(prefix);
3296 pw.print(full ? "* " : " ");
3297 pw.println(lastTask);
3298 if (full) {
3299 lastTask.dump(pw, prefix + " ");
3300 } else if (complete) {
3301 // Complete + brief == give a summary. Isn't that obvious?!?
3302 if (lastTask.intent != null) {
3303 pw.print(prefix); pw.print(" ");
3304 pw.println(lastTask.intent.toInsecureStringWithClip());
3305 }
3306 }
3307 }
3308 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3309 pw.print(" #"); pw.print(i); pw.print(": ");
3310 pw.println(r);
3311 if (full) {
3312 r.dump(pw, innerPrefix);
3313 } else if (complete) {
3314 // Complete + brief == give a summary. Isn't that obvious?!?
3315 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3316 if (r.app != null) {
3317 pw.print(innerPrefix); pw.println(r.app);
3318 }
3319 }
3320 if (client && r.app != null && r.app.thread != null) {
3321 // flush anything that is already in the PrintWriter since the thread is going
3322 // to write to the file descriptor directly
3323 pw.flush();
3324 try {
3325 TransferPipe tp = new TransferPipe();
3326 try {
3327 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3328 r.appToken, innerPrefix, args);
3329 // Short timeout, since blocking here can
3330 // deadlock with the application.
3331 tp.go(fd, 2000);
3332 } finally {
3333 tp.kill();
3334 }
3335 } catch (IOException e) {
3336 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3337 } catch (RemoteException e) {
3338 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3339 }
3340 needNL = true;
3341 }
3342 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003343 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003345
Craig Mautnerf3333272013-04-22 10:55:53 -07003346 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003347 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3348 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003349 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3350 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003351 }
3352
3353 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003354 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003355 }
3356
3357 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003358 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3359 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003360 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3361 }
3362
Craig Mautner05d29032013-05-03 13:40:13 -07003363 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003364 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3365 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3366 }
Craig Mautner05d29032013-05-03 13:40:13 -07003367 }
3368
Craig Mautner0eea92c2013-05-16 13:35:39 -07003369 void removeSleepTimeouts() {
3370 mSleepTimeout = false;
3371 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3372 }
3373
3374 final void scheduleSleepTimeout() {
3375 removeSleepTimeouts();
3376 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3377 }
3378
Craig Mautner4a1cb222013-12-04 16:14:06 -08003379 @Override
3380 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003381 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3383 }
3384
3385 @Override
3386 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003387 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003388 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3389 }
3390
3391 @Override
3392 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003393 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003394 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3395 }
3396
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003397 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003398 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003399 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003400 newDisplay = mActivityDisplays.get(displayId) == null;
3401 if (newDisplay) {
3402 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003403 if (activityDisplay.mDisplay == null) {
3404 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3405 return;
3406 }
Craig Mautner4504de52013-12-20 09:06:56 -08003407 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003408 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003409 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 }
Craig Mautner4504de52013-12-20 09:06:56 -08003411 if (newDisplay) {
3412 mWindowManager.onDisplayAdded(displayId);
3413 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414 }
3415
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003416 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003417 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003418 mService.mContext.getResources().getDimensionPixelSize(
3419 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003420 }
3421
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003422 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003423 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003424 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3425 if (activityDisplay != null) {
3426 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003428 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003429 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003430 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 }
3432 }
3433 mWindowManager.onDisplayRemoved(displayId);
3434 }
3435
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003436 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003438 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3439 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003440 // TODO: Update the bounds.
3441 }
3442 }
3443 mWindowManager.onDisplayChanged(displayId);
3444 }
3445
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003446 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003447 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 StackInfo info = new StackInfo();
3449 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3450 info.displayId = Display.DEFAULT_DISPLAY;
3451 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003452 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003453 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003454 info.position = display != null
3455 ? display.mStacks.indexOf(stack)
3456 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003457
3458 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3459 final int numTasks = tasks.size();
3460 int[] taskIds = new int[numTasks];
3461 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003462 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003463 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 for (int i = 0; i < numTasks; ++i) {
3465 final TaskRecord task = tasks.get(i);
3466 taskIds[i] = task.taskId;
3467 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3468 : task.realActivity != null ? task.realActivity.flattenToString()
3469 : task.getTopActivity() != null ? task.getTopActivity().packageName
3470 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003471 taskBounds[i] = new Rect();
3472 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003473 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003474 }
3475 info.taskIds = taskIds;
3476 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003477 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003478 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003479
3480 final ActivityRecord top = stack.topRunningActivityLocked();
3481 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003482 return info;
3483 }
3484
3485 StackInfo getStackInfoLocked(int stackId) {
3486 ActivityStack stack = getStack(stackId);
3487 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003488 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 }
3490 return null;
3491 }
3492
3493 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003494 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003495 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3496 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003498 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 }
3500 }
3501 return list;
3502 }
3503
Craig Mautner15df08a2015-04-01 12:17:18 -07003504 TaskRecord getLockedTaskLocked() {
3505 final int top = mLockTaskModeTasks.size() - 1;
3506 if (top >= 0) {
3507 return mLockTaskModeTasks.get(top);
3508 }
3509 return null;
3510 }
3511
3512 boolean isLockedTask(TaskRecord task) {
3513 return mLockTaskModeTasks.contains(task);
3514 }
3515
3516 boolean isLastLockedTask(TaskRecord task) {
3517 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3518 }
3519
3520 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003521 if (!mLockTaskModeTasks.remove(task)) {
3522 return;
3523 }
3524 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3525 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003526 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003527 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3528 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003529 final Message lockTaskMsg = Message.obtain();
3530 lockTaskMsg.arg1 = task.userId;
3531 lockTaskMsg.what = LOCK_TASK_END_MSG;
3532 mHandler.sendMessage(lockTaskMsg);
3533 }
3534 }
3535
Andrii Kulianc27916642016-04-12 17:59:27 -07003536 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3537 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3538 false /* forceNonResizable */);
3539 }
3540
Jorim Jaggid53f0922016-04-06 22:16:23 -07003541 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003542 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003543 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3544 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003545 return;
3546 }
3547
Jorim Jaggicd13d332016-04-27 15:40:20 -07003548 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003549 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003550 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003551 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3552
Andrii Kulianc27916642016-04-12 17:59:27 -07003553 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3554 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003555 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003556 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3557 && !topActivity.noDisplay) {
3558 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003559 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3560 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003561 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003562 }
3563
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003564 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003565 if (mLockTaskNotify != null) {
3566 mLockTaskNotify.showToast(mLockTaskModeState);
3567 }
Jason Monka8f569c2014-07-07 12:15:21 -04003568 }
3569
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003570 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3571 if (mLockTaskModeTasks.contains(task)) {
3572 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3573 }
3574 }
3575
Craig Mautner432f64e2015-05-20 14:59:57 -07003576 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3577 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003578 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003579 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003580 final TaskRecord lockedTask = getLockedTaskLocked();
3581 if (lockedTask != null) {
3582 removeLockedTaskLocked(lockedTask);
3583 if (!mLockTaskModeTasks.isEmpty()) {
3584 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003585 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3586 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003587 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003588 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003589 return;
3590 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003591 }
Craig Mautnere0570202015-05-13 13:06:11 -07003592 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3593 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003594 return;
3595 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003596
3597 // Should have already been checked, but do it again.
3598 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003599 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3600 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003601 return;
3602 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003603 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003604 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003605 return;
3606 }
3607
3608 if (mLockTaskModeTasks.isEmpty()) {
3609 // First locktask.
3610 final Message lockTaskMsg = Message.obtain();
3611 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3612 lockTaskMsg.arg1 = task.userId;
3613 lockTaskMsg.what = LOCK_TASK_START_MSG;
3614 lockTaskMsg.arg2 = lockTaskModeState;
3615 mHandler.sendMessage(lockTaskMsg);
3616 }
3617 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003618 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3619 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003620 mLockTaskModeTasks.remove(task);
3621 mLockTaskModeTasks.add(task);
3622
3623 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003624 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003625 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003626
3627 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003628 findTaskToMoveToFrontLocked(task, 0, null, reason,
3629 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003630 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003631 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3632 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3633 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003634 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003635 }
3636
3637 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003638 return isLockTaskModeViolation(task, false);
3639 }
3640
3641 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3642 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003643 return false;
3644 }
3645 final int lockTaskAuth = task.mLockTaskAuth;
3646 switch (lockTaskAuth) {
3647 case LOCK_TASK_AUTH_DONT_LOCK:
3648 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003649 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003650 case LOCK_TASK_AUTH_LAUNCHABLE:
3651 case LOCK_TASK_AUTH_WHITELISTED:
3652 return false;
3653 case LOCK_TASK_AUTH_PINNABLE:
3654 // Pinnable tasks can't be launched on top of locktask tasks.
3655 return !mLockTaskModeTasks.isEmpty();
3656 default:
3657 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3658 return true;
3659 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003660 }
3661
Craig Mautner15df08a2015-04-01 12:17:18 -07003662 void onLockTaskPackagesUpdatedLocked() {
3663 boolean didSomething = false;
3664 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3665 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003666 final boolean wasWhitelisted =
3667 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3668 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003669 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003670 final boolean isWhitelisted =
3671 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3672 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3673 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003674 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003675 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3676 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003677 removeLockedTaskLocked(lockedTask);
3678 lockedTask.performClearTaskLocked();
3679 didSomething = true;
3680 }
3681 }
3682 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3683 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3684 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3685 final ActivityStack stack = stacks.get(stackNdx);
3686 stack.onLockTaskPackagesUpdatedLocked();
3687 }
3688 }
Craig Mautnere0570202015-05-13 13:06:11 -07003689 final ActivityRecord r = topRunningActivityLocked();
3690 final TaskRecord task = r != null ? r.task : null;
3691 if (mLockTaskModeTasks.isEmpty() && task != null
3692 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3693 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003694 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3695 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3696 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3697 false);
3698 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003699 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003700 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003701 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003702 }
3703 }
3704
Benjamin Franz43261142015-02-11 15:59:44 +00003705 int getLockTaskModeState() {
3706 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003707 }
3708
Jorim Jaggife89d122015-12-22 16:28:44 +01003709 void activityRelaunchedLocked(IBinder token) {
3710 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003711 if (mService.isSleepingOrShuttingDownLocked()) {
3712 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3713 if (r != null) {
3714 r.setSleeping(true, true);
3715 }
3716 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003717 }
3718
3719 void activityRelaunchingLocked(ActivityRecord r) {
3720 mWindowManager.notifyAppRelaunching(r.appToken);
3721 }
3722
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003723 void logStackState() {
3724 mActivityMetricsLogger.logWindowState();
3725 }
3726
Andrii Kulian933076d2016-03-29 17:04:42 -07003727 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003728 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3729 final ActivityRecord r = task.mActivities.get(i);
3730 if (r.app != null && r.app.thread != null) {
3731 mMultiWindowModeChangedActivities.add(r);
3732 }
3733 }
3734
3735 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3736 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3737 }
3738 }
3739
Andrii Kulian933076d2016-03-29 17:04:42 -07003740 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003741 final ActivityStack stack = task.stack;
3742 if (prevStack == null || prevStack == stack
3743 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3744 return;
3745 }
3746
3747 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3748 final ActivityRecord r = task.mActivities.get(i);
3749 if (r.app != null && r.app.thread != null) {
3750 mPipModeChangedActivities.add(r);
3751 }
3752 }
3753
3754 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3755 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3756 }
3757 }
3758
Tony Mak853304c2016-04-18 15:17:41 +01003759 void setDockedStackMinimized(boolean minimized) {
3760 mIsDockMinimized = minimized;
3761 if (minimized) {
3762 // Docked stack is not visible, no need to confirm credentials for its top activity.
3763 return;
3764 }
3765 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3766 if (dockedStack == null) {
3767 return;
3768 }
3769 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3770 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3771 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3772 }
3773 }
3774
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003775 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003776
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003777 public ActivityStackSupervisorHandler(Looper looper) {
3778 super(looper);
3779 }
3780
Craig Mautnerf3333272013-04-22 10:55:53 -07003781 void activityIdleInternal(ActivityRecord r) {
3782 synchronized (mService) {
3783 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3784 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003785 }
3786
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003787 @Override
3788 public void handleMessage(Message msg) {
3789 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003790 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3791 synchronized (mService) {
3792 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3793 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003794 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003795 }
3796 }
3797 } break;
3798 case REPORT_PIP_MODE_CHANGED_MSG: {
3799 synchronized (mService) {
3800 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3801 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003802 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003803 }
3804 }
3805 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003806 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003807 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3808 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003809 if (mService.mDidDexOpt) {
3810 mService.mDidDexOpt = false;
3811 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3812 nmsg.obj = msg.obj;
3813 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3814 return;
3815 }
3816 // We don't at this point know if the activity is fullscreen,
3817 // so we need to be conservative and assume it isn't.
3818 activityIdleInternal((ActivityRecord)msg.obj);
3819 } break;
3820 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003821 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003822 activityIdleInternal((ActivityRecord)msg.obj);
3823 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003824 case RESUME_TOP_ACTIVITY_MSG: {
3825 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003826 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003827 }
3828 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003829 case SLEEP_TIMEOUT_MSG: {
3830 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003831 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003832 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3833 mSleepTimeout = true;
3834 checkReadyForSleepLocked();
3835 }
3836 }
3837 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003838 case LAUNCH_TIMEOUT_MSG: {
3839 if (mService.mDidDexOpt) {
3840 mService.mDidDexOpt = false;
3841 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3842 return;
3843 }
3844 synchronized (mService) {
3845 if (mLaunchingActivity.isHeld()) {
3846 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3847 if (VALIDATE_WAKE_LOCK_CALLER
3848 && Binder.getCallingUid() != Process.myUid()) {
3849 throw new IllegalStateException("Calling must be system uid");
3850 }
3851 mLaunchingActivity.release();
3852 }
3853 }
3854 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003856 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 } break;
3858 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003859 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003860 } break;
3861 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003862 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003863 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003864 case CONTAINER_CALLBACK_VISIBILITY: {
3865 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003866 final IActivityContainerCallback callback = container.mCallback;
3867 if (callback != null) {
3868 try {
3869 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3870 } catch (RemoteException e) {
3871 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003872 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003873 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003874 case LOCK_TASK_START_MSG: {
3875 // When lock task starts, we disable the status bars.
3876 try {
Jason Monk62515be2014-05-21 16:06:19 -04003877 if (mLockTaskNotify == null) {
3878 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003879 }
Jason Monk62515be2014-05-21 16:06:19 -04003880 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003881 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003882 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003883 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003884 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003885 flags = StatusBarManager.DISABLE_MASK
3886 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003887 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003888 flags = StatusBarManager.DISABLE_MASK
3889 & (~StatusBarManager.DISABLE_BACK)
3890 & (~StatusBarManager.DISABLE_HOME)
3891 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003892 }
3893 getStatusBarService().disable(flags, mToken,
3894 mService.mContext.getPackageName());
3895 }
3896 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003897 if (getDevicePolicyManager() != null) {
3898 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003899 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003900 }
justinzhang5286d3f2014-05-12 17:06:01 -04003901 } catch (RemoteException ex) {
3902 throw new RuntimeException(ex);
3903 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003904 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003905 case LOCK_TASK_END_MSG: {
3906 // When lock task ends, we enable the status bars.
3907 try {
Jason Monk62515be2014-05-21 16:06:19 -04003908 if (getStatusBarService() != null) {
3909 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3910 mService.mContext.getPackageName());
3911 }
3912 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003913 if (getDevicePolicyManager() != null) {
3914 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3915 msg.arg1);
3916 }
Jason Monk62515be2014-05-21 16:06:19 -04003917 if (mLockTaskNotify == null) {
3918 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3919 }
3920 mLockTaskNotify.show(false);
3921 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003922 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003923 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003924 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003925 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003926 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003927 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003928 new LockPatternUtils(mService.mContext)
3929 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003930 }
3931 } catch (SettingNotFoundException e) {
3932 // No setting, don't lock.
3933 }
justinzhang5286d3f2014-05-12 17:06:01 -04003934 } catch (RemoteException ex) {
3935 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003936 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003937 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003938 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003939 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003940 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3941 if (mLockTaskNotify == null) {
3942 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3943 }
3944 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3945 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003946 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3947 final ActivityContainer container = (ActivityContainer) msg.obj;
3948 final IActivityContainerCallback callback = container.mCallback;
3949 if (callback != null) {
3950 try {
3951 callback.onAllActivitiesComplete(container.asBinder());
3952 } catch (RemoteException e) {
3953 }
3954 }
3955 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003956 case LAUNCH_TASK_BEHIND_COMPLETE: {
3957 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003958 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003959 if (r != null) {
3960 handleLaunchTaskBehindCompleteLocked(r);
3961 }
3962 }
3963 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003964
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003965 }
3966 }
3967 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003968
Ying Wangb081a592014-04-22 15:20:16 -07003969 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003970 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3971 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003972 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003973 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003975 ActivityRecord mParentActivity = null;
3976 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003977
Craig Mautnere3a00d72014-04-16 08:31:19 -07003978 boolean mVisible = true;
3979
Craig Mautner4a1cb222013-12-04 16:14:06 -08003980 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003981 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003983 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3984 final static int CONTAINER_STATE_NO_SURFACE = 1;
3985 final static int CONTAINER_STATE_FINISHING = 2;
3986 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3987
3988 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 synchronized (mService) {
3990 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003991 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003992 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003993 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003995 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003997 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003998 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003999 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004000 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004001 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004002 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 }
4004
4005 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004006 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004008 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4009 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010 return;
4011 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004012 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004013 }
4014 }
4015
4016 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004017 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004018 synchronized (mService) {
4019 if (mActivityDisplay != null) {
4020 return mActivityDisplay.mDisplayId;
4021 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004022 }
4023 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004024 }
4025
Jeff Brownca9bc702014-02-11 14:32:56 -08004026 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004027 public int getStackId() {
4028 synchronized (mService) {
4029 return mStackId;
4030 }
4031 }
4032
4033 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004034 public boolean injectEvent(InputEvent event) {
4035 final long origId = Binder.clearCallingIdentity();
4036 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004037 synchronized (mService) {
4038 if (mActivityDisplay != null) {
4039 return mInputManagerInternal.injectInputEvent(event,
4040 mActivityDisplay.mDisplayId,
4041 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4042 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004043 }
4044 return false;
4045 } finally {
4046 Binder.restoreCallingIdentity(origId);
4047 }
4048 }
4049
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004050 @Override
4051 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004052 synchronized (mService) {
4053 if (mContainerState == CONTAINER_STATE_FINISHING) {
4054 return;
4055 }
4056 mContainerState = CONTAINER_STATE_FINISHING;
4057
Craig Mautnerd163e752014-06-13 17:18:47 -07004058 long origId = Binder.clearCallingIdentity();
4059 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004060 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004061 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004062 } finally {
4063 Binder.restoreCallingIdentity(origId);
4064 }
4065 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004066 }
4067
Craig Mautner60257702014-09-17 15:02:33 -07004068 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004069 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004070 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004071 if (mActivityDisplay != null) {
4072 mActivityDisplay.detachActivitiesLocked(mStack);
4073 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004074 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004075 }
4076 }
4077
4078 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004079 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004080 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 }
4082
4083 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004084 public final int startActivityIntentSender(IIntentSender intentSender)
4085 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004086 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4087
4088 if (!(intentSender instanceof PendingIntentRecord)) {
4089 throw new IllegalArgumentException("Bad PendingIntent object");
4090 }
4091
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004092 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004093 Binder.getCallingUid(), mCurrentUser, false,
4094 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004095
4096 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4097 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4098 pendingIntent.key.requestResolvedType);
4099
4100 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4101 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4102 }
4103
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004104 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004105 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004106 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004107 throw new SecurityException(
4108 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4109 }
4110 }
4111
Craig Mautnerdf88d732014-01-27 09:21:32 -08004112 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004113 public IBinder asBinder() {
4114 return this;
4115 }
4116
Craig Mautner4504de52013-12-20 09:06:56 -08004117 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004118 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004119 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004120 }
4121
Craig Mautner4a1cb222013-12-04 16:14:06 -08004122 ActivityStackSupervisor getOuter() {
4123 return ActivityStackSupervisor.this;
4124 }
4125
Craig Mautnerd163e752014-06-13 17:18:47 -07004126 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004127 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004128 }
4129
Craig Mautner6985bad2014-04-21 15:22:06 -07004130 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004131 void setVisible(boolean visible) {
4132 if (mVisible != visible) {
4133 mVisible = visible;
4134 if (mCallback != null) {
4135 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4136 0 /* unused */, this).sendToTarget();
4137 }
4138 }
4139 }
4140
Craig Mautner6985bad2014-04-21 15:22:06 -07004141 void setDrawn() {
4142 }
4143
Craig Mautner1b4bf852014-05-26 15:06:32 -07004144 // You can always start a new task on a regular ActivityStack.
4145 boolean isEligibleForNewTasks() {
4146 return true;
4147 }
4148
Craig Mautnerd163e752014-06-13 17:18:47 -07004149 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004150 detachLocked();
4151 deleteActivityContainer(this);
4152 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004153 }
4154
Craig Mautner34b73df2014-01-12 21:11:08 -08004155 @Override
4156 public String toString() {
4157 return mIdString + (mActivityDisplay == null ? "N" : "A");
4158 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004159 }
4160
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004161 private class VirtualActivityContainer extends ActivityContainer {
4162 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004163 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004164
4165 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4166 super(getNextStackId());
4167 mParentActivity = parent;
4168 mCallback = callback;
4169 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004170 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004171 }
4172
4173 @Override
4174 public void setSurface(Surface surface, int width, int height, int density) {
4175 super.setSurface(surface, width, height, density);
4176
4177 synchronized (mService) {
4178 final long origId = Binder.clearCallingIdentity();
4179 try {
4180 setSurfaceLocked(surface, width, height, density);
4181 } finally {
4182 Binder.restoreCallingIdentity(origId);
4183 }
4184 }
4185 }
4186
4187 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4188 if (mContainerState == CONTAINER_STATE_FINISHING) {
4189 return;
4190 }
4191 VirtualActivityDisplay virtualActivityDisplay =
4192 (VirtualActivityDisplay) mActivityDisplay;
4193 if (virtualActivityDisplay == null) {
4194 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004195 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004196 mActivityDisplay = virtualActivityDisplay;
4197 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004198 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199 }
4200
4201 if (mSurface != null) {
4202 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004203 }
4204
Craig Mautner6985bad2014-04-21 15:22:06 -07004205 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004206 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004207 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004208 } else {
4209 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004210 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004211 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004212 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004213 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004214 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004215
Craig Mautnerd163e752014-06-13 17:18:47 -07004216 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004217
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004218 if (DEBUG_STACK) Slog.d(TAG_STACK,
4219 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004220 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004221
Craig Mautner6985bad2014-04-21 15:22:06 -07004222 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004223 boolean isAttachedLocked() {
4224 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004225 }
4226
4227 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004228 void setDrawn() {
4229 synchronized (mService) {
4230 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004231 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004232 }
4233 }
4234
Craig Mautner1b4bf852014-05-26 15:06:32 -07004235 // Never start a new task on an ActivityView if it isn't explicitly specified.
4236 @Override
4237 boolean isEligibleForNewTasks() {
4238 return false;
4239 }
4240
Craig Mautnerd163e752014-06-13 17:18:47 -07004241 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004242 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004243 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4244 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4245 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4246 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4247 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004248 }
4249 }
4250
Craig Mautner4a1cb222013-12-04 16:14:06 -08004251 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4252 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004253 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004254 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004255 int mDisplayId;
4256 Display mDisplay;
4257 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004258
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4260 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004261 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004262
Jose Lima4b6c6692014-08-12 17:41:12 -07004263 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004264
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004265 ActivityDisplay() {
4266 }
Craig Mautner4504de52013-12-20 09:06:56 -08004267
Craig Mautner1a70a162014-09-13 12:09:31 -07004268 // After instantiation, check that mDisplay is not null before using this. The alternative
4269 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004270 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004271 final Display display = mDisplayManager.getDisplay(displayId);
4272 if (display == null) {
4273 return;
4274 }
4275 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004276 }
4277
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004278 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004279 mDisplay = display;
4280 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004281 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004282 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004284 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004285 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004286 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4287 + " onTop=" + onTop);
4288 if (onTop) {
4289 mStacks.add(stack);
4290 } else {
4291 mStacks.add(0, stack);
4292 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004293 }
4294
Craig Mautnere0a38842013-12-16 16:14:02 -08004295 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004296 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004297 + " from displayId=" + mDisplayId);
4298 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004299 }
4300
Jose Lima4b6c6692014-08-12 17:41:12 -07004301 void setVisibleBehindActivity(ActivityRecord r) {
4302 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004303 }
4304
Jose Lima4b6c6692014-08-12 17:41:12 -07004305 boolean hasVisibleBehindActivity() {
4306 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004307 }
4308
Craig Mautner34b73df2014-01-12 21:11:08 -08004309 @Override
4310 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004311 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4312 }
4313 }
4314
4315 class VirtualActivityDisplay extends ActivityDisplay {
4316 VirtualDisplay mVirtualDisplay;
4317
Craig Mautner6985bad2014-04-21 15:22:06 -07004318 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004319 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004320 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4321 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4322 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4323 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004324
4325 init(mVirtualDisplay.getDisplay());
4326
4327 mWindowManager.handleDisplayAdded(mDisplayId);
4328 }
4329
4330 void setSurface(Surface surface) {
4331 if (mVirtualDisplay != null) {
4332 mVirtualDisplay.setSurface(surface);
4333 }
4334 }
4335
4336 @Override
4337 void detachActivitiesLocked(ActivityStack stack) {
4338 super.detachActivitiesLocked(stack);
4339 if (mVirtualDisplay != null) {
4340 mVirtualDisplay.release();
4341 mVirtualDisplay = null;
4342 }
4343 }
4344
4345 @Override
4346 public String toString() {
4347 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004348 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004349 }
Jose Lima58e66d62014-05-27 20:00:27 -07004350
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004351 /**
4352 * Adjust bounds to stay within stack bounds.
4353 *
4354 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4355 * that keep them unchanged, but be contained within the stack bounds.
4356 *
4357 * @param bounds Bounds to be adjusted.
4358 * @param stackBounds Bounds within which the other bounds should remain.
4359 */
4360 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4361 if (stackBounds == null || stackBounds.contains(bounds)) {
4362 return;
4363 }
4364
4365 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4366 final int maxRight = stackBounds.right
4367 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4368 int horizontalDiff = stackBounds.left - bounds.left;
4369 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4370 || (bounds.left + horizontalDiff >= maxRight)) {
4371 horizontalDiff = maxRight - bounds.left;
4372 }
4373 bounds.left += horizontalDiff;
4374 bounds.right += horizontalDiff;
4375 }
4376
4377 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4378 final int maxBottom = stackBounds.bottom
4379 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4380 int verticalDiff = stackBounds.top - bounds.top;
4381 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4382 || (bounds.top + verticalDiff >= maxBottom)) {
4383 verticalDiff = maxBottom - bounds.top;
4384 }
4385 bounds.top += verticalDiff;
4386 bounds.bottom += verticalDiff;
4387 }
4388 }
4389
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004390 ActivityStack findStackBehind(ActivityStack stack) {
4391 // TODO(multi-display): We are only looking for stacks on the default display.
4392 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4393 if (display == null) {
4394 return null;
4395 }
4396 final ArrayList<ActivityStack> stacks = display.mStacks;
4397 for (int i = stacks.size() - 1; i >= 0; i--) {
4398 if (stacks.get(i) == stack && i > 0) {
4399 return stacks.get(i - 1);
4400 }
4401 }
4402 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4403 + " in=" + stacks);
4404 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004405
Jorim Jaggic69bd222016-03-15 14:38:37 +01004406 /**
4407 * Puts a task into resizing mode during the next app transition.
4408 *
4409 * @param taskId the id of the task to put into resizing mode
4410 */
4411 private void setResizingDuringAnimation(int taskId) {
4412 mResizingTasksDuringAnimation.add(taskId);
4413 mWindowManager.setTaskDockedResizing(taskId, true);
4414 }
4415
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004416 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4417 final TaskRecord task;
4418 final int callingUid;
4419 final String callingPackage;
4420 final Intent intent;
4421 final int userId;
4422 final ActivityOptions activityOptions = (bOptions != null)
4423 ? new ActivityOptions(bOptions) : null;
4424 final int launchStackId = (activityOptions != null)
4425 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004426 if (launchStackId == HOME_STACK_ID) {
4427 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4428 + taskId + " can't be launch in the home stack.");
4429 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004430
4431 if (launchStackId == DOCKED_STACK_ID) {
4432 mWindowManager.setDockedStackCreateState(
4433 activityOptions.getDockCreateMode(), null /* initialBounds */);
4434
4435 // Defer updating the stack in which recents is until the app transition is done, to
4436 // not run into issues where we still need to draw the task in recents but the
4437 // docked stack is already created.
4438 deferUpdateBounds(HOME_STACK_ID);
4439 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4440 }
4441
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004442 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4443 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004444 continueUpdateBounds(HOME_STACK_ID);
4445 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004446 throw new IllegalArgumentException(
4447 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4448 }
4449
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004450 // Since we don't have an actual source record here, we assume that the currently focused
4451 // activity was the source.
4452 final ActivityStack focusedStack = getFocusedStack();
4453 final ActivityRecord sourceRecord =
4454 focusedStack != null ? focusedStack.topActivity() : null;
4455
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004456 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004457 if (task.stack.mStackId != launchStackId) {
4458 moveTaskToStackLocked(
4459 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4460 ANIMATE);
4461 }
4462 }
4463
4464 // If the user must confirm credentials (e.g. when first launching a work app and the
4465 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4466 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4467 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004468 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004469 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004470 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004471 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4472 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004473
4474 // If we are launching the task in the docked stack, put it into resizing mode so
4475 // the window renders full-screen with the background filling the void. Also only
4476 // call this at the end to make sure that tasks exists on the window manager side.
4477 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004478 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004479 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004480
4481 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4482 ActivityManager.START_TASK_TO_FRONT,
4483 sourceRecord != null ? sourceRecord.task.stack.mStackId : INVALID_STACK_ID,
4484 sourceRecord, task.stack);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004485 return ActivityManager.START_TASK_TO_FRONT;
4486 }
4487 callingUid = task.mCallingUid;
4488 callingPackage = task.mCallingPackage;
4489 intent = task.intent;
4490 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4491 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004492 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4493 null, null, 0, 0, bOptions, userId, null, task);
4494 if (launchStackId == DOCKED_STACK_ID) {
4495 setResizingDuringAnimation(task.taskId);
4496 }
4497 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004498 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004499
4500 /**
4501 * @return a list of activities which are the top ones in each visible stack. The first
4502 * entry will be the focused activity.
4503 */
4504 public List<IBinder> getTopVisibleActivities() {
4505 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4506 if (display == null) {
4507 return Collections.EMPTY_LIST;
4508 }
4509 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4510 final ArrayList<ActivityStack> stacks = display.mStacks;
4511 for (int i = stacks.size() - 1; i >= 0; i--) {
4512 ActivityStack stack = stacks.get(i);
4513 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4514 ActivityRecord top = stack.topActivity();
4515 if (top != null) {
4516 if (stack == mFocusedStack) {
4517 topActivityTokens.add(0, top.appToken);
4518 } else {
4519 topActivityTokens.add(top.appToken);
4520 }
4521 }
4522 }
4523 }
4524 return topActivityTokens;
4525 }
Craig Mautner27084302013-03-25 08:05:25 -07004526}