blob: b623b2f2b180440e9ba320dc263921ac74254764 [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
Craig Mautnerf3333272013-04-22 10:55:53 -0700374 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700375 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700376
Craig Mautnerde4ef022013-04-07 19:01:33 -0700377 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
378 * is being brought in front of us. */
379 boolean mUserLeaving = false;
380
Craig Mautner0eea92c2013-05-16 13:35:39 -0700381 /** Set when we have taken too long waiting to go to sleep. */
382 boolean mSleepTimeout = false;
383
384 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700385 * We don't want to allow the device to go to sleep while in the process
386 * of launching an activity. This is primarily to allow alarm intent
387 * receivers to launch an activity and get that to run before the device
388 * goes back to sleep.
389 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700390 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700391
392 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700393 * Set when the system is going to sleep, until we have
394 * successfully paused the current activity and released our wake lock.
395 * At that point the system is allowed to actually sleep.
396 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700397 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700398
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700399 /** Stack id of the front stack when user switched, indexed by userId. */
400 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700401
Craig Mautner4504de52013-12-20 09:06:56 -0800402 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800403 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800404 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800405
Bryce Leeaf3a4002017-03-20 10:37:12 -0700406 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800407 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700408 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800409
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800410 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
411
412 private DisplayManagerInternal mDisplayManagerInternal;
413 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800414
Craig Mautner15df08a2015-04-01 12:17:18 -0700415 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
416 * may be finished until there is only one entry left. If this is empty the system is not
417 * in lockTask mode. */
418 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000419 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700420 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
421 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000422 */
423 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400424 /**
425 * Notifies the user when entering/exiting lock-task.
426 */
427 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800428
Wale Ogunwaled046a012015-12-24 13:05:59 -0800429 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800430 boolean inResumeTopActivity;
431
Andrii Kulian1e32e022016-09-16 15:29:34 -0700432 /**
433 * Temporary rect used during docked stack resize calculation so we don't need to create a new
434 * object each time.
435 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700436 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700437
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700438 // The default minimal size that will be used if the activity doesn't specify its minimal size.
439 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700440 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700441
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700442 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
443 private boolean mTaskLayersChanged = true;
444
Jorim Jaggi275561a2016-02-23 10:11:02 -0500445 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800446
Andrii Kulian1779e612016-10-12 21:58:25 -0700447 @Override
448 protected int getChildCount() {
449 return mActivityDisplays.size();
450 }
451
452 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700453 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700454 return mActivityDisplays.valueAt(index);
455 }
456
457 @Override
458 protected ConfigurationContainer getParent() {
459 return null;
460 }
461
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700462 Configuration getDisplayOverrideConfiguration(int displayId) {
463 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
464 if (activityDisplay == null) {
465 throw new IllegalArgumentException("No display found with id: " + displayId);
466 }
467
468 return activityDisplay.getOverrideConfiguration();
469 }
470
471 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
472 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
473 if (activityDisplay == null) {
474 throw new IllegalArgumentException("No display found with id: " + displayId);
475 }
476
477 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
478 }
479
Wale Ogunwale39381972015-12-17 17:15:29 -0800480 static class FindTaskResult {
481 ActivityRecord r;
482 boolean matchedByRootAffinity;
483 }
484 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
485
Craig Mautneree36c772014-07-16 14:56:05 -0700486 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800487 * Temp storage for display ids sorted in focus order.
488 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
489 * it's more efficient, as the number of displays is usually small.
490 */
491 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
492
493 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100494 * Used to keep track whether app visibilities got changed since the last pause. Useful to
495 * determine whether to invoke the task stack change listener after pausing.
496 */
497 boolean mAppVisibilitiesChangedSinceLastPause;
498
499 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100500 * Set of tasks that are in resizing mode during an app transition to fill the "void".
501 */
502 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
503
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700504
505 /**
506 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
507 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
508 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
509 * like the docked stack going empty.
510 */
511 private boolean mAllowDockedStackResize = true;
512
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100513 /**
Tony Mak853304c2016-04-18 15:17:41 +0100514 * Is dock currently minimized.
515 */
516 boolean mIsDockMinimized;
517
Jorim Jaggife762342016-10-13 14:33:27 +0200518 final KeyguardController mKeyguardController;
519
Tony Mak853304c2016-04-18 15:17:41 +0100520 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700521 * Description of a request to start a new activity, which has been held
522 * due to app switches being disabled.
523 */
524 static class PendingActivityLaunch {
525 final ActivityRecord r;
526 final ActivityRecord sourceRecord;
527 final int startFlags;
528 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700529 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700530
531 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700532 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700533 r = _r;
534 sourceRecord = _sourceRecord;
535 startFlags = _startFlags;
536 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700537 callerApp = _callerApp;
538 }
539
540 void sendErrorResult(String message) {
541 try {
542 if (callerApp.thread != null) {
543 callerApp.thread.scheduleCrash(message);
544 }
545 } catch (RemoteException e) {
546 Slog.e(TAG, "Exception scheduling crash of failed "
547 + "activity launcher sourceRecord=" + sourceRecord, e);
548 }
Craig Mautneree36c772014-07-16 14:56:05 -0700549 }
550 }
551
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800552 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700553 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700554 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800555 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200556 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700557 }
558
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800559 void setRecentTasks(RecentTasks recentTasks) {
560 mRecentTasks = recentTasks;
561 }
562
Jeff Brown2c43c332014-06-12 22:38:59 -0700563 /**
564 * At the time when the constructor runs, the power manager has not yet been
565 * initialized. So we initialize our wakelocks afterwards.
566 */
567 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700569 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700570 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700571 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700572 }
573
justinzhang5286d3f2014-05-12 17:06:01 -0400574 // This function returns a IStatusBarService. The value is from ServiceManager.
575 // getService and is cached.
576 private IStatusBarService getStatusBarService() {
577 synchronized (mService) {
578 if (mStatusBarService == null) {
579 mStatusBarService = IStatusBarService.Stub.asInterface(
580 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
581 if (mStatusBarService == null) {
582 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
583 }
584 }
585 return mStatusBarService;
586 }
587 }
588
Jason Monk35c62a42014-06-17 10:24:47 -0400589 private IDevicePolicyManager getDevicePolicyManager() {
590 synchronized (mService) {
591 if (mDevicePolicyManager == null) {
592 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
593 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
594 if (mDevicePolicyManager == null) {
595 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
596 }
597 }
598 return mDevicePolicyManager;
599 }
600 }
601
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700602 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603 synchronized (mService) {
604 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200605 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606
607 mDisplayManager =
608 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
609 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800610 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611
612 Display[] displays = mDisplayManager.getDisplays();
613 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
614 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800615 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700616 if (activityDisplay.mDisplay == null) {
617 throw new IllegalStateException("Default Display does not exist");
618 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700620 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 }
622
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800623 mHomeStack = mFocusedStack = mLastFocusedStack =
624 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800625
626 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 }
Craig Mautner27084302013-03-25 08:05:25 -0700628 }
629
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700630 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
633
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700636 }
637
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700638 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700639 if (stack == null) {
640 return false;
641 }
642
Craig Mautnerdf88d732014-01-27 09:21:32 -0800643 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
644 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700645 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800646 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800647 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700648 }
649
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800650 /** The top most stack on its display. */
651 boolean isFrontStackOnDisplay(ActivityStack stack) {
652 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
653 }
654
655 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700656 if (stack == null) {
657 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700659
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700660 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
661 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700662 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800663 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800664 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700665 }
666
Wale Ogunwaled046a012015-12-24 13:05:59 -0800667 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800668 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
669 if (!focusCandidate.isFocusable()) {
670 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800671 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800672 }
673
674 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800675 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800676 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800677
Wale Ogunwaled046a012015-12-24 13:05:59 -0800678 EventLogTags.writeAmFocusedStack(
679 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
680 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
681 }
682
683 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800684 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800685 if (r != null && r.idle) {
686 checkFinishBootingLocked();
687 }
688 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700689 }
690
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700691 void moveHomeStackToFront(String reason) {
692 mHomeStack.moveToFront(reason);
693 }
694
Matthew Ng330757d2017-02-28 14:19:17 -0800695 void moveRecentsStackToFront(String reason) {
696 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
697 if (recentsStack != null) {
698 recentsStack.moveToFront(reason);
699 }
700 }
701
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700702 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800703 boolean moveHomeStackTaskToTop(String reason) {
704 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700705
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700706 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700707 if (top == null) {
708 return false;
709 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700710 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700711 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700712 }
713
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800714 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700715 if (!mService.mBooting && !mService.mBooted) {
716 // Not ready yet!
717 return false;
718 }
719
Craig Mautner84984fa2014-06-19 11:19:20 -0700720 if (prev != null) {
721 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
722 }
723
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800724 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700725 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800726 final String myReason = reason + " resumeHomeStackTask";
727
Mark Lua56ea122015-10-08 13:31:01 +0800728 // Only resume home activity if isn't finishing.
729 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700730 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800731 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700732 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800733 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700734 }
735
Craig Mautner8d341ef2013-03-26 09:03:27 -0700736 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800737 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
738 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700739 }
740
741 /**
742 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
743 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800744 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700745 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800746 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
747 * valid if the matchMode is
748 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700749 */
Winson Chung7900bee2017-03-10 08:59:25 -0800750 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
751 // If there is a stack id set, ensure that we are attempting to actually restore a task
752 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
753 stackId != INVALID_STACK_ID) {
754 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
755 }
756
Craig Mautnere0a38842013-12-16 16:14:02 -0800757 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800758 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800759 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
761 ActivityStack stack = stacks.get(stackNdx);
762 TaskRecord task = stack.taskForIdLocked(id);
763 if (task != null) {
764 return task;
765 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700766 }
767 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800768
Winson Chung7900bee2017-03-10 08:59:25 -0800769 // If we are matching stack tasks only, return now
770 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800771 return null;
772 }
773
Winson Chung7900bee2017-03-10 08:59:25 -0800774 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
775 // the task from recents
776 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
777 TaskRecord task = mRecentTasks.taskForIdLocked(id);
778 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
779 if (DEBUG_RECENTS && task == null) {
780 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
781 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700782 return task;
783 }
784
Winson Chung7900bee2017-03-10 08:59:25 -0800785 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700786 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700787 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
788 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800789 return null;
790 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700791 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800792 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700793 }
794
Craig Mautner6170f732013-04-02 13:05:23 -0700795 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800796 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800797 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800798 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
800 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
801 if (r != null) {
802 return r;
803 }
Craig Mautner6170f732013-04-02 13:05:23 -0700804 }
805 }
806 return null;
807 }
808
Tony Mak853304c2016-04-18 15:17:41 +0100809 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000810 * Detects whether we should show a lock screen in front of this task for a locked user.
811 * <p>
812 * We'll do this if either of the following holds:
813 * <ul>
814 * <li>The top activity explicitly belongs to {@param userId}.</li>
815 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
816 * </ul>
817 *
818 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100819 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000820 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
821 // To handle the case that work app is in the task but just is not the top one.
822 final ActivityRecord activityRecord = task.getTopActivity();
823 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
824
825 return (activityRecord != null && activityRecord.userId == userId)
826 || (resultTo != null && resultTo.userId == userId);
827 }
828
829 /**
830 * Find all visible task stacks containing {@param userId} and intercept them with an activity
831 * to block out the contents and possibly start a credential-confirming intent.
832 *
833 * @param userId user handle for the locked managed profile.
834 */
835 void lockAllProfileTasks(@UserIdInt int userId) {
836 mWindowManager.deferSurfaceLayout();
837 try {
838 final List<ActivityStack> stacks = getStacks();
839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
840 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
841 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
842 final TaskRecord task = tasks.get(taskNdx);
843
844 // Check the task for a top activity belonging to userId, or returning a result
845 // to an activity belonging to userId. Example case: a document picker for
846 // personal files, opened by a work app, should still get locked.
847 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000848 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
849 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000850 }
Tony Mak853304c2016-04-18 15:17:41 +0100851 }
852 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000853 } finally {
854 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100855 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000856 }
857
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800858 void setNextTaskIdForUserLocked(int taskId, int userId) {
859 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
860 if (taskId > currentTaskId) {
861 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700862 }
863 }
864
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700865 static int nextTaskIdForUser(int taskId, int userId) {
866 int nextTaskId = taskId + 1;
867 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
868 // Wrap around as there will be smaller task ids that are available now.
869 nextTaskId -= MAX_TASK_IDS_PER_USER;
870 }
871 return nextTaskId;
872 }
873
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800874 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800875 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
876 // for a userId u, a taskId can only be in the range
877 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
878 // 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 -0700879 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800880 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800881 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800882 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700883 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800884 if (candidateTaskId == currentTaskId) {
885 // Something wrong!
886 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
887 throw new IllegalStateException("Cannot get an available task id."
888 + " Reached limit of " + MAX_TASK_IDS_PER_USER
889 + " running tasks per user.");
890 }
891 }
892 mCurTaskIdForUser.put(userId, candidateTaskId);
893 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700894 }
895
Chong Zhang6cda19c2016-06-14 19:07:56 -0700896 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800897 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700898 if (stack == null) {
899 return null;
900 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700901 ActivityRecord resumedActivity = stack.mResumedActivity;
902 if (resumedActivity == null || resumedActivity.app == null) {
903 resumedActivity = stack.mPausingActivity;
904 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700905 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700906 }
907 }
908 return resumedActivity;
909 }
910
Dianne Hackbornff072722014-09-24 10:56:28 -0700911 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700912 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
915 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
917 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700918 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800919 continue;
920 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700921 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 if (hr != null) {
923 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
924 && processName.equals(hr.processName)) {
925 try {
George Mount2c92c972014-03-20 09:38:23 -0700926 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 didSomething = true;
928 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700929 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 Slog.w(TAG, "Exception in new application when starting activity "
931 + hr.intent.getComponent().flattenToShortString(), e);
932 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700933 }
Craig Mautner20e72272013-04-01 13:45:53 -0700934 }
Craig Mautner20e72272013-04-01 13:45:53 -0700935 }
936 }
937 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700938 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700939 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700940 }
Craig Mautner20e72272013-04-01 13:45:53 -0700941 return didSomething;
942 }
943
944 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800945 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
946 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
948 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700949 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 continue;
951 }
952 final ActivityRecord resumedActivity = stack.mResumedActivity;
953 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700954 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800955 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 return false;
957 }
Craig Mautner20e72272013-04-01 13:45:53 -0700958 }
959 }
Wei Wang65c7a152016-06-02 18:51:22 -0700960 // Send launch end powerhint when idle
961 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700962 return true;
963 }
964
Craig Mautnerde4ef022013-04-07 19:01:33 -0700965 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800966 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
967 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
969 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700970 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 return false;
974 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 }
976 }
977 }
978 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700979 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
981 mLastFocusedStack + " to=" + mFocusedStack);
982 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700983 return true;
984 }
985
986 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800987 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
989 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
991 final ActivityStack stack = stacks.get(stackNdx);
992 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800993 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700994 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800995 return false;
996 }
997 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800998 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700999 }
1000 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001001 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001002 }
1003
Craig Mautner2acc3892013-09-23 10:28:14 -07001004 /**
1005 * Pause all activities in either all of the stacks or just the back stacks.
1006 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001007 * @param resuming The resuming activity.
1008 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1009 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001010 * @return true if any activity was paused as a result of this call.
1011 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001012 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001013 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1015 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1017 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001018 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001019 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001021 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1022 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001024 }
1025 }
1026 return someActivityPaused;
1027 }
1028
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001030 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001031 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1032 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1034 final ActivityStack stack = stacks.get(stackNdx);
1035 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001036 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 Slog.d(TAG_STATES,
1039 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001040 pausing = false;
1041 } else {
1042 return false;
1043 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001044 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001045 }
1046 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001047 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001048 }
1049
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001050 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001051 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001052 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1054 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1056 final ActivityStack stack = stacks.get(stackNdx);
1057 if (stack.mResumedActivity != null &&
1058 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001059 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001060 }
1061 }
1062 }
1063 }
1064
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001065 void cancelInitializingActivities() {
1066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1067 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1069 stacks.get(stackNdx).cancelInitializingActivities();
1070 }
1071 }
1072 }
1073
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001075 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 }
1077
1078 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1079 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -07001080 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001082 if (w.who == null) {
1083 changed = true;
1084 w.timeout = false;
1085 if (r != null) {
1086 w.who = new ComponentName(r.info.packageName, r.info.name);
1087 }
1088 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
1089 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001092 if (changed) {
1093 mService.notifyAll();
1094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 }
1096
Chong Zhang5022da32016-06-21 16:31:37 -07001097 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1098 boolean changed = false;
1099 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1100 WaitResult w = mWaitingActivityLaunched.remove(i);
1101 if (w.who == null) {
1102 changed = true;
1103 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1104 // the starting activity ends up moving another activity to front, and it should
1105 // wait for this new activity to become visible instead.
1106 // Do not modify other fields.
1107 w.result = START_TASK_TO_FRONT;
1108 }
1109 }
1110 if (changed) {
1111 mService.notifyAll();
1112 }
1113 }
1114
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1116 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001117 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001118 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001119 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001120 if (w.who == null) {
1121 changed = true;
1122 w.timeout = timeout;
1123 if (r != null) {
1124 w.who = new ComponentName(r.info.packageName, r.info.name);
1125 }
1126 w.thisTime = thisTime;
1127 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001128 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001130 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001131 if (changed) {
1132 mService.notifyAll();
1133 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001134 }
1135
Craig Mautner29219d92013-04-16 20:19:12 -07001136 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001137 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001138 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001139 if (r != null) {
1140 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001141 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001142
Andrii Kulian7318d632016-07-20 18:59:28 -07001143 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001144 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1145
1146 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1147 final int displayId = mTmpOrderedDisplayIds.get(i);
1148 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1149 if (stacks == null) {
1150 continue;
1151 }
1152 for (int j = stacks.size() - 1; j >= 0; --j) {
1153 final ActivityStack stack = stacks.get(j);
1154 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1155 r = stack.topRunningActivityLocked();
1156 if (r != null) {
1157 return r;
1158 }
Craig Mautner29219d92013-04-16 20:19:12 -07001159 }
1160 }
1161 }
1162 return null;
1163 }
1164
Dianne Hackborn09233282014-04-30 11:33:59 -07001165 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001166 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001167 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1168 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001169 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001170 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001171 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001172 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1173 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001174 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001175 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001176 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001177 }
1178 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001179
1180 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1181 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1182 while (maxNum > 0) {
1183 long mostRecentActiveTime = Long.MIN_VALUE;
1184 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001185 final int numTaskLists = runningTaskLists.size();
1186 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1187 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001188 if (!stackTaskList.isEmpty()) {
1189 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1190 if (lastActiveTime > mostRecentActiveTime) {
1191 mostRecentActiveTime = lastActiveTime;
1192 selectedStackList = stackTaskList;
1193 }
1194 }
1195 }
1196 if (selectedStackList != null) {
1197 list.add(selectedStackList.remove(0));
1198 --maxNum;
1199 } else {
1200 break;
1201 }
1202 }
Craig Mautner20e72272013-04-01 13:45:53 -07001203 }
1204
Todd Kennedy7440f172015-12-09 14:31:22 -08001205 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1206 ProfilerInfo profilerInfo) {
1207 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001208 if (aInfo != null) {
1209 // Store the found target back into the intent, because now that
1210 // we have it we never want to do this again. For example, if the
1211 // user navigates back to this point in the history, we should
1212 // always restart the exact same activity.
1213 intent.setComponent(new ComponentName(
1214 aInfo.applicationInfo.packageName, aInfo.name));
1215
1216 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001217 if (!aInfo.processName.equals("system")) {
1218 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001219 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001220 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001221
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001222 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1223 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1224 }
1225
Man Caocfa78b22015-06-11 20:14:34 -07001226 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1227 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1228 }
1229
1230 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001231 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001232 }
1233 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001234 final String intentLaunchToken = intent.getLaunchToken();
1235 if (aInfo.launchToken == null && intentLaunchToken != null) {
1236 aInfo.launchToken = intentLaunchToken;
1237 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001238 }
1239 return aInfo;
1240 }
1241
Todd Kennedy7440f172015-12-09 14:31:22 -08001242 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001243 return resolveIntent(intent, resolvedType, userId, 0);
1244 }
1245
1246 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001247 synchronized (mService) {
1248 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001249 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001250 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001251 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001252 }
1253
1254 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1255 ProfilerInfo profilerInfo, int userId) {
1256 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1257 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1258 }
1259
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001260 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1261 boolean andResume, boolean checkConfig) throws RemoteException {
1262
1263 if (!allPausedActivitiesComplete()) {
1264 // While there are activities pausing we skipping starting any new activities until
1265 // pauses are complete. NOTE: that we also do this for activities that are starting in
1266 // the paused state because they will first be resumed then paused on the client side.
1267 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1268 "realStartActivityLocked: Skipping start of r=" + r
1269 + " some activities pausing...");
1270 return false;
1271 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001272
Andrii Kulianada10292017-02-09 23:19:29 -08001273 r.startFreezingScreenLocked(app, 0);
1274 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275
Andrii Kulianada10292017-02-09 23:19:29 -08001276 // schedule launch ticks to collect information about slow apps.
1277 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001278
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001279 // Have the window manager re-evaluate the orientation of the screen based on the new
1280 // activity order. Note that as a result of this, it can call back into the activity
1281 // manager with a new orientation. We don't care about that, because the activity is not
1282 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001283 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001284 final int displayId = r.getDisplayId();
1285 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1286 getDisplayOverrideConfiguration(displayId),
1287 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001288 // Deferring resume here because we're going to launch new activity shortly.
1289 // We don't want to perform a redundant launch of the same record while ensuring
1290 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001291 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1292 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 }
1294
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001295 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001296 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001297 }
1298
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 r.app = app;
1300 app.waitingToKill = null;
1301 r.launchCount++;
1302 r.lastLaunchTime = SystemClock.uptimeMillis();
1303
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001304 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001305
1306 int idx = app.activities.indexOf(r);
1307 if (idx < 0) {
1308 app.activities.add(r);
1309 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001310 mService.updateLruProcessLocked(app, true, null);
1311 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001312
Craig Mautner15df08a2015-04-01 12:17:18 -07001313 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001314 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1315 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001316 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001317 }
1318
Andrii Kulian02b7a832016-10-06 23:11:56 -07001319 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001320 try {
1321 if (app.thread == null) {
1322 throw new RemoteException();
1323 }
1324 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001325 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001327 // We don't need to deliver new intents and/or set results if activity is going
1328 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 results = r.results;
1330 newIntents = r.newIntents;
1331 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001332 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1333 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1334 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001335 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1336 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001337 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001338 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001339 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001340 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001341 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1342 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 r.sleeping = false;
1344 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001345 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001346 mService.showAskCompatModeDialogLocked(r);
1347 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001348 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001349 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1350 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1351 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001352 final String profileFile = mService.mProfileFile;
1353 if (profileFile != null) {
1354 ParcelFileDescriptor profileFd = mService.mProfileFd;
1355 if (profileFd != null) {
1356 try {
1357 profileFd = profileFd.dup();
1358 } catch (IOException e) {
1359 if (profileFd != null) {
1360 try {
1361 profileFd.close();
1362 } catch (IOException o) {
1363 }
1364 profileFd = null;
1365 }
1366 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001367 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001368
1369 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001370 mService.mSamplingInterval, mService.mAutoStopProfiler,
1371 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 }
1373 }
1374 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001375
Andrii Kulianada10292017-02-09 23:19:29 -08001376 app.hasShownUi = true;
1377 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001378 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001379 // Because we could be starting an Activity in the system process this may not go across
1380 // a Binder interface which would create a new Configuration. Consequently we have to
1381 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001382
1383 final Configuration globalConfiguration =
1384 new Configuration(mService.getGlobalConfiguration());
1385 r.setLastReportedGlobalConfiguration(globalConfiguration);
1386 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001387 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001388 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1389
Craig Mautner2420ead2013-04-01 17:13:20 -07001390 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001391 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001392 globalConfiguration,
1393 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001394 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1395 r.persistentState, results, newIntents, !andResume,
1396 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001397
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001398 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 // This may be a heavy-weight process! Note that the package
1400 // manager will ensure that only activity can run in the main
1401 // process of the .apk, which is the only thing that will be
1402 // considered heavy-weight.
1403 if (app.processName.equals(app.info.packageName)) {
1404 if (mService.mHeavyWeightProcess != null
1405 && mService.mHeavyWeightProcess != app) {
1406 Slog.w(TAG, "Starting new heavy weight process " + app
1407 + " when already running "
1408 + mService.mHeavyWeightProcess);
1409 }
1410 mService.mHeavyWeightProcess = app;
1411 Message msg = mService.mHandler.obtainMessage(
1412 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1413 msg.obj = r;
1414 mService.mHandler.sendMessage(msg);
1415 }
1416 }
1417
1418 } catch (RemoteException e) {
1419 if (r.launchFailed) {
1420 // This is the second time we failed -- finish activity
1421 // and give up.
1422 Slog.e(TAG, "Second failure launching "
1423 + r.intent.getComponent().flattenToShortString()
1424 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001425 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1427 "2nd-crash", false);
1428 return false;
1429 }
1430
1431 // This is the first time we failed -- restart process and
1432 // retry.
1433 app.activities.remove(r);
1434 throw e;
1435 }
1436
1437 r.launchFailed = false;
1438 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001439 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001440 }
1441
1442 if (andResume) {
1443 // As part of the process of launching, ActivityThread also performs
1444 // a resume.
1445 stack.minimalResumeActivityLocked(r);
1446 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001447 // This activity is not starting in the resumed state... which should look like we asked
1448 // it to pause+stop (but remain visible), and it has done so and reported back the
1449 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001450 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001451 "Moving to PAUSED: " + r + " (starting in paused state)");
1452 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 }
1454
1455 // Launch the new version setup screen if needed. We do this -after-
1456 // launching the initial activity (that is, home), so that it can have
1457 // a chance to initialize itself while in the background, making the
1458 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001459 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001460 mService.startSetupActivityLocked();
1461 }
1462
Dianne Hackborn465fa392014-09-14 14:21:18 -07001463 // Update any services we are bound to that might care about whether
1464 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001465 if (r.app != null) {
1466 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1467 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001468
Craig Mautner2420ead2013-04-01 17:13:20 -07001469 return true;
1470 }
1471
Craig Mautnere79d42682013-04-01 19:01:53 -07001472 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001473 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001474 // Is this activity's application already running?
1475 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001476 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001477
Andrii Kulian02b7a832016-10-06 23:11:56 -07001478 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001479
1480 if (app != null && app.thread != null) {
1481 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001482 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1483 || !"android".equals(r.info.packageName)) {
1484 // Don't add this if it is a platform component that is marked
1485 // to run in multiple processes, because this is actually
1486 // part of the framework so doesn't make sense to track as a
1487 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001488 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1489 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001490 }
George Mount2c92c972014-03-20 09:38:23 -07001491 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001492 return;
1493 } catch (RemoteException e) {
1494 Slog.w(TAG, "Exception when starting activity "
1495 + r.intent.getComponent().flattenToShortString(), e);
1496 }
1497
1498 // If a dead object exception was thrown -- fall through to
1499 // restart the application.
1500 }
1501
1502 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001503 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001504 }
1505
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001506 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001507 String resultWho, int requestCode, int callingPid, int callingUid,
1508 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001509 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001510 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1511 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001512 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001513 return true;
1514 }
1515 final int componentRestriction = getComponentRestrictionForCallingPackage(
1516 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1517 final int actionRestriction = getActionRestrictionForCallingPackage(
1518 intent.getAction(), callingPackage, callingPid, callingUid);
1519 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1520 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1521 if (resultRecord != null) {
1522 resultStack.sendActivityResultLocked(-1,
1523 resultRecord, resultWho, requestCode,
1524 Activity.RESULT_CANCELED, null);
1525 }
1526 final String msg;
1527 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1528 msg = "Permission Denial: starting " + intent.toString()
1529 + " from " + callerApp + " (pid=" + callingPid
1530 + ", uid=" + callingUid + ")" + " with revoked permission "
1531 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1532 } else if (!aInfo.exported) {
1533 msg = "Permission Denial: starting " + intent.toString()
1534 + " from " + callerApp + " (pid=" + callingPid
1535 + ", uid=" + callingUid + ")"
1536 + " not exported from uid " + aInfo.applicationInfo.uid;
1537 } else {
1538 msg = "Permission Denial: starting " + intent.toString()
1539 + " from " + callerApp + " (pid=" + callingPid
1540 + ", uid=" + callingUid + ")"
1541 + " requires " + aInfo.permission;
1542 }
1543 Slog.w(TAG, msg);
1544 throw new SecurityException(msg);
1545 }
1546
1547 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1548 final String message = "Appop Denial: starting " + intent.toString()
1549 + " from " + callerApp + " (pid=" + callingPid
1550 + ", uid=" + callingUid + ")"
1551 + " requires " + AppOpsManager.permissionToOp(
1552 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1553 Slog.w(TAG, message);
1554 return false;
1555 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1556 final String message = "Appop Denial: starting " + intent.toString()
1557 + " from " + callerApp + " (pid=" + callingPid
1558 + ", uid=" + callingUid + ")"
1559 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1560 Slog.w(TAG, message);
1561 return false;
1562 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001563 if (options != null) {
1564 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1565 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1566 callingPid, callingUid);
1567 if (startInTaskPerm != PERMISSION_GRANTED) {
1568 final String msg = "Permission Denial: starting " + intent.toString()
1569 + " from " + callerApp + " (pid=" + callingPid
1570 + ", uid=" + callingUid + ") with launchTaskId="
1571 + options.getLaunchTaskId();
1572 Slog.w(TAG, msg);
1573 throw new SecurityException(msg);
1574 }
1575 }
1576 // Check if someone tries to launch an activity on a private display with a different
1577 // owner.
1578 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001579 if (launchDisplayId != INVALID_DISPLAY
1580 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1581 final String msg = "Permission Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ") with launchDisplayId="
1584 + launchDisplayId;
1585 Slog.w(TAG, msg);
1586 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001587 }
1588 }
1589
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001590 return true;
1591 }
1592
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001593 /** Check if caller is allowed to launch activities on specified display. */
1594 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1595 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1596 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1597
1598 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1599 if (activityDisplay == null) {
1600 Slog.w(TAG, "Launch on display check: display not found");
1601 return false;
1602 }
1603
1604 if (!activityDisplay.isPrivate()) {
1605 // Anyone can launch on a public display.
1606 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1607 + " allow launch on public display");
1608 return true;
1609 }
1610
1611 // Check if the caller is the owner of the display.
1612 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1613 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1614 + " allow launch for owner of the display");
1615 return true;
1616 }
1617
1618 // Check if caller is present on display
1619 if (activityDisplay.isUidPresent(callingUid)) {
1620 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1621 + " allow launch for caller present on the display");
1622 return true;
1623 }
1624
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001625 // Check if the caller can manage activity stacks.
1626 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001627 callingUid);
1628 if (startAnyPerm == PERMISSION_GRANTED) {
1629 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1630 + " allow launch any on display");
1631 return true;
1632 }
1633
1634 Slog.w(TAG, "Launch on display check: denied");
1635 return false;
1636 }
1637
1638 /** Update lists of UIDs that are present on displays and have access to them. */
1639 void updateUIDsPresentOnDisplay() {
1640 mDisplayAccessUIDs.clear();
1641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1642 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001643 // Only bother calculating the whitelist for private displays
1644 if (activityDisplay.isPrivate()) {
1645 mDisplayAccessUIDs.append(
1646 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1647 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001648 }
1649 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1650 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1651 }
1652
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001653 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001654 final long identity = Binder.clearCallingIdentity();
1655 try {
1656 return UserManager.get(mService.mContext).getUserInfo(userId);
1657 } finally {
1658 Binder.restoreCallingIdentity(identity);
1659 }
1660 }
1661
Svet Ganov99b60432015-06-27 13:15:22 -07001662 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001663 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001664 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1665 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001666 == PackageManager.PERMISSION_DENIED) {
1667 return ACTIVITY_RESTRICTION_PERMISSION;
1668 }
1669
Christopher Tateff7add02015-08-17 10:23:22 -07001670 if (activityInfo.permission == null) {
1671 return ACTIVITY_RESTRICTION_NONE;
1672 }
1673
Svet Ganov99b60432015-06-27 13:15:22 -07001674 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1675 if (opCode == AppOpsManager.OP_NONE) {
1676 return ACTIVITY_RESTRICTION_NONE;
1677 }
1678
1679 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1680 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001681 if (!ignoreTargetSecurity) {
1682 return ACTIVITY_RESTRICTION_APPOP;
1683 }
Svet Ganov99b60432015-06-27 13:15:22 -07001684 }
1685
1686 return ACTIVITY_RESTRICTION_NONE;
1687 }
1688
Svetoslav7008b512015-06-24 18:47:07 -07001689 private int getActionRestrictionForCallingPackage(String action,
1690 String callingPackage, int callingPid, int callingUid) {
1691 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001692 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001693 }
1694
1695 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1696 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001697 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001698 }
1699
1700 final PackageInfo packageInfo;
1701 try {
1702 packageInfo = mService.mContext.getPackageManager()
1703 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1704 } catch (PackageManager.NameNotFoundException e) {
1705 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001706 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001707 }
1708
1709 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001710 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001711 }
1712
1713 if (mService.checkPermission(permission, callingPid, callingUid) ==
1714 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001715 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001716 }
1717
1718 final int opCode = AppOpsManager.permissionToOpCode(permission);
1719 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001721 }
1722
1723 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1724 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001729 }
1730
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001731 void setLaunchSource(int uid) {
1732 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1733 }
1734
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001735 void acquireLaunchWakelock() {
1736 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1737 throw new IllegalStateException("Calling must be system uid");
1738 }
1739 mLaunchingActivity.acquire();
1740 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1741 // To be safe, don't allow the wake lock to be held for too long.
1742 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1743 }
1744 }
1745
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001746 /**
1747 * Called when the frontmost task is idle.
1748 * @return the state of mService.mBooting before this was called.
1749 */
1750 private boolean checkFinishBootingLocked() {
1751 final boolean booting = mService.mBooting;
1752 boolean enableScreen = false;
1753 mService.mBooting = false;
1754 if (!mService.mBooted) {
1755 mService.mBooted = true;
1756 enableScreen = true;
1757 }
1758 if (booting || enableScreen) {
1759 mService.postFinishBooting(booting, enableScreen);
1760 }
1761 return booting;
1762 }
1763
Craig Mautnerf3333272013-04-22 10:55:53 -07001764 // Checked.
1765 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001766 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001767 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001768
Craig Mautnerf3333272013-04-22 10:55:53 -07001769 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001770 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001771 int NS = 0;
1772 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001773 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001774 boolean activityRemoved = false;
1775
Wale Ogunwale7d701172015-03-11 15:36:30 -07001776 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001777 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001778 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1779 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001780 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1781 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001782 if (fromTimeout) {
1783 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001784 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001785
1786 // This is a hack to semi-deal with a race condition
1787 // in the client where it can be constructed with a
1788 // newer configuration from when we asked it to launch.
1789 // We'll update with whatever configuration it now says
1790 // it used to launch.
1791 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001792 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001793 }
1794
1795 // We are now idle. If someone is waiting for a thumbnail from
1796 // us, we can now deliver.
1797 r.idle = true;
1798
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001799 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001800 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001801 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001802 }
1803 }
1804
1805 if (allResumedActivitiesIdle()) {
1806 if (r != null) {
1807 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001808 }
1809
1810 if (mLaunchingActivity.isHeld()) {
1811 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1812 if (VALIDATE_WAKE_LOCK_CALLER &&
1813 Binder.getCallingUid() != Process.myUid()) {
1814 throw new IllegalStateException("Calling must be system uid");
1815 }
1816 mLaunchingActivity.release();
1817 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001818 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001819 }
1820
1821 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001822 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1823 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001825 if ((NF = mFinishingActivities.size()) > 0) {
1826 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 mFinishingActivities.clear();
1828 }
1829
Craig Mautnerf3333272013-04-22 10:55:53 -07001830 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001831 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 mStartingUsers.clear();
1833 }
1834
Craig Mautnerf3333272013-04-22 10:55:53 -07001835 // Stop any activities that are scheduled to do so but have been
1836 // waiting for the next one to start.
1837 for (int i = 0; i < NS; i++) {
1838 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001839 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001840 if (stack != null) {
1841 if (r.finishing) {
1842 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1843 } else {
1844 stack.stopActivityLocked(r);
1845 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001846 }
1847 }
1848
1849 // Finish any activities that are scheduled to do so but have been
1850 // waiting for the next one to start.
1851 for (int i = 0; i < NF; i++) {
1852 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001853 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001854 if (stack != null) {
1855 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1856 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001857 }
1858
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001859 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001860 // Complete user switch
1861 if (startingUsers != null) {
1862 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001863 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001864 }
1865 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001866 }
1867
1868 mService.trimApplications();
1869 //dump();
1870 //mWindowManager.dump();
1871
Craig Mautnerf3333272013-04-22 10:55:53 -07001872 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001873 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001874 }
1875
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001876 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001877 }
1878
Craig Mautner8e569572013-10-11 17:36:59 -07001879 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001880 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001881 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1882 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001883 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1884 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1885 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001886 }
Craig Mautner19091252013-10-05 00:03:53 -07001887 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001888 }
1889
1890 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1892 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001893 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1894 stacks.get(stackNdx).closeSystemDialogsLocked();
1895 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001896 }
1897 }
1898
Craig Mautner93529a42013-10-04 15:03:13 -07001899 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001900 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001901 }
1902
Craig Mautner8d341ef2013-03-26 09:03:27 -07001903 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001904 * Update the last used stack id for non-current user (current user's last
1905 * used stack is the focused stack)
1906 */
1907 void updateUserStackLocked(int userId, ActivityStack stack) {
1908 if (userId != mCurrentUser) {
1909 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1910 }
1911 }
1912
1913 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001914 * @return true if some activity was finished (or would have finished if doit were true).
1915 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001916 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1917 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001918 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001919 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1920 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001921 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001922 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001923 if (stack.finishDisabledPackageActivitiesLocked(
1924 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001925 didSomething = true;
1926 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001927 }
1928 }
1929 return didSomething;
1930 }
1931
Dianne Hackborna413dc02013-07-12 12:02:55 -07001932 void updatePreviousProcessLocked(ActivityRecord r) {
1933 // Now that this process has stopped, we may want to consider
1934 // it to be the previous app to try to keep around in case
1935 // the user wants to return to it.
1936
1937 // First, found out what is currently the foreground app, so that
1938 // we don't blow away the previous app if this activity is being
1939 // hosted by the process that is actually still the foreground.
1940 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1944 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001945 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001946 if (stack.mResumedActivity != null) {
1947 fgApp = stack.mResumedActivity.app;
1948 } else if (stack.mPausingActivity != null) {
1949 fgApp = stack.mPausingActivity.app;
1950 }
1951 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001952 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001953 }
1954 }
1955
1956 // Now set this one as the previous process, only if that really
1957 // makes sense to.
1958 if (r.app != null && fgApp != null && r.app != fgApp
1959 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001960 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001961 mService.mPreviousProcess = r.app;
1962 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1963 }
1964 }
1965
Wale Ogunwaled046a012015-12-24 13:05:59 -08001966 boolean resumeFocusedStackTopActivityLocked() {
1967 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07001968 }
1969
Wale Ogunwaled046a012015-12-24 13:05:59 -08001970 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08001971 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001972 if (targetStack != null && isFocusedStack(targetStack)) {
1973 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07001974 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07001975 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
1976 if (r == null || r.state != RESUMED) {
1977 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08001978 } else if (r.state == RESUMED) {
1979 // Kick off any lingering app transitions form the MoveTaskToFront operation.
1980 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07001981 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08001982 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001983 }
1984
Todd Kennedy39bfee52016-02-24 10:28:21 -08001985 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1986 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1987 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1988 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1989 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
1990 }
1991 }
1992 }
1993
Adrian Roos20d7df32016-01-12 18:59:43 +01001994 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
1995 TaskRecord finishedTask = null;
1996 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08001997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1998 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001999 final int numStacks = stacks.size();
2000 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2001 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002002 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2003 if (stack == focusedStack || finishedTask == null) {
2004 finishedTask = t;
2005 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002006 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002007 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002008 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002009 }
2010
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002011 void finishVoiceTask(IVoiceInteractionSession session) {
2012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2014 final int numStacks = stacks.size();
2015 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2016 final ActivityStack stack = stacks.get(stackNdx);
2017 stack.finishVoiceTask(session);
2018 }
2019 }
2020 }
2021
Andrii Kulianc27916642016-04-12 17:59:27 -07002022 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2023 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002024 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2025 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002027 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2028 // Caller wants the home activity moved with it. To accomplish this,
2029 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002030 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002031 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002032 ActivityStack currentStack = task.getStack();
2033 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002034 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2035 + task + " to front. Stack is null");
2036 return;
2037 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002038
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002039 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002040 int stackId = options.getLaunchStackId();
2041 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002042 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002043 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002044 if (stackId == INVALID_STACK_ID) {
2045 stackId = task.getLaunchStackId();
2046 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002047 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002048 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2049 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002050 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002051 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2052 // still need moveTaskToFrontLocked() below for any transition settings.
2053 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002054 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2055 resizeStackLocked(stackId, bounds,
2056 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002057 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002058 } else {
2059 // WM resizeTask must be done after the task is moved to the correct stack,
2060 // because Task's setBounds() also updates dim layer's bounds, but that has
2061 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002062 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002063 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002064 }
2065 }
2066
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002067 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002068 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002069 r == null ? null : r.appTimeTracker, reason);
2070
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002071 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002072 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002073
Andrii Kulian02b7a832016-10-06 23:11:56 -07002074 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002075 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002076 }
2077
Wale Ogunwale854809c2015-12-27 16:18:19 -08002078 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002079 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002080 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002081 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002082 return false;
2083 }
2084 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2085 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002086 }
2087
Winson Chung55893332017-02-17 17:13:10 -08002088 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002089 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002090 }
2091
Winson Chung55893332017-02-17 17:13:10 -08002092 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2093 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002094 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002095 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002096 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002097 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002098 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002099 return null;
2100 }
Matthew Ng330757d2017-02-28 14:19:17 -08002101 if (stackId == DOCKED_STACK_ID) {
2102 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2103 // the other side of the docked stack and we make visibility decisions based on that.
2104 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2105 }
Winson Chung55893332017-02-17 17:13:10 -08002106 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002107 }
2108
Andrii Kulian16802aa2016-11-02 12:21:33 -07002109 /**
2110 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2111 * If there is no such stack, new dynamic stack can be created.
2112 * @param displayId Target display.
2113 * @param r Activity that should be launched there.
2114 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2115 */
2116 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2117 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2118 if (activityDisplay == null) {
2119 throw new IllegalArgumentException(
2120 "Display with displayId=" + displayId + " not found.");
2121 }
2122
2123 // Return the topmost valid stack on the display.
2124 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2125 final ActivityStack stack = activityDisplay.mStacks.get(i);
2126 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2127 return stack;
2128 }
2129 }
2130
2131 // If there is no valid stack on the external display - check if new dynamic stack will do.
2132 if (displayId != Display.DEFAULT_DISPLAY) {
2133 final int newDynamicStackId = getNextStackId();
2134 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2135 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2136 }
2137 }
2138
2139 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2140 return null;
2141 }
2142
Craig Mautner967212c2013-04-13 21:10:58 -07002143 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002144 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002145 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2146 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002147 }
2148 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002149 }
2150
Jorim Jaggife762342016-10-13 14:33:27 +02002151 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2152 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2153 }
2154
Andrii Kulian7fc22812016-12-28 13:04:11 -08002155 /**
2156 * Get next focusable stack in the system. This will search across displays and stacks
2157 * in last-focused order for a focusable and visible stack, different from the target stack.
2158 *
2159 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2160 * searching for next candidate.
2161 * @return Next focusable {@link ActivityStack}, null if not found.
2162 */
2163 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2164 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2165
2166 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2167 final int displayId = mTmpOrderedDisplayIds.get(i);
2168 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2169 if (stacks == null) {
2170 continue;
2171 }
2172 for (int j = stacks.size() - 1; j >= 0; --j) {
2173 final ActivityStack stack = stacks.get(j);
2174 if (stack != currentFocus && stack.isFocusable()
2175 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
2176 return stack;
2177 }
2178 }
2179 }
2180
2181 return null;
2182 }
2183
Craig Mautneree2e45a2014-06-27 12:10:03 -07002184 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002185 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002186 }
2187
2188 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002189 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2190 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2191 final TaskRecord task = tasks.get(taskNdx);
2192 if (task.isHomeTask()) {
2193 final ArrayList<ActivityRecord> activities = task.mActivities;
2194 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2195 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002196 if (r.isHomeActivity()
2197 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002198 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002199 }
2200 }
2201 }
2202 }
2203 return null;
2204 }
2205
Chong Zhangb15758a2015-11-17 12:12:03 -08002206 /**
2207 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2208 * the docked stack itself, or if it's side-by-side to the docked stack.
2209 */
2210 boolean isStackDockedInEffect(int stackId) {
2211 return stackId == DOCKED_STACK_ID ||
2212 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2213 }
2214
Todd Kennedyca4d8422015-01-15 15:19:22 -08002215 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002216 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002217 ActivityContainer activityContainer =
2218 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002219 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002220 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2221 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002222 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002223 return activityContainer;
2224 }
2225
Craig Mautner34b73df2014-01-12 21:11:08 -08002226 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002227 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2228 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2229 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002230 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2231 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002232 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002233 }
2234 }
2235
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002236 void deleteActivityContainerRecord(int stackId) {
2237 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2238 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2239 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002240 }
2241
Jorim Jaggidc249c42015-12-15 14:57:31 -08002242 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002243 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002244 if (stackId == DOCKED_STACK_ID) {
2245 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002246 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002247 return;
2248 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002249 final ActivityStack stack = getStack(stackId);
2250 if (stack == null) {
2251 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2252 return;
2253 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002254
Jorim Jaggidc249c42015-12-15 14:57:31 -08002255 if (!allowResizeInDockedMode && getStack(DOCKED_STACK_ID) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002256 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2257 // stack size changing so things don't get out of sync.
2258 return;
2259 }
2260
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002261 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002262 mWindowManager.deferSurfaceLayout();
2263 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002264 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002265 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002266 stack.ensureVisibleActivitiesConfigurationLocked(
2267 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002268 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002269 } finally {
2270 mWindowManager.continueSurfaceLayout();
2271 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002272 }
2273 }
2274
Jorim Jaggi192086e2016-03-11 17:17:03 +01002275 void deferUpdateBounds(int stackId) {
2276 final ActivityStack stack = getStack(stackId);
2277 if (stack != null) {
2278 stack.deferUpdateBounds();
2279 }
2280 }
2281
2282 void continueUpdateBounds(int stackId) {
2283 final ActivityStack stack = getStack(stackId);
2284 if (stack != null) {
2285 stack.continueUpdateBounds();
2286 }
2287 }
2288
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002289 void notifyAppTransitionDone() {
2290 continueUpdateBounds(HOME_STACK_ID);
2291 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2292 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002293 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002294 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002295 if (task != null) {
2296 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002297 }
2298 }
2299 mResizingTasksDuringAnimation.clear();
2300 }
2301
Robert Carre7cc44d2017-03-20 19:04:30 -07002302 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2303 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002304
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002305 final ActivityStack stack = getStack(fromStackId);
2306 if (stack == null) {
2307 return;
2308 }
2309
2310 mWindowManager.deferSurfaceLayout();
2311 try {
2312 if (fromStackId == DOCKED_STACK_ID) {
2313
2314 // We are moving all tasks from the docked stack to the fullscreen stack,
2315 // which is dismissing the docked stack, so resize all other stacks to
2316 // fullscreen here already so we don't end up with resize trashing.
2317 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2318 if (StackId.isResizeableByDockedStack(i)) {
2319 ActivityStack otherStack = getStack(i);
2320 if (otherStack != null) {
2321 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2322 true /* allowResizeInDockedMode */, DEFER_RESUME);
2323 }
2324 }
2325 }
2326
2327 // Also disable docked stack resizing since we have manually adjusted the
2328 // size of other stacks above and we don't want to trigger a docked stack
2329 // resize when we remove task from it below and it is detached from the
2330 // display because it no longer contains any tasks.
2331 mAllowDockedStackResize = false;
2332 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002333 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002334 final boolean isFullscreenStackVisible = fullscreenStack != null &&
2335 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002336 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2337 final int size = tasks.size();
2338 if (onTop) {
2339 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002340 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002341 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002342 if (fromStackId == PINNED_STACK_ID) {
2343 // Update the return-to to reflect where the pinned stack task was moved
2344 // from so that we retain the stack that was previously visible if the
2345 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002346 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2347 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung14fbe142016-12-19 16:18:24 -08002348 MetricsLogger.action(mService.mContext,
2349 MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Winson Chung0c1ca092016-11-10 17:40:34 -08002350 }
Winson Chung74666102017-02-22 17:49:24 -08002351 // Defer resume until all the tasks have been moved to the fullscreen stack
2352 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2353 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
2354 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002355 }
2356 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002357 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002358 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002359 final int position = fullscreenStack != null
2360 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2361 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002362 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung74666102017-02-22 17:49:24 -08002363 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE,
2364 DEFER_RESUME, "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002365 }
2366 }
Winson Chung74666102017-02-22 17:49:24 -08002367
2368 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2369 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002370 } finally {
2371 mAllowDockedStackResize = true;
2372 mWindowManager.continueSurfaceLayout();
2373 }
2374 }
2375
Robert Carr6914f082017-03-20 19:04:30 -07002376 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002377 mWindowManager.inSurfaceTransaction(
2378 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002379 }
2380
Jorim Jaggidc249c42015-12-15 14:57:31 -08002381 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002382 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2383 boolean preserveWindows) {
2384 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2385 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2386 false /* deferResume */);
2387 }
2388
2389 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2390 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2391 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002392
2393 if (!mAllowDockedStackResize) {
2394 // Docked stack resize currently disabled.
2395 return;
2396 }
2397
Jorim Jaggidc249c42015-12-15 14:57:31 -08002398 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2399 if (stack == null) {
2400 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2401 return;
2402 }
2403
2404 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2405 mWindowManager.deferSurfaceLayout();
2406 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002407 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2408 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002409 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002410 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002411
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002412 // TODO: Checking for isAttached might not be needed as if the user passes in null
2413 // dockedBounds then they want the docked stack to be dismissed.
2414 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2415 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002416 // In this case we make all other static stacks fullscreen and move all
2417 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002418 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002419
2420 // stack shouldn't contain anymore activities, so nothing to resume.
2421 r = null;
2422 } else {
2423 // Docked stacks occupy a dedicated region on screen so the size of all other
2424 // static stacks need to be adjusted so they don't overlap with the docked stack.
2425 // We get the bounds to use from window manager which has been adjusted for any
2426 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002427 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002428 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002429 final ActivityStack current = getStack(i);
2430 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002431 current.getStackDockedModeBounds(
2432 tempOtherTaskBounds /* currentTempTaskBounds */,
2433 tempRect /* outStackBounds */,
2434 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2435
Matthew Nge15352e2016-12-20 15:36:29 -08002436 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002437 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2438 tempOtherTaskInsetBounds, preserveWindows,
2439 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002440 }
2441 }
2442 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002443 if (!deferResume) {
2444 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2445 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002446 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002447 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002448 mWindowManager.continueSurfaceLayout();
2449 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2450 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002451 }
2452
Robert Carr0d00c2e2016-02-29 17:45:02 -08002453 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2454 final ActivityStack stack = getStack(PINNED_STACK_ID);
2455 if (stack == null) {
2456 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2457 return;
2458 }
2459 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2460 mWindowManager.deferSurfaceLayout();
2461 try {
2462 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002463 Rect insetBounds = null;
2464 if (tempPinnedTaskBounds != null) {
2465 // We always use 0,0 as the position for the inset rect because
2466 // if we are getting insets at all in the pinned stack it must mean
2467 // we are headed for fullscreen.
2468 insetBounds = tempRect;
2469 insetBounds.top = 0;
2470 insetBounds.left = 0;
2471 insetBounds.right = tempPinnedTaskBounds.width();
2472 insetBounds.bottom = tempPinnedTaskBounds.height();
2473 }
2474 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002475 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002476 } finally {
2477 mWindowManager.continueSurfaceLayout();
2478 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2479 }
2480 }
2481
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002482 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002483 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002484 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002485 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 }
2487
Wale Ogunwale1666e312016-12-16 11:27:18 -08002488 final ActivityContainer activityContainer =
2489 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002490 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002491 }
2492
Robert Carr6914f082017-03-20 19:04:30 -07002493
Robert Carre7cc44d2017-03-20 19:04:30 -07002494 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002495 final ActivityStack stack = getStack(stackId);
2496 if (stack == null) {
2497 return;
2498 }
2499
2500 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2501 if (stack.getStackId() == PINNED_STACK_ID) {
2502 final ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
2503 if (fullscreenStack != null) {
2504 final boolean isFullscreenStackVisible =
2505 fullscreenStack.getStackVisibilityLocked(null) == STACK_VISIBLE;
2506 for (int i = 0; i < tasks.size(); i++) {
2507 // Insert the task either at the top of the fullscreen stack if it is hidden,
2508 // or just under the top task if it is currently visible
2509 final int insertPosition = isFullscreenStackVisible
2510 ? Math.max(0, fullscreenStack.getChildCount() - 1)
2511 : fullscreenStack.getChildCount();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002512 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002513 // Defer resume until we remove all the tasks
2514 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, insertPosition,
2515 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME, "removeStack");
Winson Chung010927a2016-12-15 16:12:35 -08002516 }
2517 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
2518 resumeFocusedStackTopActivityLocked();
2519 } else {
2520 // If there is no fullscreen stack, then create the stack and move all the tasks
2521 // onto the stack
Winson Chung74666102017-02-22 17:49:24 -08002522 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002523 }
2524 } else {
2525 for (int i = tasks.size() - 1; i >= 0; i--) {
2526 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2527 REMOVE_FROM_RECENTS);
2528 }
2529 }
2530 }
2531
2532 /**
Robert Carr6914f082017-03-20 19:04:30 -07002533 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2534 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2535 * instead moved back onto the fullscreen stack.
2536 */
2537 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002538 mWindowManager.inSurfaceTransaction(
2539 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002540 }
2541
2542 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002543 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2544 */
2545 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2546 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2547 }
2548
2549 /**
Winson Chung010927a2016-12-15 16:12:35 -08002550 * Removes the task with the specified task id.
2551 *
2552 * @param taskId Identifier of the task to be removed.
2553 * @param killProcess Kill any process associated with the task if possible.
2554 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002555 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2556 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002557 * @return Returns true if the given task was found and removed.
2558 */
Winson Chung6954fc92017-03-24 16:22:12 -07002559 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2560 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002561 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2562 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002563 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002564 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002565 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2566 if (tr.isPersistable) {
2567 mService.notifyTaskPersisterLocked(null, true);
2568 }
2569 return true;
2570 }
2571 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2572 return false;
2573 }
2574
2575 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2576 if (removeFromRecents) {
2577 mRecentTasks.remove(tr);
2578 tr.removedFromRecents();
2579 }
2580 ComponentName component = tr.getBaseIntent().getComponent();
2581 if (component == null) {
2582 Slog.w(TAG, "No component for base intent of task: " + tr);
2583 return;
2584 }
2585
2586 // Find any running services associated with this app and stop if needed.
2587 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2588
2589 if (!killProcess) {
2590 return;
2591 }
2592
2593 // Determine if the process(es) for this task should be killed.
2594 final String pkg = component.getPackageName();
2595 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2596 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2597 for (int i = 0; i < pmap.size(); i++) {
2598
2599 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2600 for (int j = 0; j < uids.size(); j++) {
2601 ProcessRecord proc = uids.valueAt(j);
2602 if (proc.userId != tr.userId) {
2603 // Don't kill process for a different user.
2604 continue;
2605 }
2606 if (proc == mService.mHomeProcess) {
2607 // Don't kill the home process along with tasks from the same package.
2608 continue;
2609 }
2610 if (!proc.pkgList.containsKey(pkg)) {
2611 // Don't kill process that is not associated with this task.
2612 continue;
2613 }
2614
2615 for (int k = 0; k < proc.activities.size(); k++) {
2616 TaskRecord otherTask = proc.activities.get(k).task;
2617 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2618 // Don't kill process(es) that has an activity in a different task that is
2619 // also in recents.
2620 return;
2621 }
2622 }
2623
2624 if (proc.foregroundServices) {
2625 // Don't kill process(es) with foreground service.
2626 return;
2627 }
2628
2629 // Add process to kill list.
2630 procsToKill.add(proc);
2631 }
2632 }
2633
2634 // Kill the running processes.
2635 for (int i = 0; i < procsToKill.size(); i++) {
2636 ProcessRecord pr = procsToKill.get(i);
2637 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2638 && pr.curReceivers.isEmpty()) {
2639 pr.kill("remove task", true);
2640 } else {
2641 // We delay killing processes that are not in the background or running a receiver.
2642 pr.waitingToKill = "remove task";
2643 }
2644 }
2645 }
2646
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002648 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002649 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2650 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002651 break;
2652 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002653 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002654 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002655 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002656 }
2657
Chong Zhang5dcb2752015-08-18 13:50:26 -07002658 /**
2659 * Restores a recent task to a stack
2660 * @param task The recent task to be restored.
2661 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002662 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2663 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002664 * @return true if the task has been restored successfully.
2665 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002666 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002667 if (!StackId.isStaticStack(stackId)) {
2668 // If stack is not static (or stack id is invalid) - use the default one.
2669 // This means that tasks that were on external displays will be restored on the
2670 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002671 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002672 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002673 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2674 // Put it in the fullscreen stack.
2675 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002676 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002677
Andrii Kulian02b7a832016-10-06 23:11:56 -07002678 final ActivityStack currentStack = task.getStack();
2679 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002680 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002681 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002682 // Nothing else to do since it is already restored in the right stack.
2683 return true;
2684 }
2685 // Remove current stack association, so we can re-associate the task with the
2686 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002687 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002688 }
2689
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002690 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002691 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002692
2693 if (stack == null) {
2694 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002695 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2696 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002697 return false;
2698 }
2699
Wale Ogunwale72919d22016-12-08 18:58:50 -08002700 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002701 // TODO: move call for creation here and other place into Stack.addTask()
2702 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002703 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2704 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002705 final ArrayList<ActivityRecord> activities = task.mActivities;
2706 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002707 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002708 }
2709 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002710 }
2711
Wale Ogunwale040b4702015-08-06 18:10:50 -07002712 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002713 * Move stack with all its existing content to specified display.
2714 * @param stackId Id of stack to move.
2715 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002716 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002717 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002718 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002719 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2720 if (activityDisplay == null) {
2721 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2722 + displayId);
2723 }
2724 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2725 if (activityContainer != null) {
2726 if (activityContainer.isAttachedLocked()) {
2727 if (activityContainer.getDisplayId() == displayId) {
2728 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2729 + " to its current displayId=" + displayId);
2730 }
2731
Andrii Kulian250d6532017-02-08 23:30:45 -08002732 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002733 } else {
2734 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2735 + stackId + " is not attached to any display.");
2736 }
2737 } else {
2738 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2739 + stackId);
2740 }
2741
2742 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2743 !PRESERVE_WINDOWS);
2744 // TODO(multi-display): resize stacks properly if moved from split-screen.
2745 }
2746
2747 /**
Winson Chung74666102017-02-22 17:49:24 -08002748 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2749 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002750 */
Winson Chung74666102017-02-22 17:49:24 -08002751 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002752 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002753
Winson Chung74666102017-02-22 17:49:24 -08002754 // Check that we aren't reparenting to the same stack that the task is already in
2755 if (prevStack != null && prevStack.mStackId == stackId) {
2756 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2757 + " already in stackId=" + stackId);
2758 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002759 }
2760
Winson Chung74666102017-02-22 17:49:24 -08002761 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2762 // support
2763 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2764 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2765 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002766 }
2767
Winson Chung74666102017-02-22 17:49:24 -08002768 // Ensure that we aren't trying to move into a freeform stack without freeform
2769 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002770 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002771 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2772 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002773 }
2774
Winson Chung74666102017-02-22 17:49:24 -08002775 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2776 // used for split-screen mode and will cause things like the docked divider to show up. We
2777 // instead leave the task in its current stack or move it to the fullscreen stack if it
2778 // isn't currently in a stack.
2779 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2780 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2781 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2782 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002783 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002784
Winson Chung74666102017-02-22 17:49:24 -08002785 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2786 // example, a docked stack is created which will lead to the stack we are moving from being
2787 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002788 try {
Winson Chung74666102017-02-22 17:49:24 -08002789 task.mTemporarilyUnresizable = true;
2790 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002791 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002792 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002793 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002794 }
2795
Winson Chung08f81892017-03-02 15:40:51 -08002796 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002797 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2798 if (stack == null) {
2799 throw new IllegalArgumentException(
2800 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2801 }
2802
2803 final ActivityRecord r = stack.topRunningActivityLocked();
2804 if (r == null) {
2805 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2806 + " in stack=" + stack);
2807 return false;
2808 }
2809
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002810 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002811 Slog.w(TAG,
2812 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002813 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002814 return false;
2815 }
2816
Winson Chung08f81892017-03-02 15:40:51 -08002817 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2818 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002819 return true;
2820 }
2821
Winson Chung08f81892017-03-02 15:40:51 -08002822 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2823 boolean moveHomeStackToFront, String reason) {
2824
Wale Ogunwale480dca02016-02-06 13:58:29 -08002825 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002826
Wale Ogunwale1666e312016-12-16 11:27:18 -08002827 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002828 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002829
Wale Ogunwale480dca02016-02-06 13:58:29 -08002830 try {
2831 final TaskRecord task = r.task;
2832
Andrii Kulian02b7a832016-10-06 23:11:56 -07002833 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002834 // An activity can't be pinned and visible behind at the same time. Go ahead and
2835 // release it from been visible behind before pinning.
2836 requestVisibleBehindLocked(r, false);
2837 }
2838
Wale Ogunwale480dca02016-02-06 13:58:29 -08002839 // Resize the pinned stack to match the current size of the task the activity we are
2840 // going to be moving is currently contained in. We do this to have the right starting
2841 // animation bounds for the pinned stack to the desired bounds the caller wants.
2842 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2843 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002844 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002845
2846 if (task.mActivities.size() == 1) {
2847 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002848 // the stack without re-parenting the activity in a different task. We don't
2849 // move the home stack forward if we are currently entering picture-in-picture
2850 // while pausing because that changes the focused stack and may prevent the new
2851 // starting activity from resuming.
2852 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002853 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002854 // Move the home stack forward if the task we just moved to the pinned stack
2855 // was launched from home so home should be visible behind it.
2856 moveHomeStackToFront(reason);
2857 }
Winson Chung74666102017-02-22 17:49:24 -08002858 // Defer resume until below
2859 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2860 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002861 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002862 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002863 // reveal/leave the other activities in their original task.
2864
2865 // Currently, we don't support reparenting activities across tasks in two different
2866 // stacks, so instead, just create a new task in the same stack, reparent the
2867 // activity into that task, and then reparent the whole task to the new stack. This
2868 // ensures that all the necessary work to migrate states in the old and new stacks
2869 // is also done.
2870 final TaskRecord newTask = task.getStack().createTaskRecord(
2871 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2872 r.mActivityType);
2873 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2874
2875 // Defer resume until below
2876 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
2877 DEFER_RESUME, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002878 }
Winson Chungc2baac02017-01-11 13:34:47 -08002879
2880 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2881 // to the pinned stack
2882 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002883 } finally {
2884 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002885 }
2886
Wale Ogunwale480dca02016-02-06 13:58:29 -08002887 // The task might have already been running and its visibility needs to be synchronized
2888 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002889 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002890 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002891
Winson Chung08f81892017-03-02 15:40:51 -08002892 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002893 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002894 }
2895
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002896 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002897 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2898 if (r == null || !r.isFocusable()) {
2899 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2900 "moveActivityStackToFront: unfocusable r=" + r);
2901 return false;
2902 }
2903
2904 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07002905 final ActivityStack stack = r.getStack();
2906 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002907 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2908 + r + " task=" + task);
2909 return false;
2910 }
2911
Chong Zhang6cda19c2016-06-14 19:07:56 -07002912 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2913 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2914 "moveActivityStackToFront: already on top, r=" + r);
2915 return false;
2916 }
2917
2918 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2919 "moveActivityStackToFront: r=" + r);
2920
2921 stack.moveToFront(reason, task);
2922 return true;
2923 }
2924
David Stevensc6b91c62017-02-08 14:23:58 -08002925 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002926 mTmpFindTaskResult.r = null;
2927 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002928 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002929 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002930 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2931 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002932 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2933 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002934 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002935 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002936 continue;
2937 }
2938 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002939 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2940 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 continue;
2942 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002943 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002944 // It is possible to have tasks in multiple stacks with the same root affinity, so
2945 // we should keep looking after finding an affinity match to see if there is a
2946 // better match in another stack. Also, task affinity isn't a good enough reason
2947 // to target a display which isn't the source of the intent, so skip any affinity
2948 // matches not on the specified display.
2949 if (mTmpFindTaskResult.r != null) {
2950 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2951 return mTmpFindTaskResult.r;
2952 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
2953 affinityMatch = mTmpFindTaskResult.r;
2954 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002955 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002956 }
2957 }
David Stevensc6b91c62017-02-08 14:23:58 -08002958 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
2959 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002960 }
2961
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002962 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
2963 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2965 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07002967 final ActivityRecord ar = stacks.get(stackNdx)
2968 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002969 if (ar != null) {
2970 return ar;
2971 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002972 }
2973 }
2974 return null;
2975 }
2976
Craig Mautner8d341ef2013-03-26 09:03:27 -07002977 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002978 scheduleSleepTimeout();
2979 if (!mGoingToSleep.isHeld()) {
2980 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002981 if (mLaunchingActivity.isHeld()) {
2982 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2983 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002984 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002985 mLaunchingActivity.release();
2986 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002989 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002990 }
2991
2992 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002993 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002994
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002995 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 final long endTime = System.currentTimeMillis() + timeout;
2997 while (true) {
2998 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002999 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3000 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003001 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3002 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3003 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003004 }
3005 if (cantShutdown) {
3006 long timeRemaining = endTime - System.currentTimeMillis();
3007 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003009 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003010 } catch (InterruptedException e) {
3011 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003012 } else {
3013 Slog.w(TAG, "Activity manager shutdown timed out");
3014 timedout = true;
3015 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003016 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003017 } else {
3018 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003019 }
3020 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003021
3022 // Force checkReadyForSleep to complete.
3023 mSleepTimeout = true;
3024 checkReadyForSleepLocked();
3025
Craig Mautner8d341ef2013-03-26 09:03:27 -07003026 return timedout;
3027 }
3028
3029 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030 removeSleepTimeouts();
3031 if (mGoingToSleep.isHeld()) {
3032 mGoingToSleep.release();
3033 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003034 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3035 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3037 final ActivityStack stack = stacks.get(stackNdx);
3038 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003039 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003040 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003041 }
Craig Mautner5314a402013-09-26 12:40:16 -07003042 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003043 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003044 mGoingToSleepActivities.clear();
3045 }
3046
3047 void activitySleptLocked(ActivityRecord r) {
3048 mGoingToSleepActivities.remove(r);
3049 checkReadyForSleepLocked();
3050 }
3051
3052 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003053 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003054 // Do not care.
3055 return;
3056 }
3057
3058 if (!mSleepTimeout) {
3059 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003060 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3061 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003062 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3063 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3064 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003065 }
3066
3067 if (mStoppingActivities.size() > 0) {
3068 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003069 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003070 + mStoppingActivities.size() + " activities");
3071 scheduleIdleLocked();
3072 dontSleep = true;
3073 }
3074
3075 if (mGoingToSleepActivities.size() > 0) {
3076 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003077 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 + mGoingToSleepActivities.size() + " activities");
3079 dontSleep = true;
3080 }
3081
3082 if (dontSleep) {
3083 return;
3084 }
3085 }
3086
Wei Wang65c7a152016-06-02 18:51:22 -07003087 // Send launch end powerhint before going sleep
3088 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3089
Craig Mautnere0a38842013-12-16 16:14:02 -08003090 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3091 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003092 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3093 stacks.get(stackNdx).goToSleep();
3094 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003095 }
3096
3097 removeSleepTimeouts();
3098
3099 if (mGoingToSleep.isHeld()) {
3100 mGoingToSleep.release();
3101 }
3102 if (mService.mShuttingDown) {
3103 mService.notifyAll();
3104 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 }
3106
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003107 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003108 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003109 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003110 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003111 }
3112 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003113 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003114 mWindowManager.executeAppTransition();
3115 return true;
3116 }
3117 return false;
3118 }
3119
Craig Mautner8d341ef2013-03-26 09:03:27 -07003120 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003121 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3122 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003123 int stackNdx = stacks.size() - 1;
3124 while (stackNdx >= 0) {
3125 stacks.get(stackNdx).handleAppCrashLocked(app);
3126 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003127 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003128 }
3129 }
3130
Jose Lima4b6c6692014-08-12 17:41:12 -07003131 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003132 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003133 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003134 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3135 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003136 return false;
3137 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003138
3139 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3140 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3141 + " visible=" + visible + " stackId=" + stack.mStackId
3142 + " can't contain visible behind activities");
3143 return false;
3144 }
3145
Jose Lima4b6c6692014-08-12 17:41:12 -07003146 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003147 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3148 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003149
3150 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003151 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003152 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003153 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003154 return true;
3155 }
3156
3157 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003158 if (visible && top.fullscreen) {
3159 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003160 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3161 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3162 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3163 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003164 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003165 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3166 // Only the activity set as currently visible behind should actively reset its
3167 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003168 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3169 "requestVisibleBehind: returning visible=" + visible
3170 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3171 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003172 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003173 }
3174
Jose Lima4b6c6692014-08-12 17:41:12 -07003175 stack.setVisibleBehindActivity(visible ? r : null);
3176 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003177 // If there is a translucent home activity, we need to force it stop being translucent,
3178 // because we can't depend on the application to necessarily perform that operation.
3179 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003180 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003181 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003182 mService.convertFromTranslucent(next.appToken);
3183 }
3184 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003185 if (top.app != null && top.app.thread != null) {
3186 // Notify the top app of the change.
3187 try {
3188 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3189 } catch (RemoteException e) {
3190 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003191 }
3192 return true;
3193 }
3194
Craig Mautnerbb742462014-07-07 15:28:55 -07003195 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003196 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Craig Mautnerbb742462014-07-07 15:28:55 -07003197 final TaskRecord task = r.task;
Andrii Kulian02b7a832016-10-06 23:11:56 -07003198 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003199
3200 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003201 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003202 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003203 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003204 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003205
3206 // When launching tasks behind, update the last active time of the top task after the new
3207 // task has been shown briefly
3208 final ActivityRecord top = stack.topActivity();
3209 if (top != null) {
3210 top.task.touchActiveTime();
3211 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003212 }
3213
3214 void scheduleLaunchTaskBehindComplete(IBinder token) {
3215 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3216 }
3217
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003218 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3219 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003220 mKeyguardController.beginActivityVisibilityUpdate();
3221 try {
3222 // First the front stacks. In case any are not fullscreen and are in front of home.
3223 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3224 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3225 final int topStackNdx = stacks.size() - 1;
3226 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3227 final ActivityStack stack = stacks.get(stackNdx);
3228 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3229 }
Craig Mautner580ea812013-04-25 12:58:38 -07003230 }
Jorim Jaggife762342016-10-13 14:33:27 +02003231 } finally {
3232 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003233 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003234 }
3235
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003236 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3237 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3238 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3239 final int topStackNdx = stacks.size() - 1;
3240 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3241 final ActivityStack stack = stacks.get(stackNdx);
3242 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3243 }
3244 }
3245 }
3246
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003247 void invalidateTaskLayers() {
3248 mTaskLayersChanged = true;
3249 }
3250
3251 void rankTaskLayersIfNeeded() {
3252 if (!mTaskLayersChanged) {
3253 return;
3254 }
3255 mTaskLayersChanged = false;
3256 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3257 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3258 int baseLayer = 0;
3259 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3260 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3261 }
3262 }
3263 }
3264
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003265 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3266 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3267 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3268 final int topStackNdx = stacks.size() - 1;
3269 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3270 final ActivityStack stack = stacks.get(stackNdx);
3271 stack.clearOtherAppTimeTrackers(except);
3272 }
3273 }
3274 }
3275
Craig Mautner8d341ef2013-03-26 09:03:27 -07003276 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003277 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3278 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003279 final int numStacks = stacks.size();
3280 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3281 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003282 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 }
3285 }
3286
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003287 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3288 // Examine all activities currently running in the process.
3289 TaskRecord firstTask = null;
3290 // Tasks is non-null only if two or more tasks are found.
3291 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003292 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3293 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003294 ActivityRecord r = app.activities.get(i);
3295 // First, if we find an activity that is in the process of being destroyed,
3296 // then we just aren't going to do anything for now; we want things to settle
3297 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003298 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003299 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003300 return;
3301 }
3302 // Don't consider any activies that are currently not in a state where they
3303 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003304 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3305 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003306 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003307 continue;
3308 }
3309 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003310 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003311 + " from " + r);
3312 if (firstTask == null) {
3313 firstTask = r.task;
3314 } else if (firstTask != r.task) {
3315 if (tasks == null) {
3316 tasks = new ArraySet<>();
3317 tasks.add(firstTask);
3318 }
3319 tasks.add(r.task);
3320 }
3321 }
3322 }
3323 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003324 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003325 return;
3326 }
3327 // If we have activities in multiple tasks that are in a position to be destroyed,
3328 // let's iterate through the tasks and release the oldest one.
3329 final int numDisplays = mActivityDisplays.size();
3330 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3331 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3332 // Step through all stacks starting from behind, to hit the oldest things first.
3333 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3334 final ActivityStack stack = stacks.get(stackNdx);
3335 // Try to release activities in this stack; if we manage to, we are done.
3336 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3337 return;
3338 }
3339 }
3340 }
3341 }
3342
Amith Yamasani37a40c22015-06-17 13:25:42 -07003343 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003344 final int focusStackId = mFocusedStack.getStackId();
3345 // We dismiss the docked stack whenever we switch users.
3346 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003347 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3348 // also cause all tasks to be moved to the fullscreen stack at a position that is
3349 // appropriate.
3350 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003351
3352 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003353 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003354 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003355
Craig Mautner858d8a62013-04-23 17:08:34 -07003356 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003357 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3358 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003359 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003360 final ActivityStack stack = stacks.get(stackNdx);
3361 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003362 TaskRecord task = stack.topTask();
3363 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003364 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003365 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003367 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003368
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003369 ActivityStack stack = getStack(restoreStackId);
3370 if (stack == null) {
3371 stack = mHomeStack;
3372 }
3373 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003374 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003375 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003376 } else {
3377 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003378 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003379 }
Craig Mautner93529a42013-10-04 15:03:13 -07003380 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003381 }
3382
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003383 /** Checks whether the userid is a profile of the current user. */
3384 boolean isCurrentProfileLocked(int userId) {
3385 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003386 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003387 }
3388
Winson Chung4dabf232017-01-25 13:25:22 -08003389 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3390 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003391 ArrayList<ActivityRecord> stops = null;
3392
3393 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003394 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3395 ActivityRecord s = mStoppingActivities.get(activityNdx);
Andrii Kulianee056812016-09-21 15:34:45 -07003396 // TODO: Remove mWaitingVisibleActivities list and just remove activity from
3397 // mStoppingActivities when something else comes up.
Wale Ogunwale3c519302016-07-14 09:17:59 -07003398 boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003399 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003400 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3401 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003402 mWaitingVisibleActivities.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003403 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003404 if (s.finishing) {
3405 // If this activity is finishing, it is sitting on top of
3406 // everyone else but we now know it is no longer needed...
3407 // so get rid of it. Otherwise, we need to go through the
3408 // normal flow and hide it once we determine that it is
3409 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003410 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003411 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003412 }
3413 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003414 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003415 if (!processPausingActivities && s.state == PAUSING) {
3416 // Defer processing pausing activities in this iteration and reschedule
3417 // a delayed idle to reprocess it again
3418 removeTimeoutsForActivityLocked(idleActivity);
3419 scheduleIdleTimeoutLocked(idleActivity);
3420 continue;
3421 }
3422
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003423 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003424 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003425 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003426 }
3427 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003428 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003429 }
3430 }
3431
3432 return stops;
3433 }
3434
Craig Mautnercf910b02013-04-23 11:23:27 -07003435 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003436 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3437 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3438 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3439 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003440 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003441 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003442 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003443 if (r == null) Slog.e(TAG,
3444 "validateTop...: null top activity, stack=" + stack);
3445 else {
3446 final ActivityRecord pausing = stack.mPausingActivity;
3447 if (pausing != null && pausing == r) Slog.e(TAG,
3448 "validateTop...: top stack has pausing activity r=" + r
3449 + " state=" + state);
3450 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3451 "validateTop...: activity in front not resumed r=" + r
3452 + " state=" + state);
3453 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003454 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003455 final ActivityRecord resumed = stack.mResumedActivity;
3456 if (resumed != null && resumed == r) Slog.e(TAG,
3457 "validateTop...: back stack has resumed activity r=" + r
3458 + " state=" + state);
3459 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3460 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003461 }
3462 }
3463 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003464 }
3465
Craig Mautnere0570202015-05-13 13:06:11 -07003466 private String lockTaskModeToString() {
3467 switch (mLockTaskModeState) {
3468 case LOCK_TASK_MODE_LOCKED:
3469 return "LOCKED";
3470 case LOCK_TASK_MODE_PINNED:
3471 return "PINNED";
3472 case LOCK_TASK_MODE_NONE:
3473 return "NONE";
3474 default: return "unknown=" + mLockTaskModeState;
3475 }
3476 }
3477
Craig Mautner27084302013-03-25 08:05:25 -07003478 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003479 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003480 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003481 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003482 pw.print(prefix);
3483 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003484 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003485 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003486 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003487 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3488 if (packages.size() > 0) {
3489 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3490 for (int i = 0; i < packages.size(); ++i) {
3491 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3492 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3493 }
3494 }
3495 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3496 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003497 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003498
Winson43d1f262016-06-14 16:05:55 -07003499 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003500 * Dump all connected displays' configurations.
3501 * @param prefix Prefix to apply to each line of the dump.
3502 */
3503 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3504 pw.print(prefix); pw.println("Display override configurations:");
3505 final int displayCount = mActivityDisplays.size();
3506 for (int i = 0; i < displayCount; i++) {
3507 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3508 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3509 pw.println(activityDisplay.getOverrideConfiguration());
3510 }
3511 }
3512
3513 /**
Winson43d1f262016-06-14 16:05:55 -07003514 * Dumps the activities matching the given {@param name} in the either the focused stack
3515 * or all visible stacks if {@param dumpVisibleStacks} is true.
3516 */
Winson Chung6998bc42017-02-28 17:07:05 -08003517 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3518 boolean dumpFocusedStackOnly) {
3519 if (dumpFocusedStackOnly) {
3520 return mFocusedStack.getDumpActivitiesLocked(name);
3521 } else {
Winson43d1f262016-06-14 16:05:55 -07003522 ArrayList<ActivityRecord> activities = new ArrayList<>();
3523 int numDisplays = mActivityDisplays.size();
3524 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3525 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3526 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3527 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003528 if (!dumpVisibleStacksOnly ||
3529 stack.getStackVisibilityLocked(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003530 activities.addAll(stack.getDumpActivitiesLocked(name));
3531 }
3532 }
3533 }
3534 return activities;
Winson43d1f262016-06-14 16:05:55 -07003535 }
Craig Mautner20e72272013-04-01 13:45:53 -07003536 }
3537
Dianne Hackborn390517b2013-05-30 15:03:32 -07003538 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3539 boolean needSep, String prefix) {
3540 if (activity != null) {
3541 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3542 if (needSep) {
3543 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003544 }
3545 pw.print(prefix);
3546 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003547 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003548 }
3549 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003550 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003551 }
3552
Craig Mautner8d341ef2013-03-26 09:03:27 -07003553 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3554 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003555 boolean printed = false;
3556 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003557 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3558 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003559 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003560 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003561 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003562 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003563 final ActivityStack stack = stacks.get(stackNdx);
3564 StringBuilder stackHeader = new StringBuilder(128);
3565 stackHeader.append(" Stack #");
3566 stackHeader.append(stack.mStackId);
3567 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003568 stackHeader.append("\n");
3569 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3570 stackHeader.append("\n");
3571 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003572
3573 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3574 dumpClient, dumpPackage, needSep, stackHeader.toString());
3575 printed |= printedStackHeader;
3576 if (!printedStackHeader) {
3577 // Ensure we always dump the stack header even if there are no activities
3578 pw.println();
3579 pw.println(stackHeader);
3580 }
3581
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3583 !dumpAll, false, dumpPackage, true,
3584 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003585
Craig Mautner4a1cb222013-12-04 16:14:06 -08003586 needSep = printed;
3587 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3588 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003589 if (pr) {
3590 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003591 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003592 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003593 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3594 " mResumedActivity: ");
3595 if (pr) {
3596 printed = true;
3597 needSep = false;
3598 }
3599 if (dumpAll) {
3600 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3601 " mLastPausedActivity: ");
3602 if (pr) {
3603 printed = true;
3604 needSep = true;
3605 }
3606 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3607 needSep, " mLastNoHistoryActivity: ");
3608 }
3609 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003610 }
3611 }
3612
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003613 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3614 false, dumpPackage, true, " Activities waiting to finish:", null);
3615 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3616 false, dumpPackage, true, " Activities waiting to stop:", null);
3617 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3618 false, dumpPackage, true, " Activities waiting for another to become visible:",
3619 null);
3620 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3621 false, dumpPackage, true, " Activities waiting to sleep:", null);
3622 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3623 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003624
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003625 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003626 }
3627
Dianne Hackborn390517b2013-05-30 15:03:32 -07003628 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003630 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003631 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003632 String innerPrefix = null;
3633 String[] args = null;
3634 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 for (int i=list.size()-1; i>=0; i--) {
3636 final ActivityRecord r = list.get(i);
3637 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3638 continue;
3639 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003640 if (innerPrefix == null) {
3641 innerPrefix = prefix + " ";
3642 args = new String[0];
3643 }
3644 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003645 final boolean full = !brief && (complete || !r.isInHistory());
3646 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003647 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003648 needNL = false;
3649 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003650 if (header1 != null) {
3651 pw.println(header1);
3652 header1 = null;
3653 }
3654 if (header2 != null) {
3655 pw.println(header2);
3656 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003657 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003658 if (lastTask != r.task) {
3659 lastTask = r.task;
3660 pw.print(prefix);
3661 pw.print(full ? "* " : " ");
3662 pw.println(lastTask);
3663 if (full) {
3664 lastTask.dump(pw, prefix + " ");
3665 } else if (complete) {
3666 // Complete + brief == give a summary. Isn't that obvious?!?
3667 if (lastTask.intent != null) {
3668 pw.print(prefix); pw.print(" ");
3669 pw.println(lastTask.intent.toInsecureStringWithClip());
3670 }
3671 }
3672 }
3673 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3674 pw.print(" #"); pw.print(i); pw.print(": ");
3675 pw.println(r);
3676 if (full) {
3677 r.dump(pw, innerPrefix);
3678 } else if (complete) {
3679 // Complete + brief == give a summary. Isn't that obvious?!?
3680 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3681 if (r.app != null) {
3682 pw.print(innerPrefix); pw.println(r.app);
3683 }
3684 }
3685 if (client && r.app != null && r.app.thread != null) {
3686 // flush anything that is already in the PrintWriter since the thread is going
3687 // to write to the file descriptor directly
3688 pw.flush();
3689 try {
3690 TransferPipe tp = new TransferPipe();
3691 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003692 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003693 // Short timeout, since blocking here can
3694 // deadlock with the application.
3695 tp.go(fd, 2000);
3696 } finally {
3697 tp.kill();
3698 }
3699 } catch (IOException e) {
3700 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3701 } catch (RemoteException e) {
3702 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3703 }
3704 needNL = true;
3705 }
3706 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003707 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003708 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003709
Craig Mautnerf3333272013-04-22 10:55:53 -07003710 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003711 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3712 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003713 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3714 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003715 }
3716
3717 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003718 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003719 }
3720
3721 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003722 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3723 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003724 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3725 }
3726
Craig Mautner05d29032013-05-03 13:40:13 -07003727 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003728 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3729 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3730 }
Craig Mautner05d29032013-05-03 13:40:13 -07003731 }
3732
Craig Mautner0eea92c2013-05-16 13:35:39 -07003733 void removeSleepTimeouts() {
3734 mSleepTimeout = false;
3735 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3736 }
3737
3738 final void scheduleSleepTimeout() {
3739 removeSleepTimeouts();
3740 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3741 }
3742
Craig Mautner4a1cb222013-12-04 16:14:06 -08003743 @Override
3744 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003745 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003746 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3747 }
3748
3749 @Override
3750 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003751 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3753 }
3754
3755 @Override
3756 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003757 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003758 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3759 }
3760
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003761 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003762 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003764 newDisplay = mActivityDisplays.get(displayId) == null;
3765 if (newDisplay) {
3766 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003767 if (activityDisplay.mDisplay == null) {
3768 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3769 return;
3770 }
Craig Mautner4504de52013-12-20 09:06:56 -08003771 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003772 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003773 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003774 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003776 }
3777
Andrii Kulianca007a62016-11-22 16:33:28 -08003778 /** Check if display with specified id is added to the list. */
3779 boolean isDisplayAdded(int displayId) {
3780 return mActivityDisplays.get(displayId) != null;
3781 }
3782
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003783 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003784 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003785 mService.mContext.getResources().getDimensionPixelSize(
3786 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003787 }
3788
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003789 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003790 if (displayId == DEFAULT_DISPLAY) {
3791 throw new IllegalArgumentException("Can't remove the primary display.");
3792 }
3793
Craig Mautner4a1cb222013-12-04 16:14:06 -08003794 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003795 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3796 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003797 final boolean destroyContentOnRemoval
3798 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003799 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3800 while (!stacks.isEmpty()) {
3801 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003802 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003803 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3804 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003805 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003806 } else {
3807 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3808 // a valid launch stack for all activities. This way the task history from
3809 // external display will be preserved on primary after move.
3810 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003811 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003813 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003814 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003815 }
3816 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 }
3818
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003819 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003820 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003821 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3822 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003823 // TODO: Update the bounds.
3824 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003825 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003827 }
3828
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003829 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003830 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003832 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003833 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003835 info.userId = stack.mCurrentUser;
Winsond46b7272016-04-20 11:54:27 -07003836 info.visible = stack.getStackVisibilityLocked(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003837 info.position = display != null
3838 ? display.mStacks.indexOf(stack)
3839 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003840
3841 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3842 final int numTasks = tasks.size();
3843 int[] taskIds = new int[numTasks];
3844 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003845 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003846 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003847 for (int i = 0; i < numTasks; ++i) {
3848 final TaskRecord task = tasks.get(i);
3849 taskIds[i] = task.taskId;
3850 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3851 : task.realActivity != null ? task.realActivity.flattenToString()
3852 : task.getTopActivity() != null ? task.getTopActivity().packageName
3853 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003854 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003855 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003856 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003857 }
3858 info.taskIds = taskIds;
3859 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003860 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003861 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003862
3863 final ActivityRecord top = stack.topRunningActivityLocked();
3864 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003865 return info;
3866 }
3867
3868 StackInfo getStackInfoLocked(int stackId) {
3869 ActivityStack stack = getStack(stackId);
3870 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003871 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 }
3873 return null;
3874 }
3875
3876 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003877 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003878 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3879 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003881 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003882 }
3883 }
3884 return list;
3885 }
3886
Craig Mautner15df08a2015-04-01 12:17:18 -07003887 TaskRecord getLockedTaskLocked() {
3888 final int top = mLockTaskModeTasks.size() - 1;
3889 if (top >= 0) {
3890 return mLockTaskModeTasks.get(top);
3891 }
3892 return null;
3893 }
3894
3895 boolean isLockedTask(TaskRecord task) {
3896 return mLockTaskModeTasks.contains(task);
3897 }
3898
3899 boolean isLastLockedTask(TaskRecord task) {
3900 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3901 }
3902
3903 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003904 if (!mLockTaskModeTasks.remove(task)) {
3905 return;
3906 }
3907 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3908 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003909 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003910 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3911 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003912 final Message lockTaskMsg = Message.obtain();
3913 lockTaskMsg.arg1 = task.userId;
3914 lockTaskMsg.what = LOCK_TASK_END_MSG;
3915 mHandler.sendMessage(lockTaskMsg);
3916 }
3917 }
3918
Andrii Kulianc27916642016-04-12 17:59:27 -07003919 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3920 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3921 false /* forceNonResizable */);
3922 }
3923
Jorim Jaggid53f0922016-04-06 22:16:23 -07003924 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003925 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003926 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3927 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003928 return;
3929 }
3930
Jorim Jaggicd13d332016-04-27 15:40:20 -07003931 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003932 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003933 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003934 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003935
Andrii Kulianc27916642016-04-12 17:59:27 -07003936 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3937 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003938 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003939 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003940 && !topActivity.noDisplay) {
3941 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003942 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3943 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003944 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003945 }
3946
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003947 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07003948 if (mLockTaskNotify != null) {
3949 mLockTaskNotify.showToast(mLockTaskModeState);
3950 }
Jason Monka8f569c2014-07-07 12:15:21 -04003951 }
3952
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003953 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3954 if (mLockTaskModeTasks.contains(task)) {
3955 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3956 }
3957 }
3958
Craig Mautner432f64e2015-05-20 14:59:57 -07003959 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3960 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003961 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003962 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 final TaskRecord lockedTask = getLockedTaskLocked();
3964 if (lockedTask != null) {
3965 removeLockedTaskLocked(lockedTask);
3966 if (!mLockTaskModeTasks.isEmpty()) {
3967 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003968 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3969 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003970 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003971 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07003972 return;
3973 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003974 }
Craig Mautnere0570202015-05-13 13:06:11 -07003975 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3976 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003977 return;
3978 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003979
3980 // Should have already been checked, but do it again.
3981 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003982 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3983 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003984 return;
3985 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003986 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003987 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003988 return;
3989 }
3990
3991 if (mLockTaskModeTasks.isEmpty()) {
3992 // First locktask.
3993 final Message lockTaskMsg = Message.obtain();
3994 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3995 lockTaskMsg.arg1 = task.userId;
3996 lockTaskMsg.what = LOCK_TASK_START_MSG;
3997 lockTaskMsg.arg2 = lockTaskModeState;
3998 mHandler.sendMessage(lockTaskMsg);
3999 }
4000 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004001 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4002 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004003 mLockTaskModeTasks.remove(task);
4004 mLockTaskModeTasks.add(task);
4005
4006 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004007 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004008 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004009
4010 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004011 findTaskToMoveToFrontLocked(task, 0, null, reason,
4012 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004013 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004014 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004015 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004016 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004017 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004018 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004019 }
4020
4021 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004022 return isLockTaskModeViolation(task, false);
4023 }
4024
4025 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4026 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004027 return false;
4028 }
4029 final int lockTaskAuth = task.mLockTaskAuth;
4030 switch (lockTaskAuth) {
4031 case LOCK_TASK_AUTH_DONT_LOCK:
4032 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004033 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004034 case LOCK_TASK_AUTH_LAUNCHABLE:
4035 case LOCK_TASK_AUTH_WHITELISTED:
4036 return false;
4037 case LOCK_TASK_AUTH_PINNABLE:
4038 // Pinnable tasks can't be launched on top of locktask tasks.
4039 return !mLockTaskModeTasks.isEmpty();
4040 default:
4041 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4042 return true;
4043 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004044 }
4045
Craig Mautner15df08a2015-04-01 12:17:18 -07004046 void onLockTaskPackagesUpdatedLocked() {
4047 boolean didSomething = false;
4048 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4049 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004050 final boolean wasWhitelisted =
4051 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4052 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004053 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004054 final boolean isWhitelisted =
4055 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4056 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4057 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004058 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004059 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4060 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004061 removeLockedTaskLocked(lockedTask);
4062 lockedTask.performClearTaskLocked();
4063 didSomething = true;
4064 }
4065 }
4066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4067 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4068 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4069 final ActivityStack stack = stacks.get(stackNdx);
4070 stack.onLockTaskPackagesUpdatedLocked();
4071 }
4072 }
Craig Mautnere0570202015-05-13 13:06:11 -07004073 final ActivityRecord r = topRunningActivityLocked();
4074 final TaskRecord task = r != null ? r.task : null;
4075 if (mLockTaskModeTasks.isEmpty() && task != null
4076 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4077 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004078 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4079 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4080 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4081 false);
4082 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004083 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004084 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004085 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004086 }
4087 }
4088
Benjamin Franz43261142015-02-11 15:59:44 +00004089 int getLockTaskModeState() {
4090 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004091 }
4092
Jorim Jaggife89d122015-12-22 16:28:44 +01004093 void activityRelaunchedLocked(IBinder token) {
4094 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004095 if (mService.isSleepingOrShuttingDownLocked()) {
4096 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4097 if (r != null) {
4098 r.setSleeping(true, true);
4099 }
4100 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004101 }
4102
4103 void activityRelaunchingLocked(ActivityRecord r) {
4104 mWindowManager.notifyAppRelaunching(r.appToken);
4105 }
4106
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004107 void logStackState() {
4108 mActivityMetricsLogger.logWindowState();
4109 }
4110
Andrii Kulian933076d2016-03-29 17:04:42 -07004111 void scheduleReportMultiWindowModeChanged(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004112 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4113 final ActivityRecord r = task.mActivities.get(i);
4114 if (r.app != null && r.app.thread != null) {
4115 mMultiWindowModeChangedActivities.add(r);
4116 }
4117 }
4118
4119 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4120 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4121 }
4122 }
4123
Andrii Kulian933076d2016-03-29 17:04:42 -07004124 void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004125 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004126 if (prevStack == null || prevStack == stack
4127 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4128 return;
4129 }
4130
4131 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4132 final ActivityRecord r = task.mActivities.get(i);
4133 if (r.app != null && r.app.thread != null) {
4134 mPipModeChangedActivities.add(r);
4135 }
4136 }
4137
4138 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4139 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4140 }
4141 }
4142
Tony Mak853304c2016-04-18 15:17:41 +01004143 void setDockedStackMinimized(boolean minimized) {
4144 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004145 }
4146
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004147 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004148
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004149 public ActivityStackSupervisorHandler(Looper looper) {
4150 super(looper);
4151 }
4152
Winson Chung4dabf232017-01-25 13:25:22 -08004153 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004154 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004155 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4156 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004157 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004158 }
4159
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004160 @Override
4161 public void handleMessage(Message msg) {
4162 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004163 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4164 synchronized (mService) {
4165 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4166 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004167 r.scheduleMultiWindowModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004168 }
4169 }
4170 } break;
4171 case REPORT_PIP_MODE_CHANGED_MSG: {
4172 synchronized (mService) {
4173 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4174 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Andrii Kulian933076d2016-03-29 17:04:42 -07004175 r.schedulePictureInPictureModeChanged();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004176 }
4177 }
4178 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004179 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004180 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4181 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004182 if (mService.mDidDexOpt) {
4183 mService.mDidDexOpt = false;
4184 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4185 nmsg.obj = msg.obj;
4186 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4187 return;
4188 }
4189 // We don't at this point know if the activity is fullscreen,
4190 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004191 activityIdleInternal((ActivityRecord) msg.obj,
4192 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004193 } break;
4194 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004195 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004196 activityIdleInternal((ActivityRecord) msg.obj,
4197 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004198 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004199 case RESUME_TOP_ACTIVITY_MSG: {
4200 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004201 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004202 }
4203 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004204 case SLEEP_TIMEOUT_MSG: {
4205 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004206 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004207 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4208 mSleepTimeout = true;
4209 checkReadyForSleepLocked();
4210 }
4211 }
4212 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004213 case LAUNCH_TIMEOUT_MSG: {
4214 if (mService.mDidDexOpt) {
4215 mService.mDidDexOpt = false;
4216 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4217 return;
4218 }
4219 synchronized (mService) {
4220 if (mLaunchingActivity.isHeld()) {
4221 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4222 if (VALIDATE_WAKE_LOCK_CALLER
4223 && Binder.getCallingUid() != Process.myUid()) {
4224 throw new IllegalStateException("Calling must be system uid");
4225 }
4226 mLaunchingActivity.release();
4227 }
4228 }
4229 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004231 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 } break;
4233 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004234 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004235 } break;
4236 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004237 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004238 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004239 case CONTAINER_CALLBACK_VISIBILITY: {
4240 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004241 final IActivityContainerCallback callback = container.mCallback;
4242 if (callback != null) {
4243 try {
4244 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4245 } catch (RemoteException e) {
4246 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004247 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004248 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004249 case LOCK_TASK_START_MSG: {
4250 // When lock task starts, we disable the status bars.
4251 try {
Jason Monk62515be2014-05-21 16:06:19 -04004252 if (mLockTaskNotify == null) {
4253 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004254 }
Jason Monk62515be2014-05-21 16:06:19 -04004255 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004256 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004257 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004258 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004259 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004260 flags = StatusBarManager.DISABLE_MASK
4261 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004262 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004263 flags = StatusBarManager.DISABLE_MASK
4264 & (~StatusBarManager.DISABLE_BACK)
4265 & (~StatusBarManager.DISABLE_HOME)
4266 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004267 }
4268 getStatusBarService().disable(flags, mToken,
4269 mService.mContext.getPackageName());
4270 }
4271 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004272 if (getDevicePolicyManager() != null) {
4273 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004274 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004275 }
justinzhang5286d3f2014-05-12 17:06:01 -04004276 } catch (RemoteException ex) {
4277 throw new RuntimeException(ex);
4278 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004279 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004280 case LOCK_TASK_END_MSG: {
4281 // When lock task ends, we enable the status bars.
4282 try {
Jason Monk62515be2014-05-21 16:06:19 -04004283 if (getStatusBarService() != null) {
4284 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4285 mService.mContext.getPackageName());
4286 }
4287 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004288 if (getDevicePolicyManager() != null) {
4289 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4290 msg.arg1);
4291 }
Jason Monk62515be2014-05-21 16:06:19 -04004292 if (mLockTaskNotify == null) {
4293 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4294 }
4295 mLockTaskNotify.show(false);
4296 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004297 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004298 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004299 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004301 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004302 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004303 new LockPatternUtils(mService.mContext)
4304 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004305 }
4306 } catch (SettingNotFoundException e) {
4307 // No setting, don't lock.
4308 }
justinzhang5286d3f2014-05-12 17:06:01 -04004309 } catch (RemoteException ex) {
4310 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004311 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004312 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004313 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004314 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004315 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4316 if (mLockTaskNotify == null) {
4317 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4318 }
4319 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4320 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004321 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4322 final ActivityContainer container = (ActivityContainer) msg.obj;
4323 final IActivityContainerCallback callback = container.mCallback;
4324 if (callback != null) {
4325 try {
4326 callback.onAllActivitiesComplete(container.asBinder());
4327 } catch (RemoteException e) {
4328 }
4329 }
4330 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004331 case LAUNCH_TASK_BEHIND_COMPLETE: {
4332 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004333 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004334 if (r != null) {
4335 handleLaunchTaskBehindCompleteLocked(r);
4336 }
4337 }
4338 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004340 }
4341 }
4342 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004343
Ying Wangb081a592014-04-22 15:20:16 -07004344 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004345 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4346 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004348 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004349 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004350 ActivityRecord mParentActivity = null;
4351 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004352
Craig Mautnere3a00d72014-04-16 08:31:19 -07004353 boolean mVisible = true;
4354
Craig Mautner4a1cb222013-12-04 16:14:06 -08004355 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004356 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004357
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004358 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4359 final static int CONTAINER_STATE_NO_SURFACE = 1;
4360 final static int CONTAINER_STATE_FINISHING = 2;
4361 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4362
Wale Ogunwale1666e312016-12-16 11:27:18 -08004363 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004364 synchronized (mService) {
4365 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004366 mActivityDisplay = activityDisplay;
Winson Chung55893332017-02-17 17:13:10 -08004367 switch (mStackId) {
4368 case PINNED_STACK_ID:
4369 new PinnedActivityStack(this, mRecentTasks, onTop);
4370 break;
4371 default:
4372 new ActivityStack(this, mRecentTasks, onTop);
4373 break;
4374 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004375 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004376 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004377 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004378 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004379
Andrii Kulian839def92016-11-02 10:58:58 -07004380 /**
4381 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004382 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004383 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004384 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004385 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004386 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004387 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004388 if (mActivityDisplay != null) {
4389 throw new IllegalStateException("ActivityContainer is already attached, " +
4390 "displayId=" + mActivityDisplay.mDisplayId);
4391 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004392 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004393 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004394 }
4395
4396 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004397 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004398 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004399 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4400 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004401 return;
4402 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004403 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004404 }
4405 }
4406
4407 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004408 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004409 synchronized (mService) {
4410 if (mActivityDisplay != null) {
4411 return mActivityDisplay.mDisplayId;
4412 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004413 }
4414 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004415 }
4416
Jeff Brownca9bc702014-02-11 14:32:56 -08004417 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004418 public int getStackId() {
4419 synchronized (mService) {
4420 return mStackId;
4421 }
4422 }
4423
4424 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004425 public boolean injectEvent(InputEvent event) {
4426 final long origId = Binder.clearCallingIdentity();
4427 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004428 synchronized (mService) {
4429 if (mActivityDisplay != null) {
4430 return mInputManagerInternal.injectInputEvent(event,
4431 mActivityDisplay.mDisplayId,
4432 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4433 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004434 }
4435 return false;
4436 } finally {
4437 Binder.restoreCallingIdentity(origId);
4438 }
4439 }
4440
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004441 @Override
4442 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004443 synchronized (mService) {
4444 if (mContainerState == CONTAINER_STATE_FINISHING) {
4445 return;
4446 }
4447 mContainerState = CONTAINER_STATE_FINISHING;
4448
Craig Mautnerd163e752014-06-13 17:18:47 -07004449 long origId = Binder.clearCallingIdentity();
4450 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004451 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004452 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004453 } finally {
4454 Binder.restoreCallingIdentity(origId);
4455 }
4456 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004457 }
4458
Andrii Kulian03c403d2016-11-11 11:14:12 -08004459 /**
4460 * Remove the stack completely. Must be called only when there are no tasks left in it,
4461 * as this method does not finish running activities.
4462 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004463 void removeLocked() {
4464 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004465 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004466 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004467 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004468 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004469 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 }
4471
Andrii Kulian839def92016-11-02 10:58:58 -07004472 /**
4473 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4474 * completely or re-parented.
4475 */
4476 private void removeFromDisplayLocked() {
4477 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4478 + " current displayId=" + mActivityDisplay.mDisplayId);
4479
Wale Ogunwale1666e312016-12-16 11:27:18 -08004480 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004481 mActivityDisplay = null;
4482 }
4483
4484 /**
4485 * Move the stack to specified display.
4486 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004487 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004488 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004489 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004490 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4491 + mActivityDisplay + " to display=" + activityDisplay
4492 + " Callers=" + Debug.getCallers(2));
4493
4494 removeFromDisplayLocked();
4495
4496 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004497 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004498 }
4499
Craig Mautner4a1cb222013-12-04 16:14:06 -08004500 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004501 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004502 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004503 }
4504
4505 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004506 public final int startActivityIntentSender(IIntentSender intentSender)
4507 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004508 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4509
4510 if (!(intentSender instanceof PendingIntentRecord)) {
4511 throw new IllegalArgumentException("Bad PendingIntent object");
4512 }
4513
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004514 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004515 Binder.getCallingUid(), mCurrentUser, false,
4516 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004517
4518 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4519 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4520 pendingIntent.key.requestResolvedType);
4521
4522 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4523 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4524 }
4525
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004526 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004527 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004528 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004529 throw new SecurityException(
4530 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4531 }
4532 }
4533
Craig Mautnerdf88d732014-01-27 09:21:32 -08004534 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004535 public IBinder asBinder() {
4536 return this;
4537 }
4538
Craig Mautner4504de52013-12-20 09:06:56 -08004539 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004540 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004541 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004542 }
4543
Craig Mautner4a1cb222013-12-04 16:14:06 -08004544 ActivityStackSupervisor getOuter() {
4545 return ActivityStackSupervisor.this;
4546 }
4547
Craig Mautnerd163e752014-06-13 17:18:47 -07004548 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004549 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004550 }
4551
Craig Mautner6985bad2014-04-21 15:22:06 -07004552 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004553 void setVisible(boolean visible) {
4554 if (mVisible != visible) {
4555 mVisible = visible;
4556 if (mCallback != null) {
4557 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4558 0 /* unused */, this).sendToTarget();
4559 }
4560 }
4561 }
4562
Craig Mautner6985bad2014-04-21 15:22:06 -07004563 void setDrawn() {
4564 }
4565
Craig Mautner1b4bf852014-05-26 15:06:32 -07004566 // You can always start a new task on a regular ActivityStack.
4567 boolean isEligibleForNewTasks() {
4568 return true;
4569 }
4570
Craig Mautnerd163e752014-06-13 17:18:47 -07004571 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004572 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004573 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004574 }
4575
Craig Mautner34b73df2014-01-12 21:11:08 -08004576 @Override
4577 public String toString() {
4578 return mIdString + (mActivityDisplay == null ? "N" : "A");
4579 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004580 }
4581
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004582 private class VirtualActivityContainer extends ActivityContainer {
4583 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004584 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004585
4586 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004587 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4588 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004589 mParentActivity = parent;
4590 mCallback = callback;
4591 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004592 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004593 }
4594
4595 @Override
4596 public void setSurface(Surface surface, int width, int height, int density) {
4597 super.setSurface(surface, width, height, density);
4598
4599 synchronized (mService) {
4600 final long origId = Binder.clearCallingIdentity();
4601 try {
4602 setSurfaceLocked(surface, width, height, density);
4603 } finally {
4604 Binder.restoreCallingIdentity(origId);
4605 }
4606 }
4607 }
4608
4609 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4610 if (mContainerState == CONTAINER_STATE_FINISHING) {
4611 return;
4612 }
4613 VirtualActivityDisplay virtualActivityDisplay =
4614 (VirtualActivityDisplay) mActivityDisplay;
4615 if (virtualActivityDisplay == null) {
4616 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004617 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004618 mActivityDisplay = virtualActivityDisplay;
4619 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004620 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004621 }
4622
4623 if (mSurface != null) {
4624 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004625 }
4626
Craig Mautner6985bad2014-04-21 15:22:06 -07004627 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004628 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004629 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004630 } else {
4631 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004632 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004633 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004634 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004635 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004637
Craig Mautnerd163e752014-06-13 17:18:47 -07004638 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004639
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004640 if (DEBUG_STACK) Slog.d(TAG_STACK,
4641 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004642 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004643
Craig Mautner6985bad2014-04-21 15:22:06 -07004644 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004645 boolean isAttachedLocked() {
4646 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004647 }
4648
4649 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004650 void setDrawn() {
4651 synchronized (mService) {
4652 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004653 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004654 }
4655 }
4656
Craig Mautner1b4bf852014-05-26 15:06:32 -07004657 // Never start a new task on an ActivityView if it isn't explicitly specified.
4658 @Override
4659 boolean isEligibleForNewTasks() {
4660 return false;
4661 }
4662
Craig Mautnerd163e752014-06-13 17:18:47 -07004663 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004664 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004665 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4666 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4667 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4668 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4669 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004670 }
4671 }
4672
Craig Mautner4a1cb222013-12-04 16:14:06 -08004673 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4674 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004675 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004676 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004677 int mDisplayId;
4678 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004679
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4681 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004682 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004683
Jose Lima4b6c6692014-08-12 17:41:12 -07004684 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004685
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004686 /** Array of all UIDs that are present on the display. */
4687 private IntArray mDisplayAccessUIDs = new IntArray();
4688
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004689 ActivityDisplay() {
4690 }
Craig Mautner4504de52013-12-20 09:06:56 -08004691
Craig Mautner1a70a162014-09-13 12:09:31 -07004692 // After instantiation, check that mDisplay is not null before using this. The alternative
4693 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004694 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004695 final Display display = mDisplayManager.getDisplay(displayId);
4696 if (display == null) {
4697 return;
4698 }
4699 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004700 }
4701
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004702 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004703 mDisplay = display;
4704 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004705 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004706
Winson Chung7c3ede32017-03-14 14:18:34 -07004707 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004708 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004709 + " to displayId=" + mDisplayId + " position=" + position);
4710 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004711 }
4712
Wale Ogunwale1666e312016-12-16 11:27:18 -08004713 void detachStack(ActivityStack stack) {
4714 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004715 + " from displayId=" + mDisplayId);
4716 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004717 }
4718
Jose Lima4b6c6692014-08-12 17:41:12 -07004719 void setVisibleBehindActivity(ActivityRecord r) {
4720 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004721 }
4722
Jose Lima4b6c6692014-08-12 17:41:12 -07004723 boolean hasVisibleBehindActivity() {
4724 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004725 }
4726
Craig Mautner34b73df2014-01-12 21:11:08 -08004727 @Override
4728 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004729 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4730 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004731
4732 @Override
4733 protected int getChildCount() {
4734 return mStacks.size();
4735 }
4736
4737 @Override
4738 protected ConfigurationContainer getChildAt(int index) {
4739 return mStacks.get(index);
4740 }
4741
4742 @Override
4743 protected ConfigurationContainer getParent() {
4744 return ActivityStackSupervisor.this;
4745 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004746
4747 boolean isPrivate() {
4748 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4749 }
4750
4751 boolean isUidPresent(int uid) {
4752 for (ActivityStack stack : mStacks) {
4753 if (stack.isUidPresent(uid)) {
4754 return true;
4755 }
4756 }
4757 return false;
4758 }
4759
4760 /** Update and get all UIDs that are present on the display and have access to it. */
4761 private IntArray getPresentUIDs() {
4762 mDisplayAccessUIDs.clear();
4763 for (ActivityStack stack : mStacks) {
4764 stack.getPresentUIDs(mDisplayAccessUIDs);
4765 }
4766 return mDisplayAccessUIDs;
4767 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004768
4769 boolean shouldDestroyContentOnRemove() {
4770 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4771 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004772 }
4773
4774 class VirtualActivityDisplay extends ActivityDisplay {
4775 VirtualDisplay mVirtualDisplay;
4776
Craig Mautner6985bad2014-04-21 15:22:06 -07004777 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004778 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004779 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4780 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4781 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4782 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004783
4784 init(mVirtualDisplay.getDisplay());
4785
Bryce Leeaf3a4002017-03-20 10:37:12 -07004786 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004787 }
4788
4789 void setSurface(Surface surface) {
4790 if (mVirtualDisplay != null) {
4791 mVirtualDisplay.setSurface(surface);
4792 }
4793 }
4794
4795 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004796 void detachStack(ActivityStack stack) {
4797 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004798 if (mVirtualDisplay != null) {
4799 mVirtualDisplay.release();
4800 mVirtualDisplay = null;
4801 }
4802 }
4803
4804 @Override
4805 public String toString() {
4806 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004807 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004808 }
Jose Lima58e66d62014-05-27 20:00:27 -07004809
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004810 ActivityStack findStackBehind(ActivityStack stack) {
4811 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004812 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004813 if (display == null) {
4814 return null;
4815 }
4816 final ArrayList<ActivityStack> stacks = display.mStacks;
4817 for (int i = stacks.size() - 1; i >= 0; i--) {
4818 if (stacks.get(i) == stack && i > 0) {
4819 return stacks.get(i - 1);
4820 }
4821 }
4822 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4823 + " in=" + stacks);
4824 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004825
Jorim Jaggic69bd222016-03-15 14:38:37 +01004826 /**
4827 * Puts a task into resizing mode during the next app transition.
4828 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004829 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004830 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004831 private void setResizingDuringAnimation(TaskRecord task) {
4832 mResizingTasksDuringAnimation.add(task.taskId);
4833 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004834 }
4835
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004836 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4837 final TaskRecord task;
4838 final int callingUid;
4839 final String callingPackage;
4840 final Intent intent;
4841 final int userId;
4842 final ActivityOptions activityOptions = (bOptions != null)
4843 ? new ActivityOptions(bOptions) : null;
4844 final int launchStackId = (activityOptions != null)
4845 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004846 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004847 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004848 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004849 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004850
4851 if (launchStackId == DOCKED_STACK_ID) {
4852 mWindowManager.setDockedStackCreateState(
4853 activityOptions.getDockCreateMode(), null /* initialBounds */);
4854
4855 // Defer updating the stack in which recents is until the app transition is done, to
4856 // not run into issues where we still need to draw the task in recents but the
4857 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004858 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004859 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4860 }
4861
Winson Chung7900bee2017-03-10 08:59:25 -08004862 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4863 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004864 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004865 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004866 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004867 throw new IllegalArgumentException(
4868 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4869 }
4870
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004871 // Since we don't have an actual source record here, we assume that the currently focused
4872 // activity was the source.
4873 final ActivityStack focusedStack = getFocusedStack();
4874 final ActivityRecord sourceRecord =
4875 focusedStack != null ? focusedStack.topActivity() : null;
4876
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004877 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004878 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004879 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4880 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004881 }
4882 }
4883
4884 // If the user must confirm credentials (e.g. when first launching a work app and the
4885 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4886 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4887 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004888 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004889 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004890 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004891 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4892 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004893
4894 // If we are launching the task in the docked stack, put it into resizing mode so
4895 // the window renders full-screen with the background filling the void. Also only
4896 // call this at the end to make sure that tasks exists on the window manager side.
4897 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004898 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004899 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004900
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004901 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004902 ActivityManager.START_TASK_TO_FRONT,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004903 sourceRecord != null ? sourceRecord.task.getStackId() : INVALID_STACK_ID,
4904 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004905 return ActivityManager.START_TASK_TO_FRONT;
4906 }
4907 callingUid = task.mCallingUid;
4908 callingPackage = task.mCallingPackage;
4909 intent = task.intent;
4910 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4911 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004912 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4913 null, null, 0, 0, bOptions, userId, null, task);
4914 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004915 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004916 }
4917 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004918 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004919
4920 /**
4921 * @return a list of activities which are the top ones in each visible stack. The first
4922 * entry will be the focused activity.
4923 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004924 List<IBinder> getTopVisibleActivities() {
4925 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4926 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004927 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004928 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4929 // Traverse all stacks on a display.
4930 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4931 final ActivityStack stack = display.mStacks.get(j);
4932 // Get top activity from a visible stack and add it to the list.
4933 if (stack.getStackVisibilityLocked(null /* starting */)
4934 == ActivityStack.STACK_VISIBLE) {
4935 final ActivityRecord top = stack.topActivity();
4936 if (top != null) {
4937 if (stack == mFocusedStack) {
4938 topActivityTokens.add(0, top.appToken);
4939 } else {
4940 topActivityTokens.add(top.appToken);
4941 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004942 }
4943 }
4944 }
4945 }
4946 return topActivityTokens;
4947 }
Craig Mautner27084302013-03-25 08:05:25 -07004948}