blob: 68e25c37dca84ee86c532f2b25be2129d1c73b98 [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 Ogunwale919a05d2017-04-13 00:36:34 +00001480 // it to pause+stop (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;
Craig Mautner2420ead2013-04-01 17:13:20 -07001485 }
1486
1487 // Launch the new version setup screen if needed. We do this -after-
1488 // launching the initial activity (that is, home), so that it can have
1489 // a chance to initialize itself while in the background, making the
1490 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001491 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001492 mService.startSetupActivityLocked();
1493 }
1494
Dianne Hackborn465fa392014-09-14 14:21:18 -07001495 // Update any services we are bound to that might care about whether
1496 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001497 if (r.app != null) {
1498 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1499 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001500
Craig Mautner2420ead2013-04-01 17:13:20 -07001501 return true;
1502 }
1503
Craig Mautnere79d42682013-04-01 19:01:53 -07001504 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001505 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001506 // Is this activity's application already running?
1507 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001508 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001509
Andrii Kulian02b7a832016-10-06 23:11:56 -07001510 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001511
1512 if (app != null && app.thread != null) {
1513 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001514 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1515 || !"android".equals(r.info.packageName)) {
1516 // Don't add this if it is a platform component that is marked
1517 // to run in multiple processes, because this is actually
1518 // part of the framework so doesn't make sense to track as a
1519 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001520 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1521 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001522 }
George Mount2c92c972014-03-20 09:38:23 -07001523 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001524 return;
1525 } catch (RemoteException e) {
1526 Slog.w(TAG, "Exception when starting activity "
1527 + r.intent.getComponent().flattenToShortString(), e);
1528 }
1529
1530 // If a dead object exception was thrown -- fall through to
1531 // restart the application.
1532 }
1533
1534 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001535 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001536 }
1537
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001538 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001539 String resultWho, int requestCode, int callingPid, int callingUid,
1540 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001541 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001542 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1543 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001544 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001545 return true;
1546 }
1547 final int componentRestriction = getComponentRestrictionForCallingPackage(
1548 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1549 final int actionRestriction = getActionRestrictionForCallingPackage(
1550 intent.getAction(), callingPackage, callingPid, callingUid);
1551 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1552 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1553 if (resultRecord != null) {
1554 resultStack.sendActivityResultLocked(-1,
1555 resultRecord, resultWho, requestCode,
1556 Activity.RESULT_CANCELED, null);
1557 }
1558 final String msg;
1559 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1560 msg = "Permission Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")" + " with revoked permission "
1563 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1564 } else if (!aInfo.exported) {
1565 msg = "Permission Denial: starting " + intent.toString()
1566 + " from " + callerApp + " (pid=" + callingPid
1567 + ", uid=" + callingUid + ")"
1568 + " not exported from uid " + aInfo.applicationInfo.uid;
1569 } else {
1570 msg = "Permission Denial: starting " + intent.toString()
1571 + " from " + callerApp + " (pid=" + callingPid
1572 + ", uid=" + callingUid + ")"
1573 + " requires " + aInfo.permission;
1574 }
1575 Slog.w(TAG, msg);
1576 throw new SecurityException(msg);
1577 }
1578
1579 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1580 final String message = "Appop Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ")"
1583 + " requires " + AppOpsManager.permissionToOp(
1584 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1585 Slog.w(TAG, message);
1586 return false;
1587 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1588 final String message = "Appop Denial: starting " + intent.toString()
1589 + " from " + callerApp + " (pid=" + callingPid
1590 + ", uid=" + callingUid + ")"
1591 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1592 Slog.w(TAG, message);
1593 return false;
1594 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001595 if (options != null) {
1596 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1597 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1598 callingPid, callingUid);
1599 if (startInTaskPerm != PERMISSION_GRANTED) {
1600 final String msg = "Permission Denial: starting " + intent.toString()
1601 + " from " + callerApp + " (pid=" + callingPid
1602 + ", uid=" + callingUid + ") with launchTaskId="
1603 + options.getLaunchTaskId();
1604 Slog.w(TAG, msg);
1605 throw new SecurityException(msg);
1606 }
1607 }
1608 // Check if someone tries to launch an activity on a private display with a different
1609 // owner.
1610 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001611 if (launchDisplayId != INVALID_DISPLAY
1612 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
1613 final String msg = "Permission Denial: starting " + intent.toString()
1614 + " from " + callerApp + " (pid=" + callingPid
1615 + ", uid=" + callingUid + ") with launchDisplayId="
1616 + launchDisplayId;
1617 Slog.w(TAG, msg);
1618 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001619 }
1620 }
1621
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001622 return true;
1623 }
1624
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001625 /** Check if caller is allowed to launch activities on specified display. */
1626 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
1627 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1628 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1629
1630 final ActivityDisplay activityDisplay = mActivityDisplays.get(launchDisplayId);
1631 if (activityDisplay == null) {
1632 Slog.w(TAG, "Launch on display check: display not found");
1633 return false;
1634 }
1635
1636 if (!activityDisplay.isPrivate()) {
1637 // Anyone can launch on a public display.
1638 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1639 + " allow launch on public display");
1640 return true;
1641 }
1642
1643 // Check if the caller is the owner of the display.
1644 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1645 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1646 + " allow launch for owner of the display");
1647 return true;
1648 }
1649
1650 // Check if caller is present on display
1651 if (activityDisplay.isUidPresent(callingUid)) {
1652 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1653 + " allow launch for caller present on the display");
1654 return true;
1655 }
1656
Andrii Kuliane6ac20e2017-03-17 10:30:50 -07001657 // Check if the caller can manage activity stacks.
1658 final int startAnyPerm = mService.checkPermission(MANAGE_ACTIVITY_STACKS, callingPid,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001659 callingUid);
1660 if (startAnyPerm == PERMISSION_GRANTED) {
1661 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1662 + " allow launch any on display");
1663 return true;
1664 }
1665
1666 Slog.w(TAG, "Launch on display check: denied");
1667 return false;
1668 }
1669
1670 /** Update lists of UIDs that are present on displays and have access to them. */
1671 void updateUIDsPresentOnDisplay() {
1672 mDisplayAccessUIDs.clear();
1673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1674 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001675 // Only bother calculating the whitelist for private displays
1676 if (activityDisplay.isPrivate()) {
1677 mDisplayAccessUIDs.append(
1678 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1679 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001680 }
1681 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1682 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1683 }
1684
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001685 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001686 final long identity = Binder.clearCallingIdentity();
1687 try {
1688 return UserManager.get(mService.mContext).getUserInfo(userId);
1689 } finally {
1690 Binder.restoreCallingIdentity(identity);
1691 }
1692 }
1693
Svet Ganov99b60432015-06-27 13:15:22 -07001694 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001695 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001696 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1697 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001698 == PackageManager.PERMISSION_DENIED) {
1699 return ACTIVITY_RESTRICTION_PERMISSION;
1700 }
1701
Christopher Tateff7add02015-08-17 10:23:22 -07001702 if (activityInfo.permission == null) {
1703 return ACTIVITY_RESTRICTION_NONE;
1704 }
1705
Svet Ganov99b60432015-06-27 13:15:22 -07001706 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1707 if (opCode == AppOpsManager.OP_NONE) {
1708 return ACTIVITY_RESTRICTION_NONE;
1709 }
1710
1711 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1712 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001713 if (!ignoreTargetSecurity) {
1714 return ACTIVITY_RESTRICTION_APPOP;
1715 }
Svet Ganov99b60432015-06-27 13:15:22 -07001716 }
1717
1718 return ACTIVITY_RESTRICTION_NONE;
1719 }
1720
Svetoslav7008b512015-06-24 18:47:07 -07001721 private int getActionRestrictionForCallingPackage(String action,
1722 String callingPackage, int callingPid, int callingUid) {
1723 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001724 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001725 }
1726
1727 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1728 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001729 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001730 }
1731
1732 final PackageInfo packageInfo;
1733 try {
1734 packageInfo = mService.mContext.getPackageManager()
1735 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1736 } catch (PackageManager.NameNotFoundException e) {
1737 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
1741 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 if (mService.checkPermission(permission, callingPid, callingUid) ==
1746 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001747 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001748 }
1749
1750 final int opCode = AppOpsManager.permissionToOpCode(permission);
1751 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001752 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001753 }
1754
1755 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1756 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001763 void setLaunchSource(int uid) {
1764 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1765 }
1766
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001767 void acquireLaunchWakelock() {
1768 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1769 throw new IllegalStateException("Calling must be system uid");
1770 }
1771 mLaunchingActivity.acquire();
1772 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1773 // To be safe, don't allow the wake lock to be held for too long.
1774 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1775 }
1776 }
1777
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001778 /**
1779 * Called when the frontmost task is idle.
1780 * @return the state of mService.mBooting before this was called.
1781 */
1782 private boolean checkFinishBootingLocked() {
1783 final boolean booting = mService.mBooting;
1784 boolean enableScreen = false;
1785 mService.mBooting = false;
1786 if (!mService.mBooted) {
1787 mService.mBooted = true;
1788 enableScreen = true;
1789 }
1790 if (booting || enableScreen) {
1791 mService.postFinishBooting(booting, enableScreen);
1792 }
1793 return booting;
1794 }
1795
Craig Mautnerf3333272013-04-22 10:55:53 -07001796 // Checked.
1797 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001798 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001799 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001800
Craig Mautnerf3333272013-04-22 10:55:53 -07001801 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001802 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001803 int NS = 0;
1804 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001805 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001806 boolean activityRemoved = false;
1807
Wale Ogunwale7d701172015-03-11 15:36:30 -07001808 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001809 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001810 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1811 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001812 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1813 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001814 if (fromTimeout) {
1815 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001817
1818 // This is a hack to semi-deal with a race condition
1819 // in the client where it can be constructed with a
1820 // newer configuration from when we asked it to launch.
1821 // We'll update with whatever configuration it now says
1822 // it used to launch.
1823 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001824 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001825 }
1826
1827 // We are now idle. If someone is waiting for a thumbnail from
1828 // us, we can now deliver.
1829 r.idle = true;
1830
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001831 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001832 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001833 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834 }
1835 }
1836
1837 if (allResumedActivitiesIdle()) {
1838 if (r != null) {
1839 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001840 }
1841
1842 if (mLaunchingActivity.isHeld()) {
1843 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1844 if (VALIDATE_WAKE_LOCK_CALLER &&
1845 Binder.getCallingUid() != Process.myUid()) {
1846 throw new IllegalStateException("Calling must be system uid");
1847 }
1848 mLaunchingActivity.release();
1849 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001850 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001851 }
1852
1853 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001854 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1855 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001856 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001857 if ((NF = mFinishingActivities.size()) > 0) {
1858 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001859 mFinishingActivities.clear();
1860 }
1861
Craig Mautnerf3333272013-04-22 10:55:53 -07001862 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001863 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001864 mStartingUsers.clear();
1865 }
1866
Craig Mautnerf3333272013-04-22 10:55:53 -07001867 // Stop any activities that are scheduled to do so but have been
1868 // waiting for the next one to start.
1869 for (int i = 0; i < NS; i++) {
1870 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001871 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001872 if (stack != null) {
1873 if (r.finishing) {
1874 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1875 } else {
1876 stack.stopActivityLocked(r);
1877 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001878 }
1879 }
1880
1881 // Finish any activities that are scheduled to do so but have been
1882 // waiting for the next one to start.
1883 for (int i = 0; i < NF; i++) {
1884 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001885 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001886 if (stack != null) {
1887 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1888 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 }
1890
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001891 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001892 // Complete user switch
1893 if (startingUsers != null) {
1894 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001895 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001896 }
1897 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 }
1899
1900 mService.trimApplications();
1901 //dump();
1902 //mWindowManager.dump();
1903
Craig Mautnerf3333272013-04-22 10:55:53 -07001904 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001905 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 }
1907
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001908 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910
Craig Mautner8e569572013-10-11 17:36:59 -07001911 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001912 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001913 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1914 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001915 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1916 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1917 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001918 }
Craig Mautner19091252013-10-05 00:03:53 -07001919 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001920 }
1921
1922 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001923 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1924 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001925 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1926 stacks.get(stackNdx).closeSystemDialogsLocked();
1927 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001928 }
1929 }
1930
Craig Mautner93529a42013-10-04 15:03:13 -07001931 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001932 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001933 }
1934
Craig Mautner8d341ef2013-03-26 09:03:27 -07001935 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001936 * Update the last used stack id for non-current user (current user's last
1937 * used stack is the focused stack)
1938 */
1939 void updateUserStackLocked(int userId, ActivityStack stack) {
1940 if (userId != mCurrentUser) {
1941 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1942 }
1943 }
1944
1945 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001946 * @return true if some activity was finished (or would have finished if doit were true).
1947 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001948 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1949 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001950 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001954 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001955 if (stack.finishDisabledPackageActivitiesLocked(
1956 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001957 didSomething = true;
1958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
1960 }
1961 return didSomething;
1962 }
1963
Dianne Hackborna413dc02013-07-12 12:02:55 -07001964 void updatePreviousProcessLocked(ActivityRecord r) {
1965 // Now that this process has stopped, we may want to consider
1966 // it to be the previous app to try to keep around in case
1967 // the user wants to return to it.
1968
1969 // First, found out what is currently the foreground app, so that
1970 // we don't blow away the previous app if this activity is being
1971 // hosted by the process that is actually still the foreground.
1972 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001973 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1974 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001975 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1976 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001977 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001978 if (stack.mResumedActivity != null) {
1979 fgApp = stack.mResumedActivity.app;
1980 } else if (stack.mPausingActivity != null) {
1981 fgApp = stack.mPausingActivity.app;
1982 }
1983 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001984 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001985 }
1986 }
1987
1988 // Now set this one as the previous process, only if that really
1989 // makes sense to.
1990 if (r.app != null && fgApp != null && r.app != fgApp
1991 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001992 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001993 mService.mPreviousProcess = r.app;
1994 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1995 }
1996 }
1997
Wale Ogunwaled046a012015-12-24 13:05:59 -08001998 boolean resumeFocusedStackTopActivityLocked() {
1999 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002000 }
2001
Wale Ogunwaled046a012015-12-24 13:05:59 -08002002 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002003 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002004 if (targetStack != null && isFocusedStack(targetStack)) {
2005 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002006 }
Wale Ogunwale06579d62016-04-30 15:29:06 -07002007 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2008 if (r == null || r.state != RESUMED) {
2009 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002010 } else if (r.state == RESUMED) {
2011 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2012 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002013 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002014 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002015 }
2016
Todd Kennedy39bfee52016-02-24 10:28:21 -08002017 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2018 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2019 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2020 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2021 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2022 }
2023 }
2024 }
2025
Adrian Roos20d7df32016-01-12 18:59:43 +01002026 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2027 TaskRecord finishedTask = null;
2028 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002029 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2030 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002031 final int numStacks = stacks.size();
2032 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2033 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002034 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2035 if (stack == focusedStack || finishedTask == null) {
2036 finishedTask = t;
2037 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002038 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002040 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002041 }
2042
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002043 void finishVoiceTask(IVoiceInteractionSession session) {
2044 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2045 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2046 final int numStacks = stacks.size();
2047 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2048 final ActivityStack stack = stacks.get(stackNdx);
2049 stack.finishVoiceTask(session);
2050 }
2051 }
2052 }
2053
Andrii Kulianc27916642016-04-12 17:59:27 -07002054 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2055 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002056 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2057 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002058 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002059 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2060 // Caller wants the home activity moved with it. To accomplish this,
2061 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002062 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002063 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002064 ActivityStack currentStack = task.getStack();
2065 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002066 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2067 + task + " to front. Stack is null");
2068 return;
2069 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002070
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002071 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002072 int stackId = options.getLaunchStackId();
2073 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002074 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002075 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002076 if (stackId == INVALID_STACK_ID) {
2077 stackId = task.getLaunchStackId();
2078 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002079 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002080 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2081 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002082 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002083 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2084 // still need moveTaskToFrontLocked() below for any transition settings.
2085 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002086 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2087 resizeStackLocked(stackId, bounds,
2088 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002089 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002090 } else {
2091 // WM resizeTask must be done after the task is moved to the correct stack,
2092 // because Task's setBounds() also updates dim layer's bounds, but that has
2093 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002094 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002095 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002096 }
2097 }
2098
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002099 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002100 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002101 r == null ? null : r.appTimeTracker, reason);
2102
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002103 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002104 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002105
Andrii Kulian02b7a832016-10-06 23:11:56 -07002106 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, currentStack.mStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07002107 forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002108 }
2109
Wale Ogunwale854809c2015-12-27 16:18:19 -08002110 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002111 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002112 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002113 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002114 return false;
2115 }
2116 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2117 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002118 }
2119
Winson Chung55893332017-02-17 17:13:10 -08002120 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002121 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002122 }
2123
Winson Chung55893332017-02-17 17:13:10 -08002124 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2125 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002126 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002127 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002128 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002129 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002130 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002131 return null;
2132 }
Matthew Ng330757d2017-02-28 14:19:17 -08002133 if (stackId == DOCKED_STACK_ID) {
2134 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2135 // the other side of the docked stack and we make visibility decisions based on that.
2136 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2137 }
Winson Chung55893332017-02-17 17:13:10 -08002138 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
2140
Andrii Kulian16802aa2016-11-02 12:21:33 -07002141 /**
2142 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2143 * If there is no such stack, new dynamic stack can be created.
2144 * @param displayId Target display.
2145 * @param r Activity that should be launched there.
2146 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2147 */
2148 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
2149 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2150 if (activityDisplay == null) {
2151 throw new IllegalArgumentException(
2152 "Display with displayId=" + displayId + " not found.");
2153 }
2154
2155 // Return the topmost valid stack on the display.
2156 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2157 final ActivityStack stack = activityDisplay.mStacks.get(i);
2158 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, r)) {
2159 return stack;
2160 }
2161 }
2162
2163 // If there is no valid stack on the external display - check if new dynamic stack will do.
2164 if (displayId != Display.DEFAULT_DISPLAY) {
2165 final int newDynamicStackId = getNextStackId();
2166 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, r)) {
2167 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2168 }
2169 }
2170
2171 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2172 return null;
2173 }
2174
Craig Mautner967212c2013-04-13 21:10:58 -07002175 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002176 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002177 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2178 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002179 }
2180 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002181 }
2182
Jorim Jaggife762342016-10-13 14:33:27 +02002183 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2184 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2185 }
2186
Andrii Kulian7fc22812016-12-28 13:04:11 -08002187 /**
2188 * Get next focusable stack in the system. This will search across displays and stacks
2189 * in last-focused order for a focusable and visible stack, different from the target stack.
2190 *
2191 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2192 * searching for next candidate.
2193 * @return Next focusable {@link ActivityStack}, null if not found.
2194 */
2195 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2196 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2197
2198 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2199 final int displayId = mTmpOrderedDisplayIds.get(i);
2200 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
2201 if (stacks == null) {
2202 continue;
2203 }
2204 for (int j = stacks.size() - 1; j >= 0; --j) {
2205 final ActivityStack stack = stacks.get(j);
2206 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002207 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002208 return stack;
2209 }
2210 }
2211 }
2212
2213 return null;
2214 }
2215
Craig Mautneree2e45a2014-06-27 12:10:03 -07002216 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002217 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002218 }
2219
2220 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002221 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2222 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2223 final TaskRecord task = tasks.get(taskNdx);
2224 if (task.isHomeTask()) {
2225 final ArrayList<ActivityRecord> activities = task.mActivities;
2226 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2227 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002228 if (r.isHomeActivity()
2229 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002230 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002231 }
2232 }
2233 }
2234 }
2235 return null;
2236 }
2237
Chong Zhangb15758a2015-11-17 12:12:03 -08002238 /**
2239 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2240 * the docked stack itself, or if it's side-by-side to the docked stack.
2241 */
2242 boolean isStackDockedInEffect(int stackId) {
2243 return stackId == DOCKED_STACK_ID ||
2244 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2245 }
2246
Todd Kennedyca4d8422015-01-15 15:19:22 -08002247 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002248 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002249 ActivityContainer activityContainer =
2250 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002251 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002252 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2253 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002254 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002255 return activityContainer;
2256 }
2257
Craig Mautner34b73df2014-01-12 21:11:08 -08002258 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002259 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2260 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2261 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002262 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2263 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002264 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002265 }
2266 }
2267
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002268 void deleteActivityContainerRecord(int stackId) {
2269 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2270 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2271 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002272 }
2273
Jorim Jaggidc249c42015-12-15 14:57:31 -08002274 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002275 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002276 if (stackId == DOCKED_STACK_ID) {
2277 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002278 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002279 return;
2280 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002281 final ActivityStack stack = getStack(stackId);
2282 if (stack == null) {
2283 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2284 return;
2285 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002286
Winson Chunga2249ac2017-03-30 18:15:30 -07002287 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2288 getStack(DOCKED_STACK_ID) != null) {
2289 // If the docked stack exists, don't resize non-floating stacks independently of the
2290 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002291 return;
2292 }
2293
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002294 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002295 mWindowManager.deferSurfaceLayout();
2296 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002297 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002298 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002299 stack.ensureVisibleActivitiesConfigurationLocked(
2300 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002301 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002302 } finally {
2303 mWindowManager.continueSurfaceLayout();
2304 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002305 }
2306 }
2307
Jorim Jaggi192086e2016-03-11 17:17:03 +01002308 void deferUpdateBounds(int stackId) {
2309 final ActivityStack stack = getStack(stackId);
2310 if (stack != null) {
2311 stack.deferUpdateBounds();
2312 }
2313 }
2314
2315 void continueUpdateBounds(int stackId) {
2316 final ActivityStack stack = getStack(stackId);
2317 if (stack != null) {
2318 stack.continueUpdateBounds();
2319 }
2320 }
2321
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002322 void notifyAppTransitionDone() {
2323 continueUpdateBounds(HOME_STACK_ID);
2324 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2325 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002326 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002327 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002328 if (task != null) {
2329 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002330 }
2331 }
2332 mResizingTasksDuringAnimation.clear();
2333 }
2334
Robert Carre7cc44d2017-03-20 19:04:30 -07002335 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2336 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002337
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002338 final ActivityStack stack = getStack(fromStackId);
2339 if (stack == null) {
2340 return;
2341 }
2342
2343 mWindowManager.deferSurfaceLayout();
2344 try {
2345 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002346 // We are moving all tasks from the docked stack to the fullscreen stack,
2347 // which is dismissing the docked stack, so resize all other stacks to
2348 // fullscreen here already so we don't end up with resize trashing.
2349 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2350 if (StackId.isResizeableByDockedStack(i)) {
2351 ActivityStack otherStack = getStack(i);
2352 if (otherStack != null) {
2353 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2354 true /* allowResizeInDockedMode */, DEFER_RESUME);
2355 }
2356 }
2357 }
2358
2359 // Also disable docked stack resizing since we have manually adjusted the
2360 // size of other stacks above and we don't want to trigger a docked stack
2361 // resize when we remove task from it below and it is detached from the
2362 // display because it no longer contains any tasks.
2363 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002364 } else if (fromStackId == PINNED_STACK_ID) {
2365 if (onTop) {
2366 // Log if we are expanding the PiP to fullscreen
2367 MetricsLogger.action(mService.mContext,
2368 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2369 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002370 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002371 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002372 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002373 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002374 // If we are moving from the pinned stack, then the animation takes care of updating
2375 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002376 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002377 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2378 final int size = tasks.size();
2379 if (onTop) {
2380 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002381 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002382 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002383 if (fromStackId == PINNED_STACK_ID) {
2384 // Update the return-to to reflect where the pinned stack task was moved
2385 // from so that we retain the stack that was previously visible if the
2386 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002387 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2388 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002389 }
Winson Chung74666102017-02-22 17:49:24 -08002390 // Defer resume until all the tasks have been moved to the fullscreen stack
2391 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2392 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002393 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002394 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002395 }
2396 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002397 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002398 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002399 final int position = fullscreenStack != null
2400 ? Math.max(fullscreenStack.getAllTasks().size() - 1, 0) : 0;
2401 // Defer resume until all the tasks have been moved to the fullscreen stack
Matthew Ng47fa6ae2017-02-07 13:15:10 -08002402 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, position,
Winson Chung5af42fc2017-03-24 17:11:33 -07002403 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2404 schedulePictureInPictureModeChange,
2405 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002406 }
2407 }
Winson Chung74666102017-02-22 17:49:24 -08002408
2409 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2410 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002411 } finally {
2412 mAllowDockedStackResize = true;
2413 mWindowManager.continueSurfaceLayout();
2414 }
2415 }
2416
Robert Carr6914f082017-03-20 19:04:30 -07002417 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002418 mWindowManager.inSurfaceTransaction(
2419 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002420 }
2421
Jorim Jaggidc249c42015-12-15 14:57:31 -08002422 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002423 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2424 boolean preserveWindows) {
2425 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2426 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2427 false /* deferResume */);
2428 }
2429
2430 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2431 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2432 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002433
2434 if (!mAllowDockedStackResize) {
2435 // Docked stack resize currently disabled.
2436 return;
2437 }
2438
Jorim Jaggidc249c42015-12-15 14:57:31 -08002439 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2440 if (stack == null) {
2441 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2442 return;
2443 }
2444
2445 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2446 mWindowManager.deferSurfaceLayout();
2447 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002448 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2449 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002450 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002451 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002452
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002453 // TODO: Checking for isAttached might not be needed as if the user passes in null
2454 // dockedBounds then they want the docked stack to be dismissed.
2455 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2456 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002457 // In this case we make all other static stacks fullscreen and move all
2458 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002459 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002460
2461 // stack shouldn't contain anymore activities, so nothing to resume.
2462 r = null;
2463 } else {
2464 // Docked stacks occupy a dedicated region on screen so the size of all other
2465 // static stacks need to be adjusted so they don't overlap with the docked stack.
2466 // We get the bounds to use from window manager which has been adjusted for any
2467 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002468 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002469 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002470 final ActivityStack current = getStack(i);
2471 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002472 current.getStackDockedModeBounds(
2473 tempOtherTaskBounds /* currentTempTaskBounds */,
2474 tempRect /* outStackBounds */,
2475 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2476
Matthew Nge15352e2016-12-20 15:36:29 -08002477 resizeStackLocked(i, tempRect,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002478 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2479 tempOtherTaskInsetBounds, preserveWindows,
2480 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002481 }
2482 }
2483 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002484 if (!deferResume) {
2485 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2486 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002487 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002488 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002489 mWindowManager.continueSurfaceLayout();
2490 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2491 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002492 }
2493
Robert Carr0d00c2e2016-02-29 17:45:02 -08002494 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
2495 final ActivityStack stack = getStack(PINNED_STACK_ID);
2496 if (stack == null) {
2497 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2498 return;
2499 }
2500 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2501 mWindowManager.deferSurfaceLayout();
2502 try {
2503 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002504 Rect insetBounds = null;
2505 if (tempPinnedTaskBounds != null) {
2506 // We always use 0,0 as the position for the inset rect because
2507 // if we are getting insets at all in the pinned stack it must mean
2508 // we are headed for fullscreen.
2509 insetBounds = tempRect;
2510 insetBounds.top = 0;
2511 insetBounds.left = 0;
2512 insetBounds.right = tempPinnedTaskBounds.width();
2513 insetBounds.bottom = tempPinnedTaskBounds.height();
2514 }
2515 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002516 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002517 } finally {
2518 mWindowManager.continueSurfaceLayout();
2519 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2520 }
2521 }
2522
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002523 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002524 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002525 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002526 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002527 }
2528
Wale Ogunwale1666e312016-12-16 11:27:18 -08002529 final ActivityContainer activityContainer =
2530 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002531 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002532 }
2533
Robert Carr6914f082017-03-20 19:04:30 -07002534
Robert Carre7cc44d2017-03-20 19:04:30 -07002535 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002536 final ActivityStack stack = getStack(stackId);
2537 if (stack == null) {
2538 return;
2539 }
2540
2541 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2542 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002543 /**
2544 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2545 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2546 * that the client receives onStop() before it is reparented. We do this by detaching
2547 * the stack from the display so that it will be considered invisible when
2548 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2549 * invisible as well and added to the stopping list. After which we process the
2550 * stopping list by handling the idle.
2551 */
2552 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2553 pinnedStack.mForceHidden = true;
2554 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2555 pinnedStack.mForceHidden = false;
2556 activityIdleInternalLocked(null, false /* fromTimeout */,
2557 true /* processPausingActivites */, null /* configuration */);
2558
2559 // Move all the tasks to the bottom of the fullscreen stack
2560 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002561 } else {
2562 for (int i = tasks.size() - 1; i >= 0; i--) {
2563 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2564 REMOVE_FROM_RECENTS);
2565 }
2566 }
2567 }
2568
2569 /**
Robert Carr6914f082017-03-20 19:04:30 -07002570 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2571 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2572 * instead moved back onto the fullscreen stack.
2573 */
2574 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002575 mWindowManager.inSurfaceTransaction(
2576 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002577 }
2578
2579 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002580 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2581 */
2582 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2583 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2584 }
2585
2586 /**
Winson Chung010927a2016-12-15 16:12:35 -08002587 * Removes the task with the specified task id.
2588 *
2589 * @param taskId Identifier of the task to be removed.
2590 * @param killProcess Kill any process associated with the task if possible.
2591 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002592 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2593 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002594 * @return Returns true if the given task was found and removed.
2595 */
Winson Chung6954fc92017-03-24 16:22:12 -07002596 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2597 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002598 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2599 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002600 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002601 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002602 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2603 if (tr.isPersistable) {
2604 mService.notifyTaskPersisterLocked(null, true);
2605 }
2606 return true;
2607 }
2608 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2609 return false;
2610 }
2611
2612 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2613 if (removeFromRecents) {
2614 mRecentTasks.remove(tr);
2615 tr.removedFromRecents();
2616 }
2617 ComponentName component = tr.getBaseIntent().getComponent();
2618 if (component == null) {
2619 Slog.w(TAG, "No component for base intent of task: " + tr);
2620 return;
2621 }
2622
2623 // Find any running services associated with this app and stop if needed.
2624 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2625
2626 if (!killProcess) {
2627 return;
2628 }
2629
2630 // Determine if the process(es) for this task should be killed.
2631 final String pkg = component.getPackageName();
2632 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2633 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2634 for (int i = 0; i < pmap.size(); i++) {
2635
2636 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2637 for (int j = 0; j < uids.size(); j++) {
2638 ProcessRecord proc = uids.valueAt(j);
2639 if (proc.userId != tr.userId) {
2640 // Don't kill process for a different user.
2641 continue;
2642 }
2643 if (proc == mService.mHomeProcess) {
2644 // Don't kill the home process along with tasks from the same package.
2645 continue;
2646 }
2647 if (!proc.pkgList.containsKey(pkg)) {
2648 // Don't kill process that is not associated with this task.
2649 continue;
2650 }
2651
2652 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002653 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002654 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2655 // Don't kill process(es) that has an activity in a different task that is
2656 // also in recents.
2657 return;
2658 }
2659 }
2660
2661 if (proc.foregroundServices) {
2662 // Don't kill process(es) with foreground service.
2663 return;
2664 }
2665
2666 // Add process to kill list.
2667 procsToKill.add(proc);
2668 }
2669 }
2670
2671 // Kill the running processes.
2672 for (int i = 0; i < procsToKill.size(); i++) {
2673 ProcessRecord pr = procsToKill.get(i);
2674 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2675 && pr.curReceivers.isEmpty()) {
2676 pr.kill("remove task", true);
2677 } else {
2678 // We delay killing processes that are not in the background or running a receiver.
2679 pr.waitingToKill = "remove task";
2680 }
2681 }
2682 }
2683
Craig Mautner4a1cb222013-12-04 16:14:06 -08002684 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002685 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002686 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2687 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002688 break;
2689 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002690 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002691 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002692 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002693 }
2694
Chong Zhang5dcb2752015-08-18 13:50:26 -07002695 /**
2696 * Restores a recent task to a stack
2697 * @param task The recent task to be restored.
2698 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002699 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2700 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002701 * @return true if the task has been restored successfully.
2702 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002703 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002704 if (!StackId.isStaticStack(stackId)) {
2705 // If stack is not static (or stack id is invalid) - use the default one.
2706 // This means that tasks that were on external displays will be restored on the
2707 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002708 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002709 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002710 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2711 // Put it in the fullscreen stack.
2712 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002713 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002714
Andrii Kulian02b7a832016-10-06 23:11:56 -07002715 final ActivityStack currentStack = task.getStack();
2716 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002717 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002718 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002719 // Nothing else to do since it is already restored in the right stack.
2720 return true;
2721 }
2722 // Remove current stack association, so we can re-associate the task with the
2723 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002724 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002725 }
2726
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002727 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002728 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002729
2730 if (stack == null) {
2731 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2733 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002734 return false;
2735 }
2736
Wale Ogunwale72919d22016-12-08 18:58:50 -08002737 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002738 // TODO: move call for creation here and other place into Stack.addTask()
2739 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002740 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2741 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002742 final ArrayList<ActivityRecord> activities = task.mActivities;
2743 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002744 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002745 }
2746 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002747 }
2748
Wale Ogunwale040b4702015-08-06 18:10:50 -07002749 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002750 * Move stack with all its existing content to specified display.
2751 * @param stackId Id of stack to move.
2752 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002753 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002754 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002755 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07002756 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2757 if (activityDisplay == null) {
2758 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2759 + displayId);
2760 }
2761 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2762 if (activityContainer != null) {
2763 if (activityContainer.isAttachedLocked()) {
2764 if (activityContainer.getDisplayId() == displayId) {
2765 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2766 + " to its current displayId=" + displayId);
2767 }
2768
Andrii Kulian250d6532017-02-08 23:30:45 -08002769 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002770 } else {
2771 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2772 + stackId + " is not attached to any display.");
2773 }
2774 } else {
2775 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2776 + stackId);
2777 }
Andrii Kulian839def92016-11-02 10:58:58 -07002778 // TODO(multi-display): resize stacks properly if moved from split-screen.
2779 }
2780
2781 /**
Winson Chung74666102017-02-22 17:49:24 -08002782 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2783 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002784 */
Winson Chung74666102017-02-22 17:49:24 -08002785 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002786 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002787
Winson Chung74666102017-02-22 17:49:24 -08002788 // Check that we aren't reparenting to the same stack that the task is already in
2789 if (prevStack != null && prevStack.mStackId == stackId) {
2790 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2791 + " already in stackId=" + stackId);
2792 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002793 }
2794
Winson Chung74666102017-02-22 17:49:24 -08002795 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2796 // support
2797 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2798 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2799 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002800 }
2801
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002802 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2803 // multi-display.
2804 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2805 // TODO: Check ActivityView after fixing b/35349678.
2806 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2807 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2808 + " reparent task=" + task + " to stackId=" + stackId);
2809 }
2810
Winson Chung74666102017-02-22 17:49:24 -08002811 // Ensure that we aren't trying to move into a freeform stack without freeform
2812 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002813 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002814 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2815 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002816 }
2817
Winson Chung74666102017-02-22 17:49:24 -08002818 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2819 // used for split-screen mode and will cause things like the docked divider to show up. We
2820 // instead leave the task in its current stack or move it to the fullscreen stack if it
2821 // isn't currently in a stack.
2822 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2823 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2824 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2825 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002826 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002827
Winson Chung74666102017-02-22 17:49:24 -08002828 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2829 // example, a docked stack is created which will lead to the stack we are moving from being
2830 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002831 try {
Winson Chung74666102017-02-22 17:49:24 -08002832 task.mTemporarilyUnresizable = true;
2833 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002834 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002835 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002836 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002837 }
2838
Winson Chung08f81892017-03-02 15:40:51 -08002839 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002840 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2841 if (stack == null) {
2842 throw new IllegalArgumentException(
2843 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2844 }
2845
2846 final ActivityRecord r = stack.topRunningActivityLocked();
2847 if (r == null) {
2848 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2849 + " in stack=" + stack);
2850 return false;
2851 }
2852
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002853 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002854 Slog.w(TAG,
2855 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002856 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002857 return false;
2858 }
2859
Winson Chung08f81892017-03-02 15:40:51 -08002860 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, destBounds,
2861 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002862 return true;
2863 }
2864
Winson Chung08f81892017-03-02 15:40:51 -08002865 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceBounds, Rect destBounds,
2866 boolean moveHomeStackToFront, String reason) {
2867
Wale Ogunwale480dca02016-02-06 13:58:29 -08002868 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002869
Wale Ogunwale1666e312016-12-16 11:27:18 -08002870 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002871 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002872
Wale Ogunwale480dca02016-02-06 13:58:29 -08002873 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002874 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08002875
Andrii Kulian02b7a832016-10-06 23:11:56 -07002876 if (r == task.getStack().getVisibleBehindActivity()) {
Wale Ogunwale43896cf2016-04-16 10:54:30 -07002877 // An activity can't be pinned and visible behind at the same time. Go ahead and
2878 // release it from been visible behind before pinning.
2879 requestVisibleBehindLocked(r, false);
2880 }
2881
Wale Ogunwale480dca02016-02-06 13:58:29 -08002882 // Resize the pinned stack to match the current size of the task the activity we are
2883 // going to be moving is currently contained in. We do this to have the right starting
2884 // animation bounds for the pinned stack to the desired bounds the caller wants.
2885 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
2886 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002887 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002888
2889 if (task.mActivities.size() == 1) {
2890 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08002891 // the stack without re-parenting the activity in a different task. We don't
2892 // move the home stack forward if we are currently entering picture-in-picture
2893 // while pausing because that changes the focused stack and may prevent the new
2894 // starting activity from resuming.
2895 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08002896 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08002897 // Move the home stack forward if the task we just moved to the pinned stack
2898 // was launched from home so home should be visible behind it.
2899 moveHomeStackToFront(reason);
2900 }
Winson Chung5af42fc2017-03-24 17:11:33 -07002901 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002902 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002903 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002904 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08002905 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08002906 // reveal/leave the other activities in their original task.
2907
2908 // Currently, we don't support reparenting activities across tasks in two different
2909 // stacks, so instead, just create a new task in the same stack, reparent the
2910 // activity into that task, and then reparent the whole task to the new stack. This
2911 // ensures that all the necessary work to migrate states in the old and new stacks
2912 // is also done.
2913 final TaskRecord newTask = task.getStack().createTaskRecord(
2914 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
2915 r.mActivityType);
2916 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
2917
Winson Chung5af42fc2017-03-24 17:11:33 -07002918 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08002919 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07002920 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08002921 }
Winson Chungc2baac02017-01-11 13:34:47 -08002922
2923 // Reset the state that indicates it can enter PiP while pausing after we've moved it
2924 // to the pinned stack
2925 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08002926 } finally {
2927 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07002928 }
2929
Wale Ogunwale480dca02016-02-06 13:58:29 -08002930 // The task might have already been running and its visibility needs to be synchronized
2931 // with the visibility of the stack / windows.
Wale Ogunwale079a0042015-10-24 11:44:07 -07002932 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08002933 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08002934
Winson Chung47900652017-04-06 18:44:25 -07002935 // TODO(b/36099777): Schedule the PiP mode change here immediately until we can defer all
2936 // callbacks until after the bounds animation
2937 scheduleUpdatePictureInPictureModeIfNeeded(r.getTask(), destBounds, true /* immediate */);
2938
Winson Chung08f81892017-03-02 15:40:51 -08002939 stack.animateResizePinnedStack(sourceBounds, destBounds, -1 /* animationDuration */);
Winson Chungc81c0ce2017-03-17 12:27:30 -07002940 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002941 }
2942
Andrii Kulian0864bbb2017-02-16 15:45:58 -08002943 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07002944 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
2945 if (r == null || !r.isFocusable()) {
2946 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2947 "moveActivityStackToFront: unfocusable r=" + r);
2948 return false;
2949 }
2950
Bryce Leeaf691c02017-03-20 14:20:22 -07002951 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002952 final ActivityStack stack = r.getStack();
2953 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07002954 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
2955 + r + " task=" + task);
2956 return false;
2957 }
2958
Chong Zhang6cda19c2016-06-14 19:07:56 -07002959 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
2960 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2961 "moveActivityStackToFront: already on top, r=" + r);
2962 return false;
2963 }
2964
2965 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
2966 "moveActivityStackToFront: r=" + r);
2967
2968 stack.moveToFront(reason, task);
2969 return true;
2970 }
2971
David Stevensc6b91c62017-02-08 14:23:58 -08002972 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08002973 mTmpFindTaskResult.r = null;
2974 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08002975 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002976 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2978 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2980 final ActivityStack stack = stacks.get(stackNdx);
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002981 if (!r.isApplicationActivity() && !stack.isHomeOrRecentsStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002982 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07002983 continue;
2984 }
2985 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002986 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
2987 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002988 continue;
2989 }
Wale Ogunwale39381972015-12-17 17:15:29 -08002990 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08002991 // It is possible to have tasks in multiple stacks with the same root affinity, so
2992 // we should keep looking after finding an affinity match to see if there is a
2993 // better match in another stack. Also, task affinity isn't a good enough reason
2994 // to target a display which isn't the source of the intent, so skip any affinity
2995 // matches not on the specified display.
2996 if (mTmpFindTaskResult.r != null) {
2997 if (!mTmpFindTaskResult.matchedByRootAffinity) {
2998 return mTmpFindTaskResult.r;
2999 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
3000 affinityMatch = mTmpFindTaskResult.r;
3001 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003002 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003003 }
3004 }
David Stevensc6b91c62017-02-08 14:23:58 -08003005 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3006 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003007 }
3008
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003009 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3010 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003011 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3012 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003013 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003014 final ActivityRecord ar = stacks.get(stackNdx)
3015 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003016 if (ar != null) {
3017 return ar;
3018 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003019 }
3020 }
3021 return null;
3022 }
3023
Craig Mautner8d341ef2013-03-26 09:03:27 -07003024 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003025 scheduleSleepTimeout();
3026 if (!mGoingToSleep.isHeld()) {
3027 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003028 if (mLaunchingActivity.isHeld()) {
3029 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3030 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003031 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003032 mLaunchingActivity.release();
3033 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003034 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003035 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003036 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003037 }
3038
3039 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003040 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003041
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003042 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003043 final long endTime = System.currentTimeMillis() + timeout;
3044 while (true) {
3045 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003046 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3047 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003048 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3049 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3050 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003051 }
3052 if (cantShutdown) {
3053 long timeRemaining = endTime - System.currentTimeMillis();
3054 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003055 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003056 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003057 } catch (InterruptedException e) {
3058 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003059 } else {
3060 Slog.w(TAG, "Activity manager shutdown timed out");
3061 timedout = true;
3062 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003063 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003064 } else {
3065 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003066 }
3067 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003068
3069 // Force checkReadyForSleep to complete.
3070 mSleepTimeout = true;
3071 checkReadyForSleepLocked();
3072
Craig Mautner8d341ef2013-03-26 09:03:27 -07003073 return timedout;
3074 }
3075
3076 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003077 removeSleepTimeouts();
3078 if (mGoingToSleep.isHeld()) {
3079 mGoingToSleep.release();
3080 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3082 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3084 final ActivityStack stack = stacks.get(stackNdx);
3085 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003086 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003087 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 }
Craig Mautner5314a402013-09-26 12:40:16 -07003089 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003090 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003091 mGoingToSleepActivities.clear();
3092 }
3093
3094 void activitySleptLocked(ActivityRecord r) {
3095 mGoingToSleepActivities.remove(r);
3096 checkReadyForSleepLocked();
3097 }
3098
3099 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003100 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003101 // Do not care.
3102 return;
3103 }
3104
3105 if (!mSleepTimeout) {
3106 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003107 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3108 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003109 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3110 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3111 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003112 }
3113
3114 if (mStoppingActivities.size() > 0) {
3115 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003116 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003117 + mStoppingActivities.size() + " activities");
3118 scheduleIdleLocked();
3119 dontSleep = true;
3120 }
3121
3122 if (mGoingToSleepActivities.size() > 0) {
3123 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003124 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003125 + mGoingToSleepActivities.size() + " activities");
3126 dontSleep = true;
3127 }
3128
3129 if (dontSleep) {
3130 return;
3131 }
3132 }
3133
Wei Wang65c7a152016-06-02 18:51:22 -07003134 // Send launch end powerhint before going sleep
3135 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3136
Craig Mautnere0a38842013-12-16 16:14:02 -08003137 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3138 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003139 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3140 stacks.get(stackNdx).goToSleep();
3141 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003142 }
3143
3144 removeSleepTimeouts();
3145
3146 if (mGoingToSleep.isHeld()) {
3147 mGoingToSleep.release();
3148 }
3149 if (mService.mShuttingDown) {
3150 mService.notifyAll();
3151 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003152 }
3153
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003154 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003155 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003156 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003157 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003158 }
3159 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003160 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003161 mWindowManager.executeAppTransition();
3162 return true;
3163 }
3164 return false;
3165 }
3166
Craig Mautner8d341ef2013-03-26 09:03:27 -07003167 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003168 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3169 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003170 int stackNdx = stacks.size() - 1;
3171 while (stackNdx >= 0) {
3172 stacks.get(stackNdx).handleAppCrashLocked(app);
3173 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003174 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003175 }
3176 }
3177
Jose Lima4b6c6692014-08-12 17:41:12 -07003178 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003179 final ActivityStack stack = r.getStack();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003180 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003181 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3182 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003183 return false;
3184 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -07003185
3186 if (visible && !StackId.activitiesCanRequestVisibleBehind(stack.mStackId)) {
3187 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: r=" + r
3188 + " visible=" + visible + " stackId=" + stack.mStackId
3189 + " can't contain visible behind activities");
3190 return false;
3191 }
3192
Jose Lima4b6c6692014-08-12 17:41:12 -07003193 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003194 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3195 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003196
3197 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003198 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003199 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003200 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003201 return true;
3202 }
3203
3204 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003205 if (visible && top.fullscreen) {
3206 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003207 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3208 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3209 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3210 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003211 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003212 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3213 // Only the activity set as currently visible behind should actively reset its
3214 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003215 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3216 "requestVisibleBehind: returning visible=" + visible
3217 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3218 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003219 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003220 }
3221
Jose Lima4b6c6692014-08-12 17:41:12 -07003222 stack.setVisibleBehindActivity(visible ? r : null);
3223 if (!visible) {
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003224 // If there is a translucent home activity, we need to force it stop being translucent,
3225 // because we can't depend on the application to necessarily perform that operation.
3226 // Check out b/14469711 for details.
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003227 final ActivityRecord next = stack.findNextTranslucentActivity(r);
Filip Gruszczynski62c5e9d2016-02-04 11:06:54 -08003228 if (next != null && next.isHomeActivity()) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003229 mService.convertFromTranslucent(next.appToken);
3230 }
3231 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003232 if (top.app != null && top.app.thread != null) {
3233 // Notify the top app of the change.
3234 try {
3235 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3236 } catch (RemoteException e) {
3237 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003238 }
3239 return true;
3240 }
3241
Craig Mautnerbb742462014-07-07 15:28:55 -07003242 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003243 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003244 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003245 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003246
3247 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003248 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003249 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003250 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003251 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003252
3253 // When launching tasks behind, update the last active time of the top task after the new
3254 // task has been shown briefly
3255 final ActivityRecord top = stack.topActivity();
3256 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003257 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003258 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003259 }
3260
3261 void scheduleLaunchTaskBehindComplete(IBinder token) {
3262 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3263 }
3264
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003265 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3266 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003267 mKeyguardController.beginActivityVisibilityUpdate();
3268 try {
3269 // First the front stacks. In case any are not fullscreen and are in front of home.
3270 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3271 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3272 final int topStackNdx = stacks.size() - 1;
3273 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3274 final ActivityStack stack = stacks.get(stackNdx);
3275 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3276 }
Craig Mautner580ea812013-04-25 12:58:38 -07003277 }
Jorim Jaggife762342016-10-13 14:33:27 +02003278 } finally {
3279 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003280 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003281 }
3282
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003283 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3284 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3285 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3286 final int topStackNdx = stacks.size() - 1;
3287 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3288 final ActivityStack stack = stacks.get(stackNdx);
3289 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3290 }
3291 }
3292 }
3293
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003294 void invalidateTaskLayers() {
3295 mTaskLayersChanged = true;
3296 }
3297
3298 void rankTaskLayersIfNeeded() {
3299 if (!mTaskLayersChanged) {
3300 return;
3301 }
3302 mTaskLayersChanged = false;
3303 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3304 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3305 int baseLayer = 0;
3306 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3307 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3308 }
3309 }
3310 }
3311
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003312 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3313 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3315 final int topStackNdx = stacks.size() - 1;
3316 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3317 final ActivityStack stack = stacks.get(stackNdx);
3318 stack.clearOtherAppTimeTrackers(except);
3319 }
3320 }
3321 }
3322
Craig Mautner8d341ef2013-03-26 09:03:27 -07003323 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003324 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3325 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003326 final int numStacks = stacks.size();
3327 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3328 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003329 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003330 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003331 }
3332 }
3333
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003334 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3335 // Examine all activities currently running in the process.
3336 TaskRecord firstTask = null;
3337 // Tasks is non-null only if two or more tasks are found.
3338 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003339 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3340 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003341 ActivityRecord r = app.activities.get(i);
3342 // First, if we find an activity that is in the process of being destroyed,
3343 // then we just aren't going to do anything for now; we want things to settle
3344 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003345 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003346 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003347 return;
3348 }
3349 // Don't consider any activies that are currently not in a state where they
3350 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003351 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3352 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003353 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003354 continue;
3355 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003356
3357 final TaskRecord task = r.getTask();
3358 if (task != null) {
3359 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003360 + " from " + r);
3361 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003362 firstTask = task;
3363 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003364 if (tasks == null) {
3365 tasks = new ArraySet<>();
3366 tasks.add(firstTask);
3367 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003368 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003369 }
3370 }
3371 }
3372 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003373 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003374 return;
3375 }
3376 // If we have activities in multiple tasks that are in a position to be destroyed,
3377 // let's iterate through the tasks and release the oldest one.
3378 final int numDisplays = mActivityDisplays.size();
3379 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3380 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3381 // Step through all stacks starting from behind, to hit the oldest things first.
3382 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3383 final ActivityStack stack = stacks.get(stackNdx);
3384 // Try to release activities in this stack; if we manage to, we are done.
3385 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3386 return;
3387 }
3388 }
3389 }
3390 }
3391
Amith Yamasani37a40c22015-06-17 13:25:42 -07003392 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003393 final int focusStackId = mFocusedStack.getStackId();
3394 // We dismiss the docked stack whenever we switch users.
3395 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003396 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3397 // also cause all tasks to be moved to the fullscreen stack at a position that is
3398 // appropriate.
3399 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003400
3401 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003402 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003403 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003404
Craig Mautner858d8a62013-04-23 17:08:34 -07003405 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003406 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3407 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003408 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003409 final ActivityStack stack = stacks.get(stackNdx);
3410 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003411 TaskRecord task = stack.topTask();
3412 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003413 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003414 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003416 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003417
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003418 ActivityStack stack = getStack(restoreStackId);
3419 if (stack == null) {
3420 stack = mHomeStack;
3421 }
3422 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003423 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003424 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003425 } else {
3426 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003427 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003428 }
Craig Mautner93529a42013-10-04 15:03:13 -07003429 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003430 }
3431
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003432 /** Checks whether the userid is a profile of the current user. */
3433 boolean isCurrentProfileLocked(int userId) {
3434 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003435 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003436 }
3437
Winson Chung4dabf232017-01-25 13:25:22 -08003438 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3439 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003440 ArrayList<ActivityRecord> stops = null;
3441
3442 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003443 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3444 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003445 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003446 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003447 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3448 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003449 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003450 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003451 if (s.finishing) {
3452 // If this activity is finishing, it is sitting on top of
3453 // everyone else but we now know it is no longer needed...
3454 // so get rid of it. Otherwise, we need to go through the
3455 // normal flow and hide it once we determine that it is
3456 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003457 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003458 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003459 }
3460 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003461 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003462 if (!processPausingActivities && s.state == PAUSING) {
3463 // Defer processing pausing activities in this iteration and reschedule
3464 // a delayed idle to reprocess it again
3465 removeTimeoutsForActivityLocked(idleActivity);
3466 scheduleIdleTimeoutLocked(idleActivity);
3467 continue;
3468 }
3469
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003470 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003471 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003472 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003473 }
3474 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003475 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003476 }
3477 }
3478
3479 return stops;
3480 }
3481
Craig Mautnercf910b02013-04-23 11:23:27 -07003482 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003483 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3484 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3485 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3486 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003487 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003488 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003489 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 if (r == null) Slog.e(TAG,
3491 "validateTop...: null top activity, stack=" + stack);
3492 else {
3493 final ActivityRecord pausing = stack.mPausingActivity;
3494 if (pausing != null && pausing == r) Slog.e(TAG,
3495 "validateTop...: top stack has pausing activity r=" + r
3496 + " state=" + state);
3497 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3498 "validateTop...: activity in front not resumed r=" + r
3499 + " state=" + state);
3500 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003501 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003502 final ActivityRecord resumed = stack.mResumedActivity;
3503 if (resumed != null && resumed == r) Slog.e(TAG,
3504 "validateTop...: back stack has resumed activity r=" + r
3505 + " state=" + state);
3506 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3507 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003508 }
3509 }
3510 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003511 }
3512
Craig Mautnere0570202015-05-13 13:06:11 -07003513 private String lockTaskModeToString() {
3514 switch (mLockTaskModeState) {
3515 case LOCK_TASK_MODE_LOCKED:
3516 return "LOCKED";
3517 case LOCK_TASK_MODE_PINNED:
3518 return "PINNED";
3519 case LOCK_TASK_MODE_NONE:
3520 return "NONE";
3521 default: return "unknown=" + mLockTaskModeState;
3522 }
3523 }
3524
Craig Mautner27084302013-03-25 08:05:25 -07003525 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003526 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003528 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003529 pw.print(prefix);
3530 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003531 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003532 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003533 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003534 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3535 if (packages.size() > 0) {
3536 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3537 for (int i = 0; i < packages.size(); ++i) {
3538 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3539 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3540 }
3541 }
Bryce Lee4a194382017-04-04 14:32:48 -07003542 if (!mWaitingForActivityVisible.isEmpty()) {
3543 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3544 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3545 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3546 }
3547 }
3548
Jorim Jaggi8d786932016-10-26 19:08:36 -07003549 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3550 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003551 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003552
Winson43d1f262016-06-14 16:05:55 -07003553 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003554 * Dump all connected displays' configurations.
3555 * @param prefix Prefix to apply to each line of the dump.
3556 */
3557 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3558 pw.print(prefix); pw.println("Display override configurations:");
3559 final int displayCount = mActivityDisplays.size();
3560 for (int i = 0; i < displayCount; i++) {
3561 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3562 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3563 pw.println(activityDisplay.getOverrideConfiguration());
3564 }
3565 }
3566
3567 /**
Winson43d1f262016-06-14 16:05:55 -07003568 * Dumps the activities matching the given {@param name} in the either the focused stack
3569 * or all visible stacks if {@param dumpVisibleStacks} is true.
3570 */
Winson Chung6998bc42017-02-28 17:07:05 -08003571 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3572 boolean dumpFocusedStackOnly) {
3573 if (dumpFocusedStackOnly) {
3574 return mFocusedStack.getDumpActivitiesLocked(name);
3575 } else {
Winson43d1f262016-06-14 16:05:55 -07003576 ArrayList<ActivityRecord> activities = new ArrayList<>();
3577 int numDisplays = mActivityDisplays.size();
3578 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3579 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3580 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3581 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003582 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003583 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003584 activities.addAll(stack.getDumpActivitiesLocked(name));
3585 }
3586 }
3587 }
3588 return activities;
Winson43d1f262016-06-14 16:05:55 -07003589 }
Craig Mautner20e72272013-04-01 13:45:53 -07003590 }
3591
Dianne Hackborn390517b2013-05-30 15:03:32 -07003592 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3593 boolean needSep, String prefix) {
3594 if (activity != null) {
3595 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3596 if (needSep) {
3597 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003598 }
3599 pw.print(prefix);
3600 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003601 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003602 }
3603 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003604 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003605 }
3606
Craig Mautner8d341ef2013-03-26 09:03:27 -07003607 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3608 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003609 boolean printed = false;
3610 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003611 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3612 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003613 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003614 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003615 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003617 final ActivityStack stack = stacks.get(stackNdx);
3618 StringBuilder stackHeader = new StringBuilder(128);
3619 stackHeader.append(" Stack #");
3620 stackHeader.append(stack.mStackId);
3621 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003622 stackHeader.append("\n");
3623 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3624 stackHeader.append("\n");
3625 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003626
3627 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3628 dumpClient, dumpPackage, needSep, stackHeader.toString());
3629 printed |= printedStackHeader;
3630 if (!printedStackHeader) {
3631 // Ensure we always dump the stack header even if there are no activities
3632 pw.println();
3633 pw.println(stackHeader);
3634 }
3635
Craig Mautner4a1cb222013-12-04 16:14:06 -08003636 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3637 !dumpAll, false, dumpPackage, true,
3638 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003639
Craig Mautner4a1cb222013-12-04 16:14:06 -08003640 needSep = printed;
3641 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3642 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003643 if (pr) {
3644 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003645 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003646 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003647 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3648 " mResumedActivity: ");
3649 if (pr) {
3650 printed = true;
3651 needSep = false;
3652 }
3653 if (dumpAll) {
3654 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3655 " mLastPausedActivity: ");
3656 if (pr) {
3657 printed = true;
3658 needSep = true;
3659 }
3660 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3661 needSep, " mLastNoHistoryActivity: ");
3662 }
3663 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003664 }
3665 }
3666
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003667 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3668 false, dumpPackage, true, " Activities waiting to finish:", null);
3669 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3670 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003671 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3672 false, !dumpAll, false, dumpPackage, true,
3673 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003674 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3675 false, dumpPackage, true, " Activities waiting to sleep:", null);
3676 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3677 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003678
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003679 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003680 }
3681
Dianne Hackborn390517b2013-05-30 15:03:32 -07003682 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003683 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003684 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003685 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003686 String innerPrefix = null;
3687 String[] args = null;
3688 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003689 for (int i=list.size()-1; i>=0; i--) {
3690 final ActivityRecord r = list.get(i);
3691 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3692 continue;
3693 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003694 if (innerPrefix == null) {
3695 innerPrefix = prefix + " ";
3696 args = new String[0];
3697 }
3698 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003699 final boolean full = !brief && (complete || !r.isInHistory());
3700 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003701 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003702 needNL = false;
3703 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003704 if (header1 != null) {
3705 pw.println(header1);
3706 header1 = null;
3707 }
3708 if (header2 != null) {
3709 pw.println(header2);
3710 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003711 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003712 if (lastTask != r.getTask()) {
3713 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003714 pw.print(prefix);
3715 pw.print(full ? "* " : " ");
3716 pw.println(lastTask);
3717 if (full) {
3718 lastTask.dump(pw, prefix + " ");
3719 } else if (complete) {
3720 // Complete + brief == give a summary. Isn't that obvious?!?
3721 if (lastTask.intent != null) {
3722 pw.print(prefix); pw.print(" ");
3723 pw.println(lastTask.intent.toInsecureStringWithClip());
3724 }
3725 }
3726 }
3727 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3728 pw.print(" #"); pw.print(i); pw.print(": ");
3729 pw.println(r);
3730 if (full) {
3731 r.dump(pw, innerPrefix);
3732 } else if (complete) {
3733 // Complete + brief == give a summary. Isn't that obvious?!?
3734 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3735 if (r.app != null) {
3736 pw.print(innerPrefix); pw.println(r.app);
3737 }
3738 }
3739 if (client && r.app != null && r.app.thread != null) {
3740 // flush anything that is already in the PrintWriter since the thread is going
3741 // to write to the file descriptor directly
3742 pw.flush();
3743 try {
3744 TransferPipe tp = new TransferPipe();
3745 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003746 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003747 // Short timeout, since blocking here can
3748 // deadlock with the application.
3749 tp.go(fd, 2000);
3750 } finally {
3751 tp.kill();
3752 }
3753 } catch (IOException e) {
3754 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3755 } catch (RemoteException e) {
3756 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3757 }
3758 needNL = true;
3759 }
3760 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003761 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003762 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003763
Craig Mautnerf3333272013-04-22 10:55:53 -07003764 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003765 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3766 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003767 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3768 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003769 }
3770
3771 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003772 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003773 }
3774
3775 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3777 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003778 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3779 }
3780
Craig Mautner05d29032013-05-03 13:40:13 -07003781 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003782 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3783 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3784 }
Craig Mautner05d29032013-05-03 13:40:13 -07003785 }
3786
Craig Mautner0eea92c2013-05-16 13:35:39 -07003787 void removeSleepTimeouts() {
3788 mSleepTimeout = false;
3789 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3790 }
3791
3792 final void scheduleSleepTimeout() {
3793 removeSleepTimeouts();
3794 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3795 }
3796
Craig Mautner4a1cb222013-12-04 16:14:06 -08003797 @Override
3798 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003799 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003800 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3801 }
3802
3803 @Override
3804 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003805 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003806 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3807 }
3808
3809 @Override
3810 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003811 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003812 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3813 }
3814
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003815 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003816 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003818 newDisplay = mActivityDisplays.get(displayId) == null;
3819 if (newDisplay) {
3820 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003821 if (activityDisplay.mDisplay == null) {
3822 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3823 return;
3824 }
Craig Mautner4504de52013-12-20 09:06:56 -08003825 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003826 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003827 mWindowManager.onDisplayAdded(displayId);
Craig Mautner4504de52013-12-20 09:06:56 -08003828 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003830 }
3831
Andrii Kulianca007a62016-11-22 16:33:28 -08003832 /** Check if display with specified id is added to the list. */
3833 boolean isDisplayAdded(int displayId) {
3834 return mActivityDisplays.get(displayId) != null;
3835 }
3836
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003837 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003838 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003839 mService.mContext.getResources().getDimensionPixelSize(
3840 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003841 }
3842
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003843 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003844 if (displayId == DEFAULT_DISPLAY) {
3845 throw new IllegalArgumentException("Can't remove the primary display.");
3846 }
3847
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003849 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3850 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003851 final boolean destroyContentOnRemoval
3852 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003853 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3854 while (!stacks.isEmpty()) {
3855 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003856 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003857 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3858 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003859 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003860 } else {
3861 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3862 // a valid launch stack for all activities. This way the task history from
3863 // external display will be preserved on primary after move.
3864 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003865 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003867 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003868 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 }
3870 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 }
3872
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003873 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003875 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3876 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003877 // TODO: Update the bounds.
3878 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003879 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003881 }
3882
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003883 private StackInfo getStackInfoLocked(ActivityStack stack) {
Jorim Jaggife762342016-10-13 14:33:27 +02003884 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003885 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003886 stack.getWindowContainerBounds(info.bounds);
Jorim Jaggife762342016-10-13 14:33:27 +02003887 info.displayId = DEFAULT_DISPLAY;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003888 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003889 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003890 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Winson529c8e42016-05-17 11:08:40 -07003891 info.position = display != null
3892 ? display.mStacks.indexOf(stack)
3893 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894
3895 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3896 final int numTasks = tasks.size();
3897 int[] taskIds = new int[numTasks];
3898 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003899 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003900 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 for (int i = 0; i < numTasks; ++i) {
3902 final TaskRecord task = tasks.get(i);
3903 taskIds[i] = task.taskId;
3904 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3905 : task.realActivity != null ? task.realActivity.flattenToString()
3906 : task.getTopActivity() != null ? task.getTopActivity().packageName
3907 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003908 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003909 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003910 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003911 }
3912 info.taskIds = taskIds;
3913 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003914 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08003915 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07003916
3917 final ActivityRecord top = stack.topRunningActivityLocked();
3918 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003919 return info;
3920 }
3921
3922 StackInfo getStackInfoLocked(int stackId) {
3923 ActivityStack stack = getStack(stackId);
3924 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003925 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 }
3927 return null;
3928 }
3929
3930 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003931 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003932 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3933 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003935 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 }
3937 }
3938 return list;
3939 }
3940
Craig Mautner15df08a2015-04-01 12:17:18 -07003941 TaskRecord getLockedTaskLocked() {
3942 final int top = mLockTaskModeTasks.size() - 1;
3943 if (top >= 0) {
3944 return mLockTaskModeTasks.get(top);
3945 }
3946 return null;
3947 }
3948
3949 boolean isLockedTask(TaskRecord task) {
3950 return mLockTaskModeTasks.contains(task);
3951 }
3952
3953 boolean isLastLockedTask(TaskRecord task) {
3954 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3955 }
3956
3957 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003958 if (!mLockTaskModeTasks.remove(task)) {
3959 return;
3960 }
3961 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3962 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003963 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003964 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3965 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003966 final Message lockTaskMsg = Message.obtain();
3967 lockTaskMsg.arg1 = task.userId;
3968 lockTaskMsg.what = LOCK_TASK_END_MSG;
3969 mHandler.sendMessage(lockTaskMsg);
3970 }
3971 }
3972
Andrii Kulianc27916642016-04-12 17:59:27 -07003973 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId, int actualStackId) {
3974 handleNonResizableTaskIfNeeded(task, preferredStackId, actualStackId,
3975 false /* forceNonResizable */);
3976 }
3977
Jorim Jaggid53f0922016-04-06 22:16:23 -07003978 void handleNonResizableTaskIfNeeded(
Andrii Kulianc27916642016-04-12 17:59:27 -07003979 TaskRecord task, int preferredStackId, int actualStackId, boolean forceNonResizable) {
Jorim Jaggid53f0922016-04-06 22:16:23 -07003980 if ((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
3981 || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003982 return;
3983 }
3984
Jorim Jaggicd13d332016-04-27 15:40:20 -07003985 final ActivityRecord topActivity = task.getTopActivity();
Winson Chungd3395382016-12-13 11:49:09 -08003986 if (!task.supportsSplitScreen() || forceNonResizable) {
Jorim Jaggi2adba072016-03-03 13:43:39 +01003987 // Display a warning toast that we tried to put a non-dockable task in the docked stack.
Yorke Leebd54c2a2016-10-25 13:49:23 -07003988 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07003989
Andrii Kulianc27916642016-04-12 17:59:27 -07003990 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
3991 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003992 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08003993 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07003994 && !topActivity.noDisplay) {
3995 String packageName = topActivity.appInfo.packageName;
Yorke Leebd54c2a2016-10-25 13:49:23 -07003996 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
3997 task.taskId, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08003998 }
Chong Zhangb15758a2015-11-17 12:12:03 -08003999 }
4000
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004001 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004002 if (mLockTaskNotify != null) {
4003 mLockTaskNotify.showToast(mLockTaskModeState);
4004 }
Jason Monka8f569c2014-07-07 12:15:21 -04004005 }
4006
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004007 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4008 if (mLockTaskModeTasks.contains(task)) {
4009 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4010 }
4011 }
4012
Craig Mautner432f64e2015-05-20 14:59:57 -07004013 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4014 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004015 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004016 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004017 final TaskRecord lockedTask = getLockedTaskLocked();
4018 if (lockedTask != null) {
4019 removeLockedTaskLocked(lockedTask);
4020 if (!mLockTaskModeTasks.isEmpty()) {
4021 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004022 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4023 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004024 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004025 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004026 return;
4027 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004028 }
Craig Mautnere0570202015-05-13 13:06:11 -07004029 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4030 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004031 return;
4032 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004033
4034 // Should have already been checked, but do it again.
4035 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004036 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4037 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004038 return;
4039 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004040 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004041 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004042 return;
4043 }
4044
4045 if (mLockTaskModeTasks.isEmpty()) {
4046 // First locktask.
4047 final Message lockTaskMsg = Message.obtain();
4048 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4049 lockTaskMsg.arg1 = task.userId;
4050 lockTaskMsg.what = LOCK_TASK_START_MSG;
4051 lockTaskMsg.arg2 = lockTaskModeState;
4052 mHandler.sendMessage(lockTaskMsg);
4053 }
4054 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004055 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4056 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004057 mLockTaskModeTasks.remove(task);
4058 mLockTaskModeTasks.add(task);
4059
4060 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004061 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004062 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004063
4064 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004065 findTaskToMoveToFrontLocked(task, 0, null, reason,
4066 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004067 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004068 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004069 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004070 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, task.getStackId(),
Andrii Kulianc27916642016-04-12 17:59:27 -07004071 true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004072 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004073 }
4074
4075 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004076 return isLockTaskModeViolation(task, false);
4077 }
4078
4079 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4080 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004081 return false;
4082 }
4083 final int lockTaskAuth = task.mLockTaskAuth;
4084 switch (lockTaskAuth) {
4085 case LOCK_TASK_AUTH_DONT_LOCK:
4086 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004087 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004088 case LOCK_TASK_AUTH_LAUNCHABLE:
4089 case LOCK_TASK_AUTH_WHITELISTED:
4090 return false;
4091 case LOCK_TASK_AUTH_PINNABLE:
4092 // Pinnable tasks can't be launched on top of locktask tasks.
4093 return !mLockTaskModeTasks.isEmpty();
4094 default:
4095 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4096 return true;
4097 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004098 }
4099
Craig Mautner15df08a2015-04-01 12:17:18 -07004100 void onLockTaskPackagesUpdatedLocked() {
4101 boolean didSomething = false;
4102 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4103 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004104 final boolean wasWhitelisted =
4105 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4106 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004107 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004108 final boolean isWhitelisted =
4109 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4110 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4111 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004112 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004113 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4114 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004115 removeLockedTaskLocked(lockedTask);
4116 lockedTask.performClearTaskLocked();
4117 didSomething = true;
4118 }
4119 }
4120 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4121 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4122 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4123 final ActivityStack stack = stacks.get(stackNdx);
4124 stack.onLockTaskPackagesUpdatedLocked();
4125 }
4126 }
Craig Mautnere0570202015-05-13 13:06:11 -07004127 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004128 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004129 if (mLockTaskModeTasks.isEmpty() && task != null
4130 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4131 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004132 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4133 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4134 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4135 false);
4136 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004137 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004138 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004139 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004140 }
4141 }
4142
Benjamin Franz43261142015-02-11 15:59:44 +00004143 int getLockTaskModeState() {
4144 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004145 }
4146
Jorim Jaggife89d122015-12-22 16:28:44 +01004147 void activityRelaunchedLocked(IBinder token) {
4148 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004149 if (mService.isSleepingOrShuttingDownLocked()) {
4150 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4151 if (r != null) {
4152 r.setSleeping(true, true);
4153 }
4154 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004155 }
4156
4157 void activityRelaunchingLocked(ActivityRecord r) {
4158 mWindowManager.notifyAppRelaunching(r.appToken);
4159 }
4160
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004161 void logStackState() {
4162 mActivityMetricsLogger.logWindowState();
4163 }
4164
Winson Chung5af42fc2017-03-24 17:11:33 -07004165 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004166 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4167 final ActivityRecord r = task.mActivities.get(i);
4168 if (r.app != null && r.app.thread != null) {
4169 mMultiWindowModeChangedActivities.add(r);
4170 }
4171 }
4172
4173 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4174 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4175 }
4176 }
4177
Winson Chung5af42fc2017-03-24 17:11:33 -07004178 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004179 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004180 if (prevStack == null || prevStack == stack
4181 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4182 return;
4183 }
4184
Winson Chung5af42fc2017-03-24 17:11:33 -07004185 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4186 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004187
Winson Chung5af42fc2017-03-24 17:11:33 -07004188 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4189 boolean immediate) {
4190
4191 if (immediate) {
4192 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4193 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4194 final ActivityRecord r = task.mActivities.get(i);
4195 if (r.app != null && r.app.thread != null) {
4196 r.updatePictureInPictureMode(targetStackBounds);
4197 }
4198 }
4199 } else {
4200 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4201 final ActivityRecord r = task.mActivities.get(i);
4202 if (r.app != null && r.app.thread != null) {
4203 mPipModeChangedActivities.add(r);
4204 }
4205 }
4206 mPipModeChangedTargetStackBounds = targetStackBounds;
4207
4208 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4209 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4210 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004211 }
4212 }
4213
Tony Mak853304c2016-04-18 15:17:41 +01004214 void setDockedStackMinimized(boolean minimized) {
4215 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004216 }
4217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004218 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004219
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004220 public ActivityStackSupervisorHandler(Looper looper) {
4221 super(looper);
4222 }
4223
Winson Chung4dabf232017-01-25 13:25:22 -08004224 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004225 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004226 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4227 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004228 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004229 }
4230
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004231 @Override
4232 public void handleMessage(Message msg) {
4233 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004234 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4235 synchronized (mService) {
4236 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4237 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004238 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004239 }
4240 }
4241 } break;
4242 case REPORT_PIP_MODE_CHANGED_MSG: {
4243 synchronized (mService) {
4244 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4245 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004246 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004247 }
4248 }
4249 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004250 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004251 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4252 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004253 if (mService.mDidDexOpt) {
4254 mService.mDidDexOpt = false;
4255 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4256 nmsg.obj = msg.obj;
4257 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4258 return;
4259 }
4260 // We don't at this point know if the activity is fullscreen,
4261 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004262 activityIdleInternal((ActivityRecord) msg.obj,
4263 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004264 } break;
4265 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004266 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004267 activityIdleInternal((ActivityRecord) msg.obj,
4268 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004269 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004270 case RESUME_TOP_ACTIVITY_MSG: {
4271 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004272 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004273 }
4274 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004275 case SLEEP_TIMEOUT_MSG: {
4276 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004277 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004278 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4279 mSleepTimeout = true;
4280 checkReadyForSleepLocked();
4281 }
4282 }
4283 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004284 case LAUNCH_TIMEOUT_MSG: {
4285 if (mService.mDidDexOpt) {
4286 mService.mDidDexOpt = false;
4287 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4288 return;
4289 }
4290 synchronized (mService) {
4291 if (mLaunchingActivity.isHeld()) {
4292 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4293 if (VALIDATE_WAKE_LOCK_CALLER
4294 && Binder.getCallingUid() != Process.myUid()) {
4295 throw new IllegalStateException("Calling must be system uid");
4296 }
4297 mLaunchingActivity.release();
4298 }
4299 }
4300 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004301 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004302 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303 } break;
4304 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004305 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004306 } break;
4307 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004308 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004309 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004310 case CONTAINER_CALLBACK_VISIBILITY: {
4311 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004312 final IActivityContainerCallback callback = container.mCallback;
4313 if (callback != null) {
4314 try {
4315 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4316 } catch (RemoteException e) {
4317 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004318 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004319 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004320 case LOCK_TASK_START_MSG: {
4321 // When lock task starts, we disable the status bars.
4322 try {
Jason Monk62515be2014-05-21 16:06:19 -04004323 if (mLockTaskNotify == null) {
4324 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004325 }
Jason Monk62515be2014-05-21 16:06:19 -04004326 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004327 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004328 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004329 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004330 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004331 flags = StatusBarManager.DISABLE_MASK
4332 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004333 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004334 flags = StatusBarManager.DISABLE_MASK
4335 & (~StatusBarManager.DISABLE_BACK)
4336 & (~StatusBarManager.DISABLE_HOME)
4337 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004338 }
4339 getStatusBarService().disable(flags, mToken,
4340 mService.mContext.getPackageName());
4341 }
4342 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004343 if (getDevicePolicyManager() != null) {
4344 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004345 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004346 }
justinzhang5286d3f2014-05-12 17:06:01 -04004347 } catch (RemoteException ex) {
4348 throw new RuntimeException(ex);
4349 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004350 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004351 case LOCK_TASK_END_MSG: {
4352 // When lock task ends, we enable the status bars.
4353 try {
Jason Monk62515be2014-05-21 16:06:19 -04004354 if (getStatusBarService() != null) {
4355 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4356 mService.mContext.getPackageName());
4357 }
4358 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004359 if (getDevicePolicyManager() != null) {
4360 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4361 msg.arg1);
4362 }
Jason Monk62515be2014-05-21 16:06:19 -04004363 if (mLockTaskNotify == null) {
4364 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4365 }
4366 mLockTaskNotify.show(false);
4367 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004368 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004369 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004370 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004371 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004372 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004373 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004374 new LockPatternUtils(mService.mContext)
4375 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004376 }
4377 } catch (SettingNotFoundException e) {
4378 // No setting, don't lock.
4379 }
justinzhang5286d3f2014-05-12 17:06:01 -04004380 } catch (RemoteException ex) {
4381 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004382 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004383 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004384 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004385 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004386 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4387 if (mLockTaskNotify == null) {
4388 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4389 }
4390 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4391 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004392 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4393 final ActivityContainer container = (ActivityContainer) msg.obj;
4394 final IActivityContainerCallback callback = container.mCallback;
4395 if (callback != null) {
4396 try {
4397 callback.onAllActivitiesComplete(container.asBinder());
4398 } catch (RemoteException e) {
4399 }
4400 }
4401 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004402 case LAUNCH_TASK_BEHIND_COMPLETE: {
4403 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004404 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004405 if (r != null) {
4406 handleLaunchTaskBehindCompleteLocked(r);
4407 }
4408 }
4409 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004410
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004411 }
4412 }
4413 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004414
Ying Wangb081a592014-04-22 15:20:16 -07004415 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004416 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4417 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004418 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004419 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004420 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004421 ActivityRecord mParentActivity = null;
4422 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004423
Craig Mautnere3a00d72014-04-16 08:31:19 -07004424 boolean mVisible = true;
4425
Craig Mautner4a1cb222013-12-04 16:14:06 -08004426 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004427 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004428
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004429 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4430 final static int CONTAINER_STATE_NO_SURFACE = 1;
4431 final static int CONTAINER_STATE_FINISHING = 2;
4432 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4433
Wale Ogunwale1666e312016-12-16 11:27:18 -08004434 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004435 synchronized (mService) {
4436 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004437 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004438 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004439
4440 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004441 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004443 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444
Bryce Leeaf691c02017-03-20 14:20:22 -07004445 protected void createStack(int stackId, boolean onTop) {
4446 switch (stackId) {
4447 case PINNED_STACK_ID:
4448 new PinnedActivityStack(this, mRecentTasks, onTop);
4449 break;
4450 default:
4451 new ActivityStack(this, mRecentTasks, onTop);
4452 break;
4453 }
4454 }
4455
Andrii Kulian839def92016-11-02 10:58:58 -07004456 /**
4457 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004458 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004459 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004460 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004461 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004462 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004463 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004464 if (mActivityDisplay != null) {
4465 throw new IllegalStateException("ActivityContainer is already attached, " +
4466 "displayId=" + mActivityDisplay.mDisplayId);
4467 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004468 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004469 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470 }
4471
4472 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004473 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004474 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004475 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4476 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 return;
4478 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004479 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 }
4481 }
4482
4483 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004484 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004485 synchronized (mService) {
4486 if (mActivityDisplay != null) {
4487 return mActivityDisplay.mDisplayId;
4488 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004489 }
4490 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 }
4492
Jeff Brownca9bc702014-02-11 14:32:56 -08004493 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004494 public int getStackId() {
4495 synchronized (mService) {
4496 return mStackId;
4497 }
4498 }
4499
4500 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004501 public boolean injectEvent(InputEvent event) {
4502 final long origId = Binder.clearCallingIdentity();
4503 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004504 synchronized (mService) {
4505 if (mActivityDisplay != null) {
4506 return mInputManagerInternal.injectInputEvent(event,
4507 mActivityDisplay.mDisplayId,
4508 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4509 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004510 }
4511 return false;
4512 } finally {
4513 Binder.restoreCallingIdentity(origId);
4514 }
4515 }
4516
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004517 @Override
4518 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004519 synchronized (mService) {
4520 if (mContainerState == CONTAINER_STATE_FINISHING) {
4521 return;
4522 }
4523 mContainerState = CONTAINER_STATE_FINISHING;
4524
Craig Mautnerd163e752014-06-13 17:18:47 -07004525 long origId = Binder.clearCallingIdentity();
4526 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004527 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004528 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004529 } finally {
4530 Binder.restoreCallingIdentity(origId);
4531 }
4532 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004533 }
4534
Andrii Kulian03c403d2016-11-11 11:14:12 -08004535 /**
4536 * Remove the stack completely. Must be called only when there are no tasks left in it,
4537 * as this method does not finish running activities.
4538 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004539 void removeLocked() {
4540 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004541 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004542 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004543 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004544 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004545 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 }
4547
Andrii Kulian839def92016-11-02 10:58:58 -07004548 /**
4549 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4550 * completely or re-parented.
4551 */
4552 private void removeFromDisplayLocked() {
4553 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4554 + " current displayId=" + mActivityDisplay.mDisplayId);
4555
Wale Ogunwale1666e312016-12-16 11:27:18 -08004556 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004557 mActivityDisplay = null;
4558 }
4559
4560 /**
4561 * Move the stack to specified display.
4562 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004563 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004564 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004565 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004566 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4567 + mActivityDisplay + " to display=" + activityDisplay
4568 + " Callers=" + Debug.getCallers(2));
4569
4570 removeFromDisplayLocked();
4571
4572 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004573 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004574 }
4575
Craig Mautner4a1cb222013-12-04 16:14:06 -08004576 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004577 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004578 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004579 }
4580
4581 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004582 public final int startActivityIntentSender(IIntentSender intentSender)
4583 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004584 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4585
4586 if (!(intentSender instanceof PendingIntentRecord)) {
4587 throw new IllegalArgumentException("Bad PendingIntent object");
4588 }
4589
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004590 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004591 Binder.getCallingUid(), mCurrentUser, false,
4592 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004593
4594 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4595 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4596 pendingIntent.key.requestResolvedType);
4597
4598 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4599 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4600 }
4601
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004602 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004603 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004604 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004605 throw new SecurityException(
4606 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4607 }
4608 }
4609
Craig Mautnerdf88d732014-01-27 09:21:32 -08004610 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004611 public IBinder asBinder() {
4612 return this;
4613 }
4614
Craig Mautner4504de52013-12-20 09:06:56 -08004615 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004616 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004617 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004618 }
4619
Craig Mautner4a1cb222013-12-04 16:14:06 -08004620 ActivityStackSupervisor getOuter() {
4621 return ActivityStackSupervisor.this;
4622 }
4623
Craig Mautnerd163e752014-06-13 17:18:47 -07004624 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004625 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004626 }
4627
Craig Mautner6985bad2014-04-21 15:22:06 -07004628 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004629 void setVisible(boolean visible) {
4630 if (mVisible != visible) {
4631 mVisible = visible;
4632 if (mCallback != null) {
4633 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4634 0 /* unused */, this).sendToTarget();
4635 }
4636 }
4637 }
4638
Craig Mautner6985bad2014-04-21 15:22:06 -07004639 void setDrawn() {
4640 }
4641
Craig Mautner1b4bf852014-05-26 15:06:32 -07004642 // You can always start a new task on a regular ActivityStack.
4643 boolean isEligibleForNewTasks() {
4644 return true;
4645 }
4646
Craig Mautnerd163e752014-06-13 17:18:47 -07004647 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004648 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004649 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004650 }
4651
Craig Mautner34b73df2014-01-12 21:11:08 -08004652 @Override
4653 public String toString() {
4654 return mIdString + (mActivityDisplay == null ? "N" : "A");
4655 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004656 }
4657
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004658 private class VirtualActivityContainer extends ActivityContainer {
4659 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004660 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004661
4662 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004663 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4664 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004665 mParentActivity = parent;
4666 mCallback = callback;
4667 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004668 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004669 }
4670
4671 @Override
4672 public void setSurface(Surface surface, int width, int height, int density) {
4673 super.setSurface(surface, width, height, density);
4674
4675 synchronized (mService) {
4676 final long origId = Binder.clearCallingIdentity();
4677 try {
4678 setSurfaceLocked(surface, width, height, density);
4679 } finally {
4680 Binder.restoreCallingIdentity(origId);
4681 }
4682 }
4683 }
4684
4685 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4686 if (mContainerState == CONTAINER_STATE_FINISHING) {
4687 return;
4688 }
4689 VirtualActivityDisplay virtualActivityDisplay =
4690 (VirtualActivityDisplay) mActivityDisplay;
4691 if (virtualActivityDisplay == null) {
4692 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004693 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004694 mActivityDisplay = virtualActivityDisplay;
4695 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004696 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004697 }
4698
4699 if (mSurface != null) {
4700 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004701 }
4702
Craig Mautner6985bad2014-04-21 15:22:06 -07004703 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004704 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004705 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 } else {
4707 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004708 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004709 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004710 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004711 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004712 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004713
Craig Mautnerd163e752014-06-13 17:18:47 -07004714 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004715
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004716 if (DEBUG_STACK) Slog.d(TAG_STACK,
4717 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004718 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004719
Craig Mautner6985bad2014-04-21 15:22:06 -07004720 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004721 boolean isAttachedLocked() {
4722 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004723 }
4724
4725 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004726 void setDrawn() {
4727 synchronized (mService) {
4728 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004729 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004730 }
4731 }
4732
Craig Mautner1b4bf852014-05-26 15:06:32 -07004733 // Never start a new task on an ActivityView if it isn't explicitly specified.
4734 @Override
4735 boolean isEligibleForNewTasks() {
4736 return false;
4737 }
4738
Craig Mautnerd163e752014-06-13 17:18:47 -07004739 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004740 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004741 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4742 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4743 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4744 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4745 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004746 }
4747 }
4748
Craig Mautner4a1cb222013-12-04 16:14:06 -08004749 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4750 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004751 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004752 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004753 int mDisplayId;
4754 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004755
Craig Mautner4a1cb222013-12-04 16:14:06 -08004756 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4757 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004758 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004759
Jose Lima4b6c6692014-08-12 17:41:12 -07004760 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004761
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004762 /** Array of all UIDs that are present on the display. */
4763 private IntArray mDisplayAccessUIDs = new IntArray();
4764
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004765 ActivityDisplay() {
4766 }
Craig Mautner4504de52013-12-20 09:06:56 -08004767
Craig Mautner1a70a162014-09-13 12:09:31 -07004768 // After instantiation, check that mDisplay is not null before using this. The alternative
4769 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004770 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004771 final Display display = mDisplayManager.getDisplay(displayId);
4772 if (display == null) {
4773 return;
4774 }
4775 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004776 }
4777
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004778 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004779 mDisplay = display;
4780 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004781 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782
Winson Chung7c3ede32017-03-14 14:18:34 -07004783 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004784 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004785 + " to displayId=" + mDisplayId + " position=" + position);
4786 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004787 }
4788
Wale Ogunwale1666e312016-12-16 11:27:18 -08004789 void detachStack(ActivityStack stack) {
4790 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004791 + " from displayId=" + mDisplayId);
4792 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004793 }
4794
Jose Lima4b6c6692014-08-12 17:41:12 -07004795 void setVisibleBehindActivity(ActivityRecord r) {
4796 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004797 }
4798
Jose Lima4b6c6692014-08-12 17:41:12 -07004799 boolean hasVisibleBehindActivity() {
4800 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004801 }
4802
Craig Mautner34b73df2014-01-12 21:11:08 -08004803 @Override
4804 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004805 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4806 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004807
4808 @Override
4809 protected int getChildCount() {
4810 return mStacks.size();
4811 }
4812
4813 @Override
4814 protected ConfigurationContainer getChildAt(int index) {
4815 return mStacks.get(index);
4816 }
4817
4818 @Override
4819 protected ConfigurationContainer getParent() {
4820 return ActivityStackSupervisor.this;
4821 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004822
4823 boolean isPrivate() {
4824 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4825 }
4826
4827 boolean isUidPresent(int uid) {
4828 for (ActivityStack stack : mStacks) {
4829 if (stack.isUidPresent(uid)) {
4830 return true;
4831 }
4832 }
4833 return false;
4834 }
4835
4836 /** Update and get all UIDs that are present on the display and have access to it. */
4837 private IntArray getPresentUIDs() {
4838 mDisplayAccessUIDs.clear();
4839 for (ActivityStack stack : mStacks) {
4840 stack.getPresentUIDs(mDisplayAccessUIDs);
4841 }
4842 return mDisplayAccessUIDs;
4843 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004844
4845 boolean shouldDestroyContentOnRemove() {
4846 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4847 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004848 }
4849
4850 class VirtualActivityDisplay extends ActivityDisplay {
4851 VirtualDisplay mVirtualDisplay;
4852
Craig Mautner6985bad2014-04-21 15:22:06 -07004853 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004854 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004855 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4856 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4857 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4858 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004859
4860 init(mVirtualDisplay.getDisplay());
4861
Bryce Leeaf3a4002017-03-20 10:37:12 -07004862 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004863 }
4864
4865 void setSurface(Surface surface) {
4866 if (mVirtualDisplay != null) {
4867 mVirtualDisplay.setSurface(surface);
4868 }
4869 }
4870
4871 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08004872 void detachStack(ActivityStack stack) {
4873 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004874 if (mVirtualDisplay != null) {
4875 mVirtualDisplay.release();
4876 mVirtualDisplay = null;
4877 }
4878 }
4879
4880 @Override
4881 public String toString() {
4882 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004883 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004884 }
Jose Lima58e66d62014-05-27 20:00:27 -07004885
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004886 ActivityStack findStackBehind(ActivityStack stack) {
4887 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004888 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004889 if (display == null) {
4890 return null;
4891 }
4892 final ArrayList<ActivityStack> stacks = display.mStacks;
4893 for (int i = stacks.size() - 1; i >= 0; i--) {
4894 if (stacks.get(i) == stack && i > 0) {
4895 return stacks.get(i - 1);
4896 }
4897 }
4898 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4899 + " in=" + stacks);
4900 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004901
Jorim Jaggic69bd222016-03-15 14:38:37 +01004902 /**
4903 * Puts a task into resizing mode during the next app transition.
4904 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004905 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004906 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004907 private void setResizingDuringAnimation(TaskRecord task) {
4908 mResizingTasksDuringAnimation.add(task.taskId);
4909 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004910 }
4911
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004912 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4913 final TaskRecord task;
4914 final int callingUid;
4915 final String callingPackage;
4916 final Intent intent;
4917 final int userId;
4918 final ActivityOptions activityOptions = (bOptions != null)
4919 ? new ActivityOptions(bOptions) : null;
4920 final int launchStackId = (activityOptions != null)
4921 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004922 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004923 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004924 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004925 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004926
4927 if (launchStackId == DOCKED_STACK_ID) {
4928 mWindowManager.setDockedStackCreateState(
4929 activityOptions.getDockCreateMode(), null /* initialBounds */);
4930
4931 // Defer updating the stack in which recents is until the app transition is done, to
4932 // not run into issues where we still need to draw the task in recents but the
4933 // docked stack is already created.
Matthew Ng299a01f2016-12-05 11:39:23 -08004934 deferUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004935 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
4936 }
4937
Winson Chung7900bee2017-03-10 08:59:25 -08004938 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
4939 launchStackId);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004940 if (task == null) {
Matthew Ng299a01f2016-12-05 11:39:23 -08004941 continueUpdateBounds(HOME_STACK_ID);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004942 mWindowManager.executeAppTransition();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004943 throw new IllegalArgumentException(
4944 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4945 }
4946
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004947 // Since we don't have an actual source record here, we assume that the currently focused
4948 // activity was the source.
4949 final ActivityStack focusedStack = getFocusedStack();
4950 final ActivityRecord sourceRecord =
4951 focusedStack != null ? focusedStack.topActivity() : null;
4952
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004953 if (launchStackId != INVALID_STACK_ID) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004954 if (task.getStackId() != launchStackId) {
Winson Chung74666102017-02-22 17:49:24 -08004955 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
4956 DEFER_RESUME, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004957 }
4958 }
4959
4960 // If the user must confirm credentials (e.g. when first launching a work app and the
4961 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4962 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4963 && task.getRootActivity() != null) {
Wei Wang65c7a152016-06-02 18:51:22 -07004964 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
Jorim Jaggi09c49542016-04-09 01:39:40 -07004965 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004966 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
Jorim Jaggi1e630c02016-05-16 12:13:13 -07004967 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
4968 task.getTopActivity());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004969
4970 // If we are launching the task in the docked stack, put it into resizing mode so
4971 // the window renders full-screen with the background filling the void. Also only
4972 // call this at the end to make sure that tasks exists on the window manager side.
4973 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004974 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004975 }
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004976
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004977 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Jorim Jaggi6afd1562016-06-01 18:57:54 -07004978 ActivityManager.START_TASK_TO_FRONT,
Bryce Leeaf691c02017-03-20 14:20:22 -07004979 sourceRecord != null ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
Andrii Kulian02b7a832016-10-06 23:11:56 -07004980 sourceRecord, task.getStack());
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004981 return ActivityManager.START_TASK_TO_FRONT;
4982 }
4983 callingUid = task.mCallingUid;
4984 callingPackage = task.mCallingPackage;
4985 intent = task.intent;
4986 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4987 userId = task.userId;
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004988 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
4989 null, null, 0, 0, bOptions, userId, null, task);
4990 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004991 setResizingDuringAnimation(task);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004992 }
4993 return result;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004994 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004995
4996 /**
4997 * @return a list of activities which are the top ones in each visible stack. The first
4998 * entry will be the focused activity.
4999 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005000 List<IBinder> getTopVisibleActivities() {
5001 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5002 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005003 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005004 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5005 // Traverse all stacks on a display.
5006 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5007 final ActivityStack stack = display.mStacks.get(j);
5008 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005009 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005010 == ActivityStack.STACK_VISIBLE) {
5011 final ActivityRecord top = stack.topActivity();
5012 if (top != null) {
5013 if (stack == mFocusedStack) {
5014 topActivityTokens.add(0, top.appToken);
5015 } else {
5016 topActivityTokens.add(top.appToken);
5017 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005018 }
5019 }
5020 }
5021 }
5022 return topActivityTokens;
5023 }
Bryce Lee4a194382017-04-04 14:32:48 -07005024
5025 /**
5026 * Internal container to store a match qualifier alongside a WaitResult.
5027 */
5028 static class WaitInfo {
5029 private final ComponentName mTargetComponent;
5030 private final WaitResult mResult;
5031
5032 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5033 this.mTargetComponent = targetComponent;
5034 this.mResult = result;
5035 }
5036
5037 public boolean matches(ActivityRecord record) {
5038 return mTargetComponent == null ||
5039 (TextUtils.equals(mTargetComponent.getPackageName(), record.info.packageName)
5040 && TextUtils.equals(mTargetComponent.getClassName(), record.info.name));
5041 }
5042
5043 public WaitResult getResult() {
5044 return mResult;
5045 }
5046
5047 public ComponentName getComponent() {
5048 return mTargetComponent;
5049 }
5050
5051 public void dump(PrintWriter pw, String prefix) {
5052 pw.println(prefix + "WaitInfo:");
5053 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5054 pw.println(prefix + " mResult=");
5055 mResult.dump(pw, prefix);
5056 }
5057 }
Craig Mautner27084302013-03-25 08:05:25 -07005058}