blob: af3f352b369eded51551b8a4c3d96baee7406be1 [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
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020020import static android.Manifest.permission.START_ANY_ACTIVITY;
21import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
22import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
23import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
24import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.START_TASK_TO_FRONT;
26import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
27import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.HOME_STACK_ID;
32import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
33import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
34import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080035import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020036import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
37import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
38import static android.content.pm.PackageManager.PERMISSION_GRANTED;
39import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
40import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070041import static android.view.Display.FLAG_PRIVATE;
42import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080043import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Jorim Jaggife762342016-10-13 14:33:27 +020044
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
74import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
75import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
77import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
78import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
79import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
81import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
82import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
84import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
85import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
86import static com.android.server.am.ActivityStack.STACK_VISIBLE;
87import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080092import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
93import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
94import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080096import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020097
Svetoslav7008b512015-06-24 18:47:07 -070098import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080099import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100101import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800104import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800109import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700113import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400114import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Intent;
119import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700121import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.pm.PackageManager;
123import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100124import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800126import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800127import android.hardware.display.DisplayManager;
128import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800129import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800131import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800132import android.hardware.input.InputManager;
133import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100135import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700137import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700140import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700142import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700143import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700144import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400145import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700147import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700148import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700149import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100150import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700151import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700152import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400153import android.provider.Settings;
154import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700157import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700158import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800159import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700160import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800163import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800164import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800165import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800166
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800167import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800168import com.android.internal.logging.MetricsLogger;
169import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700170import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400171import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700172import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400173import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800174import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700175import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700176import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700177
Craig Mautner8d341ef2013-03-26 09:03:27 -0700178import java.io.FileDescriptor;
179import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700180import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800181import java.lang.annotation.Retention;
182import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700183import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700184import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700186import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700187
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800188public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700191 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700192 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700193 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700194 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800200 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800202
Craig Mautnerf3333272013-04-22 10:55:53 -0700203 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700204 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700205
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700209 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211
Craig Mautner05d29032013-05-03 13:40:13 -0700212 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
213 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
214 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800217 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
218 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
219 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700220 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400221 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
222 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700223 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700224 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700225 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800226 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
227 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700230
Jason Monk62515be2014-05-21 16:06:19 -0400231 private static final String LOCK_TASK_TAG = "Lock-to-App";
232
Wale Ogunwale040b4702015-08-06 18:10:50 -0700233 // Used to indicate if an object (e.g. stack) that we are trying to get
234 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800235 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700237 // Used to indicate that windows of activities should be preserved during the resize.
238 static final boolean PRESERVE_WINDOWS = true;
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. task) should be moved/created
241 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700242 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
244 // Used to indicate that an objects (e.g. task) removal from its container
245 // (e.g. stack) is due to it moving to another container.
246 static final boolean MOVING = true;
247
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700248 // Force the focus to change to the stack we are moving a task to..
249 static final boolean FORCE_FOCUS = true;
250
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700251 // Don't execute any calls to resume.
252 static final boolean DEFER_RESUME = true;
253
Winson Chung010927a2016-12-15 16:12:35 -0800254 // Used to indicate that a task is removed it should also be removed from recents.
255 static final boolean REMOVE_FROM_RECENTS = true;
256
Winson Chung6954fc92017-03-24 16:22:12 -0700257 // Used to indicate that pausing an activity should occur immediately without waiting for
258 // the activity callback indicating that it has completed pausing
259 static final boolean PAUSE_IMMEDIATELY = true;
260
Winson Chung7900bee2017-03-10 08:59:25 -0800261 /**
262 * The modes which affect which tasks are returned when calling
263 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
264 */
265 @Retention(RetentionPolicy.SOURCE)
266 @IntDef({
267 MATCH_TASK_IN_STACKS_ONLY,
268 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
269 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
270 })
271 public @interface AnyTaskForIdMatchTaskMode {}
272 // Match only tasks in the current stacks
273 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
274 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
275 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
276 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
277 // provided stack id
278 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
279
Svetoslav7008b512015-06-24 18:47:07 -0700280 // Activity actions an app cannot start if it uses a permission which is not granted.
281 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
282 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700283
Svetoslav7008b512015-06-24 18:47:07 -0700284 static {
285 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
286 Manifest.permission.CAMERA);
287 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
288 Manifest.permission.CAMERA);
289 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
290 Manifest.permission.CALL_PHONE);
291 }
292
Svet Ganov99b60432015-06-27 13:15:22 -0700293 /** Action restriction: launching the activity is not restricted. */
294 private static final int ACTIVITY_RESTRICTION_NONE = 0;
295 /** Action restriction: launching the activity is restricted by a permission. */
296 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
297 /** Action restriction: launching the activity is restricted by an app op. */
298 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700299
justinzhang5286d3f2014-05-12 17:06:01 -0400300 /** Status Bar Service **/
301 private IBinder mToken = new Binder();
302 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400303 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400304
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305 // For debugging to make sure the caller when acquiring/releasing our
306 // wake lock is the system process.
307 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800308 /** The number of distinct task ids that can be assigned to the tasks of a single user */
309 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700310
Craig Mautner27084302013-03-25 08:05:25 -0700311 final ActivityManagerService mService;
312
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800313 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800314
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700315 final ActivityStackSupervisorHandler mHandler;
316
317 /** Short cut */
318 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700320
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700321 /** Counter for next free stack ID to use for dynamic activity stacks. */
322 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700323
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800324 /**
325 * Maps the task identifier that activities are currently being started in to the userId of the
326 * task. Each time a new task is created, the entry for the userId of the task is incremented
327 */
328 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700329
Craig Mautner2420ead2013-04-01 17:13:20 -0700330 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800331 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700332
Craig Mautnere0a38842013-12-16 16:14:02 -0800333 /** The stack containing the launcher app. Assumed to always be attached to
334 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800335 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700336
Craig Mautnere0a38842013-12-16 16:14:02 -0800337 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800338 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700339
Craig Mautner4a1cb222013-12-04 16:14:06 -0800340 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
341 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800342 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800343 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700344
345 /** List of activities that are waiting for a new activity to become visible before completing
346 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800347 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700348
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700349 /** List of processes waiting to find out about the next visible activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700350 final ArrayList<WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700351
352 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700353 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355 /** List of activities that are ready to be stopped, but waiting for the next activity to
356 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800357 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358
Craig Mautnerf3333272013-04-22 10:55:53 -0700359 /** List of activities that are ready to be finished, but waiting for the previous activity to
360 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800361 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700362
Craig Mautner0eea92c2013-05-16 13:35:39 -0700363 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700365
Wale Ogunwale22e25262016-02-01 10:32:02 -0800366 /** List of activities whose multi-window mode changed that we need to report to the
367 * application */
368 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
369
370 /** List of activities whose picture-in-picture mode changed that we need to report to the
371 * application */
372 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
373
Winson Chung5af42fc2017-03-24 17:11:33 -0700374 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
375 * the application */
376 Rect mPipModeChangedTargetStackBounds;
377
Craig Mautnerf3333272013-04-22 10:55:53 -0700378 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700379 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700380
Craig Mautnerde4ef022013-04-07 19:01:33 -0700381 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
382 * is being brought in front of us. */
383 boolean mUserLeaving = false;
384
Craig Mautner0eea92c2013-05-16 13:35:39 -0700385 /** Set when we have taken too long waiting to go to sleep. */
386 boolean mSleepTimeout = false;
387
388 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700389 * We don't want to allow the device to go to sleep while in the process
390 * of launching an activity. This is primarily to allow alarm intent
391 * receivers to launch an activity and get that to run before the device
392 * goes back to sleep.
393 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700394 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700395
396 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700397 * Set when the system is going to sleep, until we have
398 * successfully paused the current activity and released our wake lock.
399 * At that point the system is allowed to actually sleep.
400 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700401 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700402
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700403 /** Stack id of the front stack when user switched, indexed by userId. */
404 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700405
Craig Mautner4504de52013-12-20 09:06:56 -0800406 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800407 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800408 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800409
Bryce Leeaf3a4002017-03-20 10:37:12 -0700410 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800411 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700412 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800414 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
415
416 private DisplayManagerInternal mDisplayManagerInternal;
417 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800418
Craig Mautner15df08a2015-04-01 12:17:18 -0700419 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
420 * may be finished until there is only one entry left. If this is empty the system is not
421 * in lockTask mode. */
422 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000423 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700424 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
425 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000426 */
427 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400428 /**
429 * Notifies the user when entering/exiting lock-task.
430 */
431 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800432
Wale Ogunwaled046a012015-12-24 13:05:59 -0800433 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800434 boolean inResumeTopActivity;
435
Andrii Kulian1e32e022016-09-16 15:29:34 -0700436 /**
437 * Temporary rect used during docked stack resize calculation so we don't need to create a new
438 * object each time.
439 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700440 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700441
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700442 // The default minimal size that will be used if the activity doesn't specify its minimal size.
443 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700444 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700445
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700446 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
447 private boolean mTaskLayersChanged = true;
448
Jorim Jaggi275561a2016-02-23 10:11:02 -0500449 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800450
Andrii Kulian1779e612016-10-12 21:58:25 -0700451 @Override
452 protected int getChildCount() {
453 return mActivityDisplays.size();
454 }
455
456 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700457 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700458 return mActivityDisplays.valueAt(index);
459 }
460
461 @Override
462 protected ConfigurationContainer getParent() {
463 return null;
464 }
465
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 Configuration getDisplayOverrideConfiguration(int displayId) {
467 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
468 if (activityDisplay == null) {
469 throw new IllegalArgumentException("No display found with id: " + displayId);
470 }
471
472 return activityDisplay.getOverrideConfiguration();
473 }
474
475 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
476 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
482 }
483
Wale Ogunwale39381972015-12-17 17:15:29 -0800484 static class FindTaskResult {
485 ActivityRecord r;
486 boolean matchedByRootAffinity;
487 }
488 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
489
Craig Mautneree36c772014-07-16 14:56:05 -0700490 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800491 * Temp storage for display ids sorted in focus order.
492 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
493 * it's more efficient, as the number of displays is usually small.
494 */
495 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
496
497 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100498 * Used to keep track whether app visibilities got changed since the last pause. Useful to
499 * determine whether to invoke the task stack change listener after pausing.
500 */
501 boolean mAppVisibilitiesChangedSinceLastPause;
502
503 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100504 * Set of tasks that are in resizing mode during an app transition to fill the "void".
505 */
506 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
507
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700508
509 /**
510 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
511 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
512 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
513 * like the docked stack going empty.
514 */
515 private boolean mAllowDockedStackResize = true;
516
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100517 /**
Tony Mak853304c2016-04-18 15:17:41 +0100518 * Is dock currently minimized.
519 */
520 boolean mIsDockMinimized;
521
Jorim Jaggife762342016-10-13 14:33:27 +0200522 final KeyguardController mKeyguardController;
523
Tony Mak853304c2016-04-18 15:17:41 +0100524 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700525 * Description of a request to start a new activity, which has been held
526 * due to app switches being disabled.
527 */
528 static class PendingActivityLaunch {
529 final ActivityRecord r;
530 final ActivityRecord sourceRecord;
531 final int startFlags;
532 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700533 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700534
535 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700536 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700537 r = _r;
538 sourceRecord = _sourceRecord;
539 startFlags = _startFlags;
540 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700541 callerApp = _callerApp;
542 }
543
544 void sendErrorResult(String message) {
545 try {
546 if (callerApp.thread != null) {
547 callerApp.thread.scheduleCrash(message);
548 }
549 } catch (RemoteException e) {
550 Slog.e(TAG, "Exception scheduling crash of failed "
551 + "activity launcher sourceRecord=" + sourceRecord, e);
552 }
Craig Mautneree36c772014-07-16 14:56:05 -0700553 }
554 }
555
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800556 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700557 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700558 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800559 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200560 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700561 }
562
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800563 void setRecentTasks(RecentTasks recentTasks) {
564 mRecentTasks = recentTasks;
565 }
566
Jeff Brown2c43c332014-06-12 22:38:59 -0700567 /**
568 * At the time when the constructor runs, the power manager has not yet been
569 * initialized. So we initialize our wakelocks afterwards.
570 */
571 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700573 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700574 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700575 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700576 }
577
justinzhang5286d3f2014-05-12 17:06:01 -0400578 // This function returns a IStatusBarService. The value is from ServiceManager.
579 // getService and is cached.
580 private IStatusBarService getStatusBarService() {
581 synchronized (mService) {
582 if (mStatusBarService == null) {
583 mStatusBarService = IStatusBarService.Stub.asInterface(
584 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
585 if (mStatusBarService == null) {
586 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
587 }
588 }
589 return mStatusBarService;
590 }
591 }
592
Jason Monk35c62a42014-06-17 10:24:47 -0400593 private IDevicePolicyManager getDevicePolicyManager() {
594 synchronized (mService) {
595 if (mDevicePolicyManager == null) {
596 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
597 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
598 if (mDevicePolicyManager == null) {
599 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
600 }
601 }
602 return mDevicePolicyManager;
603 }
604 }
605
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700606 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 synchronized (mService) {
608 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200609 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610
611 mDisplayManager =
612 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
613 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800614 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615
616 Display[] displays = mDisplayManager.getDisplays();
617 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
618 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700620 if (activityDisplay.mDisplay == null) {
621 throw new IllegalStateException("Default Display does not exist");
622 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700624 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 }
626
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800627 mHomeStack = mFocusedStack = mLastFocusedStack =
628 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800629
630 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 }
Craig Mautner27084302013-03-25 08:05:25 -0700632 }
633
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700634 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800639 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700640 }
641
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700642 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700643 if (stack == null) {
644 return false;
645 }
646
Craig Mautnerdf88d732014-01-27 09:21:32 -0800647 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
648 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700649 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800650 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800651 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700652 }
653
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800654 /** The top most stack on its display. */
655 boolean isFrontStackOnDisplay(ActivityStack stack) {
656 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
657 }
658
659 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700660 if (stack == null) {
661 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700663
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700664 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
665 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700666 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800667 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800668 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700669 }
670
Wale Ogunwaled046a012015-12-24 13:05:59 -0800671 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800672 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
673 if (!focusCandidate.isFocusable()) {
674 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800675 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800676 }
677
678 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800679 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800680 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800681
Wale Ogunwaled046a012015-12-24 13:05:59 -0800682 EventLogTags.writeAmFocusedStack(
683 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
684 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
685 }
686
687 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800688 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800689 if (r != null && r.idle) {
690 checkFinishBootingLocked();
691 }
692 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700693 }
694
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700695 void moveHomeStackToFront(String reason) {
696 mHomeStack.moveToFront(reason);
697 }
698
Matthew Ng330757d2017-02-28 14:19:17 -0800699 void moveRecentsStackToFront(String reason) {
700 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
701 if (recentsStack != null) {
702 recentsStack.moveToFront(reason);
703 }
704 }
705
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700706 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800707 boolean moveHomeStackTaskToTop(String reason) {
708 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700709
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700710 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700711 if (top == null) {
712 return false;
713 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700714 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700715 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700716 }
717
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800718 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700719 if (!mService.mBooting && !mService.mBooted) {
720 // Not ready yet!
721 return false;
722 }
723
Craig Mautner84984fa2014-06-19 11:19:20 -0700724 if (prev != null) {
725 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
726 }
727
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800728 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700729 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800730 final String myReason = reason + " resumeHomeStackTask";
731
Mark Lua56ea122015-10-08 13:31:01 +0800732 // Only resume home activity if isn't finishing.
733 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700734 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800735 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700736 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800737 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700738 }
739
Craig Mautner8d341ef2013-03-26 09:03:27 -0700740 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800741 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
742 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700743 }
744
745 /**
746 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
747 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800748 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700749 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800750 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
751 * valid if the matchMode is
752 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700753 */
Winson Chung7900bee2017-03-10 08:59:25 -0800754 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
755 // If there is a stack id set, ensure that we are attempting to actually restore a task
756 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
757 stackId != INVALID_STACK_ID) {
758 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
759 }
760
Craig Mautnere0a38842013-12-16 16:14:02 -0800761 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800763 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
765 ActivityStack stack = stacks.get(stackNdx);
766 TaskRecord task = stack.taskForIdLocked(id);
767 if (task != null) {
768 return task;
769 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700770 }
771 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800772
Winson Chung7900bee2017-03-10 08:59:25 -0800773 // If we are matching stack tasks only, return now
774 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800775 return null;
776 }
777
Winson Chung7900bee2017-03-10 08:59:25 -0800778 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
779 // the task from recents
780 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
781 TaskRecord task = mRecentTasks.taskForIdLocked(id);
782 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
783 if (DEBUG_RECENTS && task == null) {
784 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
785 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700786 return task;
787 }
788
Winson Chung7900bee2017-03-10 08:59:25 -0800789 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700790 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700791 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
792 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800793 return null;
794 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700795 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800796 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700797 }
798
Craig Mautner6170f732013-04-02 13:05:23 -0700799 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800800 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800801 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
804 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
805 if (r != null) {
806 return r;
807 }
Craig Mautner6170f732013-04-02 13:05:23 -0700808 }
809 }
810 return null;
811 }
812
Tony Mak853304c2016-04-18 15:17:41 +0100813 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000814 * Detects whether we should show a lock screen in front of this task for a locked user.
815 * <p>
816 * We'll do this if either of the following holds:
817 * <ul>
818 * <li>The top activity explicitly belongs to {@param userId}.</li>
819 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
820 * </ul>
821 *
822 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100823 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000824 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
825 // To handle the case that work app is in the task but just is not the top one.
826 final ActivityRecord activityRecord = task.getTopActivity();
827 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
828
829 return (activityRecord != null && activityRecord.userId == userId)
830 || (resultTo != null && resultTo.userId == userId);
831 }
832
833 /**
834 * Find all visible task stacks containing {@param userId} and intercept them with an activity
835 * to block out the contents and possibly start a credential-confirming intent.
836 *
837 * @param userId user handle for the locked managed profile.
838 */
839 void lockAllProfileTasks(@UserIdInt int userId) {
840 mWindowManager.deferSurfaceLayout();
841 try {
842 final List<ActivityStack> stacks = getStacks();
843 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
844 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
845 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
846 final TaskRecord task = tasks.get(taskNdx);
847
848 // Check the task for a top activity belonging to userId, or returning a result
849 // to an activity belonging to userId. Example case: a document picker for
850 // personal files, opened by a work app, should still get locked.
851 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000852 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
853 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000854 }
Tony Mak853304c2016-04-18 15:17:41 +0100855 }
856 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000857 } finally {
858 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100859 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000860 }
861
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800862 void setNextTaskIdForUserLocked(int taskId, int userId) {
863 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
864 if (taskId > currentTaskId) {
865 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700866 }
867 }
868
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700869 static int nextTaskIdForUser(int taskId, int userId) {
870 int nextTaskId = taskId + 1;
871 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
872 // Wrap around as there will be smaller task ids that are available now.
873 nextTaskId -= MAX_TASK_IDS_PER_USER;
874 }
875 return nextTaskId;
876 }
877
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800878 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800879 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
880 // for a userId u, a taskId can only be in the range
881 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
882 // 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 -0700883 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800884 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800885 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800886 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700887 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800888 if (candidateTaskId == currentTaskId) {
889 // Something wrong!
890 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
891 throw new IllegalStateException("Cannot get an available task id."
892 + " Reached limit of " + MAX_TASK_IDS_PER_USER
893 + " running tasks per user.");
894 }
895 }
896 mCurTaskIdForUser.put(userId, candidateTaskId);
897 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700898 }
899
Chong Zhang6cda19c2016-06-14 19:07:56 -0700900 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800901 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700902 if (stack == null) {
903 return null;
904 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700905 ActivityRecord resumedActivity = stack.mResumedActivity;
906 if (resumedActivity == null || resumedActivity.app == null) {
907 resumedActivity = stack.mPausingActivity;
908 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700909 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700910 }
911 }
912 return resumedActivity;
913 }
914
Dianne Hackbornff072722014-09-24 10:56:28 -0700915 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700916 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800917 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800918 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
919 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
921 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700922 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 continue;
924 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700925 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 if (hr != null) {
927 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
928 && processName.equals(hr.processName)) {
929 try {
George Mount2c92c972014-03-20 09:38:23 -0700930 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 didSomething = true;
932 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700933 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 Slog.w(TAG, "Exception in new application when starting activity "
935 + hr.intent.getComponent().flattenToShortString(), e);
936 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700937 }
Craig Mautner20e72272013-04-01 13:45:53 -0700938 }
Craig Mautner20e72272013-04-01 13:45:53 -0700939 }
940 }
941 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700942 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700943 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700944 }
Craig Mautner20e72272013-04-01 13:45:53 -0700945 return didSomething;
946 }
947
948 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
950 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800951 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
952 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700953 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800954 continue;
955 }
956 final ActivityRecord resumedActivity = stack.mResumedActivity;
957 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700958 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800959 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 return false;
961 }
Craig Mautner20e72272013-04-01 13:45:53 -0700962 }
963 }
Wei Wang65c7a152016-06-02 18:51:22 -0700964 // Send launch end powerhint when idle
965 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700966 return true;
967 }
968
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
971 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
973 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700974 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700976 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800977 return false;
978 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700979 }
980 }
981 }
982 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700983 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
985 mLastFocusedStack + " to=" + mFocusedStack);
986 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 return true;
988 }
989
990 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800991 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
993 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
995 final ActivityStack stack = stacks.get(stackNdx);
996 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800997 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700998 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800999 return false;
1000 }
1001 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 }
1004 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001005 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001006 }
1007
Craig Mautner2acc3892013-09-23 10:28:14 -07001008 /**
1009 * Pause all activities in either all of the stacks or just the back stacks.
1010 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001011 * @param resuming The resuming activity.
1012 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1013 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001014 * @return true if any activity was paused as a result of this call.
1015 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001016 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001017 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1019 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1021 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001022 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001023 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001024 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001025 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1026 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001027 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001028 }
1029 }
1030 return someActivityPaused;
1031 }
1032
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001034 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001035 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1036 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1038 final ActivityStack stack = stacks.get(stackNdx);
1039 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001040 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001041 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001042 Slog.d(TAG_STATES,
1043 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 pausing = false;
1045 } else {
1046 return false;
1047 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001048 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001049 }
1050 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001051 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001052 }
1053
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001054 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001055 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001056 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001057 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1058 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1059 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1060 final ActivityStack stack = stacks.get(stackNdx);
1061 if (stack.mResumedActivity != null &&
1062 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001063 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001064 }
1065 }
1066 }
1067 }
1068
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001069 void cancelInitializingActivities() {
1070 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1071 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1072 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1073 stacks.get(stackNdx).cancelInitializingActivities();
1074 }
1075 }
1076 }
1077
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001079 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001080 }
1081
1082 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1083 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001084 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001086 if (w.who == null) {
1087 changed = true;
1088 w.timeout = false;
1089 if (r != null) {
1090 w.who = new ComponentName(r.info.packageName, r.info.name);
1091 }
1092 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1093 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001096 if (changed) {
1097 mService.notifyAll();
1098 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001099 }
1100
Chong Zhang5022da32016-06-21 16:31:37 -07001101 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1102 boolean changed = false;
1103 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1104 WaitResult w = mWaitingActivityLaunched.remove(i);
1105 if (w.who == null) {
1106 changed = true;
1107 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1108 // the starting activity ends up moving another activity to front, and it should
1109 // wait for this new activity to become visible instead.
1110 // Do not modify other fields.
1111 w.result = START_TASK_TO_FRONT;
1112 }
1113 }
1114 if (changed) {
1115 mService.notifyAll();
1116 }
1117 }
1118
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1120 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001121 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001123 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 if (w.who == null) {
1125 changed = true;
1126 w.timeout = timeout;
1127 if (r != null) {
1128 w.who = new ComponentName(r.info.packageName, r.info.name);
1129 }
1130 w.thisTime = thisTime;
1131 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001132 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001133 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001134 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001135 if (changed) {
1136 mService.notifyAll();
1137 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001138 }
1139
Craig Mautner29219d92013-04-16 20:19:12 -07001140 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001141 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001142 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001143 if (r != null) {
1144 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001145 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001146
Andrii Kulian7318d632016-07-20 18:59:28 -07001147 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001148 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1149
1150 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1151 final int displayId = mTmpOrderedDisplayIds.get(i);
1152 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1153 if (stacks == null) {
1154 continue;
1155 }
1156 for (int j = stacks.size() - 1; j >= 0; --j) {
1157 final ActivityStack stack = stacks.get(j);
1158 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1159 r = stack.topRunningActivityLocked();
1160 if (r != null) {
1161 return r;
1162 }
Craig Mautner29219d92013-04-16 20:19:12 -07001163 }
1164 }
1165 }
1166 return null;
1167 }
1168
Dianne Hackborn09233282014-04-30 11:33:59 -07001169 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001170 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001171 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1172 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001173 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001174 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001175 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001176 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1177 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001178 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001179 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001180 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001181 }
1182 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001183
1184 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1185 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1186 while (maxNum > 0) {
1187 long mostRecentActiveTime = Long.MIN_VALUE;
1188 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001189 final int numTaskLists = runningTaskLists.size();
1190 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1191 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001192 if (!stackTaskList.isEmpty()) {
1193 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1194 if (lastActiveTime > mostRecentActiveTime) {
1195 mostRecentActiveTime = lastActiveTime;
1196 selectedStackList = stackTaskList;
1197 }
1198 }
1199 }
1200 if (selectedStackList != null) {
1201 list.add(selectedStackList.remove(0));
1202 --maxNum;
1203 } else {
1204 break;
1205 }
1206 }
Craig Mautner20e72272013-04-01 13:45:53 -07001207 }
1208
Todd Kennedy7440f172015-12-09 14:31:22 -08001209 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1210 ProfilerInfo profilerInfo) {
1211 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001212 if (aInfo != null) {
1213 // Store the found target back into the intent, because now that
1214 // we have it we never want to do this again. For example, if the
1215 // user navigates back to this point in the history, we should
1216 // always restart the exact same activity.
1217 intent.setComponent(new ComponentName(
1218 aInfo.applicationInfo.packageName, aInfo.name));
1219
1220 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001221 if (!aInfo.processName.equals("system")) {
1222 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001223 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001224 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001225
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001226 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1227 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1228 }
1229
Man Caocfa78b22015-06-11 20:14:34 -07001230 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1231 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1232 }
1233
1234 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001235 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001236 }
1237 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001238 final String intentLaunchToken = intent.getLaunchToken();
1239 if (aInfo.launchToken == null && intentLaunchToken != null) {
1240 aInfo.launchToken = intentLaunchToken;
1241 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001242 }
1243 return aInfo;
1244 }
1245
Todd Kennedy7440f172015-12-09 14:31:22 -08001246 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001247 return resolveIntent(intent, resolvedType, userId, 0);
1248 }
1249
1250 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001251 synchronized (mService) {
1252 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001253 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001254 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001255 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001256 }
1257
1258 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1259 ProfilerInfo profilerInfo, int userId) {
1260 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1261 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1262 }
1263
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001264 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1265 boolean andResume, boolean checkConfig) throws RemoteException {
1266
1267 if (!allPausedActivitiesComplete()) {
1268 // While there are activities pausing we skipping starting any new activities until
1269 // pauses are complete. NOTE: that we also do this for activities that are starting in
1270 // the paused state because they will first be resumed then paused on the client side.
1271 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1272 "realStartActivityLocked: Skipping start of r=" + r
1273 + " some activities pausing...");
1274 return false;
1275 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001276
Andrii Kulianada10292017-02-09 23:19:29 -08001277 r.startFreezingScreenLocked(app, 0);
1278 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001279
Andrii Kulianada10292017-02-09 23:19:29 -08001280 // schedule launch ticks to collect information about slow apps.
1281 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001282
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001283 // Have the window manager re-evaluate the orientation of the screen based on the new
1284 // activity order. Note that as a result of this, it can call back into the activity
1285 // manager with a new orientation. We don't care about that, because the activity is not
1286 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001287 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001288 final int displayId = r.getDisplayId();
1289 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1290 getDisplayOverrideConfiguration(displayId),
1291 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001292 // Deferring resume here because we're going to launch new activity shortly.
1293 // We don't want to perform a redundant launch of the same record while ensuring
1294 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001295 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1296 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 }
1298
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001299 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001300 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001301 }
1302
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 r.app = app;
1304 app.waitingToKill = null;
1305 r.launchCount++;
1306 r.lastLaunchTime = SystemClock.uptimeMillis();
1307
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001308 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001309
1310 int idx = app.activities.indexOf(r);
1311 if (idx < 0) {
1312 app.activities.add(r);
1313 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001314 mService.updateLruProcessLocked(app, true, null);
1315 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001316
Craig Mautner15df08a2015-04-01 12:17:18 -07001317 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001318 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1319 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001320 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001321 }
1322
Andrii Kulian02b7a832016-10-06 23:11:56 -07001323 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 try {
1325 if (app.thread == null) {
1326 throw new RemoteException();
1327 }
1328 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001329 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001330 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001331 // We don't need to deliver new intents and/or set results if activity is going
1332 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 results = r.results;
1334 newIntents = r.newIntents;
1335 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001336 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1337 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1338 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001339 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1340 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001341 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001342 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001343 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001345 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1346 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 r.sleeping = false;
1348 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001349 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001350 mService.showAskCompatModeDialogLocked(r);
1351 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001352 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1354 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1355 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001356 final String profileFile = mService.mProfileFile;
1357 if (profileFile != null) {
1358 ParcelFileDescriptor profileFd = mService.mProfileFd;
1359 if (profileFd != null) {
1360 try {
1361 profileFd = profileFd.dup();
1362 } catch (IOException e) {
1363 if (profileFd != null) {
1364 try {
1365 profileFd.close();
1366 } catch (IOException o) {
1367 }
1368 profileFd = null;
1369 }
1370 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001372
1373 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001374 mService.mSamplingInterval, mService.mAutoStopProfiler,
1375 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 }
1377 }
1378 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001379
Andrii Kulianada10292017-02-09 23:19:29 -08001380 app.hasShownUi = true;
1381 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001382 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001383 // Because we could be starting an Activity in the system process this may not go across
1384 // a Binder interface which would create a new Configuration. Consequently we have to
1385 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001386
1387 final Configuration globalConfiguration =
1388 new Configuration(mService.getGlobalConfiguration());
1389 r.setLastReportedGlobalConfiguration(globalConfiguration);
1390 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001391 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001392 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1393
Craig Mautner2420ead2013-04-01 17:13:20 -07001394 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001395 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001396 globalConfiguration,
1397 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001398 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1399 r.persistentState, results, newIntents, !andResume,
1400 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001401
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001402 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001403 // This may be a heavy-weight process! Note that the package
1404 // manager will ensure that only activity can run in the main
1405 // process of the .apk, which is the only thing that will be
1406 // considered heavy-weight.
1407 if (app.processName.equals(app.info.packageName)) {
1408 if (mService.mHeavyWeightProcess != null
1409 && mService.mHeavyWeightProcess != app) {
1410 Slog.w(TAG, "Starting new heavy weight process " + app
1411 + " when already running "
1412 + mService.mHeavyWeightProcess);
1413 }
1414 mService.mHeavyWeightProcess = app;
1415 Message msg = mService.mHandler.obtainMessage(
1416 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1417 msg.obj = r;
1418 mService.mHandler.sendMessage(msg);
1419 }
1420 }
1421
1422 } catch (RemoteException e) {
1423 if (r.launchFailed) {
1424 // This is the second time we failed -- finish activity
1425 // and give up.
1426 Slog.e(TAG, "Second failure launching "
1427 + r.intent.getComponent().flattenToShortString()
1428 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001429 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001430 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1431 "2nd-crash", false);
1432 return false;
1433 }
1434
1435 // This is the first time we failed -- restart process and
1436 // retry.
1437 app.activities.remove(r);
1438 throw e;
1439 }
1440
1441 r.launchFailed = false;
1442 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001443 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001444 }
1445
1446 if (andResume) {
1447 // As part of the process of launching, ActivityThread also performs
1448 // a resume.
1449 stack.minimalResumeActivityLocked(r);
1450 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001451 // This activity is not starting in the resumed state... which should look like we asked
1452 // it to pause+stop (but remain visible), and it has done so and reported back the
1453 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001454 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001455 "Moving to PAUSED: " + r + " (starting in paused state)");
1456 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001457 }
1458
1459 // Launch the new version setup screen if needed. We do this -after-
1460 // launching the initial activity (that is, home), so that it can have
1461 // a chance to initialize itself while in the background, making the
1462 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001463 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001464 mService.startSetupActivityLocked();
1465 }
1466
Dianne Hackborn465fa392014-09-14 14:21:18 -07001467 // Update any services we are bound to that might care about whether
1468 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001469 if (r.app != null) {
1470 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1471 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001472
Craig Mautner2420ead2013-04-01 17:13:20 -07001473 return true;
1474 }
1475
Craig Mautnere79d42682013-04-01 19:01:53 -07001476 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001477 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001478 // Is this activity's application already running?
1479 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001480 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001481
Andrii Kulian02b7a832016-10-06 23:11:56 -07001482 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001483
1484 if (app != null && app.thread != null) {
1485 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001486 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1487 || !"android".equals(r.info.packageName)) {
1488 // Don't add this if it is a platform component that is marked
1489 // to run in multiple processes, because this is actually
1490 // part of the framework so doesn't make sense to track as a
1491 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001492 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1493 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001494 }
George Mount2c92c972014-03-20 09:38:23 -07001495 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001496 return;
1497 } catch (RemoteException e) {
1498 Slog.w(TAG, "Exception when starting activity "
1499 + r.intent.getComponent().flattenToShortString(), e);
1500 }
1501
1502 // If a dead object exception was thrown -- fall through to
1503 // restart the application.
1504 }
1505
1506 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001507 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001508 }
1509
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001510 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001511 String resultWho, int requestCode, int callingPid, int callingUid,
1512 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001513 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001514 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1515 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001516 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001517 return true;
1518 }
1519 final int componentRestriction = getComponentRestrictionForCallingPackage(
1520 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1521 final int actionRestriction = getActionRestrictionForCallingPackage(
1522 intent.getAction(), callingPackage, callingPid, callingUid);
1523 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1524 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1525 if (resultRecord != null) {
1526 resultStack.sendActivityResultLocked(-1,
1527 resultRecord, resultWho, requestCode,
1528 Activity.RESULT_CANCELED, null);
1529 }
1530 final String msg;
1531 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1532 msg = "Permission Denial: starting " + intent.toString()
1533 + " from " + callerApp + " (pid=" + callingPid
1534 + ", uid=" + callingUid + ")" + " with revoked permission "
1535 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1536 } else if (!aInfo.exported) {
1537 msg = "Permission Denial: starting " + intent.toString()
1538 + " from " + callerApp + " (pid=" + callingPid
1539 + ", uid=" + callingUid + ")"
1540 + " not exported from uid " + aInfo.applicationInfo.uid;
1541 } else {
1542 msg = "Permission Denial: starting " + intent.toString()
1543 + " from " + callerApp + " (pid=" + callingPid
1544 + ", uid=" + callingUid + ")"
1545 + " requires " + aInfo.permission;
1546 }
1547 Slog.w(TAG, msg);
1548 throw new SecurityException(msg);
1549 }
1550
1551 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1552 final String message = "Appop Denial: starting " + intent.toString()
1553 + " from " + callerApp + " (pid=" + callingPid
1554 + ", uid=" + callingUid + ")"
1555 + " requires " + AppOpsManager.permissionToOp(
1556 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1557 Slog.w(TAG, message);
1558 return false;
1559 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1560 final String message = "Appop Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")"
1563 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1564 Slog.w(TAG, message);
1565 return false;
1566 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001567 if (options != null) {
1568 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1569 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1570 callingPid, callingUid);
1571 if (startInTaskPerm != PERMISSION_GRANTED) {
1572 final String msg = "Permission Denial: starting " + intent.toString()
1573 + " from " + callerApp + " (pid=" + callingPid
1574 + ", uid=" + callingUid + ") with launchTaskId="
1575 + options.getLaunchTaskId();
1576 Slog.w(TAG, msg);
1577 throw new SecurityException(msg);
1578 }
1579 }
1580 // Check if someone tries to launch an activity on a private display with a different
1581 // owner.
1582 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001583 if (launchDisplayId != INVALID_DISPLAY
1584 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1585 final String msg = "Permission Denial: starting " + intent.toString()
1586 + " from " + callerApp + " (pid=" + callingPid
1587 + ", uid=" + callingUid + ") with launchDisplayId="
1588 + launchDisplayId;
1589 Slog.w(TAG, msg);
1590 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001591 }
1592 }
1593
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001594 return true;
1595 }
1596
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001597 /** Check if caller is allowed to launch activities on specified display. */
1598 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1599 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1600 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1601
1602 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1603 if (activityDisplay == null) {
1604 Slog.w(TAG, "Launch on display check: display not found");
1605 return false;
1606 }
1607
1608 if (!activityDisplay.isPrivate()) {
1609 // Anyone can launch on a public display.
1610 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1611 + " allow launch on public display");
1612 return true;
1613 }
1614
1615 // Check if the caller is the owner of the display.
1616 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1617 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1618 + " allow launch for owner of the display");
1619 return true;
1620 }
1621
1622 // Check if caller is present on display
1623 if (activityDisplay.isUidPresent(callingUid)) {
1624 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1625 + " allow launch for caller present on the display");
1626 return true;
1627 }
1628
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001629 // Check if the caller can manage activity stacks.
1630 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001631 callingUid);
1632 if (startAnyPerm == PERMISSION_GRANTED) {
1633 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1634 + " allow launch any on display");
1635 return true;
1636 }
1637
1638 Slog.w(TAG, "Launch on display check: denied");
1639 return false;
1640 }
1641
1642 /** Update lists of UIDs that are present on displays and have access to them. */
1643 void updateUIDsPresentOnDisplay() {
1644 mDisplayAccessUIDs.clear();
1645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1646 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001647 // Only bother calculating the whitelist for private displays
1648 if (activityDisplay.isPrivate()) {
1649 mDisplayAccessUIDs.append(
1650 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1651 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001652 }
1653 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1654 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1655 }
1656
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001657 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001658 final long identity = Binder.clearCallingIdentity();
1659 try {
1660 return UserManager.get(mService.mContext).getUserInfo(userId);
1661 } finally {
1662 Binder.restoreCallingIdentity(identity);
1663 }
1664 }
1665
Svet Ganov99b60432015-06-27 13:15:22 -07001666 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001667 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001668 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1669 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001670 == PackageManager.PERMISSION_DENIED) {
1671 return ACTIVITY_RESTRICTION_PERMISSION;
1672 }
1673
Christopher Tateff7add02015-08-17 10:23:22 -07001674 if (activityInfo.permission == null) {
1675 return ACTIVITY_RESTRICTION_NONE;
1676 }
1677
Svet Ganov99b60432015-06-27 13:15:22 -07001678 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1679 if (opCode == AppOpsManager.OP_NONE) {
1680 return ACTIVITY_RESTRICTION_NONE;
1681 }
1682
1683 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1684 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001685 if (!ignoreTargetSecurity) {
1686 return ACTIVITY_RESTRICTION_APPOP;
1687 }
Svet Ganov99b60432015-06-27 13:15:22 -07001688 }
1689
1690 return ACTIVITY_RESTRICTION_NONE;
1691 }
1692
Svetoslav7008b512015-06-24 18:47:07 -07001693 private int getActionRestrictionForCallingPackage(String action,
1694 String callingPackage, int callingPid, int callingUid) {
1695 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001696 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001697 }
1698
1699 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1700 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001701 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001702 }
1703
1704 final PackageInfo packageInfo;
1705 try {
1706 packageInfo = mService.mContext.getPackageManager()
1707 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1708 } catch (PackageManager.NameNotFoundException e) {
1709 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001710 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001711 }
1712
1713 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001714 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001715 }
1716
1717 if (mService.checkPermission(permission, callingPid, callingUid) ==
1718 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001719 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001720 }
1721
1722 final int opCode = AppOpsManager.permissionToOpCode(permission);
1723 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001724 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001725 }
1726
1727 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1728 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001729 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001730 }
1731
Svet Ganov99b60432015-06-27 13:15:22 -07001732 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001733 }
1734
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001735 void setLaunchSource(int uid) {
1736 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1737 }
1738
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001739 void acquireLaunchWakelock() {
1740 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1741 throw new IllegalStateException("Calling must be system uid");
1742 }
1743 mLaunchingActivity.acquire();
1744 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1745 // To be safe, don't allow the wake lock to be held for too long.
1746 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1747 }
1748 }
1749
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001750 /**
1751 * Called when the frontmost task is idle.
1752 * @return the state of mService.mBooting before this was called.
1753 */
1754 private boolean checkFinishBootingLocked() {
1755 final boolean booting = mService.mBooting;
1756 boolean enableScreen = false;
1757 mService.mBooting = false;
1758 if (!mService.mBooted) {
1759 mService.mBooted = true;
1760 enableScreen = true;
1761 }
1762 if (booting || enableScreen) {
1763 mService.postFinishBooting(booting, enableScreen);
1764 }
1765 return booting;
1766 }
1767
Craig Mautnerf3333272013-04-22 10:55:53 -07001768 // Checked.
1769 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001770 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001771 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001772
Craig Mautnerf3333272013-04-22 10:55:53 -07001773 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001774 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001775 int NS = 0;
1776 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001777 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001778 boolean activityRemoved = false;
1779
Wale Ogunwale7d701172015-03-11 15:36:30 -07001780 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001781 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001782 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1783 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001784 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1785 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001786 if (fromTimeout) {
1787 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001788 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001789
1790 // This is a hack to semi-deal with a race condition
1791 // in the client where it can be constructed with a
1792 // newer configuration from when we asked it to launch.
1793 // We'll update with whatever configuration it now says
1794 // it used to launch.
1795 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001796 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001797 }
1798
1799 // We are now idle. If someone is waiting for a thumbnail from
1800 // us, we can now deliver.
1801 r.idle = true;
1802
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001803 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001804 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001805 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001806 }
1807 }
1808
1809 if (allResumedActivitiesIdle()) {
1810 if (r != null) {
1811 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001812 }
1813
1814 if (mLaunchingActivity.isHeld()) {
1815 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1816 if (VALIDATE_WAKE_LOCK_CALLER &&
1817 Binder.getCallingUid() != Process.myUid()) {
1818 throw new IllegalStateException("Calling must be system uid");
1819 }
1820 mLaunchingActivity.release();
1821 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001822 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 }
1824
1825 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001826 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1827 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001828 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001829 if ((NF = mFinishingActivities.size()) > 0) {
1830 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001831 mFinishingActivities.clear();
1832 }
1833
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001835 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001836 mStartingUsers.clear();
1837 }
1838
Craig Mautnerf3333272013-04-22 10:55:53 -07001839 // Stop any activities that are scheduled to do so but have been
1840 // waiting for the next one to start.
1841 for (int i = 0; i < NS; i++) {
1842 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001843 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001844 if (stack != null) {
1845 if (r.finishing) {
1846 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1847 } else {
1848 stack.stopActivityLocked(r);
1849 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001850 }
1851 }
1852
1853 // Finish any activities that are scheduled to do so but have been
1854 // waiting for the next one to start.
1855 for (int i = 0; i < NF; i++) {
1856 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001857 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001858 if (stack != null) {
1859 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1860 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001861 }
1862
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001863 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001864 // Complete user switch
1865 if (startingUsers != null) {
1866 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001867 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001868 }
1869 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001870 }
1871
1872 mService.trimApplications();
1873 //dump();
1874 //mWindowManager.dump();
1875
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001877 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001878 }
1879
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001880 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 }
1882
Craig Mautner8e569572013-10-11 17:36:59 -07001883 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001884 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001885 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1886 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1888 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1889 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001890 }
Craig Mautner19091252013-10-05 00:03:53 -07001891 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001892 }
1893
1894 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1896 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001897 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1898 stacks.get(stackNdx).closeSystemDialogsLocked();
1899 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001900 }
1901 }
1902
Craig Mautner93529a42013-10-04 15:03:13 -07001903 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001904 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001905 }
1906
Craig Mautner8d341ef2013-03-26 09:03:27 -07001907 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001908 * Update the last used stack id for non-current user (current user's last
1909 * used stack is the focused stack)
1910 */
1911 void updateUserStackLocked(int userId, ActivityStack stack) {
1912 if (userId != mCurrentUser) {
1913 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1914 }
1915 }
1916
1917 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001918 * @return true if some activity was finished (or would have finished if doit were true).
1919 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001920 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1921 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001922 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1924 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001926 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001927 if (stack.finishDisabledPackageActivitiesLocked(
1928 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001929 didSomething = true;
1930 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001931 }
1932 }
1933 return didSomething;
1934 }
1935
Dianne Hackborna413dc02013-07-12 12:02:55 -07001936 void updatePreviousProcessLocked(ActivityRecord r) {
1937 // Now that this process has stopped, we may want to consider
1938 // it to be the previous app to try to keep around in case
1939 // the user wants to return to it.
1940
1941 // First, found out what is currently the foreground app, so that
1942 // we don't blow away the previous app if this activity is being
1943 // hosted by the process that is actually still the foreground.
1944 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001945 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1946 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001947 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1948 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001949 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001950 if (stack.mResumedActivity != null) {
1951 fgApp = stack.mResumedActivity.app;
1952 } else if (stack.mPausingActivity != null) {
1953 fgApp = stack.mPausingActivity.app;
1954 }
1955 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001956 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001957 }
1958 }
1959
1960 // Now set this one as the previous process, only if that really
1961 // makes sense to.
1962 if (r.app != null && fgApp != null && r.app != fgApp
1963 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001964 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001965 mService.mPreviousProcess = r.app;
1966 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1967 }
1968 }
1969
Wale Ogunwaled046a012015-12-24 13:05:59 -08001970 boolean resumeFocusedStackTopActivityLocked() {
1971 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001972 }
1973
Wale Ogunwaled046a012015-12-24 13:05:59 -08001974 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001975 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001976 if (targetStack != null && isFocusedStack(targetStack)) {
1977 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001978 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001979 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1980 if (r == null || r.state != RESUMED) {
1981 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001982 } else if (r.state == RESUMED) {
1983 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1984 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001985 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001986 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001987 }
1988
Todd Kennedy39bfee52016-02-24 10:28:21 -08001989 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1990 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1991 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1992 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1993 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1994 }
1995 }
1996 }
1997
Adrian Roos20d7df32016-01-12 18:59:43 +01001998 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1999 TaskRecord finishedTask = null;
2000 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2002 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002003 final int numStacks = stacks.size();
2004 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2005 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002006 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2007 if (stack == focusedStack || finishedTask == null) {
2008 finishedTask = t;
2009 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002010 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002011 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002012 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002013 }
2014
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002015 void finishVoiceTask(IVoiceInteractionSession session) {
2016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2017 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2018 final int numStacks = stacks.size();
2019 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2020 final ActivityStack stack = stacks.get(stackNdx);
2021 stack.finishVoiceTask(session);
2022 }
2023 }
2024 }
2025
Andrii Kulianc27916642016-04-12 17:59:27 -07002026 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2027 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002028 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2029 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002030 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002031 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2032 // Caller wants the home activity moved with it. To accomplish this,
2033 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002034 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002035 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002036 ActivityStack currentStack = task.getStack();
2037 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002038 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2039 + task + " to front. Stack is null");
2040 return;
2041 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002042
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002043 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002044 int stackId = options.getLaunchStackId();
2045 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002046 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002047 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002048 if (stackId == INVALID_STACK_ID) {
2049 stackId = task.getLaunchStackId();
2050 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002051 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002052 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2053 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002054 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002055 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2056 // still need moveTaskToFrontLocked() below for any transition settings.
2057 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002058 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2059 resizeStackLocked(stackId, bounds,
2060 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002061 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002062 } else {
2063 // WM resizeTask must be done after the task is moved to the correct stack,
2064 // because Task's setBounds() also updates dim layer's bounds, but that has
2065 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002066 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002067 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002068 }
2069 }
2070
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002071 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002072 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002073 r == null ? null : r.appTimeTracker, reason);
2074
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002075 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002076 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002077
Andrii Kulian02b7a832016-10-06 23:11:56 -07002078 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002079 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002080 }
2081
Wale Ogunwale854809c2015-12-27 16:18:19 -08002082 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002083 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002084 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002085 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002086 return false;
2087 }
2088 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2089 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002090 }
2091
Winson Chung55893332017-02-17 17:13:10 -08002092 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002093 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002094 }
2095
Winson Chung55893332017-02-17 17:13:10 -08002096 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2097 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002098 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002099 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002100 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002101 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002102 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002103 return null;
2104 }
Matthew Ng330757d2017-02-28 14:19:17 -08002105 if (stackId == DOCKED_STACK_ID) {
2106 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2107 // the other side of the docked stack and we make visibility decisions based on that.
2108 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2109 }
Winson Chung55893332017-02-17 17:13:10 -08002110 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002111 }
2112
Andrii Kulian16802aa2016-11-02 12:21:33 -07002113 /**
2114 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2115 * If there is no such stack, new dynamic stack can be created.
2116 * @param displayId Target display.
2117 * @param r Activity that should be launched there.
2118 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2119 */
2120 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2121 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2122 if (activityDisplay == null) {
2123 throw new IllegalArgumentException(
2124 "Display with displayId=" + displayId + " not found.");
2125 }
2126
2127 // Return the topmost valid stack on the display.
2128 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2129 final ActivityStack stack = activityDisplay.mStacks.get(i);
2130 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2131 return stack;
2132 }
2133 }
2134
2135 // If there is no valid stack on the external display - check if new dynamic stack will do.
2136 if (displayId != Display.DEFAULT_DISPLAY) {
2137 final int newDynamicStackId = getNextStackId();
2138 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2139 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2140 }
2141 }
2142
2143 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2144 return null;
2145 }
2146
Craig Mautner967212c2013-04-13 21:10:58 -07002147 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002148 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002149 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2150 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002151 }
2152 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002153 }
2154
Jorim Jaggife762342016-10-13 14:33:27 +02002155 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2156 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2157 }
2158
Andrii Kulian7fc22812016-12-28 13:04:11 -08002159 /**
2160 * Get next focusable stack in the system. This will search across displays and stacks
2161 * in last-focused order for a focusable and visible stack, different from the target stack.
2162 *
2163 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2164 * searching for next candidate.
2165 * @return Next focusable {@link ActivityStack}, null if not found.
2166 */
2167 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2168 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2169
2170 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2171 final int displayId = mTmpOrderedDisplayIds.get(i);
2172 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2173 if (stacks == null) {
2174 continue;
2175 }
2176 for (int j = stacks.size() - 1; j >= 0; --j) {
2177 final ActivityStack stack = stacks.get(j);
2178 if (stack != currentFocus && stack.isFocusable()
2179 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2180 return stack;
2181 }
2182 }
2183 }
2184
2185 return null;
2186 }
2187
Craig Mautneree2e45a2014-06-27 12:10:03 -07002188 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002189 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002190 }
2191
2192 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002193 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2194 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2195 final TaskRecord task = tasks.get(taskNdx);
2196 if (task.isHomeTask()) {
2197 final ArrayList<ActivityRecord> activities = task.mActivities;
2198 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2199 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002200 if (r.isHomeActivity()
2201 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002202 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002203 }
2204 }
2205 }
2206 }
2207 return null;
2208 }
2209
Chong Zhangb15758a2015-11-17 12:12:03 -08002210 /**
2211 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2212 * the docked stack itself, or if it's side-by-side to the docked stack.
2213 */
2214 boolean isStackDockedInEffect(int stackId) {
2215 return stackId == DOCKED_STACK_ID ||
2216 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2217 }
2218
Todd Kennedyca4d8422015-01-15 15:19:22 -08002219 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002220 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002221 ActivityContainer activityContainer =
2222 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002223 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002224 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2225 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002226 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002227 return activityContainer;
2228 }
2229
Craig Mautner34b73df2014-01-12 21:11:08 -08002230 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002231 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2232 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2233 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002234 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2235 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002236 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002237 }
2238 }
2239
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002240 void deleteActivityContainerRecord(int stackId) {
2241 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2242 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2243 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002244 }
2245
Jorim Jaggidc249c42015-12-15 14:57:31 -08002246 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002247 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002248 if (stackId == DOCKED_STACK_ID) {
2249 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002250 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002251 return;
2252 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002253 final ActivityStack stack = getStack(stackId);
2254 if (stack == null) {
2255 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2256 return;
2257 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002258
Jorim Jaggidc249c42015-12-15 14:57:31 -08002259 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002260 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2261 // stack size changing so things don't get out of sync.
2262 return;
2263 }
2264
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002265 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002266 mWindowManager.deferSurfaceLayout();
2267 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002268 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002269 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002270 stack.ensureVisibleActivitiesConfigurationLocked(
2271 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002272 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002273 } finally {
2274 mWindowManager.continueSurfaceLayout();
2275 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002276 }
2277 }
2278
Jorim Jaggi192086e2016-03-11 17:17:03 +01002279 void deferUpdateBounds(int stackId) {
2280 final ActivityStack stack = getStack(stackId);
2281 if (stack != null) {
2282 stack.deferUpdateBounds();
2283 }
2284 }
2285
2286 void continueUpdateBounds(int stackId) {
2287 final ActivityStack stack = getStack(stackId);
2288 if (stack != null) {
2289 stack.continueUpdateBounds();
2290 }
2291 }
2292
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002293 void notifyAppTransitionDone() {
2294 continueUpdateBounds(HOME_STACK_ID);
2295 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2296 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002297 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002298 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002299 if (task != null) {
2300 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002301 }
2302 }
2303 mResizingTasksDuringAnimation.clear();
2304 }
2305
Robert Carre7cc44d2017-03-20 19:04:30 -07002306 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2307 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002308
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002309 final ActivityStack stack = getStack(fromStackId);
2310 if (stack == null) {
2311 return;
2312 }
2313
2314 mWindowManager.deferSurfaceLayout();
2315 try {
2316 if (fromStackId == DOCKED_STACK_ID) {
2317
2318 // We are moving all tasks from the docked stack to the fullscreen stack,
2319 // which is dismissing the docked stack, so resize all other stacks to
2320 // fullscreen here already so we don't end up with resize trashing.
2321 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2322 if (StackId.isResizeableByDockedStack(i)) {
2323 ActivityStack otherStack = getStack(i);
2324 if (otherStack != null) {
2325 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2326 true /* allowResizeInDockedMode */, DEFER_RESUME);
2327 }
2328 }
2329 }
2330
2331 // Also disable docked stack resizing since we have manually adjusted the
2332 // size of other stacks above and we don't want to trigger a docked stack
2333 // resize when we remove task from it below and it is detached from the
2334 // display because it no longer contains any tasks.
2335 mAllowDockedStackResize = false;
2336 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002337 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002338 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2339 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002340 // If we are moving from the pinned stack, then the animation takes care of updating
2341 // the picture-in-picture mode.
2342 final boolean schedulePictureInPictureModeChange = (fromStackId != PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002343 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2344 final int size = tasks.size();
2345 if (onTop) {
2346 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002347 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002348 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002349 if (fromStackId == PINNED_STACK_ID) {
2350 // Update the return-to to reflect where the pinned stack task was moved
2351 // from so that we retain the stack that was previously visible if the
2352 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002353 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2354 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002355 MetricsLogger.action(mService.mContext,
2356 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002357 }
Winson Chung74666102017-02-22 17:49:24 -08002358 // Defer resume until all the tasks have been moved to the fullscreen stack
2359 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2360 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002361 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002362 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002363 }
2364 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002365 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002366 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002367 final int position = fullscreenStack != null
2368 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2369 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002370 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002371 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2372 schedulePictureInPictureModeChange,
2373 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002374 }
2375 }
Winson Chung74666102017-02-22 17:49:24 -08002376
2377 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2378 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002379 } finally {
2380 mAllowDockedStackResize = true;
2381 mWindowManager.continueSurfaceLayout();
2382 }
2383 }
2384
Robert Carr6914f082017-03-20 19:04:30 -07002385 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002386 mWindowManager.inSurfaceTransaction(
2387 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002388 }
2389
Jorim Jaggidc249c42015-12-15 14:57:31 -08002390 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002391 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2392 boolean preserveWindows) {
2393 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2394 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2395 false /* deferResume */);
2396 }
2397
2398 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2399 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2400 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002401
2402 if (!mAllowDockedStackResize) {
2403 // Docked stack resize currently disabled.
2404 return;
2405 }
2406
Jorim Jaggidc249c42015-12-15 14:57:31 -08002407 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2408 if (stack == null) {
2409 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2410 return;
2411 }
2412
2413 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2414 mWindowManager.deferSurfaceLayout();
2415 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002416 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2417 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002418 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002419 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002420
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002421 // TODO: Checking for isAttached might not be needed as if the user passes in null
2422 // dockedBounds then they want the docked stack to be dismissed.
2423 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2424 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002425 // In this case we make all other static stacks fullscreen and move all
2426 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002427 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002428
2429 // stack shouldn't contain anymore activities, so nothing to resume.
2430 r = null;
2431 } else {
2432 // Docked stacks occupy a dedicated region on screen so the size of all other
2433 // static stacks need to be adjusted so they don't overlap with the docked stack.
2434 // We get the bounds to use from window manager which has been adjusted for any
2435 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002436 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002437 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002438 final ActivityStack current = getStack(i);
2439 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002440 current.getStackDockedModeBounds(
2441 tempOtherTaskBounds /* currentTempTaskBounds */,
2442 tempRect /* outStackBounds */,
2443 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2444
Matthew Nge15352e2016-12-20 15:36:29 -08002445 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002446 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2447 tempOtherTaskInsetBounds, preserveWindows,
2448 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002449 }
2450 }
2451 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002452 if (!deferResume) {
2453 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2454 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002455 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002456 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002457 mWindowManager.continueSurfaceLayout();
2458 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2459 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002460 }
2461
Robert Carr0d00c2e2016-02-29 17:45:02 -08002462 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2463 final ActivityStack stack = getStack(PINNED_STACK_ID);
2464 if (stack == null) {
2465 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2466 return;
2467 }
2468 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2469 mWindowManager.deferSurfaceLayout();
2470 try {
2471 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002472 Rect insetBounds = null;
2473 if (tempPinnedTaskBounds != null) {
2474 // We always use 0,0 as the position for the inset rect because
2475 // if we are getting insets at all in the pinned stack it must mean
2476 // we are headed for fullscreen.
2477 insetBounds = tempRect;
2478 insetBounds.top = 0;
2479 insetBounds.left = 0;
2480 insetBounds.right = tempPinnedTaskBounds.width();
2481 insetBounds.bottom = tempPinnedTaskBounds.height();
2482 }
2483 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002484 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002485 } finally {
2486 mWindowManager.continueSurfaceLayout();
2487 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2488 }
2489 }
2490
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002491 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002492 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002493 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002494 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002495 }
2496
Wale Ogunwale1666e312016-12-16 11:27:18 -08002497 final ActivityContainer activityContainer =
2498 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002499 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002500 }
2501
Robert Carr6914f082017-03-20 19:04:30 -07002502
Robert Carre7cc44d2017-03-20 19:04:30 -07002503 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002504 final ActivityStack stack = getStack(stackId);
2505 if (stack == null) {
2506 return;
2507 }
2508
2509 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2510 if (stack.getStackId() == PINNED_STACK_ID) {
2511 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2512 if (fullscreenStack != null) {
2513 final boolean isFullscreenStackVisible =
2514 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2515 for (int i = 0; i < tasks.size(); i++) {
2516 // Insert the task either at the top of the fullscreen stack if it is hidden,
2517 // or just under the top task if it is currently visible
2518 final int insertPosition = isFullscreenStackVisible
2519 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2520 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002521 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002522 // Defer resume until we remove all the tasks
2523 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2524 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002525 }
2526 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2527 resumeFocusedStackTopActivityLocked();
2528 } else {
2529 // If there is no fullscreen stack, then create the stack and move all the tasks
2530 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002531 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002532 }
2533 } else {
2534 for (int i = tasks.size() - 1; i >= 0; i--) {
2535 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2536 REMOVE_FROM_RECENTS);
2537 }
2538 }
2539 }
2540
2541 /**
Robert Carr6914f082017-03-20 19:04:30 -07002542 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2543 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2544 * instead moved back onto the fullscreen stack.
2545 */
2546 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002547 mWindowManager.inSurfaceTransaction(
2548 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002549 }
2550
2551 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002552 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2553 */
2554 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2555 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2556 }
2557
2558 /**
Winson Chung010927a2016-12-15 16:12:35 -08002559 * Removes the task with the specified task id.
2560 *
2561 * @param taskId Identifier of the task to be removed.
2562 * @param killProcess Kill any process associated with the task if possible.
2563 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002564 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2565 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002566 * @return Returns true if the given task was found and removed.
2567 */
Winson Chung6954fc92017-03-24 16:22:12 -07002568 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2569 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002570 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2571 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002572 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002573 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002574 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2575 if (tr.isPersistable) {
2576 mService.notifyTaskPersisterLocked(null, true);
2577 }
2578 return true;
2579 }
2580 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2581 return false;
2582 }
2583
2584 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2585 if (removeFromRecents) {
2586 mRecentTasks.remove(tr);
2587 tr.removedFromRecents();
2588 }
2589 ComponentName component = tr.getBaseIntent().getComponent();
2590 if (component == null) {
2591 Slog.w(TAG, "No component for base intent of task: " + tr);
2592 return;
2593 }
2594
2595 // Find any running services associated with this app and stop if needed.
2596 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2597
2598 if (!killProcess) {
2599 return;
2600 }
2601
2602 // Determine if the process(es) for this task should be killed.
2603 final String pkg = component.getPackageName();
2604 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2605 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2606 for (int i = 0; i < pmap.size(); i++) {
2607
2608 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2609 for (int j = 0; j < uids.size(); j++) {
2610 ProcessRecord proc = uids.valueAt(j);
2611 if (proc.userId != tr.userId) {
2612 // Don't kill process for a different user.
2613 continue;
2614 }
2615 if (proc == mService.mHomeProcess) {
2616 // Don't kill the home process along with tasks from the same package.
2617 continue;
2618 }
2619 if (!proc.pkgList.containsKey(pkg)) {
2620 // Don't kill process that is not associated with this task.
2621 continue;
2622 }
2623
2624 for (int k = 0; k < proc.activities.size(); k++) {
2625 TaskRecord otherTask = proc.activities.get(k).task;
2626 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2627 // Don't kill process(es) that has an activity in a different task that is
2628 // also in recents.
2629 return;
2630 }
2631 }
2632
2633 if (proc.foregroundServices) {
2634 // Don't kill process(es) with foreground service.
2635 return;
2636 }
2637
2638 // Add process to kill list.
2639 procsToKill.add(proc);
2640 }
2641 }
2642
2643 // Kill the running processes.
2644 for (int i = 0; i < procsToKill.size(); i++) {
2645 ProcessRecord pr = procsToKill.get(i);
2646 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2647 && pr.curReceivers.isEmpty()) {
2648 pr.kill("remove task", true);
2649 } else {
2650 // We delay killing processes that are not in the background or running a receiver.
2651 pr.waitingToKill = "remove task";
2652 }
2653 }
2654 }
2655
Craig Mautner4a1cb222013-12-04 16:14:06 -08002656 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002657 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002658 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2659 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002660 break;
2661 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002662 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002663 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002664 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002665 }
2666
Chong Zhang5dcb2752015-08-18 13:50:26 -07002667 /**
2668 * Restores a recent task to a stack
2669 * @param task The recent task to be restored.
2670 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002671 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2672 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002673 * @return true if the task has been restored successfully.
2674 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002675 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002676 if (!StackId.isStaticStack(stackId)) {
2677 // If stack is not static (or stack id is invalid) - use the default one.
2678 // This means that tasks that were on external displays will be restored on the
2679 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002680 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002681 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002682 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2683 // Put it in the fullscreen stack.
2684 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002685 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002686
Andrii Kulian02b7a832016-10-06 23:11:56 -07002687 final ActivityStack currentStack = task.getStack();
2688 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002689 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002690 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002691 // Nothing else to do since it is already restored in the right stack.
2692 return true;
2693 }
2694 // Remove current stack association, so we can re-associate the task with the
2695 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002696 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002697 }
2698
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002699 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002700 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002701
2702 if (stack == null) {
2703 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002704 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2705 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002706 return false;
2707 }
2708
Wale Ogunwale72919d22016-12-08 18:58:50 -08002709 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002710 // TODO: move call for creation here and other place into Stack.addTask()
2711 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002712 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2713 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002714 final ArrayList<ActivityRecord> activities = task.mActivities;
2715 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002716 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002717 }
2718 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002719 }
2720
Wale Ogunwale040b4702015-08-06 18:10:50 -07002721 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002722 * Move stack with all its existing content to specified display.
2723 * @param stackId Id of stack to move.
2724 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002725 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002726 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002727 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002728 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2729 if (activityDisplay == null) {
2730 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2731 + displayId);
2732 }
2733 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2734 if (activityContainer != null) {
2735 if (activityContainer.isAttachedLocked()) {
2736 if (activityContainer.getDisplayId() == displayId) {
2737 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2738 + " to its current displayId=" + displayId);
2739 }
2740
Andrii Kulian250d6532017-02-08 23:30:45 -08002741 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002742 } else {
2743 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2744 + stackId + " is not attached to any display.");
2745 }
2746 } else {
2747 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2748 + stackId);
2749 }
2750
2751 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2752 !PRESERVE_WINDOWS);
2753 // TODO(multi-display): resize stacks properly if moved from split-screen.
2754 }
2755
2756 /**
Winson Chung74666102017-02-22 17:49:24 -08002757 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2758 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002759 */
Winson Chung74666102017-02-22 17:49:24 -08002760 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002761 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002762
Winson Chung74666102017-02-22 17:49:24 -08002763 // Check that we aren't reparenting to the same stack that the task is already in
2764 if (prevStack != null && prevStack.mStackId == stackId) {
2765 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2766 + " already in stackId=" + stackId);
2767 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002768 }
2769
Winson Chung74666102017-02-22 17:49:24 -08002770 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2771 // support
2772 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2773 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2774 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002775 }
2776
Winson Chung74666102017-02-22 17:49:24 -08002777 // Ensure that we aren't trying to move into a freeform stack without freeform
2778 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002779 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002780 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2781 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002782 }
2783
Winson Chung74666102017-02-22 17:49:24 -08002784 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2785 // used for split-screen mode and will cause things like the docked divider to show up. We
2786 // instead leave the task in its current stack or move it to the fullscreen stack if it
2787 // isn't currently in a stack.
2788 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2789 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2790 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2791 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002792 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002793
Winson Chung74666102017-02-22 17:49:24 -08002794 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2795 // example, a docked stack is created which will lead to the stack we are moving from being
2796 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002797 try {
Winson Chung74666102017-02-22 17:49:24 -08002798 task.mTemporarilyUnresizable = true;
2799 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002800 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002801 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002802 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002803 }
2804
Winson Chung08f81892017-03-02 15:40:51 -08002805 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002806 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2807 if (stack == null) {
2808 throw new IllegalArgumentException(
2809 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2810 }
2811
2812 final ActivityRecord r = stack.topRunningActivityLocked();
2813 if (r == null) {
2814 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2815 + " in stack=" + stack);
2816 return false;
2817 }
2818
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002819 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002820 Slog.w(TAG,
2821 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002822 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002823 return false;
2824 }
2825
Winson Chung08f81892017-03-02 15:40:51 -08002826 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2827 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002828 return true;
2829 }
2830
Winson Chung08f81892017-03-02 15:40:51 -08002831 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2832 boolean moveHomeStackToFront, String reason) {
2833
Wale Ogunwale480dca02016-02-06 13:58:29 -08002834 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002835
Wale Ogunwale1666e312016-12-16 11:27:18 -08002836 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002837 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002838
Wale Ogunwale480dca02016-02-06 13:58:29 -08002839 try {
2840 final TaskRecord task = r.task;
2841
Andrii Kulian02b7a832016-10-06 23:11:56 -07002842 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002843 // An activity can't be pinned and visible behind at the same time. Go ahead and
2844 // release it from been visible behind before pinning.
2845 requestVisibleBehindLocked(r, false);
2846 }
2847
Wale Ogunwale480dca02016-02-06 13:58:29 -08002848 // Resize the pinned stack to match the current size of the task the activity we are
2849 // going to be moving is currently contained in. We do this to have the right starting
2850 // animation bounds for the pinned stack to the desired bounds the caller wants.
2851 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2852 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002853 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002854
2855 if (task.mActivities.size() == 1) {
2856 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002857 // the stack without re-parenting the activity in a different task. We don't
2858 // move the home stack forward if we are currently entering picture-in-picture
2859 // while pausing because that changes the focused stack and may prevent the new
2860 // starting activity from resuming.
2861 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002862 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002863 // Move the home stack forward if the task we just moved to the pinned stack
2864 // was launched from home so home should be visible behind it.
2865 moveHomeStackToFront(reason);
2866 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002867 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002868 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002869 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002870 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002871 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002872 // reveal/leave the other activities in their original task.
2873
2874 // Currently, we don't support reparenting activities across tasks in two different
2875 // stacks, so instead, just create a new task in the same stack, reparent the
2876 // activity into that task, and then reparent the whole task to the new stack. This
2877 // ensures that all the necessary work to migrate states in the old and new stacks
2878 // is also done.
2879 final TaskRecord newTask = task.getStack().createTaskRecord(
2880 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2881 r.mActivityType);
2882 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2883
Winson Chung5af42fc2017-03-24 17:11:33 -07002884 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002885 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002886 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002887 }
Winson Chungc2baac02017-01-11 13:34:47 -08002888
2889 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2890 // to the pinned stack
2891 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002892 } finally {
2893 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002894 }
2895
Wale Ogunwale480dca02016-02-06 13:58:29 -08002896 // The task might have already been running and its visibility needs to be synchronized
2897 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002898 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002899 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002900
Winson Chung08f81892017-03-02 15:40:51 -08002901 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002902 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002903 }
2904
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002905 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002906 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2907 if (r == null || !r.isFocusable()) {
2908 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2909 "moveActivityStackToFront: unfocusable r=" + r);
2910 return false;
2911 }
2912
2913 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002914 final ActivityStack stack = r.getStack();
2915 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002916 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2917 + r + " task=" + task);
2918 return false;
2919 }
2920
Chong Zhang6cda19c2016-06-14 19:07:56 -07002921 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2922 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2923 "moveActivityStackToFront: already on top, r=" + r);
2924 return false;
2925 }
2926
2927 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2928 "moveActivityStackToFront: r=" + r);
2929
2930 stack.moveToFront(reason, task);
2931 return true;
2932 }
2933
David Stevensc6b91c62017-02-08 14:23:58 -08002934 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002935 mTmpFindTaskResult.r = null;
2936 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002937 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002938 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2940 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2942 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002943 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002944 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002945 continue;
2946 }
2947 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002948 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2949 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002950 continue;
2951 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002952 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002953 // It is possible to have tasks in multiple stacks with the same root affinity, so
2954 // we should keep looking after finding an affinity match to see if there is a
2955 // better match in another stack. Also, task affinity isn't a good enough reason
2956 // to target a display which isn't the source of the intent, so skip any affinity
2957 // matches not on the specified display.
2958 if (mTmpFindTaskResult.r != null) {
2959 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2960 return mTmpFindTaskResult.r;
2961 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2962 affinityMatch = mTmpFindTaskResult.r;
2963 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002964 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002965 }
2966 }
David Stevensc6b91c62017-02-08 14:23:58 -08002967 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2968 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002969 }
2970
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002971 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2972 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2974 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002976 final ActivityRecord ar = stacks.get(stackNdx)
2977 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002978 if (ar != null) {
2979 return ar;
2980 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002981 }
2982 }
2983 return null;
2984 }
2985
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 scheduleSleepTimeout();
2988 if (!mGoingToSleep.isHeld()) {
2989 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002990 if (mLaunchingActivity.isHeld()) {
2991 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2992 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002993 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002994 mLaunchingActivity.release();
2995 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002997 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002998 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002999 }
3000
3001 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003002 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003003
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003004 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003005 final long endTime = System.currentTimeMillis() + timeout;
3006 while (true) {
3007 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3009 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003010 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3011 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3012 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003013 }
3014 if (cantShutdown) {
3015 long timeRemaining = endTime - System.currentTimeMillis();
3016 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003017 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003018 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 } catch (InterruptedException e) {
3020 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021 } else {
3022 Slog.w(TAG, "Activity manager shutdown timed out");
3023 timedout = true;
3024 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003025 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 } else {
3027 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003028 }
3029 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030
3031 // Force checkReadyForSleep to complete.
3032 mSleepTimeout = true;
3033 checkReadyForSleepLocked();
3034
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 return timedout;
3036 }
3037
3038 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003039 removeSleepTimeouts();
3040 if (mGoingToSleep.isHeld()) {
3041 mGoingToSleep.release();
3042 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3046 final ActivityStack stack = stacks.get(stackNdx);
3047 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003048 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003049 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003050 }
Craig Mautner5314a402013-09-26 12:40:16 -07003051 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003052 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003053 mGoingToSleepActivities.clear();
3054 }
3055
3056 void activitySleptLocked(ActivityRecord r) {
3057 mGoingToSleepActivities.remove(r);
3058 checkReadyForSleepLocked();
3059 }
3060
3061 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003062 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003063 // Do not care.
3064 return;
3065 }
3066
3067 if (!mSleepTimeout) {
3068 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003069 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3070 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3072 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3073 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003074 }
3075
3076 if (mStoppingActivities.size() > 0) {
3077 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003079 + mStoppingActivities.size() + " activities");
3080 scheduleIdleLocked();
3081 dontSleep = true;
3082 }
3083
3084 if (mGoingToSleepActivities.size() > 0) {
3085 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003086 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003087 + mGoingToSleepActivities.size() + " activities");
3088 dontSleep = true;
3089 }
3090
3091 if (dontSleep) {
3092 return;
3093 }
3094 }
3095
Wei Wang65c7a152016-06-02 18:51:22 -07003096 // Send launch end powerhint before going sleep
3097 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3098
Craig Mautnere0a38842013-12-16 16:14:02 -08003099 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3100 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3102 stacks.get(stackNdx).goToSleep();
3103 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003104 }
3105
3106 removeSleepTimeouts();
3107
3108 if (mGoingToSleep.isHeld()) {
3109 mGoingToSleep.release();
3110 }
3111 if (mService.mShuttingDown) {
3112 mService.notifyAll();
3113 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003114 }
3115
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003116 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003117 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003118 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003119 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003120 }
3121 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003122 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003123 mWindowManager.executeAppTransition();
3124 return true;
3125 }
3126 return false;
3127 }
3128
Craig Mautner8d341ef2013-03-26 09:03:27 -07003129 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003130 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3131 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003132 int stackNdx = stacks.size() - 1;
3133 while (stackNdx >= 0) {
3134 stacks.get(stackNdx).handleAppCrashLocked(app);
3135 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003137 }
3138 }
3139
Jose Lima4b6c6692014-08-12 17:41:12 -07003140 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003141 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003142 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003143 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3144 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003145 return false;
3146 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003147
3148 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3149 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3150 + " visible=" + visible + " stackId=" + stack.mStackId
3151 + " can't contain visible behind activities");
3152 return false;
3153 }
3154
Jose Lima4b6c6692014-08-12 17:41:12 -07003155 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3157 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003158
3159 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003160 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003161 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003162 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003163 return true;
3164 }
3165
3166 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003167 if (visible && top.fullscreen) {
3168 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003169 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3170 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3171 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3172 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003174 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3175 // Only the activity set as currently visible behind should actively reset its
3176 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003177 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3178 "requestVisibleBehind: returning visible=" + visible
3179 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3180 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003181 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003182 }
3183
Jose Lima4b6c6692014-08-12 17:41:12 -07003184 stack.setVisibleBehindActivity(visible ? r : null);
3185 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003186 // If there is a translucent home activity, we need to force it stop being translucent,
3187 // because we can't depend on the application to necessarily perform that operation.
3188 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003189 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003190 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003191 mService.convertFromTranslucent(next.appToken);
3192 }
3193 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003194 if (top.app != null && top.app.thread != null) {
3195 // Notify the top app of the change.
3196 try {
3197 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3198 } catch (RemoteException e) {
3199 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003200 }
3201 return true;
3202 }
3203
Craig Mautnerbb742462014-07-07 15:28:55 -07003204 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003205 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003206 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003207 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003208
3209 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003210 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003211 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003212 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003213 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003214
3215 // When launching tasks behind, update the last active time of the top task after the new
3216 // task has been shown briefly
3217 final ActivityRecord top = stack.topActivity();
3218 if (top != null) {
3219 top.task.touchActiveTime();
3220 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003221 }
3222
3223 void scheduleLaunchTaskBehindComplete(IBinder token) {
3224 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3225 }
3226
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003227 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3228 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003229 mKeyguardController.beginActivityVisibilityUpdate();
3230 try {
3231 // First the front stacks. In case any are not fullscreen and are in front of home.
3232 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3233 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3234 final int topStackNdx = stacks.size() - 1;
3235 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3236 final ActivityStack stack = stacks.get(stackNdx);
3237 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3238 }
Craig Mautner580ea812013-04-25 12:58:38 -07003239 }
Jorim Jaggife762342016-10-13 14:33:27 +02003240 } finally {
3241 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003242 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003243 }
3244
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003245 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3246 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3247 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3248 final int topStackNdx = stacks.size() - 1;
3249 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3250 final ActivityStack stack = stacks.get(stackNdx);
3251 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3252 }
3253 }
3254 }
3255
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003256 void invalidateTaskLayers() {
3257 mTaskLayersChanged = true;
3258 }
3259
3260 void rankTaskLayersIfNeeded() {
3261 if (!mTaskLayersChanged) {
3262 return;
3263 }
3264 mTaskLayersChanged = false;
3265 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3266 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3267 int baseLayer = 0;
3268 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3269 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3270 }
3271 }
3272 }
3273
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003274 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3275 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3276 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3277 final int topStackNdx = stacks.size() - 1;
3278 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3279 final ActivityStack stack = stacks.get(stackNdx);
3280 stack.clearOtherAppTimeTrackers(except);
3281 }
3282 }
3283 }
3284
Craig Mautner8d341ef2013-03-26 09:03:27 -07003285 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003286 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3287 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003288 final int numStacks = stacks.size();
3289 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3290 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003291 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003292 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003293 }
3294 }
3295
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003296 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3297 // Examine all activities currently running in the process.
3298 TaskRecord firstTask = null;
3299 // Tasks is non-null only if two or more tasks are found.
3300 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003301 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3302 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003303 ActivityRecord r = app.activities.get(i);
3304 // First, if we find an activity that is in the process of being destroyed,
3305 // then we just aren't going to do anything for now; we want things to settle
3306 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003307 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003309 return;
3310 }
3311 // Don't consider any activies that are currently not in a state where they
3312 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003313 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3314 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003315 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003316 continue;
3317 }
3318 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003319 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003320 + " from " + r);
3321 if (firstTask == null) {
3322 firstTask = r.task;
3323 } else if (firstTask != r.task) {
3324 if (tasks == null) {
3325 tasks = new ArraySet<>();
3326 tasks.add(firstTask);
3327 }
3328 tasks.add(r.task);
3329 }
3330 }
3331 }
3332 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003333 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 return;
3335 }
3336 // If we have activities in multiple tasks that are in a position to be destroyed,
3337 // let's iterate through the tasks and release the oldest one.
3338 final int numDisplays = mActivityDisplays.size();
3339 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3340 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3341 // Step through all stacks starting from behind, to hit the oldest things first.
3342 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3343 final ActivityStack stack = stacks.get(stackNdx);
3344 // Try to release activities in this stack; if we manage to, we are done.
3345 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3346 return;
3347 }
3348 }
3349 }
3350 }
3351
Amith Yamasani37a40c22015-06-17 13:25:42 -07003352 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003353 final int focusStackId = mFocusedStack.getStackId();
3354 // We dismiss the docked stack whenever we switch users.
3355 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003356 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3357 // also cause all tasks to be moved to the fullscreen stack at a position that is
3358 // appropriate.
3359 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003360
3361 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003362 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003363 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003364
Craig Mautner858d8a62013-04-23 17:08:34 -07003365 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003366 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3367 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003369 final ActivityStack stack = stacks.get(stackNdx);
3370 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003371 TaskRecord task = stack.topTask();
3372 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003373 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003374 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003376 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003377
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003378 ActivityStack stack = getStack(restoreStackId);
3379 if (stack == null) {
3380 stack = mHomeStack;
3381 }
3382 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003383 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003384 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003385 } else {
3386 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003387 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003388 }
Craig Mautner93529a42013-10-04 15:03:13 -07003389 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003390 }
3391
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003392 /** Checks whether the userid is a profile of the current user. */
3393 boolean isCurrentProfileLocked(int userId) {
3394 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003395 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003396 }
3397
Winson Chung4dabf232017-01-25 13:25:22 -08003398 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3399 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003400 ArrayList<ActivityRecord> stops = null;
3401
3402 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003403 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3404 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003405 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3406 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003407 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003408 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003409 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3410 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003411 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003412 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003413 if (s.finishing) {
3414 // If this activity is finishing, it is sitting on top of
3415 // everyone else but we now know it is no longer needed...
3416 // so get rid of it. Otherwise, we need to go through the
3417 // normal flow and hide it once we determine that it is
3418 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003419 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003420 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003421 }
3422 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003423 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003424 if (!processPausingActivities && s.state == PAUSING) {
3425 // Defer processing pausing activities in this iteration and reschedule
3426 // a delayed idle to reprocess it again
3427 removeTimeoutsForActivityLocked(idleActivity);
3428 scheduleIdleTimeoutLocked(idleActivity);
3429 continue;
3430 }
3431
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003432 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003433 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003434 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003435 }
3436 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003437 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003438 }
3439 }
3440
3441 return stops;
3442 }
3443
Craig Mautnercf910b02013-04-23 11:23:27 -07003444 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003445 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3446 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3447 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3448 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003449 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003450 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003451 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003452 if (r == null) Slog.e(TAG,
3453 "validateTop...: null top activity, stack=" + stack);
3454 else {
3455 final ActivityRecord pausing = stack.mPausingActivity;
3456 if (pausing != null && pausing == r) Slog.e(TAG,
3457 "validateTop...: top stack has pausing activity r=" + r
3458 + " state=" + state);
3459 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3460 "validateTop...: activity in front not resumed r=" + r
3461 + " state=" + state);
3462 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003463 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003464 final ActivityRecord resumed = stack.mResumedActivity;
3465 if (resumed != null && resumed == r) Slog.e(TAG,
3466 "validateTop...: back stack has resumed activity r=" + r
3467 + " state=" + state);
3468 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3469 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003470 }
3471 }
3472 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003473 }
3474
Craig Mautnere0570202015-05-13 13:06:11 -07003475 private String lockTaskModeToString() {
3476 switch (mLockTaskModeState) {
3477 case LOCK_TASK_MODE_LOCKED:
3478 return "LOCKED";
3479 case LOCK_TASK_MODE_PINNED:
3480 return "PINNED";
3481 case LOCK_TASK_MODE_NONE:
3482 return "NONE";
3483 default: return "unknown=" + mLockTaskModeState;
3484 }
3485 }
3486
Craig Mautner27084302013-03-25 08:05:25 -07003487 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003488 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003489 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003490 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003491 pw.print(prefix);
3492 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003493 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003494 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003495 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003496 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3497 if (packages.size() > 0) {
3498 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3499 for (int i = 0; i < packages.size(); ++i) {
3500 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3501 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3502 }
3503 }
3504 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3505 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003506 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003507
Winson43d1f262016-06-14 16:05:55 -07003508 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003509 * Dump all connected displays' configurations.
3510 * @param prefix Prefix to apply to each line of the dump.
3511 */
3512 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3513 pw.print(prefix); pw.println("Display override configurations:");
3514 final int displayCount = mActivityDisplays.size();
3515 for (int i = 0; i < displayCount; i++) {
3516 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3517 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3518 pw.println(activityDisplay.getOverrideConfiguration());
3519 }
3520 }
3521
3522 /**
Winson43d1f262016-06-14 16:05:55 -07003523 * Dumps the activities matching the given {@param name} in the either the focused stack
3524 * or all visible stacks if {@param dumpVisibleStacks} is true.
3525 */
Winson Chung6998bc42017-02-28 17:07:05 -08003526 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3527 boolean dumpFocusedStackOnly) {
3528 if (dumpFocusedStackOnly) {
3529 return mFocusedStack.getDumpActivitiesLocked(name);
3530 } else {
Winson43d1f262016-06-14 16:05:55 -07003531 ArrayList<ActivityRecord> activities = new ArrayList<>();
3532 int numDisplays = mActivityDisplays.size();
3533 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3534 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3535 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3536 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003537 if (!dumpVisibleStacksOnly ||
3538 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003539 activities.addAll(stack.getDumpActivitiesLocked(name));
3540 }
3541 }
3542 }
3543 return activities;
Winson43d1f262016-06-14 16:05:55 -07003544 }
Craig Mautner20e72272013-04-01 13:45:53 -07003545 }
3546
Dianne Hackborn390517b2013-05-30 15:03:32 -07003547 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3548 boolean needSep, String prefix) {
3549 if (activity != null) {
3550 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3551 if (needSep) {
3552 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003553 }
3554 pw.print(prefix);
3555 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003556 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003557 }
3558 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003559 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003560 }
3561
Craig Mautner8d341ef2013-03-26 09:03:27 -07003562 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3563 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003564 boolean printed = false;
3565 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3567 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003568 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003569 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003570 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003571 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003572 final ActivityStack stack = stacks.get(stackNdx);
3573 StringBuilder stackHeader = new StringBuilder(128);
3574 stackHeader.append(" Stack #");
3575 stackHeader.append(stack.mStackId);
3576 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003577 stackHeader.append("\n");
3578 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3579 stackHeader.append("\n");
3580 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003581
3582 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3583 dumpClient, dumpPackage, needSep, stackHeader.toString());
3584 printed |= printedStackHeader;
3585 if (!printedStackHeader) {
3586 // Ensure we always dump the stack header even if there are no activities
3587 pw.println();
3588 pw.println(stackHeader);
3589 }
3590
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3592 !dumpAll, false, dumpPackage, true,
3593 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003594
Craig Mautner4a1cb222013-12-04 16:14:06 -08003595 needSep = printed;
3596 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3597 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003598 if (pr) {
3599 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003601 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003602 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3603 " mResumedActivity: ");
3604 if (pr) {
3605 printed = true;
3606 needSep = false;
3607 }
3608 if (dumpAll) {
3609 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3610 " mLastPausedActivity: ");
3611 if (pr) {
3612 printed = true;
3613 needSep = true;
3614 }
3615 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3616 needSep, " mLastNoHistoryActivity: ");
3617 }
3618 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003619 }
3620 }
3621
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003622 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3623 false, dumpPackage, true, " Activities waiting to finish:", null);
3624 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3625 false, dumpPackage, true, " Activities waiting to stop:", null);
3626 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3627 false, dumpPackage, true, " Activities waiting for another to become visible:",
3628 null);
3629 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3630 false, dumpPackage, true, " Activities waiting to sleep:", null);
3631 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3632 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003633
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003634 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 }
3636
Dianne Hackborn390517b2013-05-30 15:03:32 -07003637 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003638 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003639 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003640 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003641 String innerPrefix = null;
3642 String[] args = null;
3643 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003644 for (int i=list.size()-1; i>=0; i--) {
3645 final ActivityRecord r = list.get(i);
3646 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3647 continue;
3648 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003649 if (innerPrefix == null) {
3650 innerPrefix = prefix + " ";
3651 args = new String[0];
3652 }
3653 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 final boolean full = !brief && (complete || !r.isInHistory());
3655 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003656 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003657 needNL = false;
3658 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003659 if (header1 != null) {
3660 pw.println(header1);
3661 header1 = null;
3662 }
3663 if (header2 != null) {
3664 pw.println(header2);
3665 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003666 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003667 if (lastTask != r.task) {
3668 lastTask = r.task;
3669 pw.print(prefix);
3670 pw.print(full ? "* " : " ");
3671 pw.println(lastTask);
3672 if (full) {
3673 lastTask.dump(pw, prefix + " ");
3674 } else if (complete) {
3675 // Complete + brief == give a summary. Isn't that obvious?!?
3676 if (lastTask.intent != null) {
3677 pw.print(prefix); pw.print(" ");
3678 pw.println(lastTask.intent.toInsecureStringWithClip());
3679 }
3680 }
3681 }
3682 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3683 pw.print(" #"); pw.print(i); pw.print(": ");
3684 pw.println(r);
3685 if (full) {
3686 r.dump(pw, innerPrefix);
3687 } else if (complete) {
3688 // Complete + brief == give a summary. Isn't that obvious?!?
3689 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3690 if (r.app != null) {
3691 pw.print(innerPrefix); pw.println(r.app);
3692 }
3693 }
3694 if (client && r.app != null && r.app.thread != null) {
3695 // flush anything that is already in the PrintWriter since the thread is going
3696 // to write to the file descriptor directly
3697 pw.flush();
3698 try {
3699 TransferPipe tp = new TransferPipe();
3700 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003701 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003702 // Short timeout, since blocking here can
3703 // deadlock with the application.
3704 tp.go(fd, 2000);
3705 } finally {
3706 tp.kill();
3707 }
3708 } catch (IOException e) {
3709 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3710 } catch (RemoteException e) {
3711 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3712 }
3713 needNL = true;
3714 }
3715 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003716 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003717 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003718
Craig Mautnerf3333272013-04-22 10:55:53 -07003719 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003720 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3721 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003722 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3723 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003724 }
3725
3726 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003727 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003728 }
3729
3730 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003731 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3732 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003733 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3734 }
3735
Craig Mautner05d29032013-05-03 13:40:13 -07003736 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003737 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3738 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3739 }
Craig Mautner05d29032013-05-03 13:40:13 -07003740 }
3741
Craig Mautner0eea92c2013-05-16 13:35:39 -07003742 void removeSleepTimeouts() {
3743 mSleepTimeout = false;
3744 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3745 }
3746
3747 final void scheduleSleepTimeout() {
3748 removeSleepTimeouts();
3749 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3750 }
3751
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 @Override
3753 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003754 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3756 }
3757
3758 @Override
3759 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003760 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3762 }
3763
3764 @Override
3765 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003766 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003767 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3768 }
3769
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003770 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003771 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003772 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003773 newDisplay = mActivityDisplays.get(displayId) == null;
3774 if (newDisplay) {
3775 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003776 if (activityDisplay.mDisplay == null) {
3777 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3778 return;
3779 }
Craig Mautner4504de52013-12-20 09:06:56 -08003780 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003781 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003782 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003783 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 }
3786
Andrii Kulianca007a62016-11-22 16:33:28 -08003787 /** Check if display with specified id is added to the list. */
3788 boolean isDisplayAdded(int displayId) {
3789 return mActivityDisplays.get(displayId) != null;
3790 }
3791
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003792 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003793 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003794 mService.mContext.getResources().getDimensionPixelSize(
3795 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003796 }
3797
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003798 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003799 if (displayId == DEFAULT_DISPLAY) {
3800 throw new IllegalArgumentException("Can't remove the primary display.");
3801 }
3802
Craig Mautner4a1cb222013-12-04 16:14:06 -08003803 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003804 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3805 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003806 final boolean destroyContentOnRemoval
3807 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003808 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3809 while (!stacks.isEmpty()) {
3810 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003811 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003812 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3813 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003814 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003815 } else {
3816 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3817 // a valid launch stack for all activities. This way the task history from
3818 // external display will be preserved on primary after move.
3819 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003820 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003821 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003822 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003823 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 }
3825 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 }
3827
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003828 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003830 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3831 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 // TODO: Update the bounds.
3833 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003834 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 }
3837
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003838 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003839 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003841 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003842 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003843 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003844 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003845 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003846 info.position = display != null
3847 ? display.mStacks.indexOf(stack)
3848 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849
3850 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3851 final int numTasks = tasks.size();
3852 int[] taskIds = new int[numTasks];
3853 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003854 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003855 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003856 for (int i = 0; i < numTasks; ++i) {
3857 final TaskRecord task = tasks.get(i);
3858 taskIds[i] = task.taskId;
3859 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3860 : task.realActivity != null ? task.realActivity.flattenToString()
3861 : task.getTopActivity() != null ? task.getTopActivity().packageName
3862 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003863 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003864 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003865 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 }
3867 info.taskIds = taskIds;
3868 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003869 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003870 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003871
3872 final ActivityRecord top = stack.topRunningActivityLocked();
3873 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 return info;
3875 }
3876
3877 StackInfo getStackInfoLocked(int stackId) {
3878 ActivityStack stack = getStack(stackId);
3879 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003880 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 }
3882 return null;
3883 }
3884
3885 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003886 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003887 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3888 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003889 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003890 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003891 }
3892 }
3893 return list;
3894 }
3895
Craig Mautner15df08a2015-04-01 12:17:18 -07003896 TaskRecord getLockedTaskLocked() {
3897 final int top = mLockTaskModeTasks.size() - 1;
3898 if (top >= 0) {
3899 return mLockTaskModeTasks.get(top);
3900 }
3901 return null;
3902 }
3903
3904 boolean isLockedTask(TaskRecord task) {
3905 return mLockTaskModeTasks.contains(task);
3906 }
3907
3908 boolean isLastLockedTask(TaskRecord task) {
3909 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3910 }
3911
3912 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003913 if (!mLockTaskModeTasks.remove(task)) {
3914 return;
3915 }
3916 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3917 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003918 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003919 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3920 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003921 final Message lockTaskMsg = Message.obtain();
3922 lockTaskMsg.arg1 = task.userId;
3923 lockTaskMsg.what = LOCK_TASK_END_MSG;
3924 mHandler.sendMessage(lockTaskMsg);
3925 }
3926 }
3927
Andrii Kulianc27916642016-04-12 17:59:27 -07003928 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3929 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3930 false /* forceNonResizable */);
3931 }
3932
Jorim Jaggid53f0922016-04-06 22:16:23 -07003933 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003934 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003935 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3936 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003937 return;
3938 }
3939
Jorim Jaggicd13d332016-04-27 15:40:20 -07003940 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003941 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003942 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003943 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003944
Andrii Kulianc27916642016-04-12 17:59:27 -07003945 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3946 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003947 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003948 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003949 && !topActivity.noDisplay) {
3950 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003951 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3952 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003953 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003954 }
3955
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003956 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003957 if (mLockTaskNotify != null) {
3958 mLockTaskNotify.showToast(mLockTaskModeState);
3959 }
Jason Monka8f569c2014-07-07 12:15:21 -04003960 }
3961
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003962 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3963 if (mLockTaskModeTasks.contains(task)) {
3964 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3965 }
3966 }
3967
Craig Mautner432f64e2015-05-20 14:59:57 -07003968 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3969 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003970 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003971 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 final TaskRecord lockedTask = getLockedTaskLocked();
3973 if (lockedTask != null) {
3974 removeLockedTaskLocked(lockedTask);
3975 if (!mLockTaskModeTasks.isEmpty()) {
3976 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003977 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3978 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003979 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003980 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003981 return;
3982 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003983 }
Craig Mautnere0570202015-05-13 13:06:11 -07003984 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3985 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003986 return;
3987 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003988
3989 // Should have already been checked, but do it again.
3990 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003991 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3992 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003993 return;
3994 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003995 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003996 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003997 return;
3998 }
3999
4000 if (mLockTaskModeTasks.isEmpty()) {
4001 // First locktask.
4002 final Message lockTaskMsg = Message.obtain();
4003 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4004 lockTaskMsg.arg1 = task.userId;
4005 lockTaskMsg.what = LOCK_TASK_START_MSG;
4006 lockTaskMsg.arg2 = lockTaskModeState;
4007 mHandler.sendMessage(lockTaskMsg);
4008 }
4009 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004010 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4011 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004012 mLockTaskModeTasks.remove(task);
4013 mLockTaskModeTasks.add(task);
4014
4015 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004016 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004017 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004018
4019 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004020 findTaskToMoveToFrontLocked(task, 0, null, reason,
4021 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004022 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004023 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004024 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004025 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004026 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004027 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004028 }
4029
4030 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004031 return isLockTaskModeViolation(task, false);
4032 }
4033
4034 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4035 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004036 return false;
4037 }
4038 final int lockTaskAuth = task.mLockTaskAuth;
4039 switch (lockTaskAuth) {
4040 case LOCK_TASK_AUTH_DONT_LOCK:
4041 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004042 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004043 case LOCK_TASK_AUTH_LAUNCHABLE:
4044 case LOCK_TASK_AUTH_WHITELISTED:
4045 return false;
4046 case LOCK_TASK_AUTH_PINNABLE:
4047 // Pinnable tasks can't be launched on top of locktask tasks.
4048 return !mLockTaskModeTasks.isEmpty();
4049 default:
4050 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4051 return true;
4052 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004053 }
4054
Craig Mautner15df08a2015-04-01 12:17:18 -07004055 void onLockTaskPackagesUpdatedLocked() {
4056 boolean didSomething = false;
4057 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4058 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004059 final boolean wasWhitelisted =
4060 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4061 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004062 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004063 final boolean isWhitelisted =
4064 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4065 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4066 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004067 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004068 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4069 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004070 removeLockedTaskLocked(lockedTask);
4071 lockedTask.performClearTaskLocked();
4072 didSomething = true;
4073 }
4074 }
4075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4076 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4077 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4078 final ActivityStack stack = stacks.get(stackNdx);
4079 stack.onLockTaskPackagesUpdatedLocked();
4080 }
4081 }
Craig Mautnere0570202015-05-13 13:06:11 -07004082 final ActivityRecord r = topRunningActivityLocked();
4083 final TaskRecord task = r != null ? r.task : null;
4084 if (mLockTaskModeTasks.isEmpty() && task != null
4085 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4086 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004087 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4088 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4089 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4090 false);
4091 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004092 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004093 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004094 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004095 }
4096 }
4097
Benjamin Franz43261142015-02-11 15:59:44 +00004098 int getLockTaskModeState() {
4099 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004100 }
4101
Jorim Jaggife89d122015-12-22 16:28:44 +01004102 void activityRelaunchedLocked(IBinder token) {
4103 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004104 if (mService.isSleepingOrShuttingDownLocked()) {
4105 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4106 if (r != null) {
4107 r.setSleeping(true, true);
4108 }
4109 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004110 }
4111
4112 void activityRelaunchingLocked(ActivityRecord r) {
4113 mWindowManager.notifyAppRelaunching(r.appToken);
4114 }
4115
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004116 void logStackState() {
4117 mActivityMetricsLogger.logWindowState();
4118 }
4119
Winson Chung5af42fc2017-03-24 17:11:33 -07004120 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004121 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4122 final ActivityRecord r = task.mActivities.get(i);
4123 if (r.app != null && r.app.thread != null) {
4124 mMultiWindowModeChangedActivities.add(r);
4125 }
4126 }
4127
4128 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4129 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4130 }
4131 }
4132
Winson Chung5af42fc2017-03-24 17:11:33 -07004133 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004134 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004135 if (prevStack == null || prevStack == stack
4136 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4137 return;
4138 }
4139
Winson Chung5af42fc2017-03-24 17:11:33 -07004140 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4141 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004142
Winson Chung5af42fc2017-03-24 17:11:33 -07004143 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4144 boolean immediate) {
4145
4146 if (immediate) {
4147 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4148 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4149 final ActivityRecord r = task.mActivities.get(i);
4150 if (r.app != null && r.app.thread != null) {
4151 r.updatePictureInPictureMode(targetStackBounds);
4152 }
4153 }
4154 } else {
4155 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4156 final ActivityRecord r = task.mActivities.get(i);
4157 if (r.app != null && r.app.thread != null) {
4158 mPipModeChangedActivities.add(r);
4159 }
4160 }
4161 mPipModeChangedTargetStackBounds = targetStackBounds;
4162
4163 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4164 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4165 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004166 }
4167 }
4168
Tony Mak853304c2016-04-18 15:17:41 +01004169 void setDockedStackMinimized(boolean minimized) {
4170 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004171 }
4172
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004173 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004174
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004175 public ActivityStackSupervisorHandler(Looper looper) {
4176 super(looper);
4177 }
4178
Winson Chung4dabf232017-01-25 13:25:22 -08004179 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004180 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004181 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4182 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004183 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004184 }
4185
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004186 @Override
4187 public void handleMessage(Message msg) {
4188 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004189 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4190 synchronized (mService) {
4191 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4192 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004193 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004194 }
4195 }
4196 } break;
4197 case REPORT_PIP_MODE_CHANGED_MSG: {
4198 synchronized (mService) {
4199 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4200 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004201 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004202 }
4203 }
4204 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004205 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004206 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4207 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004208 if (mService.mDidDexOpt) {
4209 mService.mDidDexOpt = false;
4210 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4211 nmsg.obj = msg.obj;
4212 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4213 return;
4214 }
4215 // We don't at this point know if the activity is fullscreen,
4216 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004217 activityIdleInternal((ActivityRecord) msg.obj,
4218 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004219 } break;
4220 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004221 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004222 activityIdleInternal((ActivityRecord) msg.obj,
4223 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004224 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004225 case RESUME_TOP_ACTIVITY_MSG: {
4226 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004227 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004228 }
4229 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004230 case SLEEP_TIMEOUT_MSG: {
4231 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004232 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004233 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4234 mSleepTimeout = true;
4235 checkReadyForSleepLocked();
4236 }
4237 }
4238 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004239 case LAUNCH_TIMEOUT_MSG: {
4240 if (mService.mDidDexOpt) {
4241 mService.mDidDexOpt = false;
4242 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4243 return;
4244 }
4245 synchronized (mService) {
4246 if (mLaunchingActivity.isHeld()) {
4247 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4248 if (VALIDATE_WAKE_LOCK_CALLER
4249 && Binder.getCallingUid() != Process.myUid()) {
4250 throw new IllegalStateException("Calling must be system uid");
4251 }
4252 mLaunchingActivity.release();
4253 }
4254 }
4255 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004257 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004258 } break;
4259 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004260 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 } break;
4262 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004263 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004265 case CONTAINER_CALLBACK_VISIBILITY: {
4266 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004267 final IActivityContainerCallback callback = container.mCallback;
4268 if (callback != null) {
4269 try {
4270 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4271 } catch (RemoteException e) {
4272 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004273 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004274 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004275 case LOCK_TASK_START_MSG: {
4276 // When lock task starts, we disable the status bars.
4277 try {
Jason Monk62515be2014-05-21 16:06:19 -04004278 if (mLockTaskNotify == null) {
4279 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004280 }
Jason Monk62515be2014-05-21 16:06:19 -04004281 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004282 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004283 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004284 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004285 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004286 flags = StatusBarManager.DISABLE_MASK
4287 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004288 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004289 flags = StatusBarManager.DISABLE_MASK
4290 & (~StatusBarManager.DISABLE_BACK)
4291 & (~StatusBarManager.DISABLE_HOME)
4292 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004293 }
4294 getStatusBarService().disable(flags, mToken,
4295 mService.mContext.getPackageName());
4296 }
4297 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004298 if (getDevicePolicyManager() != null) {
4299 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004300 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004301 }
justinzhang5286d3f2014-05-12 17:06:01 -04004302 } catch (RemoteException ex) {
4303 throw new RuntimeException(ex);
4304 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004305 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004306 case LOCK_TASK_END_MSG: {
4307 // When lock task ends, we enable the status bars.
4308 try {
Jason Monk62515be2014-05-21 16:06:19 -04004309 if (getStatusBarService() != null) {
4310 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4311 mService.mContext.getPackageName());
4312 }
4313 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004314 if (getDevicePolicyManager() != null) {
4315 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4316 msg.arg1);
4317 }
Jason Monk62515be2014-05-21 16:06:19 -04004318 if (mLockTaskNotify == null) {
4319 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4320 }
4321 mLockTaskNotify.show(false);
4322 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004323 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004324 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004325 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004326 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004327 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004328 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004329 new LockPatternUtils(mService.mContext)
4330 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004331 }
4332 } catch (SettingNotFoundException e) {
4333 // No setting, don't lock.
4334 }
justinzhang5286d3f2014-05-12 17:06:01 -04004335 } catch (RemoteException ex) {
4336 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004337 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004338 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004339 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004340 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004341 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4342 if (mLockTaskNotify == null) {
4343 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4344 }
4345 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4346 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004347 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4348 final ActivityContainer container = (ActivityContainer) msg.obj;
4349 final IActivityContainerCallback callback = container.mCallback;
4350 if (callback != null) {
4351 try {
4352 callback.onAllActivitiesComplete(container.asBinder());
4353 } catch (RemoteException e) {
4354 }
4355 }
4356 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004357 case LAUNCH_TASK_BEHIND_COMPLETE: {
4358 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004359 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004360 if (r != null) {
4361 handleLaunchTaskBehindCompleteLocked(r);
4362 }
4363 }
4364 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004365
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004366 }
4367 }
4368 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004369
Ying Wangb081a592014-04-22 15:20:16 -07004370 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004371 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4372 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004373 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004374 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004375 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004376 ActivityRecord mParentActivity = null;
4377 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004378
Craig Mautnere3a00d72014-04-16 08:31:19 -07004379 boolean mVisible = true;
4380
Craig Mautner4a1cb222013-12-04 16:14:06 -08004381 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004382 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004383
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004384 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4385 final static int CONTAINER_STATE_NO_SURFACE = 1;
4386 final static int CONTAINER_STATE_FINISHING = 2;
4387 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4388
Wale Ogunwale1666e312016-12-16 11:27:18 -08004389 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004390 synchronized (mService) {
4391 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004392 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004393 switch (mStackId) {
4394 case PINNED_STACK_ID:
4395 new PinnedActivityStack(this, mRecentTasks, onTop);
4396 break;
4397 default:
4398 new ActivityStack(this, mRecentTasks, onTop);
4399 break;
4400 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004401 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004402 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004403 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004404 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004405
Andrii Kulian839def92016-11-02 10:58:58 -07004406 /**
4407 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004408 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004409 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004410 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004411 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004412 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004413 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004414 if (mActivityDisplay != null) {
4415 throw new IllegalStateException("ActivityContainer is already attached, " +
4416 "displayId=" + mActivityDisplay.mDisplayId);
4417 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004418 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004419 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004420 }
4421
4422 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004423 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004424 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004425 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4426 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004427 return;
4428 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004429 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004430 }
4431 }
4432
4433 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004434 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004435 synchronized (mService) {
4436 if (mActivityDisplay != null) {
4437 return mActivityDisplay.mDisplayId;
4438 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004439 }
4440 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004441 }
4442
Jeff Brownca9bc702014-02-11 14:32:56 -08004443 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004444 public int getStackId() {
4445 synchronized (mService) {
4446 return mStackId;
4447 }
4448 }
4449
4450 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004451 public boolean injectEvent(InputEvent event) {
4452 final long origId = Binder.clearCallingIdentity();
4453 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004454 synchronized (mService) {
4455 if (mActivityDisplay != null) {
4456 return mInputManagerInternal.injectInputEvent(event,
4457 mActivityDisplay.mDisplayId,
4458 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4459 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004460 }
4461 return false;
4462 } finally {
4463 Binder.restoreCallingIdentity(origId);
4464 }
4465 }
4466
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004467 @Override
4468 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004469 synchronized (mService) {
4470 if (mContainerState == CONTAINER_STATE_FINISHING) {
4471 return;
4472 }
4473 mContainerState = CONTAINER_STATE_FINISHING;
4474
Craig Mautnerd163e752014-06-13 17:18:47 -07004475 long origId = Binder.clearCallingIdentity();
4476 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004477 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004478 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004479 } finally {
4480 Binder.restoreCallingIdentity(origId);
4481 }
4482 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004483 }
4484
Andrii Kulian03c403d2016-11-11 11:14:12 -08004485 /**
4486 * Remove the stack completely. Must be called only when there are no tasks left in it,
4487 * as this method does not finish running activities.
4488 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004489 void removeLocked() {
4490 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004491 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004492 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004493 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004495 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004496 }
4497
Andrii Kulian839def92016-11-02 10:58:58 -07004498 /**
4499 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4500 * completely or re-parented.
4501 */
4502 private void removeFromDisplayLocked() {
4503 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4504 + " current displayId=" + mActivityDisplay.mDisplayId);
4505
Wale Ogunwale1666e312016-12-16 11:27:18 -08004506 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004507 mActivityDisplay = null;
4508 }
4509
4510 /**
4511 * Move the stack to specified display.
4512 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004513 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004514 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004515 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004516 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4517 + mActivityDisplay + " to display=" + activityDisplay
4518 + " Callers=" + Debug.getCallers(2));
4519
4520 removeFromDisplayLocked();
4521
4522 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004523 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004524 }
4525
Craig Mautner4a1cb222013-12-04 16:14:06 -08004526 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004527 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004528 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004529 }
4530
4531 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004532 public final int startActivityIntentSender(IIntentSender intentSender)
4533 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004534 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4535
4536 if (!(intentSender instanceof PendingIntentRecord)) {
4537 throw new IllegalArgumentException("Bad PendingIntent object");
4538 }
4539
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004540 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004541 Binder.getCallingUid(), mCurrentUser, false,
4542 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004543
4544 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4545 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4546 pendingIntent.key.requestResolvedType);
4547
4548 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4549 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4550 }
4551
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004552 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004553 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004554 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004555 throw new SecurityException(
4556 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4557 }
4558 }
4559
Craig Mautnerdf88d732014-01-27 09:21:32 -08004560 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004561 public IBinder asBinder() {
4562 return this;
4563 }
4564
Craig Mautner4504de52013-12-20 09:06:56 -08004565 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004566 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004567 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004568 }
4569
Craig Mautner4a1cb222013-12-04 16:14:06 -08004570 ActivityStackSupervisor getOuter() {
4571 return ActivityStackSupervisor.this;
4572 }
4573
Craig Mautnerd163e752014-06-13 17:18:47 -07004574 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004575 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004576 }
4577
Craig Mautner6985bad2014-04-21 15:22:06 -07004578 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004579 void setVisible(boolean visible) {
4580 if (mVisible != visible) {
4581 mVisible = visible;
4582 if (mCallback != null) {
4583 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4584 0 /* unused */, this).sendToTarget();
4585 }
4586 }
4587 }
4588
Craig Mautner6985bad2014-04-21 15:22:06 -07004589 void setDrawn() {
4590 }
4591
Craig Mautner1b4bf852014-05-26 15:06:32 -07004592 // You can always start a new task on a regular ActivityStack.
4593 boolean isEligibleForNewTasks() {
4594 return true;
4595 }
4596
Craig Mautnerd163e752014-06-13 17:18:47 -07004597 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004598 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004599 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004600 }
4601
Craig Mautner34b73df2014-01-12 21:11:08 -08004602 @Override
4603 public String toString() {
4604 return mIdString + (mActivityDisplay == null ? "N" : "A");
4605 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004606 }
4607
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004608 private class VirtualActivityContainer extends ActivityContainer {
4609 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004610 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004611
4612 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004613 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4614 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004615 mParentActivity = parent;
4616 mCallback = callback;
4617 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004618 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004619 }
4620
4621 @Override
4622 public void setSurface(Surface surface, int width, int height, int density) {
4623 super.setSurface(surface, width, height, density);
4624
4625 synchronized (mService) {
4626 final long origId = Binder.clearCallingIdentity();
4627 try {
4628 setSurfaceLocked(surface, width, height, density);
4629 } finally {
4630 Binder.restoreCallingIdentity(origId);
4631 }
4632 }
4633 }
4634
4635 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4636 if (mContainerState == CONTAINER_STATE_FINISHING) {
4637 return;
4638 }
4639 VirtualActivityDisplay virtualActivityDisplay =
4640 (VirtualActivityDisplay) mActivityDisplay;
4641 if (virtualActivityDisplay == null) {
4642 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004643 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004644 mActivityDisplay = virtualActivityDisplay;
4645 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004646 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004647 }
4648
4649 if (mSurface != null) {
4650 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004651 }
4652
Craig Mautner6985bad2014-04-21 15:22:06 -07004653 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004654 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004655 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004656 } else {
4657 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004658 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004659 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004660 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004661 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004662 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004663
Craig Mautnerd163e752014-06-13 17:18:47 -07004664 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004665
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004666 if (DEBUG_STACK) Slog.d(TAG_STACK,
4667 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004668 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004669
Craig Mautner6985bad2014-04-21 15:22:06 -07004670 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004671 boolean isAttachedLocked() {
4672 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004673 }
4674
4675 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004676 void setDrawn() {
4677 synchronized (mService) {
4678 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004679 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004680 }
4681 }
4682
Craig Mautner1b4bf852014-05-26 15:06:32 -07004683 // Never start a new task on an ActivityView if it isn't explicitly specified.
4684 @Override
4685 boolean isEligibleForNewTasks() {
4686 return false;
4687 }
4688
Craig Mautnerd163e752014-06-13 17:18:47 -07004689 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004690 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004691 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4692 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4693 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4694 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4695 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696 }
4697 }
4698
Craig Mautner4a1cb222013-12-04 16:14:06 -08004699 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4700 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004701 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004702 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004703 int mDisplayId;
4704 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004705
Craig Mautner4a1cb222013-12-04 16:14:06 -08004706 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4707 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004708 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004709
Jose Lima4b6c6692014-08-12 17:41:12 -07004710 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004711
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004712 /** Array of all UIDs that are present on the display. */
4713 private IntArray mDisplayAccessUIDs = new IntArray();
4714
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004715 ActivityDisplay() {
4716 }
Craig Mautner4504de52013-12-20 09:06:56 -08004717
Craig Mautner1a70a162014-09-13 12:09:31 -07004718 // After instantiation, check that mDisplay is not null before using this. The alternative
4719 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004720 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004721 final Display display = mDisplayManager.getDisplay(displayId);
4722 if (display == null) {
4723 return;
4724 }
4725 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004726 }
4727
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004728 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004729 mDisplay = display;
4730 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004731 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004732
Winson Chung7c3ede32017-03-14 14:18:34 -07004733 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004734 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004735 + " to displayId=" + mDisplayId + " position=" + position);
4736 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004737 }
4738
Wale Ogunwale1666e312016-12-16 11:27:18 -08004739 void detachStack(ActivityStack stack) {
4740 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004741 + " from displayId=" + mDisplayId);
4742 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004743 }
4744
Jose Lima4b6c6692014-08-12 17:41:12 -07004745 void setVisibleBehindActivity(ActivityRecord r) {
4746 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004747 }
4748
Jose Lima4b6c6692014-08-12 17:41:12 -07004749 boolean hasVisibleBehindActivity() {
4750 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004751 }
4752
Craig Mautner34b73df2014-01-12 21:11:08 -08004753 @Override
4754 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004755 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4756 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004757
4758 @Override
4759 protected int getChildCount() {
4760 return mStacks.size();
4761 }
4762
4763 @Override
4764 protected ConfigurationContainer getChildAt(int index) {
4765 return mStacks.get(index);
4766 }
4767
4768 @Override
4769 protected ConfigurationContainer getParent() {
4770 return ActivityStackSupervisor.this;
4771 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004772
4773 boolean isPrivate() {
4774 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4775 }
4776
4777 boolean isUidPresent(int uid) {
4778 for (ActivityStack stack : mStacks) {
4779 if (stack.isUidPresent(uid)) {
4780 return true;
4781 }
4782 }
4783 return false;
4784 }
4785
4786 /** Update and get all UIDs that are present on the display and have access to it. */
4787 private IntArray getPresentUIDs() {
4788 mDisplayAccessUIDs.clear();
4789 for (ActivityStack stack : mStacks) {
4790 stack.getPresentUIDs(mDisplayAccessUIDs);
4791 }
4792 return mDisplayAccessUIDs;
4793 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004794
4795 boolean shouldDestroyContentOnRemove() {
4796 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4797 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004798 }
4799
4800 class VirtualActivityDisplay extends ActivityDisplay {
4801 VirtualDisplay mVirtualDisplay;
4802
Craig Mautner6985bad2014-04-21 15:22:06 -07004803 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004804 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004805 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4806 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4807 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4808 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004809
4810 init(mVirtualDisplay.getDisplay());
4811
Bryce Leeaf3a4002017-03-20 10:37:12 -07004812 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004813 }
4814
4815 void setSurface(Surface surface) {
4816 if (mVirtualDisplay != null) {
4817 mVirtualDisplay.setSurface(surface);
4818 }
4819 }
4820
4821 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004822 void detachStack(ActivityStack stack) {
4823 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004824 if (mVirtualDisplay != null) {
4825 mVirtualDisplay.release();
4826 mVirtualDisplay = null;
4827 }
4828 }
4829
4830 @Override
4831 public String toString() {
4832 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004833 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004834 }
Jose Lima58e66d62014-05-27 20:00:27 -07004835
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004836 ActivityStack findStackBehind(ActivityStack stack) {
4837 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004838 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004839 if (display == null) {
4840 return null;
4841 }
4842 final ArrayList<ActivityStack> stacks = display.mStacks;
4843 for (int i = stacks.size() - 1; i >= 0; i--) {
4844 if (stacks.get(i) == stack && i > 0) {
4845 return stacks.get(i - 1);
4846 }
4847 }
4848 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4849 + " in=" + stacks);
4850 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004851
Jorim Jaggic69bd222016-03-15 14:38:37 +01004852 /**
4853 * Puts a task into resizing mode during the next app transition.
4854 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004855 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004856 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004857 private void setResizingDuringAnimation(TaskRecord task) {
4858 mResizingTasksDuringAnimation.add(task.taskId);
4859 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004860 }
4861
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004862 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4863 final TaskRecord task;
4864 final int callingUid;
4865 final String callingPackage;
4866 final Intent intent;
4867 final int userId;
4868 final ActivityOptions activityOptions = (bOptions != null)
4869 ? new ActivityOptions(bOptions) : null;
4870 final int launchStackId = (activityOptions != null)
4871 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004872 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004873 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004874 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004875 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004876
4877 if (launchStackId == DOCKED_STACK_ID) {
4878 mWindowManager.setDockedStackCreateState(
4879 activityOptions.getDockCreateMode(), null /* initialBounds */);
4880
4881 // Defer updating the stack in which recents is until the app transition is done, to
4882 // not run into issues where we still need to draw the task in recents but the
4883 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004884 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004885 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4886 }
4887
Winson Chung7900bee2017-03-10 08:59:25 -08004888 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4889 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004890 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004891 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004892 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004893 throw new IllegalArgumentException(
4894 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4895 }
4896
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004897 // Since we don't have an actual source record here, we assume that the currently focused
4898 // activity was the source.
4899 final ActivityStack focusedStack = getFocusedStack();
4900 final ActivityRecord sourceRecord =
4901 focusedStack != null ? focusedStack.topActivity() : null;
4902
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004903 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004904 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004905 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4906 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004907 }
4908 }
4909
4910 // If the user must confirm credentials (e.g. when first launching a work app and the
4911 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4912 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4913 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004914 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004915 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004916 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004917 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4918 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004919
4920 // If we are launching the task in the docked stack, put it into resizing mode so
4921 // the window renders full-screen with the background filling the void. Also only
4922 // call this at the end to make sure that tasks exists on the window manager side.
4923 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004924 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004925 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004926
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004927 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004928 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004929 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4930 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004931 return ActivityManager.START_TASK_TO_FRONT;
4932 }
4933 callingUid = task.mCallingUid;
4934 callingPackage = task.mCallingPackage;
4935 intent = task.intent;
4936 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4937 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004938 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4939 null, null, 0, 0, bOptions, userId, null, task);
4940 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004941 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004942 }
4943 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004944 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004945
4946 /**
4947 * @return a list of activities which are the top ones in each visible stack. The first
4948 * entry will be the focused activity.
4949 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004950 List<IBinder> getTopVisibleActivities() {
4951 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4952 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004953 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004954 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4955 // Traverse all stacks on a display.
4956 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4957 final ActivityStack stack = display.mStacks.get(j);
4958 // Get top activity from a visible stack and add it to the list.
4959 if (stack.getStackVisibilityLocked(null /* starting */)
4960 == ActivityStack.STACK_VISIBLE) {
4961 final ActivityRecord top = stack.topActivity();
4962 if (top != null) {
4963 if (stack == mFocusedStack) {
4964 topActivityTokens.add(0, top.appToken);
4965 } else {
4966 topActivityTokens.add(top.appToken);
4967 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004968 }
4969 }
4970 }
4971 }
4972 return topActivityTokens;
4973 }
Craig Mautner27084302013-03-25 08:05:25 -07004974}