blob: 88de8a5fcbed38e4d4fb8d08308a265eb571b87b [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
Winson Chung47900652017-04-06 18:44:25 -070045import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBLE_BEHIND;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
72import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
73import static com.android.server.am.ActivityManagerService.ANIMATE;
74import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
75import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
76import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
86import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
87import static com.android.server.am.ActivityStack.STACK_VISIBLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
91import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
92import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080093import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
94import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
95import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020096import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080097import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020098
Svetoslav7008b512015-06-24 18:47:07 -070099import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800100import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700101import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100102import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800105import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700106import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800107import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700111import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700112import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700114import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400115import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700116import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800130import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800131import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800132import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800133import android.hardware.input.InputManager;
134import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100136import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700137import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700140import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700141import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700143import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700145import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400146import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700148import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700149import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700150import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100151import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700152import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700153import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400154import android.provider.Settings;
155import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700156import android.service.voice.IVoiceInteractionSession;
Bryce Lee4a194382017-04-04 14:32:48 -0700157import android.text.TextUtils;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700159import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700160import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800161import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700162import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800165import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800166import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800167import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800168
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800169import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800170import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700171import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400172import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700173import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400174import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700177import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700178
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179import java.io.FileDescriptor;
180import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700181import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800182import java.lang.annotation.Retention;
183import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700184import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700185import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700186import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700187import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700188
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800189public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800190 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700192 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700194 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700195 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800201 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800203
Craig Mautnerf3333272013-04-22 10:55:53 -0700204 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700206
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Craig Mautner05d29032013-05-03 13:40:13 -0700213 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
214 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
215 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700216 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800218 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
219 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
220 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700221 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400222 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
223 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700224 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700225 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700226 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800227 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
228 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800229
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700231
Jason Monk62515be2014-05-21 16:06:19 -0400232 private static final String LOCK_TASK_TAG = "Lock-to-App";
233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 // Used to indicate if an object (e.g. stack) that we are trying to get
235 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800236 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700238 // Used to indicate that windows of activities should be preserved during the resize.
239 static final boolean PRESERVE_WINDOWS = true;
240
Wale Ogunwale040b4702015-08-06 18:10:50 -0700241 // Used to indicate if an object (e.g. task) should be moved/created
242 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700243 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700244
245 // Used to indicate that an objects (e.g. task) removal from its container
246 // (e.g. stack) is due to it moving to another container.
247 static final boolean MOVING = true;
248
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700249 // Force the focus to change to the stack we are moving a task to..
250 static final boolean FORCE_FOCUS = true;
251
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700252 // Don't execute any calls to resume.
253 static final boolean DEFER_RESUME = true;
254
Winson Chung010927a2016-12-15 16:12:35 -0800255 // Used to indicate that a task is removed it should also be removed from recents.
256 static final boolean REMOVE_FROM_RECENTS = true;
257
Winson Chung6954fc92017-03-24 16:22:12 -0700258 // Used to indicate that pausing an activity should occur immediately without waiting for
259 // the activity callback indicating that it has completed pausing
260 static final boolean PAUSE_IMMEDIATELY = true;
261
Winson Chung7900bee2017-03-10 08:59:25 -0800262 /**
263 * The modes which affect which tasks are returned when calling
264 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
265 */
266 @Retention(RetentionPolicy.SOURCE)
267 @IntDef({
268 MATCH_TASK_IN_STACKS_ONLY,
269 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
270 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
271 })
272 public @interface AnyTaskForIdMatchTaskMode {}
273 // Match only tasks in the current stacks
274 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
275 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
276 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
277 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
278 // provided stack id
279 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
280
Svetoslav7008b512015-06-24 18:47:07 -0700281 // Activity actions an app cannot start if it uses a permission which is not granted.
282 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
283 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700284
Svetoslav7008b512015-06-24 18:47:07 -0700285 static {
286 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
287 Manifest.permission.CAMERA);
288 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
289 Manifest.permission.CAMERA);
290 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
291 Manifest.permission.CALL_PHONE);
292 }
293
Svet Ganov99b60432015-06-27 13:15:22 -0700294 /** Action restriction: launching the activity is not restricted. */
295 private static final int ACTIVITY_RESTRICTION_NONE = 0;
296 /** Action restriction: launching the activity is restricted by a permission. */
297 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
298 /** Action restriction: launching the activity is restricted by an app op. */
299 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700300
justinzhang5286d3f2014-05-12 17:06:01 -0400301 /** Status Bar Service **/
302 private IBinder mToken = new Binder();
303 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400304 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400305
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700306 // For debugging to make sure the caller when acquiring/releasing our
307 // wake lock is the system process.
308 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800309 /** The number of distinct task ids that can be assigned to the tasks of a single user */
310 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700311
Craig Mautner27084302013-03-25 08:05:25 -0700312 final ActivityManagerService mService;
313
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800314 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 final ActivityStackSupervisorHandler mHandler;
317
318 /** Short cut */
319 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700321
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700322 /** Counter for next free stack ID to use for dynamic activity stacks. */
323 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700324
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800325 /**
326 * Maps the task identifier that activities are currently being started in to the userId of the
327 * task. Each time a new task is created, the entry for the userId of the task is incremented
328 */
329 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Craig Mautner2420ead2013-04-01 17:13:20 -0700331 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800332 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700333
Craig Mautnere0a38842013-12-16 16:14:02 -0800334 /** The stack containing the launcher app. Assumed to always be attached to
335 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800336 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700337
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700340
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
342 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800343 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800344 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700345
346 /** List of activities that are waiting for a new activity to become visible before completing
347 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700348 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
349 // mStoppingActivities when something else comes up.
350 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
Bryce Lee4a194382017-04-04 14:32:48 -0700352 /** List of processes waiting to find out when a specific activity becomes visible. */
353 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
355 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700356 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700357
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358 /** List of activities that are ready to be stopped, but waiting for the next activity to
359 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800360 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361
Craig Mautnerf3333272013-04-22 10:55:53 -0700362 /** List of activities that are ready to be finished, but waiting for the previous activity to
363 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800367 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Wale Ogunwale22e25262016-02-01 10:32:02 -0800369 /** List of activities whose multi-window mode changed that we need to report to the
370 * application */
371 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
372
373 /** List of activities whose picture-in-picture mode changed that we need to report to the
374 * application */
375 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
376
Winson Chung5af42fc2017-03-24 17:11:33 -0700377 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
378 * the application */
379 Rect mPipModeChangedTargetStackBounds;
380
Craig Mautnerf3333272013-04-22 10:55:53 -0700381 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700382 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700383
Craig Mautnerde4ef022013-04-07 19:01:33 -0700384 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
385 * is being brought in front of us. */
386 boolean mUserLeaving = false;
387
Craig Mautner0eea92c2013-05-16 13:35:39 -0700388 /** Set when we have taken too long waiting to go to sleep. */
389 boolean mSleepTimeout = false;
390
391 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700392 * We don't want to allow the device to go to sleep while in the process
393 * of launching an activity. This is primarily to allow alarm intent
394 * receivers to launch an activity and get that to run before the device
395 * goes back to sleep.
396 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700397 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398
399 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700400 * Set when the system is going to sleep, until we have
401 * successfully paused the current activity and released our wake lock.
402 * At that point the system is allowed to actually sleep.
403 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700404 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700405
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700406 /** Stack id of the front stack when user switched, indexed by userId. */
407 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700408
Craig Mautner4504de52013-12-20 09:06:56 -0800409 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800411 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800412
Bryce Leeaf3a4002017-03-20 10:37:12 -0700413 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800414 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700415 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800417 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
418
419 private DisplayManagerInternal mDisplayManagerInternal;
420 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800421
Craig Mautner15df08a2015-04-01 12:17:18 -0700422 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
423 * may be finished until there is only one entry left. If this is empty the system is not
424 * in lockTask mode. */
425 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000426 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700427 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
428 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000429 */
430 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400431 /**
432 * Notifies the user when entering/exiting lock-task.
433 */
434 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800435
Wale Ogunwaled046a012015-12-24 13:05:59 -0800436 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800437 boolean inResumeTopActivity;
438
Andrii Kulian1e32e022016-09-16 15:29:34 -0700439 /**
440 * Temporary rect used during docked stack resize calculation so we don't need to create a new
441 * object each time.
442 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700443 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700444
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700445 // The default minimal size that will be used if the activity doesn't specify its minimal size.
446 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700447 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700448
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700449 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
450 private boolean mTaskLayersChanged = true;
451
Jorim Jaggi275561a2016-02-23 10:11:02 -0500452 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800453
Andrii Kulian1779e612016-10-12 21:58:25 -0700454 @Override
455 protected int getChildCount() {
456 return mActivityDisplays.size();
457 }
458
459 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700460 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700461 return mActivityDisplays.valueAt(index);
462 }
463
464 @Override
465 protected ConfigurationContainer getParent() {
466 return null;
467 }
468
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700469 Configuration getDisplayOverrideConfiguration(int displayId) {
470 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
471 if (activityDisplay == null) {
472 throw new IllegalArgumentException("No display found with id: " + displayId);
473 }
474
475 return activityDisplay.getOverrideConfiguration();
476 }
477
478 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
479 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
480 if (activityDisplay == null) {
481 throw new IllegalArgumentException("No display found with id: " + displayId);
482 }
483
484 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
485 }
486
Wale Ogunwale39381972015-12-17 17:15:29 -0800487 static class FindTaskResult {
488 ActivityRecord r;
489 boolean matchedByRootAffinity;
490 }
491 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
492
Craig Mautneree36c772014-07-16 14:56:05 -0700493 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800494 * Temp storage for display ids sorted in focus order.
495 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
496 * it's more efficient, as the number of displays is usually small.
497 */
498 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
499
500 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100501 * Used to keep track whether app visibilities got changed since the last pause. Useful to
502 * determine whether to invoke the task stack change listener after pausing.
503 */
504 boolean mAppVisibilitiesChangedSinceLastPause;
505
506 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100507 * Set of tasks that are in resizing mode during an app transition to fill the "void".
508 */
509 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
510
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700511
512 /**
513 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
514 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
515 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
516 * like the docked stack going empty.
517 */
518 private boolean mAllowDockedStackResize = true;
519
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100520 /**
Tony Mak853304c2016-04-18 15:17:41 +0100521 * Is dock currently minimized.
522 */
523 boolean mIsDockMinimized;
524
Jorim Jaggife762342016-10-13 14:33:27 +0200525 final KeyguardController mKeyguardController;
526
Tony Mak853304c2016-04-18 15:17:41 +0100527 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700528 * Description of a request to start a new activity, which has been held
529 * due to app switches being disabled.
530 */
531 static class PendingActivityLaunch {
532 final ActivityRecord r;
533 final ActivityRecord sourceRecord;
534 final int startFlags;
535 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700536 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700537
538 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700539 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700540 r = _r;
541 sourceRecord = _sourceRecord;
542 startFlags = _startFlags;
543 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700544 callerApp = _callerApp;
545 }
546
547 void sendErrorResult(String message) {
548 try {
549 if (callerApp.thread != null) {
550 callerApp.thread.scheduleCrash(message);
551 }
552 } catch (RemoteException e) {
553 Slog.e(TAG, "Exception scheduling crash of failed "
554 + "activity launcher sourceRecord=" + sourceRecord, e);
555 }
Craig Mautneree36c772014-07-16 14:56:05 -0700556 }
557 }
558
Bryce Leeaf691c02017-03-20 14:20:22 -0700559 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700560 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700561 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800562 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200563 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700564 }
565
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800566 void setRecentTasks(RecentTasks recentTasks) {
567 mRecentTasks = recentTasks;
568 }
569
Jeff Brown2c43c332014-06-12 22:38:59 -0700570 /**
571 * At the time when the constructor runs, the power manager has not yet been
572 * initialized. So we initialize our wakelocks afterwards.
573 */
574 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800575 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700576 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700577 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700578 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700579 }
580
justinzhang5286d3f2014-05-12 17:06:01 -0400581 // This function returns a IStatusBarService. The value is from ServiceManager.
582 // getService and is cached.
583 private IStatusBarService getStatusBarService() {
584 synchronized (mService) {
585 if (mStatusBarService == null) {
586 mStatusBarService = IStatusBarService.Stub.asInterface(
587 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
588 if (mStatusBarService == null) {
589 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
590 }
591 }
592 return mStatusBarService;
593 }
594 }
595
Jason Monk35c62a42014-06-17 10:24:47 -0400596 private IDevicePolicyManager getDevicePolicyManager() {
597 synchronized (mService) {
598 if (mDevicePolicyManager == null) {
599 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
600 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
601 if (mDevicePolicyManager == null) {
602 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
603 }
604 }
605 return mDevicePolicyManager;
606 }
607 }
608
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700609 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 synchronized (mService) {
611 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200612 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613
614 mDisplayManager =
615 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
616 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800617 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618
619 Display[] displays = mDisplayManager.getDisplays();
620 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
621 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700623 if (activityDisplay.mDisplay == null) {
624 throw new IllegalStateException("Default Display does not exist");
625 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800626 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700627 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800628 }
629
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800630 mHomeStack = mFocusedStack = mLastFocusedStack =
631 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800632
633 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 }
Craig Mautner27084302013-03-25 08:05:25 -0700635 }
636
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700637 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700639 }
640
Craig Mautnerde4ef022013-04-07 19:01:33 -0700641 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700643 }
644
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700645 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700646 if (stack == null) {
647 return false;
648 }
649
Craig Mautnerdf88d732014-01-27 09:21:32 -0800650 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
651 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700652 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800653 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800654 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700655 }
656
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800657 /** The top most stack on its display. */
658 boolean isFrontStackOnDisplay(ActivityStack stack) {
659 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
660 }
661
662 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700663 if (stack == null) {
664 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700666
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700667 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
668 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700669 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800670 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800671 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700672 }
673
Wale Ogunwaled046a012015-12-24 13:05:59 -0800674 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800675 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
676 if (!focusCandidate.isFocusable()) {
677 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800678 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800679 }
680
681 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800682 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800683 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800684
Wale Ogunwaled046a012015-12-24 13:05:59 -0800685 EventLogTags.writeAmFocusedStack(
686 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
687 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
688 }
689
690 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800691 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800692 if (r != null && r.idle) {
693 checkFinishBootingLocked();
694 }
695 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 }
697
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700698 void moveHomeStackToFront(String reason) {
699 mHomeStack.moveToFront(reason);
700 }
701
Matthew Ng330757d2017-02-28 14:19:17 -0800702 void moveRecentsStackToFront(String reason) {
703 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
704 if (recentsStack != null) {
705 recentsStack.moveToFront(reason);
706 }
707 }
708
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700709 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800710 boolean moveHomeStackTaskToTop(String reason) {
711 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700712
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700713 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700714 if (top == null) {
715 return false;
716 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700717 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700718 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700719 }
720
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800721 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700722 if (!mService.mBooting && !mService.mBooted) {
723 // Not ready yet!
724 return false;
725 }
726
Craig Mautner84984fa2014-06-19 11:19:20 -0700727 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700728 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700729 }
730
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800731 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700732 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800733 final String myReason = reason + " resumeHomeStackTask";
734
Mark Lua56ea122015-10-08 13:31:01 +0800735 // Only resume home activity if isn't finishing.
736 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700737 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800738 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700739 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800740 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700741 }
742
Craig Mautner8d341ef2013-03-26 09:03:27 -0700743 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800744 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
745 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700746 }
747
748 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700749 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700750 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800751 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700752 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800753 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
754 * valid if the matchMode is
755 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700756 */
Winson Chung7900bee2017-03-10 08:59:25 -0800757 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
758 // If there is a stack id set, ensure that we are attempting to actually restore a task
759 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
760 stackId != INVALID_STACK_ID) {
761 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
762 }
763
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800765 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
768 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700769 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 if (task != null) {
771 return task;
772 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700773 }
774 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800775
Winson Chung7900bee2017-03-10 08:59:25 -0800776 // If we are matching stack tasks only, return now
777 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800778 return null;
779 }
780
Winson Chung7900bee2017-03-10 08:59:25 -0800781 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
782 // the task from recents
783 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700784 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
785
786 if (task == null) {
787 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800788 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
789 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700790
791 return null;
792 }
793
794 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700795 return task;
796 }
797
Winson Chung7900bee2017-03-10 08:59:25 -0800798 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700799 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700800 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
801 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800802 return null;
803 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700804 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800805 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700806 }
807
Craig Mautner6170f732013-04-02 13:05:23 -0700808 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800809 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800810 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800811 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800812 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
813 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
814 if (r != null) {
815 return r;
816 }
Craig Mautner6170f732013-04-02 13:05:23 -0700817 }
818 }
819 return null;
820 }
821
Tony Mak853304c2016-04-18 15:17:41 +0100822 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000823 * Detects whether we should show a lock screen in front of this task for a locked user.
824 * <p>
825 * We'll do this if either of the following holds:
826 * <ul>
827 * <li>The top activity explicitly belongs to {@param userId}.</li>
828 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
829 * </ul>
830 *
831 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100832 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000833 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
834 // To handle the case that work app is in the task but just is not the top one.
835 final ActivityRecord activityRecord = task.getTopActivity();
836 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
837
838 return (activityRecord != null && activityRecord.userId == userId)
839 || (resultTo != null && resultTo.userId == userId);
840 }
841
842 /**
843 * Find all visible task stacks containing {@param userId} and intercept them with an activity
844 * to block out the contents and possibly start a credential-confirming intent.
845 *
846 * @param userId user handle for the locked managed profile.
847 */
848 void lockAllProfileTasks(@UserIdInt int userId) {
849 mWindowManager.deferSurfaceLayout();
850 try {
851 final List<ActivityStack> stacks = getStacks();
852 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
853 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
854 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
855 final TaskRecord task = tasks.get(taskNdx);
856
857 // Check the task for a top activity belonging to userId, or returning a result
858 // to an activity belonging to userId. Example case: a document picker for
859 // personal files, opened by a work app, should still get locked.
860 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000861 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
862 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000863 }
Tony Mak853304c2016-04-18 15:17:41 +0100864 }
865 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000866 } finally {
867 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100868 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000869 }
870
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800871 void setNextTaskIdForUserLocked(int taskId, int userId) {
872 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
873 if (taskId > currentTaskId) {
874 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700875 }
876 }
877
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700878 static int nextTaskIdForUser(int taskId, int userId) {
879 int nextTaskId = taskId + 1;
880 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
881 // Wrap around as there will be smaller task ids that are available now.
882 nextTaskId -= MAX_TASK_IDS_PER_USER;
883 }
884 return nextTaskId;
885 }
886
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800887 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800888 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
889 // for a userId u, a taskId can only be in the range
890 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
891 // 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 -0700892 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800893 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800894 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800895 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700896 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800897 if (candidateTaskId == currentTaskId) {
898 // Something wrong!
899 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
900 throw new IllegalStateException("Cannot get an available task id."
901 + " Reached limit of " + MAX_TASK_IDS_PER_USER
902 + " running tasks per user.");
903 }
904 }
905 mCurTaskIdForUser.put(userId, candidateTaskId);
906 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700907 }
908
Chong Zhang6cda19c2016-06-14 19:07:56 -0700909 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800910 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700911 if (stack == null) {
912 return null;
913 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 ActivityRecord resumedActivity = stack.mResumedActivity;
915 if (resumedActivity == null || resumedActivity.app == null) {
916 resumedActivity = stack.mPausingActivity;
917 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700918 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700919 }
920 }
921 return resumedActivity;
922 }
923
Dianne Hackbornff072722014-09-24 10:56:28 -0700924 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700925 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
928 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
930 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700931 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 continue;
933 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700934 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 if (hr != null) {
936 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
937 && processName.equals(hr.processName)) {
938 try {
George Mount2c92c972014-03-20 09:38:23 -0700939 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 didSomething = true;
941 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700942 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800943 Slog.w(TAG, "Exception in new application when starting activity "
944 + hr.intent.getComponent().flattenToShortString(), e);
945 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700946 }
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
Craig Mautner20e72272013-04-01 13:45:53 -0700948 }
949 }
950 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700951 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700952 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700953 }
Craig Mautner20e72272013-04-01 13:45:53 -0700954 return didSomething;
955 }
956
957 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800958 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
959 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
961 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700962 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 continue;
964 }
965 final ActivityRecord resumedActivity = stack.mResumedActivity;
966 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700967 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800968 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 return false;
970 }
Craig Mautner20e72272013-04-01 13:45:53 -0700971 }
972 }
Wei Wang65c7a152016-06-02 18:51:22 -0700973 // Send launch end powerhint when idle
974 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700975 return true;
976 }
977
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
980 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
982 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700983 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700985 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 return false;
987 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 }
989 }
990 }
991 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
994 mLastFocusedStack + " to=" + mFocusedStack);
995 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 return true;
997 }
998
999 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001000 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1002 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001003 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1004 final ActivityStack stack = stacks.get(stackNdx);
1005 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001006 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001007 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001008 return false;
1009 }
1010 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001012 }
1013 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001014 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 }
1016
Craig Mautner2acc3892013-09-23 10:28:14 -07001017 /**
1018 * Pause all activities in either all of the stacks or just the back stacks.
1019 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001020 * @param resuming The resuming activity.
1021 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1022 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001023 * @return true if any activity was paused as a result of this call.
1024 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001025 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001026 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1028 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1030 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001031 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001032 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001034 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1035 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001037 }
1038 }
1039 return someActivityPaused;
1040 }
1041
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001043 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1045 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001046 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1047 final ActivityStack stack = stacks.get(stackNdx);
1048 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001049 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001050 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001051 Slog.d(TAG_STATES,
1052 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001053 pausing = false;
1054 } else {
1055 return false;
1056 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001057 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001058 }
1059 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001060 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001061 }
1062
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001063 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001064 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001065 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001066 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 final ActivityStack stack = stacks.get(stackNdx);
1070 if (stack.mResumedActivity != null &&
1071 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001072 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001073 }
1074 }
1075 }
1076 }
1077
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001078 void cancelInitializingActivities() {
1079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1080 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1081 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1082 stacks.get(stackNdx).cancelInitializingActivities();
1083 }
1084 }
1085 }
1086
Bryce Lee4a194382017-04-04 14:32:48 -07001087 void waitActivityVisible(ComponentName name, WaitResult result) {
1088 final WaitInfo waitInfo = new WaitInfo(name, result);
1089 mWaitingForActivityVisible.add(waitInfo);
1090 }
1091
1092 void cleanupActivity(ActivityRecord r) {
1093 // Make sure this record is no longer in the pending finishes list.
1094 // This could happen, for example, if we are trimming activities
1095 // down to the max limit while they are still waiting to finish.
1096 mFinishingActivities.remove(r);
1097 mActivitiesWaitingForVisibleActivity.remove(r);
1098
1099 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1100 if (mWaitingForActivityVisible.get(i).matches(r)) {
1101 mWaitingForActivityVisible.remove(i);
1102 }
1103 }
1104 }
1105
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001106 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001107 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001108 }
1109
1110 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1111 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001112 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1113 final WaitInfo w = mWaitingForActivityVisible.get(i);
1114 if (w.matches(r)) {
1115 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001117 result.timeout = false;
1118 result.who = w.getComponent();
1119 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1120 result.thisTime = result.totalTime;
1121 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 if (changed) {
1125 mService.notifyAll();
1126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001127 }
1128
Chong Zhang5022da32016-06-21 16:31:37 -07001129 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1130 boolean changed = false;
1131 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1132 WaitResult w = mWaitingActivityLaunched.remove(i);
1133 if (w.who == null) {
1134 changed = true;
1135 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1136 // the starting activity ends up moving another activity to front, and it should
1137 // wait for this new activity to become visible instead.
1138 // Do not modify other fields.
1139 w.result = START_TASK_TO_FRONT;
1140 }
1141 }
1142 if (changed) {
1143 mService.notifyAll();
1144 }
1145 }
1146
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001147 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1148 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001149 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001150 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001151 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001152 if (w.who == null) {
1153 changed = true;
1154 w.timeout = timeout;
1155 if (r != null) {
1156 w.who = new ComponentName(r.info.packageName, r.info.name);
1157 }
1158 w.thisTime = thisTime;
1159 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001160 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001161 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001162 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001163 if (changed) {
1164 mService.notifyAll();
1165 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001166 }
1167
Craig Mautner29219d92013-04-16 20:19:12 -07001168 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001169 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001170 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001171 if (r != null) {
1172 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001173 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001174
Andrii Kulian7318d632016-07-20 18:59:28 -07001175 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001176 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1177
1178 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1179 final int displayId = mTmpOrderedDisplayIds.get(i);
1180 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1181 if (stacks == null) {
1182 continue;
1183 }
1184 for (int j = stacks.size() - 1; j >= 0; --j) {
1185 final ActivityStack stack = stacks.get(j);
1186 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1187 r = stack.topRunningActivityLocked();
1188 if (r != null) {
1189 return r;
1190 }
Craig Mautner29219d92013-04-16 20:19:12 -07001191 }
1192 }
1193 }
1194 return null;
1195 }
1196
Dianne Hackborn09233282014-04-30 11:33:59 -07001197 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001198 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001199 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1200 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001201 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001202 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001203 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001204 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1205 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001206 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001207 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001208 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001209 }
1210 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001211
1212 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1213 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1214 while (maxNum > 0) {
1215 long mostRecentActiveTime = Long.MIN_VALUE;
1216 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001217 final int numTaskLists = runningTaskLists.size();
1218 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1219 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001220 if (!stackTaskList.isEmpty()) {
1221 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1222 if (lastActiveTime > mostRecentActiveTime) {
1223 mostRecentActiveTime = lastActiveTime;
1224 selectedStackList = stackTaskList;
1225 }
1226 }
1227 }
1228 if (selectedStackList != null) {
1229 list.add(selectedStackList.remove(0));
1230 --maxNum;
1231 } else {
1232 break;
1233 }
1234 }
Craig Mautner20e72272013-04-01 13:45:53 -07001235 }
1236
Todd Kennedy7440f172015-12-09 14:31:22 -08001237 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1238 ProfilerInfo profilerInfo) {
1239 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001240 if (aInfo != null) {
1241 // Store the found target back into the intent, because now that
1242 // we have it we never want to do this again. For example, if the
1243 // user navigates back to this point in the history, we should
1244 // always restart the exact same activity.
1245 intent.setComponent(new ComponentName(
1246 aInfo.applicationInfo.packageName, aInfo.name));
1247
1248 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001249 if (!aInfo.processName.equals("system")) {
1250 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001251 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001252 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001253
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001254 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1255 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1256 }
1257
Man Caocfa78b22015-06-11 20:14:34 -07001258 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1259 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1260 }
1261
1262 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001263 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001264 }
1265 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001266 final String intentLaunchToken = intent.getLaunchToken();
1267 if (aInfo.launchToken == null && intentLaunchToken != null) {
1268 aInfo.launchToken = intentLaunchToken;
1269 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001270 }
1271 return aInfo;
1272 }
1273
Todd Kennedy7440f172015-12-09 14:31:22 -08001274 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001275 return resolveIntent(intent, resolvedType, userId, 0);
1276 }
1277
1278 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001279 synchronized (mService) {
1280 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001281 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001282 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001283 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001284 }
1285
1286 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1287 ProfilerInfo profilerInfo, int userId) {
1288 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1289 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1290 }
1291
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001292 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1293 boolean andResume, boolean checkConfig) throws RemoteException {
1294
1295 if (!allPausedActivitiesComplete()) {
1296 // While there are activities pausing we skipping starting any new activities until
1297 // pauses are complete. NOTE: that we also do this for activities that are starting in
1298 // the paused state because they will first be resumed then paused on the client side.
1299 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1300 "realStartActivityLocked: Skipping start of r=" + r
1301 + " some activities pausing...");
1302 return false;
1303 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001304
Andrii Kulianada10292017-02-09 23:19:29 -08001305 r.startFreezingScreenLocked(app, 0);
1306 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001307
Andrii Kulianada10292017-02-09 23:19:29 -08001308 // schedule launch ticks to collect information about slow apps.
1309 r.startLaunchTickingLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001310
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001311 // Have the window manager re-evaluate the orientation of the screen based on the new
1312 // activity order. Note that as a result of this, it can call back into the activity
1313 // manager with a new orientation. We don't care about that, because the activity is not
1314 // currently running so we are just restarting it anyway.
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 if (checkConfig) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001316 final int displayId = r.getDisplayId();
1317 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1318 getDisplayOverrideConfiguration(displayId),
1319 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07001320 // Deferring resume here because we're going to launch new activity shortly.
1321 // We don't want to perform a redundant launch of the same record while ensuring
1322 // configurations and trying to resume top activity of focused stack.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001323 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1324 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 }
1326
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001327 if (mKeyguardController.isKeyguardLocked()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001328 r.notifyUnknownVisibilityLaunched();
Jorim Jaggi5a108c22016-10-13 14:33:27 +02001329 }
1330
Craig Mautner2420ead2013-04-01 17:13:20 -07001331 r.app = app;
1332 app.waitingToKill = null;
1333 r.launchCount++;
1334 r.lastLaunchTime = SystemClock.uptimeMillis();
1335
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001336 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001337
1338 int idx = app.activities.indexOf(r);
1339 if (idx < 0) {
1340 app.activities.add(r);
1341 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001342 mService.updateLruProcessLocked(app, true, null);
1343 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001344
Bryce Leeaf691c02017-03-20 14:20:22 -07001345 final TaskRecord task = r.getTask();
Benjamin Franz469dd582015-06-09 14:24:36 +01001346 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1347 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001348 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001349 }
1350
Andrii Kulian02b7a832016-10-06 23:11:56 -07001351 final ActivityStack stack = task.getStack();
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 try {
1353 if (app.thread == null) {
1354 throw new RemoteException();
1355 }
1356 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001357 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 if (andResume) {
Andrii Kulianada10292017-02-09 23:19:29 -08001359 // We don't need to deliver new intents and/or set results if activity is going
1360 // to pause immediately after launch.
Craig Mautner2420ead2013-04-01 17:13:20 -07001361 results = r.results;
1362 newIntents = r.newIntents;
1363 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001364 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1365 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1366 + " newIntents=" + newIntents + " andResume=" + andResume);
Andrii Kulianada10292017-02-09 23:19:29 -08001367 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1368 System.identityHashCode(r), task.taskId, r.shortComponentName);
Chong Zhang85ee6542015-10-02 13:36:38 -07001369 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001370 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001371 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 }
Brian Carlstromca82e612016-04-19 23:16:08 -07001373 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1374 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
Craig Mautner2420ead2013-04-01 17:13:20 -07001375 r.sleeping = false;
1376 r.forceNewConfig = false;
Alan Viverette7df9b452016-06-16 12:47:58 -04001377 mService.showUnsupportedZoomDialogIfNeededLocked(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 mService.showAskCompatModeDialogLocked(r);
1379 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001380 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1382 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1383 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001384 final String profileFile = mService.mProfileFile;
1385 if (profileFile != null) {
1386 ParcelFileDescriptor profileFd = mService.mProfileFd;
1387 if (profileFd != null) {
1388 try {
1389 profileFd = profileFd.dup();
1390 } catch (IOException e) {
1391 if (profileFd != null) {
1392 try {
1393 profileFd.close();
1394 } catch (IOException o) {
1395 }
1396 profileFd = null;
1397 }
1398 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001400
1401 profilerInfo = new ProfilerInfo(profileFile, profileFd,
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08001402 mService.mSamplingInterval, mService.mAutoStopProfiler,
1403 mService.mStreamingOutput);
Craig Mautner2420ead2013-04-01 17:13:20 -07001404 }
1405 }
1406 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001407
Andrii Kulianada10292017-02-09 23:19:29 -08001408 app.hasShownUi = true;
1409 app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001410 app.forceProcessStateUpTo(mService.mTopProcessState);
Andrii Kulian1779e612016-10-12 21:58:25 -07001411 // Because we could be starting an Activity in the system process this may not go across
1412 // a Binder interface which would create a new Configuration. Consequently we have to
1413 // always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001414
1415 final Configuration globalConfiguration =
1416 new Configuration(mService.getGlobalConfiguration());
1417 r.setLastReportedGlobalConfiguration(globalConfiguration);
1418 final Configuration mergedOverrideConfiguration =
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001419 new Configuration(r.getMergedOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08001420 r.setLastReportedMergedOverrideConfiguration(mergedOverrideConfiguration);
1421
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Andrii Kulian8072d112016-09-16 11:11:01 -07001423 System.identityHashCode(r), r.info,
Bryce Leea163b762017-01-24 11:05:01 -08001424 globalConfiguration,
1425 mergedOverrideConfiguration, r.compat,
Andrii Kulian1779e612016-10-12 21:58:25 -07001426 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1427 r.persistentState, results, newIntents, !andResume,
1428 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001429
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001430 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001431 // This may be a heavy-weight process! Note that the package
1432 // manager will ensure that only activity can run in the main
1433 // process of the .apk, which is the only thing that will be
1434 // considered heavy-weight.
1435 if (app.processName.equals(app.info.packageName)) {
1436 if (mService.mHeavyWeightProcess != null
1437 && mService.mHeavyWeightProcess != app) {
1438 Slog.w(TAG, "Starting new heavy weight process " + app
1439 + " when already running "
1440 + mService.mHeavyWeightProcess);
1441 }
1442 mService.mHeavyWeightProcess = app;
1443 Message msg = mService.mHandler.obtainMessage(
1444 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1445 msg.obj = r;
1446 mService.mHandler.sendMessage(msg);
1447 }
1448 }
1449
1450 } catch (RemoteException e) {
1451 if (r.launchFailed) {
1452 // This is the second time we failed -- finish activity
1453 // and give up.
1454 Slog.e(TAG, "Second failure launching "
1455 + r.intent.getComponent().flattenToShortString()
1456 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001457 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001458 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1459 "2nd-crash", false);
1460 return false;
1461 }
1462
1463 // This is the first time we failed -- restart process and
1464 // retry.
1465 app.activities.remove(r);
1466 throw e;
1467 }
1468
1469 r.launchFailed = false;
1470 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001471 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001472 }
1473
1474 if (andResume) {
1475 // As part of the process of launching, ActivityThread also performs
1476 // a resume.
1477 stack.minimalResumeActivityLocked(r);
1478 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001479 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale7004a882017-04-10 09:50:21 -07001480 // it to resume+pause (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001481 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001482 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001483 "Moving to PAUSED: " + r + " (starting in paused state)");
1484 r.state = PAUSED;
Wale Ogunwale7004a882017-04-10 09:50:21 -07001485 r.stopped = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001486 }
1487
1488 // Launch the new version setup screen if needed. We do this -after-
1489 // launching the initial activity (that is, home), so that it can have
1490 // a chance to initialize itself while in the background, making the
1491 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001492 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001493 mService.startSetupActivityLocked();
1494 }
1495
Dianne Hackborn465fa392014-09-14 14:21:18 -07001496 // Update any services we are bound to that might care about whether
1497 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001498 if (r.app != null) {
1499 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1500 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001501
Craig Mautner2420ead2013-04-01 17:13:20 -07001502 return true;
1503 }
1504
Craig Mautnere79d42682013-04-01 19:01:53 -07001505 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001506 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001507 // Is this activity's application already running?
1508 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001509 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001510
Andrii Kulian02b7a832016-10-06 23:11:56 -07001511 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001512
1513 if (app != null && app.thread != null) {
1514 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001515 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1516 || !"android".equals(r.info.packageName)) {
1517 // Don't add this if it is a platform component that is marked
1518 // to run in multiple processes, because this is actually
1519 // part of the framework so doesn't make sense to track as a
1520 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001521 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1522 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001523 }
George Mount2c92c972014-03-20 09:38:23 -07001524 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001525 return;
1526 } catch (RemoteException e) {
1527 Slog.w(TAG, "Exception when starting activity "
1528 + r.intent.getComponent().flattenToShortString(), e);
1529 }
1530
1531 // If a dead object exception was thrown -- fall through to
1532 // restart the application.
1533 }
1534
1535 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001536 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001537 }
1538
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001539 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001540 String resultWho, int requestCode, int callingPid, int callingUid,
1541 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001542 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001543 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1544 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001545 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001546 return true;
1547 }
1548 final int componentRestriction = getComponentRestrictionForCallingPackage(
1549 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1550 final int actionRestriction = getActionRestrictionForCallingPackage(
1551 intent.getAction(), callingPackage, callingPid, callingUid);
1552 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1553 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1554 if (resultRecord != null) {
1555 resultStack.sendActivityResultLocked(-1,
1556 resultRecord, resultWho, requestCode,
1557 Activity.RESULT_CANCELED, null);
1558 }
1559 final String msg;
1560 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1561 msg = "Permission Denial: starting " + intent.toString()
1562 + " from " + callerApp + " (pid=" + callingPid
1563 + ", uid=" + callingUid + ")" + " with revoked permission "
1564 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1565 } else if (!aInfo.exported) {
1566 msg = "Permission Denial: starting " + intent.toString()
1567 + " from " + callerApp + " (pid=" + callingPid
1568 + ", uid=" + callingUid + ")"
1569 + " not exported from uid " + aInfo.applicationInfo.uid;
1570 } else {
1571 msg = "Permission Denial: starting " + intent.toString()
1572 + " from " + callerApp + " (pid=" + callingPid
1573 + ", uid=" + callingUid + ")"
1574 + " requires " + aInfo.permission;
1575 }
1576 Slog.w(TAG, msg);
1577 throw new SecurityException(msg);
1578 }
1579
1580 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1581 final String message = "Appop Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ")"
1584 + " requires " + AppOpsManager.permissionToOp(
1585 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1586 Slog.w(TAG, message);
1587 return false;
1588 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1589 final String message = "Appop Denial: starting " + intent.toString()
1590 + " from " + callerApp + " (pid=" + callingPid
1591 + ", uid=" + callingUid + ")"
1592 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1593 Slog.w(TAG, message);
1594 return false;
1595 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001596 if (options != null) {
1597 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1598 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1599 callingPid, callingUid);
1600 if (startInTaskPerm != PERMISSION_GRANTED) {
1601 final String msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ") with launchTaskId="
1604 + options.getLaunchTaskId();
1605 Slog.w(TAG, msg);
1606 throw new SecurityException(msg);
1607 }
1608 }
1609 // Check if someone tries to launch an activity on a private display with a different
1610 // owner.
1611 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001612 if (launchDisplayId != INVALID_DISPLAY
1613 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1614 final String msg = "Permission Denial: starting " + intent.toString()
1615 + " from " + callerApp + " (pid=" + callingPid
1616 + ", uid=" + callingUid + ") with launchDisplayId="
1617 + launchDisplayId;
1618 Slog.w(TAG, msg);
1619 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001620 }
1621 }
1622
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001623 return true;
1624 }
1625
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001626 /** Check if caller is allowed to launch activities on specified display. */
1627 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1628 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1629 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1630
1631 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1632 if (activityDisplay == null) {
1633 Slog.w(TAG, "Launch on display check: display not found");
1634 return false;
1635 }
1636
1637 if (!activityDisplay.isPrivate()) {
1638 // Anyone can launch on a public display.
1639 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1640 + " allow launch on public display");
1641 return true;
1642 }
1643
1644 // Check if the caller is the owner of the display.
1645 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1646 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1647 + " allow launch for owner of the display");
1648 return true;
1649 }
1650
1651 // Check if caller is present on display
1652 if (activityDisplay.isUidPresent(callingUid)) {
1653 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1654 + " allow launch for caller present on the display");
1655 return true;
1656 }
1657
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001658 // Check if the caller can manage activity stacks.
1659 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001660 callingUid);
1661 if (startAnyPerm == PERMISSION_GRANTED) {
1662 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1663 + " allow launch any on display");
1664 return true;
1665 }
1666
1667 Slog.w(TAG, "Launch on display check: denied");
1668 return false;
1669 }
1670
1671 /** Update lists of UIDs that are present on displays and have access to them. */
1672 void updateUIDsPresentOnDisplay() {
1673 mDisplayAccessUIDs.clear();
1674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1675 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001676 // Only bother calculating the whitelist for private displays
1677 if (activityDisplay.isPrivate()) {
1678 mDisplayAccessUIDs.append(
1679 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1680 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001681 }
1682 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1683 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1684 }
1685
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001686 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001687 final long identity = Binder.clearCallingIdentity();
1688 try {
1689 return UserManager.get(mService.mContext).getUserInfo(userId);
1690 } finally {
1691 Binder.restoreCallingIdentity(identity);
1692 }
1693 }
1694
Svet Ganov99b60432015-06-27 13:15:22 -07001695 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001697 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1698 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001699 == PackageManager.PERMISSION_DENIED) {
1700 return ACTIVITY_RESTRICTION_PERMISSION;
1701 }
1702
Christopher Tateff7add02015-08-17 10:23:22 -07001703 if (activityInfo.permission == null) {
1704 return ACTIVITY_RESTRICTION_NONE;
1705 }
1706
Svet Ganov99b60432015-06-27 13:15:22 -07001707 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1708 if (opCode == AppOpsManager.OP_NONE) {
1709 return ACTIVITY_RESTRICTION_NONE;
1710 }
1711
1712 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1713 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 if (!ignoreTargetSecurity) {
1715 return ACTIVITY_RESTRICTION_APPOP;
1716 }
Svet Ganov99b60432015-06-27 13:15:22 -07001717 }
1718
1719 return ACTIVITY_RESTRICTION_NONE;
1720 }
1721
Svetoslav7008b512015-06-24 18:47:07 -07001722 private int getActionRestrictionForCallingPackage(String action,
1723 String callingPackage, int callingPid, int callingUid) {
1724 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001725 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001726 }
1727
1728 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1729 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001730 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001731 }
1732
1733 final PackageInfo packageInfo;
1734 try {
1735 packageInfo = mService.mContext.getPackageManager()
1736 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1737 } catch (PackageManager.NameNotFoundException e) {
1738 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001739 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001740 }
1741
1742 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 if (mService.checkPermission(permission, callingPid, callingUid) ==
1747 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 final int opCode = AppOpsManager.permissionToOpCode(permission);
1752 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
1756 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1757 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001758 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001759 }
1760
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001764 void setLaunchSource(int uid) {
1765 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1766 }
1767
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001768 void acquireLaunchWakelock() {
1769 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1770 throw new IllegalStateException("Calling must be system uid");
1771 }
1772 mLaunchingActivity.acquire();
1773 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1774 // To be safe, don't allow the wake lock to be held for too long.
1775 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1776 }
1777 }
1778
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001779 /**
1780 * Called when the frontmost task is idle.
1781 * @return the state of mService.mBooting before this was called.
1782 */
1783 private boolean checkFinishBootingLocked() {
1784 final boolean booting = mService.mBooting;
1785 boolean enableScreen = false;
1786 mService.mBooting = false;
1787 if (!mService.mBooted) {
1788 mService.mBooted = true;
1789 enableScreen = true;
1790 }
1791 if (booting || enableScreen) {
1792 mService.postFinishBooting(booting, enableScreen);
1793 }
1794 return booting;
1795 }
1796
Craig Mautnerf3333272013-04-22 10:55:53 -07001797 // Checked.
1798 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001799 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001800 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001801
Craig Mautnerf3333272013-04-22 10:55:53 -07001802 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001803 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001804 int NS = 0;
1805 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001806 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001807 boolean activityRemoved = false;
1808
Wale Ogunwale7d701172015-03-11 15:36:30 -07001809 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001810 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001811 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1812 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1814 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001815 if (fromTimeout) {
1816 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001817 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001818
1819 // This is a hack to semi-deal with a race condition
1820 // in the client where it can be constructed with a
1821 // newer configuration from when we asked it to launch.
1822 // We'll update with whatever configuration it now says
1823 // it used to launch.
1824 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001825 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001826 }
1827
1828 // We are now idle. If someone is waiting for a thumbnail from
1829 // us, we can now deliver.
1830 r.idle = true;
1831
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001832 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001833 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001834 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001835 }
1836 }
1837
1838 if (allResumedActivitiesIdle()) {
1839 if (r != null) {
1840 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001841 }
1842
1843 if (mLaunchingActivity.isHeld()) {
1844 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1845 if (VALIDATE_WAKE_LOCK_CALLER &&
1846 Binder.getCallingUid() != Process.myUid()) {
1847 throw new IllegalStateException("Calling must be system uid");
1848 }
1849 mLaunchingActivity.release();
1850 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001851 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001852 }
1853
1854 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001855 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1856 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001857 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001858 if ((NF = mFinishingActivities.size()) > 0) {
1859 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001860 mFinishingActivities.clear();
1861 }
1862
Craig Mautnerf3333272013-04-22 10:55:53 -07001863 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001864 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001865 mStartingUsers.clear();
1866 }
1867
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 // Stop any activities that are scheduled to do so but have been
1869 // waiting for the next one to start.
1870 for (int i = 0; i < NS; i++) {
1871 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001872 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001873 if (stack != null) {
1874 if (r.finishing) {
1875 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1876 } else {
1877 stack.stopActivityLocked(r);
1878 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 }
1880 }
1881
1882 // Finish any activities that are scheduled to do so but have been
1883 // waiting for the next one to start.
1884 for (int i = 0; i < NF; i++) {
1885 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001886 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001887 if (stack != null) {
1888 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1889 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 }
1891
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001892 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001893 // Complete user switch
1894 if (startingUsers != null) {
1895 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001896 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001897 }
1898 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001899 }
1900
1901 mService.trimApplications();
1902 //dump();
1903 //mWindowManager.dump();
1904
Craig Mautnerf3333272013-04-22 10:55:53 -07001905 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001906 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 }
1908
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001909 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001910 }
1911
Craig Mautner8e569572013-10-11 17:36:59 -07001912 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001913 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1915 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1917 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1918 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001919 }
Craig Mautner19091252013-10-05 00:03:53 -07001920 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001921 }
1922
1923 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001924 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1925 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001926 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1927 stacks.get(stackNdx).closeSystemDialogsLocked();
1928 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001929 }
1930 }
1931
Craig Mautner93529a42013-10-04 15:03:13 -07001932 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001933 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001934 }
1935
Craig Mautner8d341ef2013-03-26 09:03:27 -07001936 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001937 * Update the last used stack id for non-current user (current user's last
1938 * used stack is the focused stack)
1939 */
1940 void updateUserStackLocked(int userId, ActivityStack stack) {
1941 if (userId != mCurrentUser) {
1942 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1943 }
1944 }
1945
1946 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001947 * @return true if some activity was finished (or would have finished if doit were true).
1948 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001949 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1950 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1953 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001955 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001956 if (stack.finishDisabledPackageActivitiesLocked(
1957 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001958 didSomething = true;
1959 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001960 }
1961 }
1962 return didSomething;
1963 }
1964
Dianne Hackborna413dc02013-07-12 12:02:55 -07001965 void updatePreviousProcessLocked(ActivityRecord r) {
1966 // Now that this process has stopped, we may want to consider
1967 // it to be the previous app to try to keep around in case
1968 // the user wants to return to it.
1969
1970 // First, found out what is currently the foreground app, so that
1971 // we don't blow away the previous app if this activity is being
1972 // hosted by the process that is actually still the foreground.
1973 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1975 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1977 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001978 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001979 if (stack.mResumedActivity != null) {
1980 fgApp = stack.mResumedActivity.app;
1981 } else if (stack.mPausingActivity != null) {
1982 fgApp = stack.mPausingActivity.app;
1983 }
1984 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001985 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001986 }
1987 }
1988
1989 // Now set this one as the previous process, only if that really
1990 // makes sense to.
1991 if (r.app != null && fgApp != null && r.app != fgApp
1992 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001993 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001994 mService.mPreviousProcess = r.app;
1995 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1996 }
1997 }
1998
Wale Ogunwaled046a012015-12-24 13:05:59 -08001999 boolean resumeFocusedStackTopActivityLocked() {
2000 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002001 }
2002
Wale Ogunwaled046a012015-12-24 13:05:59 -08002003 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002004 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002005 if (targetStack != null && isFocusedStack(targetStack)) {
2006 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002007 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002008 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2009 if (r == null || r.state != RESUMED) {
2010 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002011 } else if (r.state == RESUMED) {
2012 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2013 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002014 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002015 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002016 }
2017
Todd Kennedy39bfee52016-02-24 10:28:21 -08002018 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2020 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2022 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2023 }
2024 }
2025 }
2026
Adrian Roos20d7df32016-01-12 18:59:43 +01002027 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2028 TaskRecord finishedTask = null;
2029 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2031 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002032 final int numStacks = stacks.size();
2033 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2034 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002035 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2036 if (stack == focusedStack || finishedTask == null) {
2037 finishedTask = t;
2038 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002039 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002040 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002041 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002042 }
2043
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002044 void finishVoiceTask(IVoiceInteractionSession session) {
2045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2046 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2047 final int numStacks = stacks.size();
2048 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2049 final ActivityStack stack = stacks.get(stackNdx);
2050 stack.finishVoiceTask(session);
2051 }
2052 }
2053 }
2054
Andrii Kulianc27916642016-04-12 17:59:27 -07002055 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2056 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002057 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2058 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002059 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002060 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2061 // Caller wants the home activity moved with it. To accomplish this,
2062 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002063 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002064 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002065 ActivityStack currentStack = task.getStack();
2066 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002067 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2068 + task + " to front. Stack is null");
2069 return;
2070 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002071
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002072 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002073 int stackId = options.getLaunchStackId();
2074 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002075 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002076 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002077 if (stackId == INVALID_STACK_ID) {
2078 stackId = task.getLaunchStackId();
2079 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002080 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002081 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2082 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002083 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002084 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2085 // still need moveTaskToFrontLocked() below for any transition settings.
2086 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002087 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2088 resizeStackLocked(stackId, bounds,
2089 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002090 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002091 } else {
2092 // WM resizeTask must be done after the task is moved to the correct stack,
2093 // because Task's setBounds() also updates dim layer's bounds, but that has
2094 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002095 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002096 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002097 }
2098 }
2099
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002100 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002101 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002102 r == null ? null : r.appTimeTracker, reason);
2103
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002104 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002105 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002106
Andrii Kulian02b7a832016-10-06 23:11:56 -07002107 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002108 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002109 }
2110
Wale Ogunwale854809c2015-12-27 16:18:19 -08002111 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002112 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002113 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002114 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002115 return false;
2116 }
2117 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2118 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002119 }
2120
Winson Chung55893332017-02-17 17:13:10 -08002121 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002122 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002123 }
2124
Winson Chung55893332017-02-17 17:13:10 -08002125 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2126 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002127 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002128 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002129 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002131 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002132 return null;
2133 }
Matthew Ng330757d2017-02-28 14:19:17 -08002134 if (stackId == DOCKED_STACK_ID) {
2135 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2136 // the other side of the docked stack and we make visibility decisions based on that.
2137 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2138 }
Winson Chung55893332017-02-17 17:13:10 -08002139 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002140 }
2141
Andrii Kulian16802aa2016-11-02 12:21:33 -07002142 /**
2143 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2144 * If there is no such stack, new dynamic stack can be created.
2145 * @param displayId Target display.
2146 * @param r Activity that should be launched there.
2147 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2148 */
2149 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2150 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2151 if (activityDisplay == null) {
2152 throw new IllegalArgumentException(
2153 "Display with displayId=" + displayId + " not found.");
2154 }
2155
2156 // Return the topmost valid stack on the display.
2157 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2158 final ActivityStack stack = activityDisplay.mStacks.get(i);
2159 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2160 return stack;
2161 }
2162 }
2163
2164 // If there is no valid stack on the external display - check if new dynamic stack will do.
2165 if (displayId != Display.DEFAULT_DISPLAY) {
2166 final int newDynamicStackId = getNextStackId();
2167 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2168 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2169 }
2170 }
2171
2172 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2173 return null;
2174 }
2175
Craig Mautner967212c2013-04-13 21:10:58 -07002176 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002177 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002178 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2179 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002180 }
2181 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002182 }
2183
Jorim Jaggife762342016-10-13 14:33:27 +02002184 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2185 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2186 }
2187
Andrii Kulian7fc22812016-12-28 13:04:11 -08002188 /**
2189 * Get next focusable stack in the system. This will search across displays and stacks
2190 * in last-focused order for a focusable and visible stack, different from the target stack.
2191 *
2192 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2193 * searching for next candidate.
2194 * @return Next focusable {@link ActivityStack}, null if not found.
2195 */
2196 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2197 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2198
2199 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2200 final int displayId = mTmpOrderedDisplayIds.get(i);
2201 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2202 if (stacks == null) {
2203 continue;
2204 }
2205 for (int j = stacks.size() - 1; j >= 0; --j) {
2206 final ActivityStack stack = stacks.get(j);
2207 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002208 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002209 return stack;
2210 }
2211 }
2212 }
2213
2214 return null;
2215 }
2216
Craig Mautneree2e45a2014-06-27 12:10:03 -07002217 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002218 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002219 }
2220
2221 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002222 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2223 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2224 final TaskRecord task = tasks.get(taskNdx);
2225 if (task.isHomeTask()) {
2226 final ArrayList<ActivityRecord> activities = task.mActivities;
2227 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2228 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002229 if (r.isHomeActivity()
2230 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002231 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002232 }
2233 }
2234 }
2235 }
2236 return null;
2237 }
2238
Chong Zhangb15758a2015-11-17 12:12:03 -08002239 /**
2240 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2241 * the docked stack itself, or if it's side-by-side to the docked stack.
2242 */
2243 boolean isStackDockedInEffect(int stackId) {
2244 return stackId == DOCKED_STACK_ID ||
2245 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2246 }
2247
Todd Kennedyca4d8422015-01-15 15:19:22 -08002248 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002249 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002250 ActivityContainer activityContainer =
2251 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002252 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002253 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2254 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002255 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002256 return activityContainer;
2257 }
2258
Craig Mautner34b73df2014-01-12 21:11:08 -08002259 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002260 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2261 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2262 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002263 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2264 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002265 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002266 }
2267 }
2268
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002269 void deleteActivityContainerRecord(int stackId) {
2270 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2271 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2272 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002273 }
2274
Jorim Jaggidc249c42015-12-15 14:57:31 -08002275 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002276 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002277 if (stackId == DOCKED_STACK_ID) {
2278 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002279 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002280 return;
2281 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002282 final ActivityStack stack = getStack(stackId);
2283 if (stack == null) {
2284 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2285 return;
2286 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002287
Winson Chunga2249ac2017-03-30 18:15:30 -07002288 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2289 getStack(DOCKED_STACK_ID) != null) {
2290 // If the docked stack exists, don't resize non-floating stacks independently of the
2291 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002292 return;
2293 }
2294
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002295 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002296 mWindowManager.deferSurfaceLayout();
2297 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002298 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002299 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002300 stack.ensureVisibleActivitiesConfigurationLocked(
2301 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002302 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002303 } finally {
2304 mWindowManager.continueSurfaceLayout();
2305 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002306 }
2307 }
2308
Jorim Jaggi192086e2016-03-11 17:17:03 +01002309 void deferUpdateBounds(int stackId) {
2310 final ActivityStack stack = getStack(stackId);
2311 if (stack != null) {
2312 stack.deferUpdateBounds();
2313 }
2314 }
2315
2316 void continueUpdateBounds(int stackId) {
2317 final ActivityStack stack = getStack(stackId);
2318 if (stack != null) {
2319 stack.continueUpdateBounds();
2320 }
2321 }
2322
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002323 void notifyAppTransitionDone() {
2324 continueUpdateBounds(HOME_STACK_ID);
2325 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2326 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002327 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002328 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002329 if (task != null) {
2330 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002331 }
2332 }
2333 mResizingTasksDuringAnimation.clear();
2334 }
2335
Robert Carre7cc44d2017-03-20 19:04:30 -07002336 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2337 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002338
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002339 final ActivityStack stack = getStack(fromStackId);
2340 if (stack == null) {
2341 return;
2342 }
2343
2344 mWindowManager.deferSurfaceLayout();
2345 try {
2346 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002347 // We are moving all tasks from the docked stack to the fullscreen stack,
2348 // which is dismissing the docked stack, so resize all other stacks to
2349 // fullscreen here already so we don't end up with resize trashing.
2350 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2351 if (StackId.isResizeableByDockedStack(i)) {
2352 ActivityStack otherStack = getStack(i);
2353 if (otherStack != null) {
2354 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2355 true /* allowResizeInDockedMode */, DEFER_RESUME);
2356 }
2357 }
2358 }
2359
2360 // Also disable docked stack resizing since we have manually adjusted the
2361 // size of other stacks above and we don't want to trigger a docked stack
2362 // resize when we remove task from it below and it is detached from the
2363 // display because it no longer contains any tasks.
2364 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002365 } else if (fromStackId == PINNED_STACK_ID) {
2366 if (onTop) {
2367 // Log if we are expanding the PiP to fullscreen
2368 MetricsLogger.action(mService.mContext,
2369 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2370 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002371 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002372 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002373 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002374 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002375 // If we are moving from the pinned stack, then the animation takes care of updating
2376 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002377 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002378 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2379 final int size = tasks.size();
2380 if (onTop) {
2381 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002382 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002383 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002384 if (fromStackId == PINNED_STACK_ID) {
2385 // Update the return-to to reflect where the pinned stack task was moved
2386 // from so that we retain the stack that was previously visible if the
2387 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002388 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2389 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002390 }
Winson Chung74666102017-02-22 17:49:24 -08002391 // Defer resume until all the tasks have been moved to the fullscreen stack
2392 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2393 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002394 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002395 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002396 }
2397 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002398 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002399 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002400 final int position = fullscreenStack != null
2401 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2402 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002403 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002404 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2405 schedulePictureInPictureModeChange,
2406 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002407 }
2408 }
Winson Chung74666102017-02-22 17:49:24 -08002409
2410 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2411 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002412 } finally {
2413 mAllowDockedStackResize = true;
2414 mWindowManager.continueSurfaceLayout();
2415 }
2416 }
2417
Robert Carr6914f082017-03-20 19:04:30 -07002418 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002419 mWindowManager.inSurfaceTransaction(
2420 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002421 }
2422
Jorim Jaggidc249c42015-12-15 14:57:31 -08002423 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002424 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2425 boolean preserveWindows) {
2426 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2427 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2428 false /* deferResume */);
2429 }
2430
2431 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2432 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2433 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002434
2435 if (!mAllowDockedStackResize) {
2436 // Docked stack resize currently disabled.
2437 return;
2438 }
2439
Jorim Jaggidc249c42015-12-15 14:57:31 -08002440 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2441 if (stack == null) {
2442 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2443 return;
2444 }
2445
2446 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2447 mWindowManager.deferSurfaceLayout();
2448 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002449 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2450 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002451 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002452 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002453
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002454 // TODO: Checking for isAttached might not be needed as if the user passes in null
2455 // dockedBounds then they want the docked stack to be dismissed.
2456 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2457 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002458 // In this case we make all other static stacks fullscreen and move all
2459 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002460 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002461
2462 // stack shouldn't contain anymore activities, so nothing to resume.
2463 r = null;
2464 } else {
2465 // Docked stacks occupy a dedicated region on screen so the size of all other
2466 // static stacks need to be adjusted so they don't overlap with the docked stack.
2467 // We get the bounds to use from window manager which has been adjusted for any
2468 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002469 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002470 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002471 final ActivityStack current = getStack(i);
2472 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002473 current.getStackDockedModeBounds(
2474 tempOtherTaskBounds /* currentTempTaskBounds */,
2475 tempRect /* outStackBounds */,
2476 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2477
Matthew Nge15352e2016-12-20 15:36:29 -08002478 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002479 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2480 tempOtherTaskInsetBounds, preserveWindows,
2481 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002482 }
2483 }
2484 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002485 if (!deferResume) {
2486 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2487 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002488 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002489 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002490 mWindowManager.continueSurfaceLayout();
2491 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2492 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002493 }
2494
Robert Carr0d00c2e2016-02-29 17:45:02 -08002495 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2496 final ActivityStack stack = getStack(PINNED_STACK_ID);
2497 if (stack == null) {
2498 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2499 return;
2500 }
2501 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2502 mWindowManager.deferSurfaceLayout();
2503 try {
2504 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002505 Rect insetBounds = null;
2506 if (tempPinnedTaskBounds != null) {
2507 // We always use 0,0 as the position for the inset rect because
2508 // if we are getting insets at all in the pinned stack it must mean
2509 // we are headed for fullscreen.
2510 insetBounds = tempRect;
2511 insetBounds.top = 0;
2512 insetBounds.left = 0;
2513 insetBounds.right = tempPinnedTaskBounds.width();
2514 insetBounds.bottom = tempPinnedTaskBounds.height();
2515 }
2516 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002517 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002518 } finally {
2519 mWindowManager.continueSurfaceLayout();
2520 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2521 }
2522 }
2523
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002524 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002525 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002526 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002527 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002528 }
2529
Wale Ogunwale1666e312016-12-16 11:27:18 -08002530 final ActivityContainer activityContainer =
2531 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002532 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002533 }
2534
Robert Carr6914f082017-03-20 19:04:30 -07002535
Robert Carre7cc44d2017-03-20 19:04:30 -07002536 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002537 final ActivityStack stack = getStack(stackId);
2538 if (stack == null) {
2539 return;
2540 }
2541
2542 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2543 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002544 /**
2545 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2546 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2547 * that the client receives onStop() before it is reparented. We do this by detaching
2548 * the stack from the display so that it will be considered invisible when
2549 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2550 * invisible as well and added to the stopping list. After which we process the
2551 * stopping list by handling the idle.
2552 */
2553 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2554 pinnedStack.mForceHidden = true;
2555 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2556 pinnedStack.mForceHidden = false;
2557 activityIdleInternalLocked(null, false /* fromTimeout */,
2558 true /* processPausingActivites */, null /* configuration */);
2559
2560 // Move all the tasks to the bottom of the fullscreen stack
2561 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002562 } else {
2563 for (int i = tasks.size() - 1; i >= 0; i--) {
2564 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2565 REMOVE_FROM_RECENTS);
2566 }
2567 }
2568 }
2569
2570 /**
Robert Carr6914f082017-03-20 19:04:30 -07002571 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2572 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2573 * instead moved back onto the fullscreen stack.
2574 */
2575 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002576 mWindowManager.inSurfaceTransaction(
2577 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002578 }
2579
2580 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002581 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2582 */
2583 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2584 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2585 }
2586
2587 /**
Winson Chung010927a2016-12-15 16:12:35 -08002588 * Removes the task with the specified task id.
2589 *
2590 * @param taskId Identifier of the task to be removed.
2591 * @param killProcess Kill any process associated with the task if possible.
2592 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002593 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2594 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002595 * @return Returns true if the given task was found and removed.
2596 */
Winson Chung6954fc92017-03-24 16:22:12 -07002597 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2598 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002599 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2600 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002601 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002602 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002603 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2604 if (tr.isPersistable) {
2605 mService.notifyTaskPersisterLocked(null, true);
2606 }
2607 return true;
2608 }
2609 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2610 return false;
2611 }
2612
2613 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2614 if (removeFromRecents) {
2615 mRecentTasks.remove(tr);
2616 tr.removedFromRecents();
2617 }
2618 ComponentName component = tr.getBaseIntent().getComponent();
2619 if (component == null) {
2620 Slog.w(TAG, "No component for base intent of task: " + tr);
2621 return;
2622 }
2623
2624 // Find any running services associated with this app and stop if needed.
2625 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2626
2627 if (!killProcess) {
2628 return;
2629 }
2630
2631 // Determine if the process(es) for this task should be killed.
2632 final String pkg = component.getPackageName();
2633 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2634 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2635 for (int i = 0; i < pmap.size(); i++) {
2636
2637 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2638 for (int j = 0; j < uids.size(); j++) {
2639 ProcessRecord proc = uids.valueAt(j);
2640 if (proc.userId != tr.userId) {
2641 // Don't kill process for a different user.
2642 continue;
2643 }
2644 if (proc == mService.mHomeProcess) {
2645 // Don't kill the home process along with tasks from the same package.
2646 continue;
2647 }
2648 if (!proc.pkgList.containsKey(pkg)) {
2649 // Don't kill process that is not associated with this task.
2650 continue;
2651 }
2652
2653 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002654 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002655 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2656 // Don't kill process(es) that has an activity in a different task that is
2657 // also in recents.
2658 return;
2659 }
2660 }
2661
2662 if (proc.foregroundServices) {
2663 // Don't kill process(es) with foreground service.
2664 return;
2665 }
2666
2667 // Add process to kill list.
2668 procsToKill.add(proc);
2669 }
2670 }
2671
2672 // Kill the running processes.
2673 for (int i = 0; i < procsToKill.size(); i++) {
2674 ProcessRecord pr = procsToKill.get(i);
2675 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2676 && pr.curReceivers.isEmpty()) {
2677 pr.kill("remove task", true);
2678 } else {
2679 // We delay killing processes that are not in the background or running a receiver.
2680 pr.waitingToKill = "remove task";
2681 }
2682 }
2683 }
2684
Craig Mautner4a1cb222013-12-04 16:14:06 -08002685 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002686 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002687 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2688 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002689 break;
2690 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002691 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002692 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002693 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002694 }
2695
Chong Zhang5dcb2752015-08-18 13:50:26 -07002696 /**
2697 * Restores a recent task to a stack
2698 * @param task The recent task to be restored.
2699 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002700 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2701 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002702 * @return true if the task has been restored successfully.
2703 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002704 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002705 if (!StackId.isStaticStack(stackId)) {
2706 // If stack is not static (or stack id is invalid) - use the default one.
2707 // This means that tasks that were on external displays will be restored on the
2708 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002709 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002710 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002711 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2712 // Put it in the fullscreen stack.
2713 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002714 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002715
Andrii Kulian02b7a832016-10-06 23:11:56 -07002716 final ActivityStack currentStack = task.getStack();
2717 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002718 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002719 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002720 // Nothing else to do since it is already restored in the right stack.
2721 return true;
2722 }
2723 // Remove current stack association, so we can re-associate the task with the
2724 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002725 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002726 }
2727
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002728 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002729 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002730
2731 if (stack == null) {
2732 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002733 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2734 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002735 return false;
2736 }
2737
Wale Ogunwale72919d22016-12-08 18:58:50 -08002738 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002739 // TODO: move call for creation here and other place into Stack.addTask()
2740 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002741 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2742 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002743 final ArrayList<ActivityRecord> activities = task.mActivities;
2744 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002745 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002746 }
2747 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002748 }
2749
Wale Ogunwale040b4702015-08-06 18:10:50 -07002750 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002751 * Move stack with all its existing content to specified display.
2752 * @param stackId Id of stack to move.
2753 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002754 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002755 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002756 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002757 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2758 if (activityDisplay == null) {
2759 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2760 + displayId);
2761 }
2762 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2763 if (activityContainer != null) {
2764 if (activityContainer.isAttachedLocked()) {
2765 if (activityContainer.getDisplayId() == displayId) {
2766 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2767 + " to its current displayId=" + displayId);
2768 }
2769
Andrii Kulian250d6532017-02-08 23:30:45 -08002770 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002771 } else {
2772 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2773 + stackId + " is not attached to any display.");
2774 }
2775 } else {
2776 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2777 + stackId);
2778 }
Andrii Kulian839def92016-11-02 10:58:58 -07002779 // TODO(multi-display): resize stacks properly if moved from split-screen.
2780 }
2781
2782 /**
Winson Chung74666102017-02-22 17:49:24 -08002783 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2784 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002785 */
Winson Chung74666102017-02-22 17:49:24 -08002786 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002787 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002788
Winson Chung74666102017-02-22 17:49:24 -08002789 // Check that we aren't reparenting to the same stack that the task is already in
2790 if (prevStack != null && prevStack.mStackId == stackId) {
2791 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2792 + " already in stackId=" + stackId);
2793 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002794 }
2795
Winson Chung74666102017-02-22 17:49:24 -08002796 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2797 // support
2798 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2799 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2800 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002801 }
2802
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002803 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2804 // multi-display.
2805 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2806 // TODO: Check ActivityView after fixing b/35349678.
2807 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2808 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2809 + " reparent task=" + task + " to stackId=" + stackId);
2810 }
2811
Winson Chung74666102017-02-22 17:49:24 -08002812 // Ensure that we aren't trying to move into a freeform stack without freeform
2813 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002814 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002815 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2816 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002817 }
2818
Winson Chung74666102017-02-22 17:49:24 -08002819 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2820 // used for split-screen mode and will cause things like the docked divider to show up. We
2821 // instead leave the task in its current stack or move it to the fullscreen stack if it
2822 // isn't currently in a stack.
2823 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2824 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2825 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2826 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002827 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002828
Winson Chung74666102017-02-22 17:49:24 -08002829 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2830 // example, a docked stack is created which will lead to the stack we are moving from being
2831 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002832 try {
Winson Chung74666102017-02-22 17:49:24 -08002833 task.mTemporarilyUnresizable = true;
2834 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002835 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002836 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002837 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002838 }
2839
Winson Chung08f81892017-03-02 15:40:51 -08002840 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002841 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2842 if (stack == null) {
2843 throw new IllegalArgumentException(
2844 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2845 }
2846
2847 final ActivityRecord r = stack.topRunningActivityLocked();
2848 if (r == null) {
2849 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2850 + " in stack=" + stack);
2851 return false;
2852 }
2853
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002854 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002855 Slog.w(TAG,
2856 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002857 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002858 return false;
2859 }
2860
Winson Chung08f81892017-03-02 15:40:51 -08002861 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2862 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002863 return true;
2864 }
2865
Winson Chung08f81892017-03-02 15:40:51 -08002866 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2867 boolean moveHomeStackToFront, String reason) {
2868
Wale Ogunwale480dca02016-02-06 13:58:29 -08002869 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002870
Wale Ogunwale1666e312016-12-16 11:27:18 -08002871 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002872 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002873
Wale Ogunwale480dca02016-02-06 13:58:29 -08002874 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002875 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002876
Andrii Kulian02b7a832016-10-06 23:11:56 -07002877 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002878 // An activity can't be pinned and visible behind at the same time. Go ahead and
2879 // release it from been visible behind before pinning.
2880 requestVisibleBehindLocked(r, false);
2881 }
2882
Wale Ogunwale480dca02016-02-06 13:58:29 -08002883 // Resize the pinned stack to match the current size of the task the activity we are
2884 // going to be moving is currently contained in. We do this to have the right starting
2885 // animation bounds for the pinned stack to the desired bounds the caller wants.
2886 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2887 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002888 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002889
2890 if (task.mActivities.size() == 1) {
2891 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002892 // the stack without re-parenting the activity in a different task. We don't
2893 // move the home stack forward if we are currently entering picture-in-picture
2894 // while pausing because that changes the focused stack and may prevent the new
2895 // starting activity from resuming.
2896 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002897 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002898 // Move the home stack forward if the task we just moved to the pinned stack
2899 // was launched from home so home should be visible behind it.
2900 moveHomeStackToFront(reason);
2901 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002902 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002903 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002904 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002905 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002906 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002907 // reveal/leave the other activities in their original task.
2908
2909 // Currently, we don't support reparenting activities across tasks in two different
2910 // stacks, so instead, just create a new task in the same stack, reparent the
2911 // activity into that task, and then reparent the whole task to the new stack. This
2912 // ensures that all the necessary work to migrate states in the old and new stacks
2913 // is also done.
2914 final TaskRecord newTask = task.getStack().createTaskRecord(
2915 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2916 r.mActivityType);
2917 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2918
Winson Chung5af42fc2017-03-24 17:11:33 -07002919 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002920 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002921 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002922 }
Winson Chungc2baac02017-01-11 13:34:47 -08002923
2924 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2925 // to the pinned stack
2926 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002927 } finally {
2928 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002929 }
2930
Wale Ogunwale480dca02016-02-06 13:58:29 -08002931 // The task might have already been running and its visibility needs to be synchronized
2932 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002933 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002934 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002935
Winson Chung47900652017-04-06 18:44:25 -07002936 // TODO(b/36099777): Schedule the PiP mode change here immediately until we can defer all
2937 // callbacks until after the bounds animation
2938 scheduleUpdatePictureInPictureModeIfNeeded(r.getTask(), destBounds, true /* immediate */);
2939
Winson Chung08f81892017-03-02 15:40:51 -08002940 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002941 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002942 }
2943
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002944 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002945 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2946 if (r == null || !r.isFocusable()) {
2947 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2948 "moveActivityStackToFront: unfocusable r=" + r);
2949 return false;
2950 }
2951
Bryce Leeaf691c02017-03-20 14:20:22 -07002952 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002953 final ActivityStack stack = r.getStack();
2954 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002955 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2956 + r + " task=" + task);
2957 return false;
2958 }
2959
Chong Zhang6cda19c2016-06-14 19:07:56 -07002960 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2961 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2962 "moveActivityStackToFront: already on top, r=" + r);
2963 return false;
2964 }
2965
2966 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2967 "moveActivityStackToFront: r=" + r);
2968
2969 stack.moveToFront(reason, task);
2970 return true;
2971 }
2972
David Stevensc6b91c62017-02-08 14:23:58 -08002973 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002974 mTmpFindTaskResult.r = null;
2975 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002976 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002977 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002978 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2979 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2981 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002982 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002983 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002984 continue;
2985 }
2986 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002987 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2988 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002989 continue;
2990 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002991 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002992 // It is possible to have tasks in multiple stacks with the same root affinity, so
2993 // we should keep looking after finding an affinity match to see if there is a
2994 // better match in another stack. Also, task affinity isn't a good enough reason
2995 // to target a display which isn't the source of the intent, so skip any affinity
2996 // matches not on the specified display.
2997 if (mTmpFindTaskResult.r != null) {
2998 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2999 return mTmpFindTaskResult.r;
3000 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
3001 affinityMatch = mTmpFindTaskResult.r;
3002 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003004 }
3005 }
David Stevensc6b91c62017-02-08 14:23:58 -08003006 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3007 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003008 }
3009
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003010 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3011 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003014 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003015 final ActivityRecord ar = stacks.get(stackNdx)
3016 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003017 if (ar != null) {
3018 return ar;
3019 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003020 }
3021 }
3022 return null;
3023 }
3024
Craig Mautner8d341ef2013-03-26 09:03:27 -07003025 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003026 scheduleSleepTimeout();
3027 if (!mGoingToSleep.isHeld()) {
3028 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003029 if (mLaunchingActivity.isHeld()) {
3030 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3031 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003032 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003033 mLaunchingActivity.release();
3034 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003035 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003036 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003037 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003038 }
3039
3040 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003042
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003043 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003044 final long endTime = System.currentTimeMillis() + timeout;
3045 while (true) {
3046 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003047 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3048 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003049 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3050 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3051 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003052 }
3053 if (cantShutdown) {
3054 long timeRemaining = endTime - System.currentTimeMillis();
3055 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003056 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003057 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003058 } catch (InterruptedException e) {
3059 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003060 } else {
3061 Slog.w(TAG, "Activity manager shutdown timed out");
3062 timedout = true;
3063 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003064 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003065 } else {
3066 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003067 }
3068 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003069
3070 // Force checkReadyForSleep to complete.
3071 mSleepTimeout = true;
3072 checkReadyForSleepLocked();
3073
Craig Mautner8d341ef2013-03-26 09:03:27 -07003074 return timedout;
3075 }
3076
3077 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 removeSleepTimeouts();
3079 if (mGoingToSleep.isHeld()) {
3080 mGoingToSleep.release();
3081 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003082 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3083 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003084 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3085 final ActivityStack stack = stacks.get(stackNdx);
3086 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003087 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003088 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003089 }
Craig Mautner5314a402013-09-26 12:40:16 -07003090 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003092 mGoingToSleepActivities.clear();
Wale Ogunwale7004a882017-04-10 09:50:21 -07003093 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003094 }
3095
3096 void activitySleptLocked(ActivityRecord r) {
3097 mGoingToSleepActivities.remove(r);
3098 checkReadyForSleepLocked();
3099 }
3100
3101 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003102 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003103 // Do not care.
3104 return;
3105 }
3106
3107 if (!mSleepTimeout) {
3108 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003109 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3110 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003111 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3112 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3113 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003114 }
3115
3116 if (mStoppingActivities.size() > 0) {
3117 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003118 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003119 + mStoppingActivities.size() + " activities");
3120 scheduleIdleLocked();
3121 dontSleep = true;
3122 }
3123
3124 if (mGoingToSleepActivities.size() > 0) {
3125 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003126 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003127 + mGoingToSleepActivities.size() + " activities");
3128 dontSleep = true;
3129 }
3130
3131 if (dontSleep) {
3132 return;
3133 }
3134 }
3135
Wei Wang65c7a152016-06-02 18:51:22 -07003136 // Send launch end powerhint before going sleep
3137 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3138
Craig Mautnere0a38842013-12-16 16:14:02 -08003139 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3140 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003141 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3142 stacks.get(stackNdx).goToSleep();
3143 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003144 }
3145
3146 removeSleepTimeouts();
3147
3148 if (mGoingToSleep.isHeld()) {
3149 mGoingToSleep.release();
3150 }
3151 if (mService.mShuttingDown) {
3152 mService.notifyAll();
3153 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154 }
3155
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003156 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003157 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003158 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003159 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003160 }
3161 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003162 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003163 mWindowManager.executeAppTransition();
3164 return true;
3165 }
3166 return false;
3167 }
3168
Craig Mautner8d341ef2013-03-26 09:03:27 -07003169 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003170 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3171 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003172 int stackNdx = stacks.size() - 1;
3173 while (stackNdx >= 0) {
3174 stacks.get(stackNdx).handleAppCrashLocked(app);
3175 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177 }
3178 }
3179
Jose Lima4b6c6692014-08-12 17:41:12 -07003180 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003181 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003182 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003183 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3184 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003185 return false;
3186 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003187
3188 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3189 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3190 + " visible=" + visible + " stackId=" + stack.mStackId
3191 + " can't contain visible behind activities");
3192 return false;
3193 }
3194
Jose Lima4b6c6692014-08-12 17:41:12 -07003195 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003196 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3197 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003198
3199 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003200 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003201 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003202 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003203 return true;
3204 }
3205
3206 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003207 if (visible && top.fullscreen) {
3208 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003209 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3210 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3211 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3212 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003213 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003214 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3215 // Only the activity set as currently visible behind should actively reset its
3216 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003217 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3218 "requestVisibleBehind: returning visible=" + visible
3219 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3220 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003221 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003222 }
3223
Jose Lima4b6c6692014-08-12 17:41:12 -07003224 stack.setVisibleBehindActivity(visible ? r : null);
3225 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003226 // If there is a translucent home activity, we need to force it stop being translucent,
3227 // because we can't depend on the application to necessarily perform that operation.
3228 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003229 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003230 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003231 mService.convertFromTranslucent(next.appToken);
3232 }
3233 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003234 if (top.app != null && top.app.thread != null) {
3235 // Notify the top app of the change.
3236 try {
3237 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3238 } catch (RemoteException e) {
3239 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003240 }
3241 return true;
3242 }
3243
Craig Mautnerbb742462014-07-07 15:28:55 -07003244 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003245 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003246 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003247 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003248
3249 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003250 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003251 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003252 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003253 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003254
3255 // When launching tasks behind, update the last active time of the top task after the new
3256 // task has been shown briefly
3257 final ActivityRecord top = stack.topActivity();
3258 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003259 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003260 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003261 }
3262
3263 void scheduleLaunchTaskBehindComplete(IBinder token) {
3264 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3265 }
3266
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003267 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3268 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003269 mKeyguardController.beginActivityVisibilityUpdate();
3270 try {
3271 // First the front stacks. In case any are not fullscreen and are in front of home.
3272 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3273 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3274 final int topStackNdx = stacks.size() - 1;
3275 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3276 final ActivityStack stack = stacks.get(stackNdx);
3277 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3278 }
Craig Mautner580ea812013-04-25 12:58:38 -07003279 }
Jorim Jaggife762342016-10-13 14:33:27 +02003280 } finally {
3281 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003282 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003283 }
3284
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003285 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3286 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3287 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3288 final int topStackNdx = stacks.size() - 1;
3289 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3290 final ActivityStack stack = stacks.get(stackNdx);
3291 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3292 }
3293 }
3294 }
3295
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003296 void invalidateTaskLayers() {
3297 mTaskLayersChanged = true;
3298 }
3299
3300 void rankTaskLayersIfNeeded() {
3301 if (!mTaskLayersChanged) {
3302 return;
3303 }
3304 mTaskLayersChanged = false;
3305 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3306 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3307 int baseLayer = 0;
3308 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3309 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3310 }
3311 }
3312 }
3313
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003314 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3315 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3316 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3317 final int topStackNdx = stacks.size() - 1;
3318 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3319 final ActivityStack stack = stacks.get(stackNdx);
3320 stack.clearOtherAppTimeTrackers(except);
3321 }
3322 }
3323 }
3324
Craig Mautner8d341ef2013-03-26 09:03:27 -07003325 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003326 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3327 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003328 final int numStacks = stacks.size();
3329 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3330 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003331 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003332 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003333 }
3334 }
3335
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003336 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3337 // Examine all activities currently running in the process.
3338 TaskRecord firstTask = null;
3339 // Tasks is non-null only if two or more tasks are found.
3340 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003341 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3342 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003343 ActivityRecord r = app.activities.get(i);
3344 // First, if we find an activity that is in the process of being destroyed,
3345 // then we just aren't going to do anything for now; we want things to settle
3346 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003347 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003348 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003349 return;
3350 }
3351 // Don't consider any activies that are currently not in a state where they
3352 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003353 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3354 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003355 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003356 continue;
3357 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003358
3359 final TaskRecord task = r.getTask();
3360 if (task != null) {
3361 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003362 + " from " + r);
3363 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003364 firstTask = task;
3365 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003366 if (tasks == null) {
3367 tasks = new ArraySet<>();
3368 tasks.add(firstTask);
3369 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003370 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003371 }
3372 }
3373 }
3374 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003375 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003376 return;
3377 }
3378 // If we have activities in multiple tasks that are in a position to be destroyed,
3379 // let's iterate through the tasks and release the oldest one.
3380 final int numDisplays = mActivityDisplays.size();
3381 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3382 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3383 // Step through all stacks starting from behind, to hit the oldest things first.
3384 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3385 final ActivityStack stack = stacks.get(stackNdx);
3386 // Try to release activities in this stack; if we manage to, we are done.
3387 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3388 return;
3389 }
3390 }
3391 }
3392 }
3393
Amith Yamasani37a40c22015-06-17 13:25:42 -07003394 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003395 final int focusStackId = mFocusedStack.getStackId();
3396 // We dismiss the docked stack whenever we switch users.
3397 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003398 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3399 // also cause all tasks to be moved to the fullscreen stack at a position that is
3400 // appropriate.
3401 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003402
3403 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003404 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003405 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003406
Craig Mautner858d8a62013-04-23 17:08:34 -07003407 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003411 final ActivityStack stack = stacks.get(stackNdx);
3412 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003413 TaskRecord task = stack.topTask();
3414 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003415 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003416 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003418 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003419
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003420 ActivityStack stack = getStack(restoreStackId);
3421 if (stack == null) {
3422 stack = mHomeStack;
3423 }
3424 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003425 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003426 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003427 } else {
3428 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003429 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003430 }
Craig Mautner93529a42013-10-04 15:03:13 -07003431 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003432 }
3433
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003434 /** Checks whether the userid is a profile of the current user. */
3435 boolean isCurrentProfileLocked(int userId) {
3436 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003437 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003438 }
3439
Winson Chung4dabf232017-01-25 13:25:22 -08003440 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3441 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003442 ArrayList<ActivityRecord> stops = null;
3443
3444 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003445 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3446 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003447 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003448 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003449 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3450 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003451 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003452 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003453 if (s.finishing) {
3454 // If this activity is finishing, it is sitting on top of
3455 // everyone else but we now know it is no longer needed...
3456 // so get rid of it. Otherwise, we need to go through the
3457 // normal flow and hide it once we determine that it is
3458 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003459 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003460 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003461 }
3462 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003463 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003464 if (!processPausingActivities && s.state == PAUSING) {
3465 // Defer processing pausing activities in this iteration and reschedule
3466 // a delayed idle to reprocess it again
3467 removeTimeoutsForActivityLocked(idleActivity);
3468 scheduleIdleTimeoutLocked(idleActivity);
3469 continue;
3470 }
3471
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003472 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003473 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003474 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003475 }
3476 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003477 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003478 }
3479 }
3480
3481 return stops;
3482 }
3483
Craig Mautnercf910b02013-04-23 11:23:27 -07003484 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003485 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3486 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3487 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3488 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003489 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003491 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003492 if (r == null) Slog.e(TAG,
3493 "validateTop...: null top activity, stack=" + stack);
3494 else {
3495 final ActivityRecord pausing = stack.mPausingActivity;
3496 if (pausing != null && pausing == r) Slog.e(TAG,
3497 "validateTop...: top stack has pausing activity r=" + r
3498 + " state=" + state);
3499 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3500 "validateTop...: activity in front not resumed r=" + r
3501 + " state=" + state);
3502 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003503 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003504 final ActivityRecord resumed = stack.mResumedActivity;
3505 if (resumed != null && resumed == r) Slog.e(TAG,
3506 "validateTop...: back stack has resumed activity r=" + r
3507 + " state=" + state);
3508 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3509 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003510 }
3511 }
3512 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003513 }
3514
Craig Mautnere0570202015-05-13 13:06:11 -07003515 private String lockTaskModeToString() {
3516 switch (mLockTaskModeState) {
3517 case LOCK_TASK_MODE_LOCKED:
3518 return "LOCKED";
3519 case LOCK_TASK_MODE_PINNED:
3520 return "PINNED";
3521 case LOCK_TASK_MODE_NONE:
3522 return "NONE";
3523 default: return "unknown=" + mLockTaskModeState;
3524 }
3525 }
3526
Craig Mautner27084302013-03-25 08:05:25 -07003527 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003528 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003529 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003530 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003531 pw.print(prefix);
3532 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003533 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003534 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003535 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003536 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3537 if (packages.size() > 0) {
3538 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3539 for (int i = 0; i < packages.size(); ++i) {
3540 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3541 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3542 }
3543 }
Bryce Lee4a194382017-04-04 14:32:48 -07003544 if (!mWaitingForActivityVisible.isEmpty()) {
3545 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3546 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3547 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3548 }
3549 }
3550
Jorim Jaggi8d786932016-10-26 19:08:36 -07003551 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3552 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003553 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554
Winson43d1f262016-06-14 16:05:55 -07003555 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003556 * Dump all connected displays' configurations.
3557 * @param prefix Prefix to apply to each line of the dump.
3558 */
3559 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3560 pw.print(prefix); pw.println("Display override configurations:");
3561 final int displayCount = mActivityDisplays.size();
3562 for (int i = 0; i < displayCount; i++) {
3563 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3564 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3565 pw.println(activityDisplay.getOverrideConfiguration());
3566 }
3567 }
3568
3569 /**
Winson43d1f262016-06-14 16:05:55 -07003570 * Dumps the activities matching the given {@param name} in the either the focused stack
3571 * or all visible stacks if {@param dumpVisibleStacks} is true.
3572 */
Winson Chung6998bc42017-02-28 17:07:05 -08003573 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3574 boolean dumpFocusedStackOnly) {
3575 if (dumpFocusedStackOnly) {
3576 return mFocusedStack.getDumpActivitiesLocked(name);
3577 } else {
Winson43d1f262016-06-14 16:05:55 -07003578 ArrayList<ActivityRecord> activities = new ArrayList<>();
3579 int numDisplays = mActivityDisplays.size();
3580 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3581 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3582 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3583 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003584 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003585 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003586 activities.addAll(stack.getDumpActivitiesLocked(name));
3587 }
3588 }
3589 }
3590 return activities;
Winson43d1f262016-06-14 16:05:55 -07003591 }
Craig Mautner20e72272013-04-01 13:45:53 -07003592 }
3593
Dianne Hackborn390517b2013-05-30 15:03:32 -07003594 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3595 boolean needSep, String prefix) {
3596 if (activity != null) {
3597 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3598 if (needSep) {
3599 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003600 }
3601 pw.print(prefix);
3602 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003603 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003604 }
3605 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003606 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003607 }
3608
Craig Mautner8d341ef2013-03-26 09:03:27 -07003609 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3610 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003611 boolean printed = false;
3612 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003613 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3614 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003615 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003616 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003617 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003618 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003619 final ActivityStack stack = stacks.get(stackNdx);
3620 StringBuilder stackHeader = new StringBuilder(128);
3621 stackHeader.append(" Stack #");
3622 stackHeader.append(stack.mStackId);
3623 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003624 stackHeader.append("\n");
3625 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3626 stackHeader.append("\n");
3627 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003628
3629 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3630 dumpClient, dumpPackage, needSep, stackHeader.toString());
3631 printed |= printedStackHeader;
3632 if (!printedStackHeader) {
3633 // Ensure we always dump the stack header even if there are no activities
3634 pw.println();
3635 pw.println(stackHeader);
3636 }
3637
Craig Mautner4a1cb222013-12-04 16:14:06 -08003638 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3639 !dumpAll, false, dumpPackage, true,
3640 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003641
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642 needSep = printed;
3643 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3644 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003645 if (pr) {
3646 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003647 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003648 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3650 " mResumedActivity: ");
3651 if (pr) {
3652 printed = true;
3653 needSep = false;
3654 }
3655 if (dumpAll) {
3656 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3657 " mLastPausedActivity: ");
3658 if (pr) {
3659 printed = true;
3660 needSep = true;
3661 }
3662 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3663 needSep, " mLastNoHistoryActivity: ");
3664 }
3665 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003666 }
3667 }
3668
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003669 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3670 false, dumpPackage, true, " Activities waiting to finish:", null);
3671 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3672 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003673 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3674 false, !dumpAll, false, dumpPackage, true,
3675 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003676 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3677 false, dumpPackage, true, " Activities waiting to sleep:", null);
3678 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3679 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003680
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003681 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003682 }
3683
Dianne Hackborn390517b2013-05-30 15:03:32 -07003684 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003685 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003686 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003687 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003688 String innerPrefix = null;
3689 String[] args = null;
3690 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003691 for (int i=list.size()-1; i>=0; i--) {
3692 final ActivityRecord r = list.get(i);
3693 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3694 continue;
3695 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003696 if (innerPrefix == null) {
3697 innerPrefix = prefix + " ";
3698 args = new String[0];
3699 }
3700 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003701 final boolean full = !brief && (complete || !r.isInHistory());
3702 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003703 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003704 needNL = false;
3705 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003706 if (header1 != null) {
3707 pw.println(header1);
3708 header1 = null;
3709 }
3710 if (header2 != null) {
3711 pw.println(header2);
3712 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003713 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003714 if (lastTask != r.getTask()) {
3715 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003716 pw.print(prefix);
3717 pw.print(full ? "* " : " ");
3718 pw.println(lastTask);
3719 if (full) {
3720 lastTask.dump(pw, prefix + " ");
3721 } else if (complete) {
3722 // Complete + brief == give a summary. Isn't that obvious?!?
3723 if (lastTask.intent != null) {
3724 pw.print(prefix); pw.print(" ");
3725 pw.println(lastTask.intent.toInsecureStringWithClip());
3726 }
3727 }
3728 }
3729 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3730 pw.print(" #"); pw.print(i); pw.print(": ");
3731 pw.println(r);
3732 if (full) {
3733 r.dump(pw, innerPrefix);
3734 } else if (complete) {
3735 // Complete + brief == give a summary. Isn't that obvious?!?
3736 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3737 if (r.app != null) {
3738 pw.print(innerPrefix); pw.println(r.app);
3739 }
3740 }
3741 if (client && r.app != null && r.app.thread != null) {
3742 // flush anything that is already in the PrintWriter since the thread is going
3743 // to write to the file descriptor directly
3744 pw.flush();
3745 try {
3746 TransferPipe tp = new TransferPipe();
3747 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003748 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003749 // Short timeout, since blocking here can
3750 // deadlock with the application.
3751 tp.go(fd, 2000);
3752 } finally {
3753 tp.kill();
3754 }
3755 } catch (IOException e) {
3756 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3757 } catch (RemoteException e) {
3758 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3759 }
3760 needNL = true;
3761 }
3762 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003763 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003764 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003765
Craig Mautnerf3333272013-04-22 10:55:53 -07003766 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003767 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3768 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003769 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3770 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003771 }
3772
3773 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003774 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003775 }
3776
3777 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003778 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3779 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003780 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3781 }
3782
Craig Mautner05d29032013-05-03 13:40:13 -07003783 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003784 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3785 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3786 }
Craig Mautner05d29032013-05-03 13:40:13 -07003787 }
3788
Craig Mautner0eea92c2013-05-16 13:35:39 -07003789 void removeSleepTimeouts() {
3790 mSleepTimeout = false;
3791 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3792 }
3793
3794 final void scheduleSleepTimeout() {
3795 removeSleepTimeouts();
3796 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3797 }
3798
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 @Override
3800 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003801 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3803 }
3804
3805 @Override
3806 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003807 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003808 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3809 }
3810
3811 @Override
3812 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003813 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003814 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3815 }
3816
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003817 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003818 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003820 newDisplay = mActivityDisplays.get(displayId) == null;
3821 if (newDisplay) {
3822 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003823 if (activityDisplay.mDisplay == null) {
3824 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3825 return;
3826 }
Craig Mautner4504de52013-12-20 09:06:56 -08003827 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003828 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003829 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003830 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003832 }
3833
Andrii Kulianca007a62016-11-22 16:33:28 -08003834 /** Check if display with specified id is added to the list. */
3835 boolean isDisplayAdded(int displayId) {
3836 return mActivityDisplays.get(displayId) != null;
3837 }
3838
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003839 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003840 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003841 mService.mContext.getResources().getDimensionPixelSize(
3842 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003843 }
3844
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003845 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003846 if (displayId == DEFAULT_DISPLAY) {
3847 throw new IllegalArgumentException("Can't remove the primary display.");
3848 }
3849
Craig Mautner4a1cb222013-12-04 16:14:06 -08003850 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003851 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3852 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003853 final boolean destroyContentOnRemoval
3854 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003855 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3856 while (!stacks.isEmpty()) {
3857 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003858 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003859 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3860 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003861 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003862 } else {
3863 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3864 // a valid launch stack for all activities. This way the task history from
3865 // external display will be preserved on primary after move.
3866 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003867 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003868 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003869 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003870 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 }
3872 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003873 }
3874
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003875 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003876 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003877 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3878 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003879 // TODO: Update the bounds.
3880 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003881 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003882 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 }
3884
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003885 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003886 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003887 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003888 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003889 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003890 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003891 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003892 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003893 info.position = display != null
3894 ? display.mStacks.indexOf(stack)
3895 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896
3897 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3898 final int numTasks = tasks.size();
3899 int[] taskIds = new int[numTasks];
3900 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003901 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003902 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 for (int i = 0; i < numTasks; ++i) {
3904 final TaskRecord task = tasks.get(i);
3905 taskIds[i] = task.taskId;
3906 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3907 : task.realActivity != null ? task.realActivity.flattenToString()
3908 : task.getTopActivity() != null ? task.getTopActivity().packageName
3909 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003910 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003911 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003912 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 }
3914 info.taskIds = taskIds;
3915 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003916 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003917 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003918
3919 final ActivityRecord top = stack.topRunningActivityLocked();
3920 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 return info;
3922 }
3923
3924 StackInfo getStackInfoLocked(int stackId) {
3925 ActivityStack stack = getStack(stackId);
3926 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003927 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003928 }
3929 return null;
3930 }
3931
3932 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003933 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003934 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3935 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003937 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 }
3939 }
3940 return list;
3941 }
3942
Craig Mautner15df08a2015-04-01 12:17:18 -07003943 TaskRecord getLockedTaskLocked() {
3944 final int top = mLockTaskModeTasks.size() - 1;
3945 if (top >= 0) {
3946 return mLockTaskModeTasks.get(top);
3947 }
3948 return null;
3949 }
3950
3951 boolean isLockedTask(TaskRecord task) {
3952 return mLockTaskModeTasks.contains(task);
3953 }
3954
3955 boolean isLastLockedTask(TaskRecord task) {
3956 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3957 }
3958
3959 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003960 if (!mLockTaskModeTasks.remove(task)) {
3961 return;
3962 }
3963 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3964 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003965 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003966 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3967 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003968 final Message lockTaskMsg = Message.obtain();
3969 lockTaskMsg.arg1 = task.userId;
3970 lockTaskMsg.what = LOCK_TASK_END_MSG;
3971 mHandler.sendMessage(lockTaskMsg);
3972 }
3973 }
3974
Andrii Kulianc27916642016-04-12 17:59:27 -07003975 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3976 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3977 false /* forceNonResizable */);
3978 }
3979
Jorim Jaggid53f0922016-04-06 22:16:23 -07003980 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003981 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003982 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3983 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003984 return;
3985 }
3986
Jorim Jaggicd13d332016-04-27 15:40:20 -07003987 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003988 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003989 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003990 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003991
Andrii Kulianc27916642016-04-12 17:59:27 -07003992 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3993 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003994 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003995 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003996 && !topActivity.noDisplay) {
3997 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003998 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3999 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004000 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004001 }
4002
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004003 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004004 if (mLockTaskNotify != null) {
4005 mLockTaskNotify.showToast(mLockTaskModeState);
4006 }
Jason Monka8f569c2014-07-07 12:15:21 -04004007 }
4008
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004009 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4010 if (mLockTaskModeTasks.contains(task)) {
4011 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4012 }
4013 }
4014
Craig Mautner432f64e2015-05-20 14:59:57 -07004015 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4016 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004017 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004018 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004019 final TaskRecord lockedTask = getLockedTaskLocked();
4020 if (lockedTask != null) {
4021 removeLockedTaskLocked(lockedTask);
4022 if (!mLockTaskModeTasks.isEmpty()) {
4023 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004024 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4025 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004026 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004027 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004028 return;
4029 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004030 }
Craig Mautnere0570202015-05-13 13:06:11 -07004031 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4032 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004033 return;
4034 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004035
4036 // Should have already been checked, but do it again.
4037 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004038 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4039 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004040 return;
4041 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004042 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004043 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004044 return;
4045 }
4046
4047 if (mLockTaskModeTasks.isEmpty()) {
4048 // First locktask.
4049 final Message lockTaskMsg = Message.obtain();
4050 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4051 lockTaskMsg.arg1 = task.userId;
4052 lockTaskMsg.what = LOCK_TASK_START_MSG;
4053 lockTaskMsg.arg2 = lockTaskModeState;
4054 mHandler.sendMessage(lockTaskMsg);
4055 }
4056 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004057 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4058 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004059 mLockTaskModeTasks.remove(task);
4060 mLockTaskModeTasks.add(task);
4061
4062 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004063 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004064 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004065
4066 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004067 findTaskToMoveToFrontLocked(task, 0, null, reason,
4068 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004069 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004070 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004071 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004072 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004073 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004074 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004075 }
4076
4077 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004078 return isLockTaskModeViolation(task, false);
4079 }
4080
4081 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4082 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004083 return false;
4084 }
4085 final int lockTaskAuth = task.mLockTaskAuth;
4086 switch (lockTaskAuth) {
4087 case LOCK_TASK_AUTH_DONT_LOCK:
4088 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004089 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004090 case LOCK_TASK_AUTH_LAUNCHABLE:
4091 case LOCK_TASK_AUTH_WHITELISTED:
4092 return false;
4093 case LOCK_TASK_AUTH_PINNABLE:
4094 // Pinnable tasks can't be launched on top of locktask tasks.
4095 return !mLockTaskModeTasks.isEmpty();
4096 default:
4097 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4098 return true;
4099 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004100 }
4101
Craig Mautner15df08a2015-04-01 12:17:18 -07004102 void onLockTaskPackagesUpdatedLocked() {
4103 boolean didSomething = false;
4104 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4105 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004106 final boolean wasWhitelisted =
4107 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4108 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004109 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004110 final boolean isWhitelisted =
4111 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4112 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4113 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004114 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004115 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4116 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004117 removeLockedTaskLocked(lockedTask);
4118 lockedTask.performClearTaskLocked();
4119 didSomething = true;
4120 }
4121 }
4122 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4123 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4124 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4125 final ActivityStack stack = stacks.get(stackNdx);
4126 stack.onLockTaskPackagesUpdatedLocked();
4127 }
4128 }
Craig Mautnere0570202015-05-13 13:06:11 -07004129 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004130 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004131 if (mLockTaskModeTasks.isEmpty() && task != null
4132 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4133 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004134 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4135 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4136 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4137 false);
4138 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004139 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004140 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004141 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004142 }
4143 }
4144
Benjamin Franz43261142015-02-11 15:59:44 +00004145 int getLockTaskModeState() {
4146 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004147 }
4148
Jorim Jaggife89d122015-12-22 16:28:44 +01004149 void activityRelaunchedLocked(IBinder token) {
4150 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004151 if (mService.isSleepingOrShuttingDownLocked()) {
4152 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4153 if (r != null) {
4154 r.setSleeping(true, true);
4155 }
4156 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004157 }
4158
4159 void activityRelaunchingLocked(ActivityRecord r) {
4160 mWindowManager.notifyAppRelaunching(r.appToken);
4161 }
4162
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004163 void logStackState() {
4164 mActivityMetricsLogger.logWindowState();
4165 }
4166
Winson Chung5af42fc2017-03-24 17:11:33 -07004167 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004168 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4169 final ActivityRecord r = task.mActivities.get(i);
4170 if (r.app != null && r.app.thread != null) {
4171 mMultiWindowModeChangedActivities.add(r);
4172 }
4173 }
4174
4175 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4176 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4177 }
4178 }
4179
Winson Chung5af42fc2017-03-24 17:11:33 -07004180 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004181 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004182 if (prevStack == null || prevStack == stack
4183 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4184 return;
4185 }
4186
Winson Chung5af42fc2017-03-24 17:11:33 -07004187 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4188 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004189
Winson Chung5af42fc2017-03-24 17:11:33 -07004190 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4191 boolean immediate) {
4192
4193 if (immediate) {
4194 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4195 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4196 final ActivityRecord r = task.mActivities.get(i);
4197 if (r.app != null && r.app.thread != null) {
4198 r.updatePictureInPictureMode(targetStackBounds);
4199 }
4200 }
4201 } else {
4202 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4203 final ActivityRecord r = task.mActivities.get(i);
4204 if (r.app != null && r.app.thread != null) {
4205 mPipModeChangedActivities.add(r);
4206 }
4207 }
4208 mPipModeChangedTargetStackBounds = targetStackBounds;
4209
4210 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4211 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4212 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004213 }
4214 }
4215
Tony Mak853304c2016-04-18 15:17:41 +01004216 void setDockedStackMinimized(boolean minimized) {
4217 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004218 }
4219
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004220 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004221
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004222 public ActivityStackSupervisorHandler(Looper looper) {
4223 super(looper);
4224 }
4225
Winson Chung4dabf232017-01-25 13:25:22 -08004226 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004227 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004228 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4229 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004230 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004231 }
4232
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004233 @Override
4234 public void handleMessage(Message msg) {
4235 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004236 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4237 synchronized (mService) {
4238 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4239 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004240 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004241 }
4242 }
4243 } break;
4244 case REPORT_PIP_MODE_CHANGED_MSG: {
4245 synchronized (mService) {
4246 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4247 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004248 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004249 }
4250 }
4251 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004252 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004253 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4254 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004255 if (mService.mDidDexOpt) {
4256 mService.mDidDexOpt = false;
4257 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4258 nmsg.obj = msg.obj;
4259 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4260 return;
4261 }
4262 // We don't at this point know if the activity is fullscreen,
4263 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004264 activityIdleInternal((ActivityRecord) msg.obj,
4265 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004266 } break;
4267 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004268 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004269 activityIdleInternal((ActivityRecord) msg.obj,
4270 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004271 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004272 case RESUME_TOP_ACTIVITY_MSG: {
4273 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004274 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004275 }
4276 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004277 case SLEEP_TIMEOUT_MSG: {
4278 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004279 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004280 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4281 mSleepTimeout = true;
4282 checkReadyForSleepLocked();
4283 }
4284 }
4285 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004286 case LAUNCH_TIMEOUT_MSG: {
4287 if (mService.mDidDexOpt) {
4288 mService.mDidDexOpt = false;
4289 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4290 return;
4291 }
4292 synchronized (mService) {
4293 if (mLaunchingActivity.isHeld()) {
4294 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4295 if (VALIDATE_WAKE_LOCK_CALLER
4296 && Binder.getCallingUid() != Process.myUid()) {
4297 throw new IllegalStateException("Calling must be system uid");
4298 }
4299 mLaunchingActivity.release();
4300 }
4301 }
4302 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004304 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004305 } break;
4306 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004307 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004308 } break;
4309 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004310 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004311 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004312 case CONTAINER_CALLBACK_VISIBILITY: {
4313 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004314 final IActivityContainerCallback callback = container.mCallback;
4315 if (callback != null) {
4316 try {
4317 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4318 } catch (RemoteException e) {
4319 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004320 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004321 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004322 case LOCK_TASK_START_MSG: {
4323 // When lock task starts, we disable the status bars.
4324 try {
Jason Monk62515be2014-05-21 16:06:19 -04004325 if (mLockTaskNotify == null) {
4326 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004327 }
Jason Monk62515be2014-05-21 16:06:19 -04004328 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004329 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004330 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004331 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004332 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004333 flags = StatusBarManager.DISABLE_MASK
4334 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004335 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004336 flags = StatusBarManager.DISABLE_MASK
4337 & (~StatusBarManager.DISABLE_BACK)
4338 & (~StatusBarManager.DISABLE_HOME)
4339 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004340 }
4341 getStatusBarService().disable(flags, mToken,
4342 mService.mContext.getPackageName());
4343 }
4344 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004345 if (getDevicePolicyManager() != null) {
4346 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004347 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004348 }
justinzhang5286d3f2014-05-12 17:06:01 -04004349 } catch (RemoteException ex) {
4350 throw new RuntimeException(ex);
4351 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004352 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004353 case LOCK_TASK_END_MSG: {
4354 // When lock task ends, we enable the status bars.
4355 try {
Jason Monk62515be2014-05-21 16:06:19 -04004356 if (getStatusBarService() != null) {
4357 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4358 mService.mContext.getPackageName());
4359 }
4360 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004361 if (getDevicePolicyManager() != null) {
4362 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4363 msg.arg1);
4364 }
Jason Monk62515be2014-05-21 16:06:19 -04004365 if (mLockTaskNotify == null) {
4366 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4367 }
4368 mLockTaskNotify.show(false);
4369 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004370 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004371 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004372 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004373 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004374 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004375 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004376 new LockPatternUtils(mService.mContext)
4377 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004378 }
4379 } catch (SettingNotFoundException e) {
4380 // No setting, don't lock.
4381 }
justinzhang5286d3f2014-05-12 17:06:01 -04004382 } catch (RemoteException ex) {
4383 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004384 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004385 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004386 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004387 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004388 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4389 if (mLockTaskNotify == null) {
4390 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4391 }
4392 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4393 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004394 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4395 final ActivityContainer container = (ActivityContainer) msg.obj;
4396 final IActivityContainerCallback callback = container.mCallback;
4397 if (callback != null) {
4398 try {
4399 callback.onAllActivitiesComplete(container.asBinder());
4400 } catch (RemoteException e) {
4401 }
4402 }
4403 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004404 case LAUNCH_TASK_BEHIND_COMPLETE: {
4405 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004406 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004407 if (r != null) {
4408 handleLaunchTaskBehindCompleteLocked(r);
4409 }
4410 }
4411 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004412
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004413 }
4414 }
4415 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004416
Ying Wangb081a592014-04-22 15:20:16 -07004417 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004418 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4419 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004420 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004421 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004422 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004423 ActivityRecord mParentActivity = null;
4424 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004425
Craig Mautnere3a00d72014-04-16 08:31:19 -07004426 boolean mVisible = true;
4427
Craig Mautner4a1cb222013-12-04 16:14:06 -08004428 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004429 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004430
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004431 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4432 final static int CONTAINER_STATE_NO_SURFACE = 1;
4433 final static int CONTAINER_STATE_FINISHING = 2;
4434 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4435
Wale Ogunwale1666e312016-12-16 11:27:18 -08004436 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004437 synchronized (mService) {
4438 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004439 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004440 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004441
4442 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004443 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004445 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004446
Bryce Leeaf691c02017-03-20 14:20:22 -07004447 protected void createStack(int stackId, boolean onTop) {
4448 switch (stackId) {
4449 case PINNED_STACK_ID:
4450 new PinnedActivityStack(this, mRecentTasks, onTop);
4451 break;
4452 default:
4453 new ActivityStack(this, mRecentTasks, onTop);
4454 break;
4455 }
4456 }
4457
Andrii Kulian839def92016-11-02 10:58:58 -07004458 /**
4459 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004460 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004461 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004462 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004463 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004464 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004465 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004466 if (mActivityDisplay != null) {
4467 throw new IllegalStateException("ActivityContainer is already attached, " +
4468 "displayId=" + mActivityDisplay.mDisplayId);
4469 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004470 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004471 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 }
4473
4474 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004475 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004476 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004477 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4478 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004479 return;
4480 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004481 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004482 }
4483 }
4484
4485 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004486 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004487 synchronized (mService) {
4488 if (mActivityDisplay != null) {
4489 return mActivityDisplay.mDisplayId;
4490 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004491 }
4492 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004493 }
4494
Jeff Brownca9bc702014-02-11 14:32:56 -08004495 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004496 public int getStackId() {
4497 synchronized (mService) {
4498 return mStackId;
4499 }
4500 }
4501
4502 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004503 public boolean injectEvent(InputEvent event) {
4504 final long origId = Binder.clearCallingIdentity();
4505 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004506 synchronized (mService) {
4507 if (mActivityDisplay != null) {
4508 return mInputManagerInternal.injectInputEvent(event,
4509 mActivityDisplay.mDisplayId,
4510 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4511 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004512 }
4513 return false;
4514 } finally {
4515 Binder.restoreCallingIdentity(origId);
4516 }
4517 }
4518
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004519 @Override
4520 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004521 synchronized (mService) {
4522 if (mContainerState == CONTAINER_STATE_FINISHING) {
4523 return;
4524 }
4525 mContainerState = CONTAINER_STATE_FINISHING;
4526
Craig Mautnerd163e752014-06-13 17:18:47 -07004527 long origId = Binder.clearCallingIdentity();
4528 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004529 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004530 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004531 } finally {
4532 Binder.restoreCallingIdentity(origId);
4533 }
4534 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004535 }
4536
Andrii Kulian03c403d2016-11-11 11:14:12 -08004537 /**
4538 * Remove the stack completely. Must be called only when there are no tasks left in it,
4539 * as this method does not finish running activities.
4540 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004541 void removeLocked() {
4542 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004543 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004544 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004545 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004547 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004548 }
4549
Andrii Kulian839def92016-11-02 10:58:58 -07004550 /**
4551 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4552 * completely or re-parented.
4553 */
4554 private void removeFromDisplayLocked() {
4555 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4556 + " current displayId=" + mActivityDisplay.mDisplayId);
4557
Wale Ogunwale1666e312016-12-16 11:27:18 -08004558 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004559 mActivityDisplay = null;
4560 }
4561
4562 /**
4563 * Move the stack to specified display.
4564 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004565 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004566 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004567 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004568 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4569 + mActivityDisplay + " to display=" + activityDisplay
4570 + " Callers=" + Debug.getCallers(2));
4571
4572 removeFromDisplayLocked();
4573
4574 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004575 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004576 }
4577
Craig Mautner4a1cb222013-12-04 16:14:06 -08004578 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004579 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004580 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004581 }
4582
4583 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004584 public final int startActivityIntentSender(IIntentSender intentSender)
4585 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004586 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4587
4588 if (!(intentSender instanceof PendingIntentRecord)) {
4589 throw new IllegalArgumentException("Bad PendingIntent object");
4590 }
4591
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004592 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004593 Binder.getCallingUid(), mCurrentUser, false,
4594 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004595
4596 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4597 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4598 pendingIntent.key.requestResolvedType);
4599
4600 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4601 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4602 }
4603
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004604 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004605 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004606 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004607 throw new SecurityException(
4608 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4609 }
4610 }
4611
Craig Mautnerdf88d732014-01-27 09:21:32 -08004612 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004613 public IBinder asBinder() {
4614 return this;
4615 }
4616
Craig Mautner4504de52013-12-20 09:06:56 -08004617 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004618 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004619 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004620 }
4621
Craig Mautner4a1cb222013-12-04 16:14:06 -08004622 ActivityStackSupervisor getOuter() {
4623 return ActivityStackSupervisor.this;
4624 }
4625
Craig Mautnerd163e752014-06-13 17:18:47 -07004626 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004627 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004628 }
4629
Craig Mautner6985bad2014-04-21 15:22:06 -07004630 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004631 void setVisible(boolean visible) {
4632 if (mVisible != visible) {
4633 mVisible = visible;
4634 if (mCallback != null) {
4635 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4636 0 /* unused */, this).sendToTarget();
4637 }
4638 }
4639 }
4640
Craig Mautner6985bad2014-04-21 15:22:06 -07004641 void setDrawn() {
4642 }
4643
Craig Mautner1b4bf852014-05-26 15:06:32 -07004644 // You can always start a new task on a regular ActivityStack.
4645 boolean isEligibleForNewTasks() {
4646 return true;
4647 }
4648
Craig Mautnerd163e752014-06-13 17:18:47 -07004649 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004650 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004651 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004652 }
4653
Craig Mautner34b73df2014-01-12 21:11:08 -08004654 @Override
4655 public String toString() {
4656 return mIdString + (mActivityDisplay == null ? "N" : "A");
4657 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004658 }
4659
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004660 private class VirtualActivityContainer extends ActivityContainer {
4661 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004662 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004663
4664 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004665 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4666 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004667 mParentActivity = parent;
4668 mCallback = callback;
4669 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004670 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004671 }
4672
4673 @Override
4674 public void setSurface(Surface surface, int width, int height, int density) {
4675 super.setSurface(surface, width, height, density);
4676
4677 synchronized (mService) {
4678 final long origId = Binder.clearCallingIdentity();
4679 try {
4680 setSurfaceLocked(surface, width, height, density);
4681 } finally {
4682 Binder.restoreCallingIdentity(origId);
4683 }
4684 }
4685 }
4686
4687 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4688 if (mContainerState == CONTAINER_STATE_FINISHING) {
4689 return;
4690 }
4691 VirtualActivityDisplay virtualActivityDisplay =
4692 (VirtualActivityDisplay) mActivityDisplay;
4693 if (virtualActivityDisplay == null) {
4694 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004695 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696 mActivityDisplay = virtualActivityDisplay;
4697 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004698 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004699 }
4700
4701 if (mSurface != null) {
4702 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004703 }
4704
Craig Mautner6985bad2014-04-21 15:22:06 -07004705 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004707 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004708 } else {
4709 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004710 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004711 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004712 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004713 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004714 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004715
Craig Mautnerd163e752014-06-13 17:18:47 -07004716 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004717
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004718 if (DEBUG_STACK) Slog.d(TAG_STACK,
4719 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004720 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004721
Craig Mautner6985bad2014-04-21 15:22:06 -07004722 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004723 boolean isAttachedLocked() {
4724 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004725 }
4726
4727 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004728 void setDrawn() {
4729 synchronized (mService) {
4730 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004731 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004732 }
4733 }
4734
Craig Mautner1b4bf852014-05-26 15:06:32 -07004735 // Never start a new task on an ActivityView if it isn't explicitly specified.
4736 @Override
4737 boolean isEligibleForNewTasks() {
4738 return false;
4739 }
4740
Craig Mautnerd163e752014-06-13 17:18:47 -07004741 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004742 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004743 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4744 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4745 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4746 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4747 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004748 }
4749 }
4750
Craig Mautner4a1cb222013-12-04 16:14:06 -08004751 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4752 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004753 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004754 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004755 int mDisplayId;
4756 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004757
Craig Mautner4a1cb222013-12-04 16:14:06 -08004758 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4759 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004760 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004761
Jose Lima4b6c6692014-08-12 17:41:12 -07004762 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004763
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004764 /** Array of all UIDs that are present on the display. */
4765 private IntArray mDisplayAccessUIDs = new IntArray();
4766
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767 ActivityDisplay() {
4768 }
Craig Mautner4504de52013-12-20 09:06:56 -08004769
Craig Mautner1a70a162014-09-13 12:09:31 -07004770 // After instantiation, check that mDisplay is not null before using this. The alternative
4771 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004772 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004773 final Display display = mDisplayManager.getDisplay(displayId);
4774 if (display == null) {
4775 return;
4776 }
4777 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004778 }
4779
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004780 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004781 mDisplay = display;
4782 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004783 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004784
Winson Chung7c3ede32017-03-14 14:18:34 -07004785 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004786 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004787 + " to displayId=" + mDisplayId + " position=" + position);
4788 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004789 }
4790
Wale Ogunwale1666e312016-12-16 11:27:18 -08004791 void detachStack(ActivityStack stack) {
4792 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004793 + " from displayId=" + mDisplayId);
4794 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004795 }
4796
Jose Lima4b6c6692014-08-12 17:41:12 -07004797 void setVisibleBehindActivity(ActivityRecord r) {
4798 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004799 }
4800
Jose Lima4b6c6692014-08-12 17:41:12 -07004801 boolean hasVisibleBehindActivity() {
4802 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004803 }
4804
Craig Mautner34b73df2014-01-12 21:11:08 -08004805 @Override
4806 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004807 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4808 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004809
4810 @Override
4811 protected int getChildCount() {
4812 return mStacks.size();
4813 }
4814
4815 @Override
4816 protected ConfigurationContainer getChildAt(int index) {
4817 return mStacks.get(index);
4818 }
4819
4820 @Override
4821 protected ConfigurationContainer getParent() {
4822 return ActivityStackSupervisor.this;
4823 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004824
4825 boolean isPrivate() {
4826 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4827 }
4828
4829 boolean isUidPresent(int uid) {
4830 for (ActivityStack stack : mStacks) {
4831 if (stack.isUidPresent(uid)) {
4832 return true;
4833 }
4834 }
4835 return false;
4836 }
4837
4838 /** Update and get all UIDs that are present on the display and have access to it. */
4839 private IntArray getPresentUIDs() {
4840 mDisplayAccessUIDs.clear();
4841 for (ActivityStack stack : mStacks) {
4842 stack.getPresentUIDs(mDisplayAccessUIDs);
4843 }
4844 return mDisplayAccessUIDs;
4845 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004846
4847 boolean shouldDestroyContentOnRemove() {
4848 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4849 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004850 }
4851
4852 class VirtualActivityDisplay extends ActivityDisplay {
4853 VirtualDisplay mVirtualDisplay;
4854
Craig Mautner6985bad2014-04-21 15:22:06 -07004855 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004856 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004857 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4858 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4859 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4860 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004861
4862 init(mVirtualDisplay.getDisplay());
4863
Bryce Leeaf3a4002017-03-20 10:37:12 -07004864 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004865 }
4866
4867 void setSurface(Surface surface) {
4868 if (mVirtualDisplay != null) {
4869 mVirtualDisplay.setSurface(surface);
4870 }
4871 }
4872
4873 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004874 void detachStack(ActivityStack stack) {
4875 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004876 if (mVirtualDisplay != null) {
4877 mVirtualDisplay.release();
4878 mVirtualDisplay = null;
4879 }
4880 }
4881
4882 @Override
4883 public String toString() {
4884 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004885 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004886 }
Jose Lima58e66d62014-05-27 20:00:27 -07004887
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004888 ActivityStack findStackBehind(ActivityStack stack) {
4889 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004890 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004891 if (display == null) {
4892 return null;
4893 }
4894 final ArrayList<ActivityStack> stacks = display.mStacks;
4895 for (int i = stacks.size() - 1; i >= 0; i--) {
4896 if (stacks.get(i) == stack && i > 0) {
4897 return stacks.get(i - 1);
4898 }
4899 }
4900 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4901 + " in=" + stacks);
4902 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004903
Jorim Jaggic69bd222016-03-15 14:38:37 +01004904 /**
4905 * Puts a task into resizing mode during the next app transition.
4906 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004907 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004908 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004909 private void setResizingDuringAnimation(TaskRecord task) {
4910 mResizingTasksDuringAnimation.add(task.taskId);
4911 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004912 }
4913
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004914 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4915 final TaskRecord task;
4916 final int callingUid;
4917 final String callingPackage;
4918 final Intent intent;
4919 final int userId;
4920 final ActivityOptions activityOptions = (bOptions != null)
4921 ? new ActivityOptions(bOptions) : null;
4922 final int launchStackId = (activityOptions != null)
4923 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004924 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004925 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004926 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004927 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004928
4929 if (launchStackId == DOCKED_STACK_ID) {
4930 mWindowManager.setDockedStackCreateState(
4931 activityOptions.getDockCreateMode(), null /* initialBounds */);
4932
4933 // Defer updating the stack in which recents is until the app transition is done, to
4934 // not run into issues where we still need to draw the task in recents but the
4935 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004936 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004937 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4938 }
4939
Winson Chung7900bee2017-03-10 08:59:25 -08004940 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4941 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004942 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004943 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004944 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004945 throw new IllegalArgumentException(
4946 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4947 }
4948
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004949 // Since we don't have an actual source record here, we assume that the currently focused
4950 // activity was the source.
4951 final ActivityStack focusedStack = getFocusedStack();
4952 final ActivityRecord sourceRecord =
4953 focusedStack != null ? focusedStack.topActivity() : null;
4954
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004955 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004956 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004957 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4958 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004959 }
4960 }
4961
4962 // If the user must confirm credentials (e.g. when first launching a work app and the
4963 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4964 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4965 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004966 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004967 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004968 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004969 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4970 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004971
4972 // If we are launching the task in the docked stack, put it into resizing mode so
4973 // the window renders full-screen with the background filling the void. Also only
4974 // call this at the end to make sure that tasks exists on the window manager side.
4975 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004976 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004977 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004978
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004979 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004980 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004981 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004982 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004983 return ActivityManager.START_TASK_TO_FRONT;
4984 }
4985 callingUid = task.mCallingUid;
4986 callingPackage = task.mCallingPackage;
4987 intent = task.intent;
4988 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4989 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004990 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4991 null, null, 0, 0, bOptions, userId, null, task);
4992 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004993 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004994 }
4995 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004996 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004997
4998 /**
4999 * @return a list of activities which are the top ones in each visible stack. The first
5000 * entry will be the focused activity.
5001 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005002 List<IBinder> getTopVisibleActivities() {
5003 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5004 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005005 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005006 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5007 // Traverse all stacks on a display.
5008 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5009 final ActivityStack stack = display.mStacks.get(j);
5010 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005011 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005012 == ActivityStack.STACK_VISIBLE) {
5013 final ActivityRecord top = stack.topActivity();
5014 if (top != null) {
5015 if (stack == mFocusedStack) {
5016 topActivityTokens.add(0, top.appToken);
5017 } else {
5018 topActivityTokens.add(top.appToken);
5019 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005020 }
5021 }
5022 }
5023 }
5024 return topActivityTokens;
5025 }
Bryce Lee4a194382017-04-04 14:32:48 -07005026
5027 /**
5028 * Internal container to store a match qualifier alongside a WaitResult.
5029 */
5030 static class WaitInfo {
5031 private final ComponentName mTargetComponent;
5032 private final WaitResult mResult;
5033
5034 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5035 this.mTargetComponent = targetComponent;
5036 this.mResult = result;
5037 }
5038
5039 public boolean matches(ActivityRecord record) {
5040 return mTargetComponent == null ||
5041 (TextUtils.equals(mTargetComponent.getPackageName(), record.info.packageName)
5042 && TextUtils.equals(mTargetComponent.getClassName(), record.info.name));
5043 }
5044
5045 public WaitResult getResult() {
5046 return mResult;
5047 }
5048
5049 public ComponentName getComponent() {
5050 return mTargetComponent;
5051 }
5052
5053 public void dump(PrintWriter pw, String prefix) {
5054 pw.println(prefix + "WaitInfo:");
5055 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5056 pw.println(prefix + " mResult=");
5057 mResult.dump(pw, prefix);
5058 }
5059 }
Craig Mautner27084302013-03-25 08:05:25 -07005060}