blob: 033471c77a1186b03464e6bc4b19ba06624a4f08 [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
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800784 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800785 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
786 // for a userId u, a taskId can only be in the range
787 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
788 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
789 int candidateTaskId = currentTaskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800790 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
791 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
792 INVALID_STACK_ID) != null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800793 candidateTaskId++;
794 if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
795 // Wrap around as there will be smaller task ids that are available now.
796 candidateTaskId -= MAX_TASK_IDS_PER_USER;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700797 }
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800798 if (candidateTaskId == currentTaskId) {
799 // Something wrong!
800 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
801 throw new IllegalStateException("Cannot get an available task id."
802 + " Reached limit of " + MAX_TASK_IDS_PER_USER
803 + " running tasks per user.");
804 }
805 }
806 mCurTaskIdForUser.put(userId, candidateTaskId);
807 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700808 }
809
Craig Mautnerde4ef022013-04-07 19:01:33 -0700810 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800811 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700812 if (stack == null) {
813 return null;
814 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700815 ActivityRecord resumedActivity = stack.mResumedActivity;
816 if (resumedActivity == null || resumedActivity.app == null) {
817 resumedActivity = stack.mPausingActivity;
818 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700819 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700820 }
821 }
822 return resumedActivity;
823 }
824
Dianne Hackbornff072722014-09-24 10:56:28 -0700825 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700826 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800827 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
829 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
831 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700832 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 continue;
834 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700835 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800836 if (hr != null) {
837 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
838 && processName.equals(hr.processName)) {
839 try {
George Mount2c92c972014-03-20 09:38:23 -0700840 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 didSomething = true;
842 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700843 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 Slog.w(TAG, "Exception in new application when starting activity "
845 + hr.intent.getComponent().flattenToShortString(), e);
846 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700847 }
Craig Mautner20e72272013-04-01 13:45:53 -0700848 }
Craig Mautner20e72272013-04-01 13:45:53 -0700849 }
850 }
851 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700852 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700853 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700854 }
Craig Mautner20e72272013-04-01 13:45:53 -0700855 return didSomething;
856 }
857
858 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800859 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
862 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700863 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 continue;
865 }
866 final ActivityRecord resumedActivity = stack.mResumedActivity;
867 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700868 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800869 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 return false;
871 }
Craig Mautner20e72272013-04-01 13:45:53 -0700872 }
873 }
874 return true;
875 }
876
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
881 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700882 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800883 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700884 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 return false;
886 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 }
888 }
889 }
890 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700891 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800892 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
893 mLastFocusedStack + " to=" + mFocusedStack);
894 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 return true;
896 }
897
898 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800899 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800900 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
901 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
903 final ActivityStack stack = stacks.get(stackNdx);
904 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800905 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700906 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800907 return false;
908 }
909 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700911 }
912 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800913 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 }
915
Craig Mautner2acc3892013-09-23 10:28:14 -0700916 /**
917 * Pause all activities in either all of the stacks or just the back stacks.
918 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700919 * @return true if any activity was paused as a result of this call.
920 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700922 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
924 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
926 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700927 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700928 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700930 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
931 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700933 }
934 }
935 return someActivityPaused;
936 }
937
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700939 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
941 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
943 final ActivityStack stack = stacks.get(stackNdx);
944 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700945 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700947 Slog.d(TAG_STATES,
948 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 pausing = false;
950 } else {
951 return false;
952 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700953 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 }
955 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700956 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700957 }
958
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700959 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
960 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500961 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
963 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
964 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
965 final ActivityStack stack = stacks.get(stackNdx);
966 if (stack.mResumedActivity != null &&
967 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700968 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800969 }
970 }
971 }
972 }
973
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800974 void cancelInitializingActivities() {
975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
976 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
978 stacks.get(stackNdx).cancelInitializingActivities();
979 }
980 }
981 }
982
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700983 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700984 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700985 }
986
987 void sendWaitingVisibleReportLocked(ActivityRecord r) {
988 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700989 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700990 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700991 if (w.who == null) {
992 changed = true;
993 w.timeout = false;
994 if (r != null) {
995 w.who = new ComponentName(r.info.packageName, r.info.name);
996 }
997 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
998 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700999 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001000 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001001 if (changed) {
1002 mService.notifyAll();
1003 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001004 }
1005
Chong Zhang5022da32016-06-21 16:31:37 -07001006 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1007 boolean changed = false;
1008 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1009 WaitResult w = mWaitingActivityLaunched.remove(i);
1010 if (w.who == null) {
1011 changed = true;
1012 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1013 // the starting activity ends up moving another activity to front, and it should
1014 // wait for this new activity to become visible instead.
1015 // Do not modify other fields.
1016 w.result = START_TASK_TO_FRONT;
1017 }
1018 }
1019 if (changed) {
1020 mService.notifyAll();
1021 }
1022 }
1023
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001024 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1025 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001026 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001027 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001028 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001029 if (w.who == null) {
1030 changed = true;
1031 w.timeout = timeout;
1032 if (r != null) {
1033 w.who = new ComponentName(r.info.packageName, r.info.name);
1034 }
1035 w.thisTime = thisTime;
1036 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001037 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001038 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001040 if (changed) {
1041 mService.notifyAll();
1042 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001043 }
1044
Craig Mautner29219d92013-04-16 20:19:12 -07001045 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001046 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001047 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001048 if (r != null) {
1049 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001050 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001051
Craig Mautner4a1cb222013-12-04 16:14:06 -08001052 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001053 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001054 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1055 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001056 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001057 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001058 if (r != null) {
1059 return r;
1060 }
1061 }
1062 }
1063 return null;
1064 }
1065
Dianne Hackborn09233282014-04-30 11:33:59 -07001066 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001067 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001068 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1069 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001070 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001071 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001072 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1074 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001075 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001076 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001077 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001078 }
1079 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001080
1081 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1082 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1083 while (maxNum > 0) {
1084 long mostRecentActiveTime = Long.MIN_VALUE;
1085 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001086 final int numTaskLists = runningTaskLists.size();
1087 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1088 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001089 if (!stackTaskList.isEmpty()) {
1090 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1091 if (lastActiveTime > mostRecentActiveTime) {
1092 mostRecentActiveTime = lastActiveTime;
1093 selectedStackList = stackTaskList;
1094 }
1095 }
1096 }
1097 if (selectedStackList != null) {
1098 list.add(selectedStackList.remove(0));
1099 --maxNum;
1100 } else {
1101 break;
1102 }
1103 }
Craig Mautner20e72272013-04-01 13:45:53 -07001104 }
1105
Todd Kennedy7440f172015-12-09 14:31:22 -08001106 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1107 ProfilerInfo profilerInfo) {
1108 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001109 if (aInfo != null) {
1110 // Store the found target back into the intent, because now that
1111 // we have it we never want to do this again. For example, if the
1112 // user navigates back to this point in the history, we should
1113 // always restart the exact same activity.
1114 intent.setComponent(new ComponentName(
1115 aInfo.applicationInfo.packageName, aInfo.name));
1116
1117 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001118 if (!aInfo.processName.equals("system")) {
1119 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001120 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001121 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001123 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1124 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1125 }
1126
Man Caocfa78b22015-06-11 20:14:34 -07001127 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1128 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1129 }
1130
1131 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001132 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001133 }
1134 }
1135 }
1136 return aInfo;
1137 }
1138
Todd Kennedy7440f172015-12-09 14:31:22 -08001139 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001140 return resolveIntent(intent, resolvedType, userId, 0);
1141 }
1142
1143 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001144 try {
1145 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001146 PackageManager.MATCH_DEFAULT_ONLY | flags
1147 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001148 } catch (RemoteException e) {
1149 }
1150 return null;
1151 }
1152
1153 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1154 ProfilerInfo profilerInfo, int userId) {
1155 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1156 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1157 }
1158
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001159 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1160 boolean andResume, boolean checkConfig) throws RemoteException {
1161
1162 if (!allPausedActivitiesComplete()) {
1163 // While there are activities pausing we skipping starting any new activities until
1164 // pauses are complete. NOTE: that we also do this for activities that are starting in
1165 // the paused state because they will first be resumed then paused on the client side.
1166 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1167 "realStartActivityLocked: Skipping start of r=" + r
1168 + " some activities pausing...");
1169 return false;
1170 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001171
Craig Mautner2568c3a2015-03-26 14:22:34 -07001172 if (andResume) {
1173 r.startFreezingScreenLocked(app, 0);
1174 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001175
Craig Mautner2568c3a2015-03-26 14:22:34 -07001176 // schedule launch ticks to collect information about slow apps.
1177 r.startLaunchTickingLocked();
1178 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001179
1180 // Have the window manager re-evaluate the orientation of
1181 // the screen based on the new activity order. Note that
1182 // as a result of this, it can call back into the activity
1183 // manager with a new orientation. We don't care about that,
1184 // because the activity is not currently running so we are
1185 // just restarting it anyway.
1186 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001188 mService.mConfiguration,
1189 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001190 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 }
1192
1193 r.app = app;
1194 app.waitingToKill = null;
1195 r.launchCount++;
1196 r.lastLaunchTime = SystemClock.uptimeMillis();
1197
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001198 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001199
1200 int idx = app.activities.indexOf(r);
1201 if (idx < 0) {
1202 app.activities.add(r);
1203 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001204 mService.updateLruProcessLocked(app, true, null);
1205 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
Craig Mautner15df08a2015-04-01 12:17:18 -07001207 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001208 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1209 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001210 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001211 }
1212
1213 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 try {
1215 if (app.thread == null) {
1216 throw new RemoteException();
1217 }
1218 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001219 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001220 if (andResume) {
1221 results = r.results;
1222 newIntents = r.newIntents;
1223 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001224 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1225 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1226 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 if (andResume) {
1228 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1229 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001230 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001232 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001233 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001234 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001236 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1237 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001238 r.sleeping = false;
1239 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001240 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001241 mService.showAskCompatModeDialogLocked(r);
1242 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001243 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001244 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1245 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1246 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001247 final String profileFile = mService.mProfileFile;
1248 if (profileFile != null) {
1249 ParcelFileDescriptor profileFd = mService.mProfileFd;
1250 if (profileFd != null) {
1251 try {
1252 profileFd = profileFd.dup();
1253 } catch (IOException e) {
1254 if (profileFd != null) {
1255 try {
1256 profileFd.close();
1257 } catch (IOException o) {
1258 }
1259 profileFd = null;
1260 }
1261 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001263
1264 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1265 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 }
1267 }
1268 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001269
Craig Mautner2568c3a2015-03-26 14:22:34 -07001270 if (andResume) {
1271 app.hasShownUi = true;
1272 app.pendingUiClean = true;
1273 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001274 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001276 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001277 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001278 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001279 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001281 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 // This may be a heavy-weight process! Note that the package
1283 // manager will ensure that only activity can run in the main
1284 // process of the .apk, which is the only thing that will be
1285 // considered heavy-weight.
1286 if (app.processName.equals(app.info.packageName)) {
1287 if (mService.mHeavyWeightProcess != null
1288 && mService.mHeavyWeightProcess != app) {
1289 Slog.w(TAG, "Starting new heavy weight process " + app
1290 + " when already running "
1291 + mService.mHeavyWeightProcess);
1292 }
1293 mService.mHeavyWeightProcess = app;
1294 Message msg = mService.mHandler.obtainMessage(
1295 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1296 msg.obj = r;
1297 mService.mHandler.sendMessage(msg);
1298 }
1299 }
1300
1301 } catch (RemoteException e) {
1302 if (r.launchFailed) {
1303 // This is the second time we failed -- finish activity
1304 // and give up.
1305 Slog.e(TAG, "Second failure launching "
1306 + r.intent.getComponent().flattenToShortString()
1307 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001308 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1310 "2nd-crash", false);
1311 return false;
1312 }
1313
1314 // This is the first time we failed -- restart process and
1315 // retry.
1316 app.activities.remove(r);
1317 throw e;
1318 }
1319
1320 r.launchFailed = false;
1321 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001322 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 }
1324
1325 if (andResume) {
1326 // As part of the process of launching, ActivityThread also performs
1327 // a resume.
1328 stack.minimalResumeActivityLocked(r);
1329 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001330 // This activity is not starting in the resumed state... which should look like we asked
1331 // it to pause+stop (but remain visible), and it has done so and reported back the
1332 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001333 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001334 "Moving to PAUSED: " + r + " (starting in paused state)");
1335 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001336 }
1337
1338 // Launch the new version setup screen if needed. We do this -after-
1339 // launching the initial activity (that is, home), so that it can have
1340 // a chance to initialize itself while in the background, making the
1341 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001342 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 mService.startSetupActivityLocked();
1344 }
1345
Dianne Hackborn465fa392014-09-14 14:21:18 -07001346 // Update any services we are bound to that might care about whether
1347 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001348 if (r.app != null) {
1349 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1350 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001351
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 return true;
1353 }
1354
Craig Mautnere79d42682013-04-01 19:01:53 -07001355 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001356 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001357 // Is this activity's application already running?
1358 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001359 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001360
1361 r.task.stack.setLaunchTime(r);
1362
1363 if (app != null && app.thread != null) {
1364 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001365 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1366 || !"android".equals(r.info.packageName)) {
1367 // Don't add this if it is a platform component that is marked
1368 // to run in multiple processes, because this is actually
1369 // part of the framework so doesn't make sense to track as a
1370 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001371 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1372 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001373 }
George Mount2c92c972014-03-20 09:38:23 -07001374 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001375 return;
1376 } catch (RemoteException e) {
1377 Slog.w(TAG, "Exception when starting activity "
1378 + r.intent.getComponent().flattenToShortString(), e);
1379 }
1380
1381 // If a dead object exception was thrown -- fall through to
1382 // restart the application.
1383 }
1384
1385 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001386 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001387 }
1388
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001389 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001390 String resultWho, int requestCode, int callingPid, int callingUid,
1391 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001392 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001393 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1394 callingUid);
1395 if (startAnyPerm == PERMISSION_GRANTED) {
1396 return true;
1397 }
1398 final int componentRestriction = getComponentRestrictionForCallingPackage(
1399 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1400 final int actionRestriction = getActionRestrictionForCallingPackage(
1401 intent.getAction(), callingPackage, callingPid, callingUid);
1402 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1403 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1404 if (resultRecord != null) {
1405 resultStack.sendActivityResultLocked(-1,
1406 resultRecord, resultWho, requestCode,
1407 Activity.RESULT_CANCELED, null);
1408 }
1409 final String msg;
1410 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1411 msg = "Permission Denial: starting " + intent.toString()
1412 + " from " + callerApp + " (pid=" + callingPid
1413 + ", uid=" + callingUid + ")" + " with revoked permission "
1414 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1415 } else if (!aInfo.exported) {
1416 msg = "Permission Denial: starting " + intent.toString()
1417 + " from " + callerApp + " (pid=" + callingPid
1418 + ", uid=" + callingUid + ")"
1419 + " not exported from uid " + aInfo.applicationInfo.uid;
1420 } else {
1421 msg = "Permission Denial: starting " + intent.toString()
1422 + " from " + callerApp + " (pid=" + callingPid
1423 + ", uid=" + callingUid + ")"
1424 + " requires " + aInfo.permission;
1425 }
1426 Slog.w(TAG, msg);
1427 throw new SecurityException(msg);
1428 }
1429
1430 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1431 final String message = "Appop Denial: starting " + intent.toString()
1432 + " from " + callerApp + " (pid=" + callingPid
1433 + ", uid=" + callingUid + ")"
1434 + " requires " + AppOpsManager.permissionToOp(
1435 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1436 Slog.w(TAG, message);
1437 return false;
1438 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1439 final String message = "Appop Denial: starting " + intent.toString()
1440 + " from " + callerApp + " (pid=" + callingPid
1441 + ", uid=" + callingUid + ")"
1442 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1443 Slog.w(TAG, message);
1444 return false;
1445 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001446 if (options != null && options.getLaunchTaskId() != -1) {
1447 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1448 callingPid, callingUid);
1449 if (startInTaskPerm != PERMISSION_GRANTED) {
1450 final String msg = "Permission Denial: starting " + intent.toString()
1451 + " from " + callerApp + " (pid=" + callingPid
1452 + ", uid=" + callingUid + ") with launchTaskId="
1453 + options.getLaunchTaskId();
1454 Slog.w(TAG, msg);
1455 throw new SecurityException(msg);
1456 }
1457 }
1458
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001459 return true;
1460 }
1461
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001462 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001463 final long identity = Binder.clearCallingIdentity();
1464 try {
1465 return UserManager.get(mService.mContext).getUserInfo(userId);
1466 } finally {
1467 Binder.restoreCallingIdentity(identity);
1468 }
1469 }
1470
Svet Ganov99b60432015-06-27 13:15:22 -07001471 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001472 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001473 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1474 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001475 == PackageManager.PERMISSION_DENIED) {
1476 return ACTIVITY_RESTRICTION_PERMISSION;
1477 }
1478
Christopher Tateff7add02015-08-17 10:23:22 -07001479 if (activityInfo.permission == null) {
1480 return ACTIVITY_RESTRICTION_NONE;
1481 }
1482
Svet Ganov99b60432015-06-27 13:15:22 -07001483 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1484 if (opCode == AppOpsManager.OP_NONE) {
1485 return ACTIVITY_RESTRICTION_NONE;
1486 }
1487
1488 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1489 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001490 if (!ignoreTargetSecurity) {
1491 return ACTIVITY_RESTRICTION_APPOP;
1492 }
Svet Ganov99b60432015-06-27 13:15:22 -07001493 }
1494
1495 return ACTIVITY_RESTRICTION_NONE;
1496 }
1497
Svetoslav7008b512015-06-24 18:47:07 -07001498 private int getActionRestrictionForCallingPackage(String action,
1499 String callingPackage, int callingPid, int callingUid) {
1500 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001501 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001502 }
1503
1504 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1505 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001506 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001507 }
1508
1509 final PackageInfo packageInfo;
1510 try {
1511 packageInfo = mService.mContext.getPackageManager()
1512 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1513 } catch (PackageManager.NameNotFoundException e) {
1514 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001515 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001516 }
1517
1518 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001519 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001520 }
1521
1522 if (mService.checkPermission(permission, callingPid, callingUid) ==
1523 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001524 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001525 }
1526
1527 final int opCode = AppOpsManager.permissionToOpCode(permission);
1528 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001529 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001530 }
1531
1532 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1533 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001534 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001535 }
1536
Svet Ganov99b60432015-06-27 13:15:22 -07001537 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001538 }
1539
Wale Ogunwaled046a012015-12-24 13:05:59 -08001540 boolean moveActivityStackToFront(ActivityRecord r, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001541 if (r == null) {
1542 // Not sure what you are trying to do, but it is not going to work...
1543 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001544 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001545 final TaskRecord task = r.task;
1546 if (task == null || task.stack == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001547 Slog.w(TAG, "Can't move stack to front for r=" + r + " task=" + task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001548 return false;
1549 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001550 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001551 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001552 }
1553
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001554 void setLaunchSource(int uid) {
1555 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1556 }
1557
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001558 void acquireLaunchWakelock() {
1559 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1560 throw new IllegalStateException("Calling must be system uid");
1561 }
1562 mLaunchingActivity.acquire();
1563 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1564 // To be safe, don't allow the wake lock to be held for too long.
1565 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1566 }
1567 }
1568
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001569 /**
1570 * Called when the frontmost task is idle.
1571 * @return the state of mService.mBooting before this was called.
1572 */
1573 private boolean checkFinishBootingLocked() {
1574 final boolean booting = mService.mBooting;
1575 boolean enableScreen = false;
1576 mService.mBooting = false;
1577 if (!mService.mBooted) {
1578 mService.mBooted = true;
1579 enableScreen = true;
1580 }
1581 if (booting || enableScreen) {
1582 mService.postFinishBooting(booting, enableScreen);
1583 }
1584 return booting;
1585 }
1586
Craig Mautnerf3333272013-04-22 10:55:53 -07001587 // Checked.
1588 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1589 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001590 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001591
Craig Mautnerf3333272013-04-22 10:55:53 -07001592 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001593 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001594 int NS = 0;
1595 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001596 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001597 boolean activityRemoved = false;
1598
Wale Ogunwale7d701172015-03-11 15:36:30 -07001599 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001600 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001601 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1602 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001603 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1604 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001605 if (fromTimeout) {
1606 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001607 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001608
1609 // This is a hack to semi-deal with a race condition
1610 // in the client where it can be constructed with a
1611 // newer configuration from when we asked it to launch.
1612 // We'll update with whatever configuration it now says
1613 // it used to launch.
1614 if (config != null) {
1615 r.configuration = config;
1616 }
1617
1618 // We are now idle. If someone is waiting for a thumbnail from
1619 // us, we can now deliver.
1620 r.idle = true;
1621
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001622 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001623 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001624 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001625 }
1626 }
1627
1628 if (allResumedActivitiesIdle()) {
1629 if (r != null) {
1630 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001631 }
1632
1633 if (mLaunchingActivity.isHeld()) {
1634 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1635 if (VALIDATE_WAKE_LOCK_CALLER &&
1636 Binder.getCallingUid() != Process.myUid()) {
1637 throw new IllegalStateException("Calling must be system uid");
1638 }
1639 mLaunchingActivity.release();
1640 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001641 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001642 }
1643
1644 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001645 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001646 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001647 if ((NF = mFinishingActivities.size()) > 0) {
1648 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001649 mFinishingActivities.clear();
1650 }
1651
Craig Mautnerf3333272013-04-22 10:55:53 -07001652 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001653 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001654 mStartingUsers.clear();
1655 }
1656
Craig Mautnerf3333272013-04-22 10:55:53 -07001657 // Stop any activities that are scheduled to do so but have been
1658 // waiting for the next one to start.
1659 for (int i = 0; i < NS; i++) {
1660 r = stops.get(i);
1661 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001662 if (stack != null) {
1663 if (r.finishing) {
1664 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1665 } else {
1666 stack.stopActivityLocked(r);
1667 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001668 }
1669 }
1670
1671 // Finish any activities that are scheduled to do so but have been
1672 // waiting for the next one to start.
1673 for (int i = 0; i < NF; i++) {
1674 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001675 final ActivityStack stack = r.task.stack;
1676 if (stack != null) {
1677 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1678 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001679 }
1680
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001681 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001682 // Complete user switch
1683 if (startingUsers != null) {
1684 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001685 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001686 }
1687 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001688 }
1689
1690 mService.trimApplications();
1691 //dump();
1692 //mWindowManager.dump();
1693
Craig Mautnerf3333272013-04-22 10:55:53 -07001694 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001695 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001696 }
1697
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001698 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001699 }
1700
Craig Mautner8e569572013-10-11 17:36:59 -07001701 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001702 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1706 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1707 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001708 }
Craig Mautner19091252013-10-05 00:03:53 -07001709 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001710 }
1711
1712 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1714 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1716 stacks.get(stackNdx).closeSystemDialogsLocked();
1717 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001718 }
1719 }
1720
Craig Mautner93529a42013-10-04 15:03:13 -07001721 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001722 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001723 }
1724
Craig Mautner8d341ef2013-03-26 09:03:27 -07001725 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001726 * Update the last used stack id for non-current user (current user's last
1727 * used stack is the focused stack)
1728 */
1729 void updateUserStackLocked(int userId, ActivityStack stack) {
1730 if (userId != mCurrentUser) {
1731 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1732 }
1733 }
1734
1735 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001736 * @return true if some activity was finished (or would have finished if doit were true).
1737 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001738 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1739 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001740 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1742 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001744 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001745 if (stack.finishDisabledPackageActivitiesLocked(
1746 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001747 didSomething = true;
1748 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001749 }
1750 }
1751 return didSomething;
1752 }
1753
Dianne Hackborna413dc02013-07-12 12:02:55 -07001754 void updatePreviousProcessLocked(ActivityRecord r) {
1755 // Now that this process has stopped, we may want to consider
1756 // it to be the previous app to try to keep around in case
1757 // the user wants to return to it.
1758
1759 // First, found out what is currently the foreground app, so that
1760 // we don't blow away the previous app if this activity is being
1761 // hosted by the process that is actually still the foreground.
1762 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1766 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001767 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001768 if (stack.mResumedActivity != null) {
1769 fgApp = stack.mResumedActivity.app;
1770 } else if (stack.mPausingActivity != null) {
1771 fgApp = stack.mPausingActivity.app;
1772 }
1773 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001774 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001775 }
1776 }
1777
1778 // Now set this one as the previous process, only if that really
1779 // makes sense to.
1780 if (r.app != null && fgApp != null && r.app != fgApp
1781 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001782 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001783 mService.mPreviousProcess = r.app;
1784 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1785 }
1786 }
1787
Wale Ogunwaled046a012015-12-24 13:05:59 -08001788 boolean resumeFocusedStackTopActivityLocked() {
1789 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001790 }
1791
Wale Ogunwaled046a012015-12-24 13:05:59 -08001792 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001793 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001794 if (targetStack != null && isFocusedStack(targetStack)) {
1795 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001796 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001797 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1798 if (r == null || r.state != RESUMED) {
1799 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1800 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001801 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001802 }
1803
Todd Kennedy39bfee52016-02-24 10:28:21 -08001804 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1805 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1806 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1807 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1808 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1809 }
1810 }
1811 }
1812
Adrian Roos20d7df32016-01-12 18:59:43 +01001813 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1814 TaskRecord finishedTask = null;
1815 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1817 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001818 final int numStacks = stacks.size();
1819 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1820 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001821 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1822 if (stack == focusedStack || finishedTask == null) {
1823 finishedTask = t;
1824 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001825 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001826 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001827 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001828 }
1829
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001830 void finishVoiceTask(IVoiceInteractionSession session) {
1831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1832 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1833 final int numStacks = stacks.size();
1834 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1835 final ActivityStack stack = stacks.get(stackNdx);
1836 stack.finishVoiceTask(session);
1837 }
1838 }
1839 }
1840
Andrii Kulianc27916642016-04-12 17:59:27 -07001841 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1842 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001843 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1844 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001845 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001846 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1847 // Caller wants the home activity moved with it. To accomplish this,
1848 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001849 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001850 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001851 if (task.stack == null) {
1852 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1853 + task + " to front. Stack is null");
1854 return;
1855 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001856
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001857 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001858 int stackId = options.getLaunchStackId();
1859 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001860 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001861 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001862 if (stackId == INVALID_STACK_ID) {
1863 stackId = task.getLaunchStackId();
1864 }
Chong Zhang112eb8c2015-11-02 11:17:00 -08001865 if (stackId != task.stack.mStackId) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08001866 final ActivityStack stack = moveTaskToStackUncheckedLocked(
1867 task, stackId, ON_TOP, !FORCE_FOCUS, reason);
1868 stackId = stack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001869 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1870 // still need moveTaskToFrontLocked() below for any transition settings.
1871 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001872 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1873 resizeStackLocked(stackId, bounds,
1874 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001875 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001876 } else {
1877 // WM resizeTask must be done after the task is moved to the correct stack,
1878 // because Task's setBounds() also updates dim layer's bounds, but that has
1879 // dependency on the stack.
Andrii Kulian73336d812016-03-24 12:56:08 -07001880 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig,
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001881 false /* relayout */, false /* forced */);
1882 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001883 }
1884 }
1885
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001886 final ActivityRecord r = task.getTopActivity();
1887 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
1888 r == null ? null : r.appTimeTracker, reason);
1889
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001890 if (DEBUG_STACK) Slog.d(TAG_STACK,
1891 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001892
Andrii Kulianc27916642016-04-12 17:59:27 -07001893 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
1894 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001895 }
1896
Wale Ogunwale854809c2015-12-27 16:18:19 -08001897 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001898 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001899 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001900 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001901 return false;
1902 }
1903 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1904 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001905 }
1906
Craig Mautner967212c2013-04-13 21:10:58 -07001907 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001908 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001909 }
1910
1911 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001912 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1913 if (activityContainer != null) {
1914 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001915 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001916 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001917 return null;
1918 }
1919 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001920 }
1921
Craig Mautner967212c2013-04-13 21:10:58 -07001922 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001923 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1925 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001926 }
1927 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001928 }
1929
Craig Mautner4a1cb222013-12-04 16:14:06 -08001930 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001931 ActivityRecord homeActivity = getHomeActivity();
1932 if (homeActivity != null) {
1933 return homeActivity.appToken;
1934 }
1935 return null;
1936 }
1937
1938 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001939 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001940 }
1941
1942 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1944 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1945 final TaskRecord task = tasks.get(taskNdx);
1946 if (task.isHomeTask()) {
1947 final ArrayList<ActivityRecord> activities = task.mActivities;
1948 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1949 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001950 if (r.isHomeActivity()
1951 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001952 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001953 }
1954 }
1955 }
1956 }
1957 return null;
1958 }
1959
Chong Zhangb15758a2015-11-17 12:12:03 -08001960 /**
1961 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1962 * the docked stack itself, or if it's side-by-side to the docked stack.
1963 */
1964 boolean isStackDockedInEffect(int stackId) {
1965 return stackId == DOCKED_STACK_ID ||
1966 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1967 }
1968
Todd Kennedyca4d8422015-01-15 15:19:22 -08001969 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001970 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001971 ActivityContainer activityContainer =
1972 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001973 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001974 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1975 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001976 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001977 return activityContainer;
1978 }
1979
Craig Mautner34b73df2014-01-12 21:11:08 -08001980 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001981 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1982 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1983 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001984 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1985 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001986 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001987 }
1988 }
1989
Craig Mautner95da1082014-02-24 17:54:35 -08001990 void deleteActivityContainer(IActivityContainer container) {
1991 ActivityContainer activityContainer = (ActivityContainer)container;
1992 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001993 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1994 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001995 final int stackId = activityContainer.mStackId;
1996 mActivityContainers.remove(stackId);
1997 mWindowManager.removeStack(stackId);
1998 }
1999 }
2000
Jorim Jaggidc249c42015-12-15 14:57:31 -08002001 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002002 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002003 if (stackId == DOCKED_STACK_ID) {
2004 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
2005 preserveWindows);
2006 return;
2007 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002008 final ActivityStack stack = getStack(stackId);
2009 if (stack == null) {
2010 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2011 return;
2012 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002013
Jorim Jaggidc249c42015-12-15 14:57:31 -08002014 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002015 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2016 // stack size changing so things don't get out of sync.
2017 return;
2018 }
2019
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002020 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002021 mWindowManager.deferSurfaceLayout();
2022 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002023 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002024 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002025 stack.ensureVisibleActivitiesConfigurationLocked(
2026 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002027 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002028 } finally {
2029 mWindowManager.continueSurfaceLayout();
2030 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002031 }
2032 }
2033
Jorim Jaggi192086e2016-03-11 17:17:03 +01002034 void deferUpdateBounds(int stackId) {
2035 final ActivityStack stack = getStack(stackId);
2036 if (stack != null) {
2037 stack.deferUpdateBounds();
2038 }
2039 }
2040
2041 void continueUpdateBounds(int stackId) {
2042 final ActivityStack stack = getStack(stackId);
2043 if (stack != null) {
2044 stack.continueUpdateBounds();
2045 }
2046 }
2047
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002048 void notifyAppTransitionDone() {
2049 continueUpdateBounds(HOME_STACK_ID);
2050 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2051 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002052 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002053 mWindowManager.setTaskDockedResizing(taskId, false);
2054 }
2055 }
2056 mResizingTasksDuringAnimation.clear();
2057 }
2058
Jorim Jaggi192086e2016-03-11 17:17:03 +01002059 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002060 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002061 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002062
Jorim Jaggi192086e2016-03-11 17:17:03 +01002063 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2064 return;
2065 }
2066
Jorim Jaggidc249c42015-12-15 14:57:31 -08002067 mTmpBounds.clear();
2068 mTmpConfigs.clear();
2069 mTmpInsetBounds.clear();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002070 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Jorim Jaggif15a7352016-04-04 23:54:30 -07002071 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
2072 final Rect insetBounds = tempTaskInsetBounds != null ? tempTaskInsetBounds : taskBounds;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002073 for (int i = tasks.size() - 1; i >= 0; i--) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002074 final TaskRecord task = tasks.get(i);
2075 if (task.isResizeable()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002076 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2077 // For freeform stack we don't adjust the size of the tasks to match that
2078 // of the stack, but we do try to make sure the tasks are still contained
2079 // with the bounds of the stack.
2080 tempRect2.set(task.mBounds);
2081 fitWithinBounds(tempRect2, bounds);
2082 task.updateOverrideConfiguration(tempRect2);
2083 } else {
Jorim Jaggif15a7352016-04-04 23:54:30 -07002084 task.updateOverrideConfiguration(taskBounds, insetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002085 }
2086 }
2087
2088 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2089 mTmpBounds.put(task.taskId, task.mBounds);
2090 if (tempTaskInsetBounds != null) {
2091 mTmpInsetBounds.put(task.taskId, tempTaskInsetBounds);
2092 }
2093 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002094
2095 // We might trigger a configuration change. Save the current task bounds for freezing.
2096 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002097 stack.mFullscreen = mWindowManager.resizeStack(stack.mStackId, bounds, mTmpConfigs,
2098 mTmpBounds, mTmpInsetBounds);
2099 stack.setBounds(bounds);
2100 }
2101
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002102 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2103 final ActivityStack stack = getStack(fromStackId);
2104 if (stack == null) {
2105 return;
2106 }
2107
2108 mWindowManager.deferSurfaceLayout();
2109 try {
2110 if (fromStackId == DOCKED_STACK_ID) {
2111
2112 // We are moving all tasks from the docked stack to the fullscreen stack,
2113 // which is dismissing the docked stack, so resize all other stacks to
2114 // fullscreen here already so we don't end up with resize trashing.
2115 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2116 if (StackId.isResizeableByDockedStack(i)) {
2117 ActivityStack otherStack = getStack(i);
2118 if (otherStack != null) {
2119 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2120 true /* allowResizeInDockedMode */, DEFER_RESUME);
2121 }
2122 }
2123 }
2124
2125 // Also disable docked stack resizing since we have manually adjusted the
2126 // size of other stacks above and we don't want to trigger a docked stack
2127 // resize when we remove task from it below and it is detached from the
2128 // display because it no longer contains any tasks.
2129 mAllowDockedStackResize = false;
2130 }
2131 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2132 final int size = tasks.size();
2133 if (onTop) {
2134 for (int i = 0; i < size; i++) {
2135 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002136 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002137 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002138 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002139
2140 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2141 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002142 } else {
2143 for (int i = size - 1; i >= 0; i--) {
2144 positionTaskInStackLocked(tasks.get(i).taskId,
2145 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2146 }
2147 }
2148 } finally {
2149 mAllowDockedStackResize = true;
2150 mWindowManager.continueSurfaceLayout();
2151 }
2152 }
2153
Jorim Jaggidc249c42015-12-15 14:57:31 -08002154 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2155 Rect tempDockedTaskInsetBounds,
2156 Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002157
2158 if (!mAllowDockedStackResize) {
2159 // Docked stack resize currently disabled.
2160 return;
2161 }
2162
Jorim Jaggidc249c42015-12-15 14:57:31 -08002163 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2164 if (stack == null) {
2165 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2166 return;
2167 }
2168
2169 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2170 mWindowManager.deferSurfaceLayout();
2171 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002172 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2173 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002174 ActivityRecord r = stack.topRunningActivityLocked();
2175 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2176 tempDockedTaskInsetBounds);
2177
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002178 // TODO: Checking for isAttached might not be needed as if the user passes in null
2179 // dockedBounds then they want the docked stack to be dismissed.
2180 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2181 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002182 // In this case we make all other static stacks fullscreen and move all
2183 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002184 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002185
2186 // stack shouldn't contain anymore activities, so nothing to resume.
2187 r = null;
2188 } else {
2189 // Docked stacks occupy a dedicated region on screen so the size of all other
2190 // static stacks need to be adjusted so they don't overlap with the docked stack.
2191 // We get the bounds to use from window manager which has been adjusted for any
2192 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002193 mWindowManager.getStackDockedModeBounds(
2194 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002195 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002196 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2197 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2198 tempOtherTaskInsetBounds, preserveWindows,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002199 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002200 }
2201 }
2202 }
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002203 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002204 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002205 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002206 mWindowManager.continueSurfaceLayout();
2207 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2208 }
2209
2210 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2211 tempDockedTaskBounds != null
2212 || tempDockedTaskInsetBounds != null
2213 || tempOtherTaskBounds != null
2214 || tempOtherTaskInsetBounds != null);
2215 }
2216
Robert Carr0d00c2e2016-02-29 17:45:02 -08002217 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2218 final ActivityStack stack = getStack(PINNED_STACK_ID);
2219 if (stack == null) {
2220 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2221 return;
2222 }
2223 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2224 mWindowManager.deferSurfaceLayout();
2225 try {
2226 ActivityRecord r = stack.topRunningActivityLocked();
2227 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2228 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002229 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002230 } finally {
2231 mWindowManager.continueSurfaceLayout();
2232 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2233 }
2234 }
2235
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002236 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2237 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002238 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002239 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002240 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002241 }
2242
Chong Zhang87b21722015-09-21 15:39:51 -07002243 // If this is a forced resize, let it go through even if the bounds is not changing,
2244 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002245 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002246 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002248 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002249 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002250 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002251
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002252 if (!mWindowManager.isValidTaskId(task.taskId)) {
2253 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002254 // All we can do for now is update the bounds so it can be used when the task is
2255 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002256 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002257 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2258 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002259 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002260 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002261 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002262 }
2263
Chong Zhang6de2ae82015-09-30 18:25:21 -07002264 // Do not move the task to another stack here.
2265 // This method assumes that the task is already placed in the right stack.
2266 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002267
Chong Zhang6de2ae82015-09-30 18:25:21 -07002268 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002269
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07002270 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002271 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002272 // way and the activity was kept the way it was. If it's false, it means the activity had
2273 // to be relaunched due to configuration change.
2274 boolean kept = true;
2275 if (overrideConfig != null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002276 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002277 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002278 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002279 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002280
2281 if (!deferResume) {
2282
2283 // All other activities must be made visible with their correct configuration.
2284 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2285 if (!kept) {
2286 resumeFocusedStackTopActivityLocked();
2287 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002288 }
2289 }
2290 }
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002291 mWindowManager.resizeTask(task.taskId, task.mBounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002292
2293 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002294 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002295 }
2296
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002297 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002298 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2299 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002300 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002301 }
2302
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002303 ActivityContainer activityContainer = new ActivityContainer(stackId);
2304 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002305 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002306 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002307 }
2308
2309 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002310 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002311 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2312 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002313 break;
2314 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002315 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002316 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002317 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002318 }
2319
Chong Zhang5dcb2752015-08-18 13:50:26 -07002320 /**
2321 * Restores a recent task to a stack
2322 * @param task The recent task to be restored.
2323 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002324 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002325 * @return true if the task has been restored successfully.
2326 */
2327 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2328 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002329 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002330 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2331 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2332 // Put it in the fullscreen stack.
2333 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002334 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002335
Wale Ogunwale706ed792015-08-02 10:29:44 -07002336 if (task.stack != null) {
2337 // Task has already been restored once. See if we need to do anything more
2338 if (task.stack.mStackId == stackId) {
2339 // Nothing else to do since it is already restored in the right stack.
2340 return true;
2341 }
2342 // Remove current stack association, so we can re-associate the task with the
2343 // right stack below.
Wale Ogunwale06579d62016-04-30 15:29:06 -07002344 task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002345 }
2346
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002347 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002348 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002349
2350 if (stack == null) {
2351 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002352 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2353 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002354 return false;
2355 }
2356
Wale Ogunwale5f986092015-12-04 15:35:38 -08002357 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2359 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002360 final ArrayList<ActivityRecord> activities = task.mActivities;
2361 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002362 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002363 }
2364 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002365 }
2366
Wale Ogunwale040b4702015-08-06 18:10:50 -07002367 /**
2368 * Moves the specified task record to the input stack id.
2369 * WARNING: This method performs an unchecked/raw move of the task and
2370 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002371 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002372 * @param task Task to move.
2373 * @param stackId Id of stack to move task to.
2374 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002375 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002376 * @param reason Reason the task is been moved.
2377 * @return The stack the task was moved to.
2378 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002379 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002380 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002381
2382 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2383 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2384 + "support multi-window task=" + task + " to stackId=" + stackId);
2385 }
2386
Wale Ogunwale06579d62016-04-30 15:29:06 -07002387 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002388 final ActivityStack prevStack = task.stack;
2389 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002390 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002391 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2392 // Whenever we are moving the top activity from the front stack we want to make sure to move
2393 // the stack to the front.
2394 final boolean wasFront = isFrontStack(prevStack)
2395 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002396
Chong Zhangd545dce2016-02-29 18:09:17 -08002397 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002398 // We don't allow moving a unresizeable task to the docked stack since the docked
2399 // stack is used for split-screen mode and will cause things like the docked divider to
2400 // show up. We instead leave the task in its current stack or move it to the fullscreen
2401 // stack if it isn't currently in a stack.
2402 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002403 Slog.w(TAG, "Can not move unresizeable task=" + task
2404 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2405 }
2406
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002407 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2408 // if a docked stack is created below which will lead to the stack we are moving from and
2409 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002410 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002411 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002412 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002413 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002414 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002415
2416 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002417 // move focus to the new stack by moving the stack to the front.
2418 stack.moveToFrontAndResumeStateIfNeeded(
2419 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002420
Wale Ogunwale040b4702015-08-06 18:10:50 -07002421 return stack;
2422 }
2423
Chong Zhange4fbd322016-03-01 14:44:03 -08002424 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002425 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002426 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2427 false /* deferResume */);
2428 }
2429
2430 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2431 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002432 final TaskRecord task = anyTaskForIdLocked(taskId);
2433 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002434 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002435 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002436 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002437
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002438 if (task.stack != null && task.stack.mStackId == stackId) {
2439 // You are already in the right stack silly...
2440 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002441 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002442 }
2443
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002444 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2445 throw new IllegalArgumentException("moveTaskToStack:"
2446 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2447 }
2448
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002449 final ActivityRecord topActivity = task.getTopActivity();
Jorim Jaggie9098022016-01-27 19:29:40 -08002450 final int sourceStackId = task.stack != null ? task.stack.mStackId : INVALID_STACK_ID;
Chong Zhangf596cd52016-01-05 13:42:44 -08002451 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002452 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002453 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002454 // We are about to relaunch the activity because its configuration changed due to
2455 // being maximized, i.e. size change. The activity will first remove the old window
2456 // and then add a new one. This call will tell window manager about this, so it can
2457 // preserve the old window until the new one is drawn. This prevents having a gap
2458 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002459 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002460 // Note here we always set the replacing window first, as the flags might be needed
2461 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Jorim Jaggi030979c2015-11-20 15:14:43 -08002462 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002463 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002464
2465 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002466 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002467 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002468 try {
2469 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002470 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002471 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002472
Wale Ogunwale961f4852016-02-01 20:25:54 -08002473 if (!animate) {
2474 stack.mNoAnimActivities.add(topActivity);
2475 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002476
Wale Ogunwale961f4852016-02-01 20:25:54 -08002477 // We might trigger a configuration change. Save the current task bounds for freezing.
2478 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2479
2480 // Make sure the task has the appropriate bounds/size for the stack it is in.
2481 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002482 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2483 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002484 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2485 Rect bounds = task.getLaunchBounds();
2486 if (bounds == null) {
2487 stack.layoutTaskInStack(task, null);
2488 bounds = task.mBounds;
2489 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002490 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2491 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002492 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002493 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2494 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002495 }
2496 } finally {
2497 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002498 }
2499
2500 if (mightReplaceWindow) {
2501 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2502 // window), we need to clear the replace window settings. Otherwise, we schedule a
2503 // timeout to remove the old window if the replacing window is not coming in time.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002504 mWindowManager.scheduleClearReplacingWindowIfNeeded(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002505 }
2506
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002507 if (!deferResume) {
2508
2509 // The task might have already been running and its visibility needs to be synchronized with
2510 // the visibility of the stack / windows.
2511 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2512 resumeFocusedStackTopActivityLocked();
2513 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002514
Jorim Jaggid53f0922016-04-06 22:16:23 -07002515 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002516
2517 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002518 }
2519
Wale Ogunwale079a0042015-10-24 11:44:07 -07002520 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2521 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2522 if (stack == null) {
2523 throw new IllegalArgumentException(
2524 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2525 }
2526
2527 final ActivityRecord r = stack.topRunningActivityLocked();
2528 if (r == null) {
2529 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2530 + " in stack=" + stack);
2531 return false;
2532 }
2533
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002534 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002535 Slog.w(TAG,
2536 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002537 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002538 return false;
2539 }
2540
Wale Ogunwale480dca02016-02-06 13:58:29 -08002541 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002542 return true;
2543 }
2544
Wale Ogunwale480dca02016-02-06 13:58:29 -08002545 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2546 mWindowManager.deferSurfaceLayout();
2547 try {
2548 final TaskRecord task = r.task;
2549
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002550 if (r == task.stack.getVisibleBehindActivity()) {
2551 // An activity can't be pinned and visible behind at the same time. Go ahead and
2552 // release it from been visible behind before pinning.
2553 requestVisibleBehindLocked(r, false);
2554 }
2555
Wale Ogunwale480dca02016-02-06 13:58:29 -08002556 // Need to make sure the pinned stack exist so we can resize it below...
2557 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2558
2559 // Resize the pinned stack to match the current size of the task the activity we are
2560 // going to be moving is currently contained in. We do this to have the right starting
2561 // animation bounds for the pinned stack to the desired bounds the caller wants.
2562 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2563 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002564 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002565
2566 if (task.mActivities.size() == 1) {
2567 // There is only one activity in the task. So, we can just move the task over to
2568 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002569 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2570 // Move the home stack forward if the task we just moved to the pinned stack
2571 // was launched from home so home should be visible behind it.
2572 moveHomeStackToFront(reason);
2573 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002574 moveTaskToStackLocked(
2575 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2576 } else {
2577 stack.moveActivityToStack(r);
2578 }
2579 } finally {
2580 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002581 }
2582
Wale Ogunwale480dca02016-02-06 13:58:29 -08002583 // The task might have already been running and its visibility needs to be synchronized
2584 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002585 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002586 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002587
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002588 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002589 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002590 }
2591
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002592 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2593 final TaskRecord task = anyTaskForIdLocked(taskId);
2594 if (task == null) {
2595 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2596 return;
2597 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002598 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2599
2600 task.updateOverrideConfigurationForStack(stack);
2601
2602 mWindowManager.positionTaskInStack(
2603 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002604 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002605 // The task might have already been running and its visibility needs to be synchronized with
2606 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002607 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002608 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002609 }
2610
Craig Mautnerac6f8432013-07-17 13:24:59 -07002611 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002612 mTmpFindTaskResult.r = null;
2613 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002614 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002615 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2616 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002617 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2618 final ActivityStack stack = stacks.get(stackNdx);
2619 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002620 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002621 continue;
2622 }
2623 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002624 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2625 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002626 continue;
2627 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002628 stack.findTaskLocked(r, mTmpFindTaskResult);
2629 // It is possible to have task in multiple stacks with the same root affinity.
2630 // If the match we found was based on root affinity we keep on looking to see if
2631 // there is a better match in another stack. We eventually return the match based
2632 // on root affinity if we don't find a better match.
2633 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2634 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002635 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002636 }
2637 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002638 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2639 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002640 }
2641
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002642 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2643 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002644 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2645 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002647 final ActivityRecord ar = stacks.get(stackNdx)
2648 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 if (ar != null) {
2650 return ar;
2651 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002652 }
2653 }
2654 return null;
2655 }
2656
Craig Mautner8d341ef2013-03-26 09:03:27 -07002657 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002658 scheduleSleepTimeout();
2659 if (!mGoingToSleep.isHeld()) {
2660 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002661 if (mLaunchingActivity.isHeld()) {
2662 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2663 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002664 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002665 mLaunchingActivity.release();
2666 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002667 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002668 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002669 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002670 }
2671
2672 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002674
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002675 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002676 final long endTime = System.currentTimeMillis() + timeout;
2677 while (true) {
2678 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002679 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2680 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2682 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2683 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002684 }
2685 if (cantShutdown) {
2686 long timeRemaining = endTime - System.currentTimeMillis();
2687 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002689 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002690 } catch (InterruptedException e) {
2691 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002692 } else {
2693 Slog.w(TAG, "Activity manager shutdown timed out");
2694 timedout = true;
2695 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002696 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002697 } else {
2698 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002699 }
2700 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002701
2702 // Force checkReadyForSleep to complete.
2703 mSleepTimeout = true;
2704 checkReadyForSleepLocked();
2705
Craig Mautner8d341ef2013-03-26 09:03:27 -07002706 return timedout;
2707 }
2708
2709 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002710 removeSleepTimeouts();
2711 if (mGoingToSleep.isHeld()) {
2712 mGoingToSleep.release();
2713 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002714 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2715 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002716 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2717 final ActivityStack stack = stacks.get(stackNdx);
2718 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002719 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002720 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002721 }
Craig Mautner5314a402013-09-26 12:40:16 -07002722 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002723 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002724 mGoingToSleepActivities.clear();
2725 }
2726
2727 void activitySleptLocked(ActivityRecord r) {
2728 mGoingToSleepActivities.remove(r);
2729 checkReadyForSleepLocked();
2730 }
2731
2732 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002733 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 // Do not care.
2735 return;
2736 }
2737
2738 if (!mSleepTimeout) {
2739 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2741 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002742 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2743 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2744 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002745 }
2746
2747 if (mStoppingActivities.size() > 0) {
2748 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002749 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002750 + mStoppingActivities.size() + " activities");
2751 scheduleIdleLocked();
2752 dontSleep = true;
2753 }
2754
2755 if (mGoingToSleepActivities.size() > 0) {
2756 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002757 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002758 + mGoingToSleepActivities.size() + " activities");
2759 dontSleep = true;
2760 }
2761
2762 if (dontSleep) {
2763 return;
2764 }
2765 }
2766
Craig Mautnere0a38842013-12-16 16:14:02 -08002767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2770 stacks.get(stackNdx).goToSleep();
2771 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002772 }
2773
2774 removeSleepTimeouts();
2775
2776 if (mGoingToSleep.isHeld()) {
2777 mGoingToSleep.release();
2778 }
2779 if (mService.mShuttingDown) {
2780 mService.notifyAll();
2781 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002782 }
2783
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002784 boolean reportResumedActivityLocked(ActivityRecord r) {
2785 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002786 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002787 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002788 }
2789 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002790 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002791 mWindowManager.executeAppTransition();
2792 return true;
2793 }
2794 return false;
2795 }
2796
Craig Mautner8d341ef2013-03-26 09:03:27 -07002797 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002798 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2799 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002800 int stackNdx = stacks.size() - 1;
2801 while (stackNdx >= 0) {
2802 stacks.get(stackNdx).handleAppCrashLocked(app);
2803 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002805 }
2806 }
2807
Jose Lima4b6c6692014-08-12 17:41:12 -07002808 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002809 final ActivityStack stack = r.task.stack;
2810 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002811 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2812 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002813 return false;
2814 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002815
2816 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2817 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2818 + " visible=" + visible + " stackId=" + stack.mStackId
2819 + " can't contain visible behind activities");
2820 return false;
2821 }
2822
Jose Lima4b6c6692014-08-12 17:41:12 -07002823 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002824 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2825 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002826
2827 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002828 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002830 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002831 return true;
2832 }
2833
2834 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002835 if (visible && top.fullscreen) {
2836 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2838 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2839 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2840 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002841 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002842 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2843 // Only the activity set as currently visible behind should actively reset its
2844 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002845 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2846 "requestVisibleBehind: returning visible=" + visible
2847 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2848 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002849 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002850 }
2851
Jose Lima4b6c6692014-08-12 17:41:12 -07002852 stack.setVisibleBehindActivity(visible ? r : null);
2853 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002854 // If there is a translucent home activity, we need to force it stop being translucent,
2855 // because we can't depend on the application to necessarily perform that operation.
2856 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002857 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002858 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002859 mService.convertFromTranslucent(next.appToken);
2860 }
2861 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002862 if (top.app != null && top.app.thread != null) {
2863 // Notify the top app of the change.
2864 try {
2865 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2866 } catch (RemoteException e) {
2867 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868 }
2869 return true;
2870 }
2871
Craig Mautnerbb742462014-07-07 15:28:55 -07002872 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2873 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002874 final TaskRecord task = r.task;
Winson730bf062016-03-31 18:04:56 -07002875 final ActivityStack stack = task.stack;
2876
2877 r.mLaunchTaskBehind = false;
2878 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002879 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002880 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002881 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002882
2883 // When launching tasks behind, update the last active time of the top task after the new
2884 // task has been shown briefly
2885 final ActivityRecord top = stack.topActivity();
2886 if (top != null) {
2887 top.task.touchActiveTime();
2888 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002889 }
2890
2891 void scheduleLaunchTaskBehindComplete(IBinder token) {
2892 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2893 }
2894
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002895 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2896 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002897 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2899 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 final int topStackNdx = stacks.size() - 1;
2901 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2902 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002903 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002904 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002906 }
2907
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002908 void invalidateTaskLayers() {
2909 mTaskLayersChanged = true;
2910 }
2911
2912 void rankTaskLayersIfNeeded() {
2913 if (!mTaskLayersChanged) {
2914 return;
2915 }
2916 mTaskLayersChanged = false;
2917 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2918 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2919 int baseLayer = 0;
2920 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2921 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2922 }
2923 }
2924 }
2925
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002926 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2929 final int topStackNdx = stacks.size() - 1;
2930 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2931 final ActivityStack stack = stacks.get(stackNdx);
2932 stack.clearOtherAppTimeTrackers(except);
2933 }
2934 }
2935 }
2936
Craig Mautner8d341ef2013-03-26 09:03:27 -07002937 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002938 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2939 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002940 final int numStacks = stacks.size();
2941 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2942 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002943 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 }
2946 }
2947
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002948 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2949 // Examine all activities currently running in the process.
2950 TaskRecord firstTask = null;
2951 // Tasks is non-null only if two or more tasks are found.
2952 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002953 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2954 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002955 ActivityRecord r = app.activities.get(i);
2956 // First, if we find an activity that is in the process of being destroyed,
2957 // then we just aren't going to do anything for now; we want things to settle
2958 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002959 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002960 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002961 return;
2962 }
2963 // Don't consider any activies that are currently not in a state where they
2964 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002965 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2966 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002968 continue;
2969 }
2970 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002971 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002972 + " from " + r);
2973 if (firstTask == null) {
2974 firstTask = r.task;
2975 } else if (firstTask != r.task) {
2976 if (tasks == null) {
2977 tasks = new ArraySet<>();
2978 tasks.add(firstTask);
2979 }
2980 tasks.add(r.task);
2981 }
2982 }
2983 }
2984 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002985 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002986 return;
2987 }
2988 // If we have activities in multiple tasks that are in a position to be destroyed,
2989 // let's iterate through the tasks and release the oldest one.
2990 final int numDisplays = mActivityDisplays.size();
2991 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2992 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2993 // Step through all stacks starting from behind, to hit the oldest things first.
2994 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2995 final ActivityStack stack = stacks.get(stackNdx);
2996 // Try to release activities in this stack; if we manage to, we are done.
2997 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2998 return;
2999 }
3000 }
3001 }
3002 }
3003
Amith Yamasani37a40c22015-06-17 13:25:42 -07003004 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003005 final int focusStackId = mFocusedStack.getStackId();
3006 // We dismiss the docked stack whenever we switch users.
3007 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3008
3009 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003010 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003011 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003012
Craig Mautner858d8a62013-04-23 17:08:34 -07003013 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3015 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003017 final ActivityStack stack = stacks.get(stackNdx);
3018 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003019 TaskRecord task = stack.topTask();
3020 if (task != null) {
3021 mWindowManager.moveTaskToTop(task.taskId);
3022 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003023 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003024 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003025
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003026 ActivityStack stack = getStack(restoreStackId);
3027 if (stack == null) {
3028 stack = mHomeStack;
3029 }
3030 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003032 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003033 } else {
3034 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003035 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003036 }
Craig Mautner93529a42013-10-04 15:03:13 -07003037 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003038 }
3039
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003040 /** Checks whether the userid is a profile of the current user. */
3041 boolean isCurrentProfileLocked(int userId) {
3042 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003043 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003044 }
3045
Chong Zhang45c25ce2015-08-10 22:18:26 -07003046 /** Checks whether the activity should be shown for current user. */
3047 boolean okToShowLocked(ActivityRecord r) {
3048 return r != null && (isCurrentProfileLocked(r.userId)
3049 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3050 }
3051
Craig Mautnerde4ef022013-04-07 19:01:33 -07003052 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003053 ArrayList<ActivityRecord> stops = null;
3054
3055 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003056 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3057 ActivityRecord s = mStoppingActivities.get(activityNdx);
3058 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003059 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003060 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3061 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003062 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003063 if (s.finishing) {
3064 // If this activity is finishing, it is sitting on top of
3065 // everyone else but we now know it is no longer needed...
3066 // so get rid of it. Otherwise, we need to go through the
3067 // normal flow and hide it once we determine that it is
3068 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003069 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003070 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003071 }
3072 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003073 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003074 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003075 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003076 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 }
3078 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003079 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003080 }
3081 }
3082
3083 return stops;
3084 }
3085
Craig Mautnercf910b02013-04-23 11:23:27 -07003086 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003087 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3088 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3089 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3090 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003091 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003092 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003093 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003094 if (r == null) Slog.e(TAG,
3095 "validateTop...: null top activity, stack=" + stack);
3096 else {
3097 final ActivityRecord pausing = stack.mPausingActivity;
3098 if (pausing != null && pausing == r) Slog.e(TAG,
3099 "validateTop...: top stack has pausing activity r=" + r
3100 + " state=" + state);
3101 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3102 "validateTop...: activity in front not resumed r=" + r
3103 + " state=" + state);
3104 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003105 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003106 final ActivityRecord resumed = stack.mResumedActivity;
3107 if (resumed != null && resumed == r) Slog.e(TAG,
3108 "validateTop...: back stack has resumed activity r=" + r
3109 + " state=" + state);
3110 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3111 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003112 }
3113 }
3114 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003115 }
3116
Craig Mautnere0570202015-05-13 13:06:11 -07003117 private String lockTaskModeToString() {
3118 switch (mLockTaskModeState) {
3119 case LOCK_TASK_MODE_LOCKED:
3120 return "LOCKED";
3121 case LOCK_TASK_MODE_PINNED:
3122 return "PINNED";
3123 case LOCK_TASK_MODE_NONE:
3124 return "NONE";
3125 default: return "unknown=" + mLockTaskModeState;
3126 }
3127 }
3128
Craig Mautner27084302013-03-25 08:05:25 -07003129 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003130 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003131 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003132 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003133 pw.print(prefix);
3134 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003135 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003136 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003137 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3138 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3139 if (packages.size() > 0) {
3140 pw.println(" mLockTaskPackages (userId:packages)=");
3141 for (int i = 0; i < packages.size(); ++i) {
3142 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3143 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3144 }
3145 }
3146 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003147 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003148
Craig Mautner20e72272013-04-01 13:45:53 -07003149 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003150 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003151 }
3152
Dianne Hackborn390517b2013-05-30 15:03:32 -07003153 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3154 boolean needSep, String prefix) {
3155 if (activity != null) {
3156 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3157 if (needSep) {
3158 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003159 }
3160 pw.print(prefix);
3161 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003162 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003163 }
3164 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003165 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003166 }
3167
Craig Mautner8d341ef2013-03-26 09:03:27 -07003168 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3169 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003170 boolean printed = false;
3171 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003172 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3173 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003174 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003175 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003176 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003177 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 final ActivityStack stack = stacks.get(stackNdx);
3179 StringBuilder stackHeader = new StringBuilder(128);
3180 stackHeader.append(" Stack #");
3181 stackHeader.append(stack.mStackId);
3182 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003183 stackHeader.append("\n");
3184 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3185 stackHeader.append("\n");
3186 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3188 needSep, stackHeader.toString());
3189 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3190 !dumpAll, false, dumpPackage, true,
3191 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003192
Craig Mautner4a1cb222013-12-04 16:14:06 -08003193 needSep = printed;
3194 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3195 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003196 if (pr) {
3197 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003198 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003199 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003200 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3201 " mResumedActivity: ");
3202 if (pr) {
3203 printed = true;
3204 needSep = false;
3205 }
3206 if (dumpAll) {
3207 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3208 " mLastPausedActivity: ");
3209 if (pr) {
3210 printed = true;
3211 needSep = true;
3212 }
3213 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3214 needSep, " mLastNoHistoryActivity: ");
3215 }
3216 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 }
3218 }
3219
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003220 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3221 false, dumpPackage, true, " Activities waiting to finish:", null);
3222 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3223 false, dumpPackage, true, " Activities waiting to stop:", null);
3224 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3225 false, dumpPackage, true, " Activities waiting for another to become visible:",
3226 null);
3227 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3228 false, dumpPackage, true, " Activities waiting to sleep:", null);
3229 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3230 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003231
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003232 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 }
3234
Dianne Hackborn390517b2013-05-30 15:03:32 -07003235 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003237 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003238 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003239 String innerPrefix = null;
3240 String[] args = null;
3241 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 for (int i=list.size()-1; i>=0; i--) {
3243 final ActivityRecord r = list.get(i);
3244 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3245 continue;
3246 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003247 if (innerPrefix == null) {
3248 innerPrefix = prefix + " ";
3249 args = new String[0];
3250 }
3251 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252 final boolean full = !brief && (complete || !r.isInHistory());
3253 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003254 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003255 needNL = false;
3256 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003257 if (header1 != null) {
3258 pw.println(header1);
3259 header1 = null;
3260 }
3261 if (header2 != null) {
3262 pw.println(header2);
3263 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003264 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003265 if (lastTask != r.task) {
3266 lastTask = r.task;
3267 pw.print(prefix);
3268 pw.print(full ? "* " : " ");
3269 pw.println(lastTask);
3270 if (full) {
3271 lastTask.dump(pw, prefix + " ");
3272 } else if (complete) {
3273 // Complete + brief == give a summary. Isn't that obvious?!?
3274 if (lastTask.intent != null) {
3275 pw.print(prefix); pw.print(" ");
3276 pw.println(lastTask.intent.toInsecureStringWithClip());
3277 }
3278 }
3279 }
3280 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3281 pw.print(" #"); pw.print(i); pw.print(": ");
3282 pw.println(r);
3283 if (full) {
3284 r.dump(pw, innerPrefix);
3285 } else if (complete) {
3286 // Complete + brief == give a summary. Isn't that obvious?!?
3287 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3288 if (r.app != null) {
3289 pw.print(innerPrefix); pw.println(r.app);
3290 }
3291 }
3292 if (client && r.app != null && r.app.thread != null) {
3293 // flush anything that is already in the PrintWriter since the thread is going
3294 // to write to the file descriptor directly
3295 pw.flush();
3296 try {
3297 TransferPipe tp = new TransferPipe();
3298 try {
3299 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3300 r.appToken, innerPrefix, args);
3301 // Short timeout, since blocking here can
3302 // deadlock with the application.
3303 tp.go(fd, 2000);
3304 } finally {
3305 tp.kill();
3306 }
3307 } catch (IOException e) {
3308 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3309 } catch (RemoteException e) {
3310 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3311 }
3312 needNL = true;
3313 }
3314 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003315 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003317
Craig Mautnerf3333272013-04-22 10:55:53 -07003318 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003319 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3320 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003321 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3322 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003323 }
3324
3325 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003326 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003327 }
3328
3329 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003330 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3331 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003332 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3333 }
3334
Craig Mautner05d29032013-05-03 13:40:13 -07003335 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003336 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3337 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3338 }
Craig Mautner05d29032013-05-03 13:40:13 -07003339 }
3340
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341 void removeSleepTimeouts() {
3342 mSleepTimeout = false;
3343 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3344 }
3345
3346 final void scheduleSleepTimeout() {
3347 removeSleepTimeouts();
3348 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3349 }
3350
Craig Mautner4a1cb222013-12-04 16:14:06 -08003351 @Override
3352 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003353 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003354 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3355 }
3356
3357 @Override
3358 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003359 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003360 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3361 }
3362
3363 @Override
3364 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003365 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3367 }
3368
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003369 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003370 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003371 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003372 newDisplay = mActivityDisplays.get(displayId) == null;
3373 if (newDisplay) {
3374 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003375 if (activityDisplay.mDisplay == null) {
3376 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3377 return;
3378 }
Craig Mautner4504de52013-12-20 09:06:56 -08003379 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003380 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003381 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003382 }
Craig Mautner4504de52013-12-20 09:06:56 -08003383 if (newDisplay) {
3384 mWindowManager.onDisplayAdded(displayId);
3385 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003386 }
3387
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003388 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003389 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003390 mService.mContext.getResources().getDimensionPixelSize(
3391 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003392 }
3393
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003394 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003396 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3397 if (activityDisplay != null) {
3398 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003399 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003400 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003402 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003403 }
3404 }
3405 mWindowManager.onDisplayRemoved(displayId);
3406 }
3407
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003408 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003410 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3411 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003412 // TODO: Update the bounds.
3413 }
3414 }
3415 mWindowManager.onDisplayChanged(displayId);
3416 }
3417
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003418 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003419 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 StackInfo info = new StackInfo();
3421 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3422 info.displayId = Display.DEFAULT_DISPLAY;
3423 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003424 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003425 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003426 info.position = display != null
3427 ? display.mStacks.indexOf(stack)
3428 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003429
3430 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3431 final int numTasks = tasks.size();
3432 int[] taskIds = new int[numTasks];
3433 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003434 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003435 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 for (int i = 0; i < numTasks; ++i) {
3437 final TaskRecord task = tasks.get(i);
3438 taskIds[i] = task.taskId;
3439 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3440 : task.realActivity != null ? task.realActivity.flattenToString()
3441 : task.getTopActivity() != null ? task.getTopActivity().packageName
3442 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003443 taskBounds[i] = new Rect();
3444 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003445 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 }
3447 info.taskIds = taskIds;
3448 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003449 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003450 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003451
3452 final ActivityRecord top = stack.topRunningActivityLocked();
3453 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003454 return info;
3455 }
3456
3457 StackInfo getStackInfoLocked(int stackId) {
3458 ActivityStack stack = getStack(stackId);
3459 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003460 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 }
3462 return null;
3463 }
3464
3465 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003466 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003467 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3468 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003469 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003470 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 }
3472 }
3473 return list;
3474 }
3475
Craig Mautner15df08a2015-04-01 12:17:18 -07003476 TaskRecord getLockedTaskLocked() {
3477 final int top = mLockTaskModeTasks.size() - 1;
3478 if (top >= 0) {
3479 return mLockTaskModeTasks.get(top);
3480 }
3481 return null;
3482 }
3483
3484 boolean isLockedTask(TaskRecord task) {
3485 return mLockTaskModeTasks.contains(task);
3486 }
3487
3488 boolean isLastLockedTask(TaskRecord task) {
3489 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3490 }
3491
3492 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003493 if (!mLockTaskModeTasks.remove(task)) {
3494 return;
3495 }
3496 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3497 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003498 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003499 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3500 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003501 final Message lockTaskMsg = Message.obtain();
3502 lockTaskMsg.arg1 = task.userId;
3503 lockTaskMsg.what = LOCK_TASK_END_MSG;
3504 mHandler.sendMessage(lockTaskMsg);
3505 }
3506 }
3507
Andrii Kulianc27916642016-04-12 17:59:27 -07003508 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3509 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3510 false /* forceNonResizable */);
3511 }
3512
Jorim Jaggid53f0922016-04-06 22:16:23 -07003513 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003514 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003515 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3516 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003517 return;
3518 }
3519
Jorim Jaggicd13d332016-04-27 15:40:20 -07003520 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003521 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003522 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003523 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3524
Andrii Kulianc27916642016-04-12 17:59:27 -07003525 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3526 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003527 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003528 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3529 && !topActivity.noDisplay) {
3530 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003531 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3532 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003533 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003534 }
3535
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003536 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003537 if (mLockTaskNotify != null) {
3538 mLockTaskNotify.showToast(mLockTaskModeState);
3539 }
Jason Monka8f569c2014-07-07 12:15:21 -04003540 }
3541
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003542 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3543 if (mLockTaskModeTasks.contains(task)) {
3544 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3545 }
3546 }
3547
Craig Mautner432f64e2015-05-20 14:59:57 -07003548 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3549 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003550 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003551 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003552 final TaskRecord lockedTask = getLockedTaskLocked();
3553 if (lockedTask != null) {
3554 removeLockedTaskLocked(lockedTask);
3555 if (!mLockTaskModeTasks.isEmpty()) {
3556 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003557 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3558 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003559 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003560 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003561 return;
3562 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003563 }
Craig Mautnere0570202015-05-13 13:06:11 -07003564 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3565 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003566 return;
3567 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003568
3569 // Should have already been checked, but do it again.
3570 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003571 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3572 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003573 return;
3574 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003575 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003576 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003577 return;
3578 }
3579
3580 if (mLockTaskModeTasks.isEmpty()) {
3581 // First locktask.
3582 final Message lockTaskMsg = Message.obtain();
3583 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3584 lockTaskMsg.arg1 = task.userId;
3585 lockTaskMsg.what = LOCK_TASK_START_MSG;
3586 lockTaskMsg.arg2 = lockTaskModeState;
3587 mHandler.sendMessage(lockTaskMsg);
3588 }
3589 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003590 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3591 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003592 mLockTaskModeTasks.remove(task);
3593 mLockTaskModeTasks.add(task);
3594
3595 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003596 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003597 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003598
3599 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003600 findTaskToMoveToFrontLocked(task, 0, null, reason,
3601 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003602 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003603 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
3604 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.stack.mStackId,
3605 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003606 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003607 }
3608
3609 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003610 return isLockTaskModeViolation(task, false);
3611 }
3612
3613 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3614 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003615 return false;
3616 }
3617 final int lockTaskAuth = task.mLockTaskAuth;
3618 switch (lockTaskAuth) {
3619 case LOCK_TASK_AUTH_DONT_LOCK:
3620 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003621 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003622 case LOCK_TASK_AUTH_LAUNCHABLE:
3623 case LOCK_TASK_AUTH_WHITELISTED:
3624 return false;
3625 case LOCK_TASK_AUTH_PINNABLE:
3626 // Pinnable tasks can't be launched on top of locktask tasks.
3627 return !mLockTaskModeTasks.isEmpty();
3628 default:
3629 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3630 return true;
3631 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003632 }
3633
Craig Mautner15df08a2015-04-01 12:17:18 -07003634 void onLockTaskPackagesUpdatedLocked() {
3635 boolean didSomething = false;
3636 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3637 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003638 final boolean wasWhitelisted =
3639 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3640 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003641 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003642 final boolean isWhitelisted =
3643 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3644 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3645 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003646 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003647 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3648 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003649 removeLockedTaskLocked(lockedTask);
3650 lockedTask.performClearTaskLocked();
3651 didSomething = true;
3652 }
3653 }
3654 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3655 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3657 final ActivityStack stack = stacks.get(stackNdx);
3658 stack.onLockTaskPackagesUpdatedLocked();
3659 }
3660 }
Craig Mautnere0570202015-05-13 13:06:11 -07003661 final ActivityRecord r = topRunningActivityLocked();
3662 final TaskRecord task = r != null ? r.task : null;
3663 if (mLockTaskModeTasks.isEmpty() && task != null
3664 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3665 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003666 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3667 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3668 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3669 false);
3670 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003671 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003672 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003673 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003674 }
3675 }
3676
Benjamin Franz43261142015-02-11 15:59:44 +00003677 int getLockTaskModeState() {
3678 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003679 }
3680
Jorim Jaggife89d122015-12-22 16:28:44 +01003681 void activityRelaunchedLocked(IBinder token) {
3682 mWindowManager.notifyAppRelaunchingFinished(token);
3683 }
3684
3685 void activityRelaunchingLocked(ActivityRecord r) {
3686 mWindowManager.notifyAppRelaunching(r.appToken);
3687 }
3688
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003689 void logStackState() {
3690 mActivityMetricsLogger.logWindowState();
3691 }
3692
Andrii Kulian933076d2016-03-29 17:04:42 -07003693 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003694 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3695 final ActivityRecord r = task.mActivities.get(i);
3696 if (r.app != null && r.app.thread != null) {
3697 mMultiWindowModeChangedActivities.add(r);
3698 }
3699 }
3700
3701 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3702 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3703 }
3704 }
3705
Andrii Kulian933076d2016-03-29 17:04:42 -07003706 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003707 final ActivityStack stack = task.stack;
3708 if (prevStack == null || prevStack == stack
3709 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3710 return;
3711 }
3712
3713 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3714 final ActivityRecord r = task.mActivities.get(i);
3715 if (r.app != null && r.app.thread != null) {
3716 mPipModeChangedActivities.add(r);
3717 }
3718 }
3719
3720 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3721 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3722 }
3723 }
3724
Tony Mak853304c2016-04-18 15:17:41 +01003725 void setDockedStackMinimized(boolean minimized) {
3726 mIsDockMinimized = minimized;
3727 if (minimized) {
3728 // Docked stack is not visible, no need to confirm credentials for its top activity.
3729 return;
3730 }
3731 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3732 if (dockedStack == null) {
3733 return;
3734 }
3735 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3736 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3737 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3738 }
3739 }
3740
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003741 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003742
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003743 public ActivityStackSupervisorHandler(Looper looper) {
3744 super(looper);
3745 }
3746
Craig Mautnerf3333272013-04-22 10:55:53 -07003747 void activityIdleInternal(ActivityRecord r) {
3748 synchronized (mService) {
3749 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3750 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003751 }
3752
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003753 @Override
3754 public void handleMessage(Message msg) {
3755 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003756 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3757 synchronized (mService) {
3758 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3759 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003760 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003761 }
3762 }
3763 } break;
3764 case REPORT_PIP_MODE_CHANGED_MSG: {
3765 synchronized (mService) {
3766 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3767 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003768 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003769 }
3770 }
3771 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003772 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003773 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3774 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003775 if (mService.mDidDexOpt) {
3776 mService.mDidDexOpt = false;
3777 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3778 nmsg.obj = msg.obj;
3779 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3780 return;
3781 }
3782 // We don't at this point know if the activity is fullscreen,
3783 // so we need to be conservative and assume it isn't.
3784 activityIdleInternal((ActivityRecord)msg.obj);
3785 } break;
3786 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003787 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003788 activityIdleInternal((ActivityRecord)msg.obj);
3789 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003790 case RESUME_TOP_ACTIVITY_MSG: {
3791 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003792 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003793 }
3794 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003795 case SLEEP_TIMEOUT_MSG: {
3796 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003797 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003798 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3799 mSleepTimeout = true;
3800 checkReadyForSleepLocked();
3801 }
3802 }
3803 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003804 case LAUNCH_TIMEOUT_MSG: {
3805 if (mService.mDidDexOpt) {
3806 mService.mDidDexOpt = false;
3807 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3808 return;
3809 }
3810 synchronized (mService) {
3811 if (mLaunchingActivity.isHeld()) {
3812 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3813 if (VALIDATE_WAKE_LOCK_CALLER
3814 && Binder.getCallingUid() != Process.myUid()) {
3815 throw new IllegalStateException("Calling must be system uid");
3816 }
3817 mLaunchingActivity.release();
3818 }
3819 }
3820 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003822 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 } break;
3824 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003825 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 } break;
3827 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003828 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003830 case CONTAINER_CALLBACK_VISIBILITY: {
3831 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003832 final IActivityContainerCallback callback = container.mCallback;
3833 if (callback != null) {
3834 try {
3835 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3836 } catch (RemoteException e) {
3837 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003838 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003839 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003840 case LOCK_TASK_START_MSG: {
3841 // When lock task starts, we disable the status bars.
3842 try {
Jason Monk62515be2014-05-21 16:06:19 -04003843 if (mLockTaskNotify == null) {
3844 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003845 }
Jason Monk62515be2014-05-21 16:06:19 -04003846 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003847 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003848 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003849 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003850 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003851 flags = StatusBarManager.DISABLE_MASK
3852 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003853 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003854 flags = StatusBarManager.DISABLE_MASK
3855 & (~StatusBarManager.DISABLE_BACK)
3856 & (~StatusBarManager.DISABLE_HOME)
3857 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003858 }
3859 getStatusBarService().disable(flags, mToken,
3860 mService.mContext.getPackageName());
3861 }
3862 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003863 if (getDevicePolicyManager() != null) {
3864 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003865 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003866 }
justinzhang5286d3f2014-05-12 17:06:01 -04003867 } catch (RemoteException ex) {
3868 throw new RuntimeException(ex);
3869 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003870 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003871 case LOCK_TASK_END_MSG: {
3872 // When lock task ends, we enable the status bars.
3873 try {
Jason Monk62515be2014-05-21 16:06:19 -04003874 if (getStatusBarService() != null) {
3875 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3876 mService.mContext.getPackageName());
3877 }
3878 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003879 if (getDevicePolicyManager() != null) {
3880 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3881 msg.arg1);
3882 }
Jason Monk62515be2014-05-21 16:06:19 -04003883 if (mLockTaskNotify == null) {
3884 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3885 }
3886 mLockTaskNotify.show(false);
3887 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003888 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003889 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003890 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003891 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003892 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003893 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003894 new LockPatternUtils(mService.mContext)
3895 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003896 }
3897 } catch (SettingNotFoundException e) {
3898 // No setting, don't lock.
3899 }
justinzhang5286d3f2014-05-12 17:06:01 -04003900 } catch (RemoteException ex) {
3901 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003902 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003903 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003904 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003905 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003906 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3907 if (mLockTaskNotify == null) {
3908 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3909 }
3910 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3911 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003912 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3913 final ActivityContainer container = (ActivityContainer) msg.obj;
3914 final IActivityContainerCallback callback = container.mCallback;
3915 if (callback != null) {
3916 try {
3917 callback.onAllActivitiesComplete(container.asBinder());
3918 } catch (RemoteException e) {
3919 }
3920 }
3921 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003922 case LAUNCH_TASK_BEHIND_COMPLETE: {
3923 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003924 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003925 if (r != null) {
3926 handleLaunchTaskBehindCompleteLocked(r);
3927 }
3928 }
3929 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003930
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003931 }
3932 }
3933 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003934
Ying Wangb081a592014-04-22 15:20:16 -07003935 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003936 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3937 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003939 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003941 ActivityRecord mParentActivity = null;
3942 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003943
Craig Mautnere3a00d72014-04-16 08:31:19 -07003944 boolean mVisible = true;
3945
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003947 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003949 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3950 final static int CONTAINER_STATE_NO_SURFACE = 1;
3951 final static int CONTAINER_STATE_FINISHING = 2;
3952 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3953
3954 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955 synchronized (mService) {
3956 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003957 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003958 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003959 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003961 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003962
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003963 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003964 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003965 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08003966 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003967 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003968 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003969 }
3970
3971 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003972 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003973 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003974 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3975 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 return;
3977 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003978 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003979 }
3980 }
3981
3982 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003983 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003984 synchronized (mService) {
3985 if (mActivityDisplay != null) {
3986 return mActivityDisplay.mDisplayId;
3987 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003988 }
3989 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003990 }
3991
Jeff Brownca9bc702014-02-11 14:32:56 -08003992 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08003993 public int getStackId() {
3994 synchronized (mService) {
3995 return mStackId;
3996 }
3997 }
3998
3999 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004000 public boolean injectEvent(InputEvent event) {
4001 final long origId = Binder.clearCallingIdentity();
4002 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004003 synchronized (mService) {
4004 if (mActivityDisplay != null) {
4005 return mInputManagerInternal.injectInputEvent(event,
4006 mActivityDisplay.mDisplayId,
4007 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4008 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004009 }
4010 return false;
4011 } finally {
4012 Binder.restoreCallingIdentity(origId);
4013 }
4014 }
4015
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004016 @Override
4017 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004018 synchronized (mService) {
4019 if (mContainerState == CONTAINER_STATE_FINISHING) {
4020 return;
4021 }
4022 mContainerState = CONTAINER_STATE_FINISHING;
4023
Craig Mautnerd163e752014-06-13 17:18:47 -07004024 long origId = Binder.clearCallingIdentity();
4025 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004026 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004027 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004028 } finally {
4029 Binder.restoreCallingIdentity(origId);
4030 }
4031 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004032 }
4033
Craig Mautner60257702014-09-17 15:02:33 -07004034 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004035 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004036 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004037 if (mActivityDisplay != null) {
4038 mActivityDisplay.detachActivitiesLocked(mStack);
4039 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004040 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004041 }
4042 }
4043
4044 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004045 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004046 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 }
4048
4049 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004050 public final int startActivityIntentSender(IIntentSender intentSender)
4051 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004052 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4053
4054 if (!(intentSender instanceof PendingIntentRecord)) {
4055 throw new IllegalArgumentException("Bad PendingIntent object");
4056 }
4057
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004058 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004059 Binder.getCallingUid(), mCurrentUser, false,
4060 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004061
4062 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4063 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4064 pendingIntent.key.requestResolvedType);
4065
4066 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4067 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4068 }
4069
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004070 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004071 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004072 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004073 throw new SecurityException(
4074 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4075 }
4076 }
4077
Craig Mautnerdf88d732014-01-27 09:21:32 -08004078 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004079 public IBinder asBinder() {
4080 return this;
4081 }
4082
Craig Mautner4504de52013-12-20 09:06:56 -08004083 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004084 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004085 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004086 }
4087
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 ActivityStackSupervisor getOuter() {
4089 return ActivityStackSupervisor.this;
4090 }
4091
Craig Mautnerd163e752014-06-13 17:18:47 -07004092 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004093 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 }
4095
Craig Mautner6985bad2014-04-21 15:22:06 -07004096 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004097 void setVisible(boolean visible) {
4098 if (mVisible != visible) {
4099 mVisible = visible;
4100 if (mCallback != null) {
4101 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4102 0 /* unused */, this).sendToTarget();
4103 }
4104 }
4105 }
4106
Craig Mautner6985bad2014-04-21 15:22:06 -07004107 void setDrawn() {
4108 }
4109
Craig Mautner1b4bf852014-05-26 15:06:32 -07004110 // You can always start a new task on a regular ActivityStack.
4111 boolean isEligibleForNewTasks() {
4112 return true;
4113 }
4114
Craig Mautnerd163e752014-06-13 17:18:47 -07004115 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004116 detachLocked();
4117 deleteActivityContainer(this);
4118 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004119 }
4120
Craig Mautner34b73df2014-01-12 21:11:08 -08004121 @Override
4122 public String toString() {
4123 return mIdString + (mActivityDisplay == null ? "N" : "A");
4124 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004125 }
4126
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004127 private class VirtualActivityContainer extends ActivityContainer {
4128 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004129 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004130
4131 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4132 super(getNextStackId());
4133 mParentActivity = parent;
4134 mCallback = callback;
4135 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004136 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004137 }
4138
4139 @Override
4140 public void setSurface(Surface surface, int width, int height, int density) {
4141 super.setSurface(surface, width, height, density);
4142
4143 synchronized (mService) {
4144 final long origId = Binder.clearCallingIdentity();
4145 try {
4146 setSurfaceLocked(surface, width, height, density);
4147 } finally {
4148 Binder.restoreCallingIdentity(origId);
4149 }
4150 }
4151 }
4152
4153 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4154 if (mContainerState == CONTAINER_STATE_FINISHING) {
4155 return;
4156 }
4157 VirtualActivityDisplay virtualActivityDisplay =
4158 (VirtualActivityDisplay) mActivityDisplay;
4159 if (virtualActivityDisplay == null) {
4160 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004161 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004162 mActivityDisplay = virtualActivityDisplay;
4163 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004164 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004165 }
4166
4167 if (mSurface != null) {
4168 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004169 }
4170
Craig Mautner6985bad2014-04-21 15:22:06 -07004171 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004172 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004173 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004174 } else {
4175 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004176 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004177 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004178 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004179 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004180 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004181
Craig Mautnerd163e752014-06-13 17:18:47 -07004182 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004183
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004184 if (DEBUG_STACK) Slog.d(TAG_STACK,
4185 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004186 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004187
Craig Mautner6985bad2014-04-21 15:22:06 -07004188 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004189 boolean isAttachedLocked() {
4190 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004191 }
4192
4193 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004194 void setDrawn() {
4195 synchronized (mService) {
4196 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004197 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004198 }
4199 }
4200
Craig Mautner1b4bf852014-05-26 15:06:32 -07004201 // Never start a new task on an ActivityView if it isn't explicitly specified.
4202 @Override
4203 boolean isEligibleForNewTasks() {
4204 return false;
4205 }
4206
Craig Mautnerd163e752014-06-13 17:18:47 -07004207 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004208 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004209 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4210 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4211 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4212 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4213 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004214 }
4215 }
4216
Craig Mautner4a1cb222013-12-04 16:14:06 -08004217 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4218 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004219 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004220 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004221 int mDisplayId;
4222 Display mDisplay;
4223 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004224
Craig Mautner4a1cb222013-12-04 16:14:06 -08004225 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4226 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004227 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004228
Jose Lima4b6c6692014-08-12 17:41:12 -07004229 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004230
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 ActivityDisplay() {
4232 }
Craig Mautner4504de52013-12-20 09:06:56 -08004233
Craig Mautner1a70a162014-09-13 12:09:31 -07004234 // After instantiation, check that mDisplay is not null before using this. The alternative
4235 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004236 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004237 final Display display = mDisplayManager.getDisplay(displayId);
4238 if (display == null) {
4239 return;
4240 }
4241 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004242 }
4243
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004244 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004245 mDisplay = display;
4246 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004248 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004249
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004250 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004251 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004252 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4253 + " onTop=" + onTop);
4254 if (onTop) {
4255 mStacks.add(stack);
4256 } else {
4257 mStacks.add(0, stack);
4258 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 }
4260
Craig Mautnere0a38842013-12-16 16:14:02 -08004261 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004262 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004263 + " from displayId=" + mDisplayId);
4264 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004265 }
4266
Jose Lima4b6c6692014-08-12 17:41:12 -07004267 void setVisibleBehindActivity(ActivityRecord r) {
4268 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004269 }
4270
Jose Lima4b6c6692014-08-12 17:41:12 -07004271 boolean hasVisibleBehindActivity() {
4272 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004273 }
4274
Craig Mautner34b73df2014-01-12 21:11:08 -08004275 @Override
4276 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004277 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4278 }
4279 }
4280
4281 class VirtualActivityDisplay extends ActivityDisplay {
4282 VirtualDisplay mVirtualDisplay;
4283
Craig Mautner6985bad2014-04-21 15:22:06 -07004284 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004285 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004286 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4287 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4288 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4289 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004290
4291 init(mVirtualDisplay.getDisplay());
4292
4293 mWindowManager.handleDisplayAdded(mDisplayId);
4294 }
4295
4296 void setSurface(Surface surface) {
4297 if (mVirtualDisplay != null) {
4298 mVirtualDisplay.setSurface(surface);
4299 }
4300 }
4301
4302 @Override
4303 void detachActivitiesLocked(ActivityStack stack) {
4304 super.detachActivitiesLocked(stack);
4305 if (mVirtualDisplay != null) {
4306 mVirtualDisplay.release();
4307 mVirtualDisplay = null;
4308 }
4309 }
4310
4311 @Override
4312 public String toString() {
4313 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004314 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004315 }
Jose Lima58e66d62014-05-27 20:00:27 -07004316
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004317 /**
4318 * Adjust bounds to stay within stack bounds.
4319 *
4320 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4321 * that keep them unchanged, but be contained within the stack bounds.
4322 *
4323 * @param bounds Bounds to be adjusted.
4324 * @param stackBounds Bounds within which the other bounds should remain.
4325 */
4326 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4327 if (stackBounds == null || stackBounds.contains(bounds)) {
4328 return;
4329 }
4330
4331 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4332 final int maxRight = stackBounds.right
4333 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4334 int horizontalDiff = stackBounds.left - bounds.left;
4335 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4336 || (bounds.left + horizontalDiff >= maxRight)) {
4337 horizontalDiff = maxRight - bounds.left;
4338 }
4339 bounds.left += horizontalDiff;
4340 bounds.right += horizontalDiff;
4341 }
4342
4343 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4344 final int maxBottom = stackBounds.bottom
4345 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4346 int verticalDiff = stackBounds.top - bounds.top;
4347 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4348 || (bounds.top + verticalDiff >= maxBottom)) {
4349 verticalDiff = maxBottom - bounds.top;
4350 }
4351 bounds.top += verticalDiff;
4352 bounds.bottom += verticalDiff;
4353 }
4354 }
4355
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004356 ActivityStack findStackBehind(ActivityStack stack) {
4357 // TODO(multi-display): We are only looking for stacks on the default display.
4358 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4359 if (display == null) {
4360 return null;
4361 }
4362 final ArrayList<ActivityStack> stacks = display.mStacks;
4363 for (int i = stacks.size() - 1; i >= 0; i--) {
4364 if (stacks.get(i) == stack && i > 0) {
4365 return stacks.get(i - 1);
4366 }
4367 }
4368 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4369 + " in=" + stacks);
4370 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004371
Jorim Jaggic69bd222016-03-15 14:38:37 +01004372 /**
4373 * Puts a task into resizing mode during the next app transition.
4374 *
4375 * @param taskId the id of the task to put into resizing mode
4376 */
4377 private void setResizingDuringAnimation(int taskId) {
4378 mResizingTasksDuringAnimation.add(taskId);
4379 mWindowManager.setTaskDockedResizing(taskId, true);
4380 }
4381
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004382 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4383 final TaskRecord task;
4384 final int callingUid;
4385 final String callingPackage;
4386 final Intent intent;
4387 final int userId;
4388 final ActivityOptions activityOptions = (bOptions != null)
4389 ? new ActivityOptions(bOptions) : null;
4390 final int launchStackId = (activityOptions != null)
4391 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004392 if (launchStackId == HOME_STACK_ID) {
4393 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4394 + taskId + " can't be launch in the home stack.");
4395 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004396
4397 if (launchStackId == DOCKED_STACK_ID) {
4398 mWindowManager.setDockedStackCreateState(
4399 activityOptions.getDockCreateMode(), null /* initialBounds */);
4400
4401 // Defer updating the stack in which recents is until the app transition is done, to
4402 // not run into issues where we still need to draw the task in recents but the
4403 // docked stack is already created.
4404 deferUpdateBounds(HOME_STACK_ID);
4405 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4406 }
4407
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004408 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4409 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004410 continueUpdateBounds(HOME_STACK_ID);
4411 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004412 throw new IllegalArgumentException(
4413 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4414 }
4415
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004416 // Since we don't have an actual source record here, we assume that the currently focused
4417 // activity was the source.
4418 final ActivityStack focusedStack = getFocusedStack();
4419 final ActivityRecord sourceRecord =
4420 focusedStack != null ? focusedStack.topActivity() : null;
4421
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004422 if (launchStackId != INVALID_STACK_ID) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004423 if (task.stack.mStackId != launchStackId) {
4424 moveTaskToStackLocked(
4425 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4426 ANIMATE);
4427 }
4428 }
4429
4430 // If the user must confirm credentials (e.g. when first launching a work app and the
4431 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4432 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4433 && task.getRootActivity() != null) {
Jorim Jaggi09c49542016-04-09 01:39:40 -07004434 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004435 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004436 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4437 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004438
4439 // If we are launching the task in the docked stack, put it into resizing mode so
4440 // the window renders full-screen with the background filling the void. Also only
4441 // call this at the end to make sure that tasks exists on the window manager side.
4442 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004443 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004444 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004445
4446 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4447 ActivityManager.START_TASK_TO_FRONT,
4448 sourceRecord != null ? sourceRecord.task.stack.mStackId : INVALID_STACK_ID,
4449 sourceRecord, task.stack);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004450 return ActivityManager.START_TASK_TO_FRONT;
4451 }
4452 callingUid = task.mCallingUid;
4453 callingPackage = task.mCallingPackage;
4454 intent = task.intent;
4455 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4456 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004457 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4458 null, null, 0, 0, bOptions, userId, null, task);
4459 if (launchStackId == DOCKED_STACK_ID) {
4460 setResizingDuringAnimation(task.taskId);
4461 }
4462 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004463 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004464
4465 /**
4466 * @return a list of activities which are the top ones in each visible stack. The first
4467 * entry will be the focused activity.
4468 */
4469 public List<IBinder> getTopVisibleActivities() {
4470 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4471 if (display == null) {
4472 return Collections.EMPTY_LIST;
4473 }
4474 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4475 final ArrayList<ActivityStack> stacks = display.mStacks;
4476 for (int i = stacks.size() - 1; i >= 0; i--) {
4477 ActivityStack stack = stacks.get(i);
4478 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4479 ActivityRecord top = stack.topActivity();
4480 if (top != null) {
4481 if (stack == mFocusedStack) {
4482 topActivityTokens.add(0, top.appToken);
4483 } else {
4484 topActivityTokens.add(top.appToken);
4485 }
4486 }
4487 }
4488 }
4489 return topActivityTokens;
4490 }
Craig Mautner27084302013-03-25 08:05:25 -07004491}