blob: ca36908e0eaaefee4dd0747842b972a72815cf99 [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;
Jorim Jaggife89d122015-12-22 16:28:44 +0100126import static android.content.pm.PackageManager.PERMISSION_GRANTED;
127import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
128import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
129import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700130import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100131import 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;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700143import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100144import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
145import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
146import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
147import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
148import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
149import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
150import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
151import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
152import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
153import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
154import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
155import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale480dca02016-02-06 13:58:29 -0800156import static com.android.server.am.ActivityManagerService.ANIMATE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100157import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggid53f0922016-04-06 22:16:23 -0700158import static com.android.server.am.ActivityManagerService.NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG;
Jorim Jaggi2adba072016-03-03 13:43:39 +0100159import static com.android.server.am.ActivityManagerService.NOTIFY_FORCED_RESIZABLE_MSG;
Jorim Jaggife89d122015-12-22 16:28:44 +0100160import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
161import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
162import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
163import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
164import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
165import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
166import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
167import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
168import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
169import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
170import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700171import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Tony Mak853304c2016-04-18 15:17:41 +0100172import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Winsond46b7272016-04-20 11:54:27 -0700173import static com.android.server.am.ActivityStack.STACK_VISIBLE;
Jorim Jaggife89d122015-12-22 16:28:44 +0100174import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
175import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
176import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
177import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
178import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100179import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Jorim Jaggife89d122015-12-22 16:28:44 +0100180
Andrii Kulian1779e612016-10-12 21:58:25 -0700181public final class ActivityStackSupervisor extends ConfigurationContainer
182 implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800183 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700185 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700187 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700193 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800194 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800196
Craig Mautnerf3333272013-04-22 10:55:53 -0700197 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700199
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700202
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700205
Craig Mautner05d29032013-05-03 13:40:13 -0700206 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
207 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
208 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800211 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
212 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
213 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700214 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400215 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
216 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700217 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700218 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700219 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800220 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
221 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800222
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700224
Jason Monk62515be2014-05-21 16:06:19 -0400225 private static final String LOCK_TASK_TAG = "Lock-to-App";
226
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227 // Used to indicate if an object (e.g. stack) that we are trying to get
228 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800229 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700231 // Used to indicate that windows of activities should be preserved during the resize.
232 static final boolean PRESERVE_WINDOWS = true;
233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 // Used to indicate if an object (e.g. task) should be moved/created
235 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700236 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237
238 // Used to indicate that an objects (e.g. task) removal from its container
239 // (e.g. stack) is due to it moving to another container.
240 static final boolean MOVING = true;
241
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700242 // Force the focus to change to the stack we are moving a task to..
243 static final boolean FORCE_FOCUS = true;
244
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700245 // Restore task from the saved recents if it can't be found in any live stack.
246 static final boolean RESTORE_FROM_RECENTS = true;
247
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700248 // Don't execute any calls to resume.
249 static final boolean DEFER_RESUME = true;
250
Svetoslav7008b512015-06-24 18:47:07 -0700251 // Activity actions an app cannot start if it uses a permission which is not granted.
252 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
253 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700254
Svetoslav7008b512015-06-24 18:47:07 -0700255 static {
256 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
257 Manifest.permission.CAMERA);
258 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
259 Manifest.permission.CAMERA);
260 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
261 Manifest.permission.CALL_PHONE);
262 }
263
Svet Ganov99b60432015-06-27 13:15:22 -0700264 /** Action restriction: launching the activity is not restricted. */
265 private static final int ACTIVITY_RESTRICTION_NONE = 0;
266 /** Action restriction: launching the activity is restricted by a permission. */
267 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
268 /** Action restriction: launching the activity is restricted by an app op. */
269 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700270
justinzhang5286d3f2014-05-12 17:06:01 -0400271 /** Status Bar Service **/
272 private IBinder mToken = new Binder();
273 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400274 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400275
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700276 // For debugging to make sure the caller when acquiring/releasing our
277 // wake lock is the system process.
278 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800279 /** The number of distinct task ids that can be assigned to the tasks of a single user */
280 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner27084302013-03-25 08:05:25 -0700282 final ActivityManagerService mService;
283
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800284 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800285
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700286 final ActivityStackSupervisorHandler mHandler;
287
288 /** Short cut */
289 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800290 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700291
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700292 /** Counter for next free stack ID to use for dynamic activity stacks. */
293 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700294
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800295 /**
296 * Maps the task identifier that activities are currently being started in to the userId of the
297 * task. Each time a new task is created, the entry for the userId of the task is incremented
298 */
299 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Craig Mautner2420ead2013-04-01 17:13:20 -0700301 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800302 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700303
Craig Mautnere0a38842013-12-16 16:14:02 -0800304 /** The stack containing the launcher app. Assumed to always be attached to
305 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800306 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700307
Craig Mautnere0a38842013-12-16 16:14:02 -0800308 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800309 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700310
Craig Mautner4a1cb222013-12-04 16:14:06 -0800311 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
312 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800314 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700315
316 /** List of activities that are waiting for a new activity to become visible before completing
317 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800318 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700319
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800321 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
323 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800324 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700325
Craig Mautnerde4ef022013-04-07 19:01:33 -0700326 /** List of activities that are ready to be stopped, but waiting for the next activity to
327 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800328 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Craig Mautnerf3333272013-04-22 10:55:53 -0700330 /** List of activities that are ready to be finished, but waiting for the previous activity to
331 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800332 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700333
Craig Mautner0eea92c2013-05-16 13:35:39 -0700334 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800335 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700336
Wale Ogunwale22e25262016-02-01 10:32:02 -0800337 /** List of activities whose multi-window mode changed that we need to report to the
338 * application */
339 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
340
341 /** List of activities whose picture-in-picture mode changed that we need to report to the
342 * application */
343 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
344
Craig Mautnerf3333272013-04-22 10:55:53 -0700345 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700346 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700347
Craig Mautnerde4ef022013-04-07 19:01:33 -0700348 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
349 * is being brought in front of us. */
350 boolean mUserLeaving = false;
351
Craig Mautner0eea92c2013-05-16 13:35:39 -0700352 /** Set when we have taken too long waiting to go to sleep. */
353 boolean mSleepTimeout = false;
354
355 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700356 * We don't want to allow the device to go to sleep while in the process
357 * of launching an activity. This is primarily to allow alarm intent
358 * receivers to launch an activity and get that to run before the device
359 * goes back to sleep.
360 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700361 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700362
363 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364 * Set when the system is going to sleep, until we have
365 * successfully paused the current activity and released our wake lock.
366 * At that point the system is allowed to actually sleep.
367 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700368 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700370 /** Stack id of the front stack when user switched, indexed by userId. */
371 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700372
Craig Mautner4504de52013-12-20 09:06:56 -0800373 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700375 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800376
377 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700378 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379
Jeff Brownca9bc702014-02-11 14:32:56 -0800380 InputManagerInternal mInputManagerInternal;
381
Craig Mautner15df08a2015-04-01 12:17:18 -0700382 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
383 * may be finished until there is only one entry left. If this is empty the system is not
384 * in lockTask mode. */
385 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000386 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700387 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
388 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000389 */
390 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400391 /**
392 * Notifies the user when entering/exiting lock-task.
393 */
394 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800395
Wale Ogunwaled046a012015-12-24 13:05:59 -0800396 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800397 boolean inResumeTopActivity;
398
Andrii Kulian1e32e022016-09-16 15:29:34 -0700399 /**
400 * Temporary rect used during docked stack resize calculation so we don't need to create a new
401 * object each time.
402 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700403 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700404
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700405 // The default minimal size that will be used if the activity doesn't specify its minimal size.
406 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700407 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700408
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700409 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
410 private boolean mTaskLayersChanged = true;
411
Jorim Jaggi275561a2016-02-23 10:11:02 -0500412 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800413
Jorim Jaggidc249c42015-12-15 14:57:31 -0800414 private final ResizeDockedStackTimeout mResizeDockedStackTimeout;
415
Andrii Kulian1779e612016-10-12 21:58:25 -0700416 @Override
417 protected int getChildCount() {
418 return mActivityDisplays.size();
419 }
420
421 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700422 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700423 return mActivityDisplays.valueAt(index);
424 }
425
426 @Override
427 protected ConfigurationContainer getParent() {
428 return null;
429 }
430
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700431 Configuration getDisplayOverrideConfiguration(int displayId) {
432 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
433 if (activityDisplay == null) {
434 throw new IllegalArgumentException("No display found with id: " + displayId);
435 }
436
437 return activityDisplay.getOverrideConfiguration();
438 }
439
440 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
441 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
442 if (activityDisplay == null) {
443 throw new IllegalArgumentException("No display found with id: " + displayId);
444 }
445
446 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
447 }
448
Wale Ogunwale39381972015-12-17 17:15:29 -0800449 static class FindTaskResult {
450 ActivityRecord r;
451 boolean matchedByRootAffinity;
452 }
453 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
454
Craig Mautneree36c772014-07-16 14:56:05 -0700455 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100456 * Used to keep track whether app visibilities got changed since the last pause. Useful to
457 * determine whether to invoke the task stack change listener after pausing.
458 */
459 boolean mAppVisibilitiesChangedSinceLastPause;
460
461 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100462 * Set of tasks that are in resizing mode during an app transition to fill the "void".
463 */
464 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
465
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700466
467 /**
468 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
469 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
470 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
471 * like the docked stack going empty.
472 */
473 private boolean mAllowDockedStackResize = true;
474
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100475 /**
Tony Mak853304c2016-04-18 15:17:41 +0100476 * Is dock currently minimized.
477 */
478 boolean mIsDockMinimized;
479
480 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700481 * Description of a request to start a new activity, which has been held
482 * due to app switches being disabled.
483 */
484 static class PendingActivityLaunch {
485 final ActivityRecord r;
486 final ActivityRecord sourceRecord;
487 final int startFlags;
488 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700489 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700490
491 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700492 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700493 r = _r;
494 sourceRecord = _sourceRecord;
495 startFlags = _startFlags;
496 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700497 callerApp = _callerApp;
498 }
499
500 void sendErrorResult(String message) {
501 try {
502 if (callerApp.thread != null) {
503 callerApp.thread.scheduleCrash(message);
504 }
505 } catch (RemoteException e) {
506 Slog.e(TAG, "Exception scheduling crash of failed "
507 + "activity launcher sourceRecord=" + sourceRecord, e);
508 }
Craig Mautneree36c772014-07-16 14:56:05 -0700509 }
510 }
511
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800512 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700513 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700514 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800515 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800516 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700517 }
518
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800519 void setRecentTasks(RecentTasks recentTasks) {
520 mRecentTasks = recentTasks;
521 }
522
Jeff Brown2c43c332014-06-12 22:38:59 -0700523 /**
524 * At the time when the constructor runs, the power manager has not yet been
525 * initialized. So we initialize our wakelocks afterwards.
526 */
527 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800528 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700529 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700530 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700531 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700532 }
533
justinzhang5286d3f2014-05-12 17:06:01 -0400534 // This function returns a IStatusBarService. The value is from ServiceManager.
535 // getService and is cached.
536 private IStatusBarService getStatusBarService() {
537 synchronized (mService) {
538 if (mStatusBarService == null) {
539 mStatusBarService = IStatusBarService.Stub.asInterface(
540 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
541 if (mStatusBarService == null) {
542 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
543 }
544 }
545 return mStatusBarService;
546 }
547 }
548
Jason Monk35c62a42014-06-17 10:24:47 -0400549 private IDevicePolicyManager getDevicePolicyManager() {
550 synchronized (mService) {
551 if (mDevicePolicyManager == null) {
552 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
553 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
554 if (mDevicePolicyManager == null) {
555 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
556 }
557 }
558 return mDevicePolicyManager;
559 }
560 }
561
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700562 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800563 synchronized (mService) {
564 mWindowManager = wm;
565
566 mDisplayManager =
567 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
568 mDisplayManager.registerDisplayListener(this, null);
569
570 Display[] displays = mDisplayManager.getDisplays();
571 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
572 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800573 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700574 if (activityDisplay.mDisplay == null) {
575 throw new IllegalStateException("Default Display does not exist");
576 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800577 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700578 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800579 }
580
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800581 mHomeStack = mFocusedStack = mLastFocusedStack =
582 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800583
584 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585 }
Craig Mautner27084302013-03-25 08:05:25 -0700586 }
587
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200588 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200590 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700591 }
592
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700593 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700595 }
596
Craig Mautnerde4ef022013-04-07 19:01:33 -0700597 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700599 }
600
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700601 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700602 if (stack == null) {
603 return false;
604 }
605
Craig Mautnerdf88d732014-01-27 09:21:32 -0800606 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
607 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700608 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800609 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800610 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700611 }
612
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700613 /** The top most stack. */
614 boolean isFrontStack(ActivityStack stack) {
615 if (stack == null) {
616 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700618
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700619 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
620 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700621 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800622 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700623 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
624 }
625
Wale Ogunwaled046a012015-12-24 13:05:59 -0800626 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800627 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
628 if (!focusCandidate.isFocusable()) {
629 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
630 focusCandidate = focusCandidate.getNextFocusableStackLocked();
631 }
632
633 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800634 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800635 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800636
Wale Ogunwaled046a012015-12-24 13:05:59 -0800637 EventLogTags.writeAmFocusedStack(
638 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
639 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
640 }
641
642 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800643 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800644 if (r != null && r.idle) {
645 checkFinishBootingLocked();
646 }
647 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700648 }
649
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700650 void moveHomeStackToFront(String reason) {
651 mHomeStack.moveToFront(reason);
652 }
653
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700654 /** Returns true if the focus activity was adjusted to the home stack top activity. */
655 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
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 */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700658 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700659 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700660
Craig Mautner84984fa2014-06-19 11:19:20 -0700661 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700662
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700663 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700664 if (top == null) {
665 return false;
666 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700667 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700668 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700669 }
670
Craig Mautner299f9602015-01-26 09:47:33 -0800671 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700672 if (!mService.mBooting && !mService.mBooted) {
673 // Not ready yet!
674 return false;
675 }
676
Craig Mautner84984fa2014-06-19 11:19:20 -0700677 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700678 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700679 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700680 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681
Craig Mautner84984fa2014-06-19 11:19:20 -0700682 if (prev != null) {
683 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
684 }
685
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700686 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
687 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800688 final String myReason = reason + " resumeHomeStackTask";
689
Mark Lua56ea122015-10-08 13:31:01 +0800690 // Only resume home activity if isn't finishing.
691 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700692 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800693 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700694 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800695 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700696 }
697
Craig Mautner8d341ef2013-03-26 09:03:27 -0700698 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700699 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700700 }
701
702 /**
703 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
704 * @param id Id of the task we would like returned.
705 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
706 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700707 * @param stackId The stack to restore the task to (default launch stack will be used if
708 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700709 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700710 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800711 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
715 ActivityStack stack = stacks.get(stackNdx);
716 TaskRecord task = stack.taskForIdLocked(id);
717 if (task != null) {
718 return task;
719 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700720 }
721 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800722
723 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700724 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800725 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800726 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700727 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800728 return null;
729 }
730
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700731 if (!restoreFromRecents) {
732 return task;
733 }
734
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700735 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700736 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
737 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800738 return null;
739 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700740 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800741 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700742 }
743
Craig Mautner6170f732013-04-02 13:05:23 -0700744 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800745 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
749 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
750 if (r != null) {
751 return r;
752 }
Craig Mautner6170f732013-04-02 13:05:23 -0700753 }
754 }
755 return null;
756 }
757
Tony Mak853304c2016-04-18 15:17:41 +0100758 /**
759 * TODO: Handle freefom mode.
760 * @return true when credential confirmation is needed for the user and there is any
761 * activity started by the user in any visible stack.
762 */
763 boolean isUserLockedProfile(@UserIdInt int userId) {
764 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
765 return false;
766 }
767 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
768 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
769 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
770 for (final ActivityStack activityStack : activityStacks) {
771 if (activityStack == null) {
772 continue;
773 }
774 if (activityStack.topRunningActivityLocked() == null) {
775 continue;
776 }
777 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
778 continue;
779 }
780 if (activityStack.isDockedStack() && mIsDockMinimized) {
781 continue;
782 }
783 final TaskRecord topTask = activityStack.topTask();
784 if (topTask == null) {
785 continue;
786 }
787 // To handle the case that work app is in the task but just is not the top one.
788 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
789 final ActivityRecord activityRecord = topTask.mActivities.get(i);
790 if (activityRecord.userId == userId) {
791 return true;
792 }
793 }
794 }
795 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000796 }
797
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800798 void setNextTaskIdForUserLocked(int taskId, int userId) {
799 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
800 if (taskId > currentTaskId) {
801 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700802 }
803 }
804
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700805 static int nextTaskIdForUser(int taskId, int userId) {
806 int nextTaskId = taskId + 1;
807 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
808 // Wrap around as there will be smaller task ids that are available now.
809 nextTaskId -= MAX_TASK_IDS_PER_USER;
810 }
811 return nextTaskId;
812 }
813
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800814 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800815 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
816 // for a userId u, a taskId can only be in the range
817 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
818 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700819 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800820 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
821 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
822 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700823 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800824 if (candidateTaskId == currentTaskId) {
825 // Something wrong!
826 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
827 throw new IllegalStateException("Cannot get an available task id."
828 + " Reached limit of " + MAX_TASK_IDS_PER_USER
829 + " running tasks per user.");
830 }
831 }
832 mCurTaskIdForUser.put(userId, candidateTaskId);
833 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700834 }
835
Chong Zhang6cda19c2016-06-14 19:07:56 -0700836 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800837 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700838 if (stack == null) {
839 return null;
840 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700841 ActivityRecord resumedActivity = stack.mResumedActivity;
842 if (resumedActivity == null || resumedActivity.app == null) {
843 resumedActivity = stack.mPausingActivity;
844 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700845 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700846 }
847 }
848 return resumedActivity;
849 }
850
Dianne Hackbornff072722014-09-24 10:56:28 -0700851 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700852 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800854 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
855 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
857 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700858 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 continue;
860 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700861 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 if (hr != null) {
863 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
864 && processName.equals(hr.processName)) {
865 try {
George Mount2c92c972014-03-20 09:38:23 -0700866 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 didSomething = true;
868 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700869 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 Slog.w(TAG, "Exception in new application when starting activity "
871 + hr.intent.getComponent().flattenToShortString(), e);
872 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700873 }
Craig Mautner20e72272013-04-01 13:45:53 -0700874 }
Craig Mautner20e72272013-04-01 13:45:53 -0700875 }
876 }
877 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700878 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700879 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700880 }
Craig Mautner20e72272013-04-01 13:45:53 -0700881 return didSomething;
882 }
883
884 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
886 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
888 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700889 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 continue;
891 }
892 final ActivityRecord resumedActivity = stack.mResumedActivity;
893 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700894 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800895 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800896 return false;
897 }
Craig Mautner20e72272013-04-01 13:45:53 -0700898 }
899 }
Wei Wang65c7a152016-06-02 18:51:22 -0700900 // Send launch end powerhint when idle
901 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700902 return true;
903 }
904
Craig Mautnerde4ef022013-04-07 19:01:33 -0700905 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800906 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
907 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
909 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700910 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700912 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 return false;
914 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700915 }
916 }
917 }
918 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700919 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
921 mLastFocusedStack + " to=" + mFocusedStack);
922 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700923 return true;
924 }
925
926 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800927 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800928 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
929 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
931 final ActivityStack stack = stacks.get(stackNdx);
932 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800933 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700934 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800935 return false;
936 }
937 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700939 }
940 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800941 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700942 }
943
Craig Mautner2acc3892013-09-23 10:28:14 -0700944 /**
945 * Pause all activities in either all of the stacks or just the back stacks.
946 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700947 * @param resuming The resuming activity.
948 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
949 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700950 * @return true if any activity was paused as a result of this call.
951 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700952 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700953 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
955 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
957 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700958 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700959 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700961 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
962 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700964 }
965 }
966 return someActivityPaused;
967 }
968
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700970 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800971 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
972 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
974 final ActivityStack stack = stacks.get(stackNdx);
975 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700976 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800977 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700978 Slog.d(TAG_STATES,
979 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 pausing = false;
981 } else {
982 return false;
983 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700984 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 }
986 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700987 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 }
989
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700990 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700991 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500992 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800993 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
994 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
996 final ActivityStack stack = stacks.get(stackNdx);
997 if (stack.mResumedActivity != null &&
998 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001000 }
1001 }
1002 }
1003 }
1004
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001005 void cancelInitializingActivities() {
1006 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1007 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1008 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1009 stacks.get(stackNdx).cancelInitializingActivities();
1010 }
1011 }
1012 }
1013
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001014 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001015 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001016 }
1017
1018 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1019 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001020 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001021 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001022 if (w.who == null) {
1023 changed = true;
1024 w.timeout = false;
1025 if (r != null) {
1026 w.who = new ComponentName(r.info.packageName, r.info.name);
1027 }
1028 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1029 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001030 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001031 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001032 if (changed) {
1033 mService.notifyAll();
1034 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001035 }
1036
Chong Zhang5022da32016-06-21 16:31:37 -07001037 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1038 boolean changed = false;
1039 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1040 WaitResult w = mWaitingActivityLaunched.remove(i);
1041 if (w.who == null) {
1042 changed = true;
1043 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1044 // the starting activity ends up moving another activity to front, and it should
1045 // wait for this new activity to become visible instead.
1046 // Do not modify other fields.
1047 w.result = START_TASK_TO_FRONT;
1048 }
1049 }
1050 if (changed) {
1051 mService.notifyAll();
1052 }
1053 }
1054
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001055 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1056 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001057 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001058 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001059 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001060 if (w.who == null) {
1061 changed = true;
1062 w.timeout = timeout;
1063 if (r != null) {
1064 w.who = new ComponentName(r.info.packageName, r.info.name);
1065 }
1066 w.thisTime = thisTime;
1067 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001068 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001069 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001070 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001071 if (changed) {
1072 mService.notifyAll();
1073 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 }
1075
Craig Mautner29219d92013-04-16 20:19:12 -07001076 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001077 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001078 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001079 if (r != null) {
1080 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001081 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001082
Andrii Kulian7318d632016-07-20 18:59:28 -07001083 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001084 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001085 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1086 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001087 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001088 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001089 if (r != null) {
1090 return r;
1091 }
1092 }
1093 }
1094 return null;
1095 }
1096
Dianne Hackborn09233282014-04-30 11:33:59 -07001097 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001098 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1100 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001101 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001102 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001103 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1105 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001106 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001107 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001108 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001109 }
1110 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001111
1112 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1113 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1114 while (maxNum > 0) {
1115 long mostRecentActiveTime = Long.MIN_VALUE;
1116 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001117 final int numTaskLists = runningTaskLists.size();
1118 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1119 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001120 if (!stackTaskList.isEmpty()) {
1121 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1122 if (lastActiveTime > mostRecentActiveTime) {
1123 mostRecentActiveTime = lastActiveTime;
1124 selectedStackList = stackTaskList;
1125 }
1126 }
1127 }
1128 if (selectedStackList != null) {
1129 list.add(selectedStackList.remove(0));
1130 --maxNum;
1131 } else {
1132 break;
1133 }
1134 }
Craig Mautner20e72272013-04-01 13:45:53 -07001135 }
1136
Todd Kennedy7440f172015-12-09 14:31:22 -08001137 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1138 ProfilerInfo profilerInfo) {
1139 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001140 if (aInfo != null) {
1141 // Store the found target back into the intent, because now that
1142 // we have it we never want to do this again. For example, if the
1143 // user navigates back to this point in the history, we should
1144 // always restart the exact same activity.
1145 intent.setComponent(new ComponentName(
1146 aInfo.applicationInfo.packageName, aInfo.name));
1147
1148 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001149 if (!aInfo.processName.equals("system")) {
1150 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001151 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001153
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001154 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1155 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1156 }
1157
Man Caocfa78b22015-06-11 20:14:34 -07001158 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1159 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1160 }
1161
1162 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001163 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001164 }
1165 }
1166 }
1167 return aInfo;
1168 }
1169
Todd Kennedy7440f172015-12-09 14:31:22 -08001170 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001171 return resolveIntent(intent, resolvedType, userId, 0);
1172 }
1173
1174 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001175 try {
1176 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001177 PackageManager.MATCH_DEFAULT_ONLY | flags
1178 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001179 } catch (RemoteException e) {
1180 }
1181 return null;
1182 }
1183
1184 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1185 ProfilerInfo profilerInfo, int userId) {
1186 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1187 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1188 }
1189
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001190 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1191 boolean andResume, boolean checkConfig) throws RemoteException {
1192
1193 if (!allPausedActivitiesComplete()) {
1194 // While there are activities pausing we skipping starting any new activities until
1195 // pauses are complete. NOTE: that we also do this for activities that are starting in
1196 // the paused state because they will first be resumed then paused on the client side.
1197 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1198 "realStartActivityLocked: Skipping start of r=" + r
1199 + " some activities pausing...");
1200 return false;
1201 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001202
Craig Mautner2568c3a2015-03-26 14:22:34 -07001203 if (andResume) {
1204 r.startFreezingScreenLocked(app, 0);
1205 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001206
Craig Mautner2568c3a2015-03-26 14:22:34 -07001207 // schedule launch ticks to collect information about slow apps.
1208 r.startLaunchTickingLocked();
1209 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001210
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001211 // Have the window manager re-evaluate the orientation of the screen based on the new
1212 // activity order. Note that as a result of this, it can call back into the activity
1213 // manager with a new orientation. We don't care about that, because the activity is not
1214 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001215 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001216 final int displayId = r.getDisplayId();
1217 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1218 getDisplayOverrideConfiguration(displayId),
1219 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001220 // Deferring resume here because we're going to launch new activity shortly.
1221 // We don't want to perform a redundant launch of the same record while ensuring
1222 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001223 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1224 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 }
1226
1227 r.app = app;
1228 app.waitingToKill = null;
1229 r.launchCount++;
1230 r.lastLaunchTime = SystemClock.uptimeMillis();
1231
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001232 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001233
1234 int idx = app.activities.indexOf(r);
1235 if (idx < 0) {
1236 app.activities.add(r);
1237 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001238 mService.updateLruProcessLocked(app, true, null);
1239 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001240
Craig Mautner15df08a2015-04-01 12:17:18 -07001241 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001242 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1243 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001244 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001245 }
1246
Andrii Kulian02b7a832016-10-06 23:11:56 -07001247 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 try {
1249 if (app.thread == null) {
1250 throw new RemoteException();
1251 }
1252 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001253 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 if (andResume) {
1255 results = r.results;
1256 newIntents = r.newIntents;
1257 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001258 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1259 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1260 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001261 if (andResume) {
1262 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1263 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001264 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001265 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001266 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001267 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001268 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001270 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1271 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 r.sleeping = false;
1273 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001274 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 mService.showAskCompatModeDialogLocked(r);
1276 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001277 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1279 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1280 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001281 final String profileFile = mService.mProfileFile;
1282 if (profileFile != null) {
1283 ParcelFileDescriptor profileFd = mService.mProfileFd;
1284 if (profileFd != null) {
1285 try {
1286 profileFd = profileFd.dup();
1287 } catch (IOException e) {
1288 if (profileFd != null) {
1289 try {
1290 profileFd.close();
1291 } catch (IOException o) {
1292 }
1293 profileFd = null;
1294 }
1295 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001297
1298 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1299 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 }
1301 }
1302 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001303
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 if (andResume) {
1305 app.hasShownUi = true;
1306 app.pendingUiClean = true;
1307 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001308 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001309 // Because we could be starting an Activity in the system process this may not go across
1310 // a Binder interface which would create a new Configuration. Consequently we have to
1311 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001312 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001313 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001314 new Configuration(mService.getGlobalConfiguration()),
1315 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1316 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1317 r.persistentState, results, newIntents, !andResume,
1318 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001319
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001320 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001321 // This may be a heavy-weight process! Note that the package
1322 // manager will ensure that only activity can run in the main
1323 // process of the .apk, which is the only thing that will be
1324 // considered heavy-weight.
1325 if (app.processName.equals(app.info.packageName)) {
1326 if (mService.mHeavyWeightProcess != null
1327 && mService.mHeavyWeightProcess != app) {
1328 Slog.w(TAG, "Starting new heavy weight process " + app
1329 + " when already running "
1330 + mService.mHeavyWeightProcess);
1331 }
1332 mService.mHeavyWeightProcess = app;
1333 Message msg = mService.mHandler.obtainMessage(
1334 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1335 msg.obj = r;
1336 mService.mHandler.sendMessage(msg);
1337 }
1338 }
1339
1340 } catch (RemoteException e) {
1341 if (r.launchFailed) {
1342 // This is the second time we failed -- finish activity
1343 // and give up.
1344 Slog.e(TAG, "Second failure launching "
1345 + r.intent.getComponent().flattenToShortString()
1346 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001347 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001348 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1349 "2nd-crash", false);
1350 return false;
1351 }
1352
1353 // This is the first time we failed -- restart process and
1354 // retry.
1355 app.activities.remove(r);
1356 throw e;
1357 }
1358
1359 r.launchFailed = false;
1360 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001361 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001362 }
1363
1364 if (andResume) {
1365 // As part of the process of launching, ActivityThread also performs
1366 // a resume.
1367 stack.minimalResumeActivityLocked(r);
1368 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001369 // This activity is not starting in the resumed state... which should look like we asked
1370 // it to pause+stop (but remain visible), and it has done so and reported back the
1371 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001372 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001373 "Moving to PAUSED: " + r + " (starting in paused state)");
1374 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 }
1376
1377 // Launch the new version setup screen if needed. We do this -after-
1378 // launching the initial activity (that is, home), so that it can have
1379 // a chance to initialize itself while in the background, making the
1380 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001381 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 mService.startSetupActivityLocked();
1383 }
1384
Dianne Hackborn465fa392014-09-14 14:21:18 -07001385 // Update any services we are bound to that might care about whether
1386 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001387 if (r.app != null) {
1388 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1389 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001390
Craig Mautner2420ead2013-04-01 17:13:20 -07001391 return true;
1392 }
1393
Craig Mautnere79d42682013-04-01 19:01:53 -07001394 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001395 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001396 // Is this activity's application already running?
1397 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001398 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001399
Andrii Kulian02b7a832016-10-06 23:11:56 -07001400 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001401
1402 if (app != null && app.thread != null) {
1403 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001404 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1405 || !"android".equals(r.info.packageName)) {
1406 // Don't add this if it is a platform component that is marked
1407 // to run in multiple processes, because this is actually
1408 // part of the framework so doesn't make sense to track as a
1409 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001410 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1411 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001412 }
George Mount2c92c972014-03-20 09:38:23 -07001413 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001414 return;
1415 } catch (RemoteException e) {
1416 Slog.w(TAG, "Exception when starting activity "
1417 + r.intent.getComponent().flattenToShortString(), e);
1418 }
1419
1420 // If a dead object exception was thrown -- fall through to
1421 // restart the application.
1422 }
1423
1424 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001425 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001426 }
1427
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001428 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001429 String resultWho, int requestCode, int callingPid, int callingUid,
1430 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001431 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001432 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1433 callingUid);
1434 if (startAnyPerm == PERMISSION_GRANTED) {
1435 return true;
1436 }
1437 final int componentRestriction = getComponentRestrictionForCallingPackage(
1438 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1439 final int actionRestriction = getActionRestrictionForCallingPackage(
1440 intent.getAction(), callingPackage, callingPid, callingUid);
1441 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1442 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1443 if (resultRecord != null) {
1444 resultStack.sendActivityResultLocked(-1,
1445 resultRecord, resultWho, requestCode,
1446 Activity.RESULT_CANCELED, null);
1447 }
1448 final String msg;
1449 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1450 msg = "Permission Denial: starting " + intent.toString()
1451 + " from " + callerApp + " (pid=" + callingPid
1452 + ", uid=" + callingUid + ")" + " with revoked permission "
1453 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1454 } else if (!aInfo.exported) {
1455 msg = "Permission Denial: starting " + intent.toString()
1456 + " from " + callerApp + " (pid=" + callingPid
1457 + ", uid=" + callingUid + ")"
1458 + " not exported from uid " + aInfo.applicationInfo.uid;
1459 } else {
1460 msg = "Permission Denial: starting " + intent.toString()
1461 + " from " + callerApp + " (pid=" + callingPid
1462 + ", uid=" + callingUid + ")"
1463 + " requires " + aInfo.permission;
1464 }
1465 Slog.w(TAG, msg);
1466 throw new SecurityException(msg);
1467 }
1468
1469 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1470 final String message = "Appop Denial: starting " + intent.toString()
1471 + " from " + callerApp + " (pid=" + callingPid
1472 + ", uid=" + callingUid + ")"
1473 + " requires " + AppOpsManager.permissionToOp(
1474 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1475 Slog.w(TAG, message);
1476 return false;
1477 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1478 final String message = "Appop Denial: starting " + intent.toString()
1479 + " from " + callerApp + " (pid=" + callingPid
1480 + ", uid=" + callingUid + ")"
1481 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1482 Slog.w(TAG, message);
1483 return false;
1484 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001485 if (options != null && options.getLaunchTaskId() != -1) {
1486 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1487 callingPid, callingUid);
1488 if (startInTaskPerm != PERMISSION_GRANTED) {
1489 final String msg = "Permission Denial: starting " + intent.toString()
1490 + " from " + callerApp + " (pid=" + callingPid
1491 + ", uid=" + callingUid + ") with launchTaskId="
1492 + options.getLaunchTaskId();
1493 Slog.w(TAG, msg);
1494 throw new SecurityException(msg);
1495 }
1496 }
1497
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001498 return true;
1499 }
1500
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001501 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001502 final long identity = Binder.clearCallingIdentity();
1503 try {
1504 return UserManager.get(mService.mContext).getUserInfo(userId);
1505 } finally {
1506 Binder.restoreCallingIdentity(identity);
1507 }
1508 }
1509
Svet Ganov99b60432015-06-27 13:15:22 -07001510 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001511 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001512 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1513 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001514 == PackageManager.PERMISSION_DENIED) {
1515 return ACTIVITY_RESTRICTION_PERMISSION;
1516 }
1517
Christopher Tateff7add02015-08-17 10:23:22 -07001518 if (activityInfo.permission == null) {
1519 return ACTIVITY_RESTRICTION_NONE;
1520 }
1521
Svet Ganov99b60432015-06-27 13:15:22 -07001522 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1523 if (opCode == AppOpsManager.OP_NONE) {
1524 return ACTIVITY_RESTRICTION_NONE;
1525 }
1526
1527 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1528 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001529 if (!ignoreTargetSecurity) {
1530 return ACTIVITY_RESTRICTION_APPOP;
1531 }
Svet Ganov99b60432015-06-27 13:15:22 -07001532 }
1533
1534 return ACTIVITY_RESTRICTION_NONE;
1535 }
1536
Svetoslav7008b512015-06-24 18:47:07 -07001537 private int getActionRestrictionForCallingPackage(String action,
1538 String callingPackage, int callingPid, int callingUid) {
1539 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001540 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001541 }
1542
1543 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1544 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001545 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001546 }
1547
1548 final PackageInfo packageInfo;
1549 try {
1550 packageInfo = mService.mContext.getPackageManager()
1551 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1552 } catch (PackageManager.NameNotFoundException e) {
1553 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001554 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001555 }
1556
1557 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001558 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001559 }
1560
1561 if (mService.checkPermission(permission, callingPid, callingUid) ==
1562 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001563 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001564 }
1565
1566 final int opCode = AppOpsManager.permissionToOpCode(permission);
1567 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001568 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001569 }
1570
1571 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1572 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001573 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001574 }
1575
Svet Ganov99b60432015-06-27 13:15:22 -07001576 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001577 }
1578
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001579 void setLaunchSource(int uid) {
1580 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1581 }
1582
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001583 void acquireLaunchWakelock() {
1584 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1585 throw new IllegalStateException("Calling must be system uid");
1586 }
1587 mLaunchingActivity.acquire();
1588 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1589 // To be safe, don't allow the wake lock to be held for too long.
1590 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1591 }
1592 }
1593
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001594 /**
1595 * Called when the frontmost task is idle.
1596 * @return the state of mService.mBooting before this was called.
1597 */
1598 private boolean checkFinishBootingLocked() {
1599 final boolean booting = mService.mBooting;
1600 boolean enableScreen = false;
1601 mService.mBooting = false;
1602 if (!mService.mBooted) {
1603 mService.mBooted = true;
1604 enableScreen = true;
1605 }
1606 if (booting || enableScreen) {
1607 mService.postFinishBooting(booting, enableScreen);
1608 }
1609 return booting;
1610 }
1611
Craig Mautnerf3333272013-04-22 10:55:53 -07001612 // Checked.
1613 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1614 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001615 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001616
Craig Mautnerf3333272013-04-22 10:55:53 -07001617 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001618 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001619 int NS = 0;
1620 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001621 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001622 boolean activityRemoved = false;
1623
Wale Ogunwale7d701172015-03-11 15:36:30 -07001624 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001625 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001626 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1627 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001628 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1629 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001630 if (fromTimeout) {
1631 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001632 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001633
1634 // This is a hack to semi-deal with a race condition
1635 // in the client where it can be constructed with a
1636 // newer configuration from when we asked it to launch.
1637 // We'll update with whatever configuration it now says
1638 // it used to launch.
1639 if (config != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001640 r.setLastReportedConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001641 }
1642
1643 // We are now idle. If someone is waiting for a thumbnail from
1644 // us, we can now deliver.
1645 r.idle = true;
1646
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001647 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001648 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001649 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001650 }
1651 }
1652
1653 if (allResumedActivitiesIdle()) {
1654 if (r != null) {
1655 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001656 }
1657
1658 if (mLaunchingActivity.isHeld()) {
1659 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1660 if (VALIDATE_WAKE_LOCK_CALLER &&
1661 Binder.getCallingUid() != Process.myUid()) {
1662 throw new IllegalStateException("Calling must be system uid");
1663 }
1664 mLaunchingActivity.release();
1665 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001666 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001667 }
1668
1669 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001670 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001671 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001672 if ((NF = mFinishingActivities.size()) > 0) {
1673 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001674 mFinishingActivities.clear();
1675 }
1676
Craig Mautnerf3333272013-04-22 10:55:53 -07001677 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001678 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001679 mStartingUsers.clear();
1680 }
1681
Craig Mautnerf3333272013-04-22 10:55:53 -07001682 // Stop any activities that are scheduled to do so but have been
1683 // waiting for the next one to start.
1684 for (int i = 0; i < NS; i++) {
1685 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001686 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001687 if (stack != null) {
1688 if (r.finishing) {
1689 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1690 } else {
1691 stack.stopActivityLocked(r);
1692 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001693 }
1694 }
1695
1696 // Finish any activities that are scheduled to do so but have been
1697 // waiting for the next one to start.
1698 for (int i = 0; i < NF; i++) {
1699 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001700 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001701 if (stack != null) {
1702 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1703 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001704 }
1705
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001706 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001707 // Complete user switch
1708 if (startingUsers != null) {
1709 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001710 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001711 }
1712 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001713 }
1714
1715 mService.trimApplications();
1716 //dump();
1717 //mWindowManager.dump();
1718
Craig Mautnerf3333272013-04-22 10:55:53 -07001719 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001720 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001721 }
1722
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001723 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001724 }
1725
Craig Mautner8e569572013-10-11 17:36:59 -07001726 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001727 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1729 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1731 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1732 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001733 }
Craig Mautner19091252013-10-05 00:03:53 -07001734 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001735 }
1736
1737 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001738 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1739 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001740 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1741 stacks.get(stackNdx).closeSystemDialogsLocked();
1742 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001743 }
1744 }
1745
Craig Mautner93529a42013-10-04 15:03:13 -07001746 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001747 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001748 }
1749
Craig Mautner8d341ef2013-03-26 09:03:27 -07001750 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001751 * Update the last used stack id for non-current user (current user's last
1752 * used stack is the focused stack)
1753 */
1754 void updateUserStackLocked(int userId, ActivityStack stack) {
1755 if (userId != mCurrentUser) {
1756 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1757 }
1758 }
1759
1760 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001761 * @return true if some activity was finished (or would have finished if doit were true).
1762 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001763 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1764 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001765 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001766 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1767 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001768 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001769 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001770 if (stack.finishDisabledPackageActivitiesLocked(
1771 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001772 didSomething = true;
1773 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001774 }
1775 }
1776 return didSomething;
1777 }
1778
Dianne Hackborna413dc02013-07-12 12:02:55 -07001779 void updatePreviousProcessLocked(ActivityRecord r) {
1780 // Now that this process has stopped, we may want to consider
1781 // it to be the previous app to try to keep around in case
1782 // the user wants to return to it.
1783
1784 // First, found out what is currently the foreground app, so that
1785 // we don't blow away the previous app if this activity is being
1786 // hosted by the process that is actually still the foreground.
1787 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001788 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1789 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1791 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001792 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001793 if (stack.mResumedActivity != null) {
1794 fgApp = stack.mResumedActivity.app;
1795 } else if (stack.mPausingActivity != null) {
1796 fgApp = stack.mPausingActivity.app;
1797 }
1798 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001799 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001800 }
1801 }
1802
1803 // Now set this one as the previous process, only if that really
1804 // makes sense to.
1805 if (r.app != null && fgApp != null && r.app != fgApp
1806 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001807 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001808 mService.mPreviousProcess = r.app;
1809 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1810 }
1811 }
1812
Wale Ogunwaled046a012015-12-24 13:05:59 -08001813 boolean resumeFocusedStackTopActivityLocked() {
1814 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001815 }
1816
Wale Ogunwaled046a012015-12-24 13:05:59 -08001817 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001818 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001819 if (targetStack != null && isFocusedStack(targetStack)) {
1820 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001821 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001822 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1823 if (r == null || r.state != RESUMED) {
1824 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1825 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001826 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001827 }
1828
Todd Kennedy39bfee52016-02-24 10:28:21 -08001829 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1831 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1832 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1833 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1834 }
1835 }
1836 }
1837
Adrian Roos20d7df32016-01-12 18:59:43 +01001838 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1839 TaskRecord finishedTask = null;
1840 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001843 final int numStacks = stacks.size();
1844 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1845 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001846 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1847 if (stack == focusedStack || finishedTask == null) {
1848 finishedTask = t;
1849 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001850 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001851 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001852 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001853 }
1854
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001855 void finishVoiceTask(IVoiceInteractionSession session) {
1856 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1857 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1858 final int numStacks = stacks.size();
1859 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1860 final ActivityStack stack = stacks.get(stackNdx);
1861 stack.finishVoiceTask(session);
1862 }
1863 }
1864 }
1865
Andrii Kulianc27916642016-04-12 17:59:27 -07001866 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1867 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001868 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1869 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001870 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001871 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1872 // Caller wants the home activity moved with it. To accomplish this,
1873 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001874 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001875 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001876 ActivityStack currentStack = task.getStack();
1877 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001878 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1879 + task + " to front. Stack is null");
1880 return;
1881 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001882
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001883 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001884 int stackId = options.getLaunchStackId();
1885 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001886 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001887 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001888 if (stackId == INVALID_STACK_ID) {
1889 stackId = task.getLaunchStackId();
1890 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001891 if (stackId != currentStack.mStackId) {
1892 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1893 !FORCE_FOCUS, reason);
1894 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001895 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1896 // still need moveTaskToFrontLocked() below for any transition settings.
1897 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001898 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1899 resizeStackLocked(stackId, bounds,
1900 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001901 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001902 } else {
1903 // WM resizeTask must be done after the task is moved to the correct stack,
1904 // because Task's setBounds() also updates dim layer's bounds, but that has
1905 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001906 mWindowManager.resizeTask(task.taskId, task.mBounds,
1907 task.getOverrideConfiguration(), false /* relayout */,
1908 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001909 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001910 }
1911 }
1912
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001913 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001914 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001915 r == null ? null : r.appTimeTracker, reason);
1916
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001917 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001918 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001919
Andrii Kulian02b7a832016-10-06 23:11:56 -07001920 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001921 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001922 }
1923
Wale Ogunwale854809c2015-12-27 16:18:19 -08001924 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001925 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001926 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001927 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001928 return false;
1929 }
1930 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1931 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001932 }
1933
Craig Mautner967212c2013-04-13 21:10:58 -07001934 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001935 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001936 }
1937
1938 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001939 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1940 if (activityContainer != null) {
1941 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001942 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001943 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001944 return null;
1945 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001946 // TODO(multi-display): Allow creating stacks on secondary displays.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001947 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001948 }
1949
Craig Mautner967212c2013-04-13 21:10:58 -07001950 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001951 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1953 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001954 }
1955 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001956 }
1957
Craig Mautneree2e45a2014-06-27 12:10:03 -07001958 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001959 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001960 }
1961
1962 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001963 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1964 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1965 final TaskRecord task = tasks.get(taskNdx);
1966 if (task.isHomeTask()) {
1967 final ArrayList<ActivityRecord> activities = task.mActivities;
1968 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1969 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001970 if (r.isHomeActivity()
1971 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001972 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001973 }
1974 }
1975 }
1976 }
1977 return null;
1978 }
1979
Chong Zhangb15758a2015-11-17 12:12:03 -08001980 /**
1981 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1982 * the docked stack itself, or if it's side-by-side to the docked stack.
1983 */
1984 boolean isStackDockedInEffect(int stackId) {
1985 return stackId == DOCKED_STACK_ID ||
1986 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1987 }
1988
Todd Kennedyca4d8422015-01-15 15:19:22 -08001989 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001990 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001991 ActivityContainer activityContainer =
1992 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001993 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001994 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1995 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001996 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001997 return activityContainer;
1998 }
1999
Craig Mautner34b73df2014-01-12 21:11:08 -08002000 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002001 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2002 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2003 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002004 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2005 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002006 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002007 }
2008 }
2009
Craig Mautner95da1082014-02-24 17:54:35 -08002010 void deleteActivityContainer(IActivityContainer container) {
2011 ActivityContainer activityContainer = (ActivityContainer)container;
2012 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002013 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2014 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002015 final int stackId = activityContainer.mStackId;
2016 mActivityContainers.remove(stackId);
2017 mWindowManager.removeStack(stackId);
2018 }
2019 }
2020
Jorim Jaggidc249c42015-12-15 14:57:31 -08002021 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002022 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002023 if (stackId == DOCKED_STACK_ID) {
2024 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002025 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002026 return;
2027 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002028 final ActivityStack stack = getStack(stackId);
2029 if (stack == null) {
2030 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2031 return;
2032 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002033
Jorim Jaggidc249c42015-12-15 14:57:31 -08002034 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002035 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2036 // stack size changing so things don't get out of sync.
2037 return;
2038 }
2039
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002040 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002041 mWindowManager.deferSurfaceLayout();
2042 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002043 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002044 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002045 stack.ensureVisibleActivitiesConfigurationLocked(
2046 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002047 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002048 } finally {
2049 mWindowManager.continueSurfaceLayout();
2050 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002051 }
2052 }
2053
Jorim Jaggi192086e2016-03-11 17:17:03 +01002054 void deferUpdateBounds(int stackId) {
2055 final ActivityStack stack = getStack(stackId);
2056 if (stack != null) {
2057 stack.deferUpdateBounds();
2058 }
2059 }
2060
2061 void continueUpdateBounds(int stackId) {
2062 final ActivityStack stack = getStack(stackId);
2063 if (stack != null) {
2064 stack.continueUpdateBounds();
2065 }
2066 }
2067
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002068 void notifyAppTransitionDone() {
2069 continueUpdateBounds(HOME_STACK_ID);
2070 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2071 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002072 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002073 mWindowManager.setTaskDockedResizing(taskId, false);
2074 }
2075 }
2076 mResizingTasksDuringAnimation.clear();
2077 }
2078
Jorim Jaggi192086e2016-03-11 17:17:03 +01002079 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002080 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002081 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002082
Jorim Jaggi192086e2016-03-11 17:17:03 +01002083 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2084 return;
2085 }
2086
Andrii Kulian1e32e022016-09-16 15:29:34 -07002087 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002088 }
2089
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002090 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2091 final ActivityStack stack = getStack(fromStackId);
2092 if (stack == null) {
2093 return;
2094 }
2095
2096 mWindowManager.deferSurfaceLayout();
2097 try {
2098 if (fromStackId == DOCKED_STACK_ID) {
2099
2100 // We are moving all tasks from the docked stack to the fullscreen stack,
2101 // which is dismissing the docked stack, so resize all other stacks to
2102 // fullscreen here already so we don't end up with resize trashing.
2103 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2104 if (StackId.isResizeableByDockedStack(i)) {
2105 ActivityStack otherStack = getStack(i);
2106 if (otherStack != null) {
2107 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2108 true /* allowResizeInDockedMode */, DEFER_RESUME);
2109 }
2110 }
2111 }
2112
2113 // Also disable docked stack resizing since we have manually adjusted the
2114 // size of other stacks above and we don't want to trigger a docked stack
2115 // resize when we remove task from it below and it is detached from the
2116 // display because it no longer contains any tasks.
2117 mAllowDockedStackResize = false;
2118 }
2119 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2120 final int size = tasks.size();
2121 if (onTop) {
2122 for (int i = 0; i < size; i++) {
2123 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002124 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002125 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002126 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002127
2128 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2129 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002130 } else {
2131 for (int i = size - 1; i >= 0; i--) {
2132 positionTaskInStackLocked(tasks.get(i).taskId,
2133 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2134 }
2135 }
2136 } finally {
2137 mAllowDockedStackResize = true;
2138 mWindowManager.continueSurfaceLayout();
2139 }
2140 }
2141
Jorim Jaggidc249c42015-12-15 14:57:31 -08002142 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002143 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2144 boolean preserveWindows) {
2145 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2146 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2147 false /* deferResume */);
2148 }
2149
2150 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2151 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2152 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002153
2154 if (!mAllowDockedStackResize) {
2155 // Docked stack resize currently disabled.
2156 return;
2157 }
2158
Jorim Jaggidc249c42015-12-15 14:57:31 -08002159 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2160 if (stack == null) {
2161 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2162 return;
2163 }
2164
2165 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2166 mWindowManager.deferSurfaceLayout();
2167 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002168 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2169 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002170 ActivityRecord r = stack.topRunningActivityLocked();
2171 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2172 tempDockedTaskInsetBounds);
2173
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002174 // TODO: Checking for isAttached might not be needed as if the user passes in null
2175 // dockedBounds then they want the docked stack to be dismissed.
2176 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2177 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 // In this case we make all other static stacks fullscreen and move all
2179 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002180 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002181
2182 // stack shouldn't contain anymore activities, so nothing to resume.
2183 r = null;
2184 } else {
2185 // Docked stacks occupy a dedicated region on screen so the size of all other
2186 // static stacks need to be adjusted so they don't overlap with the docked stack.
2187 // We get the bounds to use from window manager which has been adjusted for any
2188 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002189 mWindowManager.getStackDockedModeBounds(
2190 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002191 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002192 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2193 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2194 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002195 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002196 }
2197 }
2198 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002199 if (!deferResume) {
2200 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2201 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002202 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002203 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002204 mWindowManager.continueSurfaceLayout();
2205 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2206 }
2207
2208 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2209 tempDockedTaskBounds != null
2210 || tempDockedTaskInsetBounds != null
2211 || tempOtherTaskBounds != null
2212 || tempOtherTaskInsetBounds != null);
2213 }
2214
Robert Carr0d00c2e2016-02-29 17:45:02 -08002215 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2216 final ActivityStack stack = getStack(PINNED_STACK_ID);
2217 if (stack == null) {
2218 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2219 return;
2220 }
2221 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2222 mWindowManager.deferSurfaceLayout();
2223 try {
2224 ActivityRecord r = stack.topRunningActivityLocked();
2225 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2226 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002227 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002228 } finally {
2229 mWindowManager.continueSurfaceLayout();
2230 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2231 }
2232 }
2233
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002234 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2235 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002236 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002237 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002238 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002239 }
2240
Chong Zhang87b21722015-09-21 15:39:51 -07002241 // If this is a forced resize, let it go through even if the bounds is not changing,
2242 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002243 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002244 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002245 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002246 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002247 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002248 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002249
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002250 if (!mWindowManager.isValidTaskId(task.taskId)) {
2251 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002252 // All we can do for now is update the bounds so it can be used when the task is
2253 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002254 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002255 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002256 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002257 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002258 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002259 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002260 }
2261
Chong Zhang6de2ae82015-09-30 18:25:21 -07002262 // Do not move the task to another stack here.
2263 // This method assumes that the task is already placed in the right stack.
2264 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002265
Chong Zhang6de2ae82015-09-30 18:25:21 -07002266 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002267
Andrii Kulian8072d112016-09-16 11:11:01 -07002268 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002269 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002270 // way and the activity was kept the way it was. If it's false, it means the activity had
2271 // to be relaunched due to configuration change.
2272 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002273 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002274 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002275 if (r != null) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002276 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002277
2278 if (!deferResume) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002279 // All other activities must be made visible with their correct configuration.
2280 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2281 if (!kept) {
2282 resumeFocusedStackTopActivityLocked();
2283 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002284 }
2285 }
2286 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002287 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2288 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002289
2290 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002291 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002292 }
2293
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002294 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002295 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2296 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002297 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002298 }
2299
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002300 ActivityContainer activityContainer = new ActivityContainer(stackId);
2301 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002302 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002303 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002304 }
2305
2306 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002307 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002308 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2309 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002310 break;
2311 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002312 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002313 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002314 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002315 }
2316
Chong Zhang5dcb2752015-08-18 13:50:26 -07002317 /**
2318 * Restores a recent task to a stack
2319 * @param task The recent task to be restored.
2320 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002321 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002322 * @return true if the task has been restored successfully.
2323 */
2324 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2325 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002326 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002327 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2328 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2329 // Put it in the fullscreen stack.
2330 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002331 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002332
Andrii Kulian02b7a832016-10-06 23:11:56 -07002333 final ActivityStack currentStack = task.getStack();
2334 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002335 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002336 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002337 // Nothing else to do since it is already restored in the right stack.
2338 return true;
2339 }
2340 // Remove current stack association, so we can re-associate the task with the
2341 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002342 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002343 }
2344
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002345 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002346 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002347
2348 if (stack == null) {
2349 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002350 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2351 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002352 return false;
2353 }
2354
Wale Ogunwale5f986092015-12-04 15:35:38 -08002355 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002356 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2357 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002358 final ArrayList<ActivityRecord> activities = task.mActivities;
2359 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002360 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002361 }
2362 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002363 }
2364
Wale Ogunwale040b4702015-08-06 18:10:50 -07002365 /**
2366 * Moves the specified task record to the input stack id.
2367 * WARNING: This method performs an unchecked/raw move of the task and
2368 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002369 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002370 * @param task Task to move.
2371 * @param stackId Id of stack to move task to.
2372 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002373 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002374 * @param reason Reason the task is been moved.
2375 * @return The stack the task was moved to.
2376 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002377 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002378 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002379
2380 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2381 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2382 + "support multi-window task=" + task + " to stackId=" + stackId);
2383 }
2384
Wale Ogunwale06579d62016-04-30 15:29:06 -07002385 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002386 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002387 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002388 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002389 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2390 // Whenever we are moving the top activity from the front stack we want to make sure to move
2391 // the stack to the front.
2392 final boolean wasFront = isFrontStack(prevStack)
2393 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002394
Chong Zhangd545dce2016-02-29 18:09:17 -08002395 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002396 // We don't allow moving a unresizeable task to the docked stack since the docked
2397 // stack is used for split-screen mode and will cause things like the docked divider to
2398 // show up. We instead leave the task in its current stack or move it to the fullscreen
2399 // stack if it isn't currently in a stack.
2400 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002401 Slog.w(TAG, "Can not move unresizeable task=" + task
2402 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2403 }
2404
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002405 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2406 // if a docked stack is created below which will lead to the stack we are moving from and
2407 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002408 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002409 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002410 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002411 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002412 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002413
2414 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002415 // move focus to the new stack by moving the stack to the front.
2416 stack.moveToFrontAndResumeStateIfNeeded(
2417 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002418
Wale Ogunwale040b4702015-08-06 18:10:50 -07002419 return stack;
2420 }
2421
Chong Zhange4fbd322016-03-01 14:44:03 -08002422 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002423 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002424 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2425 false /* deferResume */);
2426 }
2427
2428 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2429 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002430 final TaskRecord task = anyTaskForIdLocked(taskId);
2431 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002432 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002433 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002434 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002435
Andrii Kulian02b7a832016-10-06 23:11:56 -07002436 final ActivityStack currentStack = task.getStack();
2437 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002438 // You are already in the right stack silly...
2439 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002440 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002441 }
2442
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002443 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2444 throw new IllegalArgumentException("moveTaskToStack:"
2445 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2446 }
2447
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002448 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002449 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002450 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002451 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002452 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002453 // We are about to relaunch the activity because its configuration changed due to
2454 // being maximized, i.e. size change. The activity will first remove the old window
2455 // and then add a new one. This call will tell window manager about this, so it can
2456 // preserve the old window until the new one is drawn. This prevents having a gap
2457 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002458 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002459 // Note here we always set the replacing window first, as the flags might be needed
2460 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002461 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002462 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002463
2464 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002465 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002466 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002467 try {
2468 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002469 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002470 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002471
Wale Ogunwale961f4852016-02-01 20:25:54 -08002472 if (!animate) {
2473 stack.mNoAnimActivities.add(topActivity);
2474 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002475
Wale Ogunwale961f4852016-02-01 20:25:54 -08002476 // We might trigger a configuration change. Save the current task bounds for freezing.
2477 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2478
2479 // Make sure the task has the appropriate bounds/size for the stack it is in.
2480 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002481 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2482 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002483 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2484 Rect bounds = task.getLaunchBounds();
2485 if (bounds == null) {
2486 stack.layoutTaskInStack(task, null);
2487 bounds = task.mBounds;
2488 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002489 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2490 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002491 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002492 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2493 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002494 }
2495 } finally {
2496 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002497 }
2498
2499 if (mightReplaceWindow) {
2500 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2501 // window), we need to clear the replace window settings. Otherwise, we schedule a
2502 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002503 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002504 }
2505
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002506 if (!deferResume) {
2507
2508 // The task might have already been running and its visibility needs to be synchronized with
2509 // the visibility of the stack / windows.
2510 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2511 resumeFocusedStackTopActivityLocked();
2512 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002513
Jorim Jaggid53f0922016-04-06 22:16:23 -07002514 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002515
2516 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002517 }
2518
Wale Ogunwale079a0042015-10-24 11:44:07 -07002519 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2520 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2521 if (stack == null) {
2522 throw new IllegalArgumentException(
2523 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2524 }
2525
2526 final ActivityRecord r = stack.topRunningActivityLocked();
2527 if (r == null) {
2528 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2529 + " in stack=" + stack);
2530 return false;
2531 }
2532
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002533 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002534 Slog.w(TAG,
2535 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002536 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002537 return false;
2538 }
2539
Wale Ogunwale480dca02016-02-06 13:58:29 -08002540 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002541 return true;
2542 }
2543
Wale Ogunwale480dca02016-02-06 13:58:29 -08002544 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2545 mWindowManager.deferSurfaceLayout();
2546 try {
2547 final TaskRecord task = r.task;
2548
Andrii Kulian02b7a832016-10-06 23:11:56 -07002549 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002550 // An activity can't be pinned and visible behind at the same time. Go ahead and
2551 // release it from been visible behind before pinning.
2552 requestVisibleBehindLocked(r, false);
2553 }
2554
Wale Ogunwale480dca02016-02-06 13:58:29 -08002555 // Need to make sure the pinned stack exist so we can resize it below...
2556 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2557
2558 // Resize the pinned stack to match the current size of the task the activity we are
2559 // going to be moving is currently contained in. We do this to have the right starting
2560 // animation bounds for the pinned stack to the desired bounds the caller wants.
2561 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2562 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002563 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002564
2565 if (task.mActivities.size() == 1) {
2566 // There is only one activity in the task. So, we can just move the task over to
2567 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002568 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2569 // Move the home stack forward if the task we just moved to the pinned stack
2570 // was launched from home so home should be visible behind it.
2571 moveHomeStackToFront(reason);
2572 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002573 moveTaskToStackLocked(
2574 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2575 } else {
2576 stack.moveActivityToStack(r);
2577 }
2578 } finally {
2579 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002580 }
2581
Wale Ogunwale480dca02016-02-06 13:58:29 -08002582 // The task might have already been running and its visibility needs to be synchronized
2583 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002584 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002585 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002586
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002587 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002588 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002589 }
2590
Chong Zhang6cda19c2016-06-14 19:07:56 -07002591 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2592 if (r == null || !r.isFocusable()) {
2593 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2594 "moveActivityStackToFront: unfocusable r=" + r);
2595 return false;
2596 }
2597
2598 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002599 final ActivityStack stack = r.getStack();
2600 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002601 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2602 + r + " task=" + task);
2603 return false;
2604 }
2605
Chong Zhang6cda19c2016-06-14 19:07:56 -07002606 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2607 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2608 "moveActivityStackToFront: already on top, r=" + r);
2609 return false;
2610 }
2611
2612 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2613 "moveActivityStackToFront: r=" + r);
2614
2615 stack.moveToFront(reason, task);
2616 return true;
2617 }
2618
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002619 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2620 final TaskRecord task = anyTaskForIdLocked(taskId);
2621 if (task == null) {
2622 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2623 return;
2624 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002625 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2626
2627 task.updateOverrideConfigurationForStack(stack);
2628
2629 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002630 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002631 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002632 // The task might have already been running and its visibility needs to be synchronized with
2633 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002634 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002635 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002636 }
2637
Craig Mautnerac6f8432013-07-17 13:24:59 -07002638 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002639 mTmpFindTaskResult.r = null;
2640 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002641 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2643 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2645 final ActivityStack stack = stacks.get(stackNdx);
2646 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002647 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002648 continue;
2649 }
2650 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002651 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2652 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002653 continue;
2654 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002655 stack.findTaskLocked(r, mTmpFindTaskResult);
2656 // It is possible to have task in multiple stacks with the same root affinity.
2657 // If the match we found was based on root affinity we keep on looking to see if
2658 // there is a better match in another stack. We eventually return the match based
2659 // on root affinity if we don't find a better match.
2660 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2661 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002662 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002663 }
2664 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002665 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2666 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002667 }
2668
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002669 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2670 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002673 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002674 final ActivityRecord ar = stacks.get(stackNdx)
2675 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 if (ar != null) {
2677 return ar;
2678 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002679 }
2680 }
2681 return null;
2682 }
2683
Craig Mautner8d341ef2013-03-26 09:03:27 -07002684 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002685 scheduleSleepTimeout();
2686 if (!mGoingToSleep.isHeld()) {
2687 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002688 if (mLaunchingActivity.isHeld()) {
2689 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2690 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002691 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002692 mLaunchingActivity.release();
2693 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002694 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002696 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002697 }
2698
2699 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002700 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002701
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002702 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002703 final long endTime = System.currentTimeMillis() + timeout;
2704 while (true) {
2705 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002706 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2707 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2709 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2710 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002711 }
2712 if (cantShutdown) {
2713 long timeRemaining = endTime - System.currentTimeMillis();
2714 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002715 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002716 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002717 } catch (InterruptedException e) {
2718 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002719 } else {
2720 Slog.w(TAG, "Activity manager shutdown timed out");
2721 timedout = true;
2722 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002723 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002724 } else {
2725 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002726 }
2727 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002728
2729 // Force checkReadyForSleep to complete.
2730 mSleepTimeout = true;
2731 checkReadyForSleepLocked();
2732
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 return timedout;
2734 }
2735
2736 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002737 removeSleepTimeouts();
2738 if (mGoingToSleep.isHeld()) {
2739 mGoingToSleep.release();
2740 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2742 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2744 final ActivityStack stack = stacks.get(stackNdx);
2745 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002746 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002747 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002748 }
Craig Mautner5314a402013-09-26 12:40:16 -07002749 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002750 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002751 mGoingToSleepActivities.clear();
2752 }
2753
2754 void activitySleptLocked(ActivityRecord r) {
2755 mGoingToSleepActivities.remove(r);
2756 checkReadyForSleepLocked();
2757 }
2758
2759 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002760 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002761 // Do not care.
2762 return;
2763 }
2764
2765 if (!mSleepTimeout) {
2766 boolean dontSleep = false;
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 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2771 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002772 }
2773
2774 if (mStoppingActivities.size() > 0) {
2775 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002776 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002777 + mStoppingActivities.size() + " activities");
2778 scheduleIdleLocked();
2779 dontSleep = true;
2780 }
2781
2782 if (mGoingToSleepActivities.size() > 0) {
2783 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002784 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002785 + mGoingToSleepActivities.size() + " activities");
2786 dontSleep = true;
2787 }
2788
2789 if (dontSleep) {
2790 return;
2791 }
2792 }
2793
Wei Wang65c7a152016-06-02 18:51:22 -07002794 // Send launch end powerhint before going sleep
2795 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2796
Craig Mautnere0a38842013-12-16 16:14:02 -08002797 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2798 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2800 stacks.get(stackNdx).goToSleep();
2801 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002802 }
2803
2804 removeSleepTimeouts();
2805
2806 if (mGoingToSleep.isHeld()) {
2807 mGoingToSleep.release();
2808 }
2809 if (mService.mShuttingDown) {
2810 mService.notifyAll();
2811 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002812 }
2813
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002814 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002815 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002816 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002817 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002818 }
2819 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002820 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002821 mWindowManager.executeAppTransition();
2822 return true;
2823 }
2824 return false;
2825 }
2826
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2829 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002830 int stackNdx = stacks.size() - 1;
2831 while (stackNdx >= 0) {
2832 stacks.get(stackNdx).handleAppCrashLocked(app);
2833 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002834 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002835 }
2836 }
2837
Jose Lima4b6c6692014-08-12 17:41:12 -07002838 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002839 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002840 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002841 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2842 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002843 return false;
2844 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002845
2846 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2847 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2848 + " visible=" + visible + " stackId=" + stack.mStackId
2849 + " can't contain visible behind activities");
2850 return false;
2851 }
2852
Jose Lima4b6c6692014-08-12 17:41:12 -07002853 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002854 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2855 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002856
2857 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002858 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002859 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002860 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002861 return true;
2862 }
2863
2864 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002865 if (visible && top.fullscreen) {
2866 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002867 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2868 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2869 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2870 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002871 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002872 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2873 // Only the activity set as currently visible behind should actively reset its
2874 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002875 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2876 "requestVisibleBehind: returning visible=" + visible
2877 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2878 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002879 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002880 }
2881
Jose Lima4b6c6692014-08-12 17:41:12 -07002882 stack.setVisibleBehindActivity(visible ? r : null);
2883 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002884 // If there is a translucent home activity, we need to force it stop being translucent,
2885 // because we can't depend on the application to necessarily perform that operation.
2886 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002887 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002888 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002889 mService.convertFromTranslucent(next.appToken);
2890 }
2891 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002892 if (top.app != null && top.app.thread != null) {
2893 // Notify the top app of the change.
2894 try {
2895 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2896 } catch (RemoteException e) {
2897 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002898 }
2899 return true;
2900 }
2901
Craig Mautnerbb742462014-07-07 15:28:55 -07002902 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002903 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002904 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002905 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002906
2907 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002908 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002909 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002910 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002911 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002912
2913 // When launching tasks behind, update the last active time of the top task after the new
2914 // task has been shown briefly
2915 final ActivityRecord top = stack.topActivity();
2916 if (top != null) {
2917 top.task.touchActiveTime();
2918 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002919 }
2920
2921 void scheduleLaunchTaskBehindComplete(IBinder token) {
2922 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2923 }
2924
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002925 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2926 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002927 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002928 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2929 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 final int topStackNdx = stacks.size() - 1;
2931 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2932 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002933 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002934 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002935 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002936 }
2937
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002938 void invalidateTaskLayers() {
2939 mTaskLayersChanged = true;
2940 }
2941
2942 void rankTaskLayersIfNeeded() {
2943 if (!mTaskLayersChanged) {
2944 return;
2945 }
2946 mTaskLayersChanged = false;
2947 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2948 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2949 int baseLayer = 0;
2950 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2951 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2952 }
2953 }
2954 }
2955
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002956 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2957 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2958 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2959 final int topStackNdx = stacks.size() - 1;
2960 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2961 final ActivityStack stack = stacks.get(stackNdx);
2962 stack.clearOtherAppTimeTrackers(except);
2963 }
2964 }
2965 }
2966
Craig Mautner8d341ef2013-03-26 09:03:27 -07002967 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002968 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2969 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002970 final int numStacks = stacks.size();
2971 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2972 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002973 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002974 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002975 }
2976 }
2977
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002978 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2979 // Examine all activities currently running in the process.
2980 TaskRecord firstTask = null;
2981 // Tasks is non-null only if two or more tasks are found.
2982 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002983 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2984 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002985 ActivityRecord r = app.activities.get(i);
2986 // First, if we find an activity that is in the process of being destroyed,
2987 // then we just aren't going to do anything for now; we want things to settle
2988 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002989 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002990 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002991 return;
2992 }
2993 // Don't consider any activies that are currently not in a state where they
2994 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002995 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2996 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002997 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002998 continue;
2999 }
3000 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003001 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003002 + " from " + r);
3003 if (firstTask == null) {
3004 firstTask = r.task;
3005 } else if (firstTask != r.task) {
3006 if (tasks == null) {
3007 tasks = new ArraySet<>();
3008 tasks.add(firstTask);
3009 }
3010 tasks.add(r.task);
3011 }
3012 }
3013 }
3014 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003015 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003016 return;
3017 }
3018 // If we have activities in multiple tasks that are in a position to be destroyed,
3019 // let's iterate through the tasks and release the oldest one.
3020 final int numDisplays = mActivityDisplays.size();
3021 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3022 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3023 // Step through all stacks starting from behind, to hit the oldest things first.
3024 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3025 final ActivityStack stack = stacks.get(stackNdx);
3026 // Try to release activities in this stack; if we manage to, we are done.
3027 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3028 return;
3029 }
3030 }
3031 }
3032 }
3033
Amith Yamasani37a40c22015-06-17 13:25:42 -07003034 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003035 final int focusStackId = mFocusedStack.getStackId();
3036 // We dismiss the docked stack whenever we switch users.
3037 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3038
3039 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003040 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003041 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003042
Craig Mautner858d8a62013-04-23 17:08:34 -07003043 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3045 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003046 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003047 final ActivityStack stack = stacks.get(stackNdx);
3048 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003049 TaskRecord task = stack.topTask();
3050 if (task != null) {
3051 mWindowManager.moveTaskToTop(task.taskId);
3052 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003053 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003054 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003055
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003056 ActivityStack stack = getStack(restoreStackId);
3057 if (stack == null) {
3058 stack = mHomeStack;
3059 }
3060 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003061 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003062 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003063 } else {
3064 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003065 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003066 }
Craig Mautner93529a42013-10-04 15:03:13 -07003067 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003068 }
3069
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003070 /** Checks whether the userid is a profile of the current user. */
3071 boolean isCurrentProfileLocked(int userId) {
3072 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003073 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003074 }
3075
Craig Mautnerde4ef022013-04-07 19:01:33 -07003076 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 ArrayList<ActivityRecord> stops = null;
3078
3079 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003080 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3081 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003082 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3083 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003084 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003085 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003086 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3087 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003088 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003089 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003090 if (s.finishing) {
3091 // If this activity is finishing, it is sitting on top of
3092 // everyone else but we now know it is no longer needed...
3093 // so get rid of it. Otherwise, we need to go through the
3094 // normal flow and hide it once we determine that it is
3095 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003096 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003097 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003098 }
3099 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003100 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003101 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003102 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003103 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003104 }
3105 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003106 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003107 }
3108 }
3109
3110 return stops;
3111 }
3112
Craig Mautnercf910b02013-04-23 11:23:27 -07003113 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003114 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3115 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3116 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3117 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003118 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003119 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003120 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003121 if (r == null) Slog.e(TAG,
3122 "validateTop...: null top activity, stack=" + stack);
3123 else {
3124 final ActivityRecord pausing = stack.mPausingActivity;
3125 if (pausing != null && pausing == r) Slog.e(TAG,
3126 "validateTop...: top stack has pausing activity r=" + r
3127 + " state=" + state);
3128 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3129 "validateTop...: activity in front not resumed r=" + r
3130 + " state=" + state);
3131 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003132 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003133 final ActivityRecord resumed = stack.mResumedActivity;
3134 if (resumed != null && resumed == r) Slog.e(TAG,
3135 "validateTop...: back stack has resumed activity r=" + r
3136 + " state=" + state);
3137 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3138 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003139 }
3140 }
3141 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003142 }
3143
Craig Mautnere0570202015-05-13 13:06:11 -07003144 private String lockTaskModeToString() {
3145 switch (mLockTaskModeState) {
3146 case LOCK_TASK_MODE_LOCKED:
3147 return "LOCKED";
3148 case LOCK_TASK_MODE_PINNED:
3149 return "PINNED";
3150 case LOCK_TASK_MODE_NONE:
3151 return "NONE";
3152 default: return "unknown=" + mLockTaskModeState;
3153 }
3154 }
3155
Craig Mautner27084302013-03-25 08:05:25 -07003156 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003157 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003158 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003159 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003160 pw.print(prefix);
3161 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003162 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003163 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003164 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3165 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3166 if (packages.size() > 0) {
3167 pw.println(" mLockTaskPackages (userId:packages)=");
3168 for (int i = 0; i < packages.size(); ++i) {
3169 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3170 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3171 }
3172 }
3173 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003174 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175
Winson43d1f262016-06-14 16:05:55 -07003176 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003177 * Dump all connected displays' configurations.
3178 * @param prefix Prefix to apply to each line of the dump.
3179 */
3180 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3181 pw.print(prefix); pw.println("Display override configurations:");
3182 final int displayCount = mActivityDisplays.size();
3183 for (int i = 0; i < displayCount; i++) {
3184 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3185 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3186 pw.println(activityDisplay.getOverrideConfiguration());
3187 }
3188 }
3189
3190 /**
Winson43d1f262016-06-14 16:05:55 -07003191 * Dumps the activities matching the given {@param name} in the either the focused stack
3192 * or all visible stacks if {@param dumpVisibleStacks} is true.
3193 */
3194 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3195 if (dumpVisibleStacks) {
3196 ArrayList<ActivityRecord> activities = new ArrayList<>();
3197 int numDisplays = mActivityDisplays.size();
3198 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3199 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3200 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3201 ActivityStack stack = stacks.get(stackNdx);
3202 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3203 activities.addAll(stack.getDumpActivitiesLocked(name));
3204 }
3205 }
3206 }
3207 return activities;
3208 } else {
3209 return mFocusedStack.getDumpActivitiesLocked(name);
3210 }
Craig Mautner20e72272013-04-01 13:45:53 -07003211 }
3212
Dianne Hackborn390517b2013-05-30 15:03:32 -07003213 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3214 boolean needSep, String prefix) {
3215 if (activity != null) {
3216 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3217 if (needSep) {
3218 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003219 }
3220 pw.print(prefix);
3221 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003222 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003223 }
3224 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003225 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003226 }
3227
Craig Mautner8d341ef2013-03-26 09:03:27 -07003228 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3229 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003230 boolean printed = false;
3231 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3233 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003234 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003235 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003236 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003237 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003238 final ActivityStack stack = stacks.get(stackNdx);
3239 StringBuilder stackHeader = new StringBuilder(128);
3240 stackHeader.append(" Stack #");
3241 stackHeader.append(stack.mStackId);
3242 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003243 stackHeader.append("\n");
3244 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3245 stackHeader.append("\n");
3246 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003247 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3248 needSep, stackHeader.toString());
3249 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3250 !dumpAll, false, dumpPackage, true,
3251 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003252
Craig Mautner4a1cb222013-12-04 16:14:06 -08003253 needSep = printed;
3254 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3255 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003256 if (pr) {
3257 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003258 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003259 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003260 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3261 " mResumedActivity: ");
3262 if (pr) {
3263 printed = true;
3264 needSep = false;
3265 }
3266 if (dumpAll) {
3267 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3268 " mLastPausedActivity: ");
3269 if (pr) {
3270 printed = true;
3271 needSep = true;
3272 }
3273 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3274 needSep, " mLastNoHistoryActivity: ");
3275 }
3276 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003277 }
3278 }
3279
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003280 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3281 false, dumpPackage, true, " Activities waiting to finish:", null);
3282 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3283 false, dumpPackage, true, " Activities waiting to stop:", null);
3284 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3285 false, dumpPackage, true, " Activities waiting for another to become visible:",
3286 null);
3287 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3288 false, dumpPackage, true, " Activities waiting to sleep:", null);
3289 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3290 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003291
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003292 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 }
3294
Dianne Hackborn390517b2013-05-30 15:03:32 -07003295 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003296 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003297 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003298 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003299 String innerPrefix = null;
3300 String[] args = null;
3301 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003302 for (int i=list.size()-1; i>=0; i--) {
3303 final ActivityRecord r = list.get(i);
3304 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3305 continue;
3306 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003307 if (innerPrefix == null) {
3308 innerPrefix = prefix + " ";
3309 args = new String[0];
3310 }
3311 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 final boolean full = !brief && (complete || !r.isInHistory());
3313 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003314 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003315 needNL = false;
3316 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003317 if (header1 != null) {
3318 pw.println(header1);
3319 header1 = null;
3320 }
3321 if (header2 != null) {
3322 pw.println(header2);
3323 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003324 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003325 if (lastTask != r.task) {
3326 lastTask = r.task;
3327 pw.print(prefix);
3328 pw.print(full ? "* " : " ");
3329 pw.println(lastTask);
3330 if (full) {
3331 lastTask.dump(pw, prefix + " ");
3332 } else if (complete) {
3333 // Complete + brief == give a summary. Isn't that obvious?!?
3334 if (lastTask.intent != null) {
3335 pw.print(prefix); pw.print(" ");
3336 pw.println(lastTask.intent.toInsecureStringWithClip());
3337 }
3338 }
3339 }
3340 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3341 pw.print(" #"); pw.print(i); pw.print(": ");
3342 pw.println(r);
3343 if (full) {
3344 r.dump(pw, innerPrefix);
3345 } else if (complete) {
3346 // Complete + brief == give a summary. Isn't that obvious?!?
3347 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3348 if (r.app != null) {
3349 pw.print(innerPrefix); pw.println(r.app);
3350 }
3351 }
3352 if (client && r.app != null && r.app.thread != null) {
3353 // flush anything that is already in the PrintWriter since the thread is going
3354 // to write to the file descriptor directly
3355 pw.flush();
3356 try {
3357 TransferPipe tp = new TransferPipe();
3358 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003359 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 // Short timeout, since blocking here can
3361 // deadlock with the application.
3362 tp.go(fd, 2000);
3363 } finally {
3364 tp.kill();
3365 }
3366 } catch (IOException e) {
3367 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3368 } catch (RemoteException e) {
3369 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3370 }
3371 needNL = true;
3372 }
3373 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003374 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003375 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003376
Craig Mautnerf3333272013-04-22 10:55:53 -07003377 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003378 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3379 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003380 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3381 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003382 }
3383
3384 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003385 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003386 }
3387
3388 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003389 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3390 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003391 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3392 }
3393
Craig Mautner05d29032013-05-03 13:40:13 -07003394 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003395 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3396 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3397 }
Craig Mautner05d29032013-05-03 13:40:13 -07003398 }
3399
Craig Mautner0eea92c2013-05-16 13:35:39 -07003400 void removeSleepTimeouts() {
3401 mSleepTimeout = false;
3402 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3403 }
3404
3405 final void scheduleSleepTimeout() {
3406 removeSleepTimeouts();
3407 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3408 }
3409
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 @Override
3411 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003412 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003413 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3414 }
3415
3416 @Override
3417 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003418 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003419 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3420 }
3421
3422 @Override
3423 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003424 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003425 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3426 }
3427
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003428 DisplayInfo getDisplayInfo(int displayId) {
3429 return mActivityDisplays.get(displayId).mDisplayInfo;
3430 }
3431
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003432 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003433 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003434 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003435 newDisplay = mActivityDisplays.get(displayId) == null;
3436 if (newDisplay) {
3437 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003438 if (activityDisplay.mDisplay == null) {
3439 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3440 return;
3441 }
Craig Mautner4504de52013-12-20 09:06:56 -08003442 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003443 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003444 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003445 }
Craig Mautner4504de52013-12-20 09:06:56 -08003446 if (newDisplay) {
3447 mWindowManager.onDisplayAdded(displayId);
3448 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003449 }
3450
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003451 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003452 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003453 mService.mContext.getResources().getDimensionPixelSize(
3454 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003455 }
3456
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003457 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003458 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003459 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3460 if (activityDisplay != null) {
3461 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003463 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003465 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003466 }
3467 }
3468 mWindowManager.onDisplayRemoved(displayId);
3469 }
3470
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003471 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003472 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003473 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3474 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003475 // TODO: Update the bounds.
3476 }
3477 }
3478 mWindowManager.onDisplayChanged(displayId);
3479 }
3480
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003481 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003482 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003483 StackInfo info = new StackInfo();
3484 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3485 info.displayId = Display.DEFAULT_DISPLAY;
3486 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003487 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003488 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003489 info.position = display != null
3490 ? display.mStacks.indexOf(stack)
3491 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003492
3493 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3494 final int numTasks = tasks.size();
3495 int[] taskIds = new int[numTasks];
3496 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003497 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003498 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 for (int i = 0; i < numTasks; ++i) {
3500 final TaskRecord task = tasks.get(i);
3501 taskIds[i] = task.taskId;
3502 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3503 : task.realActivity != null ? task.realActivity.flattenToString()
3504 : task.getTopActivity() != null ? task.getTopActivity().packageName
3505 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003506 taskBounds[i] = new Rect();
3507 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003508 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003509 }
3510 info.taskIds = taskIds;
3511 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003512 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003513 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003514
3515 final ActivityRecord top = stack.topRunningActivityLocked();
3516 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003517 return info;
3518 }
3519
3520 StackInfo getStackInfoLocked(int stackId) {
3521 ActivityStack stack = getStack(stackId);
3522 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003523 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 }
3525 return null;
3526 }
3527
3528 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003529 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003530 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3531 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003533 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003534 }
3535 }
3536 return list;
3537 }
3538
Craig Mautner15df08a2015-04-01 12:17:18 -07003539 TaskRecord getLockedTaskLocked() {
3540 final int top = mLockTaskModeTasks.size() - 1;
3541 if (top >= 0) {
3542 return mLockTaskModeTasks.get(top);
3543 }
3544 return null;
3545 }
3546
3547 boolean isLockedTask(TaskRecord task) {
3548 return mLockTaskModeTasks.contains(task);
3549 }
3550
3551 boolean isLastLockedTask(TaskRecord task) {
3552 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3553 }
3554
3555 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003556 if (!mLockTaskModeTasks.remove(task)) {
3557 return;
3558 }
3559 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3560 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003561 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003562 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3563 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003564 final Message lockTaskMsg = Message.obtain();
3565 lockTaskMsg.arg1 = task.userId;
3566 lockTaskMsg.what = LOCK_TASK_END_MSG;
3567 mHandler.sendMessage(lockTaskMsg);
3568 }
3569 }
3570
Andrii Kulianc27916642016-04-12 17:59:27 -07003571 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3572 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3573 false /* forceNonResizable */);
3574 }
3575
Jorim Jaggid53f0922016-04-06 22:16:23 -07003576 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003577 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003578 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3579 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003580 return;
3581 }
3582
Jorim Jaggicd13d332016-04-27 15:40:20 -07003583 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003584 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003585 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003586 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3587
Andrii Kulianc27916642016-04-12 17:59:27 -07003588 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3589 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003590 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003591 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3592 && !topActivity.noDisplay) {
3593 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003594 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3595 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003596 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003597 }
3598
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003599 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003600 if (mLockTaskNotify != null) {
3601 mLockTaskNotify.showToast(mLockTaskModeState);
3602 }
Jason Monka8f569c2014-07-07 12:15:21 -04003603 }
3604
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003605 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3606 if (mLockTaskModeTasks.contains(task)) {
3607 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3608 }
3609 }
3610
Craig Mautner432f64e2015-05-20 14:59:57 -07003611 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3612 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003613 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003614 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003615 final TaskRecord lockedTask = getLockedTaskLocked();
3616 if (lockedTask != null) {
3617 removeLockedTaskLocked(lockedTask);
3618 if (!mLockTaskModeTasks.isEmpty()) {
3619 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003620 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3621 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003622 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003623 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003624 return;
3625 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003626 }
Craig Mautnere0570202015-05-13 13:06:11 -07003627 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3628 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003629 return;
3630 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003631
3632 // Should have already been checked, but do it again.
3633 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003634 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3635 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003636 return;
3637 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003638 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003639 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003640 return;
3641 }
3642
3643 if (mLockTaskModeTasks.isEmpty()) {
3644 // First locktask.
3645 final Message lockTaskMsg = Message.obtain();
3646 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3647 lockTaskMsg.arg1 = task.userId;
3648 lockTaskMsg.what = LOCK_TASK_START_MSG;
3649 lockTaskMsg.arg2 = lockTaskModeState;
3650 mHandler.sendMessage(lockTaskMsg);
3651 }
3652 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003653 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3654 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003655 mLockTaskModeTasks.remove(task);
3656 mLockTaskModeTasks.add(task);
3657
3658 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003659 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003660 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003661
3662 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003663 findTaskToMoveToFrontLocked(task, 0, null, reason,
3664 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003665 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003666 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003667 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003668 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003669 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003670 }
3671
3672 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003673 return isLockTaskModeViolation(task, false);
3674 }
3675
3676 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3677 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003678 return false;
3679 }
3680 final int lockTaskAuth = task.mLockTaskAuth;
3681 switch (lockTaskAuth) {
3682 case LOCK_TASK_AUTH_DONT_LOCK:
3683 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003684 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003685 case LOCK_TASK_AUTH_LAUNCHABLE:
3686 case LOCK_TASK_AUTH_WHITELISTED:
3687 return false;
3688 case LOCK_TASK_AUTH_PINNABLE:
3689 // Pinnable tasks can't be launched on top of locktask tasks.
3690 return !mLockTaskModeTasks.isEmpty();
3691 default:
3692 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3693 return true;
3694 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003695 }
3696
Craig Mautner15df08a2015-04-01 12:17:18 -07003697 void onLockTaskPackagesUpdatedLocked() {
3698 boolean didSomething = false;
3699 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3700 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003701 final boolean wasWhitelisted =
3702 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3703 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003704 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003705 final boolean isWhitelisted =
3706 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3707 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3708 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003709 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003710 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3711 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003712 removeLockedTaskLocked(lockedTask);
3713 lockedTask.performClearTaskLocked();
3714 didSomething = true;
3715 }
3716 }
3717 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3718 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3719 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3720 final ActivityStack stack = stacks.get(stackNdx);
3721 stack.onLockTaskPackagesUpdatedLocked();
3722 }
3723 }
Craig Mautnere0570202015-05-13 13:06:11 -07003724 final ActivityRecord r = topRunningActivityLocked();
3725 final TaskRecord task = r != null ? r.task : null;
3726 if (mLockTaskModeTasks.isEmpty() && task != null
3727 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3728 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003729 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3730 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3731 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3732 false);
3733 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003734 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003735 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003736 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003737 }
3738 }
3739
Benjamin Franz43261142015-02-11 15:59:44 +00003740 int getLockTaskModeState() {
3741 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003742 }
3743
Jorim Jaggife89d122015-12-22 16:28:44 +01003744 void activityRelaunchedLocked(IBinder token) {
3745 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003746 if (mService.isSleepingOrShuttingDownLocked()) {
3747 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3748 if (r != null) {
3749 r.setSleeping(true, true);
3750 }
3751 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003752 }
3753
3754 void activityRelaunchingLocked(ActivityRecord r) {
3755 mWindowManager.notifyAppRelaunching(r.appToken);
3756 }
3757
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003758 void logStackState() {
3759 mActivityMetricsLogger.logWindowState();
3760 }
3761
Andrii Kulian933076d2016-03-29 17:04:42 -07003762 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003763 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3764 final ActivityRecord r = task.mActivities.get(i);
3765 if (r.app != null && r.app.thread != null) {
3766 mMultiWindowModeChangedActivities.add(r);
3767 }
3768 }
3769
3770 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3771 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3772 }
3773 }
3774
Andrii Kulian933076d2016-03-29 17:04:42 -07003775 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003776 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003777 if (prevStack == null || prevStack == stack
3778 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3779 return;
3780 }
3781
3782 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3783 final ActivityRecord r = task.mActivities.get(i);
3784 if (r.app != null && r.app.thread != null) {
3785 mPipModeChangedActivities.add(r);
3786 }
3787 }
3788
3789 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3790 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3791 }
3792 }
3793
Tony Mak853304c2016-04-18 15:17:41 +01003794 void setDockedStackMinimized(boolean minimized) {
3795 mIsDockMinimized = minimized;
3796 if (minimized) {
3797 // Docked stack is not visible, no need to confirm credentials for its top activity.
3798 return;
3799 }
3800 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3801 if (dockedStack == null) {
3802 return;
3803 }
3804 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3805 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3806 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3807 }
3808 }
3809
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003810 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003811
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003812 public ActivityStackSupervisorHandler(Looper looper) {
3813 super(looper);
3814 }
3815
Craig Mautnerf3333272013-04-22 10:55:53 -07003816 void activityIdleInternal(ActivityRecord r) {
3817 synchronized (mService) {
3818 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3819 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003820 }
3821
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003822 @Override
3823 public void handleMessage(Message msg) {
3824 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003825 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3826 synchronized (mService) {
3827 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3828 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003829 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003830 }
3831 }
3832 } break;
3833 case REPORT_PIP_MODE_CHANGED_MSG: {
3834 synchronized (mService) {
3835 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3836 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003837 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003838 }
3839 }
3840 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003841 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003842 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3843 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003844 if (mService.mDidDexOpt) {
3845 mService.mDidDexOpt = false;
3846 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3847 nmsg.obj = msg.obj;
3848 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3849 return;
3850 }
3851 // We don't at this point know if the activity is fullscreen,
3852 // so we need to be conservative and assume it isn't.
3853 activityIdleInternal((ActivityRecord)msg.obj);
3854 } break;
3855 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003856 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003857 activityIdleInternal((ActivityRecord)msg.obj);
3858 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003859 case RESUME_TOP_ACTIVITY_MSG: {
3860 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003861 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003862 }
3863 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003864 case SLEEP_TIMEOUT_MSG: {
3865 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003866 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003867 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3868 mSleepTimeout = true;
3869 checkReadyForSleepLocked();
3870 }
3871 }
3872 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003873 case LAUNCH_TIMEOUT_MSG: {
3874 if (mService.mDidDexOpt) {
3875 mService.mDidDexOpt = false;
3876 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3877 return;
3878 }
3879 synchronized (mService) {
3880 if (mLaunchingActivity.isHeld()) {
3881 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3882 if (VALIDATE_WAKE_LOCK_CALLER
3883 && Binder.getCallingUid() != Process.myUid()) {
3884 throw new IllegalStateException("Calling must be system uid");
3885 }
3886 mLaunchingActivity.release();
3887 }
3888 }
3889 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003891 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003892 } break;
3893 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003894 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003895 } break;
3896 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003897 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003898 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003899 case CONTAINER_CALLBACK_VISIBILITY: {
3900 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003901 final IActivityContainerCallback callback = container.mCallback;
3902 if (callback != null) {
3903 try {
3904 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3905 } catch (RemoteException e) {
3906 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003907 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003908 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003909 case LOCK_TASK_START_MSG: {
3910 // When lock task starts, we disable the status bars.
3911 try {
Jason Monk62515be2014-05-21 16:06:19 -04003912 if (mLockTaskNotify == null) {
3913 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003914 }
Jason Monk62515be2014-05-21 16:06:19 -04003915 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003916 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003917 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003918 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003919 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003920 flags = StatusBarManager.DISABLE_MASK
3921 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003922 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003923 flags = StatusBarManager.DISABLE_MASK
3924 & (~StatusBarManager.DISABLE_BACK)
3925 & (~StatusBarManager.DISABLE_HOME)
3926 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003927 }
3928 getStatusBarService().disable(flags, mToken,
3929 mService.mContext.getPackageName());
3930 }
3931 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003932 if (getDevicePolicyManager() != null) {
3933 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003934 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003935 }
justinzhang5286d3f2014-05-12 17:06:01 -04003936 } catch (RemoteException ex) {
3937 throw new RuntimeException(ex);
3938 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003939 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003940 case LOCK_TASK_END_MSG: {
3941 // When lock task ends, we enable the status bars.
3942 try {
Jason Monk62515be2014-05-21 16:06:19 -04003943 if (getStatusBarService() != null) {
3944 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3945 mService.mContext.getPackageName());
3946 }
3947 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003948 if (getDevicePolicyManager() != null) {
3949 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3950 msg.arg1);
3951 }
Jason Monk62515be2014-05-21 16:06:19 -04003952 if (mLockTaskNotify == null) {
3953 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3954 }
3955 mLockTaskNotify.show(false);
3956 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003957 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003958 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003959 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003960 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003961 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003962 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003963 new LockPatternUtils(mService.mContext)
3964 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003965 }
3966 } catch (SettingNotFoundException e) {
3967 // No setting, don't lock.
3968 }
justinzhang5286d3f2014-05-12 17:06:01 -04003969 } catch (RemoteException ex) {
3970 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003971 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003973 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003974 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003975 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3976 if (mLockTaskNotify == null) {
3977 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3978 }
3979 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3980 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003981 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3982 final ActivityContainer container = (ActivityContainer) msg.obj;
3983 final IActivityContainerCallback callback = container.mCallback;
3984 if (callback != null) {
3985 try {
3986 callback.onAllActivitiesComplete(container.asBinder());
3987 } catch (RemoteException e) {
3988 }
3989 }
3990 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003991 case LAUNCH_TASK_BEHIND_COMPLETE: {
3992 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003993 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003994 if (r != null) {
3995 handleLaunchTaskBehindCompleteLocked(r);
3996 }
3997 }
3998 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003999
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004000 }
4001 }
4002 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004003
Ying Wangb081a592014-04-22 15:20:16 -07004004 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004005 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4006 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004008 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004010 ActivityRecord mParentActivity = null;
4011 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004012
Craig Mautnere3a00d72014-04-16 08:31:19 -07004013 boolean mVisible = true;
4014
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004016 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004018 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4019 final static int CONTAINER_STATE_NO_SURFACE = 1;
4020 final static int CONTAINER_STATE_FINISHING = 2;
4021 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4022
4023 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004024 synchronized (mService) {
4025 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004026 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004027 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004028 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004030 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004031
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004032 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004033 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004034 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004035 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004036 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004037 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004038 }
4039
4040 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004041 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4044 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 return;
4046 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004047 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 }
4049 }
4050
4051 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004052 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004053 synchronized (mService) {
4054 if (mActivityDisplay != null) {
4055 return mActivityDisplay.mDisplayId;
4056 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004057 }
4058 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 }
4060
Jeff Brownca9bc702014-02-11 14:32:56 -08004061 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004062 public int getStackId() {
4063 synchronized (mService) {
4064 return mStackId;
4065 }
4066 }
4067
4068 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004069 public boolean injectEvent(InputEvent event) {
4070 final long origId = Binder.clearCallingIdentity();
4071 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004072 synchronized (mService) {
4073 if (mActivityDisplay != null) {
4074 return mInputManagerInternal.injectInputEvent(event,
4075 mActivityDisplay.mDisplayId,
4076 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4077 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004078 }
4079 return false;
4080 } finally {
4081 Binder.restoreCallingIdentity(origId);
4082 }
4083 }
4084
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004085 @Override
4086 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004087 synchronized (mService) {
4088 if (mContainerState == CONTAINER_STATE_FINISHING) {
4089 return;
4090 }
4091 mContainerState = CONTAINER_STATE_FINISHING;
4092
Craig Mautnerd163e752014-06-13 17:18:47 -07004093 long origId = Binder.clearCallingIdentity();
4094 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004095 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004096 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004097 } finally {
4098 Binder.restoreCallingIdentity(origId);
4099 }
4100 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004101 }
4102
Craig Mautner60257702014-09-17 15:02:33 -07004103 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004104 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004105 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004106 if (mActivityDisplay != null) {
4107 mActivityDisplay.detachActivitiesLocked(mStack);
4108 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004109 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 }
4111 }
4112
4113 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004114 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004115 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004116 }
4117
4118 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004119 public final int startActivityIntentSender(IIntentSender intentSender)
4120 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004121 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4122
4123 if (!(intentSender instanceof PendingIntentRecord)) {
4124 throw new IllegalArgumentException("Bad PendingIntent object");
4125 }
4126
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004127 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004128 Binder.getCallingUid(), mCurrentUser, false,
4129 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004130
4131 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4132 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4133 pendingIntent.key.requestResolvedType);
4134
4135 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4136 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4137 }
4138
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004139 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004140 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004141 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004142 throw new SecurityException(
4143 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4144 }
4145 }
4146
Craig Mautnerdf88d732014-01-27 09:21:32 -08004147 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004148 public IBinder asBinder() {
4149 return this;
4150 }
4151
Craig Mautner4504de52013-12-20 09:06:56 -08004152 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004153 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004154 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004155 }
4156
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 ActivityStackSupervisor getOuter() {
4158 return ActivityStackSupervisor.this;
4159 }
4160
Craig Mautnerd163e752014-06-13 17:18:47 -07004161 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004162 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004163 }
4164
Craig Mautner6985bad2014-04-21 15:22:06 -07004165 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004166 void setVisible(boolean visible) {
4167 if (mVisible != visible) {
4168 mVisible = visible;
4169 if (mCallback != null) {
4170 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4171 0 /* unused */, this).sendToTarget();
4172 }
4173 }
4174 }
4175
Craig Mautner6985bad2014-04-21 15:22:06 -07004176 void setDrawn() {
4177 }
4178
Craig Mautner1b4bf852014-05-26 15:06:32 -07004179 // You can always start a new task on a regular ActivityStack.
4180 boolean isEligibleForNewTasks() {
4181 return true;
4182 }
4183
Craig Mautnerd163e752014-06-13 17:18:47 -07004184 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004185 detachLocked();
4186 deleteActivityContainer(this);
4187 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004188 }
4189
Craig Mautner34b73df2014-01-12 21:11:08 -08004190 @Override
4191 public String toString() {
4192 return mIdString + (mActivityDisplay == null ? "N" : "A");
4193 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004194 }
4195
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004196 private class VirtualActivityContainer extends ActivityContainer {
4197 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004198 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004199
4200 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4201 super(getNextStackId());
4202 mParentActivity = parent;
4203 mCallback = callback;
4204 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004205 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004206 }
4207
4208 @Override
4209 public void setSurface(Surface surface, int width, int height, int density) {
4210 super.setSurface(surface, width, height, density);
4211
4212 synchronized (mService) {
4213 final long origId = Binder.clearCallingIdentity();
4214 try {
4215 setSurfaceLocked(surface, width, height, density);
4216 } finally {
4217 Binder.restoreCallingIdentity(origId);
4218 }
4219 }
4220 }
4221
4222 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4223 if (mContainerState == CONTAINER_STATE_FINISHING) {
4224 return;
4225 }
4226 VirtualActivityDisplay virtualActivityDisplay =
4227 (VirtualActivityDisplay) mActivityDisplay;
4228 if (virtualActivityDisplay == null) {
4229 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004230 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004231 mActivityDisplay = virtualActivityDisplay;
4232 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004233 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004234 }
4235
4236 if (mSurface != null) {
4237 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004238 }
4239
Craig Mautner6985bad2014-04-21 15:22:06 -07004240 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004241 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004242 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004243 } else {
4244 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004245 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004246 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004247 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004248 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004249 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004250
Craig Mautnerd163e752014-06-13 17:18:47 -07004251 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004252
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004253 if (DEBUG_STACK) Slog.d(TAG_STACK,
4254 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004255 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004256
Craig Mautner6985bad2014-04-21 15:22:06 -07004257 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004258 boolean isAttachedLocked() {
4259 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004260 }
4261
4262 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004263 void setDrawn() {
4264 synchronized (mService) {
4265 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004266 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004267 }
4268 }
4269
Craig Mautner1b4bf852014-05-26 15:06:32 -07004270 // Never start a new task on an ActivityView if it isn't explicitly specified.
4271 @Override
4272 boolean isEligibleForNewTasks() {
4273 return false;
4274 }
4275
Craig Mautnerd163e752014-06-13 17:18:47 -07004276 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004277 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004278 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4279 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4280 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4281 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4282 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004283 }
4284 }
4285
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4287 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004288 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004289 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004290 int mDisplayId;
4291 Display mDisplay;
4292 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004293
Craig Mautner4a1cb222013-12-04 16:14:06 -08004294 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4295 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004296 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004297
Jose Lima4b6c6692014-08-12 17:41:12 -07004298 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004299
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004300 ActivityDisplay() {
4301 }
Craig Mautner4504de52013-12-20 09:06:56 -08004302
Craig Mautner1a70a162014-09-13 12:09:31 -07004303 // After instantiation, check that mDisplay is not null before using this. The alternative
4304 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004305 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004306 final Display display = mDisplayManager.getDisplay(displayId);
4307 if (display == null) {
4308 return;
4309 }
4310 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004311 }
4312
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004313 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004314 mDisplay = display;
4315 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004316 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004317 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004318
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004319 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004320 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004321 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4322 + " onTop=" + onTop);
4323 if (onTop) {
4324 mStacks.add(stack);
4325 } else {
4326 mStacks.add(0, stack);
4327 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004328 }
4329
Craig Mautnere0a38842013-12-16 16:14:02 -08004330 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004331 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004332 + " from displayId=" + mDisplayId);
4333 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004334 }
4335
Jose Lima4b6c6692014-08-12 17:41:12 -07004336 void setVisibleBehindActivity(ActivityRecord r) {
4337 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004338 }
4339
Jose Lima4b6c6692014-08-12 17:41:12 -07004340 boolean hasVisibleBehindActivity() {
4341 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004342 }
4343
Craig Mautner34b73df2014-01-12 21:11:08 -08004344 @Override
4345 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004346 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4347 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004348
4349 @Override
4350 protected int getChildCount() {
4351 return mStacks.size();
4352 }
4353
4354 @Override
4355 protected ConfigurationContainer getChildAt(int index) {
4356 return mStacks.get(index);
4357 }
4358
4359 @Override
4360 protected ConfigurationContainer getParent() {
4361 return ActivityStackSupervisor.this;
4362 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004363 }
4364
4365 class VirtualActivityDisplay extends ActivityDisplay {
4366 VirtualDisplay mVirtualDisplay;
4367
Craig Mautner6985bad2014-04-21 15:22:06 -07004368 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004369 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004370 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4371 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4372 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4373 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004374
4375 init(mVirtualDisplay.getDisplay());
4376
4377 mWindowManager.handleDisplayAdded(mDisplayId);
4378 }
4379
4380 void setSurface(Surface surface) {
4381 if (mVirtualDisplay != null) {
4382 mVirtualDisplay.setSurface(surface);
4383 }
4384 }
4385
4386 @Override
4387 void detachActivitiesLocked(ActivityStack stack) {
4388 super.detachActivitiesLocked(stack);
4389 if (mVirtualDisplay != null) {
4390 mVirtualDisplay.release();
4391 mVirtualDisplay = null;
4392 }
4393 }
4394
4395 @Override
4396 public String toString() {
4397 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004398 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004399 }
Jose Lima58e66d62014-05-27 20:00:27 -07004400
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004401 ActivityStack findStackBehind(ActivityStack stack) {
4402 // TODO(multi-display): We are only looking for stacks on the default display.
4403 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4404 if (display == null) {
4405 return null;
4406 }
4407 final ArrayList<ActivityStack> stacks = display.mStacks;
4408 for (int i = stacks.size() - 1; i >= 0; i--) {
4409 if (stacks.get(i) == stack && i > 0) {
4410 return stacks.get(i - 1);
4411 }
4412 }
4413 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4414 + " in=" + stacks);
4415 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004416
Jorim Jaggic69bd222016-03-15 14:38:37 +01004417 /**
4418 * Puts a task into resizing mode during the next app transition.
4419 *
4420 * @param taskId the id of the task to put into resizing mode
4421 */
4422 private void setResizingDuringAnimation(int taskId) {
4423 mResizingTasksDuringAnimation.add(taskId);
4424 mWindowManager.setTaskDockedResizing(taskId, true);
4425 }
4426
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004427 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4428 final TaskRecord task;
4429 final int callingUid;
4430 final String callingPackage;
4431 final Intent intent;
4432 final int userId;
4433 final ActivityOptions activityOptions = (bOptions != null)
4434 ? new ActivityOptions(bOptions) : null;
4435 final int launchStackId = (activityOptions != null)
4436 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004437 if (launchStackId == HOME_STACK_ID) {
4438 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4439 + taskId + " can't be launch in the home stack.");
4440 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004441
4442 if (launchStackId == DOCKED_STACK_ID) {
4443 mWindowManager.setDockedStackCreateState(
4444 activityOptions.getDockCreateMode(), null /* initialBounds */);
4445
4446 // Defer updating the stack in which recents is until the app transition is done, to
4447 // not run into issues where we still need to draw the task in recents but the
4448 // docked stack is already created.
4449 deferUpdateBounds(HOME_STACK_ID);
4450 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4451 }
4452
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004453 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4454 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004455 continueUpdateBounds(HOME_STACK_ID);
4456 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004457 throw new IllegalArgumentException(
4458 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4459 }
4460
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004461 // Since we don't have an actual source record here, we assume that the currently focused
4462 // activity was the source.
4463 final ActivityStack focusedStack = getFocusedStack();
4464 final ActivityRecord sourceRecord =
4465 focusedStack != null ? focusedStack.topActivity() : null;
4466
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004467 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004468 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004469 moveTaskToStackLocked(
4470 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4471 ANIMATE);
4472 }
4473 }
4474
4475 // If the user must confirm credentials (e.g. when first launching a work app and the
4476 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4477 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4478 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004479 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004480 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004481 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004482 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4483 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004484
4485 // If we are launching the task in the docked stack, put it into resizing mode so
4486 // the window renders full-screen with the background filling the void. Also only
4487 // call this at the end to make sure that tasks exists on the window manager side.
4488 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004489 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004490 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004491
4492 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4493 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004494 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4495 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004496 return ActivityManager.START_TASK_TO_FRONT;
4497 }
4498 callingUid = task.mCallingUid;
4499 callingPackage = task.mCallingPackage;
4500 intent = task.intent;
4501 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4502 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004503 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4504 null, null, 0, 0, bOptions, userId, null, task);
4505 if (launchStackId == DOCKED_STACK_ID) {
4506 setResizingDuringAnimation(task.taskId);
4507 }
4508 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004509 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004510
4511 /**
4512 * @return a list of activities which are the top ones in each visible stack. The first
4513 * entry will be the focused activity.
4514 */
4515 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004516 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4517 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Amith Yamasanie8222e52016-04-08 15:28:47 -07004518 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4519 if (display == null) {
4520 return Collections.EMPTY_LIST;
4521 }
4522 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4523 final ArrayList<ActivityStack> stacks = display.mStacks;
4524 for (int i = stacks.size() - 1; i >= 0; i--) {
4525 ActivityStack stack = stacks.get(i);
4526 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4527 ActivityRecord top = stack.topActivity();
4528 if (top != null) {
4529 if (stack == mFocusedStack) {
4530 topActivityTokens.add(0, top.appToken);
4531 } else {
4532 topActivityTokens.add(top.appToken);
4533 }
4534 }
4535 }
4536 }
4537 return topActivityTokens;
4538 }
Craig Mautner27084302013-03-25 08:05:25 -07004539}