blob: f08a3b97dcf38a0416f4a3f16960f360991dda03 [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;
Vishnu Naircf235042018-11-02 13:27:00 -070030import static android.app.WaitResult.INVALID_DELAY;
Wale Ogunwale68278562017-09-23 17:13:55 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070034import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
35import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
40import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070042import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070043import static android.app.WindowConfiguration.activityTypeToString;
44import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070045import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Adrian Roosa6d6aab2018-04-19 18:58:22 +020047import static android.graphics.Rect.copyOrNull;
chaviw59b98852017-06-13 12:05:44 -070048import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010049import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020050import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
51import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070052import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070053import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070054
Winson Chung47900652017-04-06 18:44:25 -070055import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020056import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020057import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020059import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
64import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
65import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020066import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020068import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
76import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
77import static com.android.server.am.ActivityManagerService.ANIMATE;
78import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020079import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
80import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
81import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
82import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
83import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
84import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
85import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
86import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
87import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070088import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020089import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
90import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010091import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080092import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
93import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
94import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070095import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
96import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
97import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
98import static com.android.server.am.ActivityStackSupervisorProto.IS_HOME_RECENTS_COMPONENT;
99import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
100import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100101import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -0700102
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800103import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200104
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800106import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700107import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700108import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100109import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700110import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800112import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800113import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700114import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700116import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700117import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700118import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700119import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700120import android.app.WindowConfiguration.ActivityType;
121import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800122import android.app.servertransaction.ActivityLifecycleItem;
123import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700124import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800125import android.app.servertransaction.PauseActivityItem;
126import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700127import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700128import android.content.Context;
129import android.content.Intent;
130import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700131import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700132import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.content.pm.PackageManager;
134import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100135import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800137import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800138import android.hardware.display.DisplayManager;
139import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800140import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800141import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100143import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700144import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700145import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700146import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700147import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700148import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700150import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700151import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700152import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700153import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700154import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100155import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700156import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700157import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700158import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700159import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700160import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700161import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800162import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700163import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700164import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800166import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700167import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700168import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800169import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800170
Andreas Gampea36dc622018-02-05 17:19:22 -0800171import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700172import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800173import com.android.internal.content.ReferrerIntent;
Chenjie Yu52cacc62017-12-08 18:11:45 -0800174import com.android.internal.os.logging.MetricsLoggerWrapper;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700175import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700176import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800177import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700178import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700179import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700180import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700181import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700182
Craig Mautner8d341ef2013-03-26 09:03:27 -0700183import java.io.FileDescriptor;
184import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700185import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800186import java.lang.annotation.Retention;
187import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700188import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700189import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700190import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700191import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700192
Winson Chung1dbc8112017-09-28 18:05:31 -0700193public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
194 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800195 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700196 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700203 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800204 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800205
Craig Mautnerf3333272013-04-22 10:55:53 -0700206 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700208
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700210 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700211
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700213 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214
Craig Mautner05d29032013-05-03 13:40:13 -0700215 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
216 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
217 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700218 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700219 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800220 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
221 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
222 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700223 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800224 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
225 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800226
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228
Wale Ogunwale040b4702015-08-06 18:10:50 -0700229 // Used to indicate if an object (e.g. stack) that we are trying to get
230 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800231 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700232
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700233 // Used to indicate that windows of activities should be preserved during the resize.
234 static final boolean PRESERVE_WINDOWS = true;
235
Wale Ogunwale040b4702015-08-06 18:10:50 -0700236 // Used to indicate if an object (e.g. task) should be moved/created
237 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700238 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700239
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700240 // Don't execute any calls to resume.
241 static final boolean DEFER_RESUME = true;
242
Winson Chung010927a2016-12-15 16:12:35 -0800243 // Used to indicate that a task is removed it should also be removed from recents.
244 static final boolean REMOVE_FROM_RECENTS = true;
245
Winson Chung6954fc92017-03-24 16:22:12 -0700246 // Used to indicate that pausing an activity should occur immediately without waiting for
247 // the activity callback indicating that it has completed pausing
248 static final boolean PAUSE_IMMEDIATELY = true;
249
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200250 /** True if the docked stack is currently being resized. */
251 private boolean mDockedStackResizing;
252
253 /**
254 * True if there are pending docked bounds that need to be applied after
255 * {@link #mDockedStackResizing} is reset to false.
256 */
257 private boolean mHasPendingDockedBounds;
258 private Rect mPendingDockedBounds;
259 private Rect mPendingTempDockedTaskBounds;
260 private Rect mPendingTempDockedTaskInsetBounds;
261 private Rect mPendingTempOtherTaskBounds;
262 private Rect mPendingTempOtherTaskInsetBounds;
263
Winson Chung7900bee2017-03-10 08:59:25 -0800264 /**
265 * The modes which affect which tasks are returned when calling
266 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
267 */
268 @Retention(RetentionPolicy.SOURCE)
269 @IntDef({
270 MATCH_TASK_IN_STACKS_ONLY,
271 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
272 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
273 })
274 public @interface AnyTaskForIdMatchTaskMode {}
275 // Match only tasks in the current stacks
276 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
277 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
278 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
279 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
280 // provided stack id
281 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
282
Svetoslav7008b512015-06-24 18:47:07 -0700283 // Activity actions an app cannot start if it uses a permission which is not granted.
284 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
285 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700286
Svetoslav7008b512015-06-24 18:47:07 -0700287 static {
288 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
289 Manifest.permission.CAMERA);
290 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
291 Manifest.permission.CAMERA);
292 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
293 Manifest.permission.CALL_PHONE);
294 }
295
Svet Ganov99b60432015-06-27 13:15:22 -0700296 /** Action restriction: launching the activity is not restricted. */
297 private static final int ACTIVITY_RESTRICTION_NONE = 0;
298 /** Action restriction: launching the activity is restricted by a permission. */
299 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
300 /** Action restriction: launching the activity is restricted by an app op. */
301 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700302
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700303 // For debugging to make sure the caller when acquiring/releasing our
304 // wake lock is the system process.
305 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800306 /** The number of distinct task ids that can be assigned to the tasks of a single user */
307 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700308
Craig Mautner27084302013-03-25 08:05:25 -0700309 final ActivityManagerService mService;
310
Winson Chung61c9e5a2017-10-11 10:39:32 -0700311 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700312 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800313
Winson Chung61c9e5a2017-10-11 10:39:32 -0700314 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700315 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700316
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700317 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000318 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700319
320 /** Short cut */
321 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700323
Bryce Leeec55eb02017-12-05 20:51:27 -0800324 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700325
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800326 /**
327 * Maps the task identifier that activities are currently being started in to the userId of the
328 * task. Each time a new task is created, the entry for the userId of the task is incremented
329 */
330 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700331
Craig Mautner2420ead2013-04-01 17:13:20 -0700332 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800333 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700334
Craig Mautnere0a38842013-12-16 16:14:02 -0800335 /** The stack containing the launcher app. Assumed to always be attached to
336 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800337 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700338
Craig Mautnere0a38842013-12-16 16:14:02 -0800339 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800340 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700341
Craig Mautner4a1cb222013-12-04 16:14:06 -0800342 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
343 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800344 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800345 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346
347 /** List of activities that are waiting for a new activity to become visible before completing
348 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700349 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
350 // mStoppingActivities when something else comes up.
351 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700352
Bryce Lee4a194382017-04-04 14:32:48 -0700353 /** List of processes waiting to find out when a specific activity becomes visible. */
354 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700355
356 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700357 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700358
Craig Mautnerde4ef022013-04-07 19:01:33 -0700359 /** List of activities that are ready to be stopped, but waiting for the next activity to
360 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800361 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362
Craig Mautnerf3333272013-04-22 10:55:53 -0700363 /** List of activities that are ready to be finished, but waiting for the previous activity to
364 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800365 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700366
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800368 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700369
Wale Ogunwale22e25262016-02-01 10:32:02 -0800370 /** List of activities whose multi-window mode changed that we need to report to the
371 * application */
372 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
373
374 /** List of activities whose picture-in-picture mode changed that we need to report to the
375 * application */
376 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
377
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100378 /**
379 * Animations that for the current transition have requested not to
380 * be considered for the transition animation.
381 */
382 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
383
Winson Chung5af42fc2017-03-24 17:11:33 -0700384 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
385 * the application */
386 Rect mPipModeChangedTargetStackBounds;
387
Craig Mautnerf3333272013-04-22 10:55:53 -0700388 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700389 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700390
Craig Mautnerde4ef022013-04-07 19:01:33 -0700391 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
392 * is being brought in front of us. */
393 boolean mUserLeaving = false;
394
Bryce Leed3624e12017-11-30 08:51:45 -0800395 /** Set when a power hint has started, but not ended. */
396 private boolean mPowerHintSent;
397
Craig Mautner0eea92c2013-05-16 13:35:39 -0700398 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700399 * We don't want to allow the device to go to sleep while in the process
400 * of launching an activity. This is primarily to allow alarm intent
401 * receivers to launch an activity and get that to run before the device
402 * goes back to sleep.
403 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700404 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700405
406 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700407 * Set when the system is going to sleep, until we have
408 * successfully paused the current activity and released our wake lock.
409 * At that point the system is allowed to actually sleep.
410 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700411 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700412
David Stevens9440dc82017-03-16 19:00:20 -0700413 /**
414 * A list of tokens that cause the top activity to be put to sleep.
415 * They are used by components that may hide and block interaction with underlying
416 * activities.
417 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700418 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700419
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700420 /** Stack id of the front stack when user switched, indexed by userId. */
421 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700422
Bryce Leeaf3a4002017-03-20 10:37:12 -0700423 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800424 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700425 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800427 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
428
429 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800430
Wale Ogunwaled046a012015-12-24 13:05:59 -0800431 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800432 boolean inResumeTopActivity;
433
Andrii Kulian1e32e022016-09-16 15:29:34 -0700434 /**
435 * Temporary rect used during docked stack resize calculation so we don't need to create a new
436 * object each time.
437 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700438 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700439 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700440
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700441 // The default minimal size that will be used if the activity doesn't specify its minimal size.
442 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700443 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700444
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700445 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
446 private boolean mTaskLayersChanged = true;
447
Bryce Lee2a3cc462017-10-27 10:57:35 -0700448 private ActivityMetricsLogger mActivityMetricsLogger;
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
633 public KeyguardController getKeyguardController() {
634 return mKeyguardController;
635 }
636
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800637 void setRecentTasks(RecentTasks recentTasks) {
638 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700639 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800640 }
641
Winson Chung3f0e59a2017-10-25 10:19:05 -0700642 @VisibleForTesting
643 RunningTasks createRunningTasks() {
644 return new RunningTasks();
645 }
646
Jeff Brown2c43c332014-06-12 22:38:59 -0700647 /**
648 * At the time when the constructor runs, the power manager has not yet been
649 * initialized. So we initialize our wakelocks afterwards.
650 */
651 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700652 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
653 mGoingToSleep = mPowerManager
654 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
655 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700656 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700657 }
658
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700659 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 synchronized (mService) {
661 mWindowManager = wm;
Bryce Lee459c0622018-03-19 11:04:01 -0700662 getKeyguardController().setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663
664 mDisplayManager =
665 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
666 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800667 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668
669 Display[] displays = mDisplayManager.getDisplays();
670 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwale45477b52018-03-06 12:24:19 -0800671 final Display display = displays[displayNdx];
672 ActivityDisplay activityDisplay = new ActivityDisplay(this, display);
673 mActivityDisplays.put(display.getDisplayId(), activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700674 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 }
676
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700677 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
678 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 }
Craig Mautner27084302013-03-25 08:05:25 -0700680 }
681
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700682 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700684 }
685
Bryce Lee7b851cc2018-04-10 14:53:13 -0700686 boolean isFocusable(ConfigurationContainer container, boolean alwaysFocusable) {
687 if (container.inSplitScreenPrimaryWindowingMode() && mIsDockMinimized) {
688 return false;
689 }
690
691 return container.getWindowConfiguration().canReceiveKeys() || alwaysFocusable;
692 }
693
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800695 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700696 }
697
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700698 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700699 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700700 }
701
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800703 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
704 if (!focusCandidate.isFocusable()) {
705 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800706 focusCandidate = getNextFocusableStackLocked(focusCandidate, false /* ignoreCurrent */);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800707 }
708
709 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800710 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800711 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800712
Wale Ogunwaled046a012015-12-24 13:05:59 -0800713 EventLogTags.writeAmFocusedStack(
714 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
715 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
716 }
717
718 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800719 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800720 if (r != null && r.idle) {
721 checkFinishBootingLocked();
722 }
723 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 }
725
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700726 void moveHomeStackToFront(String reason) {
727 mHomeStack.moveToFront(reason);
728 }
729
Matthew Ng330757d2017-02-28 14:19:17 -0800730 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700731 final ActivityStack recentsStack = getDefaultDisplay().getStack(
732 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800733 if (recentsStack != null) {
734 recentsStack.moveToFront(reason);
735 }
736 }
737
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700738 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800739 boolean moveHomeStackTaskToTop(String reason) {
740 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700741
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700742 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700743 if (top == null) {
744 return false;
745 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700746 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700747 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700748 }
749
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800750 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700751 if (!mService.mBooting && !mService.mBooted) {
752 // Not ready yet!
753 return false;
754 }
755
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800756 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700757 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800758 final String myReason = reason + " resumeHomeStackTask";
759
Mark Lua56ea122015-10-08 13:31:01 +0800760 // Only resume home activity if isn't finishing.
761 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700762 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800763 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700764 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800765 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700766 }
767
Craig Mautner8d341ef2013-03-26 09:03:27 -0700768 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700769 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
770 }
771
772 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700773 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700774 }
775
776 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700777 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700778 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800779 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700780 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700781 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700782 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700783 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700784 @Nullable ActivityOptions aOptions, boolean onTop) {
785 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700786 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
787 throw new IllegalArgumentException("Should not specify activity options for non-restore"
788 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800789 }
790
Craig Mautnere0a38842013-12-16 16:14:02 -0800791 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800792 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700793 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
794 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
795 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700796 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700797 if (task == null) {
798 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800799 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700800 if (aOptions != null) {
801 // Resolve the stack the task should be placed in now based on options
802 // and reparent if needed.
803 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
804 if (launchStack != null && stack != launchStack) {
805 final int reparentMode = onTop
806 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
807 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
808 "anyTaskForIdLocked");
809 }
810 }
811 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700812 }
813 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800814
Winson Chung7900bee2017-03-10 08:59:25 -0800815 // If we are matching stack tasks only, return now
816 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800817 return null;
818 }
819
Winson Chung7900bee2017-03-10 08:59:25 -0800820 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
821 // the task from recents
822 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700823 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700824
825 if (task == null) {
826 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800827 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
828 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700829
830 return null;
831 }
832
833 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700834 return task;
835 }
836
Winson Chung7900bee2017-03-10 08:59:25 -0800837 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700838 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700839 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
840 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800841 return null;
842 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700843 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800844 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700845 }
846
Craig Mautner6170f732013-04-02 13:05:23 -0700847 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800848 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800849 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700850 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
851 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
852 final ActivityStack stack = display.getChildAt(stackNdx);
853 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 if (r != null) {
855 return r;
856 }
Craig Mautner6170f732013-04-02 13:05:23 -0700857 }
858 }
859 return null;
860 }
861
Tony Mak853304c2016-04-18 15:17:41 +0100862 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000863 * Detects whether we should show a lock screen in front of this task for a locked user.
864 * <p>
865 * We'll do this if either of the following holds:
866 * <ul>
867 * <li>The top activity explicitly belongs to {@param userId}.</li>
868 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
869 * </ul>
870 *
871 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100872 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000873 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
874 // To handle the case that work app is in the task but just is not the top one.
875 final ActivityRecord activityRecord = task.getTopActivity();
876 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
877
878 return (activityRecord != null && activityRecord.userId == userId)
879 || (resultTo != null && resultTo.userId == userId);
880 }
881
882 /**
883 * Find all visible task stacks containing {@param userId} and intercept them with an activity
884 * to block out the contents and possibly start a credential-confirming intent.
885 *
886 * @param userId user handle for the locked managed profile.
887 */
888 void lockAllProfileTasks(@UserIdInt int userId) {
889 mWindowManager.deferSurfaceLayout();
890 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700891 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
892 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
893 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
894 final ActivityStack stack = display.getChildAt(stackNdx);
895 final List<TaskRecord> tasks = stack.getAllTasks();
896 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
897 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000898
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700899 // Check the task for a top activity belonging to userId, or returning a
900 // result to an activity belonging to userId. Example case: a document
901 // picker for personal files, opened by a work app, should still get locked.
902 if (taskTopActivityIsUser(task, userId)) {
903 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
904 task.taskId, userId);
905 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000906 }
Tony Mak853304c2016-04-18 15:17:41 +0100907 }
908 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000909 } finally {
910 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100911 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000912 }
913
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800914 void setNextTaskIdForUserLocked(int taskId, int userId) {
915 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
916 if (taskId > currentTaskId) {
917 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700918 }
919 }
920
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700921 static int nextTaskIdForUser(int taskId, int userId) {
922 int nextTaskId = taskId + 1;
923 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
924 // Wrap around as there will be smaller task ids that are available now.
925 nextTaskId -= MAX_TASK_IDS_PER_USER;
926 }
927 return nextTaskId;
928 }
929
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800930 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800931 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
932 // for a userId u, a taskId can only be in the range
933 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
934 // 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 -0700935 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700936 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700937 || anyTaskForIdLocked(
938 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700939 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800940 if (candidateTaskId == currentTaskId) {
941 // Something wrong!
942 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
943 throw new IllegalStateException("Cannot get an available task id."
944 + " Reached limit of " + MAX_TASK_IDS_PER_USER
945 + " running tasks per user.");
946 }
947 }
948 mCurTaskIdForUser.put(userId, candidateTaskId);
949 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700950 }
951
Chong Zhang6cda19c2016-06-14 19:07:56 -0700952 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800953 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700954 if (stack == null) {
955 return null;
956 }
Bryce Leec4ab62a2018-03-05 14:19:26 -0800957 ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700958 if (resumedActivity == null || resumedActivity.app == null) {
959 resumedActivity = stack.mPausingActivity;
960 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700961 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700962 }
963 }
964 return resumedActivity;
965 }
966
Dianne Hackbornff072722014-09-24 10:56:28 -0700967 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700968 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700971 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
972 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
973 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700974 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 continue;
976 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200977 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
978 final ActivityRecord top = stack.topRunningActivityLocked();
979 final int size = mTmpActivityList.size();
980 for (int i = 0; i < size; i++) {
981 final ActivityRecord activity = mTmpActivityList.get(i);
982 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
983 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200985 if (realStartActivityLocked(activity, app,
986 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800987 didSomething = true;
988 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700989 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800990 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200991 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700993 }
Craig Mautner20e72272013-04-01 13:45:53 -0700994 }
Craig Mautner20e72272013-04-01 13:45:53 -0700995 }
996 }
997 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700998 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700999 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001000 }
Craig Mautner20e72272013-04-01 13:45:53 -07001001 return didSomething;
1002 }
1003
1004 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001005 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001006 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1007 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1008 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001009 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001010 continue;
1011 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08001012 final ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001013 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001014 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001015 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 return false;
1017 }
Craig Mautner20e72272013-04-01 13:45:53 -07001018 }
1019 }
Wei Wang65c7a152016-06-02 18:51:22 -07001020 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -08001021 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001022 return true;
1023 }
1024
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001027 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1028 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1029 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001030 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08001031 final ActivityRecord r = stack.getResumedActivity();
Bryce Lee7ace3952018-02-16 14:34:32 -08001032 if (r != null && !r.isState(RESUMED)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 return false;
1034 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 }
1036 }
1037 }
1038 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001039 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001040 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1041 mLastFocusedStack + " to=" + mFocusedStack);
1042 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 return true;
1044 }
1045
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001046 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001047 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001048 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001049 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1050 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1051 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001052 final ActivityRecord r = stack.getResumedActivity();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001053 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001054 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001055 return false;
1056 }
1057 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001058 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001059 }
1060 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001061 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001062 }
1063
Craig Mautner2acc3892013-09-23 10:28:14 -07001064 /**
1065 * Pause all activities in either all of the stacks or just the back stacks.
1066 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001067 * @param resuming The resuming activity.
1068 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1069 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001070 * @return true if any activity was paused as a result of this call.
1071 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001072 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001073 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001074 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001075 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1076 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1077 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001078 if (!isFocusedStack(stack) && stack.getResumedActivity() != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001079 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Bryce Leec4ab62a2018-03-05 14:19:26 -08001080 " mResumedActivity=" + stack.getResumedActivity());
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001081 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1082 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001083 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001084 }
1085 }
1086 return someActivityPaused;
1087 }
1088
Craig Mautnerde4ef022013-04-07 19:01:33 -07001089 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001090 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001091 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001092 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1093 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1094 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001095 final ActivityRecord r = stack.mPausingActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08001096 if (r != null && !r.isState(PAUSED, STOPPED, STOPPING)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001097 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001098 Slog.d(TAG_STATES,
Bryce Lee7ace3952018-02-16 14:34:32 -08001099 "allPausedActivitiesComplete: r=" + r + " state=" + r.getState());
Craig Mautner4a1cb222013-12-04 16:14:06 -08001100 pausing = false;
1101 } else {
1102 return false;
1103 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001104 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001105 }
1106 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001107 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001108 }
1109
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001110 void cancelInitializingActivities() {
1111 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001112 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1113 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1114 final ActivityStack stack = display.getChildAt(stackNdx);
1115 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001116 }
1117 }
1118 }
1119
Vishnu Naircf235042018-11-02 13:27:00 -07001120 void waitActivityVisible(ComponentName name, WaitResult result, long startTimeMs) {
1121 final WaitInfo waitInfo = new WaitInfo(name, result, startTimeMs);
Bryce Lee4a194382017-04-04 14:32:48 -07001122 mWaitingForActivityVisible.add(waitInfo);
1123 }
1124
1125 void cleanupActivity(ActivityRecord r) {
1126 // Make sure this record is no longer in the pending finishes list.
1127 // This could happen, for example, if we are trimming activities
1128 // down to the max limit while they are still waiting to finish.
1129 mFinishingActivities.remove(r);
1130 mActivitiesWaitingForVisibleActivity.remove(r);
1131
1132 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001133 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001134 mWaitingForActivityVisible.remove(i);
1135 }
1136 }
1137 }
1138
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001140 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001141 }
1142
1143 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1144 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001145 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1146 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001147 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001148 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001149 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001150 result.timeout = false;
1151 result.who = w.getComponent();
Vishnu Naircf235042018-11-02 13:27:00 -07001152 result.totalTime = SystemClock.uptimeMillis() - w.getStartTime();
Bryce Lee4a194382017-04-04 14:32:48 -07001153 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001154 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001155 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001156 if (changed) {
1157 mService.notifyAll();
1158 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 }
1160
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001161 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1162 if (mWaitingActivityLaunched.isEmpty()) {
1163 return;
1164 }
1165
1166 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1167 return;
1168 }
1169
Chong Zhang5022da32016-06-21 16:31:37 -07001170 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001171
Chong Zhang5022da32016-06-21 16:31:37 -07001172 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1173 WaitResult w = mWaitingActivityLaunched.remove(i);
1174 if (w.who == null) {
1175 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001176 w.result = result;
1177
1178 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1179 // will be no followup launch signals. Assign the result and launched component.
1180 if (result == START_DELIVERED_TO_TOP) {
1181 w.who = r.realActivity;
1182 }
Chong Zhang5022da32016-06-21 16:31:37 -07001183 }
1184 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001185
Chong Zhang5022da32016-06-21 16:31:37 -07001186 if (changed) {
1187 mService.notifyAll();
1188 }
1189 }
1190
Vishnu Naircf235042018-11-02 13:27:00 -07001191 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001192 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001193 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001194 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001195 if (w.who == null) {
1196 changed = true;
1197 w.timeout = timeout;
1198 if (r != null) {
1199 w.who = new ComponentName(r.info.packageName, r.info.name);
1200 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001201 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001202 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001203 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001204 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001205 if (changed) {
1206 mService.notifyAll();
1207 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001208 }
1209
Craig Mautner29219d92013-04-16 20:19:12 -07001210 ActivityRecord topRunningActivityLocked() {
Bryce Leec961e0a2018-04-13 17:58:02 -07001211 return topRunningActivityLocked(false /* considerKeyguardState */);
1212 }
1213
1214 /**
1215 * Returns the top running activity in the focused stack. In the case the focused stack has no
1216 * such activity, the next focusable stack on top of a display is returned.
1217 * @param considerKeyguardState Indicates whether the locked state should be considered. if
1218 * {@code true} and the keyguard is locked, only activities that
1219 * can be shown on top of the keyguard will be considered.
1220 * @return The top running activity. {@code null} if none is available.
1221 */
1222 ActivityRecord topRunningActivityLocked(boolean considerKeyguardState) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001223 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001224 ActivityRecord r = focusedStack.topRunningActivityLocked();
Bryce Lee86918962018-04-26 15:28:12 -07001225 if (r != null && isValidTopRunningActivity(r, considerKeyguardState)) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001226 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001227 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001228
Andrii Kulian7318d632016-07-20 18:59:28 -07001229 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001230 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1231
1232 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1233 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001234 final ActivityDisplay display = mActivityDisplays.get(displayId);
Bryce Lee4bc5ba32018-03-20 13:58:53 -07001235
1236 // If WindowManagerService has encountered the display before we have, ignore as there
1237 // will be no stacks present and therefore no activities.
1238 if (display == null) {
1239 continue;
1240 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001241
1242 // TODO: We probably want to consider the top fullscreen stack as we could have a pinned
1243 // stack on top.
1244 final ActivityStack topStack = display.getTopStack();
1245
1246 // Only consider focusable top stacks other than the current focused one.
1247 if (topStack == null || !topStack.isFocusable() || topStack == focusedStack) {
1248 continue;
1249 }
1250
1251 final ActivityRecord topActivity = topStack.topRunningActivityLocked();
1252
1253 // Skip if no top activity.
1254 if (topActivity == null) {
1255 continue;
1256 }
1257
Bryce Leec961e0a2018-04-13 17:58:02 -07001258
1259 // This activity can be considered the top running activity if we are not
1260 // considering the locked state, the keyguard isn't locked, or we can show when
1261 // locked.
Bryce Lee86918962018-04-26 15:28:12 -07001262 if (isValidTopRunningActivity(topActivity, considerKeyguardState)) {
Bryce Leec961e0a2018-04-13 17:58:02 -07001263 return topActivity;
Craig Mautner29219d92013-04-16 20:19:12 -07001264 }
1265 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001266
Craig Mautner29219d92013-04-16 20:19:12 -07001267 return null;
1268 }
1269
Bryce Lee86918962018-04-26 15:28:12 -07001270 /**
1271 * Verifies an {@link ActivityRecord} can be the top activity based on keyguard state and
1272 * whether we are considering it.
1273 */
1274 private boolean isValidTopRunningActivity(ActivityRecord record,
1275 boolean considerKeyguardState) {
1276 if (!considerKeyguardState) {
1277 return true;
1278 }
1279
1280 final boolean keyguardLocked = getKeyguardController().isKeyguardLocked();
1281
1282 if (!keyguardLocked) {
1283 return true;
1284 }
1285
1286 return record.canShowWhenLocked();
1287 }
1288
Winson Chung61c9e5a2017-10-11 10:39:32 -07001289 @VisibleForTesting
1290 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1291 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1292 int callingUid, boolean allowed) {
1293 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1294 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001295 }
1296
Todd Kennedy7440f172015-12-09 14:31:22 -08001297 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1298 ProfilerInfo profilerInfo) {
1299 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001300 if (aInfo != null) {
1301 // Store the found target back into the intent, because now that
1302 // we have it we never want to do this again. For example, if the
1303 // user navigates back to this point in the history, we should
1304 // always restart the exact same activity.
1305 intent.setComponent(new ComponentName(
1306 aInfo.applicationInfo.packageName, aInfo.name));
1307
1308 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001309 if (!aInfo.processName.equals("system")) {
1310 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001311 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001312 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001313
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001314 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1315 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1316 }
1317
Man Caocfa78b22015-06-11 20:14:34 -07001318 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1319 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1320 }
1321
1322 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001323 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001324 }
1325 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001326 final String intentLaunchToken = intent.getLaunchToken();
1327 if (aInfo.launchToken == null && intentLaunchToken != null) {
1328 aInfo.launchToken = intentLaunchToken;
1329 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001330 }
1331 return aInfo;
1332 }
1333
Patrick Baumann78380272018-04-04 10:41:01 -07001334 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
1335 int filterCallingUid) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001336 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001337 try {
1338 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001339 int modifiedFlags = flags
1340 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001341 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001342 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1343 modifiedFlags |= PackageManager.MATCH_INSTANT;
1344 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001345
Makoto Onuki1a342742018-04-26 14:56:59 -07001346 // In order to allow cross-profile lookup, we clear the calling identity here.
1347 // Note the binder identity won't affect the result, but filterCallingUid will.
1348
1349 // Cross-user/profile call check are done at the entry points
1350 // (e.g. AMS.startActivityAsUser).
1351 final long token = Binder.clearCallingIdentity();
1352 try {
1353 return mService.getPackageManagerInternalLocked().resolveIntent(
1354 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
1355 } finally {
1356 Binder.restoreCallingIdentity(token);
1357 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001358 } finally {
1359 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1360 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001361 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001362 }
1363
1364 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -07001365 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
1366 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -08001367 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1368 }
1369
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001370 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1371 boolean andResume, boolean checkConfig) throws RemoteException {
1372
1373 if (!allPausedActivitiesComplete()) {
1374 // While there are activities pausing we skipping starting any new activities until
1375 // pauses are complete. NOTE: that we also do this for activities that are starting in
1376 // the paused state because they will first be resumed then paused on the client side.
1377 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1378 "realStartActivityLocked: Skipping start of r=" + r
1379 + " some activities pausing...");
1380 return false;
1381 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001382
Bryce Leeaf691c02017-03-20 14:20:22 -07001383 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001384 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001385
1386 beginDeferResume();
1387
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 try {
chaviw59b98852017-06-13 12:05:44 -07001389 r.startFreezingScreenLocked(app, 0);
1390
1391 // schedule launch ticks to collect information about slow apps.
1392 r.startLaunchTickingLocked();
1393
Dianne Hackborn68a06332017-11-15 17:54:18 -08001394 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001395
Bryce Lee459c0622018-03-19 11:04:01 -07001396 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001397 r.notifyUnknownVisibilityLaunched();
1398 }
1399
chaviw59b98852017-06-13 12:05:44 -07001400 // Have the window manager re-evaluate the orientation of the screen based on the new
1401 // activity order. Note that as a result of this, it can call back into the activity
1402 // manager with a new orientation. We don't care about that, because the activity is
1403 // not currently running so we are just restarting it anyway.
1404 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -07001405 // Deferring resume here because we're going to launch new activity shortly.
1406 // We don't want to perform a redundant launch of the same record while ensuring
1407 // configurations and trying to resume top activity of focused stack.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001408 ensureVisibilityAndConfig(r, r.getDisplayId(),
1409 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001410 }
chaviw59b98852017-06-13 12:05:44 -07001411
1412 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1413 true /* isTop */)) {
1414 // We only set the visibility to true if the activity is allowed to be visible
1415 // based on
1416 // keyguard state. This avoids setting this into motion in window manager that is
1417 // later cancelled due to later calls to ensure visible activities that set
1418 // visibility back to false.
1419 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 }
chaviw59b98852017-06-13 12:05:44 -07001421
chaviw59b98852017-06-13 12:05:44 -07001422 final int applicationInfoUid =
1423 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1424 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1425 Slog.wtf(TAG,
1426 "User ID for activity changing for " + r
1427 + " appInfo.uid=" + r.appInfo.uid
1428 + " info.ai.uid=" + applicationInfoUid
1429 + " old=" + r.app + " new=" + app);
1430 }
1431
1432 app.waitingToKill = null;
1433 r.launchCount++;
1434 r.lastLaunchTime = SystemClock.uptimeMillis();
1435
1436 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1437
1438 int idx = app.activities.indexOf(r);
1439 if (idx < 0) {
1440 app.activities.add(r);
1441 }
1442 mService.updateLruProcessLocked(app, true, null);
1443 mService.updateOomAdjLocked();
1444
Bryce Lee2b8e0372018-04-05 17:01:37 -07001445 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +01001446 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1447 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1448 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -07001449 && lockTaskController.getLockTaskModeState()
1450 == LOCK_TASK_MODE_LOCKED)) {
1451 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001452 }
1453
1454 try {
1455 if (app.thread == null) {
1456 throw new RemoteException();
1457 }
1458 List<ResultInfo> results = null;
1459 List<ReferrerIntent> newIntents = null;
1460 if (andResume) {
1461 // We don't need to deliver new intents and/or set results if activity is going
1462 // to pause immediately after launch.
1463 results = r.results;
1464 newIntents = r.newIntents;
1465 }
1466 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1467 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1468 + " newIntents=" + newIntents + " andResume=" + andResume);
1469 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1470 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001471 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001472 // Home process is the root process of the task.
1473 mService.mHomeProcess = task.mActivities.get(0).app;
1474 }
1475 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1476 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1477 r.sleeping = false;
1478 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001479 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001480 mService.showAskCompatModeDialogLocked(r);
1481 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1482 ProfilerInfo profilerInfo = null;
1483 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1484 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1485 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001486 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1487 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1488 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001489 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001490 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001491 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001492 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001493 }
1494 }
chaviw59b98852017-06-13 12:05:44 -07001495
Andreas Gampe2b073a02017-06-22 17:28:57 -07001496 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001497 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001498 }
1499 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001500
chaviw59b98852017-06-13 12:05:44 -07001501 app.hasShownUi = true;
1502 app.pendingUiClean = true;
1503 app.forceProcessStateUpTo(mService.mTopProcessState);
1504 // Because we could be starting an Activity in the system process this may not go
1505 // across a Binder interface which would create a new Configuration. Consequently
1506 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001507
chaviw59b98852017-06-13 12:05:44 -07001508 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1509 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1510 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001511
chaviw59b98852017-06-13 12:05:44 -07001512 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001513
1514
1515 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001516 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001517 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001518 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001519 System.identityHashCode(r), r.info,
1520 // TODO: Have this take the merged configuration instead of separate global
1521 // and override configs.
1522 mergedConfiguration.getGlobalConfiguration(),
1523 mergedConfiguration.getOverrideConfiguration(), r.compat,
1524 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001525 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1526 profilerInfo));
1527
1528 // Set desired final state.
1529 final ActivityLifecycleItem lifecycleItem;
1530 if (andResume) {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001531 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001532 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001533 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001534 }
1535 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1536
1537 // Schedule transaction.
Bryce Leeb0f993f2018-03-02 15:38:01 -08001538 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001539
Craig Mautner2420ead2013-04-01 17:13:20 -07001540
Dianne Hackbornc8e4fad2018-05-04 11:31:09 -07001541 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
1542 && mService.mHasHeavyWeightFeature) {
chaviw59b98852017-06-13 12:05:44 -07001543 // This may be a heavy-weight process! Note that the package
1544 // manager will ensure that only activity can run in the main
1545 // process of the .apk, which is the only thing that will be
1546 // considered heavy-weight.
1547 if (app.processName.equals(app.info.packageName)) {
1548 if (mService.mHeavyWeightProcess != null
1549 && mService.mHeavyWeightProcess != app) {
1550 Slog.w(TAG, "Starting new heavy weight process " + app
1551 + " when already running "
1552 + mService.mHeavyWeightProcess);
1553 }
1554 mService.mHeavyWeightProcess = app;
1555 Message msg = mService.mHandler.obtainMessage(
1556 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1557 msg.obj = r;
1558 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001559 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001560 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001561
chaviw59b98852017-06-13 12:05:44 -07001562 } catch (RemoteException e) {
1563 if (r.launchFailed) {
1564 // This is the second time we failed -- finish activity
1565 // and give up.
1566 Slog.e(TAG, "Second failure launching "
1567 + r.intent.getComponent().flattenToShortString()
1568 + ", giving up", e);
1569 mService.appDiedLocked(app);
1570 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1571 "2nd-crash", false);
1572 return false;
1573 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001574
chaviw59b98852017-06-13 12:05:44 -07001575 // This is the first time we failed -- restart process and
1576 // retry.
1577 r.launchFailed = true;
1578 app.activities.remove(r);
1579 throw e;
1580 }
1581 } finally {
1582 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001583 }
1584
1585 r.launchFailed = false;
1586 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001587 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001588 }
1589
Andrii Kulianb372da62018-01-18 10:46:24 -08001590 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1591 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001592 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001593 // As part of the process of launching, ActivityThread also performs
1594 // a resume.
1595 stack.minimalResumeActivityLocked(r);
1596 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001597 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001598 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001599 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001600 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001601 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001602 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -07001603 }
1604
1605 // Launch the new version setup screen if needed. We do this -after-
1606 // launching the initial activity (that is, home), so that it can have
1607 // a chance to initialize itself while in the background, making the
1608 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001609 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001610 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001611 }
1612
Dianne Hackborn465fa392014-09-14 14:21:18 -07001613 // Update any services we are bound to that might care about whether
1614 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001615 if (r.app != null) {
1616 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1617 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001618
Craig Mautner2420ead2013-04-01 17:13:20 -07001619 return true;
1620 }
1621
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001622 /**
1623 * Ensure all activities visibility, update orientation and configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001624 *
1625 * @param starting The currently starting activity or {@code null} if there is none.
1626 * @param displayId The id of the display where operation is executed.
1627 * @param markFrozenIfConfigChanged Whether to set {@link ActivityRecord#frozenBeforeDestroy} to
1628 * {@code true} if config changed.
1629 * @param deferResume Whether to defer resume while updating config.
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001630 * @return 'true' if starting activity was kept or wasn't provided, 'false' if it was relaunched
1631 * because of configuration update.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001632 */
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001633 boolean ensureVisibilityAndConfig(ActivityRecord starting, int displayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001634 boolean markFrozenIfConfigChanged, boolean deferResume) {
1635 // First ensure visibility without updating the config just yet. We need this to know what
1636 // activities are affecting configuration now.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001637 // Passing null here for 'starting' param value, so that visibility of actual starting
1638 // activity will be properly updated.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001639 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001640 false /* preserveWindows */, false /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001641
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001642 if (displayId == INVALID_DISPLAY) {
1643 // The caller didn't provide a valid display id, skip updating config.
1644 return true;
1645 }
1646
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001647 // 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
Craig Mautnere79d42682013-04-01 19:01:53 -07001684 if (app != null && app.thread != null) {
1685 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001686 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1687 || !"android".equals(r.info.packageName)) {
1688 // Don't add this if it is a platform component that is marked
1689 // to run in multiple processes, because this is actually
1690 // part of the framework so doesn't make sense to track as a
1691 // separate apk in the process.
Patrick Baumannc2def582018-04-04 12:14:15 -07001692 app.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode,
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001693 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001694 }
George Mount2c92c972014-03-20 09:38:23 -07001695 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001696 return;
1697 } catch (RemoteException e) {
1698 Slog.w(TAG, "Exception when starting activity "
1699 + r.intent.getComponent().flattenToShortString(), e);
1700 }
1701
1702 // If a dead object exception was thrown -- fall through to
1703 // restart the application.
1704 }
1705
1706 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001707 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001708 }
1709
Bryce Leed3624e12017-11-30 08:51:45 -08001710 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1711 boolean sendHint = forceSend;
1712
1713 if (!sendHint) {
1714 // If not forced, send power hint when the activity's process is different than the
1715 // current resumed activity.
1716 final ActivityRecord resumedActivity = getResumedActivityLocked();
1717 sendHint = resumedActivity == null
1718 || resumedActivity.app == null
1719 || !resumedActivity.app.equals(targetActivity.app);
1720 }
1721
1722 if (sendHint && mService.mLocalPowerManager != null) {
1723 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1724 mPowerHintSent = true;
1725 }
1726 }
1727
1728 void sendPowerHintForLaunchEndIfNeeded() {
1729 // Trigger launch power hint if activity is launched
1730 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1731 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1732 mPowerHintSent = false;
1733 }
1734 }
1735
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001736 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001737 String resultWho, int requestCode, int callingPid, int callingUid,
Winson Chungc9804e72018-05-15 11:01:44 -07001738 String callingPackage, boolean ignoreTargetSecurity, boolean launchingInTask,
1739 ProcessRecord callerApp, ActivityRecord resultRecord, ActivityStack resultStack) {
1740 final boolean isCallerRecents = mService.getRecentTasks() != null &&
1741 mService.getRecentTasks().isCallerRecents(callingUid);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001742 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1743 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001744 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1745 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1746 // caller is the recents component and we are specifically starting an activity in an
1747 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001748 return true;
1749 }
1750 final int componentRestriction = getComponentRestrictionForCallingPackage(
1751 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1752 final int actionRestriction = getActionRestrictionForCallingPackage(
1753 intent.getAction(), callingPackage, callingPid, callingUid);
1754 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1755 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1756 if (resultRecord != null) {
1757 resultStack.sendActivityResultLocked(-1,
1758 resultRecord, resultWho, requestCode,
1759 Activity.RESULT_CANCELED, null);
1760 }
1761 final String msg;
1762 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1763 msg = "Permission Denial: starting " + intent.toString()
1764 + " from " + callerApp + " (pid=" + callingPid
1765 + ", uid=" + callingUid + ")" + " with revoked permission "
1766 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1767 } else if (!aInfo.exported) {
1768 msg = "Permission Denial: starting " + intent.toString()
1769 + " from " + callerApp + " (pid=" + callingPid
1770 + ", uid=" + callingUid + ")"
1771 + " not exported from uid " + aInfo.applicationInfo.uid;
1772 } else {
1773 msg = "Permission Denial: starting " + intent.toString()
1774 + " from " + callerApp + " (pid=" + callingPid
1775 + ", uid=" + callingUid + ")"
1776 + " requires " + aInfo.permission;
1777 }
1778 Slog.w(TAG, msg);
1779 throw new SecurityException(msg);
1780 }
1781
1782 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1783 final String message = "Appop Denial: starting " + intent.toString()
1784 + " from " + callerApp + " (pid=" + callingPid
1785 + ", uid=" + callingUid + ")"
1786 + " requires " + AppOpsManager.permissionToOp(
1787 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1788 Slog.w(TAG, message);
1789 return false;
1790 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1791 final String message = "Appop Denial: starting " + intent.toString()
1792 + " from " + callerApp + " (pid=" + callingPid
1793 + ", uid=" + callingUid + ")"
1794 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1795 Slog.w(TAG, message);
1796 return false;
1797 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001798
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001799 return true;
1800 }
1801
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001802 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001803 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1804 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001805 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1806 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1807
Andrii Kulian02689a72017-07-06 14:28:59 -07001808 if (callingPid == -1 && callingUid == -1) {
1809 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1810 return true;
1811 }
1812
Andrii Kulian62e6f252017-05-30 22:46:53 -07001813 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001814 if (activityDisplay == null) {
1815 Slog.w(TAG, "Launch on display check: display not found");
1816 return false;
1817 }
1818
Andrii Kulian02689a72017-07-06 14:28:59 -07001819 // Check if the caller has enough privileges to embed activities and launch to private
1820 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001821 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001822 callingUid);
1823 if (startAnyPerm == PERMISSION_GRANTED) {
1824 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1825 + " allow launch any on display");
1826 return true;
1827 }
1828
Andrii Kulian8f070292017-09-12 22:56:49 -07001829 // Check if caller is already present on display
1830 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1831
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001832 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1833 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1834 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001835 // Limit launching on virtual displays, because their contents can be read from Surface
1836 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001837 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1838 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1839 + " disallow launch on virtual display for not-embedded activity.");
1840 return false;
1841 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001842 // Check if the caller is allowed to embed activities from other apps.
1843 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001844 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001845 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1846 + " disallow activity embedding without permission.");
1847 return false;
1848 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001849 }
1850
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001851 if (!activityDisplay.isPrivate()) {
1852 // Anyone can launch on a public display.
1853 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1854 + " allow launch on public display");
1855 return true;
1856 }
1857
1858 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001859 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001860 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1861 + " allow launch for owner of the display");
1862 return true;
1863 }
1864
Andrii Kulian8f070292017-09-12 22:56:49 -07001865 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001866 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1867 + " allow launch for caller present on the display");
1868 return true;
1869 }
1870
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001871 Slog.w(TAG, "Launch on display check: denied");
1872 return false;
1873 }
1874
1875 /** Update lists of UIDs that are present on displays and have access to them. */
1876 void updateUIDsPresentOnDisplay() {
1877 mDisplayAccessUIDs.clear();
1878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1879 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001880 // Only bother calculating the whitelist for private displays
1881 if (activityDisplay.isPrivate()) {
1882 mDisplayAccessUIDs.append(
1883 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1884 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001885 }
1886 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1887 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1888 }
1889
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001890 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001891 final long identity = Binder.clearCallingIdentity();
1892 try {
1893 return UserManager.get(mService.mContext).getUserInfo(userId);
1894 } finally {
1895 Binder.restoreCallingIdentity(identity);
1896 }
1897 }
1898
Svet Ganov99b60432015-06-27 13:15:22 -07001899 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001900 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001901 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1902 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001903 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001904 return ACTIVITY_RESTRICTION_PERMISSION;
1905 }
1906
Christopher Tateff7add02015-08-17 10:23:22 -07001907 if (activityInfo.permission == null) {
1908 return ACTIVITY_RESTRICTION_NONE;
1909 }
1910
Svet Ganov99b60432015-06-27 13:15:22 -07001911 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1912 if (opCode == AppOpsManager.OP_NONE) {
1913 return ACTIVITY_RESTRICTION_NONE;
1914 }
1915
1916 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1917 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001918 if (!ignoreTargetSecurity) {
1919 return ACTIVITY_RESTRICTION_APPOP;
1920 }
Svet Ganov99b60432015-06-27 13:15:22 -07001921 }
1922
1923 return ACTIVITY_RESTRICTION_NONE;
1924 }
1925
Svetoslav7008b512015-06-24 18:47:07 -07001926 private int getActionRestrictionForCallingPackage(String action,
1927 String callingPackage, int callingPid, int callingUid) {
1928 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001929 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001930 }
1931
1932 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1933 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001934 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001935 }
1936
1937 final PackageInfo packageInfo;
1938 try {
1939 packageInfo = mService.mContext.getPackageManager()
1940 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1941 } catch (PackageManager.NameNotFoundException e) {
1942 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001943 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001944 }
1945
1946 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001947 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001948 }
1949
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001950 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001951 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001952 }
1953
1954 final int opCode = AppOpsManager.permissionToOpCode(permission);
1955 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001956 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001957 }
1958
1959 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1960 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001961 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001962 }
1963
Svet Ganov99b60432015-06-27 13:15:22 -07001964 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001965 }
1966
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001967 void setLaunchSource(int uid) {
1968 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1969 }
1970
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001971 void acquireLaunchWakelock() {
1972 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1973 throw new IllegalStateException("Calling must be system uid");
1974 }
1975 mLaunchingActivity.acquire();
1976 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1977 // To be safe, don't allow the wake lock to be held for too long.
1978 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1979 }
1980 }
1981
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001982 /**
1983 * Called when the frontmost task is idle.
1984 * @return the state of mService.mBooting before this was called.
1985 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001986 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001987 private boolean checkFinishBootingLocked() {
1988 final boolean booting = mService.mBooting;
1989 boolean enableScreen = false;
1990 mService.mBooting = false;
1991 if (!mService.mBooted) {
1992 mService.mBooted = true;
1993 enableScreen = true;
1994 }
1995 if (booting || enableScreen) {
1996 mService.postFinishBooting(booting, enableScreen);
1997 }
1998 return booting;
1999 }
2000
Craig Mautnerf3333272013-04-22 10:55:53 -07002001 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08002002 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07002003 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08002004 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002005 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002006
Craig Mautnerf3333272013-04-22 10:55:53 -07002007 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002008 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002009 int NS = 0;
2010 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002011 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002012 boolean activityRemoved = false;
2013
Wale Ogunwale7d701172015-03-11 15:36:30 -07002014 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002015 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002016 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2017 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002018 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2019 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002020 if (fromTimeout) {
Vishnu Naircf235042018-11-02 13:27:00 -07002021 reportActivityLaunchedLocked(fromTimeout, r, INVALID_DELAY);
Craig Mautnerf3333272013-04-22 10:55:53 -07002022 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002023
2024 // This is a hack to semi-deal with a race condition
2025 // in the client where it can be constructed with a
2026 // newer configuration from when we asked it to launch.
2027 // We'll update with whatever configuration it now says
2028 // it used to launch.
2029 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08002030 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002031 }
2032
2033 // We are now idle. If someone is waiting for a thumbnail from
2034 // us, we can now deliver.
2035 r.idle = true;
2036
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002037 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002038 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002039 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002040 }
2041 }
2042
2043 if (allResumedActivitiesIdle()) {
2044 if (r != null) {
2045 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002046 }
2047
2048 if (mLaunchingActivity.isHeld()) {
2049 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2050 if (VALIDATE_WAKE_LOCK_CALLER &&
2051 Binder.getCallingUid() != Process.myUid()) {
2052 throw new IllegalStateException("Calling must be system uid");
2053 }
2054 mLaunchingActivity.release();
2055 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002056 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002057 }
2058
2059 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08002060 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
2061 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002062 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002063 if ((NF = mFinishingActivities.size()) > 0) {
2064 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002065 mFinishingActivities.clear();
2066 }
2067
Craig Mautnerf3333272013-04-22 10:55:53 -07002068 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002069 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002070 mStartingUsers.clear();
2071 }
2072
Craig Mautnerf3333272013-04-22 10:55:53 -07002073 // Stop any activities that are scheduled to do so but have been
2074 // waiting for the next one to start.
2075 for (int i = 0; i < NS; i++) {
2076 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002077 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002078 if (stack != null) {
2079 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08002080 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
2081 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002082 } else {
2083 stack.stopActivityLocked(r);
2084 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002085 }
2086 }
2087
2088 // Finish any activities that are scheduled to do so but have been
2089 // waiting for the next one to start.
2090 for (int i = 0; i < NF; i++) {
2091 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002092 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002093 if (stack != null) {
2094 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2095 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002096 }
2097
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002098 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002099 // Complete user switch
2100 if (startingUsers != null) {
2101 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002102 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002103 }
2104 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002105 }
2106
2107 mService.trimApplications();
2108 //dump();
2109 //mWindowManager.dump();
2110
Craig Mautnerf3333272013-04-22 10:55:53 -07002111 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002112 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002113 }
2114
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002115 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002116 }
2117
Craig Mautner8e569572013-10-11 17:36:59 -07002118 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002119 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002120 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002121 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2122 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2123 final ActivityStack stack = display.getChildAt(stackNdx);
2124 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002125 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002126 }
Craig Mautner19091252013-10-05 00:03:53 -07002127 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002128 }
2129
2130 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002131 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002132 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2133 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2134 final ActivityStack stack = display.getChildAt(stackNdx);
2135 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002136 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
2138 }
2139
Craig Mautner93529a42013-10-04 15:03:13 -07002140 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002141 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002142 }
2143
Craig Mautner8d341ef2013-03-26 09:03:27 -07002144 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002145 * Update the last used stack id for non-current user (current user's last
2146 * used stack is the focused stack)
2147 */
2148 void updateUserStackLocked(int userId, ActivityStack stack) {
2149 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002150 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002151 }
2152 }
2153
2154 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002155 * @return true if some activity was finished (or would have finished if doit were true).
2156 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002157 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2158 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002159 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002160 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002161 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2162 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2163 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002164 if (stack.finishDisabledPackageActivitiesLocked(
2165 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002166 didSomething = true;
2167 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002168 }
2169 }
2170 return didSomething;
2171 }
2172
Dianne Hackborna413dc02013-07-12 12:02:55 -07002173 void updatePreviousProcessLocked(ActivityRecord r) {
2174 // Now that this process has stopped, we may want to consider
2175 // it to be the previous app to try to keep around in case
2176 // the user wants to return to it.
2177
2178 // First, found out what is currently the foreground app, so that
2179 // we don't blow away the previous app if this activity is being
2180 // hosted by the process that is actually still the foreground.
2181 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002182 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002183 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2184 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2185 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002186 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002187 final ActivityRecord resumedActivity = stack.getResumedActivity();
2188 if (resumedActivity != null) {
2189 fgApp = resumedActivity.app;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002190 } else if (stack.mPausingActivity != null) {
2191 fgApp = stack.mPausingActivity.app;
2192 }
2193 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002194 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002195 }
2196 }
2197
2198 // Now set this one as the previous process, only if that really
2199 // makes sense to.
2200 if (r.app != null && fgApp != null && r.app != fgApp
2201 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002202 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002203 mService.mPreviousProcess = r.app;
2204 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2205 }
2206 }
2207
Wale Ogunwaled046a012015-12-24 13:05:59 -08002208 boolean resumeFocusedStackTopActivityLocked() {
2209 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002210 }
2211
Wale Ogunwaled046a012015-12-24 13:05:59 -08002212 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002213 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002214
2215 if (!readyToResume()) {
2216 return false;
2217 }
2218
Wale Ogunwaled046a012015-12-24 13:05:59 -08002219 if (targetStack != null && isFocusedStack(targetStack)) {
2220 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002221 }
chaviw59b98852017-06-13 12:05:44 -07002222
Wale Ogunwale06579d62016-04-30 15:29:06 -07002223 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08002224 if (r == null || !r.isState(RESUMED)) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07002225 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
Bryce Lee7ace3952018-02-16 14:34:32 -08002226 } else if (r.isState(RESUMED)) {
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002227 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2228 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002229 }
chaviw59b98852017-06-13 12:05:44 -07002230
Wale Ogunwaled046a012015-12-24 13:05:59 -08002231 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002232 }
2233
Todd Kennedy39bfee52016-02-24 10:28:21 -08002234 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2235 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002236 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2237 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2238 final ActivityStack stack = display.getChildAt(stackNdx);
2239 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002240 }
2241 }
2242 }
2243
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002244 /**
2245 * Finish the topmost activities in all stacks that belong to the crashed app.
2246 * @param app The app that crashed.
2247 * @param reason Reason to perform this action.
2248 * @return The task that was finished in this stack, {@code null} if haven't found any.
2249 */
2250 TaskRecord finishTopCrashedActivitiesLocked(ProcessRecord app, String reason) {
Adrian Roos20d7df32016-01-12 18:59:43 +01002251 TaskRecord finishedTask = null;
2252 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002254 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002255 // It is possible that request to finish activity might also remove its task and stack,
2256 // so we need to be careful with indexes in the loop and check child count every time.
2257 for (int stackNdx = 0; stackNdx < display.getChildCount(); ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002258 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002259 final TaskRecord t = stack.finishTopCrashedActivityLocked(app, reason);
Adrian Roos20d7df32016-01-12 18:59:43 +01002260 if (stack == focusedStack || finishedTask == null) {
2261 finishedTask = t;
2262 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002263 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002264 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002265 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002266 }
2267
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002268 void finishVoiceTask(IVoiceInteractionSession session) {
2269 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002270 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2271 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002272 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002273 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002274 stack.finishVoiceTask(session);
2275 }
2276 }
2277 }
2278
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002279 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2280 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002281 final ActivityStack currentStack = task.getStack();
2282 if (currentStack == null) {
2283 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2284 + task + " to front. Stack is null");
2285 return;
2286 }
2287
Craig Mautneraea74a52014-03-08 14:23:10 -08002288 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2289 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002290 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002291
2292 final ActivityRecord prev = topRunningActivityLocked();
2293
2294 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2295 || (prev != null && prev.isActivityTypeRecents())) {
2296 // Caller wants the home activity moved with it or the previous task is recents in which
2297 // case we always return home from the task we are moving to the front.
2298 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002299 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002300
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002301 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002302 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002303 task.updateOverrideConfiguration(bounds);
2304
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002305 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002306
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002307 if (stack != currentStack) {
2308 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002309 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002310 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002311 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2312 // still need moveTaskToFrontLocked() below for any transition settings.
2313 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002314 if (stack.resizeStackWithLaunchBounds()) {
2315 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002316 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2317 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002318 } else {
2319 // WM resizeTask must be done after the task is moved to the correct stack,
2320 // because Task's setBounds() also updates dim layer's bounds, but that has
2321 // dependency on the stack.
2322 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002323 }
2324 }
2325
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002326 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07002327 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002328 r == null ? null : r.appTimeTracker, reason);
2329
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002330 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002331 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002332
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002333 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002334 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002335 }
2336
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002337 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002338 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002339 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002340 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002341 return false;
2342 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002343 return (mService.mSupportsPictureInPicture
2344 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002345 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002346 }
2347
Bryce Leeec55eb02017-12-05 20:51:27 -08002348 LaunchParamsController getLaunchParamsController() {
2349 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002350 }
2351
Winson Chung55893332017-02-17 17:13:10 -08002352 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002353 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2354 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2355 if (stack != null) {
2356 return stack;
2357 }
2358 }
2359 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002360 }
2361
Wale Ogunwale68278562017-09-23 17:13:55 -07002362 /** @see ActivityDisplay#getStack(int, int) */
2363 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2364 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2365 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2366 if (stack != null) {
2367 return stack;
2368 }
2369 }
2370 return null;
2371 }
2372
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002373 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002374 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002375 // Preference is given to the activity type for the activity then the task since the type
2376 // once set shouldn't change.
2377 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2378 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2379 activityType = task.getActivityType();
2380 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002381 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2382 return activityType;
2383 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002384 if (options != null) {
2385 activityType = options.getLaunchActivityType();
2386 }
2387 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002388 }
2389
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002390 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2391 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2392 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002393 }
2394
2395 /**
2396 * Returns the right stack to use for launching factoring in all the input parameters.
2397 *
2398 * @param r The activity we are trying to launch. Can be null.
2399 * @param options The activity options used to the launch. Can be null.
2400 * @param candidateTask The possible task the activity might be launched in. Can be null.
2401 *
2402 * @return The stack to use for the launch or INVALID_STACK_ID.
2403 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002404 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2405 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2406 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002407 int taskId = INVALID_TASK_ID;
2408 int displayId = INVALID_DISPLAY;
2409 //Rect bounds = null;
2410
2411 // We give preference to the launch preference in activity options.
2412 if (options != null) {
2413 taskId = options.getLaunchTaskId();
2414 displayId = options.getLaunchDisplayId();
2415 // TODO: Need to work this into the equation...
2416 //bounds = options.getLaunchBounds();
2417 }
2418
2419 // First preference for stack goes to the task Id set in the activity options. Use the stack
2420 // associated with that if possible.
2421 if (taskId != INVALID_TASK_ID) {
2422 // Temporarily set the task id to invalid in case in re-entry.
2423 options.setLaunchTaskId(INVALID_TASK_ID);
2424 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002425 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002426 options.setLaunchTaskId(taskId);
2427 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002428 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002429 }
2430 }
2431
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002432 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002433 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002434
2435 // Next preference for stack goes to the display Id set in the activity options or the
2436 // candidate display.
2437 if (displayId == INVALID_DISPLAY) {
2438 displayId = candidateDisplayId;
2439 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002440 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002441 if (r != null) {
2442 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002443 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002444 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002445 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002446 }
2447 }
2448 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2449 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002450 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002451 if (stack != null) {
2452 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002453 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002454 }
2455 }
2456
2457 // Give preference to the stack and display of the input task and activity if they match the
2458 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002459 stack = null;
2460 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002461 if (candidateTask != null) {
2462 stack = candidateTask.getStack();
2463 }
2464 if (stack == null && r != null) {
2465 stack = r.getStack();
2466 }
2467 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002468 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002469 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002470 final int windowingMode =
2471 display.resolveWindowingMode(r, options, candidateTask, activityType);
2472 if (stack.isCompatible(windowingMode, activityType)) {
2473 return stack;
2474 }
Andrii Kulianec7d4c42018-04-26 18:31:58 -07002475 if (windowingMode == WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY
2476 && display.getSplitScreenPrimaryStack() == stack
2477 && candidateTask == stack.topTask()) {
2478 // This is a special case when we try to launch an activity that is currently on
2479 // top of split-screen primary stack, but is targeting split-screen secondary.
2480 // In this case we don't want to move it to another stack.
2481 // TODO(b/78788972): Remove after differentiating between preferred and required
2482 // launch options.
2483 return stack;
2484 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002485 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002486 }
2487
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002488 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002489 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002490 // TODO: Can be removed once we figure-out how non-standard types should launch
2491 // outside the default display.
2492 || (activityType != ACTIVITY_TYPE_STANDARD
2493 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2494 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002495 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002496
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002497 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002498 }
2499
Andrii Kulianb850ea52017-12-12 23:49:10 -08002500 /** @return true if activity record is null or can be launched on provided display. */
2501 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2502 if (r == null) {
2503 return true;
2504 }
2505 return r.canBeLaunchedOnDisplay(displayId);
2506 }
2507
Andrii Kulian16802aa2016-11-02 12:21:33 -07002508 /**
2509 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2510 * If there is no such stack, new dynamic stack can be created.
2511 * @param displayId Target display.
2512 * @param r Activity that should be launched there.
2513 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2514 */
2515 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002516 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002517 if (activityDisplay == null) {
2518 throw new IllegalArgumentException(
2519 "Display with displayId=" + displayId + " not found.");
2520 }
2521
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002522 if (!r.canBeLaunchedOnDisplay(displayId)) {
2523 return null;
2524 }
2525
Andrii Kulian16802aa2016-11-02 12:21:33 -07002526 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002527 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2528 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002529 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002530 return stack;
2531 }
2532 }
2533
2534 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002535 if (displayId != DEFAULT_DISPLAY) {
2536 return activityDisplay.createStack(
2537 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002538 }
2539
2540 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2541 return null;
2542 }
2543
Wale Ogunwale68278562017-09-23 17:13:55 -07002544 // TODO: Can probably be consolidated into getLaunchStack()...
2545 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2546 switch (stack.getActivityType()) {
2547 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2548 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2549 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002550 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002551 switch (stack.getWindowingMode()) {
2552 case WINDOWING_MODE_FULLSCREEN: return true;
2553 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2554 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2555 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2556 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2557 }
2558
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002559 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002560 return r.canBeLaunchedOnDisplay(displayId);
2561 }
2562 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2563 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002564 }
2565
Andrii Kulian7fc22812016-12-28 13:04:11 -08002566 /**
2567 * Get next focusable stack in the system. This will search across displays and stacks
2568 * in last-focused order for a focusable and visible stack, different from the target stack.
2569 *
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002570 * @param currentFocus The stack that previously had focus.
2571 * @param ignoreCurrent If we should ignore {@param currentFocus} when searching for next
2572 * candidate.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002573 * @return Next focusable {@link ActivityStack}, null if not found.
2574 */
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002575 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus, boolean ignoreCurrent) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002576 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2577
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002578 final int currentWindowingMode = currentFocus != null
2579 ? currentFocus.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
2580 ActivityStack candidate = null;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002581 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2582 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002583 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002584 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
Wale Ogunwalec2ea3642018-05-24 00:32:23 -07002585 if (display == null) {
2586 // Looks like the display no longer exists in the system...
2587 continue;
2588 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002589 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2590 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002591 if (ignoreCurrent && stack == currentFocus) {
2592 continue;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002593 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002594 if (!stack.isFocusable() || !stack.shouldBeVisible(null)) {
2595 continue;
2596 }
2597
2598 if (currentWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2599 && candidate == null && stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale17696de2018-03-15 16:41:05 -07002600 // If the currently focused stack is in split-screen secondary we save off the
2601 // top primary split-screen stack as a candidate for focus because we might
2602 // prefer focus to move to an other stack to avoid primary split-screen stack
2603 // overlapping with a fullscreen stack when a fullscreen stack is higher in z
2604 // than the next split-screen stack. Assistant stack, I am looking at you...
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002605 // We only move the focus to the primary-split screen stack if there isn't a
2606 // better alternative.
2607 candidate = stack;
2608 continue;
2609 }
Wale Ogunwale17696de2018-03-15 16:41:05 -07002610 if (candidate != null && stack.inSplitScreenSecondaryWindowingMode()) {
2611 // Use the candidate stack since we are now at the secondary split-screen.
2612 return candidate;
2613 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002614 return stack;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002615 }
2616 }
2617
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002618 return candidate;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002619 }
2620
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002621 /**
2622 * Get next valid stack for launching provided activity in the system. This will search across
2623 * displays and stacks in last-focused order for a focusable and visible stack, except those
2624 * that are on a currently focused display.
2625 *
2626 * @param r The activity that is being launched.
2627 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2628 * searching for the next candidate.
2629 * @return Next valid {@link ActivityStack}, null if not found.
2630 */
2631 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2632 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2633 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2634 final int displayId = mTmpOrderedDisplayIds.get(i);
2635 if (displayId == currentFocus) {
2636 continue;
2637 }
2638 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2639 if (stack != null) {
2640 return stack;
2641 }
2642 }
2643 return null;
2644 }
2645
Craig Mautneree2e45a2014-06-27 12:10:03 -07002646 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002647 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002648 }
2649
2650 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002651 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2652 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2653 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002654 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002655 final ArrayList<ActivityRecord> activities = task.mActivities;
2656 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2657 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002658 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002659 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002660 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002661 }
2662 }
2663 }
2664 }
2665 return null;
2666 }
2667
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002668 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2669 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2670 boolean deferResume) {
2671
2672 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002673 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002674 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002675 return;
2676 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002677
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002678 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002679 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002680 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002681 // If the docked stack exists, don't resize non-floating stacks independently of the
2682 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002683 return;
2684 }
2685
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002686 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002687 mWindowManager.deferSurfaceLayout();
2688 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002689 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002690 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2691 // null bounds = fullscreen windowing mode...at least for now.
2692 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2693 } else if (splitScreenActive) {
2694 // If we are in split-screen mode and this stack support split-screen, then
2695 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2696 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2697 }
2698 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002699 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002700 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002701 stack.ensureVisibleActivitiesConfigurationLocked(
2702 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002703 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002704 } finally {
2705 mWindowManager.continueSurfaceLayout();
2706 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002707 }
2708 }
2709
Winson Chungc1674272018-02-21 10:15:17 -08002710 void deferUpdateRecentsHomeStackBounds() {
2711 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
2712 deferUpdateBounds(ACTIVITY_TYPE_HOME);
2713 }
2714
Bryce Leed3624e12017-11-30 08:51:45 -08002715 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002716 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002717 if (stack != null) {
2718 stack.deferUpdateBounds();
2719 }
2720 }
2721
Winson Chungc1674272018-02-21 10:15:17 -08002722 void continueUpdateRecentsHomeStackBounds() {
2723 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
2724 continueUpdateBounds(ACTIVITY_TYPE_HOME);
2725 }
2726
Bryce Leed3624e12017-11-30 08:51:45 -08002727 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002728 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002729 if (stack != null) {
2730 stack.continueUpdateBounds();
2731 }
2732 }
2733
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002734 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08002735 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002736 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2737 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002738 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002739 if (task != null) {
2740 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002741 }
2742 }
2743 mResizingTasksDuringAnimation.clear();
2744 }
2745
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002746 /**
2747 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002748 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002749 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002750 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002751 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002752
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002753 mWindowManager.deferSurfaceLayout();
2754 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002755 final int windowingMode = fromStack.getWindowingMode();
2756 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2757 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2758
2759 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002760 // Tell the display we are exiting split-screen mode.
2761 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002762 // We are moving all tasks from the docked stack to the fullscreen stack,
2763 // which is dismissing the docked stack, so resize all other stacks to
2764 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002765 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2766 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002767 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2768 continue;
2769 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002770 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002771 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002772 }
2773
2774 // Also disable docked stack resizing since we have manually adjusted the
2775 // size of other stacks above and we don't want to trigger a docked stack
2776 // resize when we remove task from it below and it is detached from the
2777 // display because it no longer contains any tasks.
2778 mAllowDockedStackResize = false;
2779 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002780
Winson Chung5af42fc2017-03-24 17:11:33 -07002781 // If we are moving from the pinned stack, then the animation takes care of updating
2782 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002783 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002784 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002785
Wale Ogunwale388945c2017-10-04 12:13:46 -07002786 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002787 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002788 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002789 for (int i = 0; i < size; ++i) {
2790 final TaskRecord task = tasks.get(i);
2791 final ActivityStack toStack = toDisplay.getOrCreateStack(
2792 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002793
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002794 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002795 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002796 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002797 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002798 isTopTask /* animate */, DEFER_RESUME,
2799 schedulePictureInPictureModeChange,
2800 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002801 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2802 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002803 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002804 // Position the tasks in the fullscreen stack in order at the bottom of the
2805 // stack. Also defer resume until all the tasks have been moved to the
2806 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002807 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002808 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2809 schedulePictureInPictureModeChange,
2810 "moveTasksToFullscreenStack - NOT_onTop");
2811 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002812 }
2813 }
Winson Chung74666102017-02-22 17:49:24 -08002814
2815 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2816 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002817 } finally {
2818 mAllowDockedStackResize = true;
2819 mWindowManager.continueSurfaceLayout();
2820 }
2821 }
2822
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002823 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002824 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2825 }
2826
2827 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2828 mWindowManager.inSurfaceTransaction(() ->
2829 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002830 }
2831
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002832 void setSplitScreenResizing(boolean resizing) {
2833 if (resizing == mDockedStackResizing) {
2834 return;
2835 }
2836
2837 mDockedStackResizing = resizing;
2838 mWindowManager.setDockedStackResizing(resizing);
2839
2840 if (!resizing && mHasPendingDockedBounds) {
2841 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
2842 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
2843 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
2844
2845 mHasPendingDockedBounds = false;
2846 mPendingDockedBounds = null;
2847 mPendingTempDockedTaskBounds = null;
2848 mPendingTempDockedTaskInsetBounds = null;
2849 mPendingTempOtherTaskBounds = null;
2850 mPendingTempOtherTaskInsetBounds = null;
2851 }
2852 }
2853
Jorim Jaggidc249c42015-12-15 14:57:31 -08002854 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002855 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2856 boolean preserveWindows) {
2857 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2858 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2859 false /* deferResume */);
2860 }
2861
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002862 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002863 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2864 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002865
2866 if (!mAllowDockedStackResize) {
2867 // Docked stack resize currently disabled.
2868 return;
2869 }
2870
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002871 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002872 if (stack == null) {
2873 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2874 return;
2875 }
2876
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002877 if (mDockedStackResizing) {
2878 mHasPendingDockedBounds = true;
2879 mPendingDockedBounds = copyOrNull(dockedBounds);
2880 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
2881 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
2882 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
2883 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
2884 }
2885
Jorim Jaggidc249c42015-12-15 14:57:31 -08002886 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2887 mWindowManager.deferSurfaceLayout();
2888 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002889 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2890 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002891 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002892 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002893
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002894 // TODO: Checking for isAttached might not be needed as if the user passes in null
2895 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002896 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2897 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002898 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002899 // In this case we make all other static stacks fullscreen and move all
2900 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002901 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002902
2903 // stack shouldn't contain anymore activities, so nothing to resume.
2904 r = null;
2905 } else {
2906 // Docked stacks occupy a dedicated region on screen so the size of all other
2907 // static stacks need to be adjusted so they don't overlap with the docked stack.
2908 // We get the bounds to use from window manager which has been adjusted for any
2909 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002910 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002911 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002912 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2913 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002914 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002915 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002916 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002917 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002918 continue;
2919 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002920 if (mDockedStackResizing && !current.isTopActivityVisible()) {
2921 // Non-visible stacks get resized once we're done with the resize
2922 // interaction.
2923 continue;
2924 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002925 // Need to set windowing mode here before we try to get the dock bounds.
2926 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2927 current.getStackDockedModeBounds(
2928 tempOtherTaskBounds /* currentTempTaskBounds */,
2929 tempRect /* outStackBounds */,
2930 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2931
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002932 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002933 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2934 tempOtherTaskInsetBounds, preserveWindows,
2935 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002936 }
2937 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002938 if (!deferResume) {
2939 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2940 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002941 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002942 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002943 mWindowManager.continueSurfaceLayout();
2944 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2945 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002946 }
2947
Robert Carr0d00c2e2016-02-29 17:45:02 -08002948 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002949 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002950 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002951 if (stack == null) {
2952 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2953 return;
2954 }
Winson Chung19953ca2017-04-11 11:19:23 -07002955
2956 // It is possible for the bounds animation from the WM to call this but be delayed by
2957 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2958 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2959 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2960 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002961 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002962 return;
2963 }
2964
Robert Carr0d00c2e2016-02-29 17:45:02 -08002965 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2966 mWindowManager.deferSurfaceLayout();
2967 try {
2968 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002969 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02002970 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
2971 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08002972 insetBounds = tempRect;
2973 insetBounds.top = 0;
2974 insetBounds.left = 0;
2975 insetBounds.right = tempPinnedTaskBounds.width();
2976 insetBounds.bottom = tempPinnedTaskBounds.height();
2977 }
Winson Chung15036ca2018-05-31 15:51:47 -07002978 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
2979 // We have finished the animation into PiP, and are resizing the tasks to match the
2980 // stack bounds, while layouts are deferred, update any task state as a part of
2981 // transitioning it from fullscreen into a floating state.
2982 stack.onPipAnimationEndResize();
2983 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08002984 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002985 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002986 } finally {
2987 mWindowManager.continueSurfaceLayout();
2988 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2989 }
2990 }
2991
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002992 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002993 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002994 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002995 /**
2996 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2997 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2998 * that the client receives onStop() before it is reparented. We do this by detaching
2999 * the stack from the display so that it will be considered invisible when
3000 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
3001 * invisible as well and added to the stopping list. After which we process the
3002 * stopping list by handling the idle.
3003 */
3004 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
3005 pinnedStack.mForceHidden = true;
3006 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
3007 pinnedStack.mForceHidden = false;
3008 activityIdleInternalLocked(null, false /* fromTimeout */,
3009 true /* processPausingActivites */, null /* configuration */);
3010
3011 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003012 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08003013 } else {
3014 for (int i = tasks.size() - 1; i >= 0; i--) {
3015 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07003016 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08003017 }
3018 }
3019 }
3020
3021 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003022 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07003023 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
3024 * instead moved back onto the fullscreen stack.
3025 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003026 void removeStack(ActivityStack stack) {
3027 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003028 }
3029
Wale Ogunwale68278562017-09-23 17:13:55 -07003030 /**
3031 * Removes stacks in the input windowing modes from the system if they are of activity type
3032 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
3033 */
3034 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003035 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003036 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
3037 }
3038 }
3039
3040 void removeStacksWithActivityTypes(int... activityTypes) {
3041 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3042 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003043 }
Robert Carr6914f082017-03-20 19:04:30 -07003044 }
3045
3046 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003047 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
3048 */
Winson Chung0ec2a352017-10-26 11:38:30 -07003049 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
3050 String reason) {
3051 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
3052 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07003053 }
3054
3055 /**
Winson Chung010927a2016-12-15 16:12:35 -08003056 * Removes the task with the specified task id.
3057 *
3058 * @param taskId Identifier of the task to be removed.
3059 * @param killProcess Kill any process associated with the task if possible.
3060 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07003061 * @param pauseImmediately Pauses all task activities immediately without waiting for the
3062 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08003063 * @return Returns true if the given task was found and removed.
3064 */
Winson Chung6954fc92017-03-24 16:22:12 -07003065 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07003066 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003067 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08003068 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003069 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08003070 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Bryce Lee2b8e0372018-04-05 17:01:37 -07003071 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003072 if (tr.isPersistable) {
3073 mService.notifyTaskPersisterLocked(null, true);
3074 }
3075 return true;
3076 }
3077 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
3078 return false;
3079 }
3080
3081 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
3082 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07003083 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003084 }
3085 ComponentName component = tr.getBaseIntent().getComponent();
3086 if (component == null) {
3087 Slog.w(TAG, "No component for base intent of task: " + tr);
3088 return;
3089 }
3090
3091 // Find any running services associated with this app and stop if needed.
3092 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
3093
3094 if (!killProcess) {
3095 return;
3096 }
3097
3098 // Determine if the process(es) for this task should be killed.
3099 final String pkg = component.getPackageName();
3100 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
3101 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
3102 for (int i = 0; i < pmap.size(); i++) {
3103
3104 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
3105 for (int j = 0; j < uids.size(); j++) {
3106 ProcessRecord proc = uids.valueAt(j);
3107 if (proc.userId != tr.userId) {
3108 // Don't kill process for a different user.
3109 continue;
3110 }
3111 if (proc == mService.mHomeProcess) {
3112 // Don't kill the home process along with tasks from the same package.
3113 continue;
3114 }
3115 if (!proc.pkgList.containsKey(pkg)) {
3116 // Don't kill process that is not associated with this task.
3117 continue;
3118 }
3119
3120 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003121 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08003122 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
3123 // Don't kill process(es) that has an activity in a different task that is
3124 // also in recents.
3125 return;
3126 }
3127 }
3128
3129 if (proc.foregroundServices) {
3130 // Don't kill process(es) with foreground service.
3131 return;
3132 }
3133
3134 // Add process to kill list.
3135 procsToKill.add(proc);
3136 }
3137 }
3138
3139 // Kill the running processes.
3140 for (int i = 0; i < procsToKill.size(); i++) {
3141 ProcessRecord pr = procsToKill.get(i);
3142 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
3143 && pr.curReceivers.isEmpty()) {
3144 pr.kill("remove task", true);
3145 } else {
3146 // We delay killing processes that are not in the background or running a receiver.
3147 pr.waitingToKill = "remove task";
3148 }
3149 }
3150 }
3151
Chong Zhang5dcb2752015-08-18 13:50:26 -07003152 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07003153 * Called to restore the state of the task into the stack that it's supposed to go into.
3154 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07003155 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003156 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003157 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003158 * @return true if the task has been restored successfully.
3159 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003160 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
3161 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07003162 final ActivityStack currentStack = task.getStack();
3163 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003164 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003165 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003166 // Nothing else to do since it is already restored in the right stack.
3167 return true;
3168 }
3169 // Remove current stack association, so we can re-associate the task with the
3170 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003171 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003172 }
3173
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003174 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003175 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003176 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003177 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3178 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003179 final ArrayList<ActivityRecord> activities = task.mActivities;
3180 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003181 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003182 }
3183 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003184 }
3185
Winson Chung1dbc8112017-09-28 18:05:31 -07003186 @Override
3187 public void onRecentTaskAdded(TaskRecord task) {
3188 task.touchActiveTime();
3189 }
3190
3191 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003192 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003193 if (wasTrimmed) {
3194 // Task was trimmed from the recent tasks list -- remove the active task record as well
3195 // since the user won't really be able to go back to it
3196 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3197 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
3198 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003199 task.removedFromRecents();
3200 }
3201
Wale Ogunwale040b4702015-08-06 18:10:50 -07003202 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003203 * Move stack with all its existing content to specified display.
3204 * @param stackId Id of stack to move.
3205 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003206 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003207 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003208 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003209 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003210 if (activityDisplay == null) {
3211 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3212 + displayId);
3213 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003214 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003215 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003216 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3217 + stackId);
3218 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003219
3220 final ActivityDisplay currentDisplay = stack.getDisplay();
3221 if (currentDisplay == null) {
3222 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3223 + " is not attached to any display.");
3224 }
3225
3226 if (currentDisplay.mDisplayId == displayId) {
3227 throw new IllegalArgumentException("Trying to move stack=" + stack
3228 + " to its current displayId=" + displayId);
3229 }
3230
3231 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003232 // TODO(multi-display): resize stacks properly if moved from split-screen.
3233 }
3234
3235 /**
Winson Chung74666102017-02-22 17:49:24 -08003236 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3237 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003238 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003239 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003240 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003241 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003242 final int stackId = stack.mStackId;
3243 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003244
Winson Chung74666102017-02-22 17:49:24 -08003245 // Check that we aren't reparenting to the same stack that the task is already in
3246 if (prevStack != null && prevStack.mStackId == stackId) {
3247 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3248 + " already in stackId=" + stackId);
3249 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003250 }
3251
Winson Chung74666102017-02-22 17:49:24 -08003252 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3253 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003254 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003255 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003256 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003257 }
3258
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003259 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3260 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003261 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003262 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3263 + " reparent task=" + task + " to stackId=" + stackId);
3264 }
3265
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003266 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003267 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3268 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003269 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3270 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003271 }
3272
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003273 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3274 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003275 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003276 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3277 + " Moving to a fullscreen stack instead.");
3278 if (prevStack != null) {
3279 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003280 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003281 stack = stack.getDisplay().createStack(
3282 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003283 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003284 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003285 }
3286
Winson Chung08f81892017-03-02 15:40:51 -08003287 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003288 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003289 if (stack == null) {
3290 throw new IllegalArgumentException(
3291 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3292 }
3293
3294 final ActivityRecord r = stack.topRunningActivityLocked();
3295 if (r == null) {
3296 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3297 + " in stack=" + stack);
3298 return false;
3299 }
3300
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003301 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003302 Slog.w(TAG,
3303 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003304 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003305 return false;
3306 }
3307
Winson Chung3a682872017-04-10 14:38:05 -07003308 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003309 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003310 return true;
3311 }
3312
Winson Chung8bca9e42017-04-16 15:59:43 -07003313 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003314 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003315
Wale Ogunwale480dca02016-02-06 13:58:29 -08003316 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003317
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003318 final ActivityDisplay display = r.getStack().getDisplay();
3319 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003320
Bryce Lee04ab3462017-04-10 15:06:33 -07003321 // This will clear the pinned stack by moving an existing task to the full screen stack,
3322 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003323 if (stack != null) {
3324 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3325 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003326
Wale Ogunwale1666e312016-12-16 11:27:18 -08003327 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003328 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003329
Winson Chunge55c0192017-08-24 14:50:48 -07003330 // Calculate the target bounds here before the task is reparented back into pinned windowing
3331 // mode (which will reset the saved bounds)
3332 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3333
Wale Ogunwale480dca02016-02-06 13:58:29 -08003334 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003335 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003336 // Resize the pinned stack to match the current size of the task the activity we are
3337 // going to be moving is currently contained in. We do this to have the right starting
3338 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003339 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003340 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003341 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003342
3343 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003344 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003345 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3346 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003347 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003348 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003349 // reveal/leave the other activities in their original task.
3350
3351 // Currently, we don't support reparenting activities across tasks in two different
3352 // stacks, so instead, just create a new task in the same stack, reparent the
3353 // activity into that task, and then reparent the whole task to the new stack. This
3354 // ensures that all the necessary work to migrate states in the old and new stacks
3355 // is also done.
3356 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003357 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003358 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3359
Winson Chung5af42fc2017-03-24 17:11:33 -07003360 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003361 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003362 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003363 }
Winson Chungc2baac02017-01-11 13:34:47 -08003364
3365 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3366 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003367 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003368 } finally {
3369 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003370 }
3371
Winson Chunge7ba6862017-05-24 12:13:33 -07003372 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3373 true /* fromFullscreen */);
3374
3375 // Update the visibility of all activities after the they have been reparented to the new
3376 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3377 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3378 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003379 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003380 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003381
Wale Ogunwale89be5762017-10-04 13:27:49 -07003382 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003383 }
3384
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003385 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003386 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3387 if (r == null || !r.isFocusable()) {
3388 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3389 "moveActivityStackToFront: unfocusable r=" + r);
3390 return false;
3391 }
3392
Bryce Leeaf691c02017-03-20 14:20:22 -07003393 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003394 final ActivityStack stack = r.getStack();
3395 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003396 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3397 + r + " task=" + task);
3398 return false;
3399 }
3400
Chong Zhang6cda19c2016-06-14 19:07:56 -07003401 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3402 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3403 "moveActivityStackToFront: already on top, r=" + r);
3404 return false;
3405 }
3406
3407 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3408 "moveActivityStackToFront: r=" + r);
3409
3410 stack.moveToFront(reason, task);
3411 return true;
3412 }
3413
David Stevensc6b91c62017-02-08 14:23:58 -08003414 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003415 mTmpFindTaskResult.r = null;
3416 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003417 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003418 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003419 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003420 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3421 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3422 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003423 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003424 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3425 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003426 continue;
3427 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003428 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003429 // It is possible to have tasks in multiple stacks with the same root affinity, so
3430 // we should keep looking after finding an affinity match to see if there is a
3431 // better match in another stack. Also, task affinity isn't a good enough reason
3432 // to target a display which isn't the source of the intent, so skip any affinity
3433 // matches not on the specified display.
3434 if (mTmpFindTaskResult.r != null) {
3435 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3436 return mTmpFindTaskResult.r;
3437 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003438 // Note: since the traversing through the stacks is top down, the floating
3439 // tasks should always have lower priority than any affinity-matching tasks
3440 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003441 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003442 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3443 Slog.d(TAG_TASKS, "Skipping match on different display "
3444 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003445 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003447 }
3448 }
Winson Chung5b895b72017-05-01 13:46:25 -07003449
David Stevensc6b91c62017-02-08 14:23:58 -08003450 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3451 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003452 }
3453
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003454 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003455 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003456 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003457 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3458 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3459 final ActivityStack stack = display.getChildAt(stackNdx);
3460 final ActivityRecord ar = stack.findActivityLocked(
3461 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003462 if (ar != null) {
3463 return ar;
3464 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003465 }
3466 }
3467 return null;
3468 }
3469
David Stevens9440dc82017-03-16 19:00:20 -07003470 boolean hasAwakeDisplay() {
3471 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3472 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3473 if (!display.shouldSleep()) {
3474 return true;
3475 }
3476 }
3477 return false;
3478 }
3479
Craig Mautner8d341ef2013-03-26 09:03:27 -07003480 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003481 scheduleSleepTimeout();
3482 if (!mGoingToSleep.isHeld()) {
3483 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003484 if (mLaunchingActivity.isHeld()) {
3485 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3486 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003487 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003488 mLaunchingActivity.release();
3489 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003490 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003491 }
David Stevens9440dc82017-03-16 19:00:20 -07003492
3493 applySleepTokensLocked(false /* applyToStacks */);
3494
3495 checkReadyForSleepLocked(true /* allowDelay */);
3496 }
3497
3498 void prepareForShutdownLocked() {
3499 for (int i = 0; i < mActivityDisplays.size(); i++) {
3500 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3501 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003502 }
3503
3504 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003505 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003506
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003507 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003508 final long endTime = System.currentTimeMillis() + timeout;
3509 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003510 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003511 long timeRemaining = endTime - System.currentTimeMillis();
3512 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003513 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003514 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003515 } catch (InterruptedException e) {
3516 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003517 } else {
3518 Slog.w(TAG, "Activity manager shutdown timed out");
3519 timedout = true;
3520 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003521 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003522 } else {
3523 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003524 }
3525 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526
3527 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003528 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003529
Craig Mautner8d341ef2013-03-26 09:03:27 -07003530 return timedout;
3531 }
3532
3533 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003534 removeSleepTimeouts();
3535 if (mGoingToSleep.isHeld()) {
3536 mGoingToSleep.release();
3537 }
David Stevens9440dc82017-03-16 19:00:20 -07003538 }
3539
3540 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003541 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003542 // Set the sleeping state of the display.
3543 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3544 final boolean displayShouldSleep = display.shouldSleep();
3545 if (displayShouldSleep == display.isSleeping()) {
3546 continue;
3547 }
3548 display.setIsSleeping(displayShouldSleep);
3549
3550 if (!applyToStacks) {
3551 continue;
3552 }
3553
3554 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003555 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3556 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003557 if (displayShouldSleep) {
3558 stack.goToSleepIfPossible(false /* shuttingDown */);
3559 } else {
3560 stack.awakeFromSleepingLocked();
Lucas Dupin47a65c72018-02-15 14:16:18 -08003561 if (isFocusedStack(stack) && !getKeyguardController().isKeyguardOrAodShowing(
3562 display.mDisplayId)) {
Bryce Leed939cf02018-03-12 09:04:44 -07003563 // If the keyguard is unlocked - resume immediately.
3564 // It is possible that the display will not be awake at the time we
3565 // process the keyguard going away, which can happen before the sleep token
3566 // is released. As a result, it is important we resume the activity here.
David Stevens9440dc82017-03-16 19:00:20 -07003567 resumeFocusedStackTopActivityLocked();
3568 }
3569 }
3570 }
3571
3572 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3573 continue;
3574 }
3575 // The display is awake now, so clean up the going to sleep list.
3576 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3577 final ActivityRecord r = it.next();
3578 if (r.getDisplayId() == display.mDisplayId) {
3579 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 }
Craig Mautner5314a402013-09-26 12:40:16 -07003581 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003582 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003583 }
3584
3585 void activitySleptLocked(ActivityRecord r) {
3586 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003587 final ActivityStack s = r.getStack();
3588 if (s != null) {
3589 s.checkReadyForSleep();
3590 } else {
3591 checkReadyForSleepLocked(true);
3592 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003593 }
3594
David Stevens9440dc82017-03-16 19:00:20 -07003595 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003596 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003597 // Do not care.
3598 return;
3599 }
3600
David Stevens18abd0e2017-08-17 14:55:47 -07003601 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3602 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003603 }
3604
Wei Wang65c7a152016-06-02 18:51:22 -07003605 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003606 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003607
Craig Mautner0eea92c2013-05-16 13:35:39 -07003608 removeSleepTimeouts();
3609
3610 if (mGoingToSleep.isHeld()) {
3611 mGoingToSleep.release();
3612 }
3613 if (mService.mShuttingDown) {
3614 mService.notifyAll();
3615 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003616 }
3617
David Stevens18abd0e2017-08-17 14:55:47 -07003618 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3619 // successfully put to sleep.
3620 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3621 boolean allSleep = true;
3622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003623 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3624 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3625 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003626 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003627 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003628 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003629 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003630 }
3631 }
3632 }
3633 return allSleep;
3634 }
3635
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003636 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003637 // A resumed activity cannot be stopping. remove from list
3638 mStoppingActivities.remove(r);
3639
Andrii Kulian02b7a832016-10-06 23:11:56 -07003640 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003641 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003642 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003643 }
3644 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003645 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003646 mWindowManager.executeAppTransition();
3647 return true;
3648 }
3649 return false;
3650 }
3651
Craig Mautner8d341ef2013-03-26 09:03:27 -07003652 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003654 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3655 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3656 final ActivityStack stack = display.getChildAt(stackNdx);
3657 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003658 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003659 }
3660 }
3661
Craig Mautnerbb742462014-07-07 15:28:55 -07003662 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003663 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003664 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003665 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003666
3667 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003668 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003669 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003670 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003671
3672 // When launching tasks behind, update the last active time of the top task after the new
3673 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003674 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003675 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003676 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003677 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003678 }
3679
3680 void scheduleLaunchTaskBehindComplete(IBinder token) {
3681 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3682 }
3683
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003684 /**
3685 * Make sure that all activities that need to be visible in the system actually are and update
3686 * their configuration.
3687 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003688 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3689 boolean preserveWindows) {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003690 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003691 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003692 }
3693
3694 /**
3695 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
3696 */
3697 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003698 boolean preserveWindows, boolean notifyClients) {
Bryce Lee459c0622018-03-19 11:04:01 -07003699 getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02003700 try {
3701 // First the front stacks. In case any are not fullscreen and are in front of home.
3702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003703 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3704 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3705 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003706 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003707 notifyClients);
Jorim Jaggife762342016-10-13 14:33:27 +02003708 }
Craig Mautner580ea812013-04-25 12:58:38 -07003709 }
Jorim Jaggife762342016-10-13 14:33:27 +02003710 } finally {
Bryce Lee459c0622018-03-19 11:04:01 -07003711 getKeyguardController().endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003712 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003713 }
3714
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003715 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003717 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3718 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3719 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003720 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3721 }
3722 }
3723 }
3724
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003725 void invalidateTaskLayers() {
3726 mTaskLayersChanged = true;
3727 }
3728
3729 void rankTaskLayersIfNeeded() {
3730 if (!mTaskLayersChanged) {
3731 return;
3732 }
3733 mTaskLayersChanged = false;
3734 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003735 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003736 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003737 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3738 final ActivityStack stack = display.getChildAt(stackNdx);
3739 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003740 }
3741 }
3742 }
3743
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003744 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3745 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003746 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3747 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3748 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003749 stack.clearOtherAppTimeTrackers(except);
3750 }
3751 }
3752 }
3753
Craig Mautner8d341ef2013-03-26 09:03:27 -07003754 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003755 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003756 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3757 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3758 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003759 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003760 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003761 }
3762 }
3763
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003764 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3765 // Examine all activities currently running in the process.
3766 TaskRecord firstTask = null;
3767 // Tasks is non-null only if two or more tasks are found.
3768 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003769 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3770 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003771 ActivityRecord r = app.activities.get(i);
3772 // First, if we find an activity that is in the process of being destroyed,
3773 // then we just aren't going to do anything for now; we want things to settle
3774 // down before we try to prune more activities.
Bryce Lee7ace3952018-02-16 14:34:32 -08003775 if (r.finishing || r.isState(DESTROYING, DESTROYED)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003777 return;
3778 }
3779 // Don't consider any activies that are currently not in a state where they
3780 // can be destroyed.
Bryce Lee7ace3952018-02-16 14:34:32 -08003781 if (r.visible || !r.stopped || !r.haveState
3782 || r.isState(RESUMED, PAUSING, PAUSED, STOPPING)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003783 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003784 continue;
3785 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003786
3787 final TaskRecord task = r.getTask();
3788 if (task != null) {
3789 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003790 + " from " + r);
3791 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003792 firstTask = task;
3793 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003794 if (tasks == null) {
3795 tasks = new ArraySet<>();
3796 tasks.add(firstTask);
3797 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003798 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003799 }
3800 }
3801 }
3802 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003803 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003804 return;
3805 }
3806 // If we have activities in multiple tasks that are in a position to be destroyed,
3807 // let's iterate through the tasks and release the oldest one.
3808 final int numDisplays = mActivityDisplays.size();
3809 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003810 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3811 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003812 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003813 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3814 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003815 // Try to release activities in this stack; if we manage to, we are done.
3816 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3817 return;
3818 }
3819 }
3820 }
3821 }
3822
Amith Yamasani37a40c22015-06-17 13:25:42 -07003823 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003824 final int focusStackId = mFocusedStack.getStackId();
3825 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003826 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003827 if (dockedStack != null) {
3828 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3829 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003830 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3831 // also cause all tasks to be moved to the fullscreen stack at a position that is
3832 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003833 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003834
3835 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003836 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003837 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003838
Craig Mautner858d8a62013-04-23 17:08:34 -07003839 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003840 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003841 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3842 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3843 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003844 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003845 TaskRecord task = stack.topTask();
3846 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003847 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003848 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003849 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003850 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003851
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003852 ActivityStack stack = getStack(restoreStackId);
3853 if (stack == null) {
3854 stack = mHomeStack;
3855 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003856 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003857 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003858 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003859 } else {
3860 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003861 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003862 }
Craig Mautner93529a42013-10-04 15:03:13 -07003863 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003864 }
3865
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003866 /** Checks whether the userid is a profile of the current user. */
3867 boolean isCurrentProfileLocked(int userId) {
3868 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003869 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003870 }
3871
Bryce Leeb7c9b802017-05-02 14:20:24 -07003872 /**
3873 * Returns whether a stopping activity is present that should be stopped after visible, rather
3874 * than idle.
3875 * @return {@code true} if such activity is present. {@code false} otherwise.
3876 */
3877 boolean isStoppingNoHistoryActivity() {
3878 // Activities that are marked as nohistory should be stopped immediately after the resumed
3879 // activity has become visible.
3880 for (ActivityRecord record : mStoppingActivities) {
3881 if (record.isNoHistory()) {
3882 return true;
3883 }
3884 }
3885
3886 return false;
3887 }
3888
Winson Chung4dabf232017-01-25 13:25:22 -08003889 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3890 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003891 ArrayList<ActivityRecord> stops = null;
3892
3893 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003894 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3895 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003896 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003897 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003898 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3899 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003900 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003901 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003902 if (s.finishing) {
3903 // If this activity is finishing, it is sitting on top of
3904 // everyone else but we now know it is no longer needed...
3905 // so get rid of it. Otherwise, we need to go through the
3906 // normal flow and hide it once we determine that it is
3907 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003908 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003909 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003910 }
3911 }
David Stevens9440dc82017-03-16 19:00:20 -07003912 if (remove) {
3913 final ActivityStack stack = s.getStack();
3914 final boolean shouldSleepOrShutDown = stack != null
3915 ? stack.shouldSleepOrShutDownActivities()
3916 : mService.isSleepingOrShuttingDownLocked();
3917 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08003918 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07003919 // Defer processing pausing activities in this iteration and reschedule
3920 // a delayed idle to reprocess it again
3921 removeTimeoutsForActivityLocked(idleActivity);
3922 scheduleIdleTimeoutLocked(idleActivity);
3923 continue;
3924 }
Winson Chung4dabf232017-01-25 13:25:22 -08003925
David Stevens9440dc82017-03-16 19:00:20 -07003926 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3927 if (stops == null) {
3928 stops = new ArrayList<>();
3929 }
3930 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02003931
3932 // Make sure to remove it in all cases in case we entered this block with
3933 // shouldSleepOrShutDown
3934 mActivitiesWaitingForVisibleActivity.remove(s);
David Stevens9440dc82017-03-16 19:00:20 -07003935 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003936 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003937 }
3938 }
3939
3940 return stops;
3941 }
3942
Craig Mautnercf910b02013-04-23 11:23:27 -07003943 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003945 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3946 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3947 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003948 final ActivityRecord r = stack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08003949 final ActivityState state = r == null ? DESTROYED : r.getState();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003950 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003951 if (r == null) Slog.e(TAG,
3952 "validateTop...: null top activity, stack=" + stack);
3953 else {
3954 final ActivityRecord pausing = stack.mPausingActivity;
3955 if (pausing != null && pausing == r) Slog.e(TAG,
3956 "validateTop...: top stack has pausing activity r=" + r
3957 + " state=" + state);
3958 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3959 "validateTop...: activity in front not resumed r=" + r
3960 + " state=" + state);
3961 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003962 } else {
Bryce Leec4ab62a2018-03-05 14:19:26 -08003963 final ActivityRecord resumed = stack.getResumedActivity();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003964 if (resumed != null && resumed == r) Slog.e(TAG,
3965 "validateTop...: back stack has resumed activity r=" + r
3966 + " state=" + state);
3967 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3968 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003969 }
3970 }
3971 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003972 }
3973
Bryce Lee77a7dd62018-01-22 15:47:09 -08003974 public void dumpDisplays(PrintWriter pw) {
3975 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3976 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3977 pw.print("[id:" + display.mDisplayId + " stacks:");
3978 display.dumpStacks(pw);
3979 pw.print("]");
3980 }
3981 }
3982
Craig Mautner27084302013-03-25 08:05:25 -07003983 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003984 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003985 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003986 pw.print(prefix);
3987 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003988 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003989 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3990 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003991 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003992 }
Bryce Lee4a194382017-04-04 14:32:48 -07003993 if (!mWaitingForActivityVisible.isEmpty()) {
3994 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3995 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3996 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3997 }
3998 }
Winson Chungc1674272018-02-21 10:15:17 -08003999 pw.print(prefix); pw.print("isHomeRecentsComponent=");
4000 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07004001
Bryce Lee459c0622018-03-19 11:04:01 -07004002 getKeyguardController().dump(pw, prefix);
Bryce Lee2b8e0372018-04-05 17:01:37 -07004003 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07004004 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004005
Yi Jin2b30f322018-02-20 15:41:47 -08004006 public void writeToProto(ProtoOutputStream proto, long fieldId) {
4007 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02004008 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004009 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4010 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
4011 activityDisplay.writeToProto(proto, DISPLAYS);
4012 }
Bryce Lee459c0622018-03-19 11:04:01 -07004013 getKeyguardController().writeToProto(proto, KEYGUARD_CONTROLLER);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004014 if (mFocusedStack != null) {
4015 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
4016 ActivityRecord focusedActivity = getResumedActivityLocked();
4017 if (focusedActivity != null) {
4018 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
4019 }
4020 } else {
4021 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
4022 }
Winson Chungc1674272018-02-21 10:15:17 -08004023 proto.write(IS_HOME_RECENTS_COMPONENT,
4024 mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Yi Jin2b30f322018-02-20 15:41:47 -08004025 proto.end(token);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004026 }
4027
Winson43d1f262016-06-14 16:05:55 -07004028 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07004029 * Dump all connected displays' configurations.
4030 * @param prefix Prefix to apply to each line of the dump.
4031 */
4032 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
4033 pw.print(prefix); pw.println("Display override configurations:");
4034 final int displayCount = mActivityDisplays.size();
4035 for (int i = 0; i < displayCount; i++) {
4036 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
4037 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
4038 pw.println(activityDisplay.getOverrideConfiguration());
4039 }
4040 }
4041
4042 /**
Winson43d1f262016-06-14 16:05:55 -07004043 * Dumps the activities matching the given {@param name} in the either the focused stack
4044 * or all visible stacks if {@param dumpVisibleStacks} is true.
4045 */
Winson Chung6998bc42017-02-28 17:07:05 -08004046 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
4047 boolean dumpFocusedStackOnly) {
4048 if (dumpFocusedStackOnly) {
4049 return mFocusedStack.getDumpActivitiesLocked(name);
4050 } else {
Winson43d1f262016-06-14 16:05:55 -07004051 ArrayList<ActivityRecord> activities = new ArrayList<>();
4052 int numDisplays = mActivityDisplays.size();
4053 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004054 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4055 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4056 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004057 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07004058 activities.addAll(stack.getDumpActivitiesLocked(name));
4059 }
4060 }
4061 }
4062 return activities;
Winson43d1f262016-06-14 16:05:55 -07004063 }
Craig Mautner20e72272013-04-01 13:45:53 -07004064 }
4065
Dianne Hackborn390517b2013-05-30 15:03:32 -07004066 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4067 boolean needSep, String prefix) {
4068 if (activity != null) {
4069 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4070 if (needSep) {
4071 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004072 }
4073 pw.print(prefix);
4074 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004075 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004076 }
4077 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004078 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004079 }
4080
Craig Mautner8d341ef2013-03-26 09:03:27 -07004081 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4082 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004083 boolean printed = false;
4084 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004085 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4086 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004087 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004088 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004089 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4090 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4091 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004092 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004093 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07004094 + ": type=" + activityTypeToString(stack.getActivityType())
4095 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004096 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08004097 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07004098
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004099 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4100 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07004101
Craig Mautner4a1cb222013-12-04 16:14:06 -08004102 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4103 !dumpAll, false, dumpPackage, true,
4104 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004105
Craig Mautner4a1cb222013-12-04 16:14:06 -08004106 needSep = printed;
4107 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4108 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004109 if (pr) {
4110 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004112 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08004113 pr = printThisActivity(pw, stack.getResumedActivity(), dumpPackage, needSep,
Craig Mautner4a1cb222013-12-04 16:14:06 -08004114 " mResumedActivity: ");
4115 if (pr) {
4116 printed = true;
4117 needSep = false;
4118 }
4119 if (dumpAll) {
4120 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4121 " mLastPausedActivity: ");
4122 if (pr) {
4123 printed = true;
4124 needSep = true;
4125 }
4126 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4127 needSep, " mLastNoHistoryActivity: ");
4128 }
4129 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004130 }
4131 }
4132
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004133 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4134 false, dumpPackage, true, " Activities waiting to finish:", null);
4135 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4136 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07004137 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
4138 false, !dumpAll, false, dumpPackage, true,
4139 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004140 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4141 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004142
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004143 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004144 }
4145
Dianne Hackborn390517b2013-05-30 15:03:32 -07004146 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004147 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004148 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004149 String innerPrefix = null;
4150 String[] args = null;
4151 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004152 for (int i=list.size()-1; i>=0; i--) {
4153 final ActivityRecord r = list.get(i);
4154 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4155 continue;
4156 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004157 if (innerPrefix == null) {
4158 innerPrefix = prefix + " ";
4159 args = new String[0];
4160 }
4161 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004162 final boolean full = !brief && (complete || !r.isInHistory());
4163 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004164 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004165 needNL = false;
4166 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004167 if (header != null) {
4168 pw.println(header);
4169 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004170 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004171 if (lastTask != r.getTask()) {
4172 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07004173 pw.print(prefix);
4174 pw.print(full ? "* " : " ");
4175 pw.println(lastTask);
4176 if (full) {
4177 lastTask.dump(pw, prefix + " ");
4178 } else if (complete) {
4179 // Complete + brief == give a summary. Isn't that obvious?!?
4180 if (lastTask.intent != null) {
4181 pw.print(prefix); pw.print(" ");
4182 pw.println(lastTask.intent.toInsecureStringWithClip());
4183 }
4184 }
4185 }
4186 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4187 pw.print(" #"); pw.print(i); pw.print(": ");
4188 pw.println(r);
4189 if (full) {
4190 r.dump(pw, innerPrefix);
4191 } else if (complete) {
4192 // Complete + brief == give a summary. Isn't that obvious?!?
4193 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4194 if (r.app != null) {
4195 pw.print(innerPrefix); pw.println(r.app);
4196 }
4197 }
4198 if (client && r.app != null && r.app.thread != null) {
4199 // flush anything that is already in the PrintWriter since the thread is going
4200 // to write to the file descriptor directly
4201 pw.flush();
4202 try {
4203 TransferPipe tp = new TransferPipe();
4204 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004205 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004206 // Short timeout, since blocking here can
4207 // deadlock with the application.
4208 tp.go(fd, 2000);
4209 } finally {
4210 tp.kill();
4211 }
4212 } catch (IOException e) {
4213 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4214 } catch (RemoteException e) {
4215 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4216 }
4217 needNL = true;
4218 }
4219 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004220 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004221 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004222
Craig Mautnerf3333272013-04-22 10:55:53 -07004223 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004224 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4225 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004226 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4227 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004228 }
4229
4230 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004231 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004232 }
4233
4234 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004235 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4236 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004237 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4238 }
4239
Craig Mautner05d29032013-05-03 13:40:13 -07004240 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004241 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4242 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4243 }
Craig Mautner05d29032013-05-03 13:40:13 -07004244 }
4245
Craig Mautner0eea92c2013-05-16 13:35:39 -07004246 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004247 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4248 }
4249
4250 final void scheduleSleepTimeout() {
4251 removeSleepTimeouts();
4252 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4253 }
4254
Craig Mautner4a1cb222013-12-04 16:14:06 -08004255 @Override
4256 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004257 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004258 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4259 }
4260
4261 @Override
4262 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004263 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4265 }
4266
4267 @Override
4268 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004269 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4271 }
4272
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004273 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004275 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004276 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 }
4278
Andrii Kulianca007a62016-11-22 16:33:28 -08004279 /** Check if display with specified id is added to the list. */
4280 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004281 return getActivityDisplayOrCreateLocked(displayId) != null;
4282 }
4283
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004284 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004285 ActivityDisplay getActivityDisplay(int displayId) {
4286 return mActivityDisplays.get(displayId);
4287 }
4288
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004289 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4290 ActivityDisplay getDefaultDisplay() {
4291 return mActivityDisplays.get(DEFAULT_DISPLAY);
4292 }
4293
Andrii Kulian62e6f252017-05-30 22:46:53 -07004294 /**
4295 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4296 * corresponding record in display manager.
4297 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004298 // TODO: Look into consolidating with getActivityDisplay()
4299 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004300 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4301 if (activityDisplay != null) {
4302 return activityDisplay;
4303 }
4304 if (mDisplayManager == null) {
4305 // The system isn't fully initialized yet.
4306 return null;
4307 }
4308 final Display display = mDisplayManager.getDisplay(displayId);
4309 if (display == null) {
4310 // The display is not registered in DisplayManager.
4311 return null;
4312 }
4313 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale45477b52018-03-06 12:24:19 -08004314 activityDisplay = new ActivityDisplay(this, display);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004315 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004316 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4317 mWindowManager.onDisplayAdded(displayId);
4318 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004319 }
4320
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004321 @VisibleForTesting
4322 void attachDisplay(ActivityDisplay display) {
4323 mActivityDisplays.put(display.mDisplayId, display);
4324 }
4325
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004326 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004327 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004328 mService.mContext.getResources().getDimensionPixelSize(
4329 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004330 }
4331
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004332 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004333 if (displayId == DEFAULT_DISPLAY) {
4334 throw new IllegalArgumentException("Can't remove the primary display.");
4335 }
4336
Craig Mautner4a1cb222013-12-04 16:14:06 -08004337 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004338 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4339 if (activityDisplay == null) {
4340 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004341 }
Bryce Leef19cbe22018-02-02 15:09:21 -08004342
4343 activityDisplay.remove();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004344
4345 releaseSleepTokens(activityDisplay);
4346
4347 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004349 }
4350
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004351 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004352 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004353 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004354 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004355 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004356 // The window policy is responsible for stopping activities on the default display
4357 if (displayId != Display.DEFAULT_DISPLAY) {
4358 int displayState = activityDisplay.mDisplay.getState();
4359 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4360 activityDisplay.mOffToken =
4361 mService.acquireSleepToken("Display-off", displayId);
4362 } else if (displayState == Display.STATE_ON
4363 && activityDisplay.mOffToken != null) {
4364 activityDisplay.mOffToken.release();
4365 activityDisplay.mOffToken = null;
4366 }
4367 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004368
4369 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004370 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004371 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004372 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004373 }
4374
David Stevens9440dc82017-03-16 19:00:20 -07004375 SleepToken createSleepTokenLocked(String tag, int displayId) {
4376 ActivityDisplay display = mActivityDisplays.get(displayId);
4377 if (display == null) {
4378 throw new IllegalArgumentException("Invalid display: " + displayId);
4379 }
4380
4381 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4382 mSleepTokens.add(token);
4383 display.mAllSleepTokens.add(token);
4384 return token;
4385 }
4386
4387 private void removeSleepTokenLocked(SleepTokenImpl token) {
4388 mSleepTokens.remove(token);
4389
4390 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4391 if (display != null) {
4392 display.mAllSleepTokens.remove(token);
4393 if (display.mAllSleepTokens.isEmpty()) {
4394 mService.updateSleepIfNeededLocked();
4395 }
4396 }
4397 }
4398
4399 private void releaseSleepTokens(ActivityDisplay display) {
4400 if (display.mAllSleepTokens.isEmpty()) {
4401 return;
4402 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004403 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004404 mSleepTokens.remove(token);
4405 }
4406 display.mAllSleepTokens.clear();
4407
4408 mService.updateSleepIfNeededLocked();
4409 }
4410
Wale Ogunwale68278562017-09-23 17:13:55 -07004411 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004412 final int displayId = stack.mDisplayId;
4413 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004414 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004415 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004416 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004417 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004418 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004419 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004420 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004421 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004422 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004423
4424 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4425 final int numTasks = tasks.size();
4426 int[] taskIds = new int[numTasks];
4427 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004428 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004429 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004430 for (int i = 0; i < numTasks; ++i) {
4431 final TaskRecord task = tasks.get(i);
4432 taskIds[i] = task.taskId;
4433 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4434 : task.realActivity != null ? task.realActivity.flattenToString()
4435 : task.getTopActivity() != null ? task.getTopActivity().packageName
4436 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004437 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004438 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004439 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004440 }
4441 info.taskIds = taskIds;
4442 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004443 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004444 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004445
4446 final ActivityRecord top = stack.topRunningActivityLocked();
4447 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 return info;
4449 }
4450
Wale Ogunwale68278562017-09-23 17:13:55 -07004451 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004452 ActivityStack stack = getStack(stackId);
4453 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004454 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004455 }
4456 return null;
4457 }
4458
Wale Ogunwale68278562017-09-23 17:13:55 -07004459 StackInfo getStackInfo(int windowingMode, int activityType) {
4460 final ActivityStack stack = getStack(windowingMode, activityType);
4461 return (stack != null) ? getStackInfo(stack) : null;
4462 }
4463
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004465 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004466 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004467 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4468 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4469 final ActivityStack stack = display.getChildAt(stackNdx);
4470 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471 }
4472 }
4473 return list;
4474 }
4475
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004476 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004477 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004478 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004479 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004480 }
4481
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004482 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004483 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004484 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004485 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004486 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004487 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004488 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004489 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004490 return;
4491 }
4492
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004493 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004494 if (isSecondaryDisplayPreferred) {
4495 final int actualDisplayId = task.getStack().mDisplayId;
4496 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004497 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004498 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004499 // The task might have landed on a display different from requested.
4500 // TODO(multi-display): Find proper stack for the task on the default display.
4501 mService.setTaskWindowingMode(task.taskId,
4502 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4503 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004504 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4505 // display with config different from global config.
4506 mService.mTaskChangeNotificationController
4507 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004508 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004509 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004510 }
4511
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004512 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004513 // Display a warning toast that we tried to put an app that doesn't support split-screen
4514 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004515 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004516
Andrii Kulianc27916642016-04-12 17:59:27 -07004517 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4518 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004519
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004520 final ActivityStack dockedStack =
4521 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004522 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004523 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004524 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004525 return;
4526 }
4527
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004528 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004529 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4530 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004531 final String packageName = topActivity.appInfo.packageName;
4532 final int reason = isSecondaryDisplayPreferred
4533 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4534 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004535 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004536 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004537 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004538 }
4539
Jorim Jaggife89d122015-12-22 16:28:44 +01004540 void activityRelaunchedLocked(IBinder token) {
4541 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004542 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4543 if (r != null) {
4544 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004545 r.setSleeping(true, true);
4546 }
4547 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004548 }
4549
4550 void activityRelaunchingLocked(ActivityRecord r) {
4551 mWindowManager.notifyAppRelaunching(r.appToken);
4552 }
4553
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004554 void logStackState() {
4555 mActivityMetricsLogger.logWindowState();
4556 }
4557
Winson Chung5af42fc2017-03-24 17:11:33 -07004558 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004559 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4560 // end, then ensure that we defer all in between multi-window mode changes
4561 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4562 return;
4563 }
4564
Wale Ogunwale22e25262016-02-01 10:32:02 -08004565 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4566 final ActivityRecord r = task.mActivities.get(i);
4567 if (r.app != null && r.app.thread != null) {
4568 mMultiWindowModeChangedActivities.add(r);
4569 }
4570 }
4571
4572 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4573 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4574 }
4575 }
4576
Winson Chung5af42fc2017-03-24 17:11:33 -07004577 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004578 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004579 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004580 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004581 return;
4582 }
4583
Bryce Leef3c6a472017-11-14 14:53:06 -08004584 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004585 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004586
Winson Chungab76bbc2017-08-14 13:33:51 -07004587 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4588 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4589 final ActivityRecord r = task.mActivities.get(i);
4590 if (r.app != null && r.app.thread != null) {
4591 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004592 // If we are scheduling pip change, then remove this activity from multi-window
4593 // change list as the processing of pip change will make sure multi-window changed
4594 // message is processed in the right order relative to pip changed.
4595 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004596 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004597 }
4598 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004599
Winson Chungab76bbc2017-08-14 13:33:51 -07004600 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4601 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4602 }
4603 }
4604
4605 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4606 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4607 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4608 final ActivityRecord r = task.mActivities.get(i);
4609 if (r.app != null && r.app.thread != null) {
4610 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004611 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004612 }
4613 }
4614
Tony Mak853304c2016-04-18 15:17:41 +01004615 void setDockedStackMinimized(boolean minimized) {
4616 mIsDockMinimized = minimized;
Wale Ogunwale17696de2018-03-15 16:41:05 -07004617 if (mIsDockMinimized) {
4618 final ActivityStack current = getFocusedStack();
4619 if (current.inSplitScreenPrimaryWindowingMode()) {
4620 // The primary split-screen stack can't be focused while it is minimize, so move
4621 // focus to something else.
4622 current.adjustFocusToNextFocusableStack("setDockedStackMinimized");
4623 }
4624 }
Tony Mak853304c2016-04-18 15:17:41 +01004625 }
4626
chaviw59b98852017-06-13 12:05:44 -07004627 void wakeUp(String reason) {
4628 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4629 }
4630
4631 /**
4632 * Begin deferring resume to avoid duplicate resumes in one pass.
4633 */
4634 private void beginDeferResume() {
4635 mDeferResumeCount++;
4636 }
4637
4638 /**
4639 * End deferring resume and determine if resume can be called.
4640 */
4641 private void endDeferResume() {
4642 mDeferResumeCount--;
4643 }
4644
4645 /**
4646 * @return True if resume can be called.
4647 */
4648 private boolean readyToResume() {
4649 return mDeferResumeCount == 0;
4650 }
4651
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004652 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004653
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004654 public ActivityStackSupervisorHandler(Looper looper) {
4655 super(looper);
4656 }
4657
Winson Chung4dabf232017-01-25 13:25:22 -08004658 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004659 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004660 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4661 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004662 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004663 }
4664
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004665 @Override
4666 public void handleMessage(Message msg) {
4667 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004668 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4669 synchronized (mService) {
4670 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4671 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004672 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004673 }
4674 }
4675 } break;
4676 case REPORT_PIP_MODE_CHANGED_MSG: {
4677 synchronized (mService) {
4678 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4679 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004680 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4681 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004682 }
4683 }
4684 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004685 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004686 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4687 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004688 // We don't at this point know if the activity is fullscreen,
4689 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004690 activityIdleInternal((ActivityRecord) msg.obj,
4691 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004692 } break;
4693 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004694 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004695 activityIdleInternal((ActivityRecord) msg.obj,
4696 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004697 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004698 case RESUME_TOP_ACTIVITY_MSG: {
4699 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004700 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004701 }
4702 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004703 case SLEEP_TIMEOUT_MSG: {
4704 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004705 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004706 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004707 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004708 }
4709 }
4710 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004711 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004712 synchronized (mService) {
4713 if (mLaunchingActivity.isHeld()) {
4714 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4715 if (VALIDATE_WAKE_LOCK_CALLER
4716 && Binder.getCallingUid() != Process.myUid()) {
4717 throw new IllegalStateException("Calling must be system uid");
4718 }
4719 mLaunchingActivity.release();
4720 }
4721 }
4722 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004723 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004724 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004725 } break;
4726 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004727 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004728 } break;
4729 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004730 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004731 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004732 case LAUNCH_TASK_BEHIND_COMPLETE: {
4733 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004734 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004735 if (r != null) {
4736 handleLaunchTaskBehindCompleteLocked(r);
4737 }
4738 }
4739 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004740
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004741 }
4742 }
4743 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004744
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004745 ActivityStack findStackBehind(ActivityStack stack) {
4746 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004747 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004748 if (display == null) {
4749 return null;
4750 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004751 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4752 if (display.getChildAt(i) == stack && i > 0) {
4753 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004754 }
4755 }
4756 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004757 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004758 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004759
Jorim Jaggic69bd222016-03-15 14:38:37 +01004760 /**
4761 * Puts a task into resizing mode during the next app transition.
4762 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004763 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004764 */
Bryce Leed3624e12017-11-30 08:51:45 -08004765 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004766 mResizingTasksDuringAnimation.add(task.taskId);
4767 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004768 }
4769
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004770 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4771 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004772 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004773 final String callingPackage;
4774 final Intent intent;
4775 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004776 int activityType = ACTIVITY_TYPE_UNDEFINED;
4777 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004778 final ActivityOptions activityOptions = options != null
4779 ? options.getOptions(this)
4780 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004781 if (activityOptions != null) {
4782 activityType = activityOptions.getLaunchActivityType();
4783 windowingMode = activityOptions.getLaunchWindowingMode();
4784 }
4785 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004786 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004787 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004788 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004789
Matthew Ng606dd802017-06-05 14:06:32 -07004790 mWindowManager.deferSurfaceLayout();
4791 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004792 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004793 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004794 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004795
Matthew Ng606dd802017-06-05 14:06:32 -07004796 // Defer updating the stack in which recents is until the app transition is done, to
4797 // not run into issues where we still need to draw the task in recents but the
4798 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08004799 deferUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004800 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004801 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004802
Matthew Ng606dd802017-06-05 14:06:32 -07004803 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004804 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004805 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08004806 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004807 mWindowManager.executeAppTransition();
4808 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004809 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004810 }
4811
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004812 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4813 // We always want to return to the home activity instead of the recents activity
4814 // from whatever is started from the recents activity, so move the home stack
4815 // forward.
4816 moveHomeStackToFront("startActivityFromRecents");
4817 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004818
Matthew Ng606dd802017-06-05 14:06:32 -07004819 // If the user must confirm credentials (e.g. when first launching a work app and the
4820 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4821 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4822 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004823 final ActivityRecord targetActivity = task.getTopActivity();
4824
Bryce Leed3624e12017-11-30 08:51:45 -08004825 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004826 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004827 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004828 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004829 true /* fromRecents */);
4830 } finally {
4831 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4832 targetActivity);
4833 }
Matthew Ng606dd802017-06-05 14:06:32 -07004834
Bryce Leed3624e12017-11-30 08:51:45 -08004835 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4836 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4837 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004838 return ActivityManager.START_TASK_TO_FRONT;
4839 }
Matthew Ng606dd802017-06-05 14:06:32 -07004840 callingPackage = task.mCallingPackage;
4841 intent = task.intent;
4842 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4843 userId = task.userId;
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004844 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004845 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07004846 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinski3eab9512018-07-20 15:32:00 +01004847 false /* validateIncomingUser */, null /* originatingPendingIntent */);
Matthew Ng606dd802017-06-05 14:06:32 -07004848 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004849 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4850 // If we are launching the task in the docked stack, put it into resizing mode so
4851 // the window renders full-screen with the background filling the void. Also only
4852 // call this at the end to make sure that tasks exists on the window manager side.
4853 setResizingDuringAnimation(task);
4854
4855 final ActivityDisplay display = task.getStack().getDisplay();
4856 final ActivityStack topSecondaryStack =
4857 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4858 if (topSecondaryStack.isActivityTypeHome()) {
4859 // If the home activity if the top split-screen secondary stack, then the
4860 // primary split-screen stack is in the minimized mode which means it can't
4861 // receive input keys, so we should move the focused app to the home app so that
4862 // window manager can correctly calculate the focus window that can receive
4863 // input keys.
4864 moveHomeStackToFront("startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08004865
4866 // Immediately update the minimized docked stack mode, the upcoming animation
4867 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
4868 // will do the animation to the target bounds
4869 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004870 }
4871 }
Matthew Ng606dd802017-06-05 14:06:32 -07004872 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004873 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004874 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004875
4876 /**
4877 * @return a list of activities which are the top ones in each visible stack. The first
4878 * entry will be the focused activity.
4879 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004880 List<IBinder> getTopVisibleActivities() {
4881 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4882 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004883 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004884 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4885 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004886 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4887 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004888 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004889 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004890 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004891 if (top != null) {
4892 if (stack == mFocusedStack) {
4893 topActivityTokens.add(0, top.appToken);
4894 } else {
4895 topActivityTokens.add(top.appToken);
4896 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004897 }
4898 }
4899 }
4900 }
4901 return topActivityTokens;
4902 }
Bryce Lee4a194382017-04-04 14:32:48 -07004903
4904 /**
4905 * Internal container to store a match qualifier alongside a WaitResult.
4906 */
4907 static class WaitInfo {
4908 private final ComponentName mTargetComponent;
4909 private final WaitResult mResult;
Vishnu Naircf235042018-11-02 13:27:00 -07004910 /** Time stamp when we started to wait for {@link WaitResult}. */
4911 private final long mStartTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07004912
Vishnu Naircf235042018-11-02 13:27:00 -07004913 WaitInfo(ComponentName targetComponent, WaitResult result, long startTimeMs) {
Bryce Lee4a194382017-04-04 14:32:48 -07004914 this.mTargetComponent = targetComponent;
4915 this.mResult = result;
Vishnu Naircf235042018-11-02 13:27:00 -07004916 this.mStartTimeMs = startTimeMs;
Bryce Lee4a194382017-04-04 14:32:48 -07004917 }
4918
Wale Ogunwale3270f172017-04-26 07:29:42 -07004919 public boolean matches(ComponentName targetComponent) {
4920 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004921 }
4922
4923 public WaitResult getResult() {
4924 return mResult;
4925 }
4926
Vishnu Naircf235042018-11-02 13:27:00 -07004927 public long getStartTime() {
4928 return mStartTimeMs;
4929 }
4930
Bryce Lee4a194382017-04-04 14:32:48 -07004931 public ComponentName getComponent() {
4932 return mTargetComponent;
4933 }
4934
4935 public void dump(PrintWriter pw, String prefix) {
4936 pw.println(prefix + "WaitInfo:");
4937 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4938 pw.println(prefix + " mResult=");
4939 mResult.dump(pw, prefix);
4940 }
4941 }
David Stevens9440dc82017-03-16 19:00:20 -07004942
4943 private final class SleepTokenImpl extends SleepToken {
4944 private final String mTag;
4945 private final long mAcquireTime;
4946 private final int mDisplayId;
4947
4948 public SleepTokenImpl(String tag, int displayId) {
4949 mTag = tag;
4950 mDisplayId = displayId;
4951 mAcquireTime = SystemClock.uptimeMillis();
4952 }
4953
4954 @Override
4955 public void release() {
4956 synchronized (mService) {
4957 removeSleepTokenLocked(this);
4958 }
4959 }
4960
4961 @Override
4962 public String toString() {
4963 return "{\"" + mTag + "\", display " + mDisplayId
4964 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4965 }
4966 }
4967
Craig Mautner27084302013-03-25 08:05:25 -07004968}