blob: e034b824dc506e1e1b77f70bf8f127adb57bf997 [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 Kulian3c9ad072017-08-01 11:45:22 -070019import static android.Manifest.permission.ACTIVITY_EMBEDDING;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010020import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070021import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020022import static android.Manifest.permission.START_ANY_ACTIVITY;
23import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Charles Heff9b4dff2017-09-22 10:18:37 +010024import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
Bryce Lee5f0e28f2018-01-30 16:00:03 -080025import static android.app.ActivityManager.START_DELIVERED_TO_TOP;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070028import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
29import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
34import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
39import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070042import static android.app.WindowConfiguration.activityTypeToString;
43import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070044import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020045import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Adrian Roosa6d6aab2018-04-19 18:58:22 +020046import static android.graphics.Rect.copyOrNull;
chaviw59b98852017-06-13 12:05:44 -070047import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010048import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020049import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
50import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070051import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070052import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070053
Winson Chung47900652017-04-06 18:44:25 -070054import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020056import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020058import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
64import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020065import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020067import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
75import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
76import static com.android.server.am.ActivityManagerService.ANIMATE;
77import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020078import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
79import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
80import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
82import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
83import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
85import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
86import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070087import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020088import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010090import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080091import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
92import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
93import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070094import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
95import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
96import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
97import static com.android.server.am.ActivityStackSupervisorProto.IS_HOME_RECENTS_COMPONENT;
98import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
99import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100100import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -0700101
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;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700107import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100108import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700109import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800111import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700113import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700115import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700118import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700119import android.app.WindowConfiguration.ActivityType;
120import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800121import android.app.servertransaction.ActivityLifecycleItem;
122import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700123import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800124import android.app.servertransaction.PauseActivityItem;
125import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700127import android.content.Context;
128import android.content.Intent;
129import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700131import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.content.pm.PackageManager;
133import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100134import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800136import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800137import android.hardware.display.DisplayManager;
138import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800139import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800140import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100142import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700143import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700144import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700145import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700146import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700147import android.os.Message;
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;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700151import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700152import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700153import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100154import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700155import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700157import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700159import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700160import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800161import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700162import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700163import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700166import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700167import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800168import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800169
Andreas Gampea36dc622018-02-05 17:19:22 -0800170import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700171import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800172import com.android.internal.content.ReferrerIntent;
Chenjie Yu52cacc62017-12-08 18:11:45 -0800173import com.android.internal.os.logging.MetricsLoggerWrapper;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700174import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700175import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800176import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700177import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700178import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700179import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700180import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700181
Craig Mautner8d341ef2013-03-26 09:03:27 -0700182import java.io.FileDescriptor;
183import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700184import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800185import java.lang.annotation.Retention;
186import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700187import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700188import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700189import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700190import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700191
Winson Chung1dbc8112017-09-28 18:05:31 -0700192public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
193 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700195 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800203 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800204
Craig Mautnerf3333272013-04-22 10:55:53 -0700205 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700207
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700212 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213
Craig Mautner05d29032013-05-03 13:40:13 -0700214 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
215 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
216 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
220 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
221 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700222 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800223 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
224 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 // Used to indicate if an object (e.g. stack) that we are trying to get
229 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800230 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700232 // Used to indicate that windows of activities should be preserved during the resize.
233 static final boolean PRESERVE_WINDOWS = true;
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. task) should be moved/created
236 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700237 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700239 // Don't execute any calls to resume.
240 static final boolean DEFER_RESUME = true;
241
Winson Chung010927a2016-12-15 16:12:35 -0800242 // Used to indicate that a task is removed it should also be removed from recents.
243 static final boolean REMOVE_FROM_RECENTS = true;
244
Winson Chung6954fc92017-03-24 16:22:12 -0700245 // Used to indicate that pausing an activity should occur immediately without waiting for
246 // the activity callback indicating that it has completed pausing
247 static final boolean PAUSE_IMMEDIATELY = true;
248
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200249 /** True if the docked stack is currently being resized. */
250 private boolean mDockedStackResizing;
251
252 /**
253 * True if there are pending docked bounds that need to be applied after
254 * {@link #mDockedStackResizing} is reset to false.
255 */
256 private boolean mHasPendingDockedBounds;
257 private Rect mPendingDockedBounds;
258 private Rect mPendingTempDockedTaskBounds;
259 private Rect mPendingTempDockedTaskInsetBounds;
260 private Rect mPendingTempOtherTaskBounds;
261 private Rect mPendingTempOtherTaskInsetBounds;
262
Winson Chung7900bee2017-03-10 08:59:25 -0800263 /**
264 * The modes which affect which tasks are returned when calling
265 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
266 */
267 @Retention(RetentionPolicy.SOURCE)
268 @IntDef({
269 MATCH_TASK_IN_STACKS_ONLY,
270 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
271 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
272 })
273 public @interface AnyTaskForIdMatchTaskMode {}
274 // Match only tasks in the current stacks
275 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
276 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
277 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
278 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
279 // provided stack id
280 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
281
Svetoslav7008b512015-06-24 18:47:07 -0700282 // Activity actions an app cannot start if it uses a permission which is not granted.
283 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
284 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700285
Svetoslav7008b512015-06-24 18:47:07 -0700286 static {
287 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
288 Manifest.permission.CAMERA);
289 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
290 Manifest.permission.CAMERA);
291 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
292 Manifest.permission.CALL_PHONE);
293 }
294
Svet Ganov99b60432015-06-27 13:15:22 -0700295 /** Action restriction: launching the activity is not restricted. */
296 private static final int ACTIVITY_RESTRICTION_NONE = 0;
297 /** Action restriction: launching the activity is restricted by a permission. */
298 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
299 /** Action restriction: launching the activity is restricted by an app op. */
300 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700301
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700302 // For debugging to make sure the caller when acquiring/releasing our
303 // wake lock is the system process.
304 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800305 /** The number of distinct task ids that can be assigned to the tasks of a single user */
306 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700307
Craig Mautner27084302013-03-25 08:05:25 -0700308 final ActivityManagerService mService;
309
Winson Chung61c9e5a2017-10-11 10:39:32 -0700310 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700311 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800312
Winson Chung61c9e5a2017-10-11 10:39:32 -0700313 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700314 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000317 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318
319 /** Short cut */
320 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
Bryce Leeec55eb02017-12-05 20:51:27 -0800323 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700324
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800325 /**
326 * Maps the task identifier that activities are currently being started in to the userId of the
327 * task. Each time a new task is created, the entry for the userId of the task is incremented
328 */
329 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Craig Mautner2420ead2013-04-01 17:13:20 -0700331 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800332 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700333
Craig Mautnere0a38842013-12-16 16:14:02 -0800334 /** The stack containing the launcher app. Assumed to always be attached to
335 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800336 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700337
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700340
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
342 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800343 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800344 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700345
346 /** List of activities that are waiting for a new activity to become visible before completing
347 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700348 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
349 // mStoppingActivities when something else comes up.
350 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
Bryce Lee4a194382017-04-04 14:32:48 -0700352 /** List of processes waiting to find out when a specific activity becomes visible. */
353 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
355 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700356 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700357
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358 /** List of activities that are ready to be stopped, but waiting for the next activity to
359 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800360 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361
Craig Mautnerf3333272013-04-22 10:55:53 -0700362 /** List of activities that are ready to be finished, but waiting for the previous activity to
363 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800367 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Wale Ogunwale22e25262016-02-01 10:32:02 -0800369 /** List of activities whose multi-window mode changed that we need to report to the
370 * application */
371 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
372
373 /** List of activities whose picture-in-picture mode changed that we need to report to the
374 * application */
375 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
376
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100377 /**
378 * Animations that for the current transition have requested not to
379 * be considered for the transition animation.
380 */
381 final ArrayList<ActivityRecord> mNoAnimActivities = 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
Bryce Leed3624e12017-11-30 08:51:45 -0800394 /** Set when a power hint has started, but not ended. */
395 private boolean mPowerHintSent;
396
Craig Mautner0eea92c2013-05-16 13:35:39 -0700397 /**
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
David Stevens9440dc82017-03-16 19:00:20 -0700412 /**
413 * A list of tokens that cause the top activity to be put to sleep.
414 * They are used by components that may hide and block interaction with underlying
415 * activities.
416 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700417 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700418
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700419 /** Stack id of the front stack when user switched, indexed by userId. */
420 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700421
Bryce Leeaf3a4002017-03-20 10:37:12 -0700422 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700424 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800426 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
427
428 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800429
Wale Ogunwaled046a012015-12-24 13:05:59 -0800430 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800431 boolean inResumeTopActivity;
432
Andrii Kulian1e32e022016-09-16 15:29:34 -0700433 /**
434 * Temporary rect used during docked stack resize calculation so we don't need to create a new
435 * object each time.
436 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700437 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700438 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700439
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700440 // The default minimal size that will be used if the activity doesn't specify its minimal size.
441 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700442 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700443
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700444 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
445 private boolean mTaskLayersChanged = true;
446
Bryce Lee2a3cc462017-10-27 10:57:35 -0700447 private ActivityMetricsLogger mActivityMetricsLogger;
Jorim Jaggide8305a2018-05-15 12:06:35 -0700448 private LaunchTimeTracker mLaunchTimeTracker = new LaunchTimeTracker();
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800449
Jorim Jaggiea039a82017-08-02 14:37:49 +0200450 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
451
Andrii Kulian1779e612016-10-12 21:58:25 -0700452 @Override
453 protected int getChildCount() {
454 return mActivityDisplays.size();
455 }
456
457 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700458 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700459 return mActivityDisplays.valueAt(index);
460 }
461
462 @Override
463 protected ConfigurationContainer getParent() {
464 return null;
465 }
466
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700467 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700468 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700469 if (activityDisplay == null) {
470 throw new IllegalArgumentException("No display found with id: " + displayId);
471 }
472
473 return activityDisplay.getOverrideConfiguration();
474 }
475
476 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700477 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700478 if (activityDisplay == null) {
479 throw new IllegalArgumentException("No display found with id: " + displayId);
480 }
481
482 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
483 }
484
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700485 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700486 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
487 int callingUid, ActivityInfo activityInfo) {
488 if (displayId == DEFAULT_DISPLAY) {
489 // No restrictions for the default display.
490 return true;
491 }
492 if (!mService.mSupportsMultiDisplay) {
493 // Can't launch on secondary displays if feature is not supported.
494 return false;
495 }
496 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
497 // Can't apply wrong configuration to non-resizeable activities.
498 return false;
499 }
500 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
501 // Can't place activities to a display that has restricted launch rules.
502 // In this case the request should be made by explicitly adding target display id and
503 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
504 return false;
505 }
506 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700507 }
508
509 /**
510 * Check if configuration of specified display matches current global config.
511 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
512 * the same config as on the default display.
513 * @param displayId Id of the display to check.
514 * @return {@code true} if configuration matches.
515 */
516 private boolean displayConfigMatchesGlobal(int displayId) {
517 if (displayId == DEFAULT_DISPLAY) {
518 return true;
519 }
520 if (displayId == INVALID_DISPLAY) {
521 return false;
522 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700523 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700524 if (targetDisplay == null) {
525 throw new IllegalArgumentException("No display found with id: " + displayId);
526 }
527 return getConfiguration().equals(targetDisplay.getConfiguration());
528 }
529
Wale Ogunwale39381972015-12-17 17:15:29 -0800530 static class FindTaskResult {
531 ActivityRecord r;
532 boolean matchedByRootAffinity;
533 }
534 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
535
Craig Mautneree36c772014-07-16 14:56:05 -0700536 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800537 * Temp storage for display ids sorted in focus order.
538 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
539 * it's more efficient, as the number of displays is usually small.
540 */
541 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
542
543 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100544 * Used to keep track whether app visibilities got changed since the last pause. Useful to
545 * determine whether to invoke the task stack change listener after pausing.
546 */
547 boolean mAppVisibilitiesChangedSinceLastPause;
548
549 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100550 * Set of tasks that are in resizing mode during an app transition to fill the "void".
551 */
552 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
553
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700554
555 /**
556 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
557 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
558 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
559 * like the docked stack going empty.
560 */
561 private boolean mAllowDockedStackResize = true;
562
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100563 /**
Tony Mak853304c2016-04-18 15:17:41 +0100564 * Is dock currently minimized.
565 */
566 boolean mIsDockMinimized;
567
Bryce Lee2a3cc462017-10-27 10:57:35 -0700568 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200569
chaviw59b98852017-06-13 12:05:44 -0700570 private PowerManager mPowerManager;
571 private int mDeferResumeCount;
572
Bryce Lee2a3cc462017-10-27 10:57:35 -0700573 private boolean mInitialized;
574
Tony Mak853304c2016-04-18 15:17:41 +0100575 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700576 * Description of a request to start a new activity, which has been held
577 * due to app switches being disabled.
578 */
579 static class PendingActivityLaunch {
580 final ActivityRecord r;
581 final ActivityRecord sourceRecord;
582 final int startFlags;
583 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700584 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700585
586 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700587 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700588 r = _r;
589 sourceRecord = _sourceRecord;
590 startFlags = _startFlags;
591 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700592 callerApp = _callerApp;
593 }
594
595 void sendErrorResult(String message) {
596 try {
597 if (callerApp.thread != null) {
598 callerApp.thread.scheduleCrash(message);
599 }
600 } catch (RemoteException e) {
601 Slog.e(TAG, "Exception scheduling crash of failed "
602 + "activity launcher sourceRecord=" + sourceRecord, e);
603 }
Craig Mautneree36c772014-07-16 14:56:05 -0700604 }
605 }
606
Bryce Leeaf691c02017-03-20 14:20:22 -0700607 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700608 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000609 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700610 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700611 }
612
613 public void initialize() {
614 if (mInitialized) {
615 return;
616 }
617
618 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700619 mRunningTasks = createRunningTasks();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700620 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
621 mHandler.getLooper());
622 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700623
Bryce Leeec55eb02017-12-05 20:51:27 -0800624 mLaunchParamsController = new LaunchParamsController(mService);
625 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700626 }
627
Bryce Lee2a3cc462017-10-27 10:57:35 -0700628
629 public ActivityMetricsLogger getActivityMetricsLogger() {
630 return mActivityMetricsLogger;
631 }
632
Jorim Jaggide8305a2018-05-15 12:06:35 -0700633 LaunchTimeTracker getLaunchTimeTracker() {
634 return mLaunchTimeTracker;
635 }
636
Bryce Lee2a3cc462017-10-27 10:57:35 -0700637 public KeyguardController getKeyguardController() {
638 return mKeyguardController;
639 }
640
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800641 void setRecentTasks(RecentTasks recentTasks) {
642 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700643 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800644 }
645
Winson Chung3f0e59a2017-10-25 10:19:05 -0700646 @VisibleForTesting
647 RunningTasks createRunningTasks() {
648 return new RunningTasks();
649 }
650
Jeff Brown2c43c332014-06-12 22:38:59 -0700651 /**
652 * At the time when the constructor runs, the power manager has not yet been
653 * initialized. So we initialize our wakelocks afterwards.
654 */
655 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700656 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
657 mGoingToSleep = mPowerManager
658 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
659 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700660 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700661 }
662
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700663 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 synchronized (mService) {
665 mWindowManager = wm;
Bryce Lee459c0622018-03-19 11:04:01 -0700666 getKeyguardController().setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667
668 mDisplayManager =
669 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
670 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800671 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672
673 Display[] displays = mDisplayManager.getDisplays();
674 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwale45477b52018-03-06 12:24:19 -0800675 final Display display = displays[displayNdx];
676 ActivityDisplay activityDisplay = new ActivityDisplay(this, display);
677 mActivityDisplays.put(display.getDisplayId(), activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700678 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 }
680
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700681 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
682 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 }
Craig Mautner27084302013-03-25 08:05:25 -0700684 }
685
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700686 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700688 }
689
Bryce Lee7b851cc2018-04-10 14:53:13 -0700690 boolean isFocusable(ConfigurationContainer container, boolean alwaysFocusable) {
691 if (container.inSplitScreenPrimaryWindowingMode() && mIsDockMinimized) {
692 return false;
693 }
694
695 return container.getWindowConfiguration().canReceiveKeys() || alwaysFocusable;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700700 }
701
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700702 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700703 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700704 }
705
Wale Ogunwaled046a012015-12-24 13:05:59 -0800706 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800707 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
708 if (!focusCandidate.isFocusable()) {
709 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800710 focusCandidate = getNextFocusableStackLocked(focusCandidate, false /* ignoreCurrent */);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800711 }
712
713 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800714 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800715 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800716
Wale Ogunwaled046a012015-12-24 13:05:59 -0800717 EventLogTags.writeAmFocusedStack(
718 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
719 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
720 }
721
722 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800723 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800724 if (r != null && r.idle) {
725 checkFinishBootingLocked();
726 }
727 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700730 void moveHomeStackToFront(String reason) {
731 mHomeStack.moveToFront(reason);
732 }
733
Matthew Ng330757d2017-02-28 14:19:17 -0800734 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700735 final ActivityStack recentsStack = getDefaultDisplay().getStack(
736 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800737 if (recentsStack != null) {
738 recentsStack.moveToFront(reason);
739 }
740 }
741
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700742 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800743 boolean moveHomeStackTaskToTop(String reason) {
744 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700745
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700746 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700747 if (top == null) {
748 return false;
749 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700750 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700751 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700752 }
753
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800754 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700755 if (!mService.mBooting && !mService.mBooted) {
756 // Not ready yet!
757 return false;
758 }
759
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800760 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700761 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800762 final String myReason = reason + " resumeHomeStackTask";
763
Mark Lua56ea122015-10-08 13:31:01 +0800764 // Only resume home activity if isn't finishing.
765 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700766 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800767 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700768 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800769 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700770 }
771
Craig Mautner8d341ef2013-03-26 09:03:27 -0700772 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700773 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
774 }
775
776 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700777 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700778 }
779
780 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700781 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700782 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800783 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700784 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700785 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700786 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700787 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700788 @Nullable ActivityOptions aOptions, boolean onTop) {
789 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700790 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
791 throw new IllegalArgumentException("Should not specify activity options for non-restore"
792 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800793 }
794
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700797 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
798 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
799 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700800 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700801 if (task == null) {
802 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700804 if (aOptions != null) {
805 // Resolve the stack the task should be placed in now based on options
806 // and reparent if needed.
807 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
808 if (launchStack != null && stack != launchStack) {
809 final int reparentMode = onTop
810 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
811 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
812 "anyTaskForIdLocked");
813 }
814 }
815 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700816 }
817 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800818
Winson Chung7900bee2017-03-10 08:59:25 -0800819 // If we are matching stack tasks only, return now
820 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800821 return null;
822 }
823
Winson Chung7900bee2017-03-10 08:59:25 -0800824 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
825 // the task from recents
826 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700827 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700828
829 if (task == null) {
830 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800831 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
832 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700833
834 return null;
835 }
836
837 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700838 return task;
839 }
840
Winson Chung7900bee2017-03-10 08:59:25 -0800841 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700842 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700843 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
844 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800845 return null;
846 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700847 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800848 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700849 }
850
Craig Mautner6170f732013-04-02 13:05:23 -0700851 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700854 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
855 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
856 final ActivityStack stack = display.getChildAt(stackNdx);
857 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 if (r != null) {
859 return r;
860 }
Craig Mautner6170f732013-04-02 13:05:23 -0700861 }
862 }
863 return null;
864 }
865
Tony Mak853304c2016-04-18 15:17:41 +0100866 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000867 * Detects whether we should show a lock screen in front of this task for a locked user.
868 * <p>
869 * We'll do this if either of the following holds:
870 * <ul>
871 * <li>The top activity explicitly belongs to {@param userId}.</li>
872 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
873 * </ul>
874 *
875 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100876 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000877 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
878 // To handle the case that work app is in the task but just is not the top one.
879 final ActivityRecord activityRecord = task.getTopActivity();
880 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
881
882 return (activityRecord != null && activityRecord.userId == userId)
883 || (resultTo != null && resultTo.userId == userId);
884 }
885
886 /**
887 * Find all visible task stacks containing {@param userId} and intercept them with an activity
888 * to block out the contents and possibly start a credential-confirming intent.
889 *
890 * @param userId user handle for the locked managed profile.
891 */
892 void lockAllProfileTasks(@UserIdInt int userId) {
893 mWindowManager.deferSurfaceLayout();
894 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
896 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
897 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
898 final ActivityStack stack = display.getChildAt(stackNdx);
899 final List<TaskRecord> tasks = stack.getAllTasks();
900 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
901 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000902
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700903 // Check the task for a top activity belonging to userId, or returning a
904 // result to an activity belonging to userId. Example case: a document
905 // picker for personal files, opened by a work app, should still get locked.
906 if (taskTopActivityIsUser(task, userId)) {
907 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
908 task.taskId, userId);
909 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000910 }
Tony Mak853304c2016-04-18 15:17:41 +0100911 }
912 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000913 } finally {
914 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100915 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000916 }
917
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800918 void setNextTaskIdForUserLocked(int taskId, int userId) {
919 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
920 if (taskId > currentTaskId) {
921 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700922 }
923 }
924
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700925 static int nextTaskIdForUser(int taskId, int userId) {
926 int nextTaskId = taskId + 1;
927 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
928 // Wrap around as there will be smaller task ids that are available now.
929 nextTaskId -= MAX_TASK_IDS_PER_USER;
930 }
931 return nextTaskId;
932 }
933
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800934 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800935 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
936 // for a userId u, a taskId can only be in the range
937 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
938 // 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 -0700939 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700940 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700941 || anyTaskForIdLocked(
942 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700943 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800944 if (candidateTaskId == currentTaskId) {
945 // Something wrong!
946 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
947 throw new IllegalStateException("Cannot get an available task id."
948 + " Reached limit of " + MAX_TASK_IDS_PER_USER
949 + " running tasks per user.");
950 }
951 }
952 mCurTaskIdForUser.put(userId, candidateTaskId);
953 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700954 }
955
Chong Zhang6cda19c2016-06-14 19:07:56 -0700956 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800957 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700958 if (stack == null) {
959 return null;
960 }
Bryce Leec4ab62a2018-03-05 14:19:26 -0800961 ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700962 if (resumedActivity == null || resumedActivity.app == null) {
963 resumedActivity = stack.mPausingActivity;
964 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700965 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700966 }
967 }
968 return resumedActivity;
969 }
970
Dianne Hackbornff072722014-09-24 10:56:28 -0700971 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700972 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700975 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
976 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
977 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700978 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 continue;
980 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200981 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
982 final ActivityRecord top = stack.topRunningActivityLocked();
983 final int size = mTmpActivityList.size();
984 for (int i = 0; i < size; i++) {
985 final ActivityRecord activity = mTmpActivityList.get(i);
986 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
987 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800988 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200989 if (realStartActivityLocked(activity, app,
990 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 didSomething = true;
992 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700993 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200995 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700997 }
Craig Mautner20e72272013-04-01 13:45:53 -0700998 }
Craig Mautner20e72272013-04-01 13:45:53 -0700999 }
1000 }
1001 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001002 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001003 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001004 }
Craig Mautner20e72272013-04-01 13:45:53 -07001005 return didSomething;
1006 }
1007
1008 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001010 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1011 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001013 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 continue;
1015 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08001016 final ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001018 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001019 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 return false;
1021 }
Craig Mautner20e72272013-04-01 13:45:53 -07001022 }
1023 }
Wei Wang65c7a152016-06-02 18:51:22 -07001024 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -08001025 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001026 return true;
1027 }
1028
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001031 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1032 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1033 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001034 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08001035 final ActivityRecord r = stack.getResumedActivity();
Bryce Lee7ace3952018-02-16 14:34:32 -08001036 if (r != null && !r.isState(RESUMED)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 return false;
1038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 }
1040 }
1041 }
1042 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001043 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1045 mLastFocusedStack + " to=" + mFocusedStack);
1046 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 return true;
1048 }
1049
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001050 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001051 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001053 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1054 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1055 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001056 final ActivityRecord r = stack.getResumedActivity();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001057 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001058 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001059 return false;
1060 }
1061 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001062 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001063 }
1064 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001065 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001066 }
1067
Craig Mautner2acc3892013-09-23 10:28:14 -07001068 /**
1069 * Pause all activities in either all of the stacks or just the back stacks.
1070 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001071 * @param resuming The resuming activity.
1072 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1073 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001074 * @return true if any activity was paused as a result of this call.
1075 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001076 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001077 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001078 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001079 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1080 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1081 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001082 if (!isFocusedStack(stack) && stack.getResumedActivity() != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001083 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Bryce Leec4ab62a2018-03-05 14:19:26 -08001084 " mResumedActivity=" + stack.getResumedActivity());
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001085 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1086 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001088 }
1089 }
1090 return someActivityPaused;
1091 }
1092
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001094 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001096 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1097 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1098 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 final ActivityRecord r = stack.mPausingActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08001100 if (r != null && !r.isState(PAUSED, STOPPED, STOPPING)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001102 Slog.d(TAG_STATES,
Bryce Lee7ace3952018-02-16 14:34:32 -08001103 "allPausedActivitiesComplete: r=" + r + " state=" + r.getState());
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 pausing = false;
1105 } else {
1106 return false;
1107 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001108 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 }
1110 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001111 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001112 }
1113
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001114 void cancelInitializingActivities() {
1115 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001116 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1117 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1118 final ActivityStack stack = display.getChildAt(stackNdx);
1119 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001120 }
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
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001166 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1167 if (mWaitingActivityLaunched.isEmpty()) {
1168 return;
1169 }
1170
1171 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1172 return;
1173 }
1174
Chong Zhang5022da32016-06-21 16:31:37 -07001175 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001176
Chong Zhang5022da32016-06-21 16:31:37 -07001177 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1178 WaitResult w = mWaitingActivityLaunched.remove(i);
1179 if (w.who == null) {
1180 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001181 w.result = result;
1182
1183 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1184 // will be no followup launch signals. Assign the result and launched component.
1185 if (result == START_DELIVERED_TO_TOP) {
1186 w.who = r.realActivity;
1187 }
Chong Zhang5022da32016-06-21 16:31:37 -07001188 }
1189 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001190
Chong Zhang5022da32016-06-21 16:31:37 -07001191 if (changed) {
1192 mService.notifyAll();
1193 }
1194 }
1195
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001196 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1197 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001198 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001200 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001201 if (w.who == null) {
1202 changed = true;
1203 w.timeout = timeout;
1204 if (r != null) {
1205 w.who = new ComponentName(r.info.packageName, r.info.name);
1206 }
1207 w.thisTime = thisTime;
1208 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001209 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001210 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001211 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001212 if (changed) {
1213 mService.notifyAll();
1214 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001215 }
1216
Craig Mautner29219d92013-04-16 20:19:12 -07001217 ActivityRecord topRunningActivityLocked() {
Bryce Leec961e0a2018-04-13 17:58:02 -07001218 return topRunningActivityLocked(false /* considerKeyguardState */);
1219 }
1220
1221 /**
1222 * Returns the top running activity in the focused stack. In the case the focused stack has no
1223 * such activity, the next focusable stack on top of a display is returned.
1224 * @param considerKeyguardState Indicates whether the locked state should be considered. if
1225 * {@code true} and the keyguard is locked, only activities that
1226 * can be shown on top of the keyguard will be considered.
1227 * @return The top running activity. {@code null} if none is available.
1228 */
1229 ActivityRecord topRunningActivityLocked(boolean considerKeyguardState) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001230 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001231 ActivityRecord r = focusedStack.topRunningActivityLocked();
Bryce Lee86918962018-04-26 15:28:12 -07001232 if (r != null && isValidTopRunningActivity(r, considerKeyguardState)) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001233 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001234 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001235
Andrii Kulian7318d632016-07-20 18:59:28 -07001236 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001237 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1238
1239 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1240 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001241 final ActivityDisplay display = mActivityDisplays.get(displayId);
Bryce Lee4bc5ba32018-03-20 13:58:53 -07001242
1243 // If WindowManagerService has encountered the display before we have, ignore as there
1244 // will be no stacks present and therefore no activities.
1245 if (display == null) {
1246 continue;
1247 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001248
1249 // TODO: We probably want to consider the top fullscreen stack as we could have a pinned
1250 // stack on top.
1251 final ActivityStack topStack = display.getTopStack();
1252
1253 // Only consider focusable top stacks other than the current focused one.
1254 if (topStack == null || !topStack.isFocusable() || topStack == focusedStack) {
1255 continue;
1256 }
1257
1258 final ActivityRecord topActivity = topStack.topRunningActivityLocked();
1259
1260 // Skip if no top activity.
1261 if (topActivity == null) {
1262 continue;
1263 }
1264
Bryce Leec961e0a2018-04-13 17:58:02 -07001265
1266 // This activity can be considered the top running activity if we are not
1267 // considering the locked state, the keyguard isn't locked, or we can show when
1268 // locked.
Bryce Lee86918962018-04-26 15:28:12 -07001269 if (isValidTopRunningActivity(topActivity, considerKeyguardState)) {
Bryce Leec961e0a2018-04-13 17:58:02 -07001270 return topActivity;
Craig Mautner29219d92013-04-16 20:19:12 -07001271 }
1272 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001273
Craig Mautner29219d92013-04-16 20:19:12 -07001274 return null;
1275 }
1276
Bryce Lee86918962018-04-26 15:28:12 -07001277 /**
1278 * Verifies an {@link ActivityRecord} can be the top activity based on keyguard state and
1279 * whether we are considering it.
1280 */
1281 private boolean isValidTopRunningActivity(ActivityRecord record,
1282 boolean considerKeyguardState) {
1283 if (!considerKeyguardState) {
1284 return true;
1285 }
1286
1287 final boolean keyguardLocked = getKeyguardController().isKeyguardLocked();
1288
1289 if (!keyguardLocked) {
1290 return true;
1291 }
1292
1293 return record.canShowWhenLocked();
1294 }
1295
Winson Chung61c9e5a2017-10-11 10:39:32 -07001296 @VisibleForTesting
1297 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1298 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1299 int callingUid, boolean allowed) {
1300 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1301 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001302 }
1303
Todd Kennedy7440f172015-12-09 14:31:22 -08001304 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1305 ProfilerInfo profilerInfo) {
1306 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001307 if (aInfo != null) {
1308 // Store the found target back into the intent, because now that
1309 // we have it we never want to do this again. For example, if the
1310 // user navigates back to this point in the history, we should
1311 // always restart the exact same activity.
1312 intent.setComponent(new ComponentName(
1313 aInfo.applicationInfo.packageName, aInfo.name));
1314
1315 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001316 if (!aInfo.processName.equals("system")) {
1317 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001318 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001319 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001320
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001321 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1322 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1323 }
1324
Man Caocfa78b22015-06-11 20:14:34 -07001325 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1326 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1327 }
1328
1329 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001330 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001331 }
1332 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001333 final String intentLaunchToken = intent.getLaunchToken();
1334 if (aInfo.launchToken == null && intentLaunchToken != null) {
1335 aInfo.launchToken = intentLaunchToken;
1336 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001337 }
1338 return aInfo;
1339 }
1340
Patrick Baumann78380272018-04-04 10:41:01 -07001341 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
1342 int filterCallingUid) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001343 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001344 try {
1345 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001346 int modifiedFlags = flags
1347 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001348 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001349 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1350 modifiedFlags |= PackageManager.MATCH_INSTANT;
1351 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001352
Makoto Onuki1a342742018-04-26 14:56:59 -07001353 // In order to allow cross-profile lookup, we clear the calling identity here.
1354 // Note the binder identity won't affect the result, but filterCallingUid will.
1355
1356 // Cross-user/profile call check are done at the entry points
1357 // (e.g. AMS.startActivityAsUser).
1358 final long token = Binder.clearCallingIdentity();
1359 try {
1360 return mService.getPackageManagerInternalLocked().resolveIntent(
1361 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
1362 } finally {
1363 Binder.restoreCallingIdentity(token);
1364 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001365 } finally {
1366 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1367 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001368 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001369 }
1370
1371 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -07001372 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
1373 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -08001374 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1375 }
1376
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001377 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1378 boolean andResume, boolean checkConfig) throws RemoteException {
1379
1380 if (!allPausedActivitiesComplete()) {
1381 // While there are activities pausing we skipping starting any new activities until
1382 // pauses are complete. NOTE: that we also do this for activities that are starting in
1383 // the paused state because they will first be resumed then paused on the client side.
1384 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1385 "realStartActivityLocked: Skipping start of r=" + r
1386 + " some activities pausing...");
1387 return false;
1388 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001389
Bryce Leeaf691c02017-03-20 14:20:22 -07001390 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001391 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001392
1393 beginDeferResume();
1394
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 try {
chaviw59b98852017-06-13 12:05:44 -07001396 r.startFreezingScreenLocked(app, 0);
1397
1398 // schedule launch ticks to collect information about slow apps.
1399 r.startLaunchTickingLocked();
1400
Dianne Hackborn68a06332017-11-15 17:54:18 -08001401 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001402
Bryce Lee459c0622018-03-19 11:04:01 -07001403 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001404 r.notifyUnknownVisibilityLaunched();
1405 }
1406
chaviw59b98852017-06-13 12:05:44 -07001407 // Have the window manager re-evaluate the orientation of the screen based on the new
1408 // activity order. Note that as a result of this, it can call back into the activity
1409 // manager with a new orientation. We don't care about that, because the activity is
1410 // not currently running so we are just restarting it anyway.
1411 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -07001412 // Deferring resume here because we're going to launch new activity shortly.
1413 // We don't want to perform a redundant launch of the same record while ensuring
1414 // configurations and trying to resume top activity of focused stack.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001415 ensureVisibilityAndConfig(r, r.getDisplayId(),
1416 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001417 }
chaviw59b98852017-06-13 12:05:44 -07001418
1419 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1420 true /* isTop */)) {
1421 // We only set the visibility to true if the activity is allowed to be visible
1422 // based on
1423 // keyguard state. This avoids setting this into motion in window manager that is
1424 // later cancelled due to later calls to ensure visible activities that set
1425 // visibility back to false.
1426 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001427 }
chaviw59b98852017-06-13 12:05:44 -07001428
chaviw59b98852017-06-13 12:05:44 -07001429 final int applicationInfoUid =
1430 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1431 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1432 Slog.wtf(TAG,
1433 "User ID for activity changing for " + r
1434 + " appInfo.uid=" + r.appInfo.uid
1435 + " info.ai.uid=" + applicationInfoUid
1436 + " old=" + r.app + " new=" + app);
1437 }
1438
1439 app.waitingToKill = null;
1440 r.launchCount++;
1441 r.lastLaunchTime = SystemClock.uptimeMillis();
1442
1443 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1444
1445 int idx = app.activities.indexOf(r);
1446 if (idx < 0) {
1447 app.activities.add(r);
1448 }
1449 mService.updateLruProcessLocked(app, true, null);
1450 mService.updateOomAdjLocked();
1451
Bryce Lee2b8e0372018-04-05 17:01:37 -07001452 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +01001453 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1454 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1455 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -07001456 && lockTaskController.getLockTaskModeState()
1457 == LOCK_TASK_MODE_LOCKED)) {
1458 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001459 }
1460
1461 try {
1462 if (app.thread == null) {
1463 throw new RemoteException();
1464 }
1465 List<ResultInfo> results = null;
1466 List<ReferrerIntent> newIntents = null;
1467 if (andResume) {
1468 // We don't need to deliver new intents and/or set results if activity is going
1469 // to pause immediately after launch.
1470 results = r.results;
1471 newIntents = r.newIntents;
1472 }
1473 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1474 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1475 + " newIntents=" + newIntents + " andResume=" + andResume);
1476 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1477 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001478 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001479 // Home process is the root process of the task.
1480 mService.mHomeProcess = task.mActivities.get(0).app;
1481 }
1482 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1483 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1484 r.sleeping = false;
1485 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001486 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001487 mService.showAskCompatModeDialogLocked(r);
1488 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1489 ProfilerInfo profilerInfo = null;
1490 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1491 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1492 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001493 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1494 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1495 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001496 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001497 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001498 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001499 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001500 }
1501 }
chaviw59b98852017-06-13 12:05:44 -07001502
Andreas Gampe2b073a02017-06-22 17:28:57 -07001503 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001504 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001505 }
1506 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001507
chaviw59b98852017-06-13 12:05:44 -07001508 app.hasShownUi = true;
1509 app.pendingUiClean = true;
1510 app.forceProcessStateUpTo(mService.mTopProcessState);
1511 // Because we could be starting an Activity in the system process this may not go
1512 // across a Binder interface which would create a new Configuration. Consequently
1513 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001514
chaviw59b98852017-06-13 12:05:44 -07001515 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1516 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1517 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001518
chaviw59b98852017-06-13 12:05:44 -07001519 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001520
1521
1522 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001523 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001524 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001525 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001526 System.identityHashCode(r), r.info,
1527 // TODO: Have this take the merged configuration instead of separate global
1528 // and override configs.
1529 mergedConfiguration.getGlobalConfiguration(),
1530 mergedConfiguration.getOverrideConfiguration(), r.compat,
1531 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001532 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1533 profilerInfo));
1534
1535 // Set desired final state.
1536 final ActivityLifecycleItem lifecycleItem;
1537 if (andResume) {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001538 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001539 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001540 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001541 }
1542 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1543
1544 // Schedule transaction.
Bryce Leeb0f993f2018-03-02 15:38:01 -08001545 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001546
Craig Mautner2420ead2013-04-01 17:13:20 -07001547
Dianne Hackbornc8e4fad2018-05-04 11:31:09 -07001548 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
1549 && mService.mHasHeavyWeightFeature) {
chaviw59b98852017-06-13 12:05:44 -07001550 // This may be a heavy-weight process! Note that the package
1551 // manager will ensure that only activity can run in the main
1552 // process of the .apk, which is the only thing that will be
1553 // considered heavy-weight.
1554 if (app.processName.equals(app.info.packageName)) {
1555 if (mService.mHeavyWeightProcess != null
1556 && mService.mHeavyWeightProcess != app) {
1557 Slog.w(TAG, "Starting new heavy weight process " + app
1558 + " when already running "
1559 + mService.mHeavyWeightProcess);
1560 }
1561 mService.mHeavyWeightProcess = app;
1562 Message msg = mService.mHandler.obtainMessage(
1563 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1564 msg.obj = r;
1565 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001566 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001567 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001568
chaviw59b98852017-06-13 12:05:44 -07001569 } catch (RemoteException e) {
1570 if (r.launchFailed) {
1571 // This is the second time we failed -- finish activity
1572 // and give up.
1573 Slog.e(TAG, "Second failure launching "
1574 + r.intent.getComponent().flattenToShortString()
1575 + ", giving up", e);
1576 mService.appDiedLocked(app);
1577 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1578 "2nd-crash", false);
1579 return false;
1580 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001581
chaviw59b98852017-06-13 12:05:44 -07001582 // This is the first time we failed -- restart process and
1583 // retry.
1584 r.launchFailed = true;
1585 app.activities.remove(r);
1586 throw e;
1587 }
1588 } finally {
1589 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001590 }
1591
1592 r.launchFailed = false;
1593 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001594 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001595 }
1596
Andrii Kulianb372da62018-01-18 10:46:24 -08001597 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1598 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001599 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001600 // As part of the process of launching, ActivityThread also performs
1601 // a resume.
1602 stack.minimalResumeActivityLocked(r);
1603 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001604 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001605 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001606 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001607 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001608 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001609 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -07001610 }
1611
1612 // Launch the new version setup screen if needed. We do this -after-
1613 // launching the initial activity (that is, home), so that it can have
1614 // a chance to initialize itself while in the background, making the
1615 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001616 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001617 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001618 }
1619
Dianne Hackborn465fa392014-09-14 14:21:18 -07001620 // Update any services we are bound to that might care about whether
1621 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001622 if (r.app != null) {
1623 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1624 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001625
Craig Mautner2420ead2013-04-01 17:13:20 -07001626 return true;
1627 }
1628
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001629 /**
1630 * Ensure all activities visibility, update orientation and configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001631 *
1632 * @param starting The currently starting activity or {@code null} if there is none.
1633 * @param displayId The id of the display where operation is executed.
1634 * @param markFrozenIfConfigChanged Whether to set {@link ActivityRecord#frozenBeforeDestroy} to
1635 * {@code true} if config changed.
1636 * @param deferResume Whether to defer resume while updating config.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001637 */
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001638 boolean ensureVisibilityAndConfig(ActivityRecord starting, int displayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001639 boolean markFrozenIfConfigChanged, boolean deferResume) {
1640 // First ensure visibility without updating the config just yet. We need this to know what
1641 // activities are affecting configuration now.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001642 // Passing null here for 'starting' param value, so that visibility of actual starting
1643 // activity will be properly updated.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001644 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001645 false /* preserveWindows */, false /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001646
1647 // Force-update the orientation from the WindowManager, since we need the true configuration
1648 // to send to the client now.
1649 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1650 getDisplayOverrideConfiguration(displayId),
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001651 starting != null && starting.mayFreezeScreenLocked(starting.app)
1652 ? starting.appToken : null,
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001653 displayId, true /* forceUpdate */);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001654 if (starting != null && markFrozenIfConfigChanged && config != null) {
1655 starting.frozenBeforeDestroy = true;
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001656 }
1657
1658 // Update the configuration of the activities on the display.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001659 return mService.updateDisplayOverrideConfigurationLocked(config, starting, deferResume,
1660 displayId);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001661 }
1662
Sudheer Shankafab200f2017-05-17 20:41:53 -07001663 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1664 int extrasSize = 0;
1665 if (intent != null) {
1666 final Bundle extras = intent.getExtras();
1667 if (extras != null) {
1668 extrasSize = extras.getSize();
1669 }
1670 }
1671 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1672 if (extrasSize + icicleSize > 200000) {
1673 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1674 + ", icicle size: " + icicleSize);
1675 }
1676 }
1677
Craig Mautnere79d42682013-04-01 19:01:53 -07001678 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001679 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001680 // Is this activity's application already running?
1681 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001682 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001683
Jorim Jaggide8305a2018-05-15 12:06:35 -07001684 getLaunchTimeTracker().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001685
1686 if (app != null && app.thread != null) {
1687 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001688 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1689 || !"android".equals(r.info.packageName)) {
1690 // Don't add this if it is a platform component that is marked
1691 // to run in multiple processes, because this is actually
1692 // part of the framework so doesn't make sense to track as a
1693 // separate apk in the process.
Patrick Baumannc2def582018-04-04 12:14:15 -07001694 app.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode,
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001695 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001696 }
George Mount2c92c972014-03-20 09:38:23 -07001697 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001698 return;
1699 } catch (RemoteException e) {
1700 Slog.w(TAG, "Exception when starting activity "
1701 + r.intent.getComponent().flattenToShortString(), e);
1702 }
1703
1704 // If a dead object exception was thrown -- fall through to
1705 // restart the application.
1706 }
1707
1708 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001709 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001710 }
1711
Bryce Leed3624e12017-11-30 08:51:45 -08001712 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1713 boolean sendHint = forceSend;
1714
1715 if (!sendHint) {
1716 // If not forced, send power hint when the activity's process is different than the
1717 // current resumed activity.
1718 final ActivityRecord resumedActivity = getResumedActivityLocked();
1719 sendHint = resumedActivity == null
1720 || resumedActivity.app == null
1721 || !resumedActivity.app.equals(targetActivity.app);
1722 }
1723
1724 if (sendHint && mService.mLocalPowerManager != null) {
1725 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1726 mPowerHintSent = true;
1727 }
1728 }
1729
1730 void sendPowerHintForLaunchEndIfNeeded() {
1731 // Trigger launch power hint if activity is launched
1732 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1733 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1734 mPowerHintSent = false;
1735 }
1736 }
1737
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001738 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001739 String resultWho, int requestCode, int callingPid, int callingUid,
Winson Chungc9804e72018-05-15 11:01:44 -07001740 String callingPackage, boolean ignoreTargetSecurity, boolean launchingInTask,
1741 ProcessRecord callerApp, ActivityRecord resultRecord, ActivityStack resultStack) {
1742 final boolean isCallerRecents = mService.getRecentTasks() != null &&
1743 mService.getRecentTasks().isCallerRecents(callingUid);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001744 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1745 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001746 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1747 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1748 // caller is the recents component and we are specifically starting an activity in an
1749 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001750 return true;
1751 }
1752 final int componentRestriction = getComponentRestrictionForCallingPackage(
1753 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1754 final int actionRestriction = getActionRestrictionForCallingPackage(
1755 intent.getAction(), callingPackage, callingPid, callingUid);
1756 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1757 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1758 if (resultRecord != null) {
1759 resultStack.sendActivityResultLocked(-1,
1760 resultRecord, resultWho, requestCode,
1761 Activity.RESULT_CANCELED, null);
1762 }
1763 final String msg;
1764 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1765 msg = "Permission Denial: starting " + intent.toString()
1766 + " from " + callerApp + " (pid=" + callingPid
1767 + ", uid=" + callingUid + ")" + " with revoked permission "
1768 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1769 } else if (!aInfo.exported) {
1770 msg = "Permission Denial: starting " + intent.toString()
1771 + " from " + callerApp + " (pid=" + callingPid
1772 + ", uid=" + callingUid + ")"
1773 + " not exported from uid " + aInfo.applicationInfo.uid;
1774 } else {
1775 msg = "Permission Denial: starting " + intent.toString()
1776 + " from " + callerApp + " (pid=" + callingPid
1777 + ", uid=" + callingUid + ")"
1778 + " requires " + aInfo.permission;
1779 }
1780 Slog.w(TAG, msg);
1781 throw new SecurityException(msg);
1782 }
1783
1784 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1785 final String message = "Appop Denial: starting " + intent.toString()
1786 + " from " + callerApp + " (pid=" + callingPid
1787 + ", uid=" + callingUid + ")"
1788 + " requires " + AppOpsManager.permissionToOp(
1789 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1790 Slog.w(TAG, message);
1791 return false;
1792 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1793 final String message = "Appop Denial: starting " + intent.toString()
1794 + " from " + callerApp + " (pid=" + callingPid
1795 + ", uid=" + callingUid + ")"
1796 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1797 Slog.w(TAG, message);
1798 return false;
1799 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001800
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001801 return true;
1802 }
1803
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001804 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001805 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1806 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001807 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1808 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1809
Andrii Kulian02689a72017-07-06 14:28:59 -07001810 if (callingPid == -1 && callingUid == -1) {
1811 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1812 return true;
1813 }
1814
Andrii Kulian62e6f252017-05-30 22:46:53 -07001815 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001816 if (activityDisplay == null) {
1817 Slog.w(TAG, "Launch on display check: display not found");
1818 return false;
1819 }
1820
Andrii Kulian02689a72017-07-06 14:28:59 -07001821 // Check if the caller has enough privileges to embed activities and launch to private
1822 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001823 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001824 callingUid);
1825 if (startAnyPerm == PERMISSION_GRANTED) {
1826 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1827 + " allow launch any on display");
1828 return true;
1829 }
1830
Andrii Kulian8f070292017-09-12 22:56:49 -07001831 // Check if caller is already present on display
1832 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1833
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001834 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1835 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1836 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001837 // Limit launching on virtual displays, because their contents can be read from Surface
1838 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001839 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1840 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1841 + " disallow launch on virtual display for not-embedded activity.");
1842 return false;
1843 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001844 // Check if the caller is allowed to embed activities from other apps.
1845 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001846 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001847 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1848 + " disallow activity embedding without permission.");
1849 return false;
1850 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001851 }
1852
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001853 if (!activityDisplay.isPrivate()) {
1854 // Anyone can launch on a public display.
1855 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1856 + " allow launch on public display");
1857 return true;
1858 }
1859
1860 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001861 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001862 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1863 + " allow launch for owner of the display");
1864 return true;
1865 }
1866
Andrii Kulian8f070292017-09-12 22:56:49 -07001867 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001868 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1869 + " allow launch for caller present on the display");
1870 return true;
1871 }
1872
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001873 Slog.w(TAG, "Launch on display check: denied");
1874 return false;
1875 }
1876
1877 /** Update lists of UIDs that are present on displays and have access to them. */
1878 void updateUIDsPresentOnDisplay() {
1879 mDisplayAccessUIDs.clear();
1880 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1881 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001882 // Only bother calculating the whitelist for private displays
1883 if (activityDisplay.isPrivate()) {
1884 mDisplayAccessUIDs.append(
1885 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1886 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001887 }
1888 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1889 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1890 }
1891
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001892 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001893 final long identity = Binder.clearCallingIdentity();
1894 try {
1895 return UserManager.get(mService.mContext).getUserInfo(userId);
1896 } finally {
1897 Binder.restoreCallingIdentity(identity);
1898 }
1899 }
1900
Svet Ganov99b60432015-06-27 13:15:22 -07001901 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001902 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001903 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1904 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001905 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001906 return ACTIVITY_RESTRICTION_PERMISSION;
1907 }
1908
Christopher Tateff7add02015-08-17 10:23:22 -07001909 if (activityInfo.permission == null) {
1910 return ACTIVITY_RESTRICTION_NONE;
1911 }
1912
Svet Ganov99b60432015-06-27 13:15:22 -07001913 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1914 if (opCode == AppOpsManager.OP_NONE) {
1915 return ACTIVITY_RESTRICTION_NONE;
1916 }
1917
1918 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1919 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001920 if (!ignoreTargetSecurity) {
1921 return ACTIVITY_RESTRICTION_APPOP;
1922 }
Svet Ganov99b60432015-06-27 13:15:22 -07001923 }
1924
1925 return ACTIVITY_RESTRICTION_NONE;
1926 }
1927
Svetoslav7008b512015-06-24 18:47:07 -07001928 private int getActionRestrictionForCallingPackage(String action,
1929 String callingPackage, int callingPid, int callingUid) {
1930 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001931 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001932 }
1933
1934 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1935 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001936 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001937 }
1938
1939 final PackageInfo packageInfo;
1940 try {
1941 packageInfo = mService.mContext.getPackageManager()
1942 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1943 } catch (PackageManager.NameNotFoundException e) {
1944 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001945 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001946 }
1947
1948 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001949 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001950 }
1951
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001952 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001953 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001954 }
1955
1956 final int opCode = AppOpsManager.permissionToOpCode(permission);
1957 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001958 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001959 }
1960
1961 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1962 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001963 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001964 }
1965
Svet Ganov99b60432015-06-27 13:15:22 -07001966 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001967 }
1968
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001969 void setLaunchSource(int uid) {
1970 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1971 }
1972
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001973 void acquireLaunchWakelock() {
1974 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1975 throw new IllegalStateException("Calling must be system uid");
1976 }
1977 mLaunchingActivity.acquire();
1978 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1979 // To be safe, don't allow the wake lock to be held for too long.
1980 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1981 }
1982 }
1983
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001984 /**
1985 * Called when the frontmost task is idle.
1986 * @return the state of mService.mBooting before this was called.
1987 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001988 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001989 private boolean checkFinishBootingLocked() {
1990 final boolean booting = mService.mBooting;
1991 boolean enableScreen = false;
1992 mService.mBooting = false;
1993 if (!mService.mBooted) {
1994 mService.mBooted = true;
1995 enableScreen = true;
1996 }
1997 if (booting || enableScreen) {
1998 mService.postFinishBooting(booting, enableScreen);
1999 }
2000 return booting;
2001 }
2002
Craig Mautnerf3333272013-04-22 10:55:53 -07002003 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08002004 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07002005 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08002006 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002007 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002008
Craig Mautnerf3333272013-04-22 10:55:53 -07002009 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002010 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002011 int NS = 0;
2012 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002013 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002014 boolean activityRemoved = false;
2015
Wale Ogunwale7d701172015-03-11 15:36:30 -07002016 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002017 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002018 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2019 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002020 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2021 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002022 if (fromTimeout) {
2023 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002024 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002025
2026 // This is a hack to semi-deal with a race condition
2027 // in the client where it can be constructed with a
2028 // newer configuration from when we asked it to launch.
2029 // We'll update with whatever configuration it now says
2030 // it used to launch.
2031 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08002032 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002033 }
2034
2035 // We are now idle. If someone is waiting for a thumbnail from
2036 // us, we can now deliver.
2037 r.idle = true;
2038
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002039 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002040 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002041 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002042 }
2043 }
2044
2045 if (allResumedActivitiesIdle()) {
2046 if (r != null) {
2047 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002048 }
2049
2050 if (mLaunchingActivity.isHeld()) {
2051 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2052 if (VALIDATE_WAKE_LOCK_CALLER &&
2053 Binder.getCallingUid() != Process.myUid()) {
2054 throw new IllegalStateException("Calling must be system uid");
2055 }
2056 mLaunchingActivity.release();
2057 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002058 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002059 }
2060
2061 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08002062 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
2063 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002064 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002065 if ((NF = mFinishingActivities.size()) > 0) {
2066 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002067 mFinishingActivities.clear();
2068 }
2069
Craig Mautnerf3333272013-04-22 10:55:53 -07002070 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002071 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002072 mStartingUsers.clear();
2073 }
2074
Craig Mautnerf3333272013-04-22 10:55:53 -07002075 // Stop any activities that are scheduled to do so but have been
2076 // waiting for the next one to start.
2077 for (int i = 0; i < NS; i++) {
2078 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002079 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002080 if (stack != null) {
2081 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08002082 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
2083 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002084 } else {
2085 stack.stopActivityLocked(r);
2086 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002087 }
2088 }
2089
2090 // Finish any activities that are scheduled to do so but have been
2091 // waiting for the next one to start.
2092 for (int i = 0; i < NF; i++) {
2093 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002094 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002095 if (stack != null) {
2096 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2097 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002098 }
2099
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002100 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002101 // Complete user switch
2102 if (startingUsers != null) {
2103 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002104 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002105 }
2106 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002107 }
2108
2109 mService.trimApplications();
2110 //dump();
2111 //mWindowManager.dump();
2112
Craig Mautnerf3333272013-04-22 10:55:53 -07002113 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002114 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002115 }
2116
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002117 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002118 }
2119
Craig Mautner8e569572013-10-11 17:36:59 -07002120 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002121 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002122 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002123 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2124 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2125 final ActivityStack stack = display.getChildAt(stackNdx);
2126 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002127 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002128 }
Craig Mautner19091252013-10-05 00:03:53 -07002129 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
2131
2132 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002133 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002134 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2135 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2136 final ActivityStack stack = display.getChildAt(stackNdx);
2137 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 }
2140 }
2141
Craig Mautner93529a42013-10-04 15:03:13 -07002142 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002143 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002144 }
2145
Craig Mautner8d341ef2013-03-26 09:03:27 -07002146 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002147 * Update the last used stack id for non-current user (current user's last
2148 * used stack is the focused stack)
2149 */
2150 void updateUserStackLocked(int userId, ActivityStack stack) {
2151 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002152 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002153 }
2154 }
2155
2156 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002157 * @return true if some activity was finished (or would have finished if doit were true).
2158 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002159 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2160 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002161 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002162 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002163 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2164 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2165 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002166 if (stack.finishDisabledPackageActivitiesLocked(
2167 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002168 didSomething = true;
2169 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002170 }
2171 }
2172 return didSomething;
2173 }
2174
Dianne Hackborna413dc02013-07-12 12:02:55 -07002175 void updatePreviousProcessLocked(ActivityRecord r) {
2176 // Now that this process has stopped, we may want to consider
2177 // it to be the previous app to try to keep around in case
2178 // the user wants to return to it.
2179
2180 // First, found out what is currently the foreground app, so that
2181 // we don't blow away the previous app if this activity is being
2182 // hosted by the process that is actually still the foreground.
2183 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002184 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002185 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2186 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2187 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002188 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002189 final ActivityRecord resumedActivity = stack.getResumedActivity();
2190 if (resumedActivity != null) {
2191 fgApp = resumedActivity.app;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002192 } else if (stack.mPausingActivity != null) {
2193 fgApp = stack.mPausingActivity.app;
2194 }
2195 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002196 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002197 }
2198 }
2199
2200 // Now set this one as the previous process, only if that really
2201 // makes sense to.
2202 if (r.app != null && fgApp != null && r.app != fgApp
2203 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002204 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002205 mService.mPreviousProcess = r.app;
2206 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2207 }
2208 }
2209
Wale Ogunwaled046a012015-12-24 13:05:59 -08002210 boolean resumeFocusedStackTopActivityLocked() {
2211 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002212 }
2213
Wale Ogunwaled046a012015-12-24 13:05:59 -08002214 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002215 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002216
2217 if (!readyToResume()) {
2218 return false;
2219 }
2220
Wale Ogunwaled046a012015-12-24 13:05:59 -08002221 if (targetStack != null && isFocusedStack(targetStack)) {
2222 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002223 }
chaviw59b98852017-06-13 12:05:44 -07002224
Wale Ogunwale06579d62016-04-30 15:29:06 -07002225 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08002226 if (r == null || !r.isState(RESUMED)) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07002227 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
Bryce Lee7ace3952018-02-16 14:34:32 -08002228 } else if (r.isState(RESUMED)) {
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002229 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2230 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002231 }
chaviw59b98852017-06-13 12:05:44 -07002232
Wale Ogunwaled046a012015-12-24 13:05:59 -08002233 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002234 }
2235
Todd Kennedy39bfee52016-02-24 10:28:21 -08002236 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2237 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002238 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2239 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2240 final ActivityStack stack = display.getChildAt(stackNdx);
2241 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002242 }
2243 }
2244 }
2245
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002246 /**
2247 * Finish the topmost activities in all stacks that belong to the crashed app.
2248 * @param app The app that crashed.
2249 * @param reason Reason to perform this action.
2250 * @return The task that was finished in this stack, {@code null} if haven't found any.
2251 */
2252 TaskRecord finishTopCrashedActivitiesLocked(ProcessRecord app, String reason) {
Adrian Roos20d7df32016-01-12 18:59:43 +01002253 TaskRecord finishedTask = null;
2254 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002255 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002256 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002257 // It is possible that request to finish activity might also remove its task and stack,
2258 // so we need to be careful with indexes in the loop and check child count every time.
2259 for (int stackNdx = 0; stackNdx < display.getChildCount(); ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002260 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002261 final TaskRecord t = stack.finishTopCrashedActivityLocked(app, reason);
Adrian Roos20d7df32016-01-12 18:59:43 +01002262 if (stack == focusedStack || finishedTask == null) {
2263 finishedTask = t;
2264 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002265 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002266 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002267 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002268 }
2269
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002270 void finishVoiceTask(IVoiceInteractionSession session) {
2271 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002272 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2273 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002274 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002275 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002276 stack.finishVoiceTask(session);
2277 }
2278 }
2279 }
2280
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002281 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2282 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002283 final ActivityStack currentStack = task.getStack();
2284 if (currentStack == null) {
2285 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2286 + task + " to front. Stack is null");
2287 return;
2288 }
2289
Craig Mautneraea74a52014-03-08 14:23:10 -08002290 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2291 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002292 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002293
2294 final ActivityRecord prev = topRunningActivityLocked();
2295
2296 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2297 || (prev != null && prev.isActivityTypeRecents())) {
2298 // Caller wants the home activity moved with it or the previous task is recents in which
2299 // case we always return home from the task we are moving to the front.
2300 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002301 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002302
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002303 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002304 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002305 task.updateOverrideConfiguration(bounds);
2306
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002307 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002308
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002309 if (stack != currentStack) {
2310 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002311 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002312 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002313 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2314 // still need moveTaskToFrontLocked() below for any transition settings.
2315 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002316 if (stack.resizeStackWithLaunchBounds()) {
2317 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002318 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2319 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002320 } else {
2321 // WM resizeTask must be done after the task is moved to the correct stack,
2322 // because Task's setBounds() also updates dim layer's bounds, but that has
2323 // dependency on the stack.
2324 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002325 }
2326 }
2327
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002328 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07002329 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002330 r == null ? null : r.appTimeTracker, reason);
2331
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002332 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002333 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002334
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002335 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002336 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002337 }
2338
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002339 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002340 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002341 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002342 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002343 return false;
2344 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002345 return (mService.mSupportsPictureInPicture
2346 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002347 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002348 }
2349
Bryce Leeec55eb02017-12-05 20:51:27 -08002350 LaunchParamsController getLaunchParamsController() {
2351 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002352 }
2353
Winson Chung55893332017-02-17 17:13:10 -08002354 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002355 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2356 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2357 if (stack != null) {
2358 return stack;
2359 }
2360 }
2361 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002362 }
2363
Wale Ogunwale68278562017-09-23 17:13:55 -07002364 /** @see ActivityDisplay#getStack(int, int) */
2365 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2366 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2367 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2368 if (stack != null) {
2369 return stack;
2370 }
2371 }
2372 return null;
2373 }
2374
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002375 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002376 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002377 // Preference is given to the activity type for the activity then the task since the type
2378 // once set shouldn't change.
2379 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2380 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2381 activityType = task.getActivityType();
2382 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2384 return activityType;
2385 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002386 if (options != null) {
2387 activityType = options.getLaunchActivityType();
2388 }
2389 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002390 }
2391
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002392 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2393 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2394 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002395 }
2396
2397 /**
2398 * Returns the right stack to use for launching factoring in all the input parameters.
2399 *
2400 * @param r The activity we are trying to launch. Can be null.
2401 * @param options The activity options used to the launch. Can be null.
2402 * @param candidateTask The possible task the activity might be launched in. Can be null.
2403 *
2404 * @return The stack to use for the launch or INVALID_STACK_ID.
2405 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002406 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2407 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2408 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002409 int taskId = INVALID_TASK_ID;
2410 int displayId = INVALID_DISPLAY;
2411 //Rect bounds = null;
2412
2413 // We give preference to the launch preference in activity options.
2414 if (options != null) {
2415 taskId = options.getLaunchTaskId();
2416 displayId = options.getLaunchDisplayId();
2417 // TODO: Need to work this into the equation...
2418 //bounds = options.getLaunchBounds();
2419 }
2420
2421 // First preference for stack goes to the task Id set in the activity options. Use the stack
2422 // associated with that if possible.
2423 if (taskId != INVALID_TASK_ID) {
2424 // Temporarily set the task id to invalid in case in re-entry.
2425 options.setLaunchTaskId(INVALID_TASK_ID);
2426 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002427 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002428 options.setLaunchTaskId(taskId);
2429 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002430 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002431 }
2432 }
2433
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002434 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002435 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002436
2437 // Next preference for stack goes to the display Id set in the activity options or the
2438 // candidate display.
2439 if (displayId == INVALID_DISPLAY) {
2440 displayId = candidateDisplayId;
2441 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002442 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002443 if (r != null) {
2444 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002445 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002446 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002447 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002448 }
2449 }
2450 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2451 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002452 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002453 if (stack != null) {
2454 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002455 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002456 }
2457 }
2458
2459 // Give preference to the stack and display of the input task and activity if they match the
2460 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002461 stack = null;
2462 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002463 if (candidateTask != null) {
2464 stack = candidateTask.getStack();
2465 }
2466 if (stack == null && r != null) {
2467 stack = r.getStack();
2468 }
2469 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002470 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002471 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002472 final int windowingMode =
2473 display.resolveWindowingMode(r, options, candidateTask, activityType);
2474 if (stack.isCompatible(windowingMode, activityType)) {
2475 return stack;
2476 }
Andrii Kulianec7d4c42018-04-26 18:31:58 -07002477 if (windowingMode == WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY
2478 && display.getSplitScreenPrimaryStack() == stack
2479 && candidateTask == stack.topTask()) {
2480 // This is a special case when we try to launch an activity that is currently on
2481 // top of split-screen primary stack, but is targeting split-screen secondary.
2482 // In this case we don't want to move it to another stack.
2483 // TODO(b/78788972): Remove after differentiating between preferred and required
2484 // launch options.
2485 return stack;
2486 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002487 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002488 }
2489
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002490 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002491 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002492 // TODO: Can be removed once we figure-out how non-standard types should launch
2493 // outside the default display.
2494 || (activityType != ACTIVITY_TYPE_STANDARD
2495 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2496 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002497 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002498
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002499 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002500 }
2501
Andrii Kulianb850ea52017-12-12 23:49:10 -08002502 /** @return true if activity record is null or can be launched on provided display. */
2503 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2504 if (r == null) {
2505 return true;
2506 }
2507 return r.canBeLaunchedOnDisplay(displayId);
2508 }
2509
Andrii Kulian16802aa2016-11-02 12:21:33 -07002510 /**
2511 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2512 * If there is no such stack, new dynamic stack can be created.
2513 * @param displayId Target display.
2514 * @param r Activity that should be launched there.
2515 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2516 */
2517 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002518 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002519 if (activityDisplay == null) {
2520 throw new IllegalArgumentException(
2521 "Display with displayId=" + displayId + " not found.");
2522 }
2523
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002524 if (!r.canBeLaunchedOnDisplay(displayId)) {
2525 return null;
2526 }
2527
Andrii Kulian16802aa2016-11-02 12:21:33 -07002528 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002529 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2530 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002531 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002532 return stack;
2533 }
2534 }
2535
2536 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002537 if (displayId != DEFAULT_DISPLAY) {
2538 return activityDisplay.createStack(
2539 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002540 }
2541
2542 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2543 return null;
2544 }
2545
Wale Ogunwale68278562017-09-23 17:13:55 -07002546 // TODO: Can probably be consolidated into getLaunchStack()...
2547 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2548 switch (stack.getActivityType()) {
2549 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2550 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2551 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002552 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002553 switch (stack.getWindowingMode()) {
2554 case WINDOWING_MODE_FULLSCREEN: return true;
2555 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2556 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2557 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2558 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2559 }
2560
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002561 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002562 return r.canBeLaunchedOnDisplay(displayId);
2563 }
2564 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2565 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002566 }
2567
Andrii Kulian7fc22812016-12-28 13:04:11 -08002568 /**
2569 * Get next focusable stack in the system. This will search across displays and stacks
2570 * in last-focused order for a focusable and visible stack, different from the target stack.
2571 *
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002572 * @param currentFocus The stack that previously had focus.
2573 * @param ignoreCurrent If we should ignore {@param currentFocus} when searching for next
2574 * candidate.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002575 * @return Next focusable {@link ActivityStack}, null if not found.
2576 */
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002577 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus, boolean ignoreCurrent) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002578 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2579
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002580 final int currentWindowingMode = currentFocus != null
2581 ? currentFocus.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
2582 ActivityStack candidate = null;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002583 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2584 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002585 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002586 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
Wale Ogunwalec2ea3642018-05-24 00:32:23 -07002587 if (display == null) {
2588 // Looks like the display no longer exists in the system...
2589 continue;
2590 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002591 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2592 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002593 if (ignoreCurrent && stack == currentFocus) {
2594 continue;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002595 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002596 if (!stack.isFocusable() || !stack.shouldBeVisible(null)) {
2597 continue;
2598 }
2599
2600 if (currentWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2601 && candidate == null && stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale17696de2018-03-15 16:41:05 -07002602 // If the currently focused stack is in split-screen secondary we save off the
2603 // top primary split-screen stack as a candidate for focus because we might
2604 // prefer focus to move to an other stack to avoid primary split-screen stack
2605 // overlapping with a fullscreen stack when a fullscreen stack is higher in z
2606 // than the next split-screen stack. Assistant stack, I am looking at you...
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002607 // We only move the focus to the primary-split screen stack if there isn't a
2608 // better alternative.
2609 candidate = stack;
2610 continue;
2611 }
Wale Ogunwale17696de2018-03-15 16:41:05 -07002612 if (candidate != null && stack.inSplitScreenSecondaryWindowingMode()) {
2613 // Use the candidate stack since we are now at the secondary split-screen.
2614 return candidate;
2615 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002616 return stack;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002617 }
2618 }
2619
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002620 return candidate;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002621 }
2622
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002623 /**
2624 * Get next valid stack for launching provided activity in the system. This will search across
2625 * displays and stacks in last-focused order for a focusable and visible stack, except those
2626 * that are on a currently focused display.
2627 *
2628 * @param r The activity that is being launched.
2629 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2630 * searching for the next candidate.
2631 * @return Next valid {@link ActivityStack}, null if not found.
2632 */
2633 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2634 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2635 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2636 final int displayId = mTmpOrderedDisplayIds.get(i);
2637 if (displayId == currentFocus) {
2638 continue;
2639 }
2640 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2641 if (stack != null) {
2642 return stack;
2643 }
2644 }
2645 return null;
2646 }
2647
Craig Mautneree2e45a2014-06-27 12:10:03 -07002648 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002649 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002650 }
2651
2652 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002653 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2654 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2655 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002656 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 final ArrayList<ActivityRecord> activities = task.mActivities;
2658 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2659 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002660 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002661 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002662 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 }
2664 }
2665 }
2666 }
2667 return null;
2668 }
2669
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002670 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2671 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2672 boolean deferResume) {
2673
2674 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002675 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002676 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002677 return;
2678 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002679
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002680 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002681 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002682 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002683 // If the docked stack exists, don't resize non-floating stacks independently of the
2684 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002685 return;
2686 }
2687
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002688 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002689 mWindowManager.deferSurfaceLayout();
2690 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002691 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002692 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2693 // null bounds = fullscreen windowing mode...at least for now.
2694 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2695 } else if (splitScreenActive) {
2696 // If we are in split-screen mode and this stack support split-screen, then
2697 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2698 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2699 }
2700 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002701 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002702 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002703 stack.ensureVisibleActivitiesConfigurationLocked(
2704 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002705 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002706 } finally {
2707 mWindowManager.continueSurfaceLayout();
2708 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002709 }
2710 }
2711
Winson Chungc1674272018-02-21 10:15:17 -08002712 void deferUpdateRecentsHomeStackBounds() {
2713 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
2714 deferUpdateBounds(ACTIVITY_TYPE_HOME);
2715 }
2716
Bryce Leed3624e12017-11-30 08:51:45 -08002717 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002718 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002719 if (stack != null) {
2720 stack.deferUpdateBounds();
2721 }
2722 }
2723
Winson Chungc1674272018-02-21 10:15:17 -08002724 void continueUpdateRecentsHomeStackBounds() {
2725 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
2726 continueUpdateBounds(ACTIVITY_TYPE_HOME);
2727 }
2728
Bryce Leed3624e12017-11-30 08:51:45 -08002729 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002730 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002731 if (stack != null) {
2732 stack.continueUpdateBounds();
2733 }
2734 }
2735
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002736 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08002737 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002738 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2739 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002740 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002741 if (task != null) {
2742 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002743 }
2744 }
2745 mResizingTasksDuringAnimation.clear();
2746 }
2747
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002748 /**
2749 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002750 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002751 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002752 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002753 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002754
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002755 mWindowManager.deferSurfaceLayout();
2756 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002757 final int windowingMode = fromStack.getWindowingMode();
2758 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2759 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2760
2761 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002762 // Tell the display we are exiting split-screen mode.
2763 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002764 // We are moving all tasks from the docked stack to the fullscreen stack,
2765 // which is dismissing the docked stack, so resize all other stacks to
2766 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002767 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2768 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002769 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2770 continue;
2771 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002772 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002773 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002774 }
2775
2776 // Also disable docked stack resizing since we have manually adjusted the
2777 // size of other stacks above and we don't want to trigger a docked stack
2778 // resize when we remove task from it below and it is detached from the
2779 // display because it no longer contains any tasks.
2780 mAllowDockedStackResize = false;
2781 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002782
Winson Chung5af42fc2017-03-24 17:11:33 -07002783 // If we are moving from the pinned stack, then the animation takes care of updating
2784 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002785 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002786 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002787
Wale Ogunwale388945c2017-10-04 12:13:46 -07002788 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002789 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002790 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002791 for (int i = 0; i < size; ++i) {
2792 final TaskRecord task = tasks.get(i);
2793 final ActivityStack toStack = toDisplay.getOrCreateStack(
2794 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002795
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002796 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002797 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002798 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002799 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002800 isTopTask /* animate */, DEFER_RESUME,
2801 schedulePictureInPictureModeChange,
2802 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002803 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2804 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002805 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002806 // Position the tasks in the fullscreen stack in order at the bottom of the
2807 // stack. Also defer resume until all the tasks have been moved to the
2808 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002809 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002810 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2811 schedulePictureInPictureModeChange,
2812 "moveTasksToFullscreenStack - NOT_onTop");
2813 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002814 }
2815 }
Winson Chung74666102017-02-22 17:49:24 -08002816
2817 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2818 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002819 } finally {
2820 mAllowDockedStackResize = true;
2821 mWindowManager.continueSurfaceLayout();
2822 }
2823 }
2824
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002825 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002826 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2827 }
2828
2829 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2830 mWindowManager.inSurfaceTransaction(() ->
2831 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002832 }
2833
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002834 void setSplitScreenResizing(boolean resizing) {
2835 if (resizing == mDockedStackResizing) {
2836 return;
2837 }
2838
2839 mDockedStackResizing = resizing;
2840 mWindowManager.setDockedStackResizing(resizing);
2841
2842 if (!resizing && mHasPendingDockedBounds) {
2843 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
2844 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
2845 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
2846
2847 mHasPendingDockedBounds = false;
2848 mPendingDockedBounds = null;
2849 mPendingTempDockedTaskBounds = null;
2850 mPendingTempDockedTaskInsetBounds = null;
2851 mPendingTempOtherTaskBounds = null;
2852 mPendingTempOtherTaskInsetBounds = null;
2853 }
2854 }
2855
Jorim Jaggidc249c42015-12-15 14:57:31 -08002856 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002857 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2858 boolean preserveWindows) {
2859 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2860 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2861 false /* deferResume */);
2862 }
2863
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002864 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002865 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2866 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002867
2868 if (!mAllowDockedStackResize) {
2869 // Docked stack resize currently disabled.
2870 return;
2871 }
2872
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002873 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002874 if (stack == null) {
2875 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2876 return;
2877 }
2878
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002879 if (mDockedStackResizing) {
2880 mHasPendingDockedBounds = true;
2881 mPendingDockedBounds = copyOrNull(dockedBounds);
2882 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
2883 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
2884 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
2885 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
2886 }
2887
Jorim Jaggidc249c42015-12-15 14:57:31 -08002888 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2889 mWindowManager.deferSurfaceLayout();
2890 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002891 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2892 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002893 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002894 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002895
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002896 // TODO: Checking for isAttached might not be needed as if the user passes in null
2897 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002898 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2899 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002900 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002901 // In this case we make all other static stacks fullscreen and move all
2902 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002903 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002904
2905 // stack shouldn't contain anymore activities, so nothing to resume.
2906 r = null;
2907 } else {
2908 // Docked stacks occupy a dedicated region on screen so the size of all other
2909 // static stacks need to be adjusted so they don't overlap with the docked stack.
2910 // We get the bounds to use from window manager which has been adjusted for any
2911 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002912 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002913 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002914 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2915 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002916 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002917 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002918 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002919 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002920 continue;
2921 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002922 if (mDockedStackResizing && !current.isTopActivityVisible()) {
2923 // Non-visible stacks get resized once we're done with the resize
2924 // interaction.
2925 continue;
2926 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002927 // Need to set windowing mode here before we try to get the dock bounds.
2928 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2929 current.getStackDockedModeBounds(
2930 tempOtherTaskBounds /* currentTempTaskBounds */,
2931 tempRect /* outStackBounds */,
2932 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2933
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002934 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002935 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2936 tempOtherTaskInsetBounds, preserveWindows,
2937 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002938 }
2939 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002940 if (!deferResume) {
2941 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2942 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002943 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002944 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002945 mWindowManager.continueSurfaceLayout();
2946 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2947 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002948 }
2949
Robert Carr0d00c2e2016-02-29 17:45:02 -08002950 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002951 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002952 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002953 if (stack == null) {
2954 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2955 return;
2956 }
Winson Chung19953ca2017-04-11 11:19:23 -07002957
2958 // It is possible for the bounds animation from the WM to call this but be delayed by
2959 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2960 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2961 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2962 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002963 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002964 return;
2965 }
2966
Robert Carr0d00c2e2016-02-29 17:45:02 -08002967 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2968 mWindowManager.deferSurfaceLayout();
2969 try {
2970 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002971 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02002972 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
2973 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08002974 insetBounds = tempRect;
2975 insetBounds.top = 0;
2976 insetBounds.left = 0;
2977 insetBounds.right = tempPinnedTaskBounds.width();
2978 insetBounds.bottom = tempPinnedTaskBounds.height();
2979 }
Winson Chung15036ca2018-05-31 15:51:47 -07002980 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
2981 // We have finished the animation into PiP, and are resizing the tasks to match the
2982 // stack bounds, while layouts are deferred, update any task state as a part of
2983 // transitioning it from fullscreen into a floating state.
2984 stack.onPipAnimationEndResize();
2985 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08002986 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002987 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002988 } finally {
2989 mWindowManager.continueSurfaceLayout();
2990 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2991 }
2992 }
2993
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002994 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002995 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002996 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002997 /**
2998 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2999 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
3000 * that the client receives onStop() before it is reparented. We do this by detaching
3001 * the stack from the display so that it will be considered invisible when
3002 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
3003 * invisible as well and added to the stopping list. After which we process the
3004 * stopping list by handling the idle.
3005 */
3006 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
3007 pinnedStack.mForceHidden = true;
3008 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
3009 pinnedStack.mForceHidden = false;
3010 activityIdleInternalLocked(null, false /* fromTimeout */,
3011 true /* processPausingActivites */, null /* configuration */);
3012
3013 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003014 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08003015 } else {
3016 for (int i = tasks.size() - 1; i >= 0; i--) {
3017 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07003018 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08003019 }
3020 }
3021 }
3022
3023 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003024 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07003025 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
3026 * instead moved back onto the fullscreen stack.
3027 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003028 void removeStack(ActivityStack stack) {
3029 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003030 }
3031
Wale Ogunwale68278562017-09-23 17:13:55 -07003032 /**
3033 * Removes stacks in the input windowing modes from the system if they are of activity type
3034 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
3035 */
3036 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003037 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003038 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
3039 }
3040 }
3041
3042 void removeStacksWithActivityTypes(int... activityTypes) {
3043 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3044 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003045 }
Robert Carr6914f082017-03-20 19:04:30 -07003046 }
3047
3048 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003049 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
3050 */
Winson Chung0ec2a352017-10-26 11:38:30 -07003051 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
3052 String reason) {
3053 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
3054 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07003055 }
3056
3057 /**
Winson Chung010927a2016-12-15 16:12:35 -08003058 * Removes the task with the specified task id.
3059 *
3060 * @param taskId Identifier of the task to be removed.
3061 * @param killProcess Kill any process associated with the task if possible.
3062 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07003063 * @param pauseImmediately Pauses all task activities immediately without waiting for the
3064 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08003065 * @return Returns true if the given task was found and removed.
3066 */
Winson Chung6954fc92017-03-24 16:22:12 -07003067 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07003068 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003069 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08003070 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003071 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08003072 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Bryce Lee2b8e0372018-04-05 17:01:37 -07003073 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003074 if (tr.isPersistable) {
3075 mService.notifyTaskPersisterLocked(null, true);
3076 }
3077 return true;
3078 }
3079 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
3080 return false;
3081 }
3082
3083 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
3084 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07003085 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003086 }
3087 ComponentName component = tr.getBaseIntent().getComponent();
3088 if (component == null) {
3089 Slog.w(TAG, "No component for base intent of task: " + tr);
3090 return;
3091 }
3092
3093 // Find any running services associated with this app and stop if needed.
3094 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
3095
3096 if (!killProcess) {
3097 return;
3098 }
3099
3100 // Determine if the process(es) for this task should be killed.
3101 final String pkg = component.getPackageName();
3102 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
3103 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
3104 for (int i = 0; i < pmap.size(); i++) {
3105
3106 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
3107 for (int j = 0; j < uids.size(); j++) {
3108 ProcessRecord proc = uids.valueAt(j);
3109 if (proc.userId != tr.userId) {
3110 // Don't kill process for a different user.
3111 continue;
3112 }
3113 if (proc == mService.mHomeProcess) {
3114 // Don't kill the home process along with tasks from the same package.
3115 continue;
3116 }
3117 if (!proc.pkgList.containsKey(pkg)) {
3118 // Don't kill process that is not associated with this task.
3119 continue;
3120 }
3121
3122 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003123 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08003124 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
3125 // Don't kill process(es) that has an activity in a different task that is
3126 // also in recents.
3127 return;
3128 }
3129 }
3130
3131 if (proc.foregroundServices) {
3132 // Don't kill process(es) with foreground service.
3133 return;
3134 }
3135
3136 // Add process to kill list.
3137 procsToKill.add(proc);
3138 }
3139 }
3140
3141 // Kill the running processes.
3142 for (int i = 0; i < procsToKill.size(); i++) {
3143 ProcessRecord pr = procsToKill.get(i);
3144 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
3145 && pr.curReceivers.isEmpty()) {
3146 pr.kill("remove task", true);
3147 } else {
3148 // We delay killing processes that are not in the background or running a receiver.
3149 pr.waitingToKill = "remove task";
3150 }
3151 }
3152 }
3153
Chong Zhang5dcb2752015-08-18 13:50:26 -07003154 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07003155 * Called to restore the state of the task into the stack that it's supposed to go into.
3156 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07003157 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003158 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003159 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003160 * @return true if the task has been restored successfully.
3161 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003162 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
3163 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07003164 final ActivityStack currentStack = task.getStack();
3165 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003166 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003167 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003168 // Nothing else to do since it is already restored in the right stack.
3169 return true;
3170 }
3171 // Remove current stack association, so we can re-associate the task with the
3172 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003173 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003174 }
3175
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003176 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003177 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003178 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003179 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3180 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003181 final ArrayList<ActivityRecord> activities = task.mActivities;
3182 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003183 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003184 }
3185 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003186 }
3187
Winson Chung1dbc8112017-09-28 18:05:31 -07003188 @Override
3189 public void onRecentTaskAdded(TaskRecord task) {
3190 task.touchActiveTime();
3191 }
3192
3193 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003194 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003195 if (wasTrimmed) {
3196 // Task was trimmed from the recent tasks list -- remove the active task record as well
3197 // since the user won't really be able to go back to it
3198 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3199 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
3200 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003201 task.removedFromRecents();
3202 }
3203
Wale Ogunwale040b4702015-08-06 18:10:50 -07003204 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003205 * Move stack with all its existing content to specified display.
3206 * @param stackId Id of stack to move.
3207 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003208 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003209 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003210 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003211 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003212 if (activityDisplay == null) {
3213 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3214 + displayId);
3215 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003216 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003217 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003218 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3219 + stackId);
3220 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003221
3222 final ActivityDisplay currentDisplay = stack.getDisplay();
3223 if (currentDisplay == null) {
3224 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3225 + " is not attached to any display.");
3226 }
3227
3228 if (currentDisplay.mDisplayId == displayId) {
3229 throw new IllegalArgumentException("Trying to move stack=" + stack
3230 + " to its current displayId=" + displayId);
3231 }
3232
3233 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003234 // TODO(multi-display): resize stacks properly if moved from split-screen.
3235 }
3236
3237 /**
Winson Chung74666102017-02-22 17:49:24 -08003238 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3239 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003240 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003241 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003242 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003243 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003244 final int stackId = stack.mStackId;
3245 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003246
Winson Chung74666102017-02-22 17:49:24 -08003247 // Check that we aren't reparenting to the same stack that the task is already in
3248 if (prevStack != null && prevStack.mStackId == stackId) {
3249 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3250 + " already in stackId=" + stackId);
3251 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003252 }
3253
Winson Chung74666102017-02-22 17:49:24 -08003254 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3255 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003256 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003257 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003258 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003259 }
3260
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003261 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3262 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003263 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003264 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3265 + " reparent task=" + task + " to stackId=" + stackId);
3266 }
3267
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003268 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003269 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3270 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003271 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3272 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003273 }
3274
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003275 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3276 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003277 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003278 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3279 + " Moving to a fullscreen stack instead.");
3280 if (prevStack != null) {
3281 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003282 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003283 stack = stack.getDisplay().createStack(
3284 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003285 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003286 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003287 }
3288
Winson Chung08f81892017-03-02 15:40:51 -08003289 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003290 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003291 if (stack == null) {
3292 throw new IllegalArgumentException(
3293 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3294 }
3295
3296 final ActivityRecord r = stack.topRunningActivityLocked();
3297 if (r == null) {
3298 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3299 + " in stack=" + stack);
3300 return false;
3301 }
3302
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003303 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003304 Slog.w(TAG,
3305 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003306 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003307 return false;
3308 }
3309
Winson Chung3a682872017-04-10 14:38:05 -07003310 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003311 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003312 return true;
3313 }
3314
Winson Chung8bca9e42017-04-16 15:59:43 -07003315 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003316 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003317
Wale Ogunwale480dca02016-02-06 13:58:29 -08003318 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003319
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003320 final ActivityDisplay display = r.getStack().getDisplay();
3321 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003322
Bryce Lee04ab3462017-04-10 15:06:33 -07003323 // This will clear the pinned stack by moving an existing task to the full screen stack,
3324 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003325 if (stack != null) {
3326 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3327 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003328
Wale Ogunwale1666e312016-12-16 11:27:18 -08003329 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003330 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003331
Winson Chunge55c0192017-08-24 14:50:48 -07003332 // Calculate the target bounds here before the task is reparented back into pinned windowing
3333 // mode (which will reset the saved bounds)
3334 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3335
Wale Ogunwale480dca02016-02-06 13:58:29 -08003336 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003337 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003338 // Resize the pinned stack to match the current size of the task the activity we are
3339 // going to be moving is currently contained in. We do this to have the right starting
3340 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003341 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003342 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003343 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003344
3345 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003346 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003347 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3348 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003349 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003350 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003351 // reveal/leave the other activities in their original task.
3352
3353 // Currently, we don't support reparenting activities across tasks in two different
3354 // stacks, so instead, just create a new task in the same stack, reparent the
3355 // activity into that task, and then reparent the whole task to the new stack. This
3356 // ensures that all the necessary work to migrate states in the old and new stacks
3357 // is also done.
3358 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003359 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003360 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3361
Winson Chung5af42fc2017-03-24 17:11:33 -07003362 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003363 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003364 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003365 }
Winson Chungc2baac02017-01-11 13:34:47 -08003366
3367 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3368 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003369 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003370 } finally {
3371 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003372 }
3373
Winson Chunge7ba6862017-05-24 12:13:33 -07003374 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3375 true /* fromFullscreen */);
3376
3377 // Update the visibility of all activities after the they have been reparented to the new
3378 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3379 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3380 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003381 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003382 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003383
Wale Ogunwale89be5762017-10-04 13:27:49 -07003384 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003385 }
3386
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003387 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003388 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3389 if (r == null || !r.isFocusable()) {
3390 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3391 "moveActivityStackToFront: unfocusable r=" + r);
3392 return false;
3393 }
3394
Bryce Leeaf691c02017-03-20 14:20:22 -07003395 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003396 final ActivityStack stack = r.getStack();
3397 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003398 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3399 + r + " task=" + task);
3400 return false;
3401 }
3402
Chong Zhang6cda19c2016-06-14 19:07:56 -07003403 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3404 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3405 "moveActivityStackToFront: already on top, r=" + r);
3406 return false;
3407 }
3408
3409 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3410 "moveActivityStackToFront: r=" + r);
3411
3412 stack.moveToFront(reason, task);
3413 return true;
3414 }
3415
David Stevensc6b91c62017-02-08 14:23:58 -08003416 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003417 mTmpFindTaskResult.r = null;
3418 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003419 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003420 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003421 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003422 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3423 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3424 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003425 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003426 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3427 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003428 continue;
3429 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003430 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003431 // It is possible to have tasks in multiple stacks with the same root affinity, so
3432 // we should keep looking after finding an affinity match to see if there is a
3433 // better match in another stack. Also, task affinity isn't a good enough reason
3434 // to target a display which isn't the source of the intent, so skip any affinity
3435 // matches not on the specified display.
3436 if (mTmpFindTaskResult.r != null) {
3437 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3438 return mTmpFindTaskResult.r;
3439 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003440 // Note: since the traversing through the stacks is top down, the floating
3441 // tasks should always have lower priority than any affinity-matching tasks
3442 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003443 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003444 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3445 Slog.d(TAG_TASKS, "Skipping match on different display "
3446 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003447 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003448 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003449 }
3450 }
Winson Chung5b895b72017-05-01 13:46:25 -07003451
David Stevensc6b91c62017-02-08 14:23:58 -08003452 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3453 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003454 }
3455
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003456 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003457 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003458 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003459 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3460 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3461 final ActivityStack stack = display.getChildAt(stackNdx);
3462 final ActivityRecord ar = stack.findActivityLocked(
3463 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003464 if (ar != null) {
3465 return ar;
3466 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003467 }
3468 }
3469 return null;
3470 }
3471
David Stevens9440dc82017-03-16 19:00:20 -07003472 boolean hasAwakeDisplay() {
3473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3474 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3475 if (!display.shouldSleep()) {
3476 return true;
3477 }
3478 }
3479 return false;
3480 }
3481
Craig Mautner8d341ef2013-03-26 09:03:27 -07003482 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003483 scheduleSleepTimeout();
3484 if (!mGoingToSleep.isHeld()) {
3485 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003486 if (mLaunchingActivity.isHeld()) {
3487 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3488 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003489 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003490 mLaunchingActivity.release();
3491 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003492 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003493 }
David Stevens9440dc82017-03-16 19:00:20 -07003494
3495 applySleepTokensLocked(false /* applyToStacks */);
3496
3497 checkReadyForSleepLocked(true /* allowDelay */);
3498 }
3499
3500 void prepareForShutdownLocked() {
3501 for (int i = 0; i < mActivityDisplays.size(); i++) {
3502 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3503 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003504 }
3505
3506 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003507 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003508
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003509 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003510 final long endTime = System.currentTimeMillis() + timeout;
3511 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003512 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 long timeRemaining = endTime - System.currentTimeMillis();
3514 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003515 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003516 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 } catch (InterruptedException e) {
3518 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003519 } else {
3520 Slog.w(TAG, "Activity manager shutdown timed out");
3521 timedout = true;
3522 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003523 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003524 } else {
3525 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003526 }
3527 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003528
3529 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003530 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531
Craig Mautner8d341ef2013-03-26 09:03:27 -07003532 return timedout;
3533 }
3534
3535 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003536 removeSleepTimeouts();
3537 if (mGoingToSleep.isHeld()) {
3538 mGoingToSleep.release();
3539 }
David Stevens9440dc82017-03-16 19:00:20 -07003540 }
3541
3542 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003544 // Set the sleeping state of the display.
3545 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3546 final boolean displayShouldSleep = display.shouldSleep();
3547 if (displayShouldSleep == display.isSleeping()) {
3548 continue;
3549 }
3550 display.setIsSleeping(displayShouldSleep);
3551
3552 if (!applyToStacks) {
3553 continue;
3554 }
3555
3556 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003557 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3558 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003559 if (displayShouldSleep) {
3560 stack.goToSleepIfPossible(false /* shuttingDown */);
3561 } else {
3562 stack.awakeFromSleepingLocked();
Lucas Dupin47a65c72018-02-15 14:16:18 -08003563 if (isFocusedStack(stack) && !getKeyguardController().isKeyguardOrAodShowing(
3564 display.mDisplayId)) {
Bryce Leed939cf02018-03-12 09:04:44 -07003565 // If the keyguard is unlocked - resume immediately.
3566 // It is possible that the display will not be awake at the time we
3567 // process the keyguard going away, which can happen before the sleep token
3568 // is released. As a result, it is important we resume the activity here.
David Stevens9440dc82017-03-16 19:00:20 -07003569 resumeFocusedStackTopActivityLocked();
3570 }
3571 }
3572 }
3573
3574 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3575 continue;
3576 }
3577 // The display is awake now, so clean up the going to sleep list.
3578 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3579 final ActivityRecord r = it.next();
3580 if (r.getDisplayId() == display.mDisplayId) {
3581 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003582 }
Craig Mautner5314a402013-09-26 12:40:16 -07003583 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003584 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003585 }
3586
3587 void activitySleptLocked(ActivityRecord r) {
3588 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003589 final ActivityStack s = r.getStack();
3590 if (s != null) {
3591 s.checkReadyForSleep();
3592 } else {
3593 checkReadyForSleepLocked(true);
3594 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003595 }
3596
David Stevens9440dc82017-03-16 19:00:20 -07003597 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003598 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003599 // Do not care.
3600 return;
3601 }
3602
David Stevens18abd0e2017-08-17 14:55:47 -07003603 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3604 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003605 }
3606
Wei Wang65c7a152016-06-02 18:51:22 -07003607 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003608 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003609
Craig Mautner0eea92c2013-05-16 13:35:39 -07003610 removeSleepTimeouts();
3611
3612 if (mGoingToSleep.isHeld()) {
3613 mGoingToSleep.release();
3614 }
3615 if (mService.mShuttingDown) {
3616 mService.notifyAll();
3617 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003618 }
3619
David Stevens18abd0e2017-08-17 14:55:47 -07003620 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3621 // successfully put to sleep.
3622 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3623 boolean allSleep = true;
3624 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003625 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3626 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3627 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003628 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003629 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003630 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003631 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003632 }
3633 }
3634 }
3635 return allSleep;
3636 }
3637
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003638 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003639 // A resumed activity cannot be stopping. remove from list
3640 mStoppingActivities.remove(r);
3641
Andrii Kulian02b7a832016-10-06 23:11:56 -07003642 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003643 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003644 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003645 }
3646 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003647 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003648 mWindowManager.executeAppTransition();
3649 return true;
3650 }
3651 return false;
3652 }
3653
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003656 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3657 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3658 final ActivityStack stack = display.getChildAt(stackNdx);
3659 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003660 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003661 }
3662 }
3663
Craig Mautnerbb742462014-07-07 15:28:55 -07003664 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003665 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003666 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003667 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003668
3669 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003670 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003671 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003672 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003673
3674 // When launching tasks behind, update the last active time of the top task after the new
3675 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003676 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003677 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003678 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003679 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003680 }
3681
3682 void scheduleLaunchTaskBehindComplete(IBinder token) {
3683 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3684 }
3685
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003686 /**
3687 * Make sure that all activities that need to be visible in the system actually are and update
3688 * their configuration.
3689 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003690 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3691 boolean preserveWindows) {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003692 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003693 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003694 }
3695
3696 /**
3697 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
3698 */
3699 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003700 boolean preserveWindows, boolean notifyClients) {
Bryce Lee459c0622018-03-19 11:04:01 -07003701 getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02003702 try {
3703 // First the front stacks. In case any are not fullscreen and are in front of home.
3704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003705 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3706 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3707 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003708 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003709 notifyClients);
Jorim Jaggife762342016-10-13 14:33:27 +02003710 }
Craig Mautner580ea812013-04-25 12:58:38 -07003711 }
Jorim Jaggife762342016-10-13 14:33:27 +02003712 } finally {
Bryce Lee459c0622018-03-19 11:04:01 -07003713 getKeyguardController().endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003714 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003715 }
3716
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003717 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003719 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3720 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3721 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003722 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3723 }
3724 }
3725 }
3726
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003727 void invalidateTaskLayers() {
3728 mTaskLayersChanged = true;
3729 }
3730
3731 void rankTaskLayersIfNeeded() {
3732 if (!mTaskLayersChanged) {
3733 return;
3734 }
3735 mTaskLayersChanged = false;
3736 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003737 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003738 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003739 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3740 final ActivityStack stack = display.getChildAt(stackNdx);
3741 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003742 }
3743 }
3744 }
3745
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003746 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003748 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3749 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3750 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003751 stack.clearOtherAppTimeTrackers(except);
3752 }
3753 }
3754 }
3755
Craig Mautner8d341ef2013-03-26 09:03:27 -07003756 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003757 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003758 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3759 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3760 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003761 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003763 }
3764 }
3765
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003766 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3767 // Examine all activities currently running in the process.
3768 TaskRecord firstTask = null;
3769 // Tasks is non-null only if two or more tasks are found.
3770 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003771 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3772 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003773 ActivityRecord r = app.activities.get(i);
3774 // First, if we find an activity that is in the process of being destroyed,
3775 // then we just aren't going to do anything for now; we want things to settle
3776 // down before we try to prune more activities.
Bryce Lee7ace3952018-02-16 14:34:32 -08003777 if (r.finishing || r.isState(DESTROYING, DESTROYED)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003778 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003779 return;
3780 }
3781 // Don't consider any activies that are currently not in a state where they
3782 // can be destroyed.
Bryce Lee7ace3952018-02-16 14:34:32 -08003783 if (r.visible || !r.stopped || !r.haveState
3784 || r.isState(RESUMED, PAUSING, PAUSED, STOPPING)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003785 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003786 continue;
3787 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003788
3789 final TaskRecord task = r.getTask();
3790 if (task != null) {
3791 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003792 + " from " + r);
3793 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003794 firstTask = task;
3795 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003796 if (tasks == null) {
3797 tasks = new ArraySet<>();
3798 tasks.add(firstTask);
3799 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003800 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003801 }
3802 }
3803 }
3804 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003805 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003806 return;
3807 }
3808 // If we have activities in multiple tasks that are in a position to be destroyed,
3809 // let's iterate through the tasks and release the oldest one.
3810 final int numDisplays = mActivityDisplays.size();
3811 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003812 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3813 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003814 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003815 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3816 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003817 // Try to release activities in this stack; if we manage to, we are done.
3818 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3819 return;
3820 }
3821 }
3822 }
3823 }
3824
Amith Yamasani37a40c22015-06-17 13:25:42 -07003825 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003826 final int focusStackId = mFocusedStack.getStackId();
3827 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003828 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003829 if (dockedStack != null) {
3830 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3831 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003832 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3833 // also cause all tasks to be moved to the fullscreen stack at a position that is
3834 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003835 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003836
3837 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003838 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003839 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003840
Craig Mautner858d8a62013-04-23 17:08:34 -07003841 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003843 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3844 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3845 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003846 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003847 TaskRecord task = stack.topTask();
3848 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003849 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003850 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003851 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003852 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003853
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003854 ActivityStack stack = getStack(restoreStackId);
3855 if (stack == null) {
3856 stack = mHomeStack;
3857 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003858 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003860 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003861 } else {
3862 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003863 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003864 }
Craig Mautner93529a42013-10-04 15:03:13 -07003865 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003866 }
3867
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003868 /** Checks whether the userid is a profile of the current user. */
3869 boolean isCurrentProfileLocked(int userId) {
3870 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003871 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003872 }
3873
Bryce Leeb7c9b802017-05-02 14:20:24 -07003874 /**
3875 * Returns whether a stopping activity is present that should be stopped after visible, rather
3876 * than idle.
3877 * @return {@code true} if such activity is present. {@code false} otherwise.
3878 */
3879 boolean isStoppingNoHistoryActivity() {
3880 // Activities that are marked as nohistory should be stopped immediately after the resumed
3881 // activity has become visible.
3882 for (ActivityRecord record : mStoppingActivities) {
3883 if (record.isNoHistory()) {
3884 return true;
3885 }
3886 }
3887
3888 return false;
3889 }
3890
Winson Chung4dabf232017-01-25 13:25:22 -08003891 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3892 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003893 ArrayList<ActivityRecord> stops = null;
3894
3895 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003896 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3897 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003898 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003899 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003900 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3901 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003902 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003903 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003904 if (s.finishing) {
3905 // If this activity is finishing, it is sitting on top of
3906 // everyone else but we now know it is no longer needed...
3907 // so get rid of it. Otherwise, we need to go through the
3908 // normal flow and hide it once we determine that it is
3909 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003910 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003911 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003912 }
3913 }
David Stevens9440dc82017-03-16 19:00:20 -07003914 if (remove) {
3915 final ActivityStack stack = s.getStack();
3916 final boolean shouldSleepOrShutDown = stack != null
3917 ? stack.shouldSleepOrShutDownActivities()
3918 : mService.isSleepingOrShuttingDownLocked();
3919 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08003920 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07003921 // Defer processing pausing activities in this iteration and reschedule
3922 // a delayed idle to reprocess it again
3923 removeTimeoutsForActivityLocked(idleActivity);
3924 scheduleIdleTimeoutLocked(idleActivity);
3925 continue;
3926 }
Winson Chung4dabf232017-01-25 13:25:22 -08003927
David Stevens9440dc82017-03-16 19:00:20 -07003928 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3929 if (stops == null) {
3930 stops = new ArrayList<>();
3931 }
3932 stops.add(s);
3933 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003934 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003935 }
3936 }
3937
3938 return stops;
3939 }
3940
Craig Mautnercf910b02013-04-23 11:23:27 -07003941 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003942 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003943 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3944 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3945 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003946 final ActivityRecord r = stack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08003947 final ActivityState state = r == null ? DESTROYED : r.getState();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003948 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003949 if (r == null) Slog.e(TAG,
3950 "validateTop...: null top activity, stack=" + stack);
3951 else {
3952 final ActivityRecord pausing = stack.mPausingActivity;
3953 if (pausing != null && pausing == r) Slog.e(TAG,
3954 "validateTop...: top stack has pausing activity r=" + r
3955 + " state=" + state);
3956 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3957 "validateTop...: activity in front not resumed r=" + r
3958 + " state=" + state);
3959 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003960 } else {
Bryce Leec4ab62a2018-03-05 14:19:26 -08003961 final ActivityRecord resumed = stack.getResumedActivity();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003962 if (resumed != null && resumed == r) Slog.e(TAG,
3963 "validateTop...: back stack has resumed activity r=" + r
3964 + " state=" + state);
3965 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3966 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003967 }
3968 }
3969 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003970 }
3971
Bryce Lee77a7dd62018-01-22 15:47:09 -08003972 public void dumpDisplays(PrintWriter pw) {
3973 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3974 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3975 pw.print("[id:" + display.mDisplayId + " stacks:");
3976 display.dumpStacks(pw);
3977 pw.print("]");
3978 }
3979 }
3980
Craig Mautner27084302013-03-25 08:05:25 -07003981 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003982 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003983 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003984 pw.print(prefix);
3985 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003986 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003987 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3988 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003989 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003990 }
Bryce Lee4a194382017-04-04 14:32:48 -07003991 if (!mWaitingForActivityVisible.isEmpty()) {
3992 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3993 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3994 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3995 }
3996 }
Winson Chungc1674272018-02-21 10:15:17 -08003997 pw.print(prefix); pw.print("isHomeRecentsComponent=");
3998 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07003999
Bryce Lee459c0622018-03-19 11:04:01 -07004000 getKeyguardController().dump(pw, prefix);
Bryce Lee2b8e0372018-04-05 17:01:37 -07004001 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07004002 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004003
Yi Jin2b30f322018-02-20 15:41:47 -08004004 public void writeToProto(ProtoOutputStream proto, long fieldId) {
4005 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02004006 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004007 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4008 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
4009 activityDisplay.writeToProto(proto, DISPLAYS);
4010 }
Bryce Lee459c0622018-03-19 11:04:01 -07004011 getKeyguardController().writeToProto(proto, KEYGUARD_CONTROLLER);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004012 if (mFocusedStack != null) {
4013 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
4014 ActivityRecord focusedActivity = getResumedActivityLocked();
4015 if (focusedActivity != null) {
4016 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
4017 }
4018 } else {
4019 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
4020 }
Winson Chungc1674272018-02-21 10:15:17 -08004021 proto.write(IS_HOME_RECENTS_COMPONENT,
4022 mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Yi Jin2b30f322018-02-20 15:41:47 -08004023 proto.end(token);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004024 }
4025
Winson43d1f262016-06-14 16:05:55 -07004026 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07004027 * Dump all connected displays' configurations.
4028 * @param prefix Prefix to apply to each line of the dump.
4029 */
4030 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
4031 pw.print(prefix); pw.println("Display override configurations:");
4032 final int displayCount = mActivityDisplays.size();
4033 for (int i = 0; i < displayCount; i++) {
4034 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
4035 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
4036 pw.println(activityDisplay.getOverrideConfiguration());
4037 }
4038 }
4039
4040 /**
Winson43d1f262016-06-14 16:05:55 -07004041 * Dumps the activities matching the given {@param name} in the either the focused stack
4042 * or all visible stacks if {@param dumpVisibleStacks} is true.
4043 */
Winson Chung6998bc42017-02-28 17:07:05 -08004044 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
4045 boolean dumpFocusedStackOnly) {
4046 if (dumpFocusedStackOnly) {
4047 return mFocusedStack.getDumpActivitiesLocked(name);
4048 } else {
Winson43d1f262016-06-14 16:05:55 -07004049 ArrayList<ActivityRecord> activities = new ArrayList<>();
4050 int numDisplays = mActivityDisplays.size();
4051 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004052 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4053 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4054 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004055 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07004056 activities.addAll(stack.getDumpActivitiesLocked(name));
4057 }
4058 }
4059 }
4060 return activities;
Winson43d1f262016-06-14 16:05:55 -07004061 }
Craig Mautner20e72272013-04-01 13:45:53 -07004062 }
4063
Dianne Hackborn390517b2013-05-30 15:03:32 -07004064 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4065 boolean needSep, String prefix) {
4066 if (activity != null) {
4067 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4068 if (needSep) {
4069 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004070 }
4071 pw.print(prefix);
4072 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004073 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004074 }
4075 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004076 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004077 }
4078
Craig Mautner8d341ef2013-03-26 09:03:27 -07004079 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4080 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004081 boolean printed = false;
4082 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004083 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4084 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004085 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004086 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004087 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4088 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4089 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004090 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004091 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07004092 + ": type=" + activityTypeToString(stack.getActivityType())
4093 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004094 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08004095 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07004096
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004097 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4098 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07004099
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4101 !dumpAll, false, dumpPackage, true,
4102 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004103
Craig Mautner4a1cb222013-12-04 16:14:06 -08004104 needSep = printed;
4105 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4106 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004107 if (pr) {
4108 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004110 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08004111 pr = printThisActivity(pw, stack.getResumedActivity(), dumpPackage, needSep,
Craig Mautner4a1cb222013-12-04 16:14:06 -08004112 " mResumedActivity: ");
4113 if (pr) {
4114 printed = true;
4115 needSep = false;
4116 }
4117 if (dumpAll) {
4118 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4119 " mLastPausedActivity: ");
4120 if (pr) {
4121 printed = true;
4122 needSep = true;
4123 }
4124 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4125 needSep, " mLastNoHistoryActivity: ");
4126 }
4127 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004128 }
4129 }
4130
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004131 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4132 false, dumpPackage, true, " Activities waiting to finish:", null);
4133 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4134 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07004135 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
4136 false, !dumpAll, false, dumpPackage, true,
4137 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004138 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4139 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004140
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004141 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004142 }
4143
Dianne Hackborn390517b2013-05-30 15:03:32 -07004144 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004145 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004146 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004147 String innerPrefix = null;
4148 String[] args = null;
4149 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004150 for (int i=list.size()-1; i>=0; i--) {
4151 final ActivityRecord r = list.get(i);
4152 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4153 continue;
4154 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004155 if (innerPrefix == null) {
4156 innerPrefix = prefix + " ";
4157 args = new String[0];
4158 }
4159 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004160 final boolean full = !brief && (complete || !r.isInHistory());
4161 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004162 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004163 needNL = false;
4164 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004165 if (header != null) {
4166 pw.println(header);
4167 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004168 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004169 if (lastTask != r.getTask()) {
4170 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07004171 pw.print(prefix);
4172 pw.print(full ? "* " : " ");
4173 pw.println(lastTask);
4174 if (full) {
4175 lastTask.dump(pw, prefix + " ");
4176 } else if (complete) {
4177 // Complete + brief == give a summary. Isn't that obvious?!?
4178 if (lastTask.intent != null) {
4179 pw.print(prefix); pw.print(" ");
4180 pw.println(lastTask.intent.toInsecureStringWithClip());
4181 }
4182 }
4183 }
4184 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4185 pw.print(" #"); pw.print(i); pw.print(": ");
4186 pw.println(r);
4187 if (full) {
4188 r.dump(pw, innerPrefix);
4189 } else if (complete) {
4190 // Complete + brief == give a summary. Isn't that obvious?!?
4191 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4192 if (r.app != null) {
4193 pw.print(innerPrefix); pw.println(r.app);
4194 }
4195 }
4196 if (client && r.app != null && r.app.thread != null) {
4197 // flush anything that is already in the PrintWriter since the thread is going
4198 // to write to the file descriptor directly
4199 pw.flush();
4200 try {
4201 TransferPipe tp = new TransferPipe();
4202 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004203 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004204 // Short timeout, since blocking here can
4205 // deadlock with the application.
4206 tp.go(fd, 2000);
4207 } finally {
4208 tp.kill();
4209 }
4210 } catch (IOException e) {
4211 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4212 } catch (RemoteException e) {
4213 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4214 }
4215 needNL = true;
4216 }
4217 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004218 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004219 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004220
Craig Mautnerf3333272013-04-22 10:55:53 -07004221 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004222 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4223 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004224 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4225 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004226 }
4227
4228 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004229 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004230 }
4231
4232 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004233 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4234 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004235 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4236 }
4237
Craig Mautner05d29032013-05-03 13:40:13 -07004238 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004239 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4240 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4241 }
Craig Mautner05d29032013-05-03 13:40:13 -07004242 }
4243
Craig Mautner0eea92c2013-05-16 13:35:39 -07004244 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004245 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4246 }
4247
4248 final void scheduleSleepTimeout() {
4249 removeSleepTimeouts();
4250 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4251 }
4252
Craig Mautner4a1cb222013-12-04 16:14:06 -08004253 @Override
4254 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004255 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4257 }
4258
4259 @Override
4260 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004261 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004262 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4263 }
4264
4265 @Override
4266 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004267 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4269 }
4270
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004271 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004272 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004273 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004275 }
4276
Andrii Kulianca007a62016-11-22 16:33:28 -08004277 /** Check if display with specified id is added to the list. */
4278 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004279 return getActivityDisplayOrCreateLocked(displayId) != null;
4280 }
4281
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004282 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004283 ActivityDisplay getActivityDisplay(int displayId) {
4284 return mActivityDisplays.get(displayId);
4285 }
4286
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004287 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4288 ActivityDisplay getDefaultDisplay() {
4289 return mActivityDisplays.get(DEFAULT_DISPLAY);
4290 }
4291
Andrii Kulian62e6f252017-05-30 22:46:53 -07004292 /**
4293 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4294 * corresponding record in display manager.
4295 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004296 // TODO: Look into consolidating with getActivityDisplay()
4297 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004298 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4299 if (activityDisplay != null) {
4300 return activityDisplay;
4301 }
4302 if (mDisplayManager == null) {
4303 // The system isn't fully initialized yet.
4304 return null;
4305 }
4306 final Display display = mDisplayManager.getDisplay(displayId);
4307 if (display == null) {
4308 // The display is not registered in DisplayManager.
4309 return null;
4310 }
4311 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale45477b52018-03-06 12:24:19 -08004312 activityDisplay = new ActivityDisplay(this, display);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004313 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004314 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4315 mWindowManager.onDisplayAdded(displayId);
4316 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004317 }
4318
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004319 @VisibleForTesting
4320 void attachDisplay(ActivityDisplay display) {
4321 mActivityDisplays.put(display.mDisplayId, display);
4322 }
4323
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004324 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004325 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004326 mService.mContext.getResources().getDimensionPixelSize(
4327 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004328 }
4329
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004330 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004331 if (displayId == DEFAULT_DISPLAY) {
4332 throw new IllegalArgumentException("Can't remove the primary display.");
4333 }
4334
Craig Mautner4a1cb222013-12-04 16:14:06 -08004335 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004336 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4337 if (activityDisplay == null) {
4338 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339 }
Bryce Leef19cbe22018-02-02 15:09:21 -08004340
4341 activityDisplay.remove();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004342
4343 releaseSleepTokens(activityDisplay);
4344
4345 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004346 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 }
4348
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004349 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004351 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004352 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004353 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004354 // The window policy is responsible for stopping activities on the default display
4355 if (displayId != Display.DEFAULT_DISPLAY) {
4356 int displayState = activityDisplay.mDisplay.getState();
4357 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4358 activityDisplay.mOffToken =
4359 mService.acquireSleepToken("Display-off", displayId);
4360 } else if (displayState == Display.STATE_ON
4361 && activityDisplay.mOffToken != null) {
4362 activityDisplay.mOffToken.release();
4363 activityDisplay.mOffToken = null;
4364 }
4365 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004366
4367 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004369 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004370 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004371 }
4372
David Stevens9440dc82017-03-16 19:00:20 -07004373 SleepToken createSleepTokenLocked(String tag, int displayId) {
4374 ActivityDisplay display = mActivityDisplays.get(displayId);
4375 if (display == null) {
4376 throw new IllegalArgumentException("Invalid display: " + displayId);
4377 }
4378
4379 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4380 mSleepTokens.add(token);
4381 display.mAllSleepTokens.add(token);
4382 return token;
4383 }
4384
4385 private void removeSleepTokenLocked(SleepTokenImpl token) {
4386 mSleepTokens.remove(token);
4387
4388 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4389 if (display != null) {
4390 display.mAllSleepTokens.remove(token);
4391 if (display.mAllSleepTokens.isEmpty()) {
4392 mService.updateSleepIfNeededLocked();
4393 }
4394 }
4395 }
4396
4397 private void releaseSleepTokens(ActivityDisplay display) {
4398 if (display.mAllSleepTokens.isEmpty()) {
4399 return;
4400 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004401 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004402 mSleepTokens.remove(token);
4403 }
4404 display.mAllSleepTokens.clear();
4405
4406 mService.updateSleepIfNeededLocked();
4407 }
4408
Wale Ogunwale68278562017-09-23 17:13:55 -07004409 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004410 final int displayId = stack.mDisplayId;
4411 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004412 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004413 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004414 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004415 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004416 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004417 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004418 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004419 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004420 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004421
4422 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4423 final int numTasks = tasks.size();
4424 int[] taskIds = new int[numTasks];
4425 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004426 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004427 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004428 for (int i = 0; i < numTasks; ++i) {
4429 final TaskRecord task = tasks.get(i);
4430 taskIds[i] = task.taskId;
4431 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4432 : task.realActivity != null ? task.realActivity.flattenToString()
4433 : task.getTopActivity() != null ? task.getTopActivity().packageName
4434 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004435 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004436 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004437 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004438 }
4439 info.taskIds = taskIds;
4440 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004441 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004442 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004443
4444 final ActivityRecord top = stack.topRunningActivityLocked();
4445 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004446 return info;
4447 }
4448
Wale Ogunwale68278562017-09-23 17:13:55 -07004449 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004450 ActivityStack stack = getStack(stackId);
4451 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004452 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004453 }
4454 return null;
4455 }
4456
Wale Ogunwale68278562017-09-23 17:13:55 -07004457 StackInfo getStackInfo(int windowingMode, int activityType) {
4458 final ActivityStack stack = getStack(windowingMode, activityType);
4459 return (stack != null) ? getStackInfo(stack) : null;
4460 }
4461
Craig Mautner4a1cb222013-12-04 16:14:06 -08004462 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004463 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004464 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004465 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4466 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4467 final ActivityStack stack = display.getChildAt(stackNdx);
4468 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004469 }
4470 }
4471 return list;
4472 }
4473
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004474 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004475 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004476 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004477 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004478 }
4479
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004480 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004481 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004482 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004483 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004484 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004485 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004486 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004487 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004488 return;
4489 }
4490
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004491 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004492 if (isSecondaryDisplayPreferred) {
4493 final int actualDisplayId = task.getStack().mDisplayId;
4494 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004495 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004496 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004497 // The task might have landed on a display different from requested.
4498 // TODO(multi-display): Find proper stack for the task on the default display.
4499 mService.setTaskWindowingMode(task.taskId,
4500 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4501 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004502 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4503 // display with config different from global config.
4504 mService.mTaskChangeNotificationController
4505 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004506 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004507 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004508 }
4509
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004510 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004511 // Display a warning toast that we tried to put an app that doesn't support split-screen
4512 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004513 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004514
Andrii Kulianc27916642016-04-12 17:59:27 -07004515 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4516 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004517
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004518 final ActivityStack dockedStack =
4519 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004520 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004521 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004522 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004523 return;
4524 }
4525
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004526 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004527 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4528 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004529 final String packageName = topActivity.appInfo.packageName;
4530 final int reason = isSecondaryDisplayPreferred
4531 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4532 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004533 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004534 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004535 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004536 }
4537
Jorim Jaggife89d122015-12-22 16:28:44 +01004538 void activityRelaunchedLocked(IBinder token) {
4539 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004540 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4541 if (r != null) {
4542 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004543 r.setSleeping(true, true);
4544 }
4545 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004546 }
4547
4548 void activityRelaunchingLocked(ActivityRecord r) {
4549 mWindowManager.notifyAppRelaunching(r.appToken);
4550 }
4551
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004552 void logStackState() {
4553 mActivityMetricsLogger.logWindowState();
4554 }
4555
Winson Chung5af42fc2017-03-24 17:11:33 -07004556 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004557 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4558 // end, then ensure that we defer all in between multi-window mode changes
4559 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4560 return;
4561 }
4562
Wale Ogunwale22e25262016-02-01 10:32:02 -08004563 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4564 final ActivityRecord r = task.mActivities.get(i);
4565 if (r.app != null && r.app.thread != null) {
4566 mMultiWindowModeChangedActivities.add(r);
4567 }
4568 }
4569
4570 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4571 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4572 }
4573 }
4574
Winson Chung5af42fc2017-03-24 17:11:33 -07004575 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004576 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004577 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004578 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004579 return;
4580 }
4581
Bryce Leef3c6a472017-11-14 14:53:06 -08004582 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004583 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004584
Winson Chungab76bbc2017-08-14 13:33:51 -07004585 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4586 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4587 final ActivityRecord r = task.mActivities.get(i);
4588 if (r.app != null && r.app.thread != null) {
4589 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004590 // If we are scheduling pip change, then remove this activity from multi-window
4591 // change list as the processing of pip change will make sure multi-window changed
4592 // message is processed in the right order relative to pip changed.
4593 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004594 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004595 }
4596 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004597
Winson Chungab76bbc2017-08-14 13:33:51 -07004598 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4599 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4600 }
4601 }
4602
4603 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4604 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4605 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4606 final ActivityRecord r = task.mActivities.get(i);
4607 if (r.app != null && r.app.thread != null) {
4608 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004609 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004610 }
4611 }
4612
Tony Mak853304c2016-04-18 15:17:41 +01004613 void setDockedStackMinimized(boolean minimized) {
4614 mIsDockMinimized = minimized;
Wale Ogunwale17696de2018-03-15 16:41:05 -07004615 if (mIsDockMinimized) {
4616 final ActivityStack current = getFocusedStack();
4617 if (current.inSplitScreenPrimaryWindowingMode()) {
4618 // The primary split-screen stack can't be focused while it is minimize, so move
4619 // focus to something else.
4620 current.adjustFocusToNextFocusableStack("setDockedStackMinimized");
4621 }
4622 }
Tony Mak853304c2016-04-18 15:17:41 +01004623 }
4624
chaviw59b98852017-06-13 12:05:44 -07004625 void wakeUp(String reason) {
4626 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4627 }
4628
4629 /**
4630 * Begin deferring resume to avoid duplicate resumes in one pass.
4631 */
4632 private void beginDeferResume() {
4633 mDeferResumeCount++;
4634 }
4635
4636 /**
4637 * End deferring resume and determine if resume can be called.
4638 */
4639 private void endDeferResume() {
4640 mDeferResumeCount--;
4641 }
4642
4643 /**
4644 * @return True if resume can be called.
4645 */
4646 private boolean readyToResume() {
4647 return mDeferResumeCount == 0;
4648 }
4649
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004650 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004651
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004652 public ActivityStackSupervisorHandler(Looper looper) {
4653 super(looper);
4654 }
4655
Winson Chung4dabf232017-01-25 13:25:22 -08004656 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004657 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004658 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4659 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004660 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004661 }
4662
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004663 @Override
4664 public void handleMessage(Message msg) {
4665 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004666 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4667 synchronized (mService) {
4668 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4669 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004670 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004671 }
4672 }
4673 } break;
4674 case REPORT_PIP_MODE_CHANGED_MSG: {
4675 synchronized (mService) {
4676 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4677 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004678 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4679 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004680 }
4681 }
4682 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004683 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004684 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4685 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004686 // We don't at this point know if the activity is fullscreen,
4687 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004688 activityIdleInternal((ActivityRecord) msg.obj,
4689 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004690 } break;
4691 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004692 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004693 activityIdleInternal((ActivityRecord) msg.obj,
4694 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004695 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004696 case RESUME_TOP_ACTIVITY_MSG: {
4697 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004698 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004699 }
4700 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004701 case SLEEP_TIMEOUT_MSG: {
4702 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004703 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004704 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004705 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004706 }
4707 }
4708 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004709 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004710 synchronized (mService) {
4711 if (mLaunchingActivity.isHeld()) {
4712 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4713 if (VALIDATE_WAKE_LOCK_CALLER
4714 && Binder.getCallingUid() != Process.myUid()) {
4715 throw new IllegalStateException("Calling must be system uid");
4716 }
4717 mLaunchingActivity.release();
4718 }
4719 }
4720 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004721 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004722 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004723 } break;
4724 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004725 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004726 } break;
4727 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004728 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004729 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004730 case LAUNCH_TASK_BEHIND_COMPLETE: {
4731 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004732 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004733 if (r != null) {
4734 handleLaunchTaskBehindCompleteLocked(r);
4735 }
4736 }
4737 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004738
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004739 }
4740 }
4741 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004742
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004743 ActivityStack findStackBehind(ActivityStack stack) {
4744 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004745 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004746 if (display == null) {
4747 return null;
4748 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004749 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4750 if (display.getChildAt(i) == stack && i > 0) {
4751 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004752 }
4753 }
4754 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004755 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004756 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004757
Jorim Jaggic69bd222016-03-15 14:38:37 +01004758 /**
4759 * Puts a task into resizing mode during the next app transition.
4760 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004761 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004762 */
Bryce Leed3624e12017-11-30 08:51:45 -08004763 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004764 mResizingTasksDuringAnimation.add(task.taskId);
4765 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004766 }
4767
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004768 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4769 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004770 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004771 final String callingPackage;
4772 final Intent intent;
4773 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004774 int activityType = ACTIVITY_TYPE_UNDEFINED;
4775 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004776 final ActivityOptions activityOptions = options != null
4777 ? options.getOptions(this)
4778 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004779 if (activityOptions != null) {
4780 activityType = activityOptions.getLaunchActivityType();
4781 windowingMode = activityOptions.getLaunchWindowingMode();
4782 }
4783 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004784 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004785 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004786 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004787
Matthew Ng606dd802017-06-05 14:06:32 -07004788 mWindowManager.deferSurfaceLayout();
4789 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004790 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004791 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004792 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004793
Matthew Ng606dd802017-06-05 14:06:32 -07004794 // Defer updating the stack in which recents is until the app transition is done, to
4795 // not run into issues where we still need to draw the task in recents but the
4796 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08004797 deferUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004798 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004799 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004800
Matthew Ng606dd802017-06-05 14:06:32 -07004801 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004802 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004803 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08004804 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004805 mWindowManager.executeAppTransition();
4806 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004807 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004808 }
4809
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004810 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4811 // We always want to return to the home activity instead of the recents activity
4812 // from whatever is started from the recents activity, so move the home stack
4813 // forward.
4814 moveHomeStackToFront("startActivityFromRecents");
4815 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004816
Matthew Ng606dd802017-06-05 14:06:32 -07004817 // If the user must confirm credentials (e.g. when first launching a work app and the
4818 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4819 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4820 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004821 final ActivityRecord targetActivity = task.getTopActivity();
4822
Bryce Leed3624e12017-11-30 08:51:45 -08004823 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004824 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004825 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004826 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004827 true /* fromRecents */);
4828 } finally {
4829 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4830 targetActivity);
4831 }
Matthew Ng606dd802017-06-05 14:06:32 -07004832
Bryce Leed3624e12017-11-30 08:51:45 -08004833 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4834 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4835 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004836 return ActivityManager.START_TASK_TO_FRONT;
4837 }
Matthew Ng606dd802017-06-05 14:06:32 -07004838 callingPackage = task.mCallingPackage;
4839 intent = task.intent;
4840 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4841 userId = task.userId;
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004842 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004843 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07004844 null, 0, 0, options, userId, task, "startActivityFromRecents",
4845 false /* validateIncomingUser */);
Matthew Ng606dd802017-06-05 14:06:32 -07004846 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004847 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4848 // If we are launching the task in the docked stack, put it into resizing mode so
4849 // the window renders full-screen with the background filling the void. Also only
4850 // call this at the end to make sure that tasks exists on the window manager side.
4851 setResizingDuringAnimation(task);
4852
4853 final ActivityDisplay display = task.getStack().getDisplay();
4854 final ActivityStack topSecondaryStack =
4855 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4856 if (topSecondaryStack.isActivityTypeHome()) {
4857 // If the home activity if the top split-screen secondary stack, then the
4858 // primary split-screen stack is in the minimized mode which means it can't
4859 // receive input keys, so we should move the focused app to the home app so that
4860 // window manager can correctly calculate the focus window that can receive
4861 // input keys.
4862 moveHomeStackToFront("startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08004863
4864 // Immediately update the minimized docked stack mode, the upcoming animation
4865 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
4866 // will do the animation to the target bounds
4867 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004868 }
4869 }
Matthew Ng606dd802017-06-05 14:06:32 -07004870 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004871 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004872 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004873
4874 /**
4875 * @return a list of activities which are the top ones in each visible stack. The first
4876 * entry will be the focused activity.
4877 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004878 List<IBinder> getTopVisibleActivities() {
4879 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4880 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004881 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004882 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4883 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004884 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4885 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004886 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004887 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004888 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004889 if (top != null) {
4890 if (stack == mFocusedStack) {
4891 topActivityTokens.add(0, top.appToken);
4892 } else {
4893 topActivityTokens.add(top.appToken);
4894 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004895 }
4896 }
4897 }
4898 }
4899 return topActivityTokens;
4900 }
Bryce Lee4a194382017-04-04 14:32:48 -07004901
4902 /**
4903 * Internal container to store a match qualifier alongside a WaitResult.
4904 */
4905 static class WaitInfo {
4906 private final ComponentName mTargetComponent;
4907 private final WaitResult mResult;
4908
4909 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4910 this.mTargetComponent = targetComponent;
4911 this.mResult = result;
4912 }
4913
Wale Ogunwale3270f172017-04-26 07:29:42 -07004914 public boolean matches(ComponentName targetComponent) {
4915 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004916 }
4917
4918 public WaitResult getResult() {
4919 return mResult;
4920 }
4921
4922 public ComponentName getComponent() {
4923 return mTargetComponent;
4924 }
4925
4926 public void dump(PrintWriter pw, String prefix) {
4927 pw.println(prefix + "WaitInfo:");
4928 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4929 pw.println(prefix + " mResult=");
4930 mResult.dump(pw, prefix);
4931 }
4932 }
David Stevens9440dc82017-03-16 19:00:20 -07004933
4934 private final class SleepTokenImpl extends SleepToken {
4935 private final String mTag;
4936 private final long mAcquireTime;
4937 private final int mDisplayId;
4938
4939 public SleepTokenImpl(String tag, int displayId) {
4940 mTag = tag;
4941 mDisplayId = displayId;
4942 mAcquireTime = SystemClock.uptimeMillis();
4943 }
4944
4945 @Override
4946 public void release() {
4947 synchronized (mService) {
4948 removeSleepTokenLocked(this);
4949 }
4950 }
4951
4952 @Override
4953 public String toString() {
4954 return "{\"" + mTag + "\", display " + mDisplayId
4955 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4956 }
4957 }
4958
Craig Mautner27084302013-03-25 08:05:25 -07004959}