blob: 90d9149a2f66a09d03dfccb0ac4104949e0d3793 [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 Kulian3a95edc2017-06-28 16:21:07 -070019import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kuliane6ac20e2017-03-17 10:30:50 -070020import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
22import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
23import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
24import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
25import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
27import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
30import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
31import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
32import static android.app.ActivityManager.StackId.HOME_STACK_ID;
33import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
34import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
35import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080036import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070037import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
38import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020039import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
40import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
41import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070042import static android.os.Process.SYSTEM_UID;
chaviw59b98852017-06-13 12:05:44 -070043import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Jorim Jaggife762342016-10-13 14:33:27 +020044import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
45import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070046import static android.view.Display.FLAG_PRIVATE;
47import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080048import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070049import static android.view.Display.TYPE_VIRTUAL;
Jorim Jaggife762342016-10-13 14:33:27 +020050
Winson Chung47900652017-04-06 18:44:25 -070051import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
75import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBLE_BEHIND;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
77import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
78import static com.android.server.am.ActivityManagerService.ANIMATE;
79import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
80import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
81import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Jorim Jaggife762342016-10-13 14:33:27 +020082import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
83import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
84import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
85import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
86import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
87import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
88import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
89import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
90import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
91import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
92import static com.android.server.am.ActivityStack.STACK_VISIBLE;
93import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
94import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
95import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
96import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
97import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080098import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
99import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
100import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +0200101import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800102import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200103
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800105import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700106import android.annotation.NonNull;
Tony Mak853304c2016-04-18 15:17:41 +0100107import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800110import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700111import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700114import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800115import android.app.IActivityContainerCallback;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -0400118import android.app.StatusBarManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700119import android.app.WaitResult;
Jason Monk35c62a42014-06-17 10:24:47 -0400120import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import android.content.Intent;
125import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700127import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.pm.PackageManager;
129import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100130import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800132import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800133import android.hardware.display.DisplayManager;
134import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -0800135import android.hardware.display.DisplayManagerGlobal;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800136import android.hardware.display.DisplayManagerInternal;
Craig Mautner4504de52013-12-20 09:06:56 -0800137import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -0800138import android.hardware.input.InputManager;
139import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100141import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700142import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700143import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700145import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700146import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700147import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400151import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700152import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700153import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700154import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700155import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100156import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700157import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400159import android.provider.Settings;
160import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700161import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700162import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700163import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700164import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800165import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700166import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700167import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800168import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800170import android.view.Display;
Jeff Brownca9bc702014-02-11 14:32:56 -0800171import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800172import android.view.Surface;
Chong Zhangb15758a2015-11-17 12:12:03 -0800173
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800174import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800175import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700176import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400177import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700178import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400179import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800180import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700181import com.android.server.am.ActivityStack.ActivityState;
Winson Chung19953ca2017-04-11 11:19:23 -0700182import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700183import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700184
Craig Mautner8d341ef2013-03-26 09:03:27 -0700185import java.io.FileDescriptor;
186import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700187import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800188import java.lang.annotation.Retention;
189import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700190import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700191import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700192import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700193import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700194
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800195public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800196 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700198 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700200 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700203 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700204 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700206 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800207 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700212
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700214 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700217 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218
Craig Mautner05d29032013-05-03 13:40:13 -0700219 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
220 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
221 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700223 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
225 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
226 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700227 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400228 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
229 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700230 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700231 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700232 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800233 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
234 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237
Jason Monk62515be2014-05-21 16:06:19 -0400238 private static final String LOCK_TASK_TAG = "Lock-to-App";
239
Wale Ogunwale040b4702015-08-06 18:10:50 -0700240 // Used to indicate if an object (e.g. stack) that we are trying to get
241 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800242 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700243
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700244 // Used to indicate that windows of activities should be preserved during the resize.
245 static final boolean PRESERVE_WINDOWS = true;
246
Wale Ogunwale040b4702015-08-06 18:10:50 -0700247 // Used to indicate if an object (e.g. task) should be moved/created
248 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700249 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700250
251 // Used to indicate that an objects (e.g. task) removal from its container
252 // (e.g. stack) is due to it moving to another container.
253 static final boolean MOVING = true;
254
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700255 // Force the focus to change to the stack we are moving a task to..
256 static final boolean FORCE_FOCUS = true;
257
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700258 // Don't execute any calls to resume.
259 static final boolean DEFER_RESUME = true;
260
Winson Chung010927a2016-12-15 16:12:35 -0800261 // Used to indicate that a task is removed it should also be removed from recents.
262 static final boolean REMOVE_FROM_RECENTS = true;
263
Winson Chung6954fc92017-03-24 16:22:12 -0700264 // Used to indicate that pausing an activity should occur immediately without waiting for
265 // the activity callback indicating that it has completed pausing
266 static final boolean PAUSE_IMMEDIATELY = true;
267
Winson Chung7900bee2017-03-10 08:59:25 -0800268 /**
269 * The modes which affect which tasks are returned when calling
270 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
271 */
272 @Retention(RetentionPolicy.SOURCE)
273 @IntDef({
274 MATCH_TASK_IN_STACKS_ONLY,
275 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
276 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
277 })
278 public @interface AnyTaskForIdMatchTaskMode {}
279 // Match only tasks in the current stacks
280 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
281 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
282 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
283 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
284 // provided stack id
285 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
286
Svetoslav7008b512015-06-24 18:47:07 -0700287 // Activity actions an app cannot start if it uses a permission which is not granted.
288 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
289 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700290
Svetoslav7008b512015-06-24 18:47:07 -0700291 static {
292 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
293 Manifest.permission.CAMERA);
294 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
295 Manifest.permission.CAMERA);
296 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
297 Manifest.permission.CALL_PHONE);
298 }
299
Svet Ganov99b60432015-06-27 13:15:22 -0700300 /** Action restriction: launching the activity is not restricted. */
301 private static final int ACTIVITY_RESTRICTION_NONE = 0;
302 /** Action restriction: launching the activity is restricted by a permission. */
303 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
304 /** Action restriction: launching the activity is restricted by an app op. */
305 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700306
justinzhang5286d3f2014-05-12 17:06:01 -0400307 /** Status Bar Service **/
308 private IBinder mToken = new Binder();
309 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400310 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400311
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700312 // For debugging to make sure the caller when acquiring/releasing our
313 // wake lock is the system process.
314 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800315 /** The number of distinct task ids that can be assigned to the tasks of a single user */
316 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700317
Craig Mautner27084302013-03-25 08:05:25 -0700318 final ActivityManagerService mService;
319
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800320 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800321
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322 final ActivityStackSupervisorHandler mHandler;
323
324 /** Short cut */
325 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700328 /** Counter for next free stack ID to use for dynamic activity stacks. */
329 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800331 /**
332 * Maps the task identifier that activities are currently being started in to the userId of the
333 * task. Each time a new task is created, the entry for the userId of the task is incremented
334 */
335 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700336
Craig Mautner2420ead2013-04-01 17:13:20 -0700337 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800338 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700339
Craig Mautnere0a38842013-12-16 16:14:02 -0800340 /** The stack containing the launcher app. Assumed to always be attached to
341 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800342 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700343
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800345 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700346
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
348 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800349 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800350 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
352 /** List of activities that are waiting for a new activity to become visible before completing
353 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700354 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
355 // mStoppingActivities when something else comes up.
356 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700357
Bryce Lee4a194382017-04-04 14:32:48 -0700358 /** List of processes waiting to find out when a specific activity becomes visible. */
359 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700360
361 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700362 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700363
Craig Mautnerde4ef022013-04-07 19:01:33 -0700364 /** List of activities that are ready to be stopped, but waiting for the next activity to
365 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800366 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700367
Craig Mautnerf3333272013-04-22 10:55:53 -0700368 /** List of activities that are ready to be finished, but waiting for the previous activity to
369 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800370 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700371
Craig Mautner0eea92c2013-05-16 13:35:39 -0700372 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800373 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700374
Wale Ogunwale22e25262016-02-01 10:32:02 -0800375 /** List of activities whose multi-window mode changed that we need to report to the
376 * application */
377 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
378
379 /** List of activities whose picture-in-picture mode changed that we need to report to the
380 * application */
381 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
382
Winson Chung5af42fc2017-03-24 17:11:33 -0700383 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
384 * the application */
385 Rect mPipModeChangedTargetStackBounds;
386
Craig Mautnerf3333272013-04-22 10:55:53 -0700387 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700388 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700389
Craig Mautnerde4ef022013-04-07 19:01:33 -0700390 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
391 * is being brought in front of us. */
392 boolean mUserLeaving = false;
393
Craig Mautner0eea92c2013-05-16 13:35:39 -0700394 /** Set when we have taken too long waiting to go to sleep. */
395 boolean mSleepTimeout = false;
396
397 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398 * We don't want to allow the device to go to sleep while in the process
399 * of launching an activity. This is primarily to allow alarm intent
400 * receivers to launch an activity and get that to run before the device
401 * goes back to sleep.
402 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700403 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404
405 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406 * Set when the system is going to sleep, until we have
407 * successfully paused the current activity and released our wake lock.
408 * At that point the system is allowed to actually sleep.
409 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700411
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700412 /** Stack id of the front stack when user switched, indexed by userId. */
413 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700414
Craig Mautner4504de52013-12-20 09:06:56 -0800415 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Wale Ogunwale1666e312016-12-16 11:27:18 -0800417 SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418
Bryce Leeaf3a4002017-03-20 10:37:12 -0700419 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700421 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800422
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800423 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
424
425 private DisplayManagerInternal mDisplayManagerInternal;
426 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800427
Craig Mautner15df08a2015-04-01 12:17:18 -0700428 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
429 * may be finished until there is only one entry left. If this is empty the system is not
430 * in lockTask mode. */
431 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000432 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700433 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
434 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000435 */
436 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400437 /**
438 * Notifies the user when entering/exiting lock-task.
439 */
440 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800441
Wale Ogunwaled046a012015-12-24 13:05:59 -0800442 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800443 boolean inResumeTopActivity;
444
Andrii Kulian1e32e022016-09-16 15:29:34 -0700445 /**
446 * Temporary rect used during docked stack resize calculation so we don't need to create a new
447 * object each time.
448 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700449 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700450
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700451 // The default minimal size that will be used if the activity doesn't specify its minimal size.
452 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700453 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700454
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700455 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
456 private boolean mTaskLayersChanged = true;
457
Jorim Jaggi275561a2016-02-23 10:11:02 -0500458 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800459
Andrii Kulian1779e612016-10-12 21:58:25 -0700460 @Override
461 protected int getChildCount() {
462 return mActivityDisplays.size();
463 }
464
465 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700466 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700467 return mActivityDisplays.valueAt(index);
468 }
469
470 @Override
471 protected ConfigurationContainer getParent() {
472 return null;
473 }
474
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700475 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700476 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700477 if (activityDisplay == null) {
478 throw new IllegalArgumentException("No display found with id: " + displayId);
479 }
480
481 return activityDisplay.getOverrideConfiguration();
482 }
483
484 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700485 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700486 if (activityDisplay == null) {
487 throw new IllegalArgumentException("No display found with id: " + displayId);
488 }
489
490 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
491 }
492
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700493 /** Check if placing task or activity on specified display is allowed. */
494 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable) {
495 return displayId == DEFAULT_DISPLAY || (mService.mSupportsMultiDisplay
496 && (resizeable || displayConfigMatchesGlobal(displayId)));
497 }
498
499 /**
500 * Check if configuration of specified display matches current global config.
501 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
502 * the same config as on the default display.
503 * @param displayId Id of the display to check.
504 * @return {@code true} if configuration matches.
505 */
506 private boolean displayConfigMatchesGlobal(int displayId) {
507 if (displayId == DEFAULT_DISPLAY) {
508 return true;
509 }
510 if (displayId == INVALID_DISPLAY) {
511 return false;
512 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700513 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700514 if (targetDisplay == null) {
515 throw new IllegalArgumentException("No display found with id: " + displayId);
516 }
517 return getConfiguration().equals(targetDisplay.getConfiguration());
518 }
519
Wale Ogunwale39381972015-12-17 17:15:29 -0800520 static class FindTaskResult {
521 ActivityRecord r;
522 boolean matchedByRootAffinity;
523 }
524 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
525
Craig Mautneree36c772014-07-16 14:56:05 -0700526 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800527 * Temp storage for display ids sorted in focus order.
528 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
529 * it's more efficient, as the number of displays is usually small.
530 */
531 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
532
533 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100534 * Used to keep track whether app visibilities got changed since the last pause. Useful to
535 * determine whether to invoke the task stack change listener after pausing.
536 */
537 boolean mAppVisibilitiesChangedSinceLastPause;
538
539 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100540 * Set of tasks that are in resizing mode during an app transition to fill the "void".
541 */
542 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
543
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700544
545 /**
546 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
547 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
548 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
549 * like the docked stack going empty.
550 */
551 private boolean mAllowDockedStackResize = true;
552
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100553 /**
Tony Mak853304c2016-04-18 15:17:41 +0100554 * Is dock currently minimized.
555 */
556 boolean mIsDockMinimized;
557
Jorim Jaggife762342016-10-13 14:33:27 +0200558 final KeyguardController mKeyguardController;
559
chaviw59b98852017-06-13 12:05:44 -0700560 private PowerManager mPowerManager;
561 private int mDeferResumeCount;
562
Tony Mak853304c2016-04-18 15:17:41 +0100563 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700564 * Description of a request to start a new activity, which has been held
565 * due to app switches being disabled.
566 */
567 static class PendingActivityLaunch {
568 final ActivityRecord r;
569 final ActivityRecord sourceRecord;
570 final int startFlags;
571 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700572 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700573
574 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700575 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700576 r = _r;
577 sourceRecord = _sourceRecord;
578 startFlags = _startFlags;
579 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700580 callerApp = _callerApp;
581 }
582
583 void sendErrorResult(String message) {
584 try {
585 if (callerApp.thread != null) {
586 callerApp.thread.scheduleCrash(message);
587 }
588 } catch (RemoteException e) {
589 Slog.e(TAG, "Exception scheduling crash of failed "
590 + "activity launcher sourceRecord=" + sourceRecord, e);
591 }
Craig Mautneree36c772014-07-16 14:56:05 -0700592 }
593 }
594
Bryce Leeaf691c02017-03-20 14:20:22 -0700595 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700596 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700597 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800598 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200599 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700600 }
601
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800602 void setRecentTasks(RecentTasks recentTasks) {
603 mRecentTasks = recentTasks;
604 }
605
Jeff Brown2c43c332014-06-12 22:38:59 -0700606 /**
607 * At the time when the constructor runs, the power manager has not yet been
608 * initialized. So we initialize our wakelocks afterwards.
609 */
610 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700611 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
612 mGoingToSleep = mPowerManager
613 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
614 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700615 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700616 }
617
justinzhang5286d3f2014-05-12 17:06:01 -0400618 // This function returns a IStatusBarService. The value is from ServiceManager.
619 // getService and is cached.
620 private IStatusBarService getStatusBarService() {
621 synchronized (mService) {
622 if (mStatusBarService == null) {
623 mStatusBarService = IStatusBarService.Stub.asInterface(
624 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
625 if (mStatusBarService == null) {
626 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
627 }
628 }
629 return mStatusBarService;
630 }
631 }
632
Jason Monk35c62a42014-06-17 10:24:47 -0400633 private IDevicePolicyManager getDevicePolicyManager() {
634 synchronized (mService) {
635 if (mDevicePolicyManager == null) {
636 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
637 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
638 if (mDevicePolicyManager == null) {
639 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
640 }
641 }
642 return mDevicePolicyManager;
643 }
644 }
645
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700646 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 synchronized (mService) {
648 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200649 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650
651 mDisplayManager =
652 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
653 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800654 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655
656 Display[] displays = mDisplayManager.getDisplays();
657 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
658 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800659 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700660 if (activityDisplay.mDisplay == null) {
661 throw new IllegalStateException("Default Display does not exist");
662 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800663 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700664 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 }
666
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800667 mHomeStack = mFocusedStack = mLastFocusedStack =
668 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800669
670 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 }
Craig Mautner27084302013-03-25 08:05:25 -0700672 }
673
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700674 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700680 }
681
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700682 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700683 if (stack == null) {
684 return false;
685 }
686
Craig Mautnerdf88d732014-01-27 09:21:32 -0800687 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
688 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700689 stack = parent.getStack();
Craig Mautnerdf88d732014-01-27 09:21:32 -0800690 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800691 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
693
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800694 /** The top most stack on its display. */
695 boolean isFrontStackOnDisplay(ActivityStack stack) {
696 return isFrontOfStackList(stack, stack.mActivityContainer.mActivityDisplay.mStacks);
697 }
698
699 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700700 if (stack == null) {
701 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700703
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
705 if (parent != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700706 stack = parent.getStack();
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800707 }
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800708 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700709 }
710
Wale Ogunwaled046a012015-12-24 13:05:59 -0800711 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800712 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
713 if (!focusCandidate.isFocusable()) {
714 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800715 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800716 }
717
718 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800719 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800720 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800721
Wale Ogunwaled046a012015-12-24 13:05:59 -0800722 EventLogTags.writeAmFocusedStack(
723 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
724 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
725 }
726
727 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800728 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800729 if (r != null && r.idle) {
730 checkFinishBootingLocked();
731 }
732 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700733 }
734
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700735 void moveHomeStackToFront(String reason) {
736 mHomeStack.moveToFront(reason);
737 }
738
Matthew Ng330757d2017-02-28 14:19:17 -0800739 void moveRecentsStackToFront(String reason) {
740 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
741 if (recentsStack != null) {
742 recentsStack.moveToFront(reason);
743 }
744 }
745
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700746 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800747 boolean moveHomeStackTaskToTop(String reason) {
748 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700749
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700750 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700751 if (top == null) {
752 return false;
753 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700754 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700755 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700756 }
757
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800758 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700759 if (!mService.mBooting && !mService.mBooted) {
760 // Not ready yet!
761 return false;
762 }
763
Craig Mautner84984fa2014-06-19 11:19:20 -0700764 if (prev != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700765 prev.getTask().setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner84984fa2014-06-19 11:19:20 -0700766 }
767
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800768 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700769 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800770 final String myReason = reason + " resumeHomeStackTask";
771
Mark Lua56ea122015-10-08 13:31:01 +0800772 // Only resume home activity if isn't finishing.
773 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700774 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800775 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700776 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800777 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700778 }
779
Craig Mautner8d341ef2013-03-26 09:03:27 -0700780 TaskRecord anyTaskForIdLocked(int id) {
Winson Chung7900bee2017-03-10 08:59:25 -0800781 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
782 INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700783 }
784
785 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700786 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700787 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800788 * @param matchMode The mode to match the given task id in.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700789 * @param stackId The stack to restore the task to (default launch stack will be used if
Winson Chung7900bee2017-03-10 08:59:25 -0800790 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}). Only
791 * valid if the matchMode is
792 * {@link #MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE}.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700793 */
Winson Chung7900bee2017-03-10 08:59:25 -0800794 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode, int stackId) {
795 // If there is a stack id set, ensure that we are attempting to actually restore a task
796 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE &&
797 stackId != INVALID_STACK_ID) {
798 throw new IllegalArgumentException("Should not specify stackId for non-restore lookup");
799 }
800
Craig Mautnere0a38842013-12-16 16:14:02 -0800801 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800802 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800803 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800804 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
805 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700806 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800807 if (task != null) {
808 return task;
809 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700810 }
811 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800812
Winson Chung7900bee2017-03-10 08:59:25 -0800813 // If we are matching stack tasks only, return now
814 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800815 return null;
816 }
817
Winson Chung7900bee2017-03-10 08:59:25 -0800818 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
819 // the task from recents
820 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700821 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
822
823 if (task == null) {
824 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800825 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
826 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700827
828 return null;
829 }
830
831 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700832 return task;
833 }
834
Winson Chung7900bee2017-03-10 08:59:25 -0800835 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700836 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700837 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
838 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800839 return null;
840 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700841 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800842 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700843 }
844
Craig Mautner6170f732013-04-02 13:05:23 -0700845 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800848 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
850 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
851 if (r != null) {
852 return r;
853 }
Craig Mautner6170f732013-04-02 13:05:23 -0700854 }
855 }
856 return null;
857 }
858
Tony Mak853304c2016-04-18 15:17:41 +0100859 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000860 * Detects whether we should show a lock screen in front of this task for a locked user.
861 * <p>
862 * We'll do this if either of the following holds:
863 * <ul>
864 * <li>The top activity explicitly belongs to {@param userId}.</li>
865 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
866 * </ul>
867 *
868 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100869 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000870 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
871 // To handle the case that work app is in the task but just is not the top one.
872 final ActivityRecord activityRecord = task.getTopActivity();
873 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
874
875 return (activityRecord != null && activityRecord.userId == userId)
876 || (resultTo != null && resultTo.userId == userId);
877 }
878
879 /**
880 * Find all visible task stacks containing {@param userId} and intercept them with an activity
881 * to block out the contents and possibly start a credential-confirming intent.
882 *
883 * @param userId user handle for the locked managed profile.
884 */
885 void lockAllProfileTasks(@UserIdInt int userId) {
886 mWindowManager.deferSurfaceLayout();
887 try {
888 final List<ActivityStack> stacks = getStacks();
889 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
890 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
891 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
892 final TaskRecord task = tasks.get(taskNdx);
893
894 // Check the task for a top activity belonging to userId, or returning a result
895 // to an activity belonging to userId. Example case: a document picker for
896 // personal files, opened by a work app, should still get locked.
897 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000898 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
899 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000900 }
Tony Mak853304c2016-04-18 15:17:41 +0100901 }
902 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000903 } finally {
904 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100905 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000906 }
907
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800908 void setNextTaskIdForUserLocked(int taskId, int userId) {
909 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
910 if (taskId > currentTaskId) {
911 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700912 }
913 }
914
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700915 static int nextTaskIdForUser(int taskId, int userId) {
916 int nextTaskId = taskId + 1;
917 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
918 // Wrap around as there will be smaller task ids that are available now.
919 nextTaskId -= MAX_TASK_IDS_PER_USER;
920 }
921 return nextTaskId;
922 }
923
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800924 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800925 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
926 // for a userId u, a taskId can only be in the range
927 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
928 // 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 -0700929 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800930 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Winson Chung7900bee2017-03-10 08:59:25 -0800931 || anyTaskForIdLocked(candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800932 INVALID_STACK_ID) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700933 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800934 if (candidateTaskId == currentTaskId) {
935 // Something wrong!
936 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
937 throw new IllegalStateException("Cannot get an available task id."
938 + " Reached limit of " + MAX_TASK_IDS_PER_USER
939 + " running tasks per user.");
940 }
941 }
942 mCurTaskIdForUser.put(userId, candidateTaskId);
943 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700944 }
945
Chong Zhang6cda19c2016-06-14 19:07:56 -0700946 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800947 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700948 if (stack == null) {
949 return null;
950 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 ActivityRecord resumedActivity = stack.mResumedActivity;
952 if (resumedActivity == null || resumedActivity.app == null) {
953 resumedActivity = stack.mPausingActivity;
954 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700955 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 }
957 }
958 return resumedActivity;
959 }
960
Dianne Hackbornff072722014-09-24 10:56:28 -0700961 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700962 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800964 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
965 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
967 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700968 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 continue;
970 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700971 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 if (hr != null) {
973 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
974 && processName.equals(hr.processName)) {
975 try {
George Mount2c92c972014-03-20 09:38:23 -0700976 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800977 didSomething = true;
978 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700979 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 Slog.w(TAG, "Exception in new application when starting activity "
981 + hr.intent.getComponent().flattenToShortString(), e);
982 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700983 }
Craig Mautner20e72272013-04-01 13:45:53 -0700984 }
Craig Mautner20e72272013-04-01 13:45:53 -0700985 }
986 }
987 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700988 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700989 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700990 }
Craig Mautner20e72272013-04-01 13:45:53 -0700991 return didSomething;
992 }
993
994 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800995 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
996 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
998 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700999 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001000 continue;
1001 }
1002 final ActivityRecord resumedActivity = stack.mResumedActivity;
1003 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001004 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001005 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001006 return false;
1007 }
Craig Mautner20e72272013-04-01 13:45:53 -07001008 }
1009 }
Wei Wang65c7a152016-06-02 18:51:22 -07001010 // Send launch end powerhint when idle
1011 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001012 return true;
1013 }
1014
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001016 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1017 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001018 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1019 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001020 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001021 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001022 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 return false;
1024 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 }
1026 }
1027 }
1028 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001029 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001030 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1031 mLastFocusedStack + " to=" + mFocusedStack);
1032 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 return true;
1034 }
1035
1036 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001038 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1039 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1041 final ActivityStack stack = stacks.get(stackNdx);
1042 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001043 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001044 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001045 return false;
1046 }
1047 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001048 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001049 }
1050 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001051 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001052 }
1053
Craig Mautner2acc3892013-09-23 10:28:14 -07001054 /**
1055 * Pause all activities in either all of the stacks or just the back stacks.
1056 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001057 * @param resuming The resuming activity.
1058 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1059 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001060 * @return true if any activity was paused as a result of this call.
1061 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001062 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001063 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001064 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1065 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001066 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1067 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001068 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001069 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001070 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001071 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1072 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001074 }
1075 }
1076 return someActivityPaused;
1077 }
1078
Craig Mautnerde4ef022013-04-07 19:01:33 -07001079 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001080 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001081 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1082 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1084 final ActivityStack stack = stacks.get(stackNdx);
1085 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001086 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001088 Slog.d(TAG_STATES,
1089 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001090 pausing = false;
1091 } else {
1092 return false;
1093 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001094 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 }
1096 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001097 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001098 }
1099
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001100 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
Wale Ogunwale950faff2016-08-08 09:51:04 -07001101 ActivityRecord resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -05001102 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -08001103 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1104 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1105 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1106 final ActivityStack stack = stacks.get(stackNdx);
1107 if (stack.mResumedActivity != null &&
1108 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001109 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001110 }
1111 }
1112 }
1113 }
1114
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001115 void cancelInitializingActivities() {
1116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1117 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1118 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1119 stacks.get(stackNdx).cancelInitializingActivities();
1120 }
1121 }
1122 }
1123
Bryce Lee4a194382017-04-04 14:32:48 -07001124 void waitActivityVisible(ComponentName name, WaitResult result) {
1125 final WaitInfo waitInfo = new WaitInfo(name, result);
1126 mWaitingForActivityVisible.add(waitInfo);
1127 }
1128
1129 void cleanupActivity(ActivityRecord r) {
1130 // Make sure this record is no longer in the pending finishes list.
1131 // This could happen, for example, if we are trimming activities
1132 // down to the max limit while they are still waiting to finish.
1133 mFinishingActivities.remove(r);
1134 mActivitiesWaitingForVisibleActivity.remove(r);
1135
1136 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001137 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001138 mWaitingForActivityVisible.remove(i);
1139 }
1140 }
1141 }
1142
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001143 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001144 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001145 }
1146
1147 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1148 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001149 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1150 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001151 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001152 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001153 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001154 result.timeout = false;
1155 result.who = w.getComponent();
1156 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1157 result.thisTime = result.totalTime;
1158 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001161 if (changed) {
1162 mService.notifyAll();
1163 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001164 }
1165
Chong Zhang5022da32016-06-21 16:31:37 -07001166 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1167 boolean changed = false;
1168 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1169 WaitResult w = mWaitingActivityLaunched.remove(i);
1170 if (w.who == null) {
1171 changed = true;
1172 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1173 // the starting activity ends up moving another activity to front, and it should
1174 // wait for this new activity to become visible instead.
1175 // Do not modify other fields.
1176 w.result = START_TASK_TO_FRONT;
1177 }
1178 }
1179 if (changed) {
1180 mService.notifyAll();
1181 }
1182 }
1183
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001184 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1185 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001186 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001188 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001189 if (w.who == null) {
1190 changed = true;
1191 w.timeout = timeout;
1192 if (r != null) {
1193 w.who = new ComponentName(r.info.packageName, r.info.name);
1194 }
1195 w.thisTime = thisTime;
1196 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001197 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001198 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001200 if (changed) {
1201 mService.notifyAll();
1202 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001203 }
1204
Craig Mautner29219d92013-04-16 20:19:12 -07001205 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001206 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001207 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001208 if (r != null) {
1209 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001210 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001211
Andrii Kulian7318d632016-07-20 18:59:28 -07001212 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001213 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1214
1215 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1216 final int displayId = mTmpOrderedDisplayIds.get(i);
1217 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1218 if (stacks == null) {
1219 continue;
1220 }
1221 for (int j = stacks.size() - 1; j >= 0; --j) {
1222 final ActivityStack stack = stacks.get(j);
1223 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1224 r = stack.topRunningActivityLocked();
1225 if (r != null) {
1226 return r;
1227 }
Craig Mautner29219d92013-04-16 20:19:12 -07001228 }
1229 }
1230 }
1231 return null;
1232 }
1233
Dianne Hackborn09233282014-04-30 11:33:59 -07001234 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001235 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001236 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1237 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001238 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001239 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001240 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001241 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1242 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001244 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001245 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001246 }
1247 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001248
1249 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1250 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1251 while (maxNum > 0) {
1252 long mostRecentActiveTime = Long.MIN_VALUE;
1253 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001254 final int numTaskLists = runningTaskLists.size();
1255 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1256 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001257 if (!stackTaskList.isEmpty()) {
1258 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1259 if (lastActiveTime > mostRecentActiveTime) {
1260 mostRecentActiveTime = lastActiveTime;
1261 selectedStackList = stackTaskList;
1262 }
1263 }
1264 }
1265 if (selectedStackList != null) {
1266 list.add(selectedStackList.remove(0));
1267 --maxNum;
1268 } else {
1269 break;
1270 }
1271 }
Craig Mautner20e72272013-04-01 13:45:53 -07001272 }
1273
Todd Kennedy7440f172015-12-09 14:31:22 -08001274 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1275 ProfilerInfo profilerInfo) {
1276 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001277 if (aInfo != null) {
1278 // Store the found target back into the intent, because now that
1279 // we have it we never want to do this again. For example, if the
1280 // user navigates back to this point in the history, we should
1281 // always restart the exact same activity.
1282 intent.setComponent(new ComponentName(
1283 aInfo.applicationInfo.packageName, aInfo.name));
1284
1285 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001286 if (!aInfo.processName.equals("system")) {
1287 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001288 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001289 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001290
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001291 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1292 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1293 }
1294
Man Caocfa78b22015-06-11 20:14:34 -07001295 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1296 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1297 }
1298
1299 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001300 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001301 }
1302 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001303 final String intentLaunchToken = intent.getLaunchToken();
1304 if (aInfo.launchToken == null && intentLaunchToken != null) {
1305 aInfo.launchToken = intentLaunchToken;
1306 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001307 }
1308 return aInfo;
1309 }
1310
Todd Kennedy7440f172015-12-09 14:31:22 -08001311 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001312 return resolveIntent(intent, resolvedType, userId, 0);
1313 }
1314
1315 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001316 synchronized (mService) {
1317 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001318 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001319 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001320 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001321 }
1322
1323 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1324 ProfilerInfo profilerInfo, int userId) {
1325 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1326 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1327 }
1328
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001329 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1330 boolean andResume, boolean checkConfig) throws RemoteException {
1331
1332 if (!allPausedActivitiesComplete()) {
1333 // While there are activities pausing we skipping starting any new activities until
1334 // pauses are complete. NOTE: that we also do this for activities that are starting in
1335 // the paused state because they will first be resumed then paused on the client side.
1336 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1337 "realStartActivityLocked: Skipping start of r=" + r
1338 + " some activities pausing...");
1339 return false;
1340 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001341
Bryce Leeaf691c02017-03-20 14:20:22 -07001342 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001343 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001344
1345 beginDeferResume();
1346
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 try {
chaviw59b98852017-06-13 12:05:44 -07001348 r.startFreezingScreenLocked(app, 0);
1349
1350 // schedule launch ticks to collect information about slow apps.
1351 r.startLaunchTickingLocked();
1352
1353 r.app = app;
1354
1355 // Have the window manager re-evaluate the orientation of the screen based on the new
1356 // activity order. Note that as a result of this, it can call back into the activity
1357 // manager with a new orientation. We don't care about that, because the activity is
1358 // not currently running so we are just restarting it anyway.
1359 if (checkConfig) {
1360 final int displayId = r.getDisplayId();
1361 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1362 getDisplayOverrideConfiguration(displayId),
1363 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1364 // Deferring resume here because we're going to launch new activity shortly.
1365 // We don't want to perform a redundant launch of the same record while ensuring
1366 // configurations and trying to resume top activity of focused stack.
1367 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1368 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001369 }
chaviw59b98852017-06-13 12:05:44 -07001370
1371 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1372 true /* isTop */)) {
1373 // We only set the visibility to true if the activity is allowed to be visible
1374 // based on
1375 // keyguard state. This avoids setting this into motion in window manager that is
1376 // later cancelled due to later calls to ensure visible activities that set
1377 // visibility back to false.
1378 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 }
chaviw59b98852017-06-13 12:05:44 -07001380
1381 if (mKeyguardController.isKeyguardLocked()) {
1382 r.notifyUnknownVisibilityLaunched();
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 }
chaviw59b98852017-06-13 12:05:44 -07001384 final int applicationInfoUid =
1385 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1386 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1387 Slog.wtf(TAG,
1388 "User ID for activity changing for " + r
1389 + " appInfo.uid=" + r.appInfo.uid
1390 + " info.ai.uid=" + applicationInfoUid
1391 + " old=" + r.app + " new=" + app);
1392 }
1393
1394 app.waitingToKill = null;
1395 r.launchCount++;
1396 r.lastLaunchTime = SystemClock.uptimeMillis();
1397
1398 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1399
1400 int idx = app.activities.indexOf(r);
1401 if (idx < 0) {
1402 app.activities.add(r);
1403 }
1404 mService.updateLruProcessLocked(app, true, null);
1405 mService.updateOomAdjLocked();
1406
1407 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1408 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
1409 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE",
1410 false);
1411 }
1412
1413 try {
1414 if (app.thread == null) {
1415 throw new RemoteException();
1416 }
1417 List<ResultInfo> results = null;
1418 List<ReferrerIntent> newIntents = null;
1419 if (andResume) {
1420 // We don't need to deliver new intents and/or set results if activity is going
1421 // to pause immediately after launch.
1422 results = r.results;
1423 newIntents = r.newIntents;
1424 }
1425 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1426 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1427 + " newIntents=" + newIntents + " andResume=" + andResume);
1428 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1429 System.identityHashCode(r), task.taskId, r.shortComponentName);
1430 if (r.isHomeActivity()) {
1431 // Home process is the root process of the task.
1432 mService.mHomeProcess = task.mActivities.get(0).app;
1433 }
1434 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1435 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1436 r.sleeping = false;
1437 r.forceNewConfig = false;
1438 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1439 mService.showAskCompatModeDialogLocked(r);
1440 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1441 ProfilerInfo profilerInfo = null;
1442 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1443 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1444 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001445 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1446 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1447 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001448 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001449 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001450 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001451 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001452 }
1453 }
chaviw59b98852017-06-13 12:05:44 -07001454
Andreas Gampe2b073a02017-06-22 17:28:57 -07001455 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001456 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001457 }
1458 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001459
chaviw59b98852017-06-13 12:05:44 -07001460 app.hasShownUi = true;
1461 app.pendingUiClean = true;
1462 app.forceProcessStateUpTo(mService.mTopProcessState);
1463 // Because we could be starting an Activity in the system process this may not go
1464 // across a Binder interface which would create a new Configuration. Consequently
1465 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001466
chaviw59b98852017-06-13 12:05:44 -07001467 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1468 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1469 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001470
chaviw59b98852017-06-13 12:05:44 -07001471 logIfTransactionTooLarge(r.intent, r.icicle);
1472 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1473 System.identityHashCode(r), r.info,
1474 // TODO: Have this take the merged configuration instead of separate global
1475 // and override configs.
1476 mergedConfiguration.getGlobalConfiguration(),
1477 mergedConfiguration.getOverrideConfiguration(), r.compat,
1478 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1479 r.persistentState, results, newIntents, !andResume,
1480 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001481
chaviw59b98852017-06-13 12:05:44 -07001482 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1483 // This may be a heavy-weight process! Note that the package
1484 // manager will ensure that only activity can run in the main
1485 // process of the .apk, which is the only thing that will be
1486 // considered heavy-weight.
1487 if (app.processName.equals(app.info.packageName)) {
1488 if (mService.mHeavyWeightProcess != null
1489 && mService.mHeavyWeightProcess != app) {
1490 Slog.w(TAG, "Starting new heavy weight process " + app
1491 + " when already running "
1492 + mService.mHeavyWeightProcess);
1493 }
1494 mService.mHeavyWeightProcess = app;
1495 Message msg = mService.mHandler.obtainMessage(
1496 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1497 msg.obj = r;
1498 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001499 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001500 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001501
chaviw59b98852017-06-13 12:05:44 -07001502 } catch (RemoteException e) {
1503 if (r.launchFailed) {
1504 // This is the second time we failed -- finish activity
1505 // and give up.
1506 Slog.e(TAG, "Second failure launching "
1507 + r.intent.getComponent().flattenToShortString()
1508 + ", giving up", e);
1509 mService.appDiedLocked(app);
1510 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1511 "2nd-crash", false);
1512 return false;
1513 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001514
chaviw59b98852017-06-13 12:05:44 -07001515 // This is the first time we failed -- restart process and
1516 // retry.
1517 r.launchFailed = true;
1518 app.activities.remove(r);
1519 throw e;
1520 }
1521 } finally {
1522 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001523 }
1524
1525 r.launchFailed = false;
1526 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001527 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001528 }
1529
chaviw59b98852017-06-13 12:05:44 -07001530 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001531 // As part of the process of launching, ActivityThread also performs
1532 // a resume.
1533 stack.minimalResumeActivityLocked(r);
1534 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001535 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001536 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001537 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001538 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001539 "Moving to PAUSED: " + r + " (starting in paused state)");
1540 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001541 }
1542
1543 // Launch the new version setup screen if needed. We do this -after-
1544 // launching the initial activity (that is, home), so that it can have
1545 // a chance to initialize itself while in the background, making the
1546 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001547 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001548 mService.startSetupActivityLocked();
1549 }
1550
Dianne Hackborn465fa392014-09-14 14:21:18 -07001551 // Update any services we are bound to that might care about whether
1552 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001553 if (r.app != null) {
1554 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1555 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001556
Craig Mautner2420ead2013-04-01 17:13:20 -07001557 return true;
1558 }
1559
Sudheer Shankafab200f2017-05-17 20:41:53 -07001560 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1561 int extrasSize = 0;
1562 if (intent != null) {
1563 final Bundle extras = intent.getExtras();
1564 if (extras != null) {
1565 extrasSize = extras.getSize();
1566 }
1567 }
1568 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1569 if (extrasSize + icicleSize > 200000) {
1570 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1571 + ", icicle size: " + icicleSize);
1572 }
1573 }
1574
Craig Mautnere79d42682013-04-01 19:01:53 -07001575 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001576 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001577 // Is this activity's application already running?
1578 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001579 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001580
Andrii Kulian02b7a832016-10-06 23:11:56 -07001581 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001582
1583 if (app != null && app.thread != null) {
1584 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001585 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1586 || !"android".equals(r.info.packageName)) {
1587 // Don't add this if it is a platform component that is marked
1588 // to run in multiple processes, because this is actually
1589 // part of the framework so doesn't make sense to track as a
1590 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001591 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1592 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001593 }
George Mount2c92c972014-03-20 09:38:23 -07001594 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001595 return;
1596 } catch (RemoteException e) {
1597 Slog.w(TAG, "Exception when starting activity "
1598 + r.intent.getComponent().flattenToShortString(), e);
1599 }
1600
1601 // If a dead object exception was thrown -- fall through to
1602 // restart the application.
1603 }
1604
1605 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001606 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001607 }
1608
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001609 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001610 String resultWho, int requestCode, int callingPid, int callingUid,
1611 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001612 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001613 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1614 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001615 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001616 return true;
1617 }
1618 final int componentRestriction = getComponentRestrictionForCallingPackage(
1619 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1620 final int actionRestriction = getActionRestrictionForCallingPackage(
1621 intent.getAction(), callingPackage, callingPid, callingUid);
1622 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1623 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1624 if (resultRecord != null) {
1625 resultStack.sendActivityResultLocked(-1,
1626 resultRecord, resultWho, requestCode,
1627 Activity.RESULT_CANCELED, null);
1628 }
1629 final String msg;
1630 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1631 msg = "Permission Denial: starting " + intent.toString()
1632 + " from " + callerApp + " (pid=" + callingPid
1633 + ", uid=" + callingUid + ")" + " with revoked permission "
1634 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1635 } else if (!aInfo.exported) {
1636 msg = "Permission Denial: starting " + intent.toString()
1637 + " from " + callerApp + " (pid=" + callingPid
1638 + ", uid=" + callingUid + ")"
1639 + " not exported from uid " + aInfo.applicationInfo.uid;
1640 } else {
1641 msg = "Permission Denial: starting " + intent.toString()
1642 + " from " + callerApp + " (pid=" + callingPid
1643 + ", uid=" + callingUid + ")"
1644 + " requires " + aInfo.permission;
1645 }
1646 Slog.w(TAG, msg);
1647 throw new SecurityException(msg);
1648 }
1649
1650 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1651 final String message = "Appop Denial: starting " + intent.toString()
1652 + " from " + callerApp + " (pid=" + callingPid
1653 + ", uid=" + callingUid + ")"
1654 + " requires " + AppOpsManager.permissionToOp(
1655 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1656 Slog.w(TAG, message);
1657 return false;
1658 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1659 final String message = "Appop Denial: starting " + intent.toString()
1660 + " from " + callerApp + " (pid=" + callingPid
1661 + ", uid=" + callingUid + ")"
1662 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1663 Slog.w(TAG, message);
1664 return false;
1665 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001666 if (options != null) {
1667 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1668 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1669 callingPid, callingUid);
1670 if (startInTaskPerm != PERMISSION_GRANTED) {
1671 final String msg = "Permission Denial: starting " + intent.toString()
1672 + " from " + callerApp + " (pid=" + callingPid
1673 + ", uid=" + callingUid + ") with launchTaskId="
1674 + options.getLaunchTaskId();
1675 Slog.w(TAG, msg);
1676 throw new SecurityException(msg);
1677 }
1678 }
1679 // Check if someone tries to launch an activity on a private display with a different
1680 // owner.
1681 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001682 if (launchDisplayId != INVALID_DISPLAY
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001683 && !isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, launchDisplayId)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 final String msg = "Permission Denial: starting " + intent.toString()
1685 + " from " + callerApp + " (pid=" + callingPid
1686 + ", uid=" + callingUid + ") with launchDisplayId="
1687 + launchDisplayId;
1688 Slog.w(TAG, msg);
1689 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001690 }
1691 }
1692
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001693 return true;
1694 }
1695
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001696 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001697 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001698 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1699 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1700
Andrii Kulian62e6f252017-05-30 22:46:53 -07001701 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001702 if (activityDisplay == null) {
1703 Slog.w(TAG, "Launch on display check: display not found");
1704 return false;
1705 }
1706
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001707 // Check if the caller can manage activity stacks.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001708 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001709 callingUid);
1710 if (startAnyPerm == PERMISSION_GRANTED) {
1711 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1712 + " allow launch any on display");
1713 return true;
1714 }
1715
1716 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001717 && activityDisplay.mDisplay.getOwnerUid() != SYSTEM_UID) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001718 // Limit launching on virtual displays, because their contents can be read from Surface
1719 // by apps that created them.
1720 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1721 + " disallow launch on virtual display for not-embedded activity");
1722 return false;
1723 }
1724
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001725 if (!activityDisplay.isPrivate()) {
1726 // Anyone can launch on a public display.
1727 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1728 + " allow launch on public display");
1729 return true;
1730 }
1731
1732 // Check if the caller is the owner of the display.
1733 if (activityDisplay.mDisplay.getOwnerUid() == callingUid) {
1734 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1735 + " allow launch for owner of the display");
1736 return true;
1737 }
1738
1739 // Check if caller is present on display
1740 if (activityDisplay.isUidPresent(callingUid)) {
1741 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1742 + " allow launch for caller present on the display");
1743 return true;
1744 }
1745
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001746 Slog.w(TAG, "Launch on display check: denied");
1747 return false;
1748 }
1749
1750 /** Update lists of UIDs that are present on displays and have access to them. */
1751 void updateUIDsPresentOnDisplay() {
1752 mDisplayAccessUIDs.clear();
1753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1754 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001755 // Only bother calculating the whitelist for private displays
1756 if (activityDisplay.isPrivate()) {
1757 mDisplayAccessUIDs.append(
1758 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1759 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001760 }
1761 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1762 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1763 }
1764
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001765 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001766 final long identity = Binder.clearCallingIdentity();
1767 try {
1768 return UserManager.get(mService.mContext).getUserInfo(userId);
1769 } finally {
1770 Binder.restoreCallingIdentity(identity);
1771 }
1772 }
1773
Svet Ganov99b60432015-06-27 13:15:22 -07001774 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001775 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001776 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1777 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001778 == PackageManager.PERMISSION_DENIED) {
1779 return ACTIVITY_RESTRICTION_PERMISSION;
1780 }
1781
Christopher Tateff7add02015-08-17 10:23:22 -07001782 if (activityInfo.permission == null) {
1783 return ACTIVITY_RESTRICTION_NONE;
1784 }
1785
Svet Ganov99b60432015-06-27 13:15:22 -07001786 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1787 if (opCode == AppOpsManager.OP_NONE) {
1788 return ACTIVITY_RESTRICTION_NONE;
1789 }
1790
1791 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1792 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001793 if (!ignoreTargetSecurity) {
1794 return ACTIVITY_RESTRICTION_APPOP;
1795 }
Svet Ganov99b60432015-06-27 13:15:22 -07001796 }
1797
1798 return ACTIVITY_RESTRICTION_NONE;
1799 }
1800
Svetoslav7008b512015-06-24 18:47:07 -07001801 private int getActionRestrictionForCallingPackage(String action,
1802 String callingPackage, int callingPid, int callingUid) {
1803 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001804 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001805 }
1806
1807 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1808 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001809 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001810 }
1811
1812 final PackageInfo packageInfo;
1813 try {
1814 packageInfo = mService.mContext.getPackageManager()
1815 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1816 } catch (PackageManager.NameNotFoundException e) {
1817 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001818 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001819 }
1820
1821 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001822 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001823 }
1824
1825 if (mService.checkPermission(permission, callingPid, callingUid) ==
1826 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001827 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001828 }
1829
1830 final int opCode = AppOpsManager.permissionToOpCode(permission);
1831 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001832 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001833 }
1834
1835 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1836 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001837 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001838 }
1839
Svet Ganov99b60432015-06-27 13:15:22 -07001840 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001841 }
1842
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001843 void setLaunchSource(int uid) {
1844 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1845 }
1846
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001847 void acquireLaunchWakelock() {
1848 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1849 throw new IllegalStateException("Calling must be system uid");
1850 }
1851 mLaunchingActivity.acquire();
1852 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1853 // To be safe, don't allow the wake lock to be held for too long.
1854 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1855 }
1856 }
1857
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001858 /**
1859 * Called when the frontmost task is idle.
1860 * @return the state of mService.mBooting before this was called.
1861 */
1862 private boolean checkFinishBootingLocked() {
1863 final boolean booting = mService.mBooting;
1864 boolean enableScreen = false;
1865 mService.mBooting = false;
1866 if (!mService.mBooted) {
1867 mService.mBooted = true;
1868 enableScreen = true;
1869 }
1870 if (booting || enableScreen) {
1871 mService.postFinishBooting(booting, enableScreen);
1872 }
1873 return booting;
1874 }
1875
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 // Checked.
1877 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001878 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001879 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001880
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001882 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001883 int NS = 0;
1884 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001886 boolean activityRemoved = false;
1887
Wale Ogunwale7d701172015-03-11 15:36:30 -07001888 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001890 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1891 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001892 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1893 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001894 if (fromTimeout) {
1895 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001896 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001897
1898 // This is a hack to semi-deal with a race condition
1899 // in the client where it can be constructed with a
1900 // newer configuration from when we asked it to launch.
1901 // We'll update with whatever configuration it now says
1902 // it used to launch.
1903 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001904 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001905 }
1906
1907 // We are now idle. If someone is waiting for a thumbnail from
1908 // us, we can now deliver.
1909 r.idle = true;
1910
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001911 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001912 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001913 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001914 }
1915 }
1916
1917 if (allResumedActivitiesIdle()) {
1918 if (r != null) {
1919 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001920 }
1921
1922 if (mLaunchingActivity.isHeld()) {
1923 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1924 if (VALIDATE_WAKE_LOCK_CALLER &&
1925 Binder.getCallingUid() != Process.myUid()) {
1926 throw new IllegalStateException("Calling must be system uid");
1927 }
1928 mLaunchingActivity.release();
1929 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001930 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001931 }
1932
1933 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001934 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1935 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001936 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001937 if ((NF = mFinishingActivities.size()) > 0) {
1938 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001939 mFinishingActivities.clear();
1940 }
1941
Craig Mautnerf3333272013-04-22 10:55:53 -07001942 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001943 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001944 mStartingUsers.clear();
1945 }
1946
Craig Mautnerf3333272013-04-22 10:55:53 -07001947 // Stop any activities that are scheduled to do so but have been
1948 // waiting for the next one to start.
1949 for (int i = 0; i < NS; i++) {
1950 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001951 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001952 if (stack != null) {
1953 if (r.finishing) {
1954 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1955 } else {
1956 stack.stopActivityLocked(r);
1957 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001958 }
1959 }
1960
1961 // Finish any activities that are scheduled to do so but have been
1962 // waiting for the next one to start.
1963 for (int i = 0; i < NF; i++) {
1964 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001965 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001966 if (stack != null) {
1967 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1968 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001969 }
1970
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001971 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001972 // Complete user switch
1973 if (startingUsers != null) {
1974 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001975 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001976 }
1977 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001978 }
1979
1980 mService.trimApplications();
1981 //dump();
1982 //mWindowManager.dump();
1983
Craig Mautnerf3333272013-04-22 10:55:53 -07001984 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001985 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001986 }
1987
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001988 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001989 }
1990
Craig Mautner8e569572013-10-11 17:36:59 -07001991 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001992 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001993 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1994 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1996 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1997 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001998 }
Craig Mautner19091252013-10-05 00:03:53 -07001999 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002000 }
2001
2002 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2004 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002005 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2006 stacks.get(stackNdx).closeSystemDialogsLocked();
2007 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002008 }
2009 }
2010
Craig Mautner93529a42013-10-04 15:03:13 -07002011 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002012 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002013 }
2014
Craig Mautner8d341ef2013-03-26 09:03:27 -07002015 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002016 * Update the last used stack id for non-current user (current user's last
2017 * used stack is the focused stack)
2018 */
2019 void updateUserStackLocked(int userId, ActivityStack stack) {
2020 if (userId != mCurrentUser) {
2021 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2022 }
2023 }
2024
2025 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002026 * @return true if some activity was finished (or would have finished if doit were true).
2027 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002028 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2029 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002030 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002031 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2032 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002033 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002034 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002035 if (stack.finishDisabledPackageActivitiesLocked(
2036 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002037 didSomething = true;
2038 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002039 }
2040 }
2041 return didSomething;
2042 }
2043
Dianne Hackborna413dc02013-07-12 12:02:55 -07002044 void updatePreviousProcessLocked(ActivityRecord r) {
2045 // Now that this process has stopped, we may want to consider
2046 // it to be the previous app to try to keep around in case
2047 // the user wants to return to it.
2048
2049 // First, found out what is currently the foreground app, so that
2050 // we don't blow away the previous app if this activity is being
2051 // hosted by the process that is actually still the foreground.
2052 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2056 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002057 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002058 if (stack.mResumedActivity != null) {
2059 fgApp = stack.mResumedActivity.app;
2060 } else if (stack.mPausingActivity != null) {
2061 fgApp = stack.mPausingActivity.app;
2062 }
2063 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002064 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002065 }
2066 }
2067
2068 // Now set this one as the previous process, only if that really
2069 // makes sense to.
2070 if (r.app != null && fgApp != null && r.app != fgApp
2071 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002072 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002073 mService.mPreviousProcess = r.app;
2074 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2075 }
2076 }
2077
Wale Ogunwaled046a012015-12-24 13:05:59 -08002078 boolean resumeFocusedStackTopActivityLocked() {
2079 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002080 }
2081
Wale Ogunwaled046a012015-12-24 13:05:59 -08002082 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002083 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002084
2085 if (!readyToResume()) {
2086 return false;
2087 }
2088
Wale Ogunwaled046a012015-12-24 13:05:59 -08002089 if (targetStack != null && isFocusedStack(targetStack)) {
2090 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002091 }
chaviw59b98852017-06-13 12:05:44 -07002092
Wale Ogunwale06579d62016-04-30 15:29:06 -07002093 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2094 if (r == null || r.state != RESUMED) {
2095 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002096 } else if (r.state == RESUMED) {
2097 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2098 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002099 }
chaviw59b98852017-06-13 12:05:44 -07002100
Wale Ogunwaled046a012015-12-24 13:05:59 -08002101 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002102 }
2103
Todd Kennedy39bfee52016-02-24 10:28:21 -08002104 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2105 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2106 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2107 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2108 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2109 }
2110 }
2111 }
2112
Adrian Roos20d7df32016-01-12 18:59:43 +01002113 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2114 TaskRecord finishedTask = null;
2115 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2117 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002118 final int numStacks = stacks.size();
2119 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2120 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002121 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2122 if (stack == focusedStack || finishedTask == null) {
2123 finishedTask = t;
2124 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002125 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002126 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002127 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002128 }
2129
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002130 void finishVoiceTask(IVoiceInteractionSession session) {
2131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2132 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2133 final int numStacks = stacks.size();
2134 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2135 final ActivityStack stack = stacks.get(stackNdx);
2136 stack.finishVoiceTask(session);
2137 }
2138 }
2139 }
2140
Andrii Kulianc27916642016-04-12 17:59:27 -07002141 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2142 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002143 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2144 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002145 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002146 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2147 // Caller wants the home activity moved with it. To accomplish this,
2148 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002149 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002150 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002151 ActivityStack currentStack = task.getStack();
2152 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002153 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2154 + task + " to front. Stack is null");
2155 return;
2156 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002157
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002158 if (task.isResizeable() && options != null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002159 int stackId = options.getLaunchStackId();
2160 if (canUseActivityOptionsLaunchBounds(options, stackId)) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002161 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
Chong Zhang0fa656b2015-08-31 15:17:21 -07002162 task.updateOverrideConfiguration(bounds);
Wale Ogunwale854809c2015-12-27 16:18:19 -08002163 if (stackId == INVALID_STACK_ID) {
2164 stackId = task.getLaunchStackId();
2165 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002166 if (stackId != currentStack.mStackId) {
Winson Chung74666102017-02-22 17:49:24 -08002167 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2168 DEFER_RESUME, "findTaskToMoveToFrontLocked");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002169 stackId = currentStack.mStackId;
Chong Zhang112eb8c2015-11-02 11:17:00 -08002170 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2171 // still need moveTaskToFrontLocked() below for any transition settings.
2172 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002173 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2174 resizeStackLocked(stackId, bounds,
2175 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002176 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002177 } else {
2178 // WM resizeTask must be done after the task is moved to the correct stack,
2179 // because Task's setBounds() also updates dim layer's bounds, but that has
2180 // dependency on the stack.
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002181 task.resizeWindowContainer();
Wale Ogunwalecacfaa22016-01-15 11:26:08 -08002182 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002183 }
2184 }
2185
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002186 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002187 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002188 r == null ? null : r.appTimeTracker, reason);
2189
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002190 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002191 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002192
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002193 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
2194 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002195 }
2196
Wale Ogunwale854809c2015-12-27 16:18:19 -08002197 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options, int launchStackId) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002198 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002199 // window management or is launching into the pinned stack.
Wale Ogunwale5122df02016-01-29 22:33:38 -08002200 if (options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002201 return false;
2202 }
2203 return (mService.mSupportsPictureInPicture && launchStackId == PINNED_STACK_ID)
2204 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002205 }
2206
Winson Chung55893332017-02-17 17:13:10 -08002207 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002208 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002209 }
2210
Winson Chung55893332017-02-17 17:13:10 -08002211 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2212 boolean createOnTop) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002213 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002214 if (activityContainer != null) {
Winson Chung55893332017-02-17 17:13:10 -08002215 return (T) activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002216 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002217 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002218 return null;
2219 }
Matthew Ng330757d2017-02-28 14:19:17 -08002220 if (stackId == DOCKED_STACK_ID) {
2221 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2222 // the other side of the docked stack and we make visibility decisions based on that.
2223 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2224 }
Winson Chung55893332017-02-17 17:13:10 -08002225 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002226 }
2227
Andrii Kulian16802aa2016-11-02 12:21:33 -07002228 /**
2229 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2230 * If there is no such stack, new dynamic stack can be created.
2231 * @param displayId Target display.
2232 * @param r Activity that should be launched there.
2233 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2234 */
2235 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002236 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002237 if (activityDisplay == null) {
2238 throw new IllegalArgumentException(
2239 "Display with displayId=" + displayId + " not found.");
2240 }
2241
2242 // Return the topmost valid stack on the display.
2243 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2244 final ActivityStack stack = activityDisplay.mStacks.get(i);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002245 if (mService.mActivityStarter.isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002246 return stack;
2247 }
2248 }
2249
2250 // If there is no valid stack on the external display - check if new dynamic stack will do.
2251 if (displayId != Display.DEFAULT_DISPLAY) {
2252 final int newDynamicStackId = getNextStackId();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002253 if (mService.mActivityStarter.isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002254 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2255 }
2256 }
2257
2258 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2259 return null;
2260 }
2261
Craig Mautner967212c2013-04-13 21:10:58 -07002262 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002263 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002264 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2265 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002266 }
2267 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002268 }
2269
Jorim Jaggife762342016-10-13 14:33:27 +02002270 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2271 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2272 }
2273
Andrii Kulian7fc22812016-12-28 13:04:11 -08002274 /**
2275 * Get next focusable stack in the system. This will search across displays and stacks
2276 * in last-focused order for a focusable and visible stack, different from the target stack.
2277 *
2278 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2279 * searching for next candidate.
2280 * @return Next focusable {@link ActivityStack}, null if not found.
2281 */
2282 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2283 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2284
2285 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2286 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002287 // If a display is registered in WM, it must also be available in AM.
2288 @SuppressWarnings("ConstantConditions")
2289 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002290 for (int j = stacks.size() - 1; j >= 0; --j) {
2291 final ActivityStack stack = stacks.get(j);
2292 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002293 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002294 return stack;
2295 }
2296 }
2297 }
2298
2299 return null;
2300 }
2301
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002302 /**
2303 * Get next valid stack for launching provided activity in the system. This will search across
2304 * displays and stacks in last-focused order for a focusable and visible stack, except those
2305 * that are on a currently focused display.
2306 *
2307 * @param r The activity that is being launched.
2308 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2309 * searching for the next candidate.
2310 * @return Next valid {@link ActivityStack}, null if not found.
2311 */
2312 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2313 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2314 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2315 final int displayId = mTmpOrderedDisplayIds.get(i);
2316 if (displayId == currentFocus) {
2317 continue;
2318 }
2319 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2320 if (stack != null) {
2321 return stack;
2322 }
2323 }
2324 return null;
2325 }
2326
Craig Mautneree2e45a2014-06-27 12:10:03 -07002327 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002328 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002329 }
2330
2331 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002332 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2333 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2334 final TaskRecord task = tasks.get(taskNdx);
2335 if (task.isHomeTask()) {
2336 final ArrayList<ActivityRecord> activities = task.mActivities;
2337 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2338 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002339 if (r.isHomeActivity()
2340 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002341 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002342 }
2343 }
2344 }
2345 }
2346 return null;
2347 }
2348
Chong Zhangb15758a2015-11-17 12:12:03 -08002349 /**
2350 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
2351 * the docked stack itself, or if it's side-by-side to the docked stack.
2352 */
2353 boolean isStackDockedInEffect(int stackId) {
2354 return stackId == DOCKED_STACK_ID ||
2355 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
2356 }
2357
Todd Kennedyca4d8422015-01-15 15:19:22 -08002358 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002359 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002360 ActivityContainer activityContainer =
2361 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002362 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002363 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2364 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002365 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002366 return activityContainer;
2367 }
2368
Craig Mautner34b73df2014-01-12 21:11:08 -08002369 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002370 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2371 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2372 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002373 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2374 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002375 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002376 }
2377 }
2378
Andrii Kulian6d6fb402016-10-26 16:15:25 -07002379 void deleteActivityContainerRecord(int stackId) {
2380 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2381 "deleteActivityContainerRecord: callers=" + Debug.getCallers(4));
2382 mActivityContainers.remove(stackId);
Craig Mautner95da1082014-02-24 17:54:35 -08002383 }
2384
Jorim Jaggidc249c42015-12-15 14:57:31 -08002385 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002386 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002387 if (stackId == DOCKED_STACK_ID) {
2388 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002389 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002390 return;
2391 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002392 final ActivityStack stack = getStack(stackId);
2393 if (stack == null) {
2394 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2395 return;
2396 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002397
Winson Chunga2249ac2017-03-30 18:15:30 -07002398 if (!allowResizeInDockedMode && !StackId.tasksAreFloating(stackId) &&
2399 getStack(DOCKED_STACK_ID) != null) {
2400 // If the docked stack exists, don't resize non-floating stacks independently of the
2401 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002402 return;
2403 }
2404
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002405 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002406 mWindowManager.deferSurfaceLayout();
2407 try {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002408 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002409 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002410 stack.ensureVisibleActivitiesConfigurationLocked(
2411 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002412 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002413 } finally {
2414 mWindowManager.continueSurfaceLayout();
2415 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002416 }
2417 }
2418
Jorim Jaggi192086e2016-03-11 17:17:03 +01002419 void deferUpdateBounds(int stackId) {
2420 final ActivityStack stack = getStack(stackId);
2421 if (stack != null) {
2422 stack.deferUpdateBounds();
2423 }
2424 }
2425
2426 void continueUpdateBounds(int stackId) {
2427 final ActivityStack stack = getStack(stackId);
2428 if (stack != null) {
2429 stack.continueUpdateBounds();
2430 }
2431 }
2432
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002433 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002434 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002435 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2436 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002437 final TaskRecord task =
Winson Chung7900bee2017-03-10 08:59:25 -08002438 anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY, INVALID_STACK_ID);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002439 if (task != null) {
2440 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002441 }
2442 }
2443 mResizingTasksDuringAnimation.clear();
2444 }
2445
Robert Carre7cc44d2017-03-20 19:04:30 -07002446 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2447 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002448
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002449 final ActivityStack stack = getStack(fromStackId);
2450 if (stack == null) {
2451 return;
2452 }
2453
2454 mWindowManager.deferSurfaceLayout();
2455 try {
2456 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002457 // We are moving all tasks from the docked stack to the fullscreen stack,
2458 // which is dismissing the docked stack, so resize all other stacks to
2459 // fullscreen here already so we don't end up with resize trashing.
2460 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
2461 if (StackId.isResizeableByDockedStack(i)) {
2462 ActivityStack otherStack = getStack(i);
2463 if (otherStack != null) {
2464 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2465 true /* allowResizeInDockedMode */, DEFER_RESUME);
2466 }
2467 }
2468 }
2469
2470 // Also disable docked stack resizing since we have manually adjusted the
2471 // size of other stacks above and we don't want to trigger a docked stack
2472 // resize when we remove task from it below and it is detached from the
2473 // display because it no longer contains any tasks.
2474 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002475 } else if (fromStackId == PINNED_STACK_ID) {
2476 if (onTop) {
2477 // Log if we are expanding the PiP to fullscreen
2478 MetricsLogger.action(mService.mContext,
2479 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2480 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002481 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002482 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002483 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002484 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002485 // If we are moving from the pinned stack, then the animation takes care of updating
2486 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002487 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002488 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2489 final int size = tasks.size();
2490 if (onTop) {
2491 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002492 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002493 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002494 if (fromStackId == PINNED_STACK_ID) {
2495 // Update the return-to to reflect where the pinned stack task was moved
2496 // from so that we retain the stack that was previously visible if the
2497 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Winson Chungc85d9ce2017-01-03 11:59:52 -08002498 task.setTaskToReturnTo(isFullscreenStackVisible && onTop ?
2499 APPLICATION_ACTIVITY_TYPE : HOME_ACTIVITY_TYPE);
Winson Chung0c1ca092016-11-10 17:40:34 -08002500 }
Winson Chung74666102017-02-22 17:49:24 -08002501 // Defer resume until all the tasks have been moved to the fullscreen stack
2502 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2503 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002504 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002505 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002506 }
2507 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002508 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002509 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002510 // Position the tasks in the fullscreen stack in order at the bottom of the
2511 // stack. Also defer resume until all the tasks have been moved to the
2512 // fullscreen stack.
2513 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002514 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2515 schedulePictureInPictureModeChange,
2516 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002517 }
2518 }
Winson Chung74666102017-02-22 17:49:24 -08002519
2520 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2521 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002522 } finally {
2523 mAllowDockedStackResize = true;
2524 mWindowManager.continueSurfaceLayout();
2525 }
2526 }
2527
Robert Carr6914f082017-03-20 19:04:30 -07002528 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002529 mWindowManager.inSurfaceTransaction(
2530 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002531 }
2532
Jorim Jaggidc249c42015-12-15 14:57:31 -08002533 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002534 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2535 boolean preserveWindows) {
2536 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2537 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2538 false /* deferResume */);
2539 }
2540
2541 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2542 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2543 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002544
2545 if (!mAllowDockedStackResize) {
2546 // Docked stack resize currently disabled.
2547 return;
2548 }
2549
Jorim Jaggidc249c42015-12-15 14:57:31 -08002550 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2551 if (stack == null) {
2552 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2553 return;
2554 }
2555
2556 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2557 mWindowManager.deferSurfaceLayout();
2558 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002559 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2560 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002561 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002562 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002563
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002564 // TODO: Checking for isAttached might not be needed as if the user passes in null
2565 // dockedBounds then they want the docked stack to be dismissed.
2566 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2567 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002568 // In this case we make all other static stacks fullscreen and move all
2569 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002570 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002571
2572 // stack shouldn't contain anymore activities, so nothing to resume.
2573 r = null;
2574 } else {
2575 // Docked stacks occupy a dedicated region on screen so the size of all other
2576 // static stacks need to be adjusted so they don't overlap with the docked stack.
2577 // We get the bounds to use from window manager which has been adjusted for any
2578 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002579 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002580 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002581 final ActivityStack current = getStack(i);
2582 if (current != null && StackId.isResizeableByDockedStack(i)) {
Matthew Ngaa2b6202017-02-10 14:48:21 -08002583 current.getStackDockedModeBounds(
2584 tempOtherTaskBounds /* currentTempTaskBounds */,
2585 tempRect /* outStackBounds */,
2586 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2587
Andrii Kuliane48b7d82017-04-28 14:51:05 -07002588 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
Matthew Ngaa2b6202017-02-10 14:48:21 -08002589 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2590 tempOtherTaskInsetBounds, preserveWindows,
2591 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002592 }
2593 }
2594 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002595 if (!deferResume) {
2596 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2597 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002598 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002599 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002600 mWindowManager.continueSurfaceLayout();
2601 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2602 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002603 }
2604
Robert Carr0d00c2e2016-02-29 17:45:02 -08002605 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002606 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002607 if (stack == null) {
2608 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2609 return;
2610 }
Winson Chung19953ca2017-04-11 11:19:23 -07002611
2612 // It is possible for the bounds animation from the WM to call this but be delayed by
2613 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2614 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2615 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2616 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002617 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002618 return;
2619 }
2620
Robert Carr0d00c2e2016-02-29 17:45:02 -08002621 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2622 mWindowManager.deferSurfaceLayout();
2623 try {
2624 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002625 Rect insetBounds = null;
2626 if (tempPinnedTaskBounds != null) {
2627 // We always use 0,0 as the position for the inset rect because
2628 // if we are getting insets at all in the pinned stack it must mean
2629 // we are headed for fullscreen.
2630 insetBounds = tempRect;
2631 insetBounds.top = 0;
2632 insetBounds.left = 0;
2633 insetBounds.right = tempPinnedTaskBounds.width();
2634 insetBounds.bottom = tempPinnedTaskBounds.height();
2635 }
2636 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002637 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002638 } finally {
2639 mWindowManager.continueSurfaceLayout();
2640 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2641 }
2642 }
2643
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002644 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002645 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002646 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002647 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002648 }
2649
Wale Ogunwale1666e312016-12-16 11:27:18 -08002650 final ActivityContainer activityContainer =
2651 new ActivityContainer(stackId, activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002652 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002653 }
2654
Robert Carr6914f082017-03-20 19:04:30 -07002655
Robert Carre7cc44d2017-03-20 19:04:30 -07002656 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002657 final ActivityStack stack = getStack(stackId);
2658 if (stack == null) {
2659 return;
2660 }
2661
2662 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2663 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002664 /**
2665 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2666 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2667 * that the client receives onStop() before it is reparented. We do this by detaching
2668 * the stack from the display so that it will be considered invisible when
2669 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2670 * invisible as well and added to the stopping list. After which we process the
2671 * stopping list by handling the idle.
2672 */
2673 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2674 pinnedStack.mForceHidden = true;
2675 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2676 pinnedStack.mForceHidden = false;
2677 activityIdleInternalLocked(null, false /* fromTimeout */,
2678 true /* processPausingActivites */, null /* configuration */);
2679
2680 // Move all the tasks to the bottom of the fullscreen stack
2681 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002682 } else {
2683 for (int i = tasks.size() - 1; i >= 0; i--) {
2684 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2685 REMOVE_FROM_RECENTS);
2686 }
2687 }
2688 }
2689
2690 /**
Robert Carr6914f082017-03-20 19:04:30 -07002691 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2692 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2693 * instead moved back onto the fullscreen stack.
2694 */
2695 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002696 mWindowManager.inSurfaceTransaction(
2697 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002698 }
2699
2700 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002701 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2702 */
2703 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2704 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2705 }
2706
2707 /**
Winson Chung010927a2016-12-15 16:12:35 -08002708 * Removes the task with the specified task id.
2709 *
2710 * @param taskId Identifier of the task to be removed.
2711 * @param killProcess Kill any process associated with the task if possible.
2712 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002713 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2714 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002715 * @return Returns true if the given task was found and removed.
2716 */
Winson Chung6954fc92017-03-24 16:22:12 -07002717 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2718 boolean pauseImmediately) {
Winson Chung7900bee2017-03-10 08:59:25 -08002719 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
2720 INVALID_STACK_ID);
Winson Chung010927a2016-12-15 16:12:35 -08002721 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002722 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002723 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2724 if (tr.isPersistable) {
2725 mService.notifyTaskPersisterLocked(null, true);
2726 }
2727 return true;
2728 }
2729 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2730 return false;
2731 }
2732
2733 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2734 if (removeFromRecents) {
2735 mRecentTasks.remove(tr);
2736 tr.removedFromRecents();
2737 }
2738 ComponentName component = tr.getBaseIntent().getComponent();
2739 if (component == null) {
2740 Slog.w(TAG, "No component for base intent of task: " + tr);
2741 return;
2742 }
2743
2744 // Find any running services associated with this app and stop if needed.
2745 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2746
2747 if (!killProcess) {
2748 return;
2749 }
2750
2751 // Determine if the process(es) for this task should be killed.
2752 final String pkg = component.getPackageName();
2753 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2754 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2755 for (int i = 0; i < pmap.size(); i++) {
2756
2757 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2758 for (int j = 0; j < uids.size(); j++) {
2759 ProcessRecord proc = uids.valueAt(j);
2760 if (proc.userId != tr.userId) {
2761 // Don't kill process for a different user.
2762 continue;
2763 }
2764 if (proc == mService.mHomeProcess) {
2765 // Don't kill the home process along with tasks from the same package.
2766 continue;
2767 }
2768 if (!proc.pkgList.containsKey(pkg)) {
2769 // Don't kill process that is not associated with this task.
2770 continue;
2771 }
2772
2773 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002774 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002775 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2776 // Don't kill process(es) that has an activity in a different task that is
2777 // also in recents.
2778 return;
2779 }
2780 }
2781
2782 if (proc.foregroundServices) {
2783 // Don't kill process(es) with foreground service.
2784 return;
2785 }
2786
2787 // Add process to kill list.
2788 procsToKill.add(proc);
2789 }
2790 }
2791
2792 // Kill the running processes.
2793 for (int i = 0; i < procsToKill.size(); i++) {
2794 ProcessRecord pr = procsToKill.get(i);
2795 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2796 && pr.curReceivers.isEmpty()) {
2797 pr.kill("remove task", true);
2798 } else {
2799 // We delay killing processes that are not in the background or running a receiver.
2800 pr.waitingToKill = "remove task";
2801 }
2802 }
2803 }
2804
Craig Mautner4a1cb222013-12-04 16:14:06 -08002805 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002806 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002807 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2808 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002809 break;
2810 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002811 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002812 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002813 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
2815
Chong Zhang5dcb2752015-08-18 13:50:26 -07002816 /**
2817 * Restores a recent task to a stack
2818 * @param task The recent task to be restored.
2819 * @param stackId The stack to restore the task to (default launch stack will be used
Andrii Kulian16802aa2016-11-02 12:21:33 -07002820 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}
2821 * or is not a static stack).
Chong Zhang5dcb2752015-08-18 13:50:26 -07002822 * @return true if the task has been restored successfully.
2823 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002824 boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002825 if (!StackId.isStaticStack(stackId)) {
2826 // If stack is not static (or stack id is invalid) - use the default one.
2827 // This means that tasks that were on external displays will be restored on the
2828 // primary display.
Wale Ogunwale8b06a582015-10-22 14:38:21 -07002829 stackId = task.getLaunchStackId();
Winson Chungd3395382016-12-13 11:49:09 -08002830 } else if (stackId == DOCKED_STACK_ID && !task.supportsSplitScreen()) {
Wale Ogunwale513346d2016-01-27 10:55:01 -08002831 // Preferred stack is the docked stack, but the task can't go in the docked stack.
2832 // Put it in the fullscreen stack.
2833 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Chong Zhang5dcb2752015-08-18 13:50:26 -07002834 }
Wale Ogunwale513346d2016-01-27 10:55:01 -08002835
Andrii Kulian02b7a832016-10-06 23:11:56 -07002836 final ActivityStack currentStack = task.getStack();
2837 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002838 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002839 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002840 // Nothing else to do since it is already restored in the right stack.
2841 return true;
2842 }
2843 // Remove current stack association, so we can re-associate the task with the
2844 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002845 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002846 }
2847
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002848 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07002849 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002850
2851 if (stack == null) {
2852 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002853 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2854 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002855 return false;
2856 }
2857
Wale Ogunwale72919d22016-12-08 18:58:50 -08002858 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002859 // TODO: move call for creation here and other place into Stack.addTask()
2860 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002861 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2862 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002863 final ArrayList<ActivityRecord> activities = task.mActivities;
2864 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002865 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002866 }
2867 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002868 }
2869
Wale Ogunwale040b4702015-08-06 18:10:50 -07002870 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002871 * Move stack with all its existing content to specified display.
2872 * @param stackId Id of stack to move.
2873 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002874 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002875 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002876 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002877 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002878 if (activityDisplay == null) {
2879 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2880 + displayId);
2881 }
2882 final ActivityContainer activityContainer = mActivityContainers.get(stackId);
2883 if (activityContainer != null) {
2884 if (activityContainer.isAttachedLocked()) {
2885 if (activityContainer.getDisplayId() == displayId) {
2886 throw new IllegalArgumentException("Trying to move stackId=" + stackId
2887 + " to its current displayId=" + displayId);
2888 }
2889
Andrii Kulian250d6532017-02-08 23:30:45 -08002890 activityContainer.moveToDisplayLocked(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002891 } else {
2892 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stackId="
2893 + stackId + " is not attached to any display.");
2894 }
2895 } else {
2896 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2897 + stackId);
2898 }
Andrii Kulian839def92016-11-02 10:58:58 -07002899 // TODO(multi-display): resize stacks properly if moved from split-screen.
2900 }
2901
2902 /**
Winson Chung74666102017-02-22 17:49:24 -08002903 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2904 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002905 */
Winson Chung74666102017-02-22 17:49:24 -08002906 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002907 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07002908
Winson Chung74666102017-02-22 17:49:24 -08002909 // Check that we aren't reparenting to the same stack that the task is already in
2910 if (prevStack != null && prevStack.mStackId == stackId) {
2911 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2912 + " already in stackId=" + stackId);
2913 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002914 }
2915
Winson Chung74666102017-02-22 17:49:24 -08002916 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2917 // support
2918 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
2919 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
2920 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08002921 }
2922
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002923 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2924 // multi-display.
2925 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
2926 // TODO: Check ActivityView after fixing b/35349678.
2927 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
2928 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2929 + " reparent task=" + task + " to stackId=" + stackId);
2930 }
2931
Winson Chung74666102017-02-22 17:49:24 -08002932 // Ensure that we aren't trying to move into a freeform stack without freeform
2933 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002934 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002935 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2936 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002937 }
2938
Winson Chung74666102017-02-22 17:49:24 -08002939 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
2940 // used for split-screen mode and will cause things like the docked divider to show up. We
2941 // instead leave the task in its current stack or move it to the fullscreen stack if it
2942 // isn't currently in a stack.
2943 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
2944 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
2945 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
2946 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002947 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08002948
Winson Chung74666102017-02-22 17:49:24 -08002949 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
2950 // example, a docked stack is created which will lead to the stack we are moving from being
2951 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08002952 try {
Winson Chung74666102017-02-22 17:49:24 -08002953 task.mTemporarilyUnresizable = true;
2954 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08002955 } finally {
Winson Chung74666102017-02-22 17:49:24 -08002956 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08002957 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002958 }
2959
Winson Chung08f81892017-03-02 15:40:51 -08002960 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07002961 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
2962 if (stack == null) {
2963 throw new IllegalArgumentException(
2964 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2965 }
2966
2967 final ActivityRecord r = stack.topRunningActivityLocked();
2968 if (r == null) {
2969 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2970 + " in stack=" + stack);
2971 return false;
2972 }
2973
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002974 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002975 Slog.w(TAG,
2976 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002977 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002978 return false;
2979 }
2980
Winson Chung3a682872017-04-10 14:38:05 -07002981 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08002982 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08002983 return true;
2984 }
2985
Winson Chung8bca9e42017-04-16 15:59:43 -07002986 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08002987 boolean moveHomeStackToFront, String reason) {
2988
Wale Ogunwale480dca02016-02-06 13:58:29 -08002989 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08002990
Bryce Lee04ab3462017-04-10 15:06:33 -07002991 // This will clear the pinned stack by moving an existing task to the full screen stack,
2992 // ensuring only one task is present.
2993 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
2994
Wale Ogunwale1666e312016-12-16 11:27:18 -08002995 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08002996 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002997
Wale Ogunwale480dca02016-02-06 13:58:29 -08002998 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07002999 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003000 // Resize the pinned stack to match the current size of the task the activity we are
3001 // going to be moving is currently contained in. We do this to have the right starting
3002 // animation bounds for the pinned stack to the desired bounds the caller wants.
3003 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3004 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003005 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003006
3007 if (task.mActivities.size() == 1) {
3008 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003009 // the stack without re-parenting the activity in a different task. We don't
3010 // move the home stack forward if we are currently entering picture-in-picture
3011 // while pausing because that changes the focused stack and may prevent the new
3012 // starting activity from resuming.
3013 if (moveHomeStackToFront && task.getTaskToReturnTo() == HOME_ACTIVITY_TYPE
Winson Chung105ae5f2017-03-06 15:06:28 -08003014 && (r.state == RESUMED || !r.supportsPictureInPictureWhilePausing)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003015 // Move the home stack forward if the task we just moved to the pinned stack
3016 // was launched from home so home should be visible behind it.
3017 moveHomeStackToFront(reason);
3018 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003019 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003020 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003021 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003022 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003023 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003024 // reveal/leave the other activities in their original task.
3025
3026 // Currently, we don't support reparenting activities across tasks in two different
3027 // stacks, so instead, just create a new task in the same stack, reparent the
3028 // activity into that task, and then reparent the whole task to the new stack. This
3029 // ensures that all the necessary work to migrate states in the old and new stacks
3030 // is also done.
3031 final TaskRecord newTask = task.getStack().createTaskRecord(
3032 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true,
3033 r.mActivityType);
3034 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3035
Winson Chung5af42fc2017-03-24 17:11:33 -07003036 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003037 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003038 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003039 }
Winson Chungc2baac02017-01-11 13:34:47 -08003040
3041 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3042 // to the pinned stack
3043 r.supportsPictureInPictureWhilePausing = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003044 } finally {
3045 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003046 }
3047
Winson Chunge7ba6862017-05-24 12:13:33 -07003048 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3049 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3050 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003051 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3052
Winson Chunge7ba6862017-05-24 12:13:33 -07003053 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3054 true /* fromFullscreen */);
3055
3056 // Update the visibility of all activities after the they have been reparented to the new
3057 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3058 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3059 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003060 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003061 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003062
Winson Chungb5026902017-05-03 12:45:13 -07003063 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3064 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003065 }
3066
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003067 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003068 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3069 if (r == null || !r.isFocusable()) {
3070 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3071 "moveActivityStackToFront: unfocusable r=" + r);
3072 return false;
3073 }
3074
Bryce Leeaf691c02017-03-20 14:20:22 -07003075 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003076 final ActivityStack stack = r.getStack();
3077 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003078 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3079 + r + " task=" + task);
3080 return false;
3081 }
3082
Chong Zhang6cda19c2016-06-14 19:07:56 -07003083 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3084 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3085 "moveActivityStackToFront: already on top, r=" + r);
3086 return false;
3087 }
3088
3089 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3090 "moveActivityStackToFront: r=" + r);
3091
3092 stack.moveToFront(reason, task);
3093 return true;
3094 }
3095
David Stevensc6b91c62017-02-08 14:23:58 -08003096 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003097 mTmpFindTaskResult.r = null;
3098 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003099 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003100 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003101 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3102 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3104 final ActivityStack stack = stacks.get(stackNdx);
Winson Chung91e5c882017-04-21 14:44:38 -07003105 if (!checkActivityBelongsInStack(r, stack)) {
3106 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3107 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003108 continue;
3109 }
3110 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003111 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3112 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003113 continue;
3114 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003115 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003116 // It is possible to have tasks in multiple stacks with the same root affinity, so
3117 // we should keep looking after finding an affinity match to see if there is a
3118 // better match in another stack. Also, task affinity isn't a good enough reason
3119 // to target a display which isn't the source of the intent, so skip any affinity
3120 // matches not on the specified display.
3121 if (mTmpFindTaskResult.r != null) {
3122 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3123 return mTmpFindTaskResult.r;
3124 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003125 // Note: since the traversing through the stacks is top down, the floating
3126 // tasks should always have lower priority than any affinity-matching tasks
3127 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003128 affinityMatch = mTmpFindTaskResult.r;
3129 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003130 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003131 }
3132 }
Winson Chung5b895b72017-05-01 13:46:25 -07003133
David Stevensc6b91c62017-02-08 14:23:58 -08003134 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3135 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003136 }
3137
Winson Chung91e5c882017-04-21 14:44:38 -07003138 /**
3139 * Checks that for the given activity {@param r}, its activity type matches the {@param stack}
3140 * type.
3141 */
3142 private boolean checkActivityBelongsInStack(ActivityRecord r, ActivityStack stack) {
3143 if (r.isHomeActivity()) {
3144 return stack.isHomeStack();
3145 } else if (r.isRecentsActivity()) {
3146 return stack.isRecentsStack();
3147 } else if (r.isAssistantActivity()) {
3148 return stack.isAssistantStack();
3149 }
3150 return true;
3151 }
3152
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003153 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3154 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003155 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3156 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003157 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003158 final ActivityRecord ar = stacks.get(stackNdx)
3159 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003160 if (ar != null) {
3161 return ar;
3162 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003163 }
3164 }
3165 return null;
3166 }
3167
Craig Mautner8d341ef2013-03-26 09:03:27 -07003168 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003169 scheduleSleepTimeout();
3170 if (!mGoingToSleep.isHeld()) {
3171 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003172 if (mLaunchingActivity.isHeld()) {
3173 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3174 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003175 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003176 mLaunchingActivity.release();
3177 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003179 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003180 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003181 }
3182
3183 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003185
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003186 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003187 final long endTime = System.currentTimeMillis() + timeout;
3188 while (true) {
3189 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003190 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3191 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003192 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3193 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3194 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003195 }
3196 if (cantShutdown) {
3197 long timeRemaining = endTime - System.currentTimeMillis();
3198 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003200 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003201 } catch (InterruptedException e) {
3202 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003203 } else {
3204 Slog.w(TAG, "Activity manager shutdown timed out");
3205 timedout = true;
3206 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003207 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003208 } else {
3209 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003210 }
3211 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003212
3213 // Force checkReadyForSleep to complete.
3214 mSleepTimeout = true;
3215 checkReadyForSleepLocked();
3216
Craig Mautner8d341ef2013-03-26 09:03:27 -07003217 return timedout;
3218 }
3219
3220 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003221 removeSleepTimeouts();
3222 if (mGoingToSleep.isHeld()) {
3223 mGoingToSleep.release();
3224 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003225 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3226 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003227 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3228 final ActivityStack stack = stacks.get(stackNdx);
3229 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003230 if (isFocusedStack(stack)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003231 resumeFocusedStackTopActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003232 }
Craig Mautner5314a402013-09-26 12:40:16 -07003233 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003234 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003235 mGoingToSleepActivities.clear();
3236 }
3237
3238 void activitySleptLocked(ActivityRecord r) {
3239 mGoingToSleepActivities.remove(r);
3240 checkReadyForSleepLocked();
3241 }
3242
3243 void checkReadyForSleepLocked() {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003244 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003245 // Do not care.
3246 return;
3247 }
3248
3249 if (!mSleepTimeout) {
3250 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003251 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3252 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003253 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3254 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3255 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003256 }
3257
3258 if (mStoppingActivities.size() > 0) {
3259 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003260 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003261 + mStoppingActivities.size() + " activities");
3262 scheduleIdleLocked();
3263 dontSleep = true;
3264 }
3265
3266 if (mGoingToSleepActivities.size() > 0) {
3267 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003268 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003269 + mGoingToSleepActivities.size() + " activities");
3270 dontSleep = true;
3271 }
3272
3273 if (dontSleep) {
3274 return;
3275 }
3276 }
3277
Wei Wang65c7a152016-06-02 18:51:22 -07003278 // Send launch end powerhint before going sleep
3279 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3280
Craig Mautnere0a38842013-12-16 16:14:02 -08003281 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3282 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3284 stacks.get(stackNdx).goToSleep();
3285 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003286 }
3287
3288 removeSleepTimeouts();
3289
3290 if (mGoingToSleep.isHeld()) {
3291 mGoingToSleep.release();
3292 }
3293 if (mService.mShuttingDown) {
3294 mService.notifyAll();
3295 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003296 }
3297
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003298 boolean reportResumedActivityLocked(ActivityRecord r) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003299 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003300 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003301 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003302 }
3303 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003304 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003305 mWindowManager.executeAppTransition();
3306 return true;
3307 }
3308 return false;
3309 }
3310
Craig Mautner8d341ef2013-03-26 09:03:27 -07003311 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003312 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3313 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003314 int stackNdx = stacks.size() - 1;
3315 while (stackNdx >= 0) {
3316 stacks.get(stackNdx).handleAppCrashLocked(app);
3317 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003318 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 }
3320 }
3321
Craig Mautnerbb742462014-07-07 15:28:55 -07003322 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003323 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003324 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003325 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003326
3327 r.mLaunchTaskBehind = false;
Andrii Kulian21713ac2016-10-12 22:05:05 -07003328 task.setLastThumbnailLocked(r.screenshotActivityLocked());
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003329 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003330 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003331 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003332
3333 // When launching tasks behind, update the last active time of the top task after the new
3334 // task has been shown briefly
3335 final ActivityRecord top = stack.topActivity();
3336 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003337 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003338 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003339 }
3340
3341 void scheduleLaunchTaskBehindComplete(IBinder token) {
3342 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3343 }
3344
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003345 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3346 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003347 mKeyguardController.beginActivityVisibilityUpdate();
3348 try {
3349 // First the front stacks. In case any are not fullscreen and are in front of home.
3350 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3351 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3352 final int topStackNdx = stacks.size() - 1;
3353 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3354 final ActivityStack stack = stacks.get(stackNdx);
3355 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3356 }
Craig Mautner580ea812013-04-25 12:58:38 -07003357 }
Jorim Jaggife762342016-10-13 14:33:27 +02003358 } finally {
3359 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003361 }
3362
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003363 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3364 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3365 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3366 final int topStackNdx = stacks.size() - 1;
3367 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3368 final ActivityStack stack = stacks.get(stackNdx);
3369 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3370 }
3371 }
3372 }
3373
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003374 void invalidateTaskLayers() {
3375 mTaskLayersChanged = true;
3376 }
3377
3378 void rankTaskLayersIfNeeded() {
3379 if (!mTaskLayersChanged) {
3380 return;
3381 }
3382 mTaskLayersChanged = false;
3383 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3384 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3385 int baseLayer = 0;
3386 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3387 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3388 }
3389 }
3390 }
3391
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003392 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3393 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3394 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3395 final int topStackNdx = stacks.size() - 1;
3396 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3397 final ActivityStack stack = stacks.get(stackNdx);
3398 stack.clearOtherAppTimeTrackers(except);
3399 }
3400 }
3401 }
3402
Craig Mautner8d341ef2013-03-26 09:03:27 -07003403 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003404 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3405 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003406 final int numStacks = stacks.size();
3407 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3408 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003409 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003411 }
3412 }
3413
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003414 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3415 // Examine all activities currently running in the process.
3416 TaskRecord firstTask = null;
3417 // Tasks is non-null only if two or more tasks are found.
3418 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003419 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3420 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003421 ActivityRecord r = app.activities.get(i);
3422 // First, if we find an activity that is in the process of being destroyed,
3423 // then we just aren't going to do anything for now; we want things to settle
3424 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003425 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003426 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003427 return;
3428 }
3429 // Don't consider any activies that are currently not in a state where they
3430 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003431 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3432 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003433 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003434 continue;
3435 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003436
3437 final TaskRecord task = r.getTask();
3438 if (task != null) {
3439 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003440 + " from " + r);
3441 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003442 firstTask = task;
3443 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003444 if (tasks == null) {
3445 tasks = new ArraySet<>();
3446 tasks.add(firstTask);
3447 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003448 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003449 }
3450 }
3451 }
3452 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003453 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003454 return;
3455 }
3456 // If we have activities in multiple tasks that are in a position to be destroyed,
3457 // let's iterate through the tasks and release the oldest one.
3458 final int numDisplays = mActivityDisplays.size();
3459 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3460 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3461 // Step through all stacks starting from behind, to hit the oldest things first.
3462 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3463 final ActivityStack stack = stacks.get(stackNdx);
3464 // Try to release activities in this stack; if we manage to, we are done.
3465 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3466 return;
3467 }
3468 }
3469 }
3470 }
3471
Amith Yamasani37a40c22015-06-17 13:25:42 -07003472 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003473 final int focusStackId = mFocusedStack.getStackId();
3474 // We dismiss the docked stack whenever we switch users.
3475 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003476 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3477 // also cause all tasks to be moved to the fullscreen stack at a position that is
3478 // appropriate.
3479 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003480
3481 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003482 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003483 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003484
Craig Mautner858d8a62013-04-23 17:08:34 -07003485 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003486 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3487 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003489 final ActivityStack stack = stacks.get(stackNdx);
3490 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003491 TaskRecord task = stack.topTask();
3492 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003493 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003494 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003495 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003496 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003497
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003498 ActivityStack stack = getStack(restoreStackId);
3499 if (stack == null) {
3500 stack = mHomeStack;
3501 }
3502 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003503 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003504 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003505 } else {
3506 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003507 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003508 }
Craig Mautner93529a42013-10-04 15:03:13 -07003509 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003510 }
3511
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003512 /** Checks whether the userid is a profile of the current user. */
3513 boolean isCurrentProfileLocked(int userId) {
3514 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003515 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003516 }
3517
Bryce Leeb7c9b802017-05-02 14:20:24 -07003518 /**
3519 * Returns whether a stopping activity is present that should be stopped after visible, rather
3520 * than idle.
3521 * @return {@code true} if such activity is present. {@code false} otherwise.
3522 */
3523 boolean isStoppingNoHistoryActivity() {
3524 // Activities that are marked as nohistory should be stopped immediately after the resumed
3525 // activity has become visible.
3526 for (ActivityRecord record : mStoppingActivities) {
3527 if (record.isNoHistory()) {
3528 return true;
3529 }
3530 }
3531
3532 return false;
3533 }
3534
Winson Chung4dabf232017-01-25 13:25:22 -08003535 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3536 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003537 ArrayList<ActivityRecord> stops = null;
3538
3539 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003540 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3541 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003542 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003543 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003544 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3545 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003546 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003547 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003548 if (s.finishing) {
3549 // If this activity is finishing, it is sitting on top of
3550 // everyone else but we now know it is no longer needed...
3551 // so get rid of it. Otherwise, we need to go through the
3552 // normal flow and hide it once we determine that it is
3553 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003554 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003555 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003556 }
3557 }
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003558 if ((!waitingVisible || mService.isSleepingOrShuttingDownLocked()) && remove) {
Winson Chung4dabf232017-01-25 13:25:22 -08003559 if (!processPausingActivities && s.state == PAUSING) {
3560 // Defer processing pausing activities in this iteration and reschedule
3561 // a delayed idle to reprocess it again
3562 removeTimeoutsForActivityLocked(idleActivity);
3563 scheduleIdleTimeoutLocked(idleActivity);
3564 continue;
3565 }
3566
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003567 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003568 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003569 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003570 }
3571 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003572 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003573 }
3574 }
3575
3576 return stops;
3577 }
3578
Craig Mautnercf910b02013-04-23 11:23:27 -07003579 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003580 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3581 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3582 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3583 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003584 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003585 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003586 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003587 if (r == null) Slog.e(TAG,
3588 "validateTop...: null top activity, stack=" + stack);
3589 else {
3590 final ActivityRecord pausing = stack.mPausingActivity;
3591 if (pausing != null && pausing == r) Slog.e(TAG,
3592 "validateTop...: top stack has pausing activity r=" + r
3593 + " state=" + state);
3594 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3595 "validateTop...: activity in front not resumed r=" + r
3596 + " state=" + state);
3597 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003598 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003599 final ActivityRecord resumed = stack.mResumedActivity;
3600 if (resumed != null && resumed == r) Slog.e(TAG,
3601 "validateTop...: back stack has resumed activity r=" + r
3602 + " state=" + state);
3603 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3604 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003605 }
3606 }
3607 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003608 }
3609
Craig Mautnere0570202015-05-13 13:06:11 -07003610 private String lockTaskModeToString() {
3611 switch (mLockTaskModeState) {
3612 case LOCK_TASK_MODE_LOCKED:
3613 return "LOCKED";
3614 case LOCK_TASK_MODE_PINNED:
3615 return "PINNED";
3616 case LOCK_TASK_MODE_NONE:
3617 return "NONE";
3618 default: return "unknown=" + mLockTaskModeState;
3619 }
3620 }
3621
Craig Mautner27084302013-03-25 08:05:25 -07003622 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003623 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003624 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003625 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003626 pw.print(prefix);
3627 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003628 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003629 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003630 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
Jorim Jaggi8d786932016-10-26 19:08:36 -07003631 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3632 if (packages.size() > 0) {
3633 pw.print(prefix); pw.println("mLockTaskPackages (userId:packages)=");
3634 for (int i = 0; i < packages.size(); ++i) {
3635 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3636 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3637 }
3638 }
Bryce Lee4a194382017-04-04 14:32:48 -07003639 if (!mWaitingForActivityVisible.isEmpty()) {
3640 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3641 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3642 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3643 }
3644 }
3645
Jorim Jaggi8d786932016-10-26 19:08:36 -07003646 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
3647 mKeyguardController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003648 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003649
Winson43d1f262016-06-14 16:05:55 -07003650 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003651 * Dump all connected displays' configurations.
3652 * @param prefix Prefix to apply to each line of the dump.
3653 */
3654 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3655 pw.print(prefix); pw.println("Display override configurations:");
3656 final int displayCount = mActivityDisplays.size();
3657 for (int i = 0; i < displayCount; i++) {
3658 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3659 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3660 pw.println(activityDisplay.getOverrideConfiguration());
3661 }
3662 }
3663
3664 /**
Winson43d1f262016-06-14 16:05:55 -07003665 * Dumps the activities matching the given {@param name} in the either the focused stack
3666 * or all visible stacks if {@param dumpVisibleStacks} is true.
3667 */
Winson Chung6998bc42017-02-28 17:07:05 -08003668 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3669 boolean dumpFocusedStackOnly) {
3670 if (dumpFocusedStackOnly) {
3671 return mFocusedStack.getDumpActivitiesLocked(name);
3672 } else {
Winson43d1f262016-06-14 16:05:55 -07003673 ArrayList<ActivityRecord> activities = new ArrayList<>();
3674 int numDisplays = mActivityDisplays.size();
3675 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3676 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3677 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3678 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003679 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003680 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003681 activities.addAll(stack.getDumpActivitiesLocked(name));
3682 }
3683 }
3684 }
3685 return activities;
Winson43d1f262016-06-14 16:05:55 -07003686 }
Craig Mautner20e72272013-04-01 13:45:53 -07003687 }
3688
Dianne Hackborn390517b2013-05-30 15:03:32 -07003689 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3690 boolean needSep, String prefix) {
3691 if (activity != null) {
3692 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3693 if (needSep) {
3694 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003695 }
3696 pw.print(prefix);
3697 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003698 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003699 }
3700 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003701 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003702 }
3703
Craig Mautner8d341ef2013-03-26 09:03:27 -07003704 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3705 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003706 boolean printed = false;
3707 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003708 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3709 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003710 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003711 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003712 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003714 final ActivityStack stack = stacks.get(stackNdx);
3715 StringBuilder stackHeader = new StringBuilder(128);
3716 stackHeader.append(" Stack #");
3717 stackHeader.append(stack.mStackId);
3718 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003719 stackHeader.append("\n");
3720 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3721 stackHeader.append("\n");
3722 stackHeader.append(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003723
3724 final boolean printedStackHeader = stack.dumpActivitiesLocked(fd, pw, dumpAll,
3725 dumpClient, dumpPackage, needSep, stackHeader.toString());
3726 printed |= printedStackHeader;
3727 if (!printedStackHeader) {
3728 // Ensure we always dump the stack header even if there are no activities
3729 pw.println();
3730 pw.println(stackHeader);
3731 }
3732
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3734 !dumpAll, false, dumpPackage, true,
3735 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003736
Craig Mautner4a1cb222013-12-04 16:14:06 -08003737 needSep = printed;
3738 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3739 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003740 if (pr) {
3741 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003743 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3745 " mResumedActivity: ");
3746 if (pr) {
3747 printed = true;
3748 needSep = false;
3749 }
3750 if (dumpAll) {
3751 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3752 " mLastPausedActivity: ");
3753 if (pr) {
3754 printed = true;
3755 needSep = true;
3756 }
3757 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3758 needSep, " mLastNoHistoryActivity: ");
3759 }
3760 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003761 }
3762 }
3763
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003764 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3765 false, dumpPackage, true, " Activities waiting to finish:", null);
3766 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3767 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003768 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3769 false, !dumpAll, false, dumpPackage, true,
3770 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003771 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3772 false, dumpPackage, true, " Activities waiting to sleep:", null);
3773 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3774 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003775
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003776 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003777 }
3778
Dianne Hackborn390517b2013-05-30 15:03:32 -07003779 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003780 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003781 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003782 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003783 String innerPrefix = null;
3784 String[] args = null;
3785 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003786 for (int i=list.size()-1; i>=0; i--) {
3787 final ActivityRecord r = list.get(i);
3788 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3789 continue;
3790 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003791 if (innerPrefix == null) {
3792 innerPrefix = prefix + " ";
3793 args = new String[0];
3794 }
3795 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003796 final boolean full = !brief && (complete || !r.isInHistory());
3797 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003798 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003799 needNL = false;
3800 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003801 if (header1 != null) {
3802 pw.println(header1);
3803 header1 = null;
3804 }
3805 if (header2 != null) {
3806 pw.println(header2);
3807 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003808 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003809 if (lastTask != r.getTask()) {
3810 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003811 pw.print(prefix);
3812 pw.print(full ? "* " : " ");
3813 pw.println(lastTask);
3814 if (full) {
3815 lastTask.dump(pw, prefix + " ");
3816 } else if (complete) {
3817 // Complete + brief == give a summary. Isn't that obvious?!?
3818 if (lastTask.intent != null) {
3819 pw.print(prefix); pw.print(" ");
3820 pw.println(lastTask.intent.toInsecureStringWithClip());
3821 }
3822 }
3823 }
3824 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3825 pw.print(" #"); pw.print(i); pw.print(": ");
3826 pw.println(r);
3827 if (full) {
3828 r.dump(pw, innerPrefix);
3829 } else if (complete) {
3830 // Complete + brief == give a summary. Isn't that obvious?!?
3831 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3832 if (r.app != null) {
3833 pw.print(innerPrefix); pw.println(r.app);
3834 }
3835 }
3836 if (client && r.app != null && r.app.thread != null) {
3837 // flush anything that is already in the PrintWriter since the thread is going
3838 // to write to the file descriptor directly
3839 pw.flush();
3840 try {
3841 TransferPipe tp = new TransferPipe();
3842 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003843 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003844 // Short timeout, since blocking here can
3845 // deadlock with the application.
3846 tp.go(fd, 2000);
3847 } finally {
3848 tp.kill();
3849 }
3850 } catch (IOException e) {
3851 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3852 } catch (RemoteException e) {
3853 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3854 }
3855 needNL = true;
3856 }
3857 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003858 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003859 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003860
Craig Mautnerf3333272013-04-22 10:55:53 -07003861 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003862 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3863 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003864 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3865 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003866 }
3867
3868 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003869 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003870 }
3871
3872 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003873 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3874 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003875 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3876 }
3877
Craig Mautner05d29032013-05-03 13:40:13 -07003878 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003879 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3880 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3881 }
Craig Mautner05d29032013-05-03 13:40:13 -07003882 }
3883
Craig Mautner0eea92c2013-05-16 13:35:39 -07003884 void removeSleepTimeouts() {
3885 mSleepTimeout = false;
3886 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3887 }
3888
3889 final void scheduleSleepTimeout() {
3890 removeSleepTimeouts();
3891 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3892 }
3893
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894 @Override
3895 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003896 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003897 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3898 }
3899
3900 @Override
3901 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003902 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3904 }
3905
3906 @Override
3907 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003908 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3910 }
3911
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003912 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003914 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003916 }
3917
Andrii Kulianca007a62016-11-22 16:33:28 -08003918 /** Check if display with specified id is added to the list. */
3919 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003920 return getActivityDisplayOrCreateLocked(displayId) != null;
3921 }
3922
3923 /**
3924 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3925 * corresponding record in display manager.
3926 */
3927 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
3928 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3929 if (activityDisplay != null) {
3930 return activityDisplay;
3931 }
3932 if (mDisplayManager == null) {
3933 // The system isn't fully initialized yet.
3934 return null;
3935 }
3936 final Display display = mDisplayManager.getDisplay(displayId);
3937 if (display == null) {
3938 // The display is not registered in DisplayManager.
3939 return null;
3940 }
3941 // The display hasn't been added to ActivityManager yet, create a new record now.
3942 activityDisplay = new ActivityDisplay(displayId);
3943 if (activityDisplay.mDisplay == null) {
3944 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3945 return null;
3946 }
3947 mActivityDisplays.put(displayId, activityDisplay);
3948 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3949 mWindowManager.onDisplayAdded(displayId);
3950 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003951 }
3952
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003953 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003954 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003955 mService.mContext.getResources().getDimensionPixelSize(
3956 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003957 }
3958
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003959 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003960 if (displayId == DEFAULT_DISPLAY) {
3961 throw new IllegalArgumentException("Can't remove the primary display.");
3962 }
3963
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003965 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3966 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08003967 final boolean destroyContentOnRemoval
3968 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08003969 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
3970 while (!stacks.isEmpty()) {
3971 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08003972 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003973 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
3974 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003975 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08003976 } else {
3977 // Moving all tasks to fullscreen stack, because it's guaranteed to be
3978 // a valid launch stack for all activities. This way the task history from
3979 // external display will be preserved on primary after move.
3980 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003981 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003982 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003983 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07003984 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 }
3986 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 }
3988
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003989 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003990 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003991 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3992 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003993 // TODO: Update the bounds.
3994 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07003995 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 }
3998
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003999 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004000 final int displayId = stack.mDisplayId;
4001 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004003 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004004 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004005 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004006 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004007 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004008 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004009 info.position = display != null
4010 ? display.mStacks.indexOf(stack)
4011 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012
4013 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4014 final int numTasks = tasks.size();
4015 int[] taskIds = new int[numTasks];
4016 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004017 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004018 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 for (int i = 0; i < numTasks; ++i) {
4020 final TaskRecord task = tasks.get(i);
4021 taskIds[i] = task.taskId;
4022 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4023 : task.realActivity != null ? task.realActivity.flattenToString()
4024 : task.getTopActivity() != null ? task.getTopActivity().packageName
4025 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004026 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004027 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004028 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 }
4030 info.taskIds = taskIds;
4031 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004032 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004033 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004034
4035 final ActivityRecord top = stack.topRunningActivityLocked();
4036 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004037 return info;
4038 }
4039
4040 StackInfo getStackInfoLocked(int stackId) {
4041 ActivityStack stack = getStack(stackId);
4042 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004043 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004044 }
4045 return null;
4046 }
4047
4048 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004049 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004050 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4051 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004052 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004053 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 }
4055 }
4056 return list;
4057 }
4058
Craig Mautner15df08a2015-04-01 12:17:18 -07004059 TaskRecord getLockedTaskLocked() {
4060 final int top = mLockTaskModeTasks.size() - 1;
4061 if (top >= 0) {
4062 return mLockTaskModeTasks.get(top);
4063 }
4064 return null;
4065 }
4066
4067 boolean isLockedTask(TaskRecord task) {
4068 return mLockTaskModeTasks.contains(task);
4069 }
4070
4071 boolean isLastLockedTask(TaskRecord task) {
4072 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4073 }
4074
4075 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004076 if (!mLockTaskModeTasks.remove(task)) {
4077 return;
4078 }
4079 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4080 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004081 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004082 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4083 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004084 final Message lockTaskMsg = Message.obtain();
4085 lockTaskMsg.arg1 = task.userId;
4086 lockTaskMsg.what = LOCK_TASK_END_MSG;
4087 mHandler.sendMessage(lockTaskMsg);
4088 }
4089 }
4090
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004091 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4092 int preferredDisplayId, int actualStackId) {
4093 handleNonResizableTaskIfNeeded(task, preferredStackId, preferredDisplayId, actualStackId,
Andrii Kulianc27916642016-04-12 17:59:27 -07004094 false /* forceNonResizable */);
4095 }
4096
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004097 private void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredStackId,
4098 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4099 final boolean isSecondaryDisplayPreferred =
4100 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY)
4101 || StackId.isDynamicStack(preferredStackId);
4102 if (((!isStackDockedInEffect(actualStackId) && preferredStackId != DOCKED_STACK_ID)
4103 && !isSecondaryDisplayPreferred) || task.isHomeTask()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004104 return;
4105 }
4106
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004107 // Handle incorrect launch/move to secondary display if needed.
4108 final boolean launchOnSecondaryDisplayFailed;
4109 if (isSecondaryDisplayPreferred) {
4110 final int actualDisplayId = task.getStack().mDisplayId;
4111 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4112 // The task landed on an inappropriate display somehow, move it to the default
4113 // display.
4114 // TODO(multi-display): Find proper stack for the task on the default display.
4115 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4116 true /* toTop */);
4117 launchOnSecondaryDisplayFailed = true;
4118 } else {
4119 // The task might have landed on a display different from requested.
4120 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4121 || (preferredDisplayId != INVALID_DISPLAY
4122 && preferredDisplayId != actualDisplayId);
4123 }
4124 } else {
4125 // The task wasn't requested to be on a secondary display.
4126 launchOnSecondaryDisplayFailed = false;
4127 }
4128
Jorim Jaggicd13d332016-04-27 15:40:20 -07004129 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004130 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4131 if (launchOnSecondaryDisplayFailed) {
4132 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4133 // display with config different from global config.
4134 mService.mTaskChangeNotificationController
4135 .notifyActivityLaunchOnSecondaryDisplayFailed();
4136 } else {
4137 // Display a warning toast that we tried to put a non-dockable task in the docked
4138 // stack.
4139 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4140 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004141
Andrii Kulianc27916642016-04-12 17:59:27 -07004142 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4143 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004144 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004145 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004146 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004147 final String packageName = topActivity.appInfo.packageName;
4148 final int reason = isSecondaryDisplayPreferred
4149 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4150 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004151 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004152 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004153 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004154 }
4155
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004156 void showLockTaskToast() {
Hall Liu45784f12016-05-31 15:50:48 -07004157 if (mLockTaskNotify != null) {
4158 mLockTaskNotify.showToast(mLockTaskModeState);
4159 }
Jason Monka8f569c2014-07-07 12:15:21 -04004160 }
4161
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004162 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4163 if (mLockTaskModeTasks.contains(task)) {
4164 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4165 }
4166 }
4167
Craig Mautner432f64e2015-05-20 14:59:57 -07004168 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4169 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004170 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004171 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004172 final TaskRecord lockedTask = getLockedTaskLocked();
4173 if (lockedTask != null) {
4174 removeLockedTaskLocked(lockedTask);
4175 if (!mLockTaskModeTasks.isEmpty()) {
4176 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004177 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4178 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004179 lockedTask.performClearTaskLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08004180 resumeFocusedStackTopActivityLocked();
Craig Mautner15df08a2015-04-01 12:17:18 -07004181 return;
4182 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004183 }
Craig Mautnere0570202015-05-13 13:06:11 -07004184 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4185 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004186 return;
4187 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004188
4189 // Should have already been checked, but do it again.
4190 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004191 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4192 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004193 return;
4194 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004195 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004196 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004197 return;
4198 }
4199
4200 if (mLockTaskModeTasks.isEmpty()) {
4201 // First locktask.
4202 final Message lockTaskMsg = Message.obtain();
4203 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4204 lockTaskMsg.arg1 = task.userId;
4205 lockTaskMsg.what = LOCK_TASK_START_MSG;
4206 lockTaskMsg.arg2 = lockTaskModeState;
4207 mHandler.sendMessage(lockTaskMsg);
4208 }
4209 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004210 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4211 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004212 mLockTaskModeTasks.remove(task);
4213 mLockTaskModeTasks.add(task);
4214
4215 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004216 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004217 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004218
4219 if (andResume) {
Andrii Kulianc27916642016-04-12 17:59:27 -07004220 findTaskToMoveToFrontLocked(task, 0, null, reason,
4221 lockTaskModeState != LOCK_TASK_MODE_NONE);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004222 resumeFocusedStackTopActivityLocked();
Jorim Jaggia42938e2016-11-22 14:31:38 +01004223 mWindowManager.executeAppTransition();
Andrii Kulianc27916642016-04-12 17:59:27 -07004224 } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004225 handleNonResizableTaskIfNeeded(task, INVALID_STACK_ID, DEFAULT_DISPLAY,
4226 task.getStackId(), true /* forceNonResizable */);
Craig Mautner432f64e2015-05-20 14:59:57 -07004227 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004228 }
4229
4230 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004231 return isLockTaskModeViolation(task, false);
4232 }
4233
4234 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4235 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004236 return false;
4237 }
4238 final int lockTaskAuth = task.mLockTaskAuth;
4239 switch (lockTaskAuth) {
4240 case LOCK_TASK_AUTH_DONT_LOCK:
4241 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004242 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004243 case LOCK_TASK_AUTH_LAUNCHABLE:
4244 case LOCK_TASK_AUTH_WHITELISTED:
4245 return false;
4246 case LOCK_TASK_AUTH_PINNABLE:
4247 // Pinnable tasks can't be launched on top of locktask tasks.
4248 return !mLockTaskModeTasks.isEmpty();
4249 default:
4250 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4251 return true;
4252 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004253 }
4254
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 void onLockTaskPackagesUpdatedLocked() {
4256 boolean didSomething = false;
4257 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4258 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004259 final boolean wasWhitelisted =
4260 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4261 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004262 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004263 final boolean isWhitelisted =
4264 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4265 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4266 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004267 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004268 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4269 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004270 removeLockedTaskLocked(lockedTask);
4271 lockedTask.performClearTaskLocked();
4272 didSomething = true;
4273 }
4274 }
4275 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4276 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4277 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4278 final ActivityStack stack = stacks.get(stackNdx);
4279 stack.onLockTaskPackagesUpdatedLocked();
4280 }
4281 }
Craig Mautnere0570202015-05-13 13:06:11 -07004282 final ActivityRecord r = topRunningActivityLocked();
Bryce Leeaf691c02017-03-20 14:20:22 -07004283 final TaskRecord task = r != null ? r.getTask() : null;
Craig Mautnere0570202015-05-13 13:06:11 -07004284 if (mLockTaskModeTasks.isEmpty() && task != null
4285 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4286 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004287 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4288 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4289 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4290 false);
4291 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004292 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004293 if (didSomething) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004294 resumeFocusedStackTopActivityLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004295 }
4296 }
4297
Benjamin Franz43261142015-02-11 15:59:44 +00004298 int getLockTaskModeState() {
4299 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004300 }
4301
Jorim Jaggife89d122015-12-22 16:28:44 +01004302 void activityRelaunchedLocked(IBinder token) {
4303 mWindowManager.notifyAppRelaunchingFinished(token);
Wale Ogunwale3e997362016-09-06 10:37:56 -07004304 if (mService.isSleepingOrShuttingDownLocked()) {
4305 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4306 if (r != null) {
4307 r.setSleeping(true, true);
4308 }
4309 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004310 }
4311
4312 void activityRelaunchingLocked(ActivityRecord r) {
4313 mWindowManager.notifyAppRelaunching(r.appToken);
4314 }
4315
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004316 void logStackState() {
4317 mActivityMetricsLogger.logWindowState();
4318 }
4319
Winson Chung5af42fc2017-03-24 17:11:33 -07004320 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004321 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4322 // end, then ensure that we defer all in between multi-window mode changes
4323 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4324 return;
4325 }
4326
Wale Ogunwale22e25262016-02-01 10:32:02 -08004327 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4328 final ActivityRecord r = task.mActivities.get(i);
4329 if (r.app != null && r.app.thread != null) {
4330 mMultiWindowModeChangedActivities.add(r);
4331 }
4332 }
4333
4334 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4335 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4336 }
4337 }
4338
Winson Chung5af42fc2017-03-24 17:11:33 -07004339 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004340 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004341 if (prevStack == null || prevStack == stack
4342 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4343 return;
4344 }
4345
Winson Chung5af42fc2017-03-24 17:11:33 -07004346 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds, false /* immediate */);
4347 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004348
Winson Chung5af42fc2017-03-24 17:11:33 -07004349 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds,
4350 boolean immediate) {
4351
4352 if (immediate) {
4353 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4354 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4355 final ActivityRecord r = task.mActivities.get(i);
4356 if (r.app != null && r.app.thread != null) {
4357 r.updatePictureInPictureMode(targetStackBounds);
4358 }
4359 }
4360 } else {
4361 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4362 final ActivityRecord r = task.mActivities.get(i);
4363 if (r.app != null && r.app.thread != null) {
4364 mPipModeChangedActivities.add(r);
4365 }
4366 }
4367 mPipModeChangedTargetStackBounds = targetStackBounds;
4368
4369 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4370 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4371 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004372 }
4373 }
4374
Tony Mak853304c2016-04-18 15:17:41 +01004375 void setDockedStackMinimized(boolean minimized) {
4376 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004377 }
4378
chaviw59b98852017-06-13 12:05:44 -07004379 void wakeUp(String reason) {
4380 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4381 }
4382
4383 /**
4384 * Begin deferring resume to avoid duplicate resumes in one pass.
4385 */
4386 private void beginDeferResume() {
4387 mDeferResumeCount++;
4388 }
4389
4390 /**
4391 * End deferring resume and determine if resume can be called.
4392 */
4393 private void endDeferResume() {
4394 mDeferResumeCount--;
4395 }
4396
4397 /**
4398 * @return True if resume can be called.
4399 */
4400 private boolean readyToResume() {
4401 return mDeferResumeCount == 0;
4402 }
4403
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004404 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004405
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004406 public ActivityStackSupervisorHandler(Looper looper) {
4407 super(looper);
4408 }
4409
Winson Chung4dabf232017-01-25 13:25:22 -08004410 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004411 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004412 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4413 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004414 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004415 }
4416
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004417 @Override
4418 public void handleMessage(Message msg) {
4419 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004420 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4421 synchronized (mService) {
4422 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4423 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004424 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004425 }
4426 }
4427 } break;
4428 case REPORT_PIP_MODE_CHANGED_MSG: {
4429 synchronized (mService) {
4430 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4431 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004432 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004433 }
4434 }
4435 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004436 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004437 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4438 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004439 // We don't at this point know if the activity is fullscreen,
4440 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004441 activityIdleInternal((ActivityRecord) msg.obj,
4442 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004443 } break;
4444 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004445 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004446 activityIdleInternal((ActivityRecord) msg.obj,
4447 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004448 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004449 case RESUME_TOP_ACTIVITY_MSG: {
4450 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004451 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004452 }
4453 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004454 case SLEEP_TIMEOUT_MSG: {
4455 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004456 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004457 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4458 mSleepTimeout = true;
4459 checkReadyForSleepLocked();
4460 }
4461 }
4462 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004463 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004464 synchronized (mService) {
4465 if (mLaunchingActivity.isHeld()) {
4466 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4467 if (VALIDATE_WAKE_LOCK_CALLER
4468 && Binder.getCallingUid() != Process.myUid()) {
4469 throw new IllegalStateException("Calling must be system uid");
4470 }
4471 mLaunchingActivity.release();
4472 }
4473 }
4474 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004475 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004476 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 } break;
4478 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004479 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 } break;
4481 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004482 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004483 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004484 case CONTAINER_CALLBACK_VISIBILITY: {
4485 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004486 final IActivityContainerCallback callback = container.mCallback;
4487 if (callback != null) {
4488 try {
4489 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4490 } catch (RemoteException e) {
4491 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004492 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004493 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004494 case LOCK_TASK_START_MSG: {
4495 // When lock task starts, we disable the status bars.
4496 try {
Jason Monk62515be2014-05-21 16:06:19 -04004497 if (mLockTaskNotify == null) {
4498 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004499 }
Jason Monk62515be2014-05-21 16:06:19 -04004500 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004501 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004502 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004503 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004504 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004505 flags = StatusBarManager.DISABLE_MASK
4506 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004507 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004508 flags = StatusBarManager.DISABLE_MASK
4509 & (~StatusBarManager.DISABLE_BACK)
4510 & (~StatusBarManager.DISABLE_HOME)
4511 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004512 }
4513 getStatusBarService().disable(flags, mToken,
4514 mService.mContext.getPackageName());
4515 }
4516 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004517 if (getDevicePolicyManager() != null) {
4518 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004519 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004520 }
justinzhang5286d3f2014-05-12 17:06:01 -04004521 } catch (RemoteException ex) {
4522 throw new RuntimeException(ex);
4523 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004524 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004525 case LOCK_TASK_END_MSG: {
4526 // When lock task ends, we enable the status bars.
4527 try {
Jason Monk62515be2014-05-21 16:06:19 -04004528 if (getStatusBarService() != null) {
4529 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4530 mService.mContext.getPackageName());
4531 }
4532 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004533 if (getDevicePolicyManager() != null) {
4534 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4535 msg.arg1);
4536 }
Jason Monk62515be2014-05-21 16:06:19 -04004537 if (mLockTaskNotify == null) {
4538 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4539 }
4540 mLockTaskNotify.show(false);
4541 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004542 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004543 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004544 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004545 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004546 mWindowManager.lockNow(null);
Jorim Jaggi241ae102016-11-02 21:57:33 -07004547 mWindowManager.dismissKeyguard(null /* callback */);
Jason Monke0697792014-08-04 16:28:09 -04004548 new LockPatternUtils(mService.mContext)
4549 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004550 }
4551 } catch (SettingNotFoundException e) {
4552 // No setting, don't lock.
4553 }
justinzhang5286d3f2014-05-12 17:06:01 -04004554 } catch (RemoteException ex) {
4555 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004556 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004557 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004558 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004559 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004560 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4561 if (mLockTaskNotify == null) {
4562 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4563 }
4564 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4565 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004566 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4567 final ActivityContainer container = (ActivityContainer) msg.obj;
4568 final IActivityContainerCallback callback = container.mCallback;
4569 if (callback != null) {
4570 try {
4571 callback.onAllActivitiesComplete(container.asBinder());
4572 } catch (RemoteException e) {
4573 }
4574 }
4575 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004576 case LAUNCH_TASK_BEHIND_COMPLETE: {
4577 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004578 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004579 if (r != null) {
4580 handleLaunchTaskBehindCompleteLocked(r);
4581 }
4582 }
4583 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004584
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004585 }
4586 }
4587 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004588
Ying Wangb081a592014-04-22 15:20:16 -07004589 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004590 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4591 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004592 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004593 IActivityContainerCallback mCallback = null;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004594 ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004595 ActivityRecord mParentActivity = null;
4596 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004597
Craig Mautnere3a00d72014-04-16 08:31:19 -07004598 boolean mVisible = true;
4599
Craig Mautner4a1cb222013-12-04 16:14:06 -08004600 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004601 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004602
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004603 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4604 final static int CONTAINER_STATE_NO_SURFACE = 1;
4605 final static int CONTAINER_STATE_FINISHING = 2;
4606 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4607
Wale Ogunwale1666e312016-12-16 11:27:18 -08004608 ActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004609 synchronized (mService) {
4610 mStackId = stackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004611 mActivityDisplay = activityDisplay;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004612 mIdString = "ActivtyContainer{" + mStackId + "}";
Bryce Leeaf691c02017-03-20 14:20:22 -07004613
4614 createStack(stackId, onTop);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004615 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004616 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004617 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004618
Bryce Leeaf691c02017-03-20 14:20:22 -07004619 protected void createStack(int stackId, boolean onTop) {
4620 switch (stackId) {
4621 case PINNED_STACK_ID:
4622 new PinnedActivityStack(this, mRecentTasks, onTop);
4623 break;
4624 default:
4625 new ActivityStack(this, mRecentTasks, onTop);
4626 break;
4627 }
4628 }
4629
Andrii Kulian839def92016-11-02 10:58:58 -07004630 /**
4631 * Adds the stack to specified display. Also calls WindowManager to do the same from
Wale Ogunwale1666e312016-12-16 11:27:18 -08004632 * {@link ActivityStack#reparent(ActivityDisplay, boolean)}.
Andrii Kulian839def92016-11-02 10:58:58 -07004633 * @param activityDisplay The display to add the stack to.
Andrii Kulian839def92016-11-02 10:58:58 -07004634 */
Wale Ogunwale1666e312016-12-16 11:27:18 -08004635 void addToDisplayLocked(ActivityDisplay activityDisplay) {
Andrii Kulian839def92016-11-02 10:58:58 -07004636 if (DEBUG_STACK) Slog.d(TAG_STACK, "addToDisplayLocked: " + this
Wale Ogunwale1666e312016-12-16 11:27:18 -08004637 + " to display=" + activityDisplay);
Andrii Kulian839def92016-11-02 10:58:58 -07004638 if (mActivityDisplay != null) {
4639 throw new IllegalStateException("ActivityContainer is already attached, " +
4640 "displayId=" + mActivityDisplay.mDisplayId);
4641 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004642 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004643 mStack.reparent(activityDisplay, true /* onTop */);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004644 }
4645
4646 @Override
Andrii Kulian839def92016-11-02 10:58:58 -07004647 public void addToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004648 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004649 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004650 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004651 return;
4652 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08004653 addToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004654 }
4655 }
4656
4657 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004658 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004659 synchronized (mService) {
4660 if (mActivityDisplay != null) {
4661 return mActivityDisplay.mDisplayId;
4662 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004663 }
4664 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004665 }
4666
Jeff Brownca9bc702014-02-11 14:32:56 -08004667 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004668 public int getStackId() {
4669 synchronized (mService) {
4670 return mStackId;
4671 }
4672 }
4673
4674 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004675 public boolean injectEvent(InputEvent event) {
4676 final long origId = Binder.clearCallingIdentity();
4677 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004678 synchronized (mService) {
4679 if (mActivityDisplay != null) {
4680 return mInputManagerInternal.injectInputEvent(event,
4681 mActivityDisplay.mDisplayId,
4682 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4683 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004684 }
4685 return false;
4686 } finally {
4687 Binder.restoreCallingIdentity(origId);
4688 }
4689 }
4690
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004691 @Override
4692 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004693 synchronized (mService) {
4694 if (mContainerState == CONTAINER_STATE_FINISHING) {
4695 return;
4696 }
4697 mContainerState = CONTAINER_STATE_FINISHING;
4698
Craig Mautnerd163e752014-06-13 17:18:47 -07004699 long origId = Binder.clearCallingIdentity();
4700 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004701 mStack.finishAllActivitiesLocked(false);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004702 mService.mActivityStarter.removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004703 } finally {
4704 Binder.restoreCallingIdentity(origId);
4705 }
4706 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004707 }
4708
Andrii Kulian03c403d2016-11-11 11:14:12 -08004709 /**
4710 * Remove the stack completely. Must be called only when there are no tasks left in it,
4711 * as this method does not finish running activities.
4712 */
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004713 void removeLocked() {
4714 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004715 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004716 if (mActivityDisplay != null) {
Andrii Kulian839def92016-11-02 10:58:58 -07004717 removeFromDisplayLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004718 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004719 mStack.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004720 }
4721
Andrii Kulian839def92016-11-02 10:58:58 -07004722 /**
4723 * Remove the stack from its current {@link ActivityDisplay}, so it can be either destroyed
4724 * completely or re-parented.
4725 */
4726 private void removeFromDisplayLocked() {
4727 if (DEBUG_STACK) Slog.d(TAG_STACK, "removeFromDisplayLocked: " + this
4728 + " current displayId=" + mActivityDisplay.mDisplayId);
4729
Wale Ogunwale1666e312016-12-16 11:27:18 -08004730 mActivityDisplay.detachStack(mStack);
Andrii Kulian839def92016-11-02 10:58:58 -07004731 mActivityDisplay = null;
4732 }
4733
4734 /**
4735 * Move the stack to specified display.
4736 * @param activityDisplay Target display to move the stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08004737 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07004738 */
Andrii Kulian250d6532017-02-08 23:30:45 -08004739 void moveToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Andrii Kulian839def92016-11-02 10:58:58 -07004740 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveToDisplayLocked: " + this + " from display="
4741 + mActivityDisplay + " to display=" + activityDisplay
4742 + " Callers=" + Debug.getCallers(2));
4743
4744 removeFromDisplayLocked();
4745
4746 mActivityDisplay = activityDisplay;
Andrii Kulian250d6532017-02-08 23:30:45 -08004747 mStack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07004748 }
4749
Craig Mautner4a1cb222013-12-04 16:14:06 -08004750 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004751 public final int startActivity(Intent intent) {
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004752 return mService.startActivity(intent, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004753 }
4754
4755 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004756 public final int startActivityIntentSender(IIntentSender intentSender)
4757 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004758 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4759
4760 if (!(intentSender instanceof PendingIntentRecord)) {
4761 throw new IllegalArgumentException("Bad PendingIntent object");
4762 }
4763
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004764 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004765 Binder.getCallingUid(), mCurrentUser, false,
4766 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004767
4768 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4769 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4770 pendingIntent.key.requestResolvedType);
4771
Dianne Hackborn98305522017-05-05 17:53:53 -07004772 return pendingIntent.sendInner(0, null, null, null, null, null, null, null, 0,
Craig Mautnerb9168362015-02-26 20:40:19 -08004773 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4774 }
4775
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08004776 void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004777 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004778 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004779 throw new SecurityException(
4780 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4781 }
4782 }
4783
Craig Mautnerdf88d732014-01-27 09:21:32 -08004784 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004785 public IBinder asBinder() {
4786 return this;
4787 }
4788
Craig Mautner4504de52013-12-20 09:06:56 -08004789 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004790 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004791 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004792 }
4793
Craig Mautner4a1cb222013-12-04 16:14:06 -08004794 ActivityStackSupervisor getOuter() {
4795 return ActivityStackSupervisor.this;
4796 }
4797
Craig Mautnerd163e752014-06-13 17:18:47 -07004798 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004799 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004800 }
4801
Craig Mautner6985bad2014-04-21 15:22:06 -07004802 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004803 void setVisible(boolean visible) {
4804 if (mVisible != visible) {
4805 mVisible = visible;
4806 if (mCallback != null) {
4807 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4808 0 /* unused */, this).sendToTarget();
4809 }
4810 }
4811 }
4812
Craig Mautner6985bad2014-04-21 15:22:06 -07004813 void setDrawn() {
4814 }
4815
Craig Mautner1b4bf852014-05-26 15:06:32 -07004816 // You can always start a new task on a regular ActivityStack.
4817 boolean isEligibleForNewTasks() {
4818 return true;
4819 }
4820
Craig Mautnerd163e752014-06-13 17:18:47 -07004821 void onTaskListEmptyLocked() {
Andrii Kulian6d6fb402016-10-26 16:15:25 -07004822 removeLocked();
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004823 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004824 }
4825
Craig Mautner34b73df2014-01-12 21:11:08 -08004826 @Override
4827 public String toString() {
4828 return mIdString + (mActivityDisplay == null ? "N" : "A");
4829 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004830 }
4831
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004832 private class VirtualActivityContainer extends ActivityContainer {
4833 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004834 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004835
4836 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004837 super(getNextStackId(), parent.getStack().mActivityContainer.mActivityDisplay,
4838 true /* onTop */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004839 mParentActivity = parent;
4840 mCallback = callback;
4841 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004842 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004843 }
4844
4845 @Override
4846 public void setSurface(Surface surface, int width, int height, int density) {
4847 super.setSurface(surface, width, height, density);
4848
4849 synchronized (mService) {
4850 final long origId = Binder.clearCallingIdentity();
4851 try {
4852 setSurfaceLocked(surface, width, height, density);
4853 } finally {
4854 Binder.restoreCallingIdentity(origId);
4855 }
4856 }
4857 }
4858
4859 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4860 if (mContainerState == CONTAINER_STATE_FINISHING) {
4861 return;
4862 }
4863 VirtualActivityDisplay virtualActivityDisplay =
4864 (VirtualActivityDisplay) mActivityDisplay;
4865 if (virtualActivityDisplay == null) {
4866 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004867 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004868 mActivityDisplay = virtualActivityDisplay;
4869 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwale1666e312016-12-16 11:27:18 -08004870 addToDisplayLocked(virtualActivityDisplay);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004871 }
4872
4873 if (mSurface != null) {
4874 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004875 }
4876
Craig Mautner6985bad2014-04-21 15:22:06 -07004877 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004878 if (surface != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004879 resumeFocusedStackTopActivityLocked();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004880 } else {
4881 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004882 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004883 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07004884 mStack.startPausingLocked(false, true, null, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004885 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004886 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004887
Craig Mautnerd163e752014-06-13 17:18:47 -07004888 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004889
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004890 if (DEBUG_STACK) Slog.d(TAG_STACK,
4891 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004892 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004893
Craig Mautner6985bad2014-04-21 15:22:06 -07004894 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004895 boolean isAttachedLocked() {
4896 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004897 }
4898
4899 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004900 void setDrawn() {
4901 synchronized (mService) {
4902 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004903 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004904 }
4905 }
4906
Craig Mautner1b4bf852014-05-26 15:06:32 -07004907 // Never start a new task on an ActivityView if it isn't explicitly specified.
4908 @Override
4909 boolean isEligibleForNewTasks() {
4910 return false;
4911 }
4912
Craig Mautnerd163e752014-06-13 17:18:47 -07004913 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004914 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004915 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4916 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4917 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4918 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4919 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004920 }
4921 }
4922
Craig Mautner4a1cb222013-12-04 16:14:06 -08004923 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4924 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004925 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004926 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004927 int mDisplayId;
4928 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004929
Craig Mautner4a1cb222013-12-04 16:14:06 -08004930 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4931 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004932 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004933
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004934 /** Array of all UIDs that are present on the display. */
4935 private IntArray mDisplayAccessUIDs = new IntArray();
4936
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004937 ActivityDisplay() {
4938 }
Craig Mautner4504de52013-12-20 09:06:56 -08004939
Craig Mautner1a70a162014-09-13 12:09:31 -07004940 // After instantiation, check that mDisplay is not null before using this. The alternative
4941 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004942 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004943 final Display display = mDisplayManager.getDisplay(displayId);
4944 if (display == null) {
4945 return;
4946 }
4947 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004948 }
4949
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004950 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004951 mDisplay = display;
4952 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004953 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004954
Winson Chung7c3ede32017-03-14 14:18:34 -07004955 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004956 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004957 + " to displayId=" + mDisplayId + " position=" + position);
4958 mStacks.add(position, stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004959 }
4960
Wale Ogunwale1666e312016-12-16 11:27:18 -08004961 void detachStack(ActivityStack stack) {
4962 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004963 + " from displayId=" + mDisplayId);
4964 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004965 }
4966
Craig Mautner34b73df2014-01-12 21:11:08 -08004967 @Override
4968 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004969 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4970 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004971
4972 @Override
4973 protected int getChildCount() {
4974 return mStacks.size();
4975 }
4976
4977 @Override
4978 protected ConfigurationContainer getChildAt(int index) {
4979 return mStacks.get(index);
4980 }
4981
4982 @Override
4983 protected ConfigurationContainer getParent() {
4984 return ActivityStackSupervisor.this;
4985 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004986
4987 boolean isPrivate() {
4988 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4989 }
4990
4991 boolean isUidPresent(int uid) {
4992 for (ActivityStack stack : mStacks) {
4993 if (stack.isUidPresent(uid)) {
4994 return true;
4995 }
4996 }
4997 return false;
4998 }
4999
5000 /** Update and get all UIDs that are present on the display and have access to it. */
5001 private IntArray getPresentUIDs() {
5002 mDisplayAccessUIDs.clear();
5003 for (ActivityStack stack : mStacks) {
5004 stack.getPresentUIDs(mDisplayAccessUIDs);
5005 }
5006 return mDisplayAccessUIDs;
5007 }
Andrii Kulian250d6532017-02-08 23:30:45 -08005008
5009 boolean shouldDestroyContentOnRemove() {
5010 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
5011 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005012 }
5013
5014 class VirtualActivityDisplay extends ActivityDisplay {
5015 VirtualDisplay mVirtualDisplay;
5016
Craig Mautner6985bad2014-04-21 15:22:06 -07005017 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005018 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Santos Cordonee8931e2017-04-05 10:31:15 -07005019 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null /* projection */,
5020 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null /* surface */,
Michael Wrightc39d47a2014-07-08 18:07:36 -07005021 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Santos Cordonee8931e2017-04-05 10:31:15 -07005022 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null /* callback */,
5023 null /* handler */, null /* uniqueId */);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005024
5025 init(mVirtualDisplay.getDisplay());
5026
Bryce Leeaf3a4002017-03-20 10:37:12 -07005027 mWindowManager.onDisplayAdded(mDisplayId);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005028 }
5029
5030 void setSurface(Surface surface) {
5031 if (mVirtualDisplay != null) {
5032 mVirtualDisplay.setSurface(surface);
5033 }
5034 }
5035
5036 @Override
Wale Ogunwale1666e312016-12-16 11:27:18 -08005037 void detachStack(ActivityStack stack) {
5038 super.detachStack(stack);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005039 if (mVirtualDisplay != null) {
5040 mVirtualDisplay.release();
5041 mVirtualDisplay = null;
5042 }
5043 }
5044
5045 @Override
5046 public String toString() {
5047 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005048 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005049 }
Jose Lima58e66d62014-05-27 20:00:27 -07005050
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005051 ActivityStack findStackBehind(ActivityStack stack) {
5052 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02005053 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08005054 if (display == null) {
5055 return null;
5056 }
5057 final ArrayList<ActivityStack> stacks = display.mStacks;
5058 for (int i = stacks.size() - 1; i >= 0; i--) {
5059 if (stacks.get(i) == stack && i > 0) {
5060 return stacks.get(i - 1);
5061 }
5062 }
5063 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
5064 + " in=" + stacks);
5065 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005066
Jorim Jaggic69bd222016-03-15 14:38:37 +01005067 /**
5068 * Puts a task into resizing mode during the next app transition.
5069 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005070 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01005071 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005072 private void setResizingDuringAnimation(TaskRecord task) {
5073 mResizingTasksDuringAnimation.add(task.taskId);
5074 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01005075 }
5076
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005077 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
5078 final TaskRecord task;
5079 final int callingUid;
5080 final String callingPackage;
5081 final Intent intent;
5082 final int userId;
5083 final ActivityOptions activityOptions = (bOptions != null)
5084 ? new ActivityOptions(bOptions) : null;
5085 final int launchStackId = (activityOptions != null)
5086 ? activityOptions.getLaunchStackId() : INVALID_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005087 if (StackId.isHomeOrRecentsStack(launchStackId)) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005088 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08005089 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005090 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005091
Matthew Ng606dd802017-06-05 14:06:32 -07005092 mWindowManager.deferSurfaceLayout();
5093 try {
5094 if (launchStackId == DOCKED_STACK_ID) {
5095 mWindowManager.setDockedStackCreateState(
5096 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07005097
Matthew Ng606dd802017-06-05 14:06:32 -07005098 // Defer updating the stack in which recents is until the app transition is done, to
5099 // not run into issues where we still need to draw the task in recents but the
5100 // docked stack is already created.
5101 deferUpdateBounds(RECENTS_STACK_ID);
5102 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005103 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005104
Matthew Ng606dd802017-06-05 14:06:32 -07005105 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
5106 launchStackId);
5107 if (task == null) {
5108 continueUpdateBounds(RECENTS_STACK_ID);
5109 mWindowManager.executeAppTransition();
5110 throw new IllegalArgumentException(
5111 "startActivityFromRecentsInner: Task " + taskId + " not found.");
5112 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005113
Matthew Ng606dd802017-06-05 14:06:32 -07005114 // Since we don't have an actual source record here, we assume that the currently
5115 // focused activity was the source.
5116 final ActivityStack focusedStack = getFocusedStack();
5117 final ActivityRecord sourceRecord =
5118 focusedStack != null ? focusedStack.topActivity() : null;
5119
5120 if (launchStackId != INVALID_STACK_ID) {
5121 if (task.getStackId() != launchStackId) {
5122 task.reparent(launchStackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE,
5123 DEFER_RESUME, "startActivityFromRecents");
5124 }
5125 }
5126
5127 // If the user must confirm credentials (e.g. when first launching a work app and the
5128 // Work Challenge is present) let startActivityInPackage handle the intercepting.
5129 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
5130 && task.getRootActivity() != null) {
5131 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
5132 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07005133 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07005134 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
5135 task.getTopActivity());
5136
5137 // If we are launching the task in the docked stack, put it into resizing mode so
5138 // the window renders full-screen with the background filling the void. Also only
5139 // call this at the end to make sure that tasks exists on the window manager side.
5140 if (launchStackId == DOCKED_STACK_ID) {
5141 setResizingDuringAnimation(task);
5142 }
5143
5144 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
5145 ActivityManager.START_TASK_TO_FRONT,
5146 sourceRecord != null
5147 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
5148 sourceRecord, task.getStack());
5149 return ActivityManager.START_TASK_TO_FRONT;
5150 }
5151 callingUid = task.mCallingUid;
5152 callingPackage = task.mCallingPackage;
5153 intent = task.intent;
5154 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
5155 userId = task.userId;
5156 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Wale Ogunwale692dcd62017-06-20 13:38:14 -07005157 null, null, 0, 0, bOptions, userId, null, task, "startActivityFromRecents");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005158 if (launchStackId == DOCKED_STACK_ID) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005159 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005160 }
Matthew Ng606dd802017-06-05 14:06:32 -07005161 return result;
5162 } finally {
5163 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005164 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01005165 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005166
5167 /**
5168 * @return a list of activities which are the top ones in each visible stack. The first
5169 * entry will be the focused activity.
5170 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005171 List<IBinder> getTopVisibleActivities() {
5172 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
5173 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08005174 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005175 final ActivityDisplay display = mActivityDisplays.valueAt(i);
5176 // Traverse all stacks on a display.
5177 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
5178 final ActivityStack stack = display.mStacks.get(j);
5179 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07005180 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08005181 == ActivityStack.STACK_VISIBLE) {
5182 final ActivityRecord top = stack.topActivity();
5183 if (top != null) {
5184 if (stack == mFocusedStack) {
5185 topActivityTokens.add(0, top.appToken);
5186 } else {
5187 topActivityTokens.add(top.appToken);
5188 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07005189 }
5190 }
5191 }
5192 }
5193 return topActivityTokens;
5194 }
Bryce Lee4a194382017-04-04 14:32:48 -07005195
5196 /**
5197 * Internal container to store a match qualifier alongside a WaitResult.
5198 */
5199 static class WaitInfo {
5200 private final ComponentName mTargetComponent;
5201 private final WaitResult mResult;
5202
5203 public WaitInfo(ComponentName targetComponent, WaitResult result) {
5204 this.mTargetComponent = targetComponent;
5205 this.mResult = result;
5206 }
5207
Wale Ogunwale3270f172017-04-26 07:29:42 -07005208 public boolean matches(ComponentName targetComponent) {
5209 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07005210 }
5211
5212 public WaitResult getResult() {
5213 return mResult;
5214 }
5215
5216 public ComponentName getComponent() {
5217 return mTargetComponent;
5218 }
5219
5220 public void dump(PrintWriter pw, String prefix) {
5221 pw.println(prefix + "WaitInfo:");
5222 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
5223 pw.println(prefix + " mResult=");
5224 mResult.dump(pw, prefix);
5225 }
5226 }
Craig Mautner27084302013-03-25 08:05:25 -07005227}