blob: b98bc4823835c24ca508fd6475878a7bc4587324 [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
422 protected ConfigurationContainer getChildAt(int index) {
423 return mActivityDisplays.valueAt(index);
424 }
425
426 @Override
427 protected ConfigurationContainer getParent() {
428 return null;
429 }
430
Wale Ogunwale39381972015-12-17 17:15:29 -0800431 static class FindTaskResult {
432 ActivityRecord r;
433 boolean matchedByRootAffinity;
434 }
435 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
436
Craig Mautneree36c772014-07-16 14:56:05 -0700437 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100438 * Used to keep track whether app visibilities got changed since the last pause. Useful to
439 * determine whether to invoke the task stack change listener after pausing.
440 */
441 boolean mAppVisibilitiesChangedSinceLastPause;
442
443 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100444 * Set of tasks that are in resizing mode during an app transition to fill the "void".
445 */
446 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
447
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700448
449 /**
450 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
451 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
452 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
453 * like the docked stack going empty.
454 */
455 private boolean mAllowDockedStackResize = true;
456
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100457 /**
Tony Mak853304c2016-04-18 15:17:41 +0100458 * Is dock currently minimized.
459 */
460 boolean mIsDockMinimized;
461
462 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700463 * Description of a request to start a new activity, which has been held
464 * due to app switches being disabled.
465 */
466 static class PendingActivityLaunch {
467 final ActivityRecord r;
468 final ActivityRecord sourceRecord;
469 final int startFlags;
470 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700471 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700472
473 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700474 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700475 r = _r;
476 sourceRecord = _sourceRecord;
477 startFlags = _startFlags;
478 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700479 callerApp = _callerApp;
480 }
481
482 void sendErrorResult(String message) {
483 try {
484 if (callerApp.thread != null) {
485 callerApp.thread.scheduleCrash(message);
486 }
487 } catch (RemoteException e) {
488 Slog.e(TAG, "Exception scheduling crash of failed "
489 + "activity launcher sourceRecord=" + sourceRecord, e);
490 }
Craig Mautneree36c772014-07-16 14:56:05 -0700491 }
492 }
493
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800494 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700495 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700496 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800497 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggidc249c42015-12-15 14:57:31 -0800498 mResizeDockedStackTimeout = new ResizeDockedStackTimeout(service, this, mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700499 }
500
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800501 void setRecentTasks(RecentTasks recentTasks) {
502 mRecentTasks = recentTasks;
503 }
504
Jeff Brown2c43c332014-06-12 22:38:59 -0700505 /**
506 * At the time when the constructor runs, the power manager has not yet been
507 * initialized. So we initialize our wakelocks afterwards.
508 */
509 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800510 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700511 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700512 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700513 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700514 }
515
justinzhang5286d3f2014-05-12 17:06:01 -0400516 // This function returns a IStatusBarService. The value is from ServiceManager.
517 // getService and is cached.
518 private IStatusBarService getStatusBarService() {
519 synchronized (mService) {
520 if (mStatusBarService == null) {
521 mStatusBarService = IStatusBarService.Stub.asInterface(
522 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
523 if (mStatusBarService == null) {
524 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
525 }
526 }
527 return mStatusBarService;
528 }
529 }
530
Jason Monk35c62a42014-06-17 10:24:47 -0400531 private IDevicePolicyManager getDevicePolicyManager() {
532 synchronized (mService) {
533 if (mDevicePolicyManager == null) {
534 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
535 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
536 if (mDevicePolicyManager == null) {
537 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
538 }
539 }
540 return mDevicePolicyManager;
541 }
542 }
543
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700544 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 synchronized (mService) {
546 mWindowManager = wm;
547
548 mDisplayManager =
549 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
550 mDisplayManager.registerDisplayListener(this, null);
551
552 Display[] displays = mDisplayManager.getDisplays();
553 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
554 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800555 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700556 if (activityDisplay.mDisplay == null) {
557 throw new IllegalStateException("Default Display does not exist");
558 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800559 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700560 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 }
562
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800563 mHomeStack = mFocusedStack = mLastFocusedStack =
564 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800565
566 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800567 }
Craig Mautner27084302013-03-25 08:05:25 -0700568 }
569
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200570 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700571 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200572 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700573 }
574
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700575 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800576 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700577 }
578
Craig Mautnerde4ef022013-04-07 19:01:33 -0700579 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700581 }
582
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700583 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700584 if (stack == null) {
585 return false;
586 }
587
Craig Mautnerdf88d732014-01-27 09:21:32 -0800588 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
589 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700590 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800591 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800592 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700593 }
594
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700595 /** The top most stack. */
596 boolean isFrontStack(ActivityStack stack) {
597 if (stack == null) {
598 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800599 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700600
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700601 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
602 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700603 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800604 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700605 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
606 }
607
Wale Ogunwaled046a012015-12-24 13:05:59 -0800608 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800609 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
610 if (!focusCandidate.isFocusable()) {
611 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
612 focusCandidate = focusCandidate.getNextFocusableStackLocked();
613 }
614
615 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800616 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800617 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800618
Wale Ogunwaled046a012015-12-24 13:05:59 -0800619 EventLogTags.writeAmFocusedStack(
620 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
621 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
622 }
623
624 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800625 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800626 if (r != null && r.idle) {
627 checkFinishBootingLocked();
628 }
629 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 }
631
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700632 void moveHomeStackToFront(String reason) {
633 mHomeStack.moveToFront(reason);
634 }
635
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700636 /** Returns true if the focus activity was adjusted to the home stack top activity. */
637 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700638 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700639 mWindowManager.showRecentApps(false /* fromHome */);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700640 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700641 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700642
Craig Mautner84984fa2014-06-19 11:19:20 -0700643 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700644
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700645 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700646 if (top == null) {
647 return false;
648 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700649 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700650 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700651 }
652
Craig Mautner299f9602015-01-26 09:47:33 -0800653 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700654 if (!mService.mBooting && !mService.mBooted) {
655 // Not ready yet!
656 return false;
657 }
658
Craig Mautner84984fa2014-06-19 11:19:20 -0700659 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700660 mWindowManager.showRecentApps(false /* fromHome */);
Craig Mautner84984fa2014-06-19 11:19:20 -0700661 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700662 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700663
Craig Mautner84984fa2014-06-19 11:19:20 -0700664 if (prev != null) {
665 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
666 }
667
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700668 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
669 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800670 final String myReason = reason + " resumeHomeStackTask";
671
Mark Lua56ea122015-10-08 13:31:01 +0800672 // Only resume home activity if isn't finishing.
673 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700674 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800675 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700676 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800677 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700678 }
679
Craig Mautner8d341ef2013-03-26 09:03:27 -0700680 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700681 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700682 }
683
684 /**
685 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
686 * @param id Id of the task we would like returned.
687 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
688 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700689 * @param stackId The stack to restore the task to (default launch stack will be used if
690 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700691 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700692 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800693 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800695 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
697 ActivityStack stack = stacks.get(stackNdx);
698 TaskRecord task = stack.taskForIdLocked(id);
699 if (task != null) {
700 return task;
701 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700702 }
703 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800704
705 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800707 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800708 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700709 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800710 return null;
711 }
712
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700713 if (!restoreFromRecents) {
714 return task;
715 }
716
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700717 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700718 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
719 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800720 return null;
721 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700722 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800723 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700724 }
725
Craig Mautner6170f732013-04-02 13:05:23 -0700726 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800729 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
731 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
732 if (r != null) {
733 return r;
734 }
Craig Mautner6170f732013-04-02 13:05:23 -0700735 }
736 }
737 return null;
738 }
739
Tony Mak853304c2016-04-18 15:17:41 +0100740 /**
741 * TODO: Handle freefom mode.
742 * @return true when credential confirmation is needed for the user and there is any
743 * activity started by the user in any visible stack.
744 */
745 boolean isUserLockedProfile(@UserIdInt int userId) {
746 if (!mService.mUserController.shouldConfirmCredentials(userId)) {
747 return false;
748 }
749 final ActivityStack fullScreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
750 final ActivityStack dockedStack = getStack(DOCKED_STACK_ID);
751 final ActivityStack[] activityStacks = new ActivityStack[] {fullScreenStack, dockedStack};
752 for (final ActivityStack activityStack : activityStacks) {
753 if (activityStack == null) {
754 continue;
755 }
756 if (activityStack.topRunningActivityLocked() == null) {
757 continue;
758 }
759 if (activityStack.getStackVisibilityLocked(null) == STACK_INVISIBLE) {
760 continue;
761 }
762 if (activityStack.isDockedStack() && mIsDockMinimized) {
763 continue;
764 }
765 final TaskRecord topTask = activityStack.topTask();
766 if (topTask == null) {
767 continue;
768 }
769 // To handle the case that work app is in the task but just is not the top one.
770 for (int i = topTask.mActivities.size() - 1; i >= 0; i--) {
771 final ActivityRecord activityRecord = topTask.mActivities.get(i);
772 if (activityRecord.userId == userId) {
773 return true;
774 }
775 }
776 }
777 return false;
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000778 }
779
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800780 void setNextTaskIdForUserLocked(int taskId, int userId) {
781 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
782 if (taskId > currentTaskId) {
783 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700784 }
785 }
786
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700787 static int nextTaskIdForUser(int taskId, int userId) {
788 int nextTaskId = taskId + 1;
789 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
790 // Wrap around as there will be smaller task ids that are available now.
791 nextTaskId -= MAX_TASK_IDS_PER_USER;
792 }
793 return nextTaskId;
794 }
795
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800796 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800797 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
798 // for a userId u, a taskId can only be in the range
799 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
800 // 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 -0700801 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800802 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
803 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
804 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700805 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800806 if (candidateTaskId == currentTaskId) {
807 // Something wrong!
808 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
809 throw new IllegalStateException("Cannot get an available task id."
810 + " Reached limit of " + MAX_TASK_IDS_PER_USER
811 + " running tasks per user.");
812 }
813 }
814 mCurTaskIdForUser.put(userId, candidateTaskId);
815 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700816 }
817
Chong Zhang6cda19c2016-06-14 19:07:56 -0700818 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800819 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700820 if (stack == null) {
821 return null;
822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700823 ActivityRecord resumedActivity = stack.mResumedActivity;
824 if (resumedActivity == null || resumedActivity.app == null) {
825 resumedActivity = stack.mPausingActivity;
826 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700827 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700828 }
829 }
830 return resumedActivity;
831 }
832
Dianne Hackbornff072722014-09-24 10:56:28 -0700833 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700834 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800835 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800836 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
837 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
839 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700840 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800841 continue;
842 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700843 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 if (hr != null) {
845 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
846 && processName.equals(hr.processName)) {
847 try {
George Mount2c92c972014-03-20 09:38:23 -0700848 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 didSomething = true;
850 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700851 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800852 Slog.w(TAG, "Exception in new application when starting activity "
853 + hr.intent.getComponent().flattenToShortString(), e);
854 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700855 }
Craig Mautner20e72272013-04-01 13:45:53 -0700856 }
Craig Mautner20e72272013-04-01 13:45:53 -0700857 }
858 }
859 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700860 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700861 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700862 }
Craig Mautner20e72272013-04-01 13:45:53 -0700863 return didSomething;
864 }
865
866 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800867 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
868 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
870 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700871 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 continue;
873 }
874 final ActivityRecord resumedActivity = stack.mResumedActivity;
875 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700876 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800877 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 return false;
879 }
Craig Mautner20e72272013-04-01 13:45:53 -0700880 }
881 }
Wei Wang65c7a152016-06-02 18:51:22 -0700882 // Send launch end powerhint when idle
883 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700884 return true;
885 }
886
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800888 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
889 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
891 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700892 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700894 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800895 return false;
896 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 }
898 }
899 }
900 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700901 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800902 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
903 mLastFocusedStack + " to=" + mFocusedStack);
904 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700905 return true;
906 }
907
908 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800909 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
911 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
913 final ActivityStack stack = stacks.get(stackNdx);
914 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800915 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700916 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800917 return false;
918 }
919 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700921 }
922 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800923 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700924 }
925
Craig Mautner2acc3892013-09-23 10:28:14 -0700926 /**
927 * Pause all activities in either all of the stacks or just the back stacks.
928 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700929 * @param resuming The resuming activity.
930 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
931 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700932 * @return true if any activity was paused as a result of this call.
933 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700934 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700935 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800936 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
937 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
939 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700940 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700941 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700943 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
944 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700946 }
947 }
948 return someActivityPaused;
949 }
950
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700952 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
954 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
956 final ActivityStack stack = stacks.get(stackNdx);
957 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700958 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700960 Slog.d(TAG_STATES,
961 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 pausing = false;
963 } else {
964 return false;
965 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700966 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700967 }
968 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700969 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700970 }
971
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -0700973 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500974 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
976 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
977 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
978 final ActivityStack stack = stacks.get(stackNdx);
979 if (stack.mResumedActivity != null &&
980 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700981 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800982 }
983 }
984 }
985 }
986
Wale Ogunwale2be760d2016-02-17 11:16:10 -0800987 void cancelInitializingActivities() {
988 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
989 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
990 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
991 stacks.get(stackNdx).cancelInitializingActivities();
992 }
993 }
994 }
995
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700996 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700997 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700998 }
999
1000 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1001 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001002 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001003 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001004 if (w.who == null) {
1005 changed = true;
1006 w.timeout = false;
1007 if (r != null) {
1008 w.who = new ComponentName(r.info.packageName, r.info.name);
1009 }
1010 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1011 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001012 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001013 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001014 if (changed) {
1015 mService.notifyAll();
1016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001017 }
1018
Chong Zhang5022da32016-06-21 16:31:37 -07001019 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1020 boolean changed = false;
1021 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1022 WaitResult w = mWaitingActivityLaunched.remove(i);
1023 if (w.who == null) {
1024 changed = true;
1025 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1026 // the starting activity ends up moving another activity to front, and it should
1027 // wait for this new activity to become visible instead.
1028 // Do not modify other fields.
1029 w.result = START_TASK_TO_FRONT;
1030 }
1031 }
1032 if (changed) {
1033 mService.notifyAll();
1034 }
1035 }
1036
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001037 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1038 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001039 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001041 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001042 if (w.who == null) {
1043 changed = true;
1044 w.timeout = timeout;
1045 if (r != null) {
1046 w.who = new ComponentName(r.info.packageName, r.info.name);
1047 }
1048 w.thisTime = thisTime;
1049 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001050 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001051 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001052 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001053 if (changed) {
1054 mService.notifyAll();
1055 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001056 }
1057
Craig Mautner29219d92013-04-16 20:19:12 -07001058 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001059 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001060 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001061 if (r != null) {
1062 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001063 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001064
Andrii Kulian7318d632016-07-20 18:59:28 -07001065 // Look in other non-focused and non-home stacks.
Craig Mautnere0a38842013-12-16 16:14:02 -08001066 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001067 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1068 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001069 if (stack != focusedStack && isFrontStack(stack) && stack.isFocusable()) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001070 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -07001071 if (r != null) {
1072 return r;
1073 }
1074 }
1075 }
1076 return null;
1077 }
1078
Dianne Hackborn09233282014-04-30 11:33:59 -07001079 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001080 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001081 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1082 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001083 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001084 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001085 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001086 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1087 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001088 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001089 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001090 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001091 }
1092 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001093
1094 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1095 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1096 while (maxNum > 0) {
1097 long mostRecentActiveTime = Long.MIN_VALUE;
1098 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 final int numTaskLists = runningTaskLists.size();
1100 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1101 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001102 if (!stackTaskList.isEmpty()) {
1103 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1104 if (lastActiveTime > mostRecentActiveTime) {
1105 mostRecentActiveTime = lastActiveTime;
1106 selectedStackList = stackTaskList;
1107 }
1108 }
1109 }
1110 if (selectedStackList != null) {
1111 list.add(selectedStackList.remove(0));
1112 --maxNum;
1113 } else {
1114 break;
1115 }
1116 }
Craig Mautner20e72272013-04-01 13:45:53 -07001117 }
1118
Todd Kennedy7440f172015-12-09 14:31:22 -08001119 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1120 ProfilerInfo profilerInfo) {
1121 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001122 if (aInfo != null) {
1123 // Store the found target back into the intent, because now that
1124 // we have it we never want to do this again. For example, if the
1125 // user navigates back to this point in the history, we should
1126 // always restart the exact same activity.
1127 intent.setComponent(new ComponentName(
1128 aInfo.applicationInfo.packageName, aInfo.name));
1129
1130 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001131 if (!aInfo.processName.equals("system")) {
1132 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001133 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001134 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001135
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001136 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1137 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1138 }
1139
Man Caocfa78b22015-06-11 20:14:34 -07001140 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1141 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1142 }
1143
1144 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001145 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001146 }
1147 }
1148 }
1149 return aInfo;
1150 }
1151
Todd Kennedy7440f172015-12-09 14:31:22 -08001152 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001153 return resolveIntent(intent, resolvedType, userId, 0);
1154 }
1155
1156 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy7440f172015-12-09 14:31:22 -08001157 try {
1158 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
Kenny Guyb1b30262016-02-09 16:02:35 +00001159 PackageManager.MATCH_DEFAULT_ONLY | flags
1160 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001161 } catch (RemoteException e) {
1162 }
1163 return null;
1164 }
1165
1166 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1167 ProfilerInfo profilerInfo, int userId) {
1168 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1169 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1170 }
1171
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001172 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1173 boolean andResume, boolean checkConfig) throws RemoteException {
1174
1175 if (!allPausedActivitiesComplete()) {
1176 // While there are activities pausing we skipping starting any new activities until
1177 // pauses are complete. NOTE: that we also do this for activities that are starting in
1178 // the paused state because they will first be resumed then paused on the client side.
1179 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1180 "realStartActivityLocked: Skipping start of r=" + r
1181 + " some activities pausing...");
1182 return false;
1183 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001184
Craig Mautner2568c3a2015-03-26 14:22:34 -07001185 if (andResume) {
1186 r.startFreezingScreenLocked(app, 0);
1187 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001188
Craig Mautner2568c3a2015-03-26 14:22:34 -07001189 // schedule launch ticks to collect information about slow apps.
1190 r.startLaunchTickingLocked();
1191 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001192
1193 // Have the window manager re-evaluate the orientation of
1194 // the screen based on the new activity order. Note that
1195 // as a result of this, it can call back into the activity
1196 // manager with a new orientation. We don't care about that,
1197 // because the activity is not currently running so we are
1198 // just restarting it anyway.
1199 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001200 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Andrii Kulian1779e612016-10-12 21:58:25 -07001201 mService.getGlobalConfiguration(),
Craig Mautner2420ead2013-04-01 17:13:20 -07001202 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001203 // Deferring resume here because we're going to launch new activity shortly.
1204 // We don't want to perform a redundant launch of the same record while ensuring
1205 // configurations and trying to resume top activity of focused stack.
1206 mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 }
1208
1209 r.app = app;
1210 app.waitingToKill = null;
1211 r.launchCount++;
1212 r.lastLaunchTime = SystemClock.uptimeMillis();
1213
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001214 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
1216 int idx = app.activities.indexOf(r);
1217 if (idx < 0) {
1218 app.activities.add(r);
1219 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001220 mService.updateLruProcessLocked(app, true, null);
1221 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001222
Craig Mautner15df08a2015-04-01 12:17:18 -07001223 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001224 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1225 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001226 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001227 }
1228
Andrii Kulian02b7a832016-10-06 23:11:56 -07001229 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 try {
1231 if (app.thread == null) {
1232 throw new RemoteException();
1233 }
1234 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001235 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 if (andResume) {
1237 results = r.results;
1238 newIntents = r.newIntents;
1239 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001240 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1241 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1242 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 if (andResume) {
1244 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1245 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001248 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001249 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001250 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001252 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1253 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 r.sleeping = false;
1255 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001256 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001257 mService.showAskCompatModeDialogLocked(r);
1258 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001259 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1261 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1262 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001263 final String profileFile = mService.mProfileFile;
1264 if (profileFile != null) {
1265 ParcelFileDescriptor profileFd = mService.mProfileFd;
1266 if (profileFd != null) {
1267 try {
1268 profileFd = profileFd.dup();
1269 } catch (IOException e) {
1270 if (profileFd != null) {
1271 try {
1272 profileFd.close();
1273 } catch (IOException o) {
1274 }
1275 profileFd = null;
1276 }
1277 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001279
1280 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1281 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 }
1283 }
1284 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001285
Craig Mautner2568c3a2015-03-26 14:22:34 -07001286 if (andResume) {
1287 app.hasShownUi = true;
1288 app.pendingUiClean = true;
1289 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001290 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001291 // Because we could be starting an Activity in the system process this may not go across
1292 // a Binder interface which would create a new Configuration. Consequently we have to
1293 // always create a new Configuration here.
Craig Mautner2420ead2013-04-01 17:13:20 -07001294 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001295 System.identityHashCode(r), r.info,
Andrii Kulian1779e612016-10-12 21:58:25 -07001296 new Configuration(mService.getGlobalConfiguration()),
1297 new Configuration(task.getMergedOverrideConfiguration()), r.compat,
1298 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1299 r.persistentState, results, newIntents, !andResume,
1300 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001302 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 // This may be a heavy-weight process! Note that the package
1304 // manager will ensure that only activity can run in the main
1305 // process of the .apk, which is the only thing that will be
1306 // considered heavy-weight.
1307 if (app.processName.equals(app.info.packageName)) {
1308 if (mService.mHeavyWeightProcess != null
1309 && mService.mHeavyWeightProcess != app) {
1310 Slog.w(TAG, "Starting new heavy weight process " + app
1311 + " when already running "
1312 + mService.mHeavyWeightProcess);
1313 }
1314 mService.mHeavyWeightProcess = app;
1315 Message msg = mService.mHandler.obtainMessage(
1316 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1317 msg.obj = r;
1318 mService.mHandler.sendMessage(msg);
1319 }
1320 }
1321
1322 } catch (RemoteException e) {
1323 if (r.launchFailed) {
1324 // This is the second time we failed -- finish activity
1325 // and give up.
1326 Slog.e(TAG, "Second failure launching "
1327 + r.intent.getComponent().flattenToShortString()
1328 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001329 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1331 "2nd-crash", false);
1332 return false;
1333 }
1334
1335 // This is the first time we failed -- restart process and
1336 // retry.
1337 app.activities.remove(r);
1338 throw e;
1339 }
1340
1341 r.launchFailed = false;
1342 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001343 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 }
1345
1346 if (andResume) {
1347 // As part of the process of launching, ActivityThread also performs
1348 // a resume.
1349 stack.minimalResumeActivityLocked(r);
1350 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001351 // This activity is not starting in the resumed state... which should look like we asked
1352 // it to pause+stop (but remain visible), and it has done so and reported back the
1353 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001354 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001355 "Moving to PAUSED: " + r + " (starting in paused state)");
1356 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001357 }
1358
1359 // Launch the new version setup screen if needed. We do this -after-
1360 // launching the initial activity (that is, home), so that it can have
1361 // a chance to initialize itself while in the background, making the
1362 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001363 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001364 mService.startSetupActivityLocked();
1365 }
1366
Dianne Hackborn465fa392014-09-14 14:21:18 -07001367 // Update any services we are bound to that might care about whether
1368 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001369 if (r.app != null) {
1370 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1371 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001372
Craig Mautner2420ead2013-04-01 17:13:20 -07001373 return true;
1374 }
1375
Craig Mautnere79d42682013-04-01 19:01:53 -07001376 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001377 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001378 // Is this activity's application already running?
1379 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001380 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001381
Andrii Kulian02b7a832016-10-06 23:11:56 -07001382 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001383
1384 if (app != null && app.thread != null) {
1385 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001386 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1387 || !"android".equals(r.info.packageName)) {
1388 // Don't add this if it is a platform component that is marked
1389 // to run in multiple processes, because this is actually
1390 // part of the framework so doesn't make sense to track as a
1391 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001392 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1393 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001394 }
George Mount2c92c972014-03-20 09:38:23 -07001395 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001396 return;
1397 } catch (RemoteException e) {
1398 Slog.w(TAG, "Exception when starting activity "
1399 + r.intent.getComponent().flattenToShortString(), e);
1400 }
1401
1402 // If a dead object exception was thrown -- fall through to
1403 // restart the application.
1404 }
1405
1406 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001407 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001408 }
1409
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001410 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001411 String resultWho, int requestCode, int callingPid, int callingUid,
1412 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001413 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001414 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1415 callingUid);
1416 if (startAnyPerm == PERMISSION_GRANTED) {
1417 return true;
1418 }
1419 final int componentRestriction = getComponentRestrictionForCallingPackage(
1420 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1421 final int actionRestriction = getActionRestrictionForCallingPackage(
1422 intent.getAction(), callingPackage, callingPid, callingUid);
1423 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1424 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1425 if (resultRecord != null) {
1426 resultStack.sendActivityResultLocked(-1,
1427 resultRecord, resultWho, requestCode,
1428 Activity.RESULT_CANCELED, null);
1429 }
1430 final String msg;
1431 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1432 msg = "Permission Denial: starting " + intent.toString()
1433 + " from " + callerApp + " (pid=" + callingPid
1434 + ", uid=" + callingUid + ")" + " with revoked permission "
1435 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1436 } else if (!aInfo.exported) {
1437 msg = "Permission Denial: starting " + intent.toString()
1438 + " from " + callerApp + " (pid=" + callingPid
1439 + ", uid=" + callingUid + ")"
1440 + " not exported from uid " + aInfo.applicationInfo.uid;
1441 } else {
1442 msg = "Permission Denial: starting " + intent.toString()
1443 + " from " + callerApp + " (pid=" + callingPid
1444 + ", uid=" + callingUid + ")"
1445 + " requires " + aInfo.permission;
1446 }
1447 Slog.w(TAG, msg);
1448 throw new SecurityException(msg);
1449 }
1450
1451 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1452 final String message = "Appop Denial: starting " + intent.toString()
1453 + " from " + callerApp + " (pid=" + callingPid
1454 + ", uid=" + callingUid + ")"
1455 + " requires " + AppOpsManager.permissionToOp(
1456 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1457 Slog.w(TAG, message);
1458 return false;
1459 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1460 final String message = "Appop Denial: starting " + intent.toString()
1461 + " from " + callerApp + " (pid=" + callingPid
1462 + ", uid=" + callingUid + ")"
1463 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1464 Slog.w(TAG, message);
1465 return false;
1466 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001467 if (options != null && options.getLaunchTaskId() != -1) {
1468 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1469 callingPid, callingUid);
1470 if (startInTaskPerm != PERMISSION_GRANTED) {
1471 final String msg = "Permission Denial: starting " + intent.toString()
1472 + " from " + callerApp + " (pid=" + callingPid
1473 + ", uid=" + callingUid + ") with launchTaskId="
1474 + options.getLaunchTaskId();
1475 Slog.w(TAG, msg);
1476 throw new SecurityException(msg);
1477 }
1478 }
1479
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001480 return true;
1481 }
1482
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001483 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001484 final long identity = Binder.clearCallingIdentity();
1485 try {
1486 return UserManager.get(mService.mContext).getUserInfo(userId);
1487 } finally {
1488 Binder.restoreCallingIdentity(identity);
1489 }
1490 }
1491
Svet Ganov99b60432015-06-27 13:15:22 -07001492 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001493 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001494 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1495 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001496 == PackageManager.PERMISSION_DENIED) {
1497 return ACTIVITY_RESTRICTION_PERMISSION;
1498 }
1499
Christopher Tateff7add02015-08-17 10:23:22 -07001500 if (activityInfo.permission == null) {
1501 return ACTIVITY_RESTRICTION_NONE;
1502 }
1503
Svet Ganov99b60432015-06-27 13:15:22 -07001504 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1505 if (opCode == AppOpsManager.OP_NONE) {
1506 return ACTIVITY_RESTRICTION_NONE;
1507 }
1508
1509 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1510 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001511 if (!ignoreTargetSecurity) {
1512 return ACTIVITY_RESTRICTION_APPOP;
1513 }
Svet Ganov99b60432015-06-27 13:15:22 -07001514 }
1515
1516 return ACTIVITY_RESTRICTION_NONE;
1517 }
1518
Svetoslav7008b512015-06-24 18:47:07 -07001519 private int getActionRestrictionForCallingPackage(String action,
1520 String callingPackage, int callingPid, int callingUid) {
1521 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001522 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001523 }
1524
1525 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1526 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001527 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001528 }
1529
1530 final PackageInfo packageInfo;
1531 try {
1532 packageInfo = mService.mContext.getPackageManager()
1533 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1534 } catch (PackageManager.NameNotFoundException e) {
1535 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001536 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001537 }
1538
1539 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001540 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001541 }
1542
1543 if (mService.checkPermission(permission, callingPid, callingUid) ==
1544 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001545 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001546 }
1547
1548 final int opCode = AppOpsManager.permissionToOpCode(permission);
1549 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001550 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001551 }
1552
1553 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1554 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001555 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001556 }
1557
Svet Ganov99b60432015-06-27 13:15:22 -07001558 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001559 }
1560
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001561 void setLaunchSource(int uid) {
1562 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1563 }
1564
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001565 void acquireLaunchWakelock() {
1566 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1567 throw new IllegalStateException("Calling must be system uid");
1568 }
1569 mLaunchingActivity.acquire();
1570 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1571 // To be safe, don't allow the wake lock to be held for too long.
1572 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1573 }
1574 }
1575
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001576 /**
1577 * Called when the frontmost task is idle.
1578 * @return the state of mService.mBooting before this was called.
1579 */
1580 private boolean checkFinishBootingLocked() {
1581 final boolean booting = mService.mBooting;
1582 boolean enableScreen = false;
1583 mService.mBooting = false;
1584 if (!mService.mBooted) {
1585 mService.mBooted = true;
1586 enableScreen = true;
1587 }
1588 if (booting || enableScreen) {
1589 mService.postFinishBooting(booting, enableScreen);
1590 }
1591 return booting;
1592 }
1593
Craig Mautnerf3333272013-04-22 10:55:53 -07001594 // Checked.
1595 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1596 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001597 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001598
Craig Mautnerf3333272013-04-22 10:55:53 -07001599 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001600 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001601 int NS = 0;
1602 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001603 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001604 boolean activityRemoved = false;
1605
Wale Ogunwale7d701172015-03-11 15:36:30 -07001606 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001607 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001608 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1609 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001610 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1611 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001612 if (fromTimeout) {
1613 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001614 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001615
1616 // This is a hack to semi-deal with a race condition
1617 // in the client where it can be constructed with a
1618 // newer configuration from when we asked it to launch.
1619 // We'll update with whatever configuration it now says
1620 // it used to launch.
1621 if (config != null) {
Andrii Kulian1779e612016-10-12 21:58:25 -07001622 r.mLastReportedConfiguration.setTo(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001623 }
1624
1625 // We are now idle. If someone is waiting for a thumbnail from
1626 // us, we can now deliver.
1627 r.idle = true;
1628
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001629 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001630 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001631 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001632 }
1633 }
1634
1635 if (allResumedActivitiesIdle()) {
1636 if (r != null) {
1637 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001638 }
1639
1640 if (mLaunchingActivity.isHeld()) {
1641 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1642 if (VALIDATE_WAKE_LOCK_CALLER &&
1643 Binder.getCallingUid() != Process.myUid()) {
1644 throw new IllegalStateException("Calling must be system uid");
1645 }
1646 mLaunchingActivity.release();
1647 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001648 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001649 }
1650
1651 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001652 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07001653 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001654 if ((NF = mFinishingActivities.size()) > 0) {
1655 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001656 mFinishingActivities.clear();
1657 }
1658
Craig Mautnerf3333272013-04-22 10:55:53 -07001659 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001660 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001661 mStartingUsers.clear();
1662 }
1663
Craig Mautnerf3333272013-04-22 10:55:53 -07001664 // Stop any activities that are scheduled to do so but have been
1665 // waiting for the next one to start.
1666 for (int i = 0; i < NS; i++) {
1667 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001668 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001669 if (stack != null) {
1670 if (r.finishing) {
1671 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1672 } else {
1673 stack.stopActivityLocked(r);
1674 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001675 }
1676 }
1677
1678 // Finish any activities that are scheduled to do so but have been
1679 // waiting for the next one to start.
1680 for (int i = 0; i < NF; i++) {
1681 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001682 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001683 if (stack != null) {
1684 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1685 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001686 }
1687
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001688 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001689 // Complete user switch
1690 if (startingUsers != null) {
1691 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001692 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001693 }
1694 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001695 }
1696
1697 mService.trimApplications();
1698 //dump();
1699 //mWindowManager.dump();
1700
Craig Mautnerf3333272013-04-22 10:55:53 -07001701 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001702 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001703 }
1704
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001705 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001706 }
1707
Craig Mautner8e569572013-10-11 17:36:59 -07001708 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001709 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001710 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1711 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001712 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1713 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1714 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001715 }
Craig Mautner19091252013-10-05 00:03:53 -07001716 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001717 }
1718
1719 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1721 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1723 stacks.get(stackNdx).closeSystemDialogsLocked();
1724 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001725 }
1726 }
1727
Craig Mautner93529a42013-10-04 15:03:13 -07001728 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001729 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001730 }
1731
Craig Mautner8d341ef2013-03-26 09:03:27 -07001732 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001733 * Update the last used stack id for non-current user (current user's last
1734 * used stack is the focused stack)
1735 */
1736 void updateUserStackLocked(int userId, ActivityStack stack) {
1737 if (userId != mCurrentUser) {
1738 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1739 }
1740 }
1741
1742 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001743 * @return true if some activity was finished (or would have finished if doit were true).
1744 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001745 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1746 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001747 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001751 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001752 if (stack.finishDisabledPackageActivitiesLocked(
1753 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001754 didSomething = true;
1755 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001756 }
1757 }
1758 return didSomething;
1759 }
1760
Dianne Hackborna413dc02013-07-12 12:02:55 -07001761 void updatePreviousProcessLocked(ActivityRecord r) {
1762 // Now that this process has stopped, we may want to consider
1763 // it to be the previous app to try to keep around in case
1764 // the user wants to return to it.
1765
1766 // First, found out what is currently the foreground app, so that
1767 // we don't blow away the previous app if this activity is being
1768 // hosted by the process that is actually still the foreground.
1769 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001770 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1771 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001772 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1773 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001774 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001775 if (stack.mResumedActivity != null) {
1776 fgApp = stack.mResumedActivity.app;
1777 } else if (stack.mPausingActivity != null) {
1778 fgApp = stack.mPausingActivity.app;
1779 }
1780 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001781 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001782 }
1783 }
1784
1785 // Now set this one as the previous process, only if that really
1786 // makes sense to.
1787 if (r.app != null && fgApp != null && r.app != fgApp
1788 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001789 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001790 mService.mPreviousProcess = r.app;
1791 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1792 }
1793 }
1794
Wale Ogunwaled046a012015-12-24 13:05:59 -08001795 boolean resumeFocusedStackTopActivityLocked() {
1796 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001797 }
1798
Wale Ogunwaled046a012015-12-24 13:05:59 -08001799 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001800 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001801 if (targetStack != null && isFocusedStack(targetStack)) {
1802 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001803 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001804 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1805 if (r == null || r.state != RESUMED) {
1806 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
1807 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001808 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001809 }
1810
Todd Kennedy39bfee52016-02-24 10:28:21 -08001811 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1814 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1815 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1816 }
1817 }
1818 }
1819
Adrian Roos20d7df32016-01-12 18:59:43 +01001820 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1821 TaskRecord finishedTask = null;
1822 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001823 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1824 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001825 final int numStacks = stacks.size();
1826 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1827 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01001828 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
1829 if (stack == focusedStack || finishedTask == null) {
1830 finishedTask = t;
1831 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001832 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001833 }
Adrian Roos20d7df32016-01-12 18:59:43 +01001834 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001835 }
1836
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07001837 void finishVoiceTask(IVoiceInteractionSession session) {
1838 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1839 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1840 final int numStacks = stacks.size();
1841 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
1842 final ActivityStack stack = stacks.get(stackNdx);
1843 stack.finishVoiceTask(session);
1844 }
1845 }
1846 }
1847
Andrii Kulianc27916642016-04-12 17:59:27 -07001848 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
1849 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001850 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
1851 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001852 }
Craig Mautneraea74a52014-03-08 14:23:10 -08001853 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
1854 // Caller wants the home activity moved with it. To accomplish this,
1855 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07001856 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08001857 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001858 ActivityStack currentStack = task.getStack();
1859 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001860 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
1861 + task + " to front. Stack is null");
1862 return;
1863 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001864
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001865 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001866 int stackId = options.getLaunchStackId();
1867 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001868 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07001869 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08001870 if (stackId == INVALID_STACK_ID) {
1871 stackId = task.getLaunchStackId();
1872 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001873 if (stackId != currentStack.mStackId) {
1874 currentStack = moveTaskToStackUncheckedLocked(task, stackId, ON_TOP,
1875 !FORCE_FOCUS, reason);
1876 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08001877 // moveTaskToStackUncheckedLocked() should already placed the task on top,
1878 // still need moveTaskToFrontLocked() below for any transition settings.
1879 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001880 if (StackId.resizeStackWithLaunchBounds(stackId)) {
1881 resizeStackLocked(stackId, bounds,
1882 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07001883 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001884 } else {
1885 // WM resizeTask must be done after the task is moved to the correct stack,
1886 // because Task's setBounds() also updates dim layer's bounds, but that has
1887 // dependency on the stack.
Andrii Kulian1779e612016-10-12 21:58:25 -07001888 mWindowManager.resizeTask(task.taskId, task.mBounds,
1889 task.getOverrideConfiguration(), false /* relayout */,
1890 false /* forced */);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08001891 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001892 }
1893 }
1894
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001895 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001896 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07001897 r == null ? null : r.appTimeTracker, reason);
1898
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001899 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07001900 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001901
Andrii Kulian02b7a832016-10-06 23:11:56 -07001902 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07001903 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001904 }
1905
Wale Ogunwale854809c2015-12-27 16:18:19 -08001906 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001907 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08001908 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08001909 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08001910 return false;
1911 }
1912 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
1913 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001914 }
1915
Craig Mautner967212c2013-04-13 21:10:58 -07001916 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07001917 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001918 }
1919
1920 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001921 ActivityContainer activityContainer = mActivityContainers.get(stackId);
1922 if (activityContainer != null) {
1923 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001924 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001925 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001926 return null;
1927 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07001928 // TODO(multi-display): Allow creating stacks on secondary displays.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001929 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07001930 }
1931
Craig Mautner967212c2013-04-13 21:10:58 -07001932 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001933 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001934 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1935 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001936 }
1937 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07001938 }
1939
Craig Mautneree2e45a2014-06-27 12:10:03 -07001940 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07001941 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001942 }
1943
1944 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001945 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
1946 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1947 final TaskRecord task = tasks.get(taskNdx);
1948 if (task.isHomeTask()) {
1949 final ArrayList<ActivityRecord> activities = task.mActivities;
1950 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1951 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001952 if (r.isHomeActivity()
1953 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001954 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 }
1956 }
1957 }
1958 }
1959 return null;
1960 }
1961
Chong Zhangb15758a2015-11-17 12:12:03 -08001962 /**
1963 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
1964 * the docked stack itself, or if it's side-by-side to the docked stack.
1965 */
1966 boolean isStackDockedInEffect(int stackId) {
1967 return stackId == DOCKED_STACK_ID ||
1968 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
1969 }
1970
Todd Kennedyca4d8422015-01-15 15:19:22 -08001971 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001972 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07001973 ActivityContainer activityContainer =
1974 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001975 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001976 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1977 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001978 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001979 return activityContainer;
1980 }
1981
Craig Mautner34b73df2014-01-12 21:11:08 -08001982 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001983 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
1984 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
1985 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001986 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
1987 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001988 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08001989 }
1990 }
1991
Craig Mautner95da1082014-02-24 17:54:35 -08001992 void deleteActivityContainer(IActivityContainer container) {
1993 ActivityContainer activityContainer = (ActivityContainer)container;
1994 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001995 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
1996 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08001997 final int stackId = activityContainer.mStackId;
1998 mActivityContainers.remove(stackId);
1999 mWindowManager.removeStack(stackId);
2000 }
2001 }
2002
Jorim Jaggidc249c42015-12-15 14:57:31 -08002003 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002004 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002005 if (stackId == DOCKED_STACK_ID) {
2006 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002007 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002008 return;
2009 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002010 final ActivityStack stack = getStack(stackId);
2011 if (stack == null) {
2012 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2013 return;
2014 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002015
Jorim Jaggidc249c42015-12-15 14:57:31 -08002016 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002017 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2018 // stack size changing so things don't get out of sync.
2019 return;
2020 }
2021
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002022 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002023 mWindowManager.deferSurfaceLayout();
2024 try {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002025 resizeStackUncheckedLocked(stack, bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002026 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002027 stack.ensureVisibleActivitiesConfigurationLocked(
2028 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002029 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002030 } finally {
2031 mWindowManager.continueSurfaceLayout();
2032 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002033 }
2034 }
2035
Jorim Jaggi192086e2016-03-11 17:17:03 +01002036 void deferUpdateBounds(int stackId) {
2037 final ActivityStack stack = getStack(stackId);
2038 if (stack != null) {
2039 stack.deferUpdateBounds();
2040 }
2041 }
2042
2043 void continueUpdateBounds(int stackId) {
2044 final ActivityStack stack = getStack(stackId);
2045 if (stack != null) {
2046 stack.continueUpdateBounds();
2047 }
2048 }
2049
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002050 void notifyAppTransitionDone() {
2051 continueUpdateBounds(HOME_STACK_ID);
2052 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2053 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwaled6aee182016-06-14 13:10:09 -07002054 if (anyTaskForIdLocked(taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002055 mWindowManager.setTaskDockedResizing(taskId, false);
2056 }
2057 }
2058 mResizingTasksDuringAnimation.clear();
2059 }
2060
Jorim Jaggi192086e2016-03-11 17:17:03 +01002061 void resizeStackUncheckedLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
Jorim Jaggidc249c42015-12-15 14:57:31 -08002062 Rect tempTaskInsetBounds) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002063 bounds = TaskRecord.validateBounds(bounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002064
Jorim Jaggi192086e2016-03-11 17:17:03 +01002065 if (!stack.updateBoundsAllowed(bounds, tempTaskBounds, tempTaskInsetBounds)) {
2066 return;
2067 }
2068
Andrii Kulian1e32e022016-09-16 15:29:34 -07002069 stack.updateOverrideConfiguration(bounds, tempTaskBounds, tempTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002070 }
2071
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002072 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
2073 final ActivityStack stack = getStack(fromStackId);
2074 if (stack == null) {
2075 return;
2076 }
2077
2078 mWindowManager.deferSurfaceLayout();
2079 try {
2080 if (fromStackId == DOCKED_STACK_ID) {
2081
2082 // We are moving all tasks from the docked stack to the fullscreen stack,
2083 // which is dismissing the docked stack, so resize all other stacks to
2084 // fullscreen here already so we don't end up with resize trashing.
2085 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2086 if (StackId.isResizeableByDockedStack(i)) {
2087 ActivityStack otherStack = getStack(i);
2088 if (otherStack != null) {
2089 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2090 true /* allowResizeInDockedMode */, DEFER_RESUME);
2091 }
2092 }
2093 }
2094
2095 // Also disable docked stack resizing since we have manually adjusted the
2096 // size of other stacks above and we don't want to trigger a docked stack
2097 // resize when we remove task from it below and it is detached from the
2098 // display because it no longer contains any tasks.
2099 mAllowDockedStackResize = false;
2100 }
2101 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2102 final int size = tasks.size();
2103 if (onTop) {
2104 for (int i = 0; i < size; i++) {
2105 moveTaskToStackLocked(tasks.get(i).taskId,
Wale Ogunwale3ad75d62016-04-18 16:14:18 -07002106 FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
Wale Ogunwale06579d62016-04-30 15:29:06 -07002107 "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002108 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002109
2110 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2111 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002112 } else {
2113 for (int i = size - 1; i >= 0; i--) {
2114 positionTaskInStackLocked(tasks.get(i).taskId,
2115 FULLSCREEN_WORKSPACE_STACK_ID, 0);
2116 }
2117 }
2118 } finally {
2119 mAllowDockedStackResize = true;
2120 mWindowManager.continueSurfaceLayout();
2121 }
2122 }
2123
Jorim Jaggidc249c42015-12-15 14:57:31 -08002124 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002125 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2126 boolean preserveWindows) {
2127 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2128 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2129 false /* deferResume */);
2130 }
2131
2132 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2133 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2134 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002135
2136 if (!mAllowDockedStackResize) {
2137 // Docked stack resize currently disabled.
2138 return;
2139 }
2140
Jorim Jaggidc249c42015-12-15 14:57:31 -08002141 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2142 if (stack == null) {
2143 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2144 return;
2145 }
2146
2147 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2148 mWindowManager.deferSurfaceLayout();
2149 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002150 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2151 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002152 ActivityRecord r = stack.topRunningActivityLocked();
2153 resizeStackUncheckedLocked(stack, dockedBounds, tempDockedTaskBounds,
2154 tempDockedTaskInsetBounds);
2155
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002156 // TODO: Checking for isAttached might not be needed as if the user passes in null
2157 // dockedBounds then they want the docked stack to be dismissed.
2158 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2159 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002160 // In this case we make all other static stacks fullscreen and move all
2161 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002162 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002163
2164 // stack shouldn't contain anymore activities, so nothing to resume.
2165 r = null;
2166 } else {
2167 // Docked stacks occupy a dedicated region on screen so the size of all other
2168 // static stacks need to be adjusted so they don't overlap with the docked stack.
2169 // We get the bounds to use from window manager which has been adjusted for any
2170 // screen controls and is also the same for all stacks.
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002171 mWindowManager.getStackDockedModeBounds(
2172 HOME_STACK_ID, tempRect, true /* ignoreVisibility */);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002173 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002174 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
2175 resizeStackLocked(i, tempRect, tempOtherTaskBounds,
2176 tempOtherTaskInsetBounds, preserveWindows,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002177 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002178 }
2179 }
2180 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002181 if (!deferResume) {
2182 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2183 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002184 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002185 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002186 mWindowManager.continueSurfaceLayout();
2187 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2188 }
2189
2190 mResizeDockedStackTimeout.notifyResizing(dockedBounds,
2191 tempDockedTaskBounds != null
2192 || tempDockedTaskInsetBounds != null
2193 || tempOtherTaskBounds != null
2194 || tempOtherTaskInsetBounds != null);
2195 }
2196
Robert Carr0d00c2e2016-02-29 17:45:02 -08002197 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2198 final ActivityStack stack = getStack(PINNED_STACK_ID);
2199 if (stack == null) {
2200 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2201 return;
2202 }
2203 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2204 mWindowManager.deferSurfaceLayout();
2205 try {
2206 ActivityRecord r = stack.topRunningActivityLocked();
2207 resizeStackUncheckedLocked(stack, pinnedBounds, tempPinnedTaskBounds,
2208 null);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002209 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002210 } finally {
2211 mWindowManager.continueSurfaceLayout();
2212 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2213 }
2214 }
2215
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002216 boolean resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow,
2217 boolean deferResume) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002218 if (!task.isResizeable()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002219 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
Chong Zhangf596cd52016-01-05 13:42:44 -08002220 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002221 }
2222
Chong Zhang87b21722015-09-21 15:39:51 -07002223 // If this is a forced resize, let it go through even if the bounds is not changing,
2224 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07002225 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08002226 if (Objects.equals(task.mBounds, bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002227 // Nothing to do here...
Chong Zhangf596cd52016-01-05 13:42:44 -08002228 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002229 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002230 bounds = TaskRecord.validateBounds(bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002231
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002232 if (!mWindowManager.isValidTaskId(task.taskId)) {
2233 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002234 // All we can do for now is update the bounds so it can be used when the task is
2235 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002236 task.updateOverrideConfiguration(bounds);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002237 if (task.getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002238 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002239 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002240 }
Chong Zhangf596cd52016-01-05 13:42:44 -08002241 return true;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002242 }
2243
Chong Zhang6de2ae82015-09-30 18:25:21 -07002244 // Do not move the task to another stack here.
2245 // This method assumes that the task is already placed in the right stack.
2246 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002247
Chong Zhang6de2ae82015-09-30 18:25:21 -07002248 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07002249
Andrii Kulian8072d112016-09-16 11:11:01 -07002250 final boolean updatedConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07002251 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002252 // way and the activity was kept the way it was. If it's false, it means the activity had
2253 // to be relaunched due to configuration change.
2254 boolean kept = true;
Andrii Kulian8072d112016-09-16 11:11:01 -07002255 if (updatedConfig) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002256 final ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08002257 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002258 final ActivityStack stack = r.getStack();
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002259 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002260
2261 if (!deferResume) {
2262
2263 // All other activities must be made visible with their correct configuration.
2264 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
2265 if (!kept) {
2266 resumeFocusedStackTopActivityLocked();
2267 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002268 }
2269 }
2270 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002271 mWindowManager.resizeTask(task.taskId, task.mBounds, task.getOverrideConfiguration(), kept,
2272 forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002273
2274 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Chong Zhangf596cd52016-01-05 13:42:44 -08002275 return kept;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002276 }
2277
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002278 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002279 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2280 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002281 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002282 }
2283
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002284 ActivityContainer activityContainer = new ActivityContainer(stackId);
2285 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002286 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002287 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002288 }
2289
2290 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002291 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002292 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2293 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002294 break;
2295 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002296 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002297 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002298 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002299 }
2300
Chong Zhang5dcb2752015-08-18 13:50:26 -07002301 /**
2302 * Restores a recent task to a stack
2303 * @param task The recent task to be restored.
2304 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07002305 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002306 * @return true if the task has been restored successfully.
2307 */
2308 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
2309 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002310 stackId = task.getLaunchStackId();
Wale Ogunwale513346d2016-01-27 10:55:01 -08002311 } else if (stackId == DOCKED_STACK_ID && !task.canGoInDockedStack()) {
2312 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2313 // Put it in the fullscreen stack.
2314 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002315 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002316
Andrii Kulian02b7a832016-10-06 23:11:56 -07002317 final ActivityStack currentStack = task.getStack();
2318 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002319 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002320 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002321 // Nothing else to do since it is already restored in the right stack.
2322 return true;
2323 }
2324 // Remove current stack association, so we can re-associate the task with the
2325 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002326 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002327 }
2328
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002329 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002330 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002331
2332 if (stack == null) {
2333 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002334 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2335 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002336 return false;
2337 }
2338
Wale Ogunwale5f986092015-12-04 15:35:38 -08002339 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002340 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2341 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002342 final ArrayList<ActivityRecord> activities = task.mActivities;
2343 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002344 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002345 }
2346 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002347 }
2348
Wale Ogunwale040b4702015-08-06 18:10:50 -07002349 /**
2350 * Moves the specified task record to the input stack id.
2351 * WARNING: This method performs an unchecked/raw move of the task and
2352 * can leave the system in an unstable state if used incorrectly.
Wale Ogunwale513346d2016-01-27 10:55:01 -08002353 * Use {@link #moveTaskToStackLocked} to perform safe task movement to a stack.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002354 * @param task Task to move.
2355 * @param stackId Id of stack to move task to.
2356 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07002357 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07002358 * @param reason Reason the task is been moved.
2359 * @return The stack the task was moved to.
2360 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07002361 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07002362 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
Wale Ogunwalefb1c8642016-03-02 08:28:08 -08002363
2364 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2365 throw new IllegalStateException("moveTaskToStackUncheckedLocked: Device doesn't "
2366 + "support multi-window task=" + task + " to stackId=" + stackId);
2367 }
2368
Wale Ogunwale06579d62016-04-30 15:29:06 -07002369 final ActivityRecord r = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002370 final ActivityStack prevStack = task.getStack();
Wale Ogunwaled046a012015-12-24 13:05:59 -08002371 final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
Wale Ogunwale35cdd6d2016-04-07 16:39:43 -07002372 final boolean wasResumed = prevStack.mResumedActivity == r;
Wale Ogunwaled046a012015-12-24 13:05:59 -08002373 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
2374 // Whenever we are moving the top activity from the front stack we want to make sure to move
2375 // the stack to the front.
2376 final boolean wasFront = isFrontStack(prevStack)
2377 && (prevStack.topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07002378
Chong Zhangd545dce2016-02-29 18:09:17 -08002379 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002380 // We don't allow moving a unresizeable task to the docked stack since the docked
2381 // stack is used for split-screen mode and will cause things like the docked divider to
2382 // show up. We instead leave the task in its current stack or move it to the fullscreen
2383 // stack if it isn't currently in a stack.
2384 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002385 Slog.w(TAG, "Can not move unresizeable task=" + task
2386 + " to docked stack. Moving to stackId=" + stackId + " instead.");
2387 }
2388
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07002389 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
2390 // if a docked stack is created below which will lead to the stack we are moving from and
2391 // its resizeable tasks being resized.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002392 task.mTemporarilyUnresizable = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002393 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08002394 task.mTemporarilyUnresizable = false;
Wale Ogunwale040b4702015-08-06 18:10:50 -07002395 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002396 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002397
2398 // If the task had focus before (or we're requested to move focus),
Wale Ogunwaled046a012015-12-24 13:05:59 -08002399 // move focus to the new stack by moving the stack to the front.
2400 stack.moveToFrontAndResumeStateIfNeeded(
2401 r, forceFocus || wasFocused || wasFront, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07002402
Wale Ogunwale040b4702015-08-06 18:10:50 -07002403 return stack;
2404 }
2405
Chong Zhange4fbd322016-03-01 14:44:03 -08002406 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08002407 String reason, boolean animate) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002408 return moveTaskToStackLocked(taskId, stackId, toTop, forceFocus, reason, animate,
2409 false /* deferResume */);
2410 }
2411
2412 boolean moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
2413 String reason, boolean animate, boolean deferResume) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002414 final TaskRecord task = anyTaskForIdLocked(taskId);
2415 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08002416 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002417 return false;
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002418 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002419
Andrii Kulian02b7a832016-10-06 23:11:56 -07002420 final ActivityStack currentStack = task.getStack();
2421 if (currentStack != null && currentStack.mStackId == stackId) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002422 // You are already in the right stack silly...
2423 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002424 return true;
Wale Ogunwale70c65c82015-11-13 13:25:16 -08002425 }
2426
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002427 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
2428 throw new IllegalArgumentException("moveTaskToStack:"
2429 + "Attempt to move task " + taskId + " to unsupported freeform stack");
2430 }
2431
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002432 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002433 final int sourceStackId = task.getStackId();
Chong Zhangf596cd52016-01-05 13:42:44 -08002434 final boolean mightReplaceWindow =
Jorim Jaggie9098022016-01-27 19:29:40 -08002435 StackId.replaceWindowsOnTaskMove(sourceStackId, stackId) && topActivity != null;
Chong Zhangf596cd52016-01-05 13:42:44 -08002436 if (mightReplaceWindow) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002437 // We are about to relaunch the activity because its configuration changed due to
2438 // being maximized, i.e. size change. The activity will first remove the old window
2439 // and then add a new one. This call will tell window manager about this, so it can
2440 // preserve the old window until the new one is drawn. This prevents having a gap
2441 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07002442 // entrance of the new window to be properly animated.
Chong Zhangf596cd52016-01-05 13:42:44 -08002443 // Note here we always set the replacing window first, as the flags might be needed
2444 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002445 mWindowManager.setWillReplaceWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002446 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002447
2448 mWindowManager.deferSurfaceLayout();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08002449 final int preferredLaunchStackId = stackId;
Chong Zhangf596cd52016-01-05 13:42:44 -08002450 boolean kept = true;
Wale Ogunwale961f4852016-02-01 20:25:54 -08002451 try {
2452 final ActivityStack stack = moveTaskToStackUncheckedLocked(
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002453 task, stackId, toTop, forceFocus, reason + " moveTaskToStack");
Wale Ogunwale961f4852016-02-01 20:25:54 -08002454 stackId = stack.mStackId;
Jorim Jaggie9098022016-01-27 19:29:40 -08002455
Wale Ogunwale961f4852016-02-01 20:25:54 -08002456 if (!animate) {
2457 stack.mNoAnimActivities.add(topActivity);
2458 }
Jorim Jaggie9098022016-01-27 19:29:40 -08002459
Wale Ogunwale961f4852016-02-01 20:25:54 -08002460 // We might trigger a configuration change. Save the current task bounds for freezing.
2461 mWindowManager.prepareFreezingTaskBounds(stack.mStackId);
2462
2463 // Make sure the task has the appropriate bounds/size for the stack it is in.
2464 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002465 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2466 !mightReplaceWindow, deferResume);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002467 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
2468 Rect bounds = task.getLaunchBounds();
2469 if (bounds == null) {
2470 stack.layoutTaskInStack(task, null);
2471 bounds = task.mBounds;
2472 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002473 kept = resizeTaskLocked(task, bounds, RESIZE_MODE_FORCED, !mightReplaceWindow,
2474 deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002475 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002476 kept = resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM,
2477 !mightReplaceWindow, deferResume);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002478 }
2479 } finally {
2480 mWindowManager.continueSurfaceLayout();
Chong Zhangf596cd52016-01-05 13:42:44 -08002481 }
2482
2483 if (mightReplaceWindow) {
2484 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
2485 // window), we need to clear the replace window settings. Otherwise, we schedule a
2486 // timeout to remove the old window if the replacing window is not coming in time.
Wale Ogunwale9bc47732016-08-10 14:44:22 -07002487 mWindowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002488 }
2489
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002490 if (!deferResume) {
2491
2492 // The task might have already been running and its visibility needs to be synchronized with
2493 // the visibility of the stack / windows.
2494 ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
2495 resumeFocusedStackTopActivityLocked();
2496 }
Chong Zhangb15758a2015-11-17 12:12:03 -08002497
Jorim Jaggid53f0922016-04-06 22:16:23 -07002498 handleNonResizableTaskIfNeeded(task, preferredLaunchStackId, stackId);
Chong Zhange4fbd322016-03-01 14:44:03 -08002499
2500 return (preferredLaunchStackId == stackId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002501 }
2502
Wale Ogunwale079a0042015-10-24 11:44:07 -07002503 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
2504 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2505 if (stack == null) {
2506 throw new IllegalArgumentException(
2507 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2508 }
2509
2510 final ActivityRecord r = stack.topRunningActivityLocked();
2511 if (r == null) {
2512 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2513 + " in stack=" + stack);
2514 return false;
2515 }
2516
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002517 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002518 Slog.w(TAG,
2519 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002520 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002521 return false;
2522 }
2523
Wale Ogunwale480dca02016-02-06 13:58:29 -08002524 moveActivityToPinnedStackLocked(r, "moveTopActivityToPinnedStack", bounds);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002525 return true;
2526 }
2527
Wale Ogunwale480dca02016-02-06 13:58:29 -08002528 void moveActivityToPinnedStackLocked(ActivityRecord r, String reason, Rect bounds) {
2529 mWindowManager.deferSurfaceLayout();
2530 try {
2531 final TaskRecord task = r.task;
2532
Andrii Kulian02b7a832016-10-06 23:11:56 -07002533 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002534 // An activity can't be pinned and visible behind at the same time. Go ahead and
2535 // release it from been visible behind before pinning.
2536 requestVisibleBehindLocked(r, false);
2537 }
2538
Wale Ogunwale480dca02016-02-06 13:58:29 -08002539 // Need to make sure the pinned stack exist so we can resize it below...
2540 final ActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
2541
2542 // Resize the pinned stack to match the current size of the task the activity we are
2543 // going to be moving is currently contained in. We do this to have the right starting
2544 // animation bounds for the pinned stack to the desired bounds the caller wants.
2545 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2546 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002547 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002548
2549 if (task.mActivities.size() == 1) {
2550 // There is only one activity in the task. So, we can just move the task over to
2551 // the stack without re-parenting the activity in a different task.
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002552 if (task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE) {
2553 // Move the home stack forward if the task we just moved to the pinned stack
2554 // was launched from home so home should be visible behind it.
2555 moveHomeStackToFront(reason);
2556 }
Wale Ogunwale480dca02016-02-06 13:58:29 -08002557 moveTaskToStackLocked(
2558 task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS, reason, !ANIMATE);
2559 } else {
2560 stack.moveActivityToStack(r);
2561 }
2562 } finally {
2563 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002564 }
2565
Wale Ogunwale480dca02016-02-06 13:58:29 -08002566 // The task might have already been running and its visibility needs to be synchronized
2567 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002568 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002569 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002570
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -07002571 mWindowManager.animateResizePinnedStack(bounds, -1);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002572 mService.notifyActivityPinnedLocked();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002573 }
2574
Chong Zhang6cda19c2016-06-14 19:07:56 -07002575 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2576 if (r == null || !r.isFocusable()) {
2577 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2578 "moveActivityStackToFront: unfocusable r=" + r);
2579 return false;
2580 }
2581
2582 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002583 final ActivityStack stack = r.getStack();
2584 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002585 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2586 + r + " task=" + task);
2587 return false;
2588 }
2589
Chong Zhang6cda19c2016-06-14 19:07:56 -07002590 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2591 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2592 "moveActivityStackToFront: already on top, r=" + r);
2593 return false;
2594 }
2595
2596 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2597 "moveActivityStackToFront: r=" + r);
2598
2599 stack.moveToFront(reason, task);
2600 return true;
2601 }
2602
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002603 void positionTaskInStackLocked(int taskId, int stackId, int position) {
2604 final TaskRecord task = anyTaskForIdLocked(taskId);
2605 if (task == null) {
2606 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
2607 return;
2608 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08002609 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
2610
2611 task.updateOverrideConfigurationForStack(stack);
2612
2613 mWindowManager.positionTaskInStack(
Andrii Kulian1779e612016-10-12 21:58:25 -07002614 taskId, stackId, position, task.mBounds, task.getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002615 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002616 // The task might have already been running and its visibility needs to be synchronized with
2617 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002618 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002619 resumeFocusedStackTopActivityLocked();
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002620 }
2621
Craig Mautnerac6f8432013-07-17 13:24:59 -07002622 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002623 mTmpFindTaskResult.r = null;
2624 mTmpFindTaskResult.matchedByRootAffinity = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002625 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002626 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2627 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002628 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2629 final ActivityStack stack = stacks.get(stackNdx);
2630 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002631 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002632 continue;
2633 }
2634 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002635 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2636 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002637 continue;
2638 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002639 stack.findTaskLocked(r, mTmpFindTaskResult);
2640 // It is possible to have task in multiple stacks with the same root affinity.
2641 // If the match we found was based on root affinity we keep on looking to see if
2642 // there is a better match in another stack. We eventually return the match based
2643 // on root affinity if we don't find a better match.
2644 if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
2645 return mTmpFindTaskResult.r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002646 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002647 }
2648 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002649 if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
2650 return mTmpFindTaskResult.r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002651 }
2652
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002653 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2654 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2656 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002658 final ActivityRecord ar = stacks.get(stackNdx)
2659 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002660 if (ar != null) {
2661 return ar;
2662 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002663 }
2664 }
2665 return null;
2666 }
2667
Craig Mautner8d341ef2013-03-26 09:03:27 -07002668 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002669 scheduleSleepTimeout();
2670 if (!mGoingToSleep.isHeld()) {
2671 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002672 if (mLaunchingActivity.isHeld()) {
2673 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2674 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002675 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002676 mLaunchingActivity.release();
2677 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002680 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002681 }
2682
2683 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002684 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002685
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002686 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002687 final long endTime = System.currentTimeMillis() + timeout;
2688 while (true) {
2689 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002690 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2691 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002692 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2693 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2694 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002695 }
2696 if (cantShutdown) {
2697 long timeRemaining = endTime - System.currentTimeMillis();
2698 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002699 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002700 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002701 } catch (InterruptedException e) {
2702 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002703 } else {
2704 Slog.w(TAG, "Activity manager shutdown timed out");
2705 timedout = true;
2706 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002707 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002708 } else {
2709 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 }
2711 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002712
2713 // Force checkReadyForSleep to complete.
2714 mSleepTimeout = true;
2715 checkReadyForSleepLocked();
2716
Craig Mautner8d341ef2013-03-26 09:03:27 -07002717 return timedout;
2718 }
2719
2720 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002721 removeSleepTimeouts();
2722 if (mGoingToSleep.isHeld()) {
2723 mGoingToSleep.release();
2724 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2726 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002727 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2728 final ActivityStack stack = stacks.get(stackNdx);
2729 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002730 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002731 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002732 }
Craig Mautner5314a402013-09-26 12:40:16 -07002733 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002735 mGoingToSleepActivities.clear();
2736 }
2737
2738 void activitySleptLocked(ActivityRecord r) {
2739 mGoingToSleepActivities.remove(r);
2740 checkReadyForSleepLocked();
2741 }
2742
2743 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002744 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002745 // Do not care.
2746 return;
2747 }
2748
2749 if (!mSleepTimeout) {
2750 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002751 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2752 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002753 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2754 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2755 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002756 }
2757
2758 if (mStoppingActivities.size() > 0) {
2759 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002760 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002761 + mStoppingActivities.size() + " activities");
2762 scheduleIdleLocked();
2763 dontSleep = true;
2764 }
2765
2766 if (mGoingToSleepActivities.size() > 0) {
2767 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002768 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07002769 + mGoingToSleepActivities.size() + " activities");
2770 dontSleep = true;
2771 }
2772
2773 if (dontSleep) {
2774 return;
2775 }
2776 }
2777
Wei Wang65c7a152016-06-02 18:51:22 -07002778 // Send launch end powerhint before going sleep
2779 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
2780
Craig Mautnere0a38842013-12-16 16:14:02 -08002781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2782 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002783 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2784 stacks.get(stackNdx).goToSleep();
2785 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002786 }
2787
2788 removeSleepTimeouts();
2789
2790 if (mGoingToSleep.isHeld()) {
2791 mGoingToSleep.release();
2792 }
2793 if (mService.mShuttingDown) {
2794 mService.notifyAll();
2795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 }
2797
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002798 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002799 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002800 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002801 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002802 }
2803 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002804 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002805 mWindowManager.executeAppTransition();
2806 return true;
2807 }
2808 return false;
2809 }
2810
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07002814 int stackNdx = stacks.size() - 1;
2815 while (stackNdx >= 0) {
2816 stacks.get(stackNdx).handleAppCrashLocked(app);
2817 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002818 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 }
2820 }
2821
Jose Lima4b6c6692014-08-12 17:41:12 -07002822 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002823 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002824 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002825 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2826 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002827 return false;
2828 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002829
2830 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
2831 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
2832 + " visible=" + visible + " stackId=" + stack.mStackId
2833 + " can't contain visible behind activities");
2834 return false;
2835 }
2836
Jose Lima4b6c6692014-08-12 17:41:12 -07002837 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002838 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2839 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002840
2841 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002842 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002843 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07002844 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002845 return true;
2846 }
2847
2848 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002849 if (visible && top.fullscreen) {
2850 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002851 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2852 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
2853 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
2854 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002855 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002856 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2857 // Only the activity set as currently visible behind should actively reset its
2858 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002859 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
2860 "requestVisibleBehind: returning visible=" + visible
2861 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
2862 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07002863 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002864 }
2865
Jose Lima4b6c6692014-08-12 17:41:12 -07002866 stack.setVisibleBehindActivity(visible ? r : null);
2867 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002868 // If there is a translucent home activity, we need to force it stop being translucent,
2869 // because we can't depend on the application to necessarily perform that operation.
2870 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002871 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08002872 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002873 mService.convertFromTranslucent(next.appToken);
2874 }
2875 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002876 if (top.app != null && top.app.thread != null) {
2877 // Notify the top app of the change.
2878 try {
2879 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2880 } catch (RemoteException e) {
2881 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002882 }
2883 return true;
2884 }
2885
Craig Mautnerbb742462014-07-07 15:28:55 -07002886 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2887 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07002888 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002889 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07002890
2891 r.mLaunchTaskBehind = false;
2892 task.setLastThumbnailLocked(stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002893 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08002894 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07002895 mWindowManager.setAppVisibility(r.appToken, false);
Winson730bf062016-03-31 18:04:56 -07002896
2897 // When launching tasks behind, update the last active time of the top task after the new
2898 // task has been shown briefly
2899 final ActivityRecord top = stack.topActivity();
2900 if (top != null) {
2901 top.task.touchActiveTime();
2902 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002903 }
2904
2905 void scheduleLaunchTaskBehindComplete(IBinder token) {
2906 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2907 }
2908
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002909 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2910 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07002911 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002912 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2913 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002914 final int topStackNdx = stacks.size() - 1;
2915 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2916 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002917 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07002918 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002919 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002920 }
2921
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002922 void invalidateTaskLayers() {
2923 mTaskLayersChanged = true;
2924 }
2925
2926 void rankTaskLayersIfNeeded() {
2927 if (!mTaskLayersChanged) {
2928 return;
2929 }
2930 mTaskLayersChanged = false;
2931 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
2932 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2933 int baseLayer = 0;
2934 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2935 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
2936 }
2937 }
2938 }
2939
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002940 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2943 final int topStackNdx = stacks.size() - 1;
2944 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2945 final ActivityStack stack = stacks.get(stackNdx);
2946 stack.clearOtherAppTimeTrackers(except);
2947 }
2948 }
2949 }
2950
Craig Mautner8d341ef2013-03-26 09:03:27 -07002951 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2953 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 final int numStacks = stacks.size();
2955 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2956 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002957 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 }
2960 }
2961
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002962 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2963 // Examine all activities currently running in the process.
2964 TaskRecord firstTask = null;
2965 // Tasks is non-null only if two or more tasks are found.
2966 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
2968 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002969 ActivityRecord r = app.activities.get(i);
2970 // First, if we find an activity that is in the process of being destroyed,
2971 // then we just aren't going to do anything for now; we want things to settle
2972 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002973 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002974 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002975 return;
2976 }
2977 // Don't consider any activies that are currently not in a state where they
2978 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002979 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
2980 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002981 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002982 continue;
2983 }
2984 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002985 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002986 + " from " + r);
2987 if (firstTask == null) {
2988 firstTask = r.task;
2989 } else if (firstTask != r.task) {
2990 if (tasks == null) {
2991 tasks = new ArraySet<>();
2992 tasks.add(firstTask);
2993 }
2994 tasks.add(r.task);
2995 }
2996 }
2997 }
2998 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002999 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003000 return;
3001 }
3002 // If we have activities in multiple tasks that are in a position to be destroyed,
3003 // let's iterate through the tasks and release the oldest one.
3004 final int numDisplays = mActivityDisplays.size();
3005 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3006 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3007 // Step through all stacks starting from behind, to hit the oldest things first.
3008 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3009 final ActivityStack stack = stacks.get(stackNdx);
3010 // Try to release activities in this stack; if we manage to, we are done.
3011 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3012 return;
3013 }
3014 }
3015 }
3016 }
3017
Amith Yamasani37a40c22015-06-17 13:25:42 -07003018 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003019 final int focusStackId = mFocusedStack.getStackId();
3020 // We dismiss the docked stack whenever we switch users.
3021 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
3022
3023 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003024 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003025 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003026
Craig Mautner858d8a62013-04-23 17:08:34 -07003027 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 final ActivityStack stack = stacks.get(stackNdx);
3032 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003033 TaskRecord task = stack.topTask();
3034 if (task != null) {
3035 mWindowManager.moveTaskToTop(task.taskId);
3036 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003037 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003038 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003039
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003040 ActivityStack stack = getStack(restoreStackId);
3041 if (stack == null) {
3042 stack = mHomeStack;
3043 }
3044 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003045 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003046 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003047 } else {
3048 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003049 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003050 }
Craig Mautner93529a42013-10-04 15:03:13 -07003051 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003052 }
3053
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003054 /** Checks whether the userid is a profile of the current user. */
3055 boolean isCurrentProfileLocked(int userId) {
3056 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003057 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003058 }
3059
Craig Mautnerde4ef022013-04-07 19:01:33 -07003060 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003061 ArrayList<ActivityRecord> stops = null;
3062
3063 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003064 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3065 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003066 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3067 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003068 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003069 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003070 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3071 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003072 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003073 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 if (s.finishing) {
3075 // If this activity is finishing, it is sitting on top of
3076 // everyone else but we now know it is no longer needed...
3077 // so get rid of it. Otherwise, we need to go through the
3078 // normal flow and hide it once we determine that it is
3079 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003080 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003081 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 }
3083 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003084 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003085 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003086 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003087 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003088 }
3089 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003090 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003091 }
3092 }
3093
3094 return stops;
3095 }
3096
Craig Mautnercf910b02013-04-23 11:23:27 -07003097 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003098 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3099 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3100 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3101 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003102 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003103 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003104 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003105 if (r == null) Slog.e(TAG,
3106 "validateTop...: null top activity, stack=" + stack);
3107 else {
3108 final ActivityRecord pausing = stack.mPausingActivity;
3109 if (pausing != null && pausing == r) Slog.e(TAG,
3110 "validateTop...: top stack has pausing activity r=" + r
3111 + " state=" + state);
3112 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3113 "validateTop...: activity in front not resumed r=" + r
3114 + " state=" + state);
3115 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003116 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003117 final ActivityRecord resumed = stack.mResumedActivity;
3118 if (resumed != null && resumed == r) Slog.e(TAG,
3119 "validateTop...: back stack has resumed activity r=" + r
3120 + " state=" + state);
3121 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3122 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003123 }
3124 }
3125 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003126 }
3127
Craig Mautnere0570202015-05-13 13:06:11 -07003128 private String lockTaskModeToString() {
3129 switch (mLockTaskModeState) {
3130 case LOCK_TASK_MODE_LOCKED:
3131 return "LOCKED";
3132 case LOCK_TASK_MODE_PINNED:
3133 return "PINNED";
3134 case LOCK_TASK_MODE_NONE:
3135 return "NONE";
3136 default: return "unknown=" + mLockTaskModeState;
3137 }
3138 }
3139
Craig Mautner27084302013-03-25 08:05:25 -07003140 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003141 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003142 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003143 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003144 pw.print(prefix);
3145 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003146 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003147 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003148 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3149 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3150 if (packages.size() > 0) {
3151 pw.println(" mLockTaskPackages (userId:packages)=");
3152 for (int i = 0; i < packages.size(); ++i) {
3153 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3154 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3155 }
3156 }
3157 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003158 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003159
Winson43d1f262016-06-14 16:05:55 -07003160 /**
3161 * Dumps the activities matching the given {@param name} in the either the focused stack
3162 * or all visible stacks if {@param dumpVisibleStacks} is true.
3163 */
3164 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacks) {
3165 if (dumpVisibleStacks) {
3166 ArrayList<ActivityRecord> activities = new ArrayList<>();
3167 int numDisplays = mActivityDisplays.size();
3168 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3169 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3170 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3171 ActivityStack stack = stacks.get(stackNdx);
3172 if (stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
3173 activities.addAll(stack.getDumpActivitiesLocked(name));
3174 }
3175 }
3176 }
3177 return activities;
3178 } else {
3179 return mFocusedStack.getDumpActivitiesLocked(name);
3180 }
Craig Mautner20e72272013-04-01 13:45:53 -07003181 }
3182
Dianne Hackborn390517b2013-05-30 15:03:32 -07003183 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3184 boolean needSep, String prefix) {
3185 if (activity != null) {
3186 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3187 if (needSep) {
3188 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003189 }
3190 pw.print(prefix);
3191 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003192 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003193 }
3194 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003195 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003196 }
3197
Craig Mautner8d341ef2013-03-26 09:03:27 -07003198 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3199 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003200 boolean printed = false;
3201 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003202 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3203 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003204 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003205 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003206 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003207 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003208 final ActivityStack stack = stacks.get(stackNdx);
3209 StringBuilder stackHeader = new StringBuilder(128);
3210 stackHeader.append(" Stack #");
3211 stackHeader.append(stack.mStackId);
3212 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003213 stackHeader.append("\n");
3214 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3215 stackHeader.append("\n");
3216 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003217 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3218 needSep, stackHeader.toString());
3219 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3220 !dumpAll, false, dumpPackage, true,
3221 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003222
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 needSep = printed;
3224 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3225 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003226 if (pr) {
3227 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003229 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003230 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3231 " mResumedActivity: ");
3232 if (pr) {
3233 printed = true;
3234 needSep = false;
3235 }
3236 if (dumpAll) {
3237 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3238 " mLastPausedActivity: ");
3239 if (pr) {
3240 printed = true;
3241 needSep = true;
3242 }
3243 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3244 needSep, " mLastNoHistoryActivity: ");
3245 }
3246 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003247 }
3248 }
3249
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003250 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3251 false, dumpPackage, true, " Activities waiting to finish:", null);
3252 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3253 false, dumpPackage, true, " Activities waiting to stop:", null);
3254 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3255 false, dumpPackage, true, " Activities waiting for another to become visible:",
3256 null);
3257 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3258 false, dumpPackage, true, " Activities waiting to sleep:", null);
3259 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3260 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003261
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003262 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003263 }
3264
Dianne Hackborn390517b2013-05-30 15:03:32 -07003265 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003266 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003267 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003268 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003269 String innerPrefix = null;
3270 String[] args = null;
3271 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003272 for (int i=list.size()-1; i>=0; i--) {
3273 final ActivityRecord r = list.get(i);
3274 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3275 continue;
3276 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003277 if (innerPrefix == null) {
3278 innerPrefix = prefix + " ";
3279 args = new String[0];
3280 }
3281 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003282 final boolean full = !brief && (complete || !r.isInHistory());
3283 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003284 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003285 needNL = false;
3286 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003287 if (header1 != null) {
3288 pw.println(header1);
3289 header1 = null;
3290 }
3291 if (header2 != null) {
3292 pw.println(header2);
3293 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003294 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 if (lastTask != r.task) {
3296 lastTask = r.task;
3297 pw.print(prefix);
3298 pw.print(full ? "* " : " ");
3299 pw.println(lastTask);
3300 if (full) {
3301 lastTask.dump(pw, prefix + " ");
3302 } else if (complete) {
3303 // Complete + brief == give a summary. Isn't that obvious?!?
3304 if (lastTask.intent != null) {
3305 pw.print(prefix); pw.print(" ");
3306 pw.println(lastTask.intent.toInsecureStringWithClip());
3307 }
3308 }
3309 }
3310 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3311 pw.print(" #"); pw.print(i); pw.print(": ");
3312 pw.println(r);
3313 if (full) {
3314 r.dump(pw, innerPrefix);
3315 } else if (complete) {
3316 // Complete + brief == give a summary. Isn't that obvious?!?
3317 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3318 if (r.app != null) {
3319 pw.print(innerPrefix); pw.println(r.app);
3320 }
3321 }
3322 if (client && r.app != null && r.app.thread != null) {
3323 // flush anything that is already in the PrintWriter since the thread is going
3324 // to write to the file descriptor directly
3325 pw.flush();
3326 try {
3327 TransferPipe tp = new TransferPipe();
3328 try {
3329 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3330 r.appToken, innerPrefix, args);
3331 // Short timeout, since blocking here can
3332 // deadlock with the application.
3333 tp.go(fd, 2000);
3334 } finally {
3335 tp.kill();
3336 }
3337 } catch (IOException e) {
3338 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3339 } catch (RemoteException e) {
3340 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3341 }
3342 needNL = true;
3343 }
3344 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003345 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003347
Craig Mautnerf3333272013-04-22 10:55:53 -07003348 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003349 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3350 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003351 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3352 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003353 }
3354
3355 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003356 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003357 }
3358
3359 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003360 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3361 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003362 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3363 }
3364
Craig Mautner05d29032013-05-03 13:40:13 -07003365 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003366 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3367 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3368 }
Craig Mautner05d29032013-05-03 13:40:13 -07003369 }
3370
Craig Mautner0eea92c2013-05-16 13:35:39 -07003371 void removeSleepTimeouts() {
3372 mSleepTimeout = false;
3373 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3374 }
3375
3376 final void scheduleSleepTimeout() {
3377 removeSleepTimeouts();
3378 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3379 }
3380
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 @Override
3382 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003383 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3385 }
3386
3387 @Override
3388 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003389 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3391 }
3392
3393 @Override
3394 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003395 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3397 }
3398
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003399 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003400 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003402 newDisplay = mActivityDisplays.get(displayId) == null;
3403 if (newDisplay) {
3404 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003405 if (activityDisplay.mDisplay == null) {
3406 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3407 return;
3408 }
Craig Mautner4504de52013-12-20 09:06:56 -08003409 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003410 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08003411 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003412 }
Craig Mautner4504de52013-12-20 09:06:56 -08003413 if (newDisplay) {
3414 mWindowManager.onDisplayAdded(displayId);
3415 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003416 }
3417
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003418 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003419 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003420 mService.mContext.getResources().getDimensionPixelSize(
3421 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003422 }
3423
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003424 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003425 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003426 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3427 if (activityDisplay != null) {
3428 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003429 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003430 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003432 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003433 }
3434 }
3435 mWindowManager.onDisplayRemoved(displayId);
3436 }
3437
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003438 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003439 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003440 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3441 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442 // TODO: Update the bounds.
3443 }
3444 }
3445 mWindowManager.onDisplayChanged(displayId);
3446 }
3447
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003448 private StackInfo getStackInfoLocked(ActivityStack stack) {
Winson529c8e42016-05-17 11:08:40 -07003449 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003450 StackInfo info = new StackInfo();
3451 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3452 info.displayId = Display.DEFAULT_DISPLAY;
3453 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003454 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003455 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003456 info.position = display != null
3457 ? display.mStacks.indexOf(stack)
3458 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003459
3460 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3461 final int numTasks = tasks.size();
3462 int[] taskIds = new int[numTasks];
3463 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003464 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003465 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003466 for (int i = 0; i < numTasks; ++i) {
3467 final TaskRecord task = tasks.get(i);
3468 taskIds[i] = task.taskId;
3469 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3470 : task.realActivity != null ? task.realActivity.flattenToString()
3471 : task.getTopActivity() != null ? task.getTopActivity().packageName
3472 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003473 taskBounds[i] = new Rect();
3474 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003475 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003476 }
3477 info.taskIds = taskIds;
3478 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003479 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003480 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003481
3482 final ActivityRecord top = stack.topRunningActivityLocked();
3483 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003484 return info;
3485 }
3486
3487 StackInfo getStackInfoLocked(int stackId) {
3488 ActivityStack stack = getStack(stackId);
3489 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003490 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003491 }
3492 return null;
3493 }
3494
3495 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003496 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003497 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3498 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003500 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003501 }
3502 }
3503 return list;
3504 }
3505
Craig Mautner15df08a2015-04-01 12:17:18 -07003506 TaskRecord getLockedTaskLocked() {
3507 final int top = mLockTaskModeTasks.size() - 1;
3508 if (top >= 0) {
3509 return mLockTaskModeTasks.get(top);
3510 }
3511 return null;
3512 }
3513
3514 boolean isLockedTask(TaskRecord task) {
3515 return mLockTaskModeTasks.contains(task);
3516 }
3517
3518 boolean isLastLockedTask(TaskRecord task) {
3519 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3520 }
3521
3522 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003523 if (!mLockTaskModeTasks.remove(task)) {
3524 return;
3525 }
3526 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3527 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003528 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003529 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3530 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003531 final Message lockTaskMsg = Message.obtain();
3532 lockTaskMsg.arg1 = task.userId;
3533 lockTaskMsg.what = LOCK_TASK_END_MSG;
3534 mHandler.sendMessage(lockTaskMsg);
3535 }
3536 }
3537
Andrii Kulianc27916642016-04-12 17:59:27 -07003538 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3539 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3540 false /* forceNonResizable */);
3541 }
3542
Jorim Jaggid53f0922016-04-06 22:16:23 -07003543 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003544 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003545 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3546 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003547 return;
3548 }
3549
Jorim Jaggicd13d332016-04-27 15:40:20 -07003550 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulianc27916642016-04-12 17:59:27 -07003551 if (!task.canGoInDockedStack() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003552 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Jorim Jaggid53f0922016-04-06 22:16:23 -07003553 mService.mHandler.sendEmptyMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
3554
Andrii Kulianc27916642016-04-12 17:59:27 -07003555 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3556 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003557 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Jorim Jaggicd13d332016-04-27 15:40:20 -07003558 } else if (topActivity != null && topActivity.isNonResizableOrForced()
3559 && !topActivity.noDisplay) {
3560 String packageName = topActivity.appInfo.packageName;
Jorim Jaggi2adba072016-03-03 13:43:39 +01003561 mService.mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, task.taskId, 0,
3562 packageName).sendToTarget();
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003563 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003564 }
3565
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003566 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003567 if (mLockTaskNotify != null) {
3568 mLockTaskNotify.showToast(mLockTaskModeState);
3569 }
Jason Monka8f569c2014-07-07 12:15:21 -04003570 }
3571
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003572 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3573 if (mLockTaskModeTasks.contains(task)) {
3574 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3575 }
3576 }
3577
Craig Mautner432f64e2015-05-20 14:59:57 -07003578 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3579 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003580 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003581 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003582 final TaskRecord lockedTask = getLockedTaskLocked();
3583 if (lockedTask != null) {
3584 removeLockedTaskLocked(lockedTask);
3585 if (!mLockTaskModeTasks.isEmpty()) {
3586 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003587 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3588 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003589 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003590 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003591 return;
3592 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003593 }
Craig Mautnere0570202015-05-13 13:06:11 -07003594 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3595 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003596 return;
3597 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003598
3599 // Should have already been checked, but do it again.
3600 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003601 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3602 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003603 return;
3604 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003605 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003606 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003607 return;
3608 }
3609
3610 if (mLockTaskModeTasks.isEmpty()) {
3611 // First locktask.
3612 final Message lockTaskMsg = Message.obtain();
3613 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3614 lockTaskMsg.arg1 = task.userId;
3615 lockTaskMsg.what = LOCK_TASK_START_MSG;
3616 lockTaskMsg.arg2 = lockTaskModeState;
3617 mHandler.sendMessage(lockTaskMsg);
3618 }
3619 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003620 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3621 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003622 mLockTaskModeTasks.remove(task);
3623 mLockTaskModeTasks.add(task);
3624
3625 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07003626 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07003627 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003628
3629 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07003630 findTaskToMoveToFrontLocked(task, 0, null, reason,
3631 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003632 resumeFocusedStackTopActivityLocked();
Andrii Kulianc27916642016-04-12 17:59:27 -07003633 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003634 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07003635 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07003636 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003637 }
3638
3639 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003640 return isLockTaskModeViolation(task, false);
3641 }
3642
3643 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3644 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003645 return false;
3646 }
3647 final int lockTaskAuth = task.mLockTaskAuth;
3648 switch (lockTaskAuth) {
3649 case LOCK_TASK_AUTH_DONT_LOCK:
3650 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003651 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003652 case LOCK_TASK_AUTH_LAUNCHABLE:
3653 case LOCK_TASK_AUTH_WHITELISTED:
3654 return false;
3655 case LOCK_TASK_AUTH_PINNABLE:
3656 // Pinnable tasks can't be launched on top of locktask tasks.
3657 return !mLockTaskModeTasks.isEmpty();
3658 default:
3659 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3660 return true;
3661 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003662 }
3663
Craig Mautner15df08a2015-04-01 12:17:18 -07003664 void onLockTaskPackagesUpdatedLocked() {
3665 boolean didSomething = false;
3666 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3667 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003668 final boolean wasWhitelisted =
3669 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3670 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003671 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003672 final boolean isWhitelisted =
3673 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3674 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3675 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003676 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003677 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3678 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003679 removeLockedTaskLocked(lockedTask);
3680 lockedTask.performClearTaskLocked();
3681 didSomething = true;
3682 }
3683 }
3684 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3685 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3686 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3687 final ActivityStack stack = stacks.get(stackNdx);
3688 stack.onLockTaskPackagesUpdatedLocked();
3689 }
3690 }
Craig Mautnere0570202015-05-13 13:06:11 -07003691 final ActivityRecord r = topRunningActivityLocked();
3692 final TaskRecord task = r != null ? r.task : null;
3693 if (mLockTaskModeTasks.isEmpty() && task != null
3694 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
3695 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07003696 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
3697 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
3698 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
3699 false);
3700 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07003701 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003702 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003703 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003704 }
3705 }
3706
Benjamin Franz43261142015-02-11 15:59:44 +00003707 int getLockTaskModeState() {
3708 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08003709 }
3710
Jorim Jaggife89d122015-12-22 16:28:44 +01003711 void activityRelaunchedLocked(IBinder token) {
3712 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07003713 if (mService.isSleepingOrShuttingDownLocked()) {
3714 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
3715 if (r != null) {
3716 r.setSleeping(true, true);
3717 }
3718 }
Jorim Jaggife89d122015-12-22 16:28:44 +01003719 }
3720
3721 void activityRelaunchingLocked(ActivityRecord r) {
3722 mWindowManager.notifyAppRelaunching(r.appToken);
3723 }
3724
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003725 void logStackState() {
3726 mActivityMetricsLogger.logWindowState();
3727 }
3728
Andrii Kulian933076d2016-03-29 17:04:42 -07003729 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003730 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3731 final ActivityRecord r = task.mActivities.get(i);
3732 if (r.app != null && r.app.thread != null) {
3733 mMultiWindowModeChangedActivities.add(r);
3734 }
3735 }
3736
3737 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
3738 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
3739 }
3740 }
3741
Andrii Kulian933076d2016-03-29 17:04:42 -07003742 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003743 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003744 if (prevStack == null || prevStack == stack
3745 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
3746 return;
3747 }
3748
3749 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
3750 final ActivityRecord r = task.mActivities.get(i);
3751 if (r.app != null && r.app.thread != null) {
3752 mPipModeChangedActivities.add(r);
3753 }
3754 }
3755
3756 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
3757 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
3758 }
3759 }
3760
Tony Mak853304c2016-04-18 15:17:41 +01003761 void setDockedStackMinimized(boolean minimized) {
3762 mIsDockMinimized = minimized;
3763 if (minimized) {
3764 // Docked stack is not visible, no need to confirm credentials for its top activity.
3765 return;
3766 }
3767 final ActivityStack dockedStack = getStack(StackId.DOCKED_STACK_ID);
3768 if (dockedStack == null) {
3769 return;
3770 }
3771 final ActivityRecord top = dockedStack.topRunningActivityLocked();
3772 if (top != null && mService.mUserController.shouldConfirmCredentials(top.userId)) {
3773 mService.mActivityStarter.showConfirmDeviceCredential(top.userId);
3774 }
3775 }
3776
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003777 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003778
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003779 public ActivityStackSupervisorHandler(Looper looper) {
3780 super(looper);
3781 }
3782
Craig Mautnerf3333272013-04-22 10:55:53 -07003783 void activityIdleInternal(ActivityRecord r) {
3784 synchronized (mService) {
3785 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3786 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003787 }
3788
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003789 @Override
3790 public void handleMessage(Message msg) {
3791 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08003792 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
3793 synchronized (mService) {
3794 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
3795 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003796 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003797 }
3798 }
3799 } break;
3800 case REPORT_PIP_MODE_CHANGED_MSG: {
3801 synchronized (mService) {
3802 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
3803 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07003804 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08003805 }
3806 }
3807 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07003808 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003809 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3810 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003811 if (mService.mDidDexOpt) {
3812 mService.mDidDexOpt = false;
3813 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3814 nmsg.obj = msg.obj;
3815 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3816 return;
3817 }
3818 // We don't at this point know if the activity is fullscreen,
3819 // so we need to be conservative and assume it isn't.
3820 activityIdleInternal((ActivityRecord)msg.obj);
3821 } break;
3822 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003823 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003824 activityIdleInternal((ActivityRecord)msg.obj);
3825 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003826 case RESUME_TOP_ACTIVITY_MSG: {
3827 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003828 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07003829 }
3830 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003831 case SLEEP_TIMEOUT_MSG: {
3832 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003833 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003834 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3835 mSleepTimeout = true;
3836 checkReadyForSleepLocked();
3837 }
3838 }
3839 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003840 case LAUNCH_TIMEOUT_MSG: {
3841 if (mService.mDidDexOpt) {
3842 mService.mDidDexOpt = false;
3843 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3844 return;
3845 }
3846 synchronized (mService) {
3847 if (mLaunchingActivity.isHeld()) {
3848 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3849 if (VALIDATE_WAKE_LOCK_CALLER
3850 && Binder.getCallingUid() != Process.myUid()) {
3851 throw new IllegalStateException("Calling must be system uid");
3852 }
3853 mLaunchingActivity.release();
3854 }
3855 }
3856 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003858 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859 } break;
3860 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003861 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003862 } break;
3863 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003864 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003866 case CONTAINER_CALLBACK_VISIBILITY: {
3867 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003868 final IActivityContainerCallback callback = container.mCallback;
3869 if (callback != null) {
3870 try {
3871 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3872 } catch (RemoteException e) {
3873 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003874 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003875 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003876 case LOCK_TASK_START_MSG: {
3877 // When lock task starts, we disable the status bars.
3878 try {
Jason Monk62515be2014-05-21 16:06:19 -04003879 if (mLockTaskNotify == null) {
3880 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003881 }
Jason Monk62515be2014-05-21 16:06:19 -04003882 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00003883 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04003884 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00003885 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003886 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003887 flags = StatusBarManager.DISABLE_MASK
3888 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07003889 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00003890 flags = StatusBarManager.DISABLE_MASK
3891 & (~StatusBarManager.DISABLE_BACK)
3892 & (~StatusBarManager.DISABLE_HOME)
3893 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04003894 }
3895 getStatusBarService().disable(flags, mToken,
3896 mService.mContext.getPackageName());
3897 }
3898 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003899 if (getDevicePolicyManager() != null) {
3900 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003901 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003902 }
justinzhang5286d3f2014-05-12 17:06:01 -04003903 } catch (RemoteException ex) {
3904 throw new RuntimeException(ex);
3905 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003906 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003907 case LOCK_TASK_END_MSG: {
3908 // When lock task ends, we enable the status bars.
3909 try {
Jason Monk62515be2014-05-21 16:06:19 -04003910 if (getStatusBarService() != null) {
3911 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3912 mService.mContext.getPackageName());
3913 }
3914 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003915 if (getDevicePolicyManager() != null) {
3916 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3917 msg.arg1);
3918 }
Jason Monk62515be2014-05-21 16:06:19 -04003919 if (mLockTaskNotify == null) {
3920 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3921 }
3922 mLockTaskNotify.show(false);
3923 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04003924 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04003925 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04003926 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07003927 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003928 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003929 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003930 new LockPatternUtils(mService.mContext)
3931 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003932 }
3933 } catch (SettingNotFoundException e) {
3934 // No setting, don't lock.
3935 }
justinzhang5286d3f2014-05-12 17:06:01 -04003936 } catch (RemoteException ex) {
3937 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00003938 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07003939 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04003940 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003941 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003942 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
3943 if (mLockTaskNotify == null) {
3944 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3945 }
3946 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
3947 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003948 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3949 final ActivityContainer container = (ActivityContainer) msg.obj;
3950 final IActivityContainerCallback callback = container.mCallback;
3951 if (callback != null) {
3952 try {
3953 callback.onAllActivitiesComplete(container.asBinder());
3954 } catch (RemoteException e) {
3955 }
3956 }
3957 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003958 case LAUNCH_TASK_BEHIND_COMPLETE: {
3959 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003960 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07003961 if (r != null) {
3962 handleLaunchTaskBehindCompleteLocked(r);
3963 }
3964 }
3965 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08003966
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003967 }
3968 }
3969 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003970
Ying Wangb081a592014-04-22 15:20:16 -07003971 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08003972 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
3973 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003974 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003975 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003976 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003977 ActivityRecord mParentActivity = null;
3978 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003979
Craig Mautnere3a00d72014-04-16 08:31:19 -07003980 boolean mVisible = true;
3981
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003983 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003984
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003985 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3986 final static int CONTAINER_STATE_NO_SURFACE = 1;
3987 final static int CONTAINER_STATE_FINISHING = 2;
3988 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3989
3990 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 synchronized (mService) {
3992 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003993 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003994 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003995 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003997 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003999 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004000 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004001 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004002 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004003 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004004 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 }
4006
4007 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004008 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004010 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4011 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 return;
4013 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004014 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004015 }
4016 }
4017
4018 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004019 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004020 synchronized (mService) {
4021 if (mActivityDisplay != null) {
4022 return mActivityDisplay.mDisplayId;
4023 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004024 }
4025 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 }
4027
Jeff Brownca9bc702014-02-11 14:32:56 -08004028 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004029 public int getStackId() {
4030 synchronized (mService) {
4031 return mStackId;
4032 }
4033 }
4034
4035 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004036 public boolean injectEvent(InputEvent event) {
4037 final long origId = Binder.clearCallingIdentity();
4038 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004039 synchronized (mService) {
4040 if (mActivityDisplay != null) {
4041 return mInputManagerInternal.injectInputEvent(event,
4042 mActivityDisplay.mDisplayId,
4043 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4044 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004045 }
4046 return false;
4047 } finally {
4048 Binder.restoreCallingIdentity(origId);
4049 }
4050 }
4051
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004052 @Override
4053 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004054 synchronized (mService) {
4055 if (mContainerState == CONTAINER_STATE_FINISHING) {
4056 return;
4057 }
4058 mContainerState = CONTAINER_STATE_FINISHING;
4059
Craig Mautnerd163e752014-06-13 17:18:47 -07004060 long origId = Binder.clearCallingIdentity();
4061 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004062 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004063 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004064 } finally {
4065 Binder.restoreCallingIdentity(origId);
4066 }
4067 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004068 }
4069
Craig Mautner60257702014-09-17 15:02:33 -07004070 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004071 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004072 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004073 if (mActivityDisplay != null) {
4074 mActivityDisplay.detachActivitiesLocked(mStack);
4075 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004076 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 }
4078 }
4079
4080 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004081 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004082 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084
4085 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004086 public final int startActivityIntentSender(IIntentSender intentSender)
4087 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004088 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4089
4090 if (!(intentSender instanceof PendingIntentRecord)) {
4091 throw new IllegalArgumentException("Bad PendingIntent object");
4092 }
4093
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004094 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004095 Binder.getCallingUid(), mCurrentUser, false,
4096 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004097
4098 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4099 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4100 pendingIntent.key.requestResolvedType);
4101
4102 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4103 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4104 }
4105
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004106 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004107 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004108 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004109 throw new SecurityException(
4110 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4111 }
4112 }
4113
Craig Mautnerdf88d732014-01-27 09:21:32 -08004114 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004115 public IBinder asBinder() {
4116 return this;
4117 }
4118
Craig Mautner4504de52013-12-20 09:06:56 -08004119 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004120 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004121 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004122 }
4123
Craig Mautner4a1cb222013-12-04 16:14:06 -08004124 ActivityStackSupervisor getOuter() {
4125 return ActivityStackSupervisor.this;
4126 }
4127
Craig Mautnerd163e752014-06-13 17:18:47 -07004128 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004129 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004130 }
4131
Craig Mautner6985bad2014-04-21 15:22:06 -07004132 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004133 void setVisible(boolean visible) {
4134 if (mVisible != visible) {
4135 mVisible = visible;
4136 if (mCallback != null) {
4137 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4138 0 /* unused */, this).sendToTarget();
4139 }
4140 }
4141 }
4142
Craig Mautner6985bad2014-04-21 15:22:06 -07004143 void setDrawn() {
4144 }
4145
Craig Mautner1b4bf852014-05-26 15:06:32 -07004146 // You can always start a new task on a regular ActivityStack.
4147 boolean isEligibleForNewTasks() {
4148 return true;
4149 }
4150
Craig Mautnerd163e752014-06-13 17:18:47 -07004151 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004152 detachLocked();
4153 deleteActivityContainer(this);
4154 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004155 }
4156
Craig Mautner34b73df2014-01-12 21:11:08 -08004157 @Override
4158 public String toString() {
4159 return mIdString + (mActivityDisplay == null ? "N" : "A");
4160 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004161 }
4162
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004163 private class VirtualActivityContainer extends ActivityContainer {
4164 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004165 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004166
4167 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4168 super(getNextStackId());
4169 mParentActivity = parent;
4170 mCallback = callback;
4171 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004172 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004173 }
4174
4175 @Override
4176 public void setSurface(Surface surface, int width, int height, int density) {
4177 super.setSurface(surface, width, height, density);
4178
4179 synchronized (mService) {
4180 final long origId = Binder.clearCallingIdentity();
4181 try {
4182 setSurfaceLocked(surface, width, height, density);
4183 } finally {
4184 Binder.restoreCallingIdentity(origId);
4185 }
4186 }
4187 }
4188
4189 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4190 if (mContainerState == CONTAINER_STATE_FINISHING) {
4191 return;
4192 }
4193 VirtualActivityDisplay virtualActivityDisplay =
4194 (VirtualActivityDisplay) mActivityDisplay;
4195 if (virtualActivityDisplay == null) {
4196 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004197 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004198 mActivityDisplay = virtualActivityDisplay;
4199 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004200 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004201 }
4202
4203 if (mSurface != null) {
4204 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004205 }
4206
Craig Mautner6985bad2014-04-21 15:22:06 -07004207 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004208 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004209 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004210 } else {
4211 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004212 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004213 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004214 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004215 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004216 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004217
Craig Mautnerd163e752014-06-13 17:18:47 -07004218 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004219
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004220 if (DEBUG_STACK) Slog.d(TAG_STACK,
4221 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004222 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004223
Craig Mautner6985bad2014-04-21 15:22:06 -07004224 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004225 boolean isAttachedLocked() {
4226 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004227 }
4228
4229 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004230 void setDrawn() {
4231 synchronized (mService) {
4232 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004233 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004234 }
4235 }
4236
Craig Mautner1b4bf852014-05-26 15:06:32 -07004237 // Never start a new task on an ActivityView if it isn't explicitly specified.
4238 @Override
4239 boolean isEligibleForNewTasks() {
4240 return false;
4241 }
4242
Craig Mautnerd163e752014-06-13 17:18:47 -07004243 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004244 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004245 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4246 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4247 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4248 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4249 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004250 }
4251 }
4252
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4254 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004255 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004257 int mDisplayId;
4258 Display mDisplay;
4259 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004260
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4262 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004263 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264
Jose Lima4b6c6692014-08-12 17:41:12 -07004265 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004266
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004267 ActivityDisplay() {
4268 }
Craig Mautner4504de52013-12-20 09:06:56 -08004269
Craig Mautner1a70a162014-09-13 12:09:31 -07004270 // After instantiation, check that mDisplay is not null before using this. The alternative
4271 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004272 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004273 final Display display = mDisplayManager.getDisplay(displayId);
4274 if (display == null) {
4275 return;
4276 }
4277 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004278 }
4279
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004280 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004281 mDisplay = display;
4282 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004284 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004285
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004286 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004287 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004288 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4289 + " onTop=" + onTop);
4290 if (onTop) {
4291 mStacks.add(stack);
4292 } else {
4293 mStacks.add(0, stack);
4294 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004295 }
4296
Craig Mautnere0a38842013-12-16 16:14:02 -08004297 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004298 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004299 + " from displayId=" + mDisplayId);
4300 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004301 }
4302
Jose Lima4b6c6692014-08-12 17:41:12 -07004303 void setVisibleBehindActivity(ActivityRecord r) {
4304 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004305 }
4306
Jose Lima4b6c6692014-08-12 17:41:12 -07004307 boolean hasVisibleBehindActivity() {
4308 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004309 }
4310
Craig Mautner34b73df2014-01-12 21:11:08 -08004311 @Override
4312 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004313 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4314 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004315
4316 @Override
4317 protected int getChildCount() {
4318 return mStacks.size();
4319 }
4320
4321 @Override
4322 protected ConfigurationContainer getChildAt(int index) {
4323 return mStacks.get(index);
4324 }
4325
4326 @Override
4327 protected ConfigurationContainer getParent() {
4328 return ActivityStackSupervisor.this;
4329 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004330 }
4331
4332 class VirtualActivityDisplay extends ActivityDisplay {
4333 VirtualDisplay mVirtualDisplay;
4334
Craig Mautner6985bad2014-04-21 15:22:06 -07004335 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004336 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004337 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4338 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4339 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4340 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004341
4342 init(mVirtualDisplay.getDisplay());
4343
4344 mWindowManager.handleDisplayAdded(mDisplayId);
4345 }
4346
4347 void setSurface(Surface surface) {
4348 if (mVirtualDisplay != null) {
4349 mVirtualDisplay.setSurface(surface);
4350 }
4351 }
4352
4353 @Override
4354 void detachActivitiesLocked(ActivityStack stack) {
4355 super.detachActivitiesLocked(stack);
4356 if (mVirtualDisplay != null) {
4357 mVirtualDisplay.release();
4358 mVirtualDisplay = null;
4359 }
4360 }
4361
4362 @Override
4363 public String toString() {
4364 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004365 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004366 }
Jose Lima58e66d62014-05-27 20:00:27 -07004367
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004368 ActivityStack findStackBehind(ActivityStack stack) {
4369 // TODO(multi-display): We are only looking for stacks on the default display.
4370 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4371 if (display == null) {
4372 return null;
4373 }
4374 final ArrayList<ActivityStack> stacks = display.mStacks;
4375 for (int i = stacks.size() - 1; i >= 0; i--) {
4376 if (stacks.get(i) == stack && i > 0) {
4377 return stacks.get(i - 1);
4378 }
4379 }
4380 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4381 + " in=" + stacks);
4382 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004383
Jorim Jaggic69bd222016-03-15 14:38:37 +01004384 /**
4385 * Puts a task into resizing mode during the next app transition.
4386 *
4387 * @param taskId the id of the task to put into resizing mode
4388 */
4389 private void setResizingDuringAnimation(int taskId) {
4390 mResizingTasksDuringAnimation.add(taskId);
4391 mWindowManager.setTaskDockedResizing(taskId, true);
4392 }
4393
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004394 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4395 final TaskRecord task;
4396 final int callingUid;
4397 final String callingPackage;
4398 final Intent intent;
4399 final int userId;
4400 final ActivityOptions activityOptions = (bOptions != null)
4401 ? new ActivityOptions(bOptions) : null;
4402 final int launchStackId = (activityOptions != null)
4403 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004404 if (launchStackId == HOME_STACK_ID) {
4405 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
4406 + taskId + " can't be launch in the home stack.");
4407 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004408
4409 if (launchStackId == DOCKED_STACK_ID) {
4410 mWindowManager.setDockedStackCreateState(
4411 activityOptions.getDockCreateMode(), null /* initialBounds */);
4412
4413 // Defer updating the stack in which recents is until the app transition is done, to
4414 // not run into issues where we still need to draw the task in recents but the
4415 // docked stack is already created.
4416 deferUpdateBounds(HOME_STACK_ID);
4417 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4418 }
4419
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004420 task = anyTaskForIdLocked(taskId, RESTORE_FROM_RECENTS, launchStackId);
4421 if (task == null) {
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004422 continueUpdateBounds(HOME_STACK_ID);
4423 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004424 throw new IllegalArgumentException(
4425 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4426 }
4427
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004428 // Since we don't have an actual source record here, we assume that the currently focused
4429 // activity was the source.
4430 final ActivityStack focusedStack = getFocusedStack();
4431 final ActivityRecord sourceRecord =
4432 focusedStack != null ? focusedStack.topActivity() : null;
4433
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004434 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004435 if (task.getStackId() != launchStackId) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004436 moveTaskToStackLocked(
4437 taskId, launchStackId, ON_TOP, FORCE_FOCUS, "startActivityFromRecents",
4438 ANIMATE);
4439 }
4440 }
4441
4442 // If the user must confirm credentials (e.g. when first launching a work app and the
4443 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4444 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4445 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004446 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004447 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004448 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004449 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4450 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004451
4452 // If we are launching the task in the docked stack, put it into resizing mode so
4453 // the window renders full-screen with the background filling the void. Also only
4454 // call this at the end to make sure that tasks exists on the window manager side.
4455 if (launchStackId == DOCKED_STACK_ID) {
Jorim Jaggic69bd222016-03-15 14:38:37 +01004456 setResizingDuringAnimation(taskId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004457 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004458
4459 mService.mActivityStarter.postStartActivityUncheckedProcessing(task.getTopActivity(),
4460 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004461 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4462 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004463 return ActivityManager.START_TASK_TO_FRONT;
4464 }
4465 callingUid = task.mCallingUid;
4466 callingPackage = task.mCallingPackage;
4467 intent = task.intent;
4468 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4469 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004470 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4471 null, null, 0, 0, bOptions, userId, null, task);
4472 if (launchStackId == DOCKED_STACK_ID) {
4473 setResizingDuringAnimation(task.taskId);
4474 }
4475 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004476 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004477
4478 /**
4479 * @return a list of activities which are the top ones in each visible stack. The first
4480 * entry will be the focused activity.
4481 */
4482 public List<IBinder> getTopVisibleActivities() {
Andrii Kulian1e32e022016-09-16 15:29:34 -07004483 // TODO(multi-display): Get rid of DEFAULT_DISPLAY here. Used in
4484 // VoiceInteractionManagerServiceImpl#showSessionLocked.
Amith Yamasanie8222e52016-04-08 15:28:47 -07004485 final ActivityDisplay display = mActivityDisplays.get(Display.DEFAULT_DISPLAY);
4486 if (display == null) {
4487 return Collections.EMPTY_LIST;
4488 }
4489 ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4490 final ArrayList<ActivityStack> stacks = display.mStacks;
4491 for (int i = stacks.size() - 1; i >= 0; i--) {
4492 ActivityStack stack = stacks.get(i);
4493 if (stack.getStackVisibilityLocked(null) == ActivityStack.STACK_VISIBLE) {
4494 ActivityRecord top = stack.topActivity();
4495 if (top != null) {
4496 if (stack == mFocusedStack) {
4497 topActivityTokens.add(0, top.appToken);
4498 } else {
4499 topActivityTokens.add(top.appToken);
4500 }
4501 }
4502 }
4503 }
4504 return topActivityTokens;
4505 }
Craig Mautner27084302013-03-25 08:05:25 -07004506}