blob: 2ae056fe18a54763f3f3bdb56f9ad6f56f19097a [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Andrii Kulian3c9ad072017-08-01 11:45:22 -070019import static android.Manifest.permission.ACTIVITY_EMBEDDING;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010020import static android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070021import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020022import static android.Manifest.permission.START_ANY_ACTIVITY;
23import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Charles Heff9b4dff2017-09-22 10:18:37 +010024import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
Bryce Lee5f0e28f2018-01-30 16:00:03 -080025import static android.app.ActivityManager.START_DELIVERED_TO_TOP;
Jorim Jaggife762342016-10-13 14:33:27 +020026import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070028import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
29import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070031import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070032import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070033import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
34import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070036import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
39import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070042import static android.app.WindowConfiguration.activityTypeToString;
43import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070044import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020045import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Adrian Roosa6d6aab2018-04-19 18:58:22 +020046import static android.graphics.Rect.copyOrNull;
chaviw59b98852017-06-13 12:05:44 -070047import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010048import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020049import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
50import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070051import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070052import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070053
Winson Chung47900652017-04-06 18:44:25 -070054import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020056import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020058import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
64import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020065import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020067import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
75import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
76import static com.android.server.am.ActivityManagerService.ANIMATE;
77import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020078import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
79import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
80import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
82import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
83import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
85import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
86import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070087import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020088import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
89import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010090import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080091import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
92import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
93import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070094import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
95import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
96import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
97import static com.android.server.am.ActivityStackSupervisorProto.IS_HOME_RECENTS_COMPONENT;
98import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
99import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100100import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -0700101
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800102import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200103
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800105import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700106import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700107import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100108import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700109import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800111import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700113import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700115import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700118import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700119import android.app.WindowConfiguration.ActivityType;
120import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800121import android.app.servertransaction.ActivityLifecycleItem;
122import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700123import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800124import android.app.servertransaction.PauseActivityItem;
125import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700127import android.content.Context;
128import android.content.Intent;
129import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700131import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.content.pm.PackageManager;
133import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100134import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800136import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800137import android.hardware.display.DisplayManager;
138import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800139import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800140import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700141import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100142import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700143import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700144import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700145import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700146import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700147import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700148import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700151import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700152import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700153import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100154import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700155import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700157import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700158import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700159import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700160import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800161import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700162import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700163import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800164import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800165import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700166import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700167import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800168import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800169
Andreas Gampea36dc622018-02-05 17:19:22 -0800170import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700171import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800172import com.android.internal.content.ReferrerIntent;
Chenjie Yu52cacc62017-12-08 18:11:45 -0800173import com.android.internal.os.logging.MetricsLoggerWrapper;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700174import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700175import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800176import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700177import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700178import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700179import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700180import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700181
Craig Mautner8d341ef2013-03-26 09:03:27 -0700182import java.io.FileDescriptor;
183import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700184import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800185import java.lang.annotation.Retention;
186import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700187import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700188import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700189import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700190import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700191
Winson Chung1dbc8112017-09-28 18:05:31 -0700192public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
193 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700195 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700198 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700200 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700202 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800203 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800204
Craig Mautnerf3333272013-04-22 10:55:53 -0700205 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700206 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700207
Craig Mautner0eea92c2013-05-16 13:35:39 -0700208 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700209 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700210
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700211 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700212 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700213
Craig Mautner05d29032013-05-03 13:40:13 -0700214 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
215 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
216 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700217 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700218 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
220 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
221 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700222 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800223 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
224 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800225
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700227
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228 // Used to indicate if an object (e.g. stack) that we are trying to get
229 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800230 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700231
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700232 // Used to indicate that windows of activities should be preserved during the resize.
233 static final boolean PRESERVE_WINDOWS = true;
234
Wale Ogunwale040b4702015-08-06 18:10:50 -0700235 // Used to indicate if an object (e.g. task) should be moved/created
236 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700237 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700238
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700239 // Don't execute any calls to resume.
240 static final boolean DEFER_RESUME = true;
241
Winson Chung010927a2016-12-15 16:12:35 -0800242 // Used to indicate that a task is removed it should also be removed from recents.
243 static final boolean REMOVE_FROM_RECENTS = true;
244
Winson Chung6954fc92017-03-24 16:22:12 -0700245 // Used to indicate that pausing an activity should occur immediately without waiting for
246 // the activity callback indicating that it has completed pausing
247 static final boolean PAUSE_IMMEDIATELY = true;
248
Adrian Roosa6d6aab2018-04-19 18:58:22 +0200249 /** True if the docked stack is currently being resized. */
250 private boolean mDockedStackResizing;
251
252 /**
253 * True if there are pending docked bounds that need to be applied after
254 * {@link #mDockedStackResizing} is reset to false.
255 */
256 private boolean mHasPendingDockedBounds;
257 private Rect mPendingDockedBounds;
258 private Rect mPendingTempDockedTaskBounds;
259 private Rect mPendingTempDockedTaskInsetBounds;
260 private Rect mPendingTempOtherTaskBounds;
261 private Rect mPendingTempOtherTaskInsetBounds;
262
Winson Chung7900bee2017-03-10 08:59:25 -0800263 /**
264 * The modes which affect which tasks are returned when calling
265 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
266 */
267 @Retention(RetentionPolicy.SOURCE)
268 @IntDef({
269 MATCH_TASK_IN_STACKS_ONLY,
270 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
271 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
272 })
273 public @interface AnyTaskForIdMatchTaskMode {}
274 // Match only tasks in the current stacks
275 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
276 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
277 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
278 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
279 // provided stack id
280 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
281
Svetoslav7008b512015-06-24 18:47:07 -0700282 // Activity actions an app cannot start if it uses a permission which is not granted.
283 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
284 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700285
Svetoslav7008b512015-06-24 18:47:07 -0700286 static {
287 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
288 Manifest.permission.CAMERA);
289 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
290 Manifest.permission.CAMERA);
291 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
292 Manifest.permission.CALL_PHONE);
293 }
294
Svet Ganov99b60432015-06-27 13:15:22 -0700295 /** Action restriction: launching the activity is not restricted. */
296 private static final int ACTIVITY_RESTRICTION_NONE = 0;
297 /** Action restriction: launching the activity is restricted by a permission. */
298 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
299 /** Action restriction: launching the activity is restricted by an app op. */
300 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700301
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700302 // For debugging to make sure the caller when acquiring/releasing our
303 // wake lock is the system process.
304 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800305 /** The number of distinct task ids that can be assigned to the tasks of a single user */
306 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700307
Craig Mautner27084302013-03-25 08:05:25 -0700308 final ActivityManagerService mService;
309
Winson Chung61c9e5a2017-10-11 10:39:32 -0700310 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700311 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800312
Winson Chung61c9e5a2017-10-11 10:39:32 -0700313 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700314 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700315
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700316 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000317 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318
319 /** Short cut */
320 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700322
Bryce Leeec55eb02017-12-05 20:51:27 -0800323 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700324
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800325 /**
326 * Maps the task identifier that activities are currently being started in to the userId of the
327 * task. Each time a new task is created, the entry for the userId of the task is incremented
328 */
329 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700330
Craig Mautner2420ead2013-04-01 17:13:20 -0700331 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800332 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700333
Craig Mautnere0a38842013-12-16 16:14:02 -0800334 /** The stack containing the launcher app. Assumed to always be attached to
335 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800336 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700337
Craig Mautnere0a38842013-12-16 16:14:02 -0800338 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800339 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700340
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
342 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800343 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800344 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700345
346 /** List of activities that are waiting for a new activity to become visible before completing
347 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700348 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
349 // mStoppingActivities when something else comes up.
350 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700351
Bryce Lee4a194382017-04-04 14:32:48 -0700352 /** List of processes waiting to find out when a specific activity becomes visible. */
353 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700354
355 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700356 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700357
Craig Mautnerde4ef022013-04-07 19:01:33 -0700358 /** List of activities that are ready to be stopped, but waiting for the next activity to
359 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800360 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361
Craig Mautnerf3333272013-04-22 10:55:53 -0700362 /** List of activities that are ready to be finished, but waiting for the previous activity to
363 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800364 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800367 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700368
Wale Ogunwale22e25262016-02-01 10:32:02 -0800369 /** List of activities whose multi-window mode changed that we need to report to the
370 * application */
371 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
372
373 /** List of activities whose picture-in-picture mode changed that we need to report to the
374 * application */
375 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
376
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100377 /**
378 * Animations that for the current transition have requested not to
379 * be considered for the transition animation.
380 */
381 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
382
Winson Chung5af42fc2017-03-24 17:11:33 -0700383 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
384 * the application */
385 Rect mPipModeChangedTargetStackBounds;
386
Craig Mautnerf3333272013-04-22 10:55:53 -0700387 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700388 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700389
Craig Mautnerde4ef022013-04-07 19:01:33 -0700390 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
391 * is being brought in front of us. */
392 boolean mUserLeaving = false;
393
Bryce Leed3624e12017-11-30 08:51:45 -0800394 /** Set when a power hint has started, but not ended. */
395 private boolean mPowerHintSent;
396
Craig Mautner0eea92c2013-05-16 13:35:39 -0700397 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700398 * We don't want to allow the device to go to sleep while in the process
399 * of launching an activity. This is primarily to allow alarm intent
400 * receivers to launch an activity and get that to run before the device
401 * goes back to sleep.
402 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700403 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404
405 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700406 * Set when the system is going to sleep, until we have
407 * successfully paused the current activity and released our wake lock.
408 * At that point the system is allowed to actually sleep.
409 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700411
David Stevens9440dc82017-03-16 19:00:20 -0700412 /**
413 * A list of tokens that cause the top activity to be put to sleep.
414 * They are used by components that may hide and block interaction with underlying
415 * activities.
416 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700417 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700418
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700419 /** Stack id of the front stack when user switched, indexed by userId. */
420 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700421
Bryce Leeaf3a4002017-03-20 10:37:12 -0700422 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700424 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800426 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
427
428 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800429
Wale Ogunwaled046a012015-12-24 13:05:59 -0800430 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800431 boolean inResumeTopActivity;
432
Andrii Kulian1e32e022016-09-16 15:29:34 -0700433 /**
434 * Temporary rect used during docked stack resize calculation so we don't need to create a new
435 * object each time.
436 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700437 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700438 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700439
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700440 // The default minimal size that will be used if the activity doesn't specify its minimal size.
441 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700442 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700443
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700444 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
445 private boolean mTaskLayersChanged = true;
446
Bryce Lee2a3cc462017-10-27 10:57:35 -0700447 private ActivityMetricsLogger mActivityMetricsLogger;
Jorim Jaggide8305a2018-05-15 12:06:35 -0700448 private LaunchTimeTracker mLaunchTimeTracker = new LaunchTimeTracker();
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800449
Jorim Jaggiea039a82017-08-02 14:37:49 +0200450 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
451
Andrii Kulian1779e612016-10-12 21:58:25 -0700452 @Override
453 protected int getChildCount() {
454 return mActivityDisplays.size();
455 }
456
457 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700458 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700459 return mActivityDisplays.valueAt(index);
460 }
461
462 @Override
463 protected ConfigurationContainer getParent() {
464 return null;
465 }
466
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700467 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700468 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700469 if (activityDisplay == null) {
470 throw new IllegalArgumentException("No display found with id: " + displayId);
471 }
472
473 return activityDisplay.getOverrideConfiguration();
474 }
475
476 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700477 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700478 if (activityDisplay == null) {
479 throw new IllegalArgumentException("No display found with id: " + displayId);
480 }
481
482 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
483 }
484
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700485 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700486 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
487 int callingUid, ActivityInfo activityInfo) {
488 if (displayId == DEFAULT_DISPLAY) {
489 // No restrictions for the default display.
490 return true;
491 }
492 if (!mService.mSupportsMultiDisplay) {
493 // Can't launch on secondary displays if feature is not supported.
494 return false;
495 }
496 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
497 // Can't apply wrong configuration to non-resizeable activities.
498 return false;
499 }
500 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
501 // Can't place activities to a display that has restricted launch rules.
502 // In this case the request should be made by explicitly adding target display id and
503 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
504 return false;
505 }
506 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700507 }
508
509 /**
510 * Check if configuration of specified display matches current global config.
511 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
512 * the same config as on the default display.
513 * @param displayId Id of the display to check.
514 * @return {@code true} if configuration matches.
515 */
516 private boolean displayConfigMatchesGlobal(int displayId) {
517 if (displayId == DEFAULT_DISPLAY) {
518 return true;
519 }
520 if (displayId == INVALID_DISPLAY) {
521 return false;
522 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700523 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700524 if (targetDisplay == null) {
525 throw new IllegalArgumentException("No display found with id: " + displayId);
526 }
527 return getConfiguration().equals(targetDisplay.getConfiguration());
528 }
529
Wale Ogunwale39381972015-12-17 17:15:29 -0800530 static class FindTaskResult {
531 ActivityRecord r;
532 boolean matchedByRootAffinity;
533 }
534 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
535
Craig Mautneree36c772014-07-16 14:56:05 -0700536 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800537 * Temp storage for display ids sorted in focus order.
538 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
539 * it's more efficient, as the number of displays is usually small.
540 */
541 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
542
543 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100544 * Used to keep track whether app visibilities got changed since the last pause. Useful to
545 * determine whether to invoke the task stack change listener after pausing.
546 */
547 boolean mAppVisibilitiesChangedSinceLastPause;
548
549 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100550 * Set of tasks that are in resizing mode during an app transition to fill the "void".
551 */
552 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
553
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700554
555 /**
556 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
557 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
558 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
559 * like the docked stack going empty.
560 */
561 private boolean mAllowDockedStackResize = true;
562
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100563 /**
Tony Mak853304c2016-04-18 15:17:41 +0100564 * Is dock currently minimized.
565 */
566 boolean mIsDockMinimized;
567
Bryce Lee2a3cc462017-10-27 10:57:35 -0700568 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200569
chaviw59b98852017-06-13 12:05:44 -0700570 private PowerManager mPowerManager;
571 private int mDeferResumeCount;
572
Bryce Lee2a3cc462017-10-27 10:57:35 -0700573 private boolean mInitialized;
574
Tony Mak853304c2016-04-18 15:17:41 +0100575 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700576 * Description of a request to start a new activity, which has been held
577 * due to app switches being disabled.
578 */
579 static class PendingActivityLaunch {
580 final ActivityRecord r;
581 final ActivityRecord sourceRecord;
582 final int startFlags;
583 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700584 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700585
586 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700587 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700588 r = _r;
589 sourceRecord = _sourceRecord;
590 startFlags = _startFlags;
591 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700592 callerApp = _callerApp;
593 }
594
595 void sendErrorResult(String message) {
596 try {
597 if (callerApp.thread != null) {
598 callerApp.thread.scheduleCrash(message);
599 }
600 } catch (RemoteException e) {
601 Slog.e(TAG, "Exception scheduling crash of failed "
602 + "activity launcher sourceRecord=" + sourceRecord, e);
603 }
Craig Mautneree36c772014-07-16 14:56:05 -0700604 }
605 }
606
Bryce Leeaf691c02017-03-20 14:20:22 -0700607 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700608 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000609 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700610 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700611 }
612
613 public void initialize() {
614 if (mInitialized) {
615 return;
616 }
617
618 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700619 mRunningTasks = createRunningTasks();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700620 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
621 mHandler.getLooper());
622 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700623
Bryce Leeec55eb02017-12-05 20:51:27 -0800624 mLaunchParamsController = new LaunchParamsController(mService);
625 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700626 }
627
Bryce Lee2a3cc462017-10-27 10:57:35 -0700628
629 public ActivityMetricsLogger getActivityMetricsLogger() {
630 return mActivityMetricsLogger;
631 }
632
Jorim Jaggide8305a2018-05-15 12:06:35 -0700633 LaunchTimeTracker getLaunchTimeTracker() {
634 return mLaunchTimeTracker;
635 }
636
Bryce Lee2a3cc462017-10-27 10:57:35 -0700637 public KeyguardController getKeyguardController() {
638 return mKeyguardController;
639 }
640
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800641 void setRecentTasks(RecentTasks recentTasks) {
642 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700643 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800644 }
645
Winson Chung3f0e59a2017-10-25 10:19:05 -0700646 @VisibleForTesting
647 RunningTasks createRunningTasks() {
648 return new RunningTasks();
649 }
650
Jeff Brown2c43c332014-06-12 22:38:59 -0700651 /**
652 * At the time when the constructor runs, the power manager has not yet been
653 * initialized. So we initialize our wakelocks afterwards.
654 */
655 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700656 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
657 mGoingToSleep = mPowerManager
658 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
659 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700660 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700661 }
662
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700663 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 synchronized (mService) {
665 mWindowManager = wm;
Bryce Lee459c0622018-03-19 11:04:01 -0700666 getKeyguardController().setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667
668 mDisplayManager =
669 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
670 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800671 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672
673 Display[] displays = mDisplayManager.getDisplays();
674 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwale45477b52018-03-06 12:24:19 -0800675 final Display display = displays[displayNdx];
676 ActivityDisplay activityDisplay = new ActivityDisplay(this, display);
677 mActivityDisplays.put(display.getDisplayId(), activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700678 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 }
680
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700681 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
682 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 }
Craig Mautner27084302013-03-25 08:05:25 -0700684 }
685
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700686 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700688 }
689
Bryce Lee7b851cc2018-04-10 14:53:13 -0700690 boolean isFocusable(ConfigurationContainer container, boolean alwaysFocusable) {
691 if (container.inSplitScreenPrimaryWindowingMode() && mIsDockMinimized) {
692 return false;
693 }
694
695 return container.getWindowConfiguration().canReceiveKeys() || alwaysFocusable;
696 }
697
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700700 }
701
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700702 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700703 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700704 }
705
Wale Ogunwaled046a012015-12-24 13:05:59 -0800706 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800707 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
708 if (!focusCandidate.isFocusable()) {
709 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800710 focusCandidate = getNextFocusableStackLocked(focusCandidate, false /* ignoreCurrent */);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800711 }
712
713 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800714 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800715 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800716
Wale Ogunwaled046a012015-12-24 13:05:59 -0800717 EventLogTags.writeAmFocusedStack(
718 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
719 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
720 }
721
722 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800723 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800724 if (r != null && r.idle) {
725 checkFinishBootingLocked();
726 }
727 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700730 void moveHomeStackToFront(String reason) {
731 mHomeStack.moveToFront(reason);
732 }
733
Matthew Ng330757d2017-02-28 14:19:17 -0800734 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700735 final ActivityStack recentsStack = getDefaultDisplay().getStack(
736 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800737 if (recentsStack != null) {
738 recentsStack.moveToFront(reason);
739 }
740 }
741
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700742 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800743 boolean moveHomeStackTaskToTop(String reason) {
744 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700745
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700746 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700747 if (top == null) {
748 return false;
749 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700750 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700751 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700752 }
753
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800754 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700755 if (!mService.mBooting && !mService.mBooted) {
756 // Not ready yet!
757 return false;
758 }
759
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800760 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700761 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800762 final String myReason = reason + " resumeHomeStackTask";
763
Mark Lua56ea122015-10-08 13:31:01 +0800764 // Only resume home activity if isn't finishing.
765 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700766 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800767 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700768 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800769 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700770 }
771
Craig Mautner8d341ef2013-03-26 09:03:27 -0700772 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700773 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
774 }
775
776 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700777 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700778 }
779
780 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700781 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700782 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800783 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700784 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700785 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700786 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700787 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700788 @Nullable ActivityOptions aOptions, boolean onTop) {
789 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700790 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
791 throw new IllegalArgumentException("Should not specify activity options for non-restore"
792 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800793 }
794
Craig Mautnere0a38842013-12-16 16:14:02 -0800795 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800796 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700797 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
798 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
799 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700800 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700801 if (task == null) {
802 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800803 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700804 if (aOptions != null) {
805 // Resolve the stack the task should be placed in now based on options
806 // and reparent if needed.
807 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
808 if (launchStack != null && stack != launchStack) {
809 final int reparentMode = onTop
810 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
811 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
812 "anyTaskForIdLocked");
813 }
814 }
815 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700816 }
817 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800818
Winson Chung7900bee2017-03-10 08:59:25 -0800819 // If we are matching stack tasks only, return now
820 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800821 return null;
822 }
823
Winson Chung7900bee2017-03-10 08:59:25 -0800824 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
825 // the task from recents
826 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700827 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700828
829 if (task == null) {
830 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800831 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
832 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700833
834 return null;
835 }
836
837 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700838 return task;
839 }
840
Winson Chung7900bee2017-03-10 08:59:25 -0800841 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700842 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700843 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
844 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800845 return null;
846 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700847 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800848 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700849 }
850
Craig Mautner6170f732013-04-02 13:05:23 -0700851 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700854 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
855 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
856 final ActivityStack stack = display.getChildAt(stackNdx);
857 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 if (r != null) {
859 return r;
860 }
Craig Mautner6170f732013-04-02 13:05:23 -0700861 }
862 }
863 return null;
864 }
865
Tony Mak853304c2016-04-18 15:17:41 +0100866 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000867 * Detects whether we should show a lock screen in front of this task for a locked user.
868 * <p>
869 * We'll do this if either of the following holds:
870 * <ul>
871 * <li>The top activity explicitly belongs to {@param userId}.</li>
872 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
873 * </ul>
874 *
875 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100876 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000877 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
878 // To handle the case that work app is in the task but just is not the top one.
879 final ActivityRecord activityRecord = task.getTopActivity();
880 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
881
882 return (activityRecord != null && activityRecord.userId == userId)
883 || (resultTo != null && resultTo.userId == userId);
884 }
885
886 /**
887 * Find all visible task stacks containing {@param userId} and intercept them with an activity
888 * to block out the contents and possibly start a credential-confirming intent.
889 *
890 * @param userId user handle for the locked managed profile.
891 */
892 void lockAllProfileTasks(@UserIdInt int userId) {
893 mWindowManager.deferSurfaceLayout();
894 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
896 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
897 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
898 final ActivityStack stack = display.getChildAt(stackNdx);
899 final List<TaskRecord> tasks = stack.getAllTasks();
900 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
901 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000902
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700903 // Check the task for a top activity belonging to userId, or returning a
904 // result to an activity belonging to userId. Example case: a document
905 // picker for personal files, opened by a work app, should still get locked.
906 if (taskTopActivityIsUser(task, userId)) {
907 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
908 task.taskId, userId);
909 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000910 }
Tony Mak853304c2016-04-18 15:17:41 +0100911 }
912 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000913 } finally {
914 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100915 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000916 }
917
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800918 void setNextTaskIdForUserLocked(int taskId, int userId) {
919 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
920 if (taskId > currentTaskId) {
921 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700922 }
923 }
924
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700925 static int nextTaskIdForUser(int taskId, int userId) {
926 int nextTaskId = taskId + 1;
927 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
928 // Wrap around as there will be smaller task ids that are available now.
929 nextTaskId -= MAX_TASK_IDS_PER_USER;
930 }
931 return nextTaskId;
932 }
933
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800934 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800935 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
936 // for a userId u, a taskId can only be in the range
937 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
938 // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700939 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700940 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700941 || anyTaskForIdLocked(
942 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700943 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800944 if (candidateTaskId == currentTaskId) {
945 // Something wrong!
946 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
947 throw new IllegalStateException("Cannot get an available task id."
948 + " Reached limit of " + MAX_TASK_IDS_PER_USER
949 + " running tasks per user.");
950 }
951 }
952 mCurTaskIdForUser.put(userId, candidateTaskId);
953 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700954 }
955
Chong Zhang6cda19c2016-06-14 19:07:56 -0700956 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800957 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700958 if (stack == null) {
959 return null;
960 }
Bryce Leec4ab62a2018-03-05 14:19:26 -0800961 ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700962 if (resumedActivity == null || resumedActivity.app == null) {
963 resumedActivity = stack.mPausingActivity;
964 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700965 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700966 }
967 }
968 return resumedActivity;
969 }
970
Dianne Hackbornff072722014-09-24 10:56:28 -0700971 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700972 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700975 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
976 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
977 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700978 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 continue;
980 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200981 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
982 final ActivityRecord top = stack.topRunningActivityLocked();
983 final int size = mTmpActivityList.size();
984 for (int i = 0; i < size; i++) {
985 final ActivityRecord activity = mTmpActivityList.get(i);
986 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
987 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800988 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200989 if (realStartActivityLocked(activity, app,
990 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 didSomething = true;
992 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700993 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200995 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800996 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700997 }
Craig Mautner20e72272013-04-01 13:45:53 -0700998 }
Craig Mautner20e72272013-04-01 13:45:53 -0700999 }
1000 }
1001 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001002 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001003 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001004 }
Craig Mautner20e72272013-04-01 13:45:53 -07001005 return didSomething;
1006 }
1007
1008 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001010 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1011 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001013 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 continue;
1015 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08001016 final ActivityRecord resumedActivity = stack.getResumedActivity();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001018 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -08001019 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 return false;
1021 }
Craig Mautner20e72272013-04-01 13:45:53 -07001022 }
1023 }
Wei Wang65c7a152016-06-02 18:51:22 -07001024 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -08001025 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -07001026 return true;
1027 }
1028
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001031 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1032 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1033 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001034 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08001035 final ActivityRecord r = stack.getResumedActivity();
Bryce Lee7ace3952018-02-16 14:34:32 -08001036 if (r != null && !r.isState(RESUMED)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 return false;
1038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 }
1040 }
1041 }
1042 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001043 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001044 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1045 mLastFocusedStack + " to=" + mFocusedStack);
1046 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 return true;
1048 }
1049
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001050 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001051 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001052 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001053 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1054 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1055 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001056 final ActivityRecord r = stack.getResumedActivity();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001057 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001058 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001059 return false;
1060 }
1061 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001062 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001063 }
1064 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001065 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001066 }
1067
Craig Mautner2acc3892013-09-23 10:28:14 -07001068 /**
1069 * Pause all activities in either all of the stacks or just the back stacks.
1070 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001071 * @param resuming The resuming activity.
1072 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1073 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001074 * @return true if any activity was paused as a result of this call.
1075 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001076 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001077 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001078 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001079 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1080 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1081 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Leec4ab62a2018-03-05 14:19:26 -08001082 if (!isFocusedStack(stack) && stack.getResumedActivity() != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001083 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Bryce Leec4ab62a2018-03-05 14:19:26 -08001084 " mResumedActivity=" + stack.getResumedActivity());
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001085 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1086 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001087 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001088 }
1089 }
1090 return someActivityPaused;
1091 }
1092
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001094 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001095 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001096 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1097 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1098 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001099 final ActivityRecord r = stack.mPausingActivity;
Bryce Lee7ace3952018-02-16 14:34:32 -08001100 if (r != null && !r.isState(PAUSED, STOPPED, STOPPING)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001101 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001102 Slog.d(TAG_STATES,
Bryce Lee7ace3952018-02-16 14:34:32 -08001103 "allPausedActivitiesComplete: r=" + r + " state=" + r.getState());
Craig Mautner4a1cb222013-12-04 16:14:06 -08001104 pausing = false;
1105 } else {
1106 return false;
1107 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001108 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 }
1110 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001111 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001112 }
1113
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001114 void cancelInitializingActivities() {
1115 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001116 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1117 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1118 final ActivityStack stack = display.getChildAt(stackNdx);
1119 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001120 }
1121 }
1122 }
1123
Bryce Lee4a194382017-04-04 14:32:48 -07001124 void waitActivityVisible(ComponentName name, WaitResult result) {
1125 final WaitInfo waitInfo = new WaitInfo(name, result);
1126 mWaitingForActivityVisible.add(waitInfo);
1127 }
1128
1129 void cleanupActivity(ActivityRecord r) {
1130 // Make sure this record is no longer in the pending finishes list.
1131 // This could happen, for example, if we are trimming activities
1132 // down to the max limit while they are still waiting to finish.
1133 mFinishingActivities.remove(r);
1134 mActivitiesWaitingForVisibleActivity.remove(r);
1135
1136 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001137 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001138 mWaitingForActivityVisible.remove(i);
1139 }
1140 }
1141 }
1142
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001143 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001144 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001145 }
1146
1147 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1148 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001149 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1150 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001151 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001152 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001153 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001154 result.timeout = false;
1155 result.who = w.getComponent();
1156 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1157 result.thisTime = result.totalTime;
1158 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001161 if (changed) {
1162 mService.notifyAll();
1163 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001164 }
1165
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001166 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1167 if (mWaitingActivityLaunched.isEmpty()) {
1168 return;
1169 }
1170
1171 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1172 return;
1173 }
1174
Chong Zhang5022da32016-06-21 16:31:37 -07001175 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001176
Chong Zhang5022da32016-06-21 16:31:37 -07001177 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1178 WaitResult w = mWaitingActivityLaunched.remove(i);
1179 if (w.who == null) {
1180 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001181 w.result = result;
1182
1183 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1184 // will be no followup launch signals. Assign the result and launched component.
1185 if (result == START_DELIVERED_TO_TOP) {
1186 w.who = r.realActivity;
1187 }
Chong Zhang5022da32016-06-21 16:31:37 -07001188 }
1189 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001190
Chong Zhang5022da32016-06-21 16:31:37 -07001191 if (changed) {
1192 mService.notifyAll();
1193 }
1194 }
1195
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001196 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1197 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001198 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001199 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001200 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001201 if (w.who == null) {
1202 changed = true;
1203 w.timeout = timeout;
1204 if (r != null) {
1205 w.who = new ComponentName(r.info.packageName, r.info.name);
1206 }
1207 w.thisTime = thisTime;
1208 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001209 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001210 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001211 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001212 if (changed) {
1213 mService.notifyAll();
1214 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001215 }
1216
Craig Mautner29219d92013-04-16 20:19:12 -07001217 ActivityRecord topRunningActivityLocked() {
Bryce Leec961e0a2018-04-13 17:58:02 -07001218 return topRunningActivityLocked(false /* considerKeyguardState */);
1219 }
1220
1221 /**
1222 * Returns the top running activity in the focused stack. In the case the focused stack has no
1223 * such activity, the next focusable stack on top of a display is returned.
1224 * @param considerKeyguardState Indicates whether the locked state should be considered. if
1225 * {@code true} and the keyguard is locked, only activities that
1226 * can be shown on top of the keyguard will be considered.
1227 * @return The top running activity. {@code null} if none is available.
1228 */
1229 ActivityRecord topRunningActivityLocked(boolean considerKeyguardState) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001230 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001231 ActivityRecord r = focusedStack.topRunningActivityLocked();
Bryce Lee86918962018-04-26 15:28:12 -07001232 if (r != null && isValidTopRunningActivity(r, considerKeyguardState)) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001233 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001234 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001235
Andrii Kulian7318d632016-07-20 18:59:28 -07001236 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001237 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1238
1239 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1240 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001241 final ActivityDisplay display = mActivityDisplays.get(displayId);
Bryce Lee4bc5ba32018-03-20 13:58:53 -07001242
1243 // If WindowManagerService has encountered the display before we have, ignore as there
1244 // will be no stacks present and therefore no activities.
1245 if (display == null) {
1246 continue;
1247 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001248
1249 // TODO: We probably want to consider the top fullscreen stack as we could have a pinned
1250 // stack on top.
1251 final ActivityStack topStack = display.getTopStack();
1252
1253 // Only consider focusable top stacks other than the current focused one.
1254 if (topStack == null || !topStack.isFocusable() || topStack == focusedStack) {
1255 continue;
1256 }
1257
1258 final ActivityRecord topActivity = topStack.topRunningActivityLocked();
1259
1260 // Skip if no top activity.
1261 if (topActivity == null) {
1262 continue;
1263 }
1264
Bryce Leec961e0a2018-04-13 17:58:02 -07001265
1266 // This activity can be considered the top running activity if we are not
1267 // considering the locked state, the keyguard isn't locked, or we can show when
1268 // locked.
Bryce Lee86918962018-04-26 15:28:12 -07001269 if (isValidTopRunningActivity(topActivity, considerKeyguardState)) {
Bryce Leec961e0a2018-04-13 17:58:02 -07001270 return topActivity;
Craig Mautner29219d92013-04-16 20:19:12 -07001271 }
1272 }
Bryce Leec961e0a2018-04-13 17:58:02 -07001273
Craig Mautner29219d92013-04-16 20:19:12 -07001274 return null;
1275 }
1276
Bryce Lee86918962018-04-26 15:28:12 -07001277 /**
1278 * Verifies an {@link ActivityRecord} can be the top activity based on keyguard state and
1279 * whether we are considering it.
1280 */
1281 private boolean isValidTopRunningActivity(ActivityRecord record,
1282 boolean considerKeyguardState) {
1283 if (!considerKeyguardState) {
1284 return true;
1285 }
1286
1287 final boolean keyguardLocked = getKeyguardController().isKeyguardLocked();
1288
1289 if (!keyguardLocked) {
1290 return true;
1291 }
1292
1293 return record.canShowWhenLocked();
1294 }
1295
Winson Chung61c9e5a2017-10-11 10:39:32 -07001296 @VisibleForTesting
1297 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1298 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1299 int callingUid, boolean allowed) {
1300 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1301 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001302 }
1303
Todd Kennedy7440f172015-12-09 14:31:22 -08001304 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1305 ProfilerInfo profilerInfo) {
1306 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001307 if (aInfo != null) {
1308 // Store the found target back into the intent, because now that
1309 // we have it we never want to do this again. For example, if the
1310 // user navigates back to this point in the history, we should
1311 // always restart the exact same activity.
1312 intent.setComponent(new ComponentName(
1313 aInfo.applicationInfo.packageName, aInfo.name));
1314
1315 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001316 if (!aInfo.processName.equals("system")) {
1317 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001318 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001319 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001320
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001321 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1322 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1323 }
1324
Man Caocfa78b22015-06-11 20:14:34 -07001325 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1326 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1327 }
1328
1329 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001330 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001331 }
1332 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001333 final String intentLaunchToken = intent.getLaunchToken();
1334 if (aInfo.launchToken == null && intentLaunchToken != null) {
1335 aInfo.launchToken = intentLaunchToken;
1336 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001337 }
1338 return aInfo;
1339 }
1340
Patrick Baumann78380272018-04-04 10:41:01 -07001341 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags,
1342 int filterCallingUid) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001343 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001344 try {
1345 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001346 int modifiedFlags = flags
1347 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001348 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001349 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1350 modifiedFlags |= PackageManager.MATCH_INSTANT;
1351 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001352
Makoto Onuki1a342742018-04-26 14:56:59 -07001353 // In order to allow cross-profile lookup, we clear the calling identity here.
1354 // Note the binder identity won't affect the result, but filterCallingUid will.
1355
1356 // Cross-user/profile call check are done at the entry points
1357 // (e.g. AMS.startActivityAsUser).
1358 final long token = Binder.clearCallingIdentity();
1359 try {
1360 return mService.getPackageManagerInternalLocked().resolveIntent(
1361 intent, resolvedType, modifiedFlags, userId, true, filterCallingUid);
1362 } finally {
1363 Binder.restoreCallingIdentity(token);
1364 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001365 } finally {
1366 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1367 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001368 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001369 }
1370
1371 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Patrick Baumann78380272018-04-04 10:41:01 -07001372 ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
1373 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
Todd Kennedy7440f172015-12-09 14:31:22 -08001374 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1375 }
1376
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001377 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1378 boolean andResume, boolean checkConfig) throws RemoteException {
1379
1380 if (!allPausedActivitiesComplete()) {
1381 // While there are activities pausing we skipping starting any new activities until
1382 // pauses are complete. NOTE: that we also do this for activities that are starting in
1383 // the paused state because they will first be resumed then paused on the client side.
1384 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1385 "realStartActivityLocked: Skipping start of r=" + r
1386 + " some activities pausing...");
1387 return false;
1388 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001389
Bryce Leeaf691c02017-03-20 14:20:22 -07001390 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001391 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001392
1393 beginDeferResume();
1394
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 try {
chaviw59b98852017-06-13 12:05:44 -07001396 r.startFreezingScreenLocked(app, 0);
1397
1398 // schedule launch ticks to collect information about slow apps.
1399 r.startLaunchTickingLocked();
1400
Dianne Hackborn68a06332017-11-15 17:54:18 -08001401 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001402
Bryce Lee459c0622018-03-19 11:04:01 -07001403 if (getKeyguardController().isKeyguardLocked()) {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001404 r.notifyUnknownVisibilityLaunched();
1405 }
1406
chaviw59b98852017-06-13 12:05:44 -07001407 // Have the window manager re-evaluate the orientation of the screen based on the new
1408 // activity order. Note that as a result of this, it can call back into the activity
1409 // manager with a new orientation. We don't care about that, because the activity is
1410 // not currently running so we are just restarting it anyway.
1411 if (checkConfig) {
chaviw59b98852017-06-13 12:05:44 -07001412 // Deferring resume here because we're going to launch new activity shortly.
1413 // We don't want to perform a redundant launch of the same record while ensuring
1414 // configurations and trying to resume top activity of focused stack.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001415 ensureVisibilityAndConfig(r, r.getDisplayId(),
1416 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Craig Mautner2420ead2013-04-01 17:13:20 -07001417 }
chaviw59b98852017-06-13 12:05:44 -07001418
1419 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1420 true /* isTop */)) {
1421 // We only set the visibility to true if the activity is allowed to be visible
1422 // based on
1423 // keyguard state. This avoids setting this into motion in window manager that is
1424 // later cancelled due to later calls to ensure visible activities that set
1425 // visibility back to false.
1426 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001427 }
chaviw59b98852017-06-13 12:05:44 -07001428
chaviw59b98852017-06-13 12:05:44 -07001429 final int applicationInfoUid =
1430 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1431 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1432 Slog.wtf(TAG,
1433 "User ID for activity changing for " + r
1434 + " appInfo.uid=" + r.appInfo.uid
1435 + " info.ai.uid=" + applicationInfoUid
1436 + " old=" + r.app + " new=" + app);
1437 }
1438
1439 app.waitingToKill = null;
1440 r.launchCount++;
1441 r.lastLaunchTime = SystemClock.uptimeMillis();
1442
1443 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1444
1445 int idx = app.activities.indexOf(r);
1446 if (idx < 0) {
1447 app.activities.add(r);
1448 }
1449 mService.updateLruProcessLocked(app, true, null);
1450 mService.updateOomAdjLocked();
1451
Bryce Lee2b8e0372018-04-05 17:01:37 -07001452 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles Heff9b4dff2017-09-22 10:18:37 +01001453 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1454 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1455 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
Bryce Lee2b8e0372018-04-05 17:01:37 -07001456 && lockTaskController.getLockTaskModeState()
1457 == LOCK_TASK_MODE_LOCKED)) {
1458 lockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001459 }
1460
1461 try {
1462 if (app.thread == null) {
1463 throw new RemoteException();
1464 }
1465 List<ResultInfo> results = null;
1466 List<ReferrerIntent> newIntents = null;
1467 if (andResume) {
1468 // We don't need to deliver new intents and/or set results if activity is going
1469 // to pause immediately after launch.
1470 results = r.results;
1471 newIntents = r.newIntents;
1472 }
1473 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1474 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1475 + " newIntents=" + newIntents + " andResume=" + andResume);
1476 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1477 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001478 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001479 // Home process is the root process of the task.
1480 mService.mHomeProcess = task.mActivities.get(0).app;
1481 }
1482 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1483 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1484 r.sleeping = false;
1485 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001486 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001487 mService.showAskCompatModeDialogLocked(r);
1488 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1489 ProfilerInfo profilerInfo = null;
1490 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1491 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1492 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001493 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1494 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1495 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001496 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001497 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001498 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001499 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001500 }
1501 }
chaviw59b98852017-06-13 12:05:44 -07001502
Andreas Gampe2b073a02017-06-22 17:28:57 -07001503 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001504 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001505 }
1506 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001507
chaviw59b98852017-06-13 12:05:44 -07001508 app.hasShownUi = true;
1509 app.pendingUiClean = true;
1510 app.forceProcessStateUpTo(mService.mTopProcessState);
1511 // Because we could be starting an Activity in the system process this may not go
1512 // across a Binder interface which would create a new Configuration. Consequently
1513 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001514
chaviw59b98852017-06-13 12:05:44 -07001515 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1516 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1517 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001518
chaviw59b98852017-06-13 12:05:44 -07001519 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001520
1521
1522 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001523 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001524 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001525 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001526 System.identityHashCode(r), r.info,
1527 // TODO: Have this take the merged configuration instead of separate global
1528 // and override configs.
1529 mergedConfiguration.getGlobalConfiguration(),
1530 mergedConfiguration.getOverrideConfiguration(), r.compat,
1531 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001532 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1533 profilerInfo));
1534
1535 // Set desired final state.
1536 final ActivityLifecycleItem lifecycleItem;
1537 if (andResume) {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001538 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001539 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07001540 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001541 }
1542 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1543
1544 // Schedule transaction.
Bryce Leeb0f993f2018-03-02 15:38:01 -08001545 mService.getLifecycleManager().scheduleTransaction(clientTransaction);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001546
Craig Mautner2420ead2013-04-01 17:13:20 -07001547
Dianne Hackbornc8e4fad2018-05-04 11:31:09 -07001548 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0
1549 && mService.mHasHeavyWeightFeature) {
chaviw59b98852017-06-13 12:05:44 -07001550 // This may be a heavy-weight process! Note that the package
1551 // manager will ensure that only activity can run in the main
1552 // process of the .apk, which is the only thing that will be
1553 // considered heavy-weight.
1554 if (app.processName.equals(app.info.packageName)) {
1555 if (mService.mHeavyWeightProcess != null
1556 && mService.mHeavyWeightProcess != app) {
1557 Slog.w(TAG, "Starting new heavy weight process " + app
1558 + " when already running "
1559 + mService.mHeavyWeightProcess);
1560 }
1561 mService.mHeavyWeightProcess = app;
1562 Message msg = mService.mHandler.obtainMessage(
1563 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1564 msg.obj = r;
1565 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001566 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001567 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001568
chaviw59b98852017-06-13 12:05:44 -07001569 } catch (RemoteException e) {
1570 if (r.launchFailed) {
1571 // This is the second time we failed -- finish activity
1572 // and give up.
1573 Slog.e(TAG, "Second failure launching "
1574 + r.intent.getComponent().flattenToShortString()
1575 + ", giving up", e);
1576 mService.appDiedLocked(app);
1577 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1578 "2nd-crash", false);
1579 return false;
1580 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001581
chaviw59b98852017-06-13 12:05:44 -07001582 // This is the first time we failed -- restart process and
1583 // retry.
1584 r.launchFailed = true;
1585 app.activities.remove(r);
1586 throw e;
1587 }
1588 } finally {
1589 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001590 }
1591
1592 r.launchFailed = false;
1593 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001594 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001595 }
1596
Andrii Kulianb372da62018-01-18 10:46:24 -08001597 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1598 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001599 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001600 // As part of the process of launching, ActivityThread also performs
1601 // a resume.
1602 stack.minimalResumeActivityLocked(r);
1603 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001604 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001605 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001606 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001607 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001608 "Moving to PAUSED: " + r + " (starting in paused state)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001609 r.setState(PAUSED, "realStartActivityLocked");
Craig Mautner2420ead2013-04-01 17:13:20 -07001610 }
1611
1612 // Launch the new version setup screen if needed. We do this -after-
1613 // launching the initial activity (that is, home), so that it can have
1614 // a chance to initialize itself while in the background, making the
1615 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001616 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001617 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001618 }
1619
Dianne Hackborn465fa392014-09-14 14:21:18 -07001620 // Update any services we are bound to that might care about whether
1621 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001622 if (r.app != null) {
1623 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1624 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001625
Craig Mautner2420ead2013-04-01 17:13:20 -07001626 return true;
1627 }
1628
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001629 /**
1630 * Ensure all activities visibility, update orientation and configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001631 *
1632 * @param starting The currently starting activity or {@code null} if there is none.
1633 * @param displayId The id of the display where operation is executed.
1634 * @param markFrozenIfConfigChanged Whether to set {@link ActivityRecord#frozenBeforeDestroy} to
1635 * {@code true} if config changed.
1636 * @param deferResume Whether to defer resume while updating config.
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001637 * @return 'true' if starting activity was kept or wasn't provided, 'false' if it was relaunched
1638 * because of configuration update.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001639 */
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001640 boolean ensureVisibilityAndConfig(ActivityRecord starting, int displayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001641 boolean markFrozenIfConfigChanged, boolean deferResume) {
1642 // First ensure visibility without updating the config just yet. We need this to know what
1643 // activities are affecting configuration now.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001644 // Passing null here for 'starting' param value, so that visibility of actual starting
1645 // activity will be properly updated.
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001646 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001647 false /* preserveWindows */, false /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001648
Andrii Kulian2f0d73c2018-06-15 14:32:04 -07001649 if (displayId == INVALID_DISPLAY) {
1650 // The caller didn't provide a valid display id, skip updating config.
1651 return true;
1652 }
1653
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001654 // Force-update the orientation from the WindowManager, since we need the true configuration
1655 // to send to the client now.
1656 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1657 getDisplayOverrideConfiguration(displayId),
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001658 starting != null && starting.mayFreezeScreenLocked(starting.app)
1659 ? starting.appToken : null,
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001660 displayId, true /* forceUpdate */);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001661 if (starting != null && markFrozenIfConfigChanged && config != null) {
1662 starting.frozenBeforeDestroy = true;
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001663 }
1664
1665 // Update the configuration of the activities on the display.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001666 return mService.updateDisplayOverrideConfigurationLocked(config, starting, deferResume,
1667 displayId);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001668 }
1669
Sudheer Shankafab200f2017-05-17 20:41:53 -07001670 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1671 int extrasSize = 0;
1672 if (intent != null) {
1673 final Bundle extras = intent.getExtras();
1674 if (extras != null) {
1675 extrasSize = extras.getSize();
1676 }
1677 }
1678 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1679 if (extrasSize + icicleSize > 200000) {
1680 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1681 + ", icicle size: " + icicleSize);
1682 }
1683 }
1684
Craig Mautnere79d42682013-04-01 19:01:53 -07001685 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001686 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001687 // Is this activity's application already running?
1688 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001689 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001690
Jorim Jaggide8305a2018-05-15 12:06:35 -07001691 getLaunchTimeTracker().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001692
1693 if (app != null && app.thread != null) {
1694 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001695 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1696 || !"android".equals(r.info.packageName)) {
1697 // Don't add this if it is a platform component that is marked
1698 // to run in multiple processes, because this is actually
1699 // part of the framework so doesn't make sense to track as a
1700 // separate apk in the process.
Patrick Baumannc2def582018-04-04 12:14:15 -07001701 app.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode,
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001702 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001703 }
George Mount2c92c972014-03-20 09:38:23 -07001704 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001705 return;
1706 } catch (RemoteException e) {
1707 Slog.w(TAG, "Exception when starting activity "
1708 + r.intent.getComponent().flattenToShortString(), e);
1709 }
1710
1711 // If a dead object exception was thrown -- fall through to
1712 // restart the application.
1713 }
1714
1715 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001716 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001717 }
1718
Bryce Leed3624e12017-11-30 08:51:45 -08001719 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1720 boolean sendHint = forceSend;
1721
1722 if (!sendHint) {
1723 // If not forced, send power hint when the activity's process is different than the
1724 // current resumed activity.
1725 final ActivityRecord resumedActivity = getResumedActivityLocked();
1726 sendHint = resumedActivity == null
1727 || resumedActivity.app == null
1728 || !resumedActivity.app.equals(targetActivity.app);
1729 }
1730
1731 if (sendHint && mService.mLocalPowerManager != null) {
1732 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1733 mPowerHintSent = true;
1734 }
1735 }
1736
1737 void sendPowerHintForLaunchEndIfNeeded() {
1738 // Trigger launch power hint if activity is launched
1739 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1740 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1741 mPowerHintSent = false;
1742 }
1743 }
1744
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001745 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001746 String resultWho, int requestCode, int callingPid, int callingUid,
Winson Chungc9804e72018-05-15 11:01:44 -07001747 String callingPackage, boolean ignoreTargetSecurity, boolean launchingInTask,
1748 ProcessRecord callerApp, ActivityRecord resultRecord, ActivityStack resultStack) {
1749 final boolean isCallerRecents = mService.getRecentTasks() != null &&
1750 mService.getRecentTasks().isCallerRecents(callingUid);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001751 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1752 callingUid);
Winson Chungc9804e72018-05-15 11:01:44 -07001753 if (startAnyPerm == PERMISSION_GRANTED || (isCallerRecents && launchingInTask)) {
1754 // If the caller has START_ANY_ACTIVITY, ignore all checks below. In addition, if the
1755 // caller is the recents component and we are specifically starting an activity in an
1756 // existing task, then also allow the activity to be fully relaunched.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001757 return true;
1758 }
1759 final int componentRestriction = getComponentRestrictionForCallingPackage(
1760 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1761 final int actionRestriction = getActionRestrictionForCallingPackage(
1762 intent.getAction(), callingPackage, callingPid, callingUid);
1763 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1764 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1765 if (resultRecord != null) {
1766 resultStack.sendActivityResultLocked(-1,
1767 resultRecord, resultWho, requestCode,
1768 Activity.RESULT_CANCELED, null);
1769 }
1770 final String msg;
1771 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1772 msg = "Permission Denial: starting " + intent.toString()
1773 + " from " + callerApp + " (pid=" + callingPid
1774 + ", uid=" + callingUid + ")" + " with revoked permission "
1775 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1776 } else if (!aInfo.exported) {
1777 msg = "Permission Denial: starting " + intent.toString()
1778 + " from " + callerApp + " (pid=" + callingPid
1779 + ", uid=" + callingUid + ")"
1780 + " not exported from uid " + aInfo.applicationInfo.uid;
1781 } else {
1782 msg = "Permission Denial: starting " + intent.toString()
1783 + " from " + callerApp + " (pid=" + callingPid
1784 + ", uid=" + callingUid + ")"
1785 + " requires " + aInfo.permission;
1786 }
1787 Slog.w(TAG, msg);
1788 throw new SecurityException(msg);
1789 }
1790
1791 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1792 final String message = "Appop Denial: starting " + intent.toString()
1793 + " from " + callerApp + " (pid=" + callingPid
1794 + ", uid=" + callingUid + ")"
1795 + " requires " + AppOpsManager.permissionToOp(
1796 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1797 Slog.w(TAG, message);
1798 return false;
1799 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1800 final String message = "Appop Denial: starting " + intent.toString()
1801 + " from " + callerApp + " (pid=" + callingPid
1802 + ", uid=" + callingUid + ")"
1803 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1804 Slog.w(TAG, message);
1805 return false;
1806 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001807
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001808 return true;
1809 }
1810
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001811 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001812 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1813 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001814 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1815 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1816
Andrii Kulian02689a72017-07-06 14:28:59 -07001817 if (callingPid == -1 && callingUid == -1) {
1818 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1819 return true;
1820 }
1821
Andrii Kulian62e6f252017-05-30 22:46:53 -07001822 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001823 if (activityDisplay == null) {
1824 Slog.w(TAG, "Launch on display check: display not found");
1825 return false;
1826 }
1827
Andrii Kulian02689a72017-07-06 14:28:59 -07001828 // Check if the caller has enough privileges to embed activities and launch to private
1829 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001830 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001831 callingUid);
1832 if (startAnyPerm == PERMISSION_GRANTED) {
1833 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1834 + " allow launch any on display");
1835 return true;
1836 }
1837
Andrii Kulian8f070292017-09-12 22:56:49 -07001838 // Check if caller is already present on display
1839 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1840
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001841 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1842 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1843 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001844 // Limit launching on virtual displays, because their contents can be read from Surface
1845 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001846 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1847 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1848 + " disallow launch on virtual display for not-embedded activity.");
1849 return false;
1850 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001851 // Check if the caller is allowed to embed activities from other apps.
1852 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001853 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001854 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1855 + " disallow activity embedding without permission.");
1856 return false;
1857 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001858 }
1859
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001860 if (!activityDisplay.isPrivate()) {
1861 // Anyone can launch on a public display.
1862 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1863 + " allow launch on public display");
1864 return true;
1865 }
1866
1867 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001868 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001869 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1870 + " allow launch for owner of the display");
1871 return true;
1872 }
1873
Andrii Kulian8f070292017-09-12 22:56:49 -07001874 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001875 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1876 + " allow launch for caller present on the display");
1877 return true;
1878 }
1879
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001880 Slog.w(TAG, "Launch on display check: denied");
1881 return false;
1882 }
1883
1884 /** Update lists of UIDs that are present on displays and have access to them. */
1885 void updateUIDsPresentOnDisplay() {
1886 mDisplayAccessUIDs.clear();
1887 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1888 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001889 // Only bother calculating the whitelist for private displays
1890 if (activityDisplay.isPrivate()) {
1891 mDisplayAccessUIDs.append(
1892 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1893 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001894 }
1895 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1896 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1897 }
1898
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001899 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001900 final long identity = Binder.clearCallingIdentity();
1901 try {
1902 return UserManager.get(mService.mContext).getUserInfo(userId);
1903 } finally {
1904 Binder.restoreCallingIdentity(identity);
1905 }
1906 }
1907
Svet Ganov99b60432015-06-27 13:15:22 -07001908 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001909 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001910 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1911 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001912 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001913 return ACTIVITY_RESTRICTION_PERMISSION;
1914 }
1915
Christopher Tateff7add02015-08-17 10:23:22 -07001916 if (activityInfo.permission == null) {
1917 return ACTIVITY_RESTRICTION_NONE;
1918 }
1919
Svet Ganov99b60432015-06-27 13:15:22 -07001920 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1921 if (opCode == AppOpsManager.OP_NONE) {
1922 return ACTIVITY_RESTRICTION_NONE;
1923 }
1924
1925 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1926 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001927 if (!ignoreTargetSecurity) {
1928 return ACTIVITY_RESTRICTION_APPOP;
1929 }
Svet Ganov99b60432015-06-27 13:15:22 -07001930 }
1931
1932 return ACTIVITY_RESTRICTION_NONE;
1933 }
1934
Svetoslav7008b512015-06-24 18:47:07 -07001935 private int getActionRestrictionForCallingPackage(String action,
1936 String callingPackage, int callingPid, int callingUid) {
1937 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001938 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001939 }
1940
1941 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1942 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001943 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001944 }
1945
1946 final PackageInfo packageInfo;
1947 try {
1948 packageInfo = mService.mContext.getPackageManager()
1949 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1950 } catch (PackageManager.NameNotFoundException e) {
1951 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001952 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001953 }
1954
1955 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001956 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001957 }
1958
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001959 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001960 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001961 }
1962
1963 final int opCode = AppOpsManager.permissionToOpCode(permission);
1964 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001965 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001966 }
1967
1968 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1969 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001970 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001971 }
1972
Svet Ganov99b60432015-06-27 13:15:22 -07001973 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001974 }
1975
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001976 void setLaunchSource(int uid) {
1977 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1978 }
1979
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001980 void acquireLaunchWakelock() {
1981 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1982 throw new IllegalStateException("Calling must be system uid");
1983 }
1984 mLaunchingActivity.acquire();
1985 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1986 // To be safe, don't allow the wake lock to be held for too long.
1987 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1988 }
1989 }
1990
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001991 /**
1992 * Called when the frontmost task is idle.
1993 * @return the state of mService.mBooting before this was called.
1994 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001995 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001996 private boolean checkFinishBootingLocked() {
1997 final boolean booting = mService.mBooting;
1998 boolean enableScreen = false;
1999 mService.mBooting = false;
2000 if (!mService.mBooted) {
2001 mService.mBooted = true;
2002 enableScreen = true;
2003 }
2004 if (booting || enableScreen) {
2005 mService.postFinishBooting(booting, enableScreen);
2006 }
2007 return booting;
2008 }
2009
Craig Mautnerf3333272013-04-22 10:55:53 -07002010 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08002011 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07002012 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08002013 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002014 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002015
Craig Mautnerf3333272013-04-22 10:55:53 -07002016 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002017 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002018 int NS = 0;
2019 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002020 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002021 boolean activityRemoved = false;
2022
Wale Ogunwale7d701172015-03-11 15:36:30 -07002023 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002024 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002025 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2026 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002027 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2028 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002029 if (fromTimeout) {
2030 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002031 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002032
2033 // This is a hack to semi-deal with a race condition
2034 // in the client where it can be constructed with a
2035 // newer configuration from when we asked it to launch.
2036 // We'll update with whatever configuration it now says
2037 // it used to launch.
2038 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08002039 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002040 }
2041
2042 // We are now idle. If someone is waiting for a thumbnail from
2043 // us, we can now deliver.
2044 r.idle = true;
2045
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002046 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002047 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002048 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002049 }
2050 }
2051
2052 if (allResumedActivitiesIdle()) {
2053 if (r != null) {
2054 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002055 }
2056
2057 if (mLaunchingActivity.isHeld()) {
2058 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2059 if (VALIDATE_WAKE_LOCK_CALLER &&
2060 Binder.getCallingUid() != Process.myUid()) {
2061 throw new IllegalStateException("Calling must be system uid");
2062 }
2063 mLaunchingActivity.release();
2064 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002065 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002066 }
2067
2068 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08002069 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
2070 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002071 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002072 if ((NF = mFinishingActivities.size()) > 0) {
2073 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002074 mFinishingActivities.clear();
2075 }
2076
Craig Mautnerf3333272013-04-22 10:55:53 -07002077 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002078 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002079 mStartingUsers.clear();
2080 }
2081
Craig Mautnerf3333272013-04-22 10:55:53 -07002082 // Stop any activities that are scheduled to do so but have been
2083 // waiting for the next one to start.
2084 for (int i = 0; i < NS; i++) {
2085 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002086 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002087 if (stack != null) {
2088 if (r.finishing) {
Bryce Leef52974c2018-02-14 15:12:01 -08002089 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false,
2090 "activityIdleInternalLocked");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002091 } else {
2092 stack.stopActivityLocked(r);
2093 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002094 }
2095 }
2096
2097 // Finish any activities that are scheduled to do so but have been
2098 // waiting for the next one to start.
2099 for (int i = 0; i < NF; i++) {
2100 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002101 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07002102 if (stack != null) {
2103 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2104 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002105 }
2106
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002107 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002108 // Complete user switch
2109 if (startingUsers != null) {
2110 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002111 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002112 }
2113 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002114 }
2115
2116 mService.trimApplications();
2117 //dump();
2118 //mWindowManager.dump();
2119
Craig Mautnerf3333272013-04-22 10:55:53 -07002120 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002121 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002122 }
2123
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002124 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002125 }
2126
Craig Mautner8e569572013-10-11 17:36:59 -07002127 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002128 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002129 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002130 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2131 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2132 final ActivityStack stack = display.getChildAt(stackNdx);
2133 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002134 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002135 }
Craig Mautner19091252013-10-05 00:03:53 -07002136 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002137 }
2138
2139 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002140 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002141 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2142 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2143 final ActivityStack stack = display.getChildAt(stackNdx);
2144 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002145 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002146 }
2147 }
2148
Craig Mautner93529a42013-10-04 15:03:13 -07002149 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002150 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002151 }
2152
Craig Mautner8d341ef2013-03-26 09:03:27 -07002153 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002154 * Update the last used stack id for non-current user (current user's last
2155 * used stack is the focused stack)
2156 */
2157 void updateUserStackLocked(int userId, ActivityStack stack) {
2158 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002159 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002160 }
2161 }
2162
2163 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002164 * @return true if some activity was finished (or would have finished if doit were true).
2165 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002166 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2167 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002168 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002169 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002170 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2171 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2172 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002173 if (stack.finishDisabledPackageActivitiesLocked(
2174 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002175 didSomething = true;
2176 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002177 }
2178 }
2179 return didSomething;
2180 }
2181
Dianne Hackborna413dc02013-07-12 12:02:55 -07002182 void updatePreviousProcessLocked(ActivityRecord r) {
2183 // Now that this process has stopped, we may want to consider
2184 // it to be the previous app to try to keep around in case
2185 // the user wants to return to it.
2186
2187 // First, found out what is currently the foreground app, so that
2188 // we don't blow away the previous app if this activity is being
2189 // hosted by the process that is actually still the foreground.
2190 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002191 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002192 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2193 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2194 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002195 if (isFocusedStack(stack)) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08002196 final ActivityRecord resumedActivity = stack.getResumedActivity();
2197 if (resumedActivity != null) {
2198 fgApp = resumedActivity.app;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002199 } else if (stack.mPausingActivity != null) {
2200 fgApp = stack.mPausingActivity.app;
2201 }
2202 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002203 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002204 }
2205 }
2206
2207 // Now set this one as the previous process, only if that really
2208 // makes sense to.
2209 if (r.app != null && fgApp != null && r.app != fgApp
2210 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002211 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002212 mService.mPreviousProcess = r.app;
2213 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2214 }
2215 }
2216
Wale Ogunwaled046a012015-12-24 13:05:59 -08002217 boolean resumeFocusedStackTopActivityLocked() {
2218 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002219 }
2220
Wale Ogunwaled046a012015-12-24 13:05:59 -08002221 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002222 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002223
2224 if (!readyToResume()) {
2225 return false;
2226 }
2227
Wale Ogunwaled046a012015-12-24 13:05:59 -08002228 if (targetStack != null && isFocusedStack(targetStack)) {
2229 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002230 }
chaviw59b98852017-06-13 12:05:44 -07002231
Wale Ogunwale06579d62016-04-30 15:29:06 -07002232 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08002233 if (r == null || !r.isState(RESUMED)) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07002234 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
Bryce Lee7ace3952018-02-16 14:34:32 -08002235 } else if (r.isState(RESUMED)) {
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002236 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2237 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002238 }
chaviw59b98852017-06-13 12:05:44 -07002239
Wale Ogunwaled046a012015-12-24 13:05:59 -08002240 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002241 }
2242
Todd Kennedy39bfee52016-02-24 10:28:21 -08002243 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2244 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002245 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2246 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2247 final ActivityStack stack = display.getChildAt(stackNdx);
2248 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002249 }
2250 }
2251 }
2252
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002253 /**
2254 * Finish the topmost activities in all stacks that belong to the crashed app.
2255 * @param app The app that crashed.
2256 * @param reason Reason to perform this action.
2257 * @return The task that was finished in this stack, {@code null} if haven't found any.
2258 */
2259 TaskRecord finishTopCrashedActivitiesLocked(ProcessRecord app, String reason) {
Adrian Roos20d7df32016-01-12 18:59:43 +01002260 TaskRecord finishedTask = null;
2261 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002262 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002263 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002264 // It is possible that request to finish activity might also remove its task and stack,
2265 // so we need to be careful with indexes in the loop and check child count every time.
2266 for (int stackNdx = 0; stackNdx < display.getChildCount(); ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002267 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07002268 final TaskRecord t = stack.finishTopCrashedActivityLocked(app, reason);
Adrian Roos20d7df32016-01-12 18:59:43 +01002269 if (stack == focusedStack || finishedTask == null) {
2270 finishedTask = t;
2271 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002272 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002273 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002274 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002275 }
2276
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002277 void finishVoiceTask(IVoiceInteractionSession session) {
2278 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002279 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2280 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002281 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002282 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002283 stack.finishVoiceTask(session);
2284 }
2285 }
2286 }
2287
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002288 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2289 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002290 final ActivityStack currentStack = task.getStack();
2291 if (currentStack == null) {
2292 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2293 + task + " to front. Stack is null");
2294 return;
2295 }
2296
Craig Mautneraea74a52014-03-08 14:23:10 -08002297 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2298 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002299 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002300
2301 final ActivityRecord prev = topRunningActivityLocked();
2302
2303 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2304 || (prev != null && prev.isActivityTypeRecents())) {
2305 // Caller wants the home activity moved with it or the previous task is recents in which
2306 // case we always return home from the task we are moving to the front.
2307 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002308 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002309
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002310 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002311 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002312 task.updateOverrideConfiguration(bounds);
2313
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002314 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002316 if (stack != currentStack) {
2317 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002318 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002319 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002320 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2321 // still need moveTaskToFrontLocked() below for any transition settings.
2322 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002323 if (stack.resizeStackWithLaunchBounds()) {
2324 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002325 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2326 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002327 } else {
2328 // WM resizeTask must be done after the task is moved to the correct stack,
2329 // because Task's setBounds() also updates dim layer's bounds, but that has
2330 // dependency on the stack.
2331 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002332 }
2333 }
2334
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002335 final ActivityRecord r = task.getTopActivity();
chaviw0d562bf2018-03-15 14:24:14 -07002336 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002337 r == null ? null : r.appTimeTracker, reason);
2338
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002339 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002340 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002341
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002342 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002343 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002344 }
2345
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002346 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002347 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002348 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002349 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002350 return false;
2351 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002352 return (mService.mSupportsPictureInPicture
2353 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002354 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002355 }
2356
Bryce Leeec55eb02017-12-05 20:51:27 -08002357 LaunchParamsController getLaunchParamsController() {
2358 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002359 }
2360
Winson Chung55893332017-02-17 17:13:10 -08002361 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002362 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2363 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2364 if (stack != null) {
2365 return stack;
2366 }
2367 }
2368 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002369 }
2370
Wale Ogunwale68278562017-09-23 17:13:55 -07002371 /** @see ActivityDisplay#getStack(int, int) */
2372 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2373 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2374 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2375 if (stack != null) {
2376 return stack;
2377 }
2378 }
2379 return null;
2380 }
2381
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002382 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002384 // Preference is given to the activity type for the activity then the task since the type
2385 // once set shouldn't change.
2386 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2387 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2388 activityType = task.getActivityType();
2389 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002390 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2391 return activityType;
2392 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002393 if (options != null) {
2394 activityType = options.getLaunchActivityType();
2395 }
2396 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002397 }
2398
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002399 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2400 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2401 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002402 }
2403
2404 /**
2405 * Returns the right stack to use for launching factoring in all the input parameters.
2406 *
2407 * @param r The activity we are trying to launch. Can be null.
2408 * @param options The activity options used to the launch. Can be null.
2409 * @param candidateTask The possible task the activity might be launched in. Can be null.
2410 *
2411 * @return The stack to use for the launch or INVALID_STACK_ID.
2412 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002413 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2414 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2415 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002416 int taskId = INVALID_TASK_ID;
2417 int displayId = INVALID_DISPLAY;
2418 //Rect bounds = null;
2419
2420 // We give preference to the launch preference in activity options.
2421 if (options != null) {
2422 taskId = options.getLaunchTaskId();
2423 displayId = options.getLaunchDisplayId();
2424 // TODO: Need to work this into the equation...
2425 //bounds = options.getLaunchBounds();
2426 }
2427
2428 // First preference for stack goes to the task Id set in the activity options. Use the stack
2429 // associated with that if possible.
2430 if (taskId != INVALID_TASK_ID) {
2431 // Temporarily set the task id to invalid in case in re-entry.
2432 options.setLaunchTaskId(INVALID_TASK_ID);
2433 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002434 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002435 options.setLaunchTaskId(taskId);
2436 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002437 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002438 }
2439 }
2440
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002441 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002442 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002443
2444 // Next preference for stack goes to the display Id set in the activity options or the
2445 // candidate display.
2446 if (displayId == INVALID_DISPLAY) {
2447 displayId = candidateDisplayId;
2448 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002449 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002450 if (r != null) {
2451 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002452 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002453 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002454 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002455 }
2456 }
2457 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2458 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002459 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002460 if (stack != null) {
2461 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002462 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002463 }
2464 }
2465
2466 // Give preference to the stack and display of the input task and activity if they match the
2467 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002468 stack = null;
2469 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002470 if (candidateTask != null) {
2471 stack = candidateTask.getStack();
2472 }
2473 if (stack == null && r != null) {
2474 stack = r.getStack();
2475 }
2476 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002477 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002478 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002479 final int windowingMode =
2480 display.resolveWindowingMode(r, options, candidateTask, activityType);
2481 if (stack.isCompatible(windowingMode, activityType)) {
2482 return stack;
2483 }
Andrii Kulianec7d4c42018-04-26 18:31:58 -07002484 if (windowingMode == WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY
2485 && display.getSplitScreenPrimaryStack() == stack
2486 && candidateTask == stack.topTask()) {
2487 // This is a special case when we try to launch an activity that is currently on
2488 // top of split-screen primary stack, but is targeting split-screen secondary.
2489 // In this case we don't want to move it to another stack.
2490 // TODO(b/78788972): Remove after differentiating between preferred and required
2491 // launch options.
2492 return stack;
2493 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002494 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002495 }
2496
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002497 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002498 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002499 // TODO: Can be removed once we figure-out how non-standard types should launch
2500 // outside the default display.
2501 || (activityType != ACTIVITY_TYPE_STANDARD
2502 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2503 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002504 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002505
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002506 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002507 }
2508
Andrii Kulianb850ea52017-12-12 23:49:10 -08002509 /** @return true if activity record is null or can be launched on provided display. */
2510 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2511 if (r == null) {
2512 return true;
2513 }
2514 return r.canBeLaunchedOnDisplay(displayId);
2515 }
2516
Andrii Kulian16802aa2016-11-02 12:21:33 -07002517 /**
2518 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2519 * If there is no such stack, new dynamic stack can be created.
2520 * @param displayId Target display.
2521 * @param r Activity that should be launched there.
2522 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2523 */
2524 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002525 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002526 if (activityDisplay == null) {
2527 throw new IllegalArgumentException(
2528 "Display with displayId=" + displayId + " not found.");
2529 }
2530
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002531 if (!r.canBeLaunchedOnDisplay(displayId)) {
2532 return null;
2533 }
2534
Andrii Kulian16802aa2016-11-02 12:21:33 -07002535 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002536 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2537 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002538 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002539 return stack;
2540 }
2541 }
2542
2543 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002544 if (displayId != DEFAULT_DISPLAY) {
2545 return activityDisplay.createStack(
2546 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002547 }
2548
2549 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2550 return null;
2551 }
2552
Wale Ogunwale68278562017-09-23 17:13:55 -07002553 // TODO: Can probably be consolidated into getLaunchStack()...
2554 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2555 switch (stack.getActivityType()) {
2556 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2557 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2558 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002559 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002560 switch (stack.getWindowingMode()) {
2561 case WINDOWING_MODE_FULLSCREEN: return true;
2562 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2563 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2564 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2565 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2566 }
2567
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002568 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002569 return r.canBeLaunchedOnDisplay(displayId);
2570 }
2571 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2572 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002573 }
2574
Andrii Kulian7fc22812016-12-28 13:04:11 -08002575 /**
2576 * Get next focusable stack in the system. This will search across displays and stacks
2577 * in last-focused order for a focusable and visible stack, different from the target stack.
2578 *
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002579 * @param currentFocus The stack that previously had focus.
2580 * @param ignoreCurrent If we should ignore {@param currentFocus} when searching for next
2581 * candidate.
Andrii Kulian7fc22812016-12-28 13:04:11 -08002582 * @return Next focusable {@link ActivityStack}, null if not found.
2583 */
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002584 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus, boolean ignoreCurrent) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002585 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2586
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002587 final int currentWindowingMode = currentFocus != null
2588 ? currentFocus.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
2589 ActivityStack candidate = null;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002590 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2591 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002592 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002593 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
Wale Ogunwalec2ea3642018-05-24 00:32:23 -07002594 if (display == null) {
2595 // Looks like the display no longer exists in the system...
2596 continue;
2597 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002598 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2599 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002600 if (ignoreCurrent && stack == currentFocus) {
2601 continue;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002602 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002603 if (!stack.isFocusable() || !stack.shouldBeVisible(null)) {
2604 continue;
2605 }
2606
2607 if (currentWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2608 && candidate == null && stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale17696de2018-03-15 16:41:05 -07002609 // If the currently focused stack is in split-screen secondary we save off the
2610 // top primary split-screen stack as a candidate for focus because we might
2611 // prefer focus to move to an other stack to avoid primary split-screen stack
2612 // overlapping with a fullscreen stack when a fullscreen stack is higher in z
2613 // than the next split-screen stack. Assistant stack, I am looking at you...
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002614 // We only move the focus to the primary-split screen stack if there isn't a
2615 // better alternative.
2616 candidate = stack;
2617 continue;
2618 }
Wale Ogunwale17696de2018-03-15 16:41:05 -07002619 if (candidate != null && stack.inSplitScreenSecondaryWindowingMode()) {
2620 // Use the candidate stack since we are now at the secondary split-screen.
2621 return candidate;
2622 }
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002623 return stack;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002624 }
2625 }
2626
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08002627 return candidate;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002628 }
2629
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002630 /**
2631 * Get next valid stack for launching provided activity in the system. This will search across
2632 * displays and stacks in last-focused order for a focusable and visible stack, except those
2633 * that are on a currently focused display.
2634 *
2635 * @param r The activity that is being launched.
2636 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2637 * searching for the next candidate.
2638 * @return Next valid {@link ActivityStack}, null if not found.
2639 */
2640 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2641 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2642 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2643 final int displayId = mTmpOrderedDisplayIds.get(i);
2644 if (displayId == currentFocus) {
2645 continue;
2646 }
2647 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2648 if (stack != null) {
2649 return stack;
2650 }
2651 }
2652 return null;
2653 }
2654
Craig Mautneree2e45a2014-06-27 12:10:03 -07002655 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002656 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002657 }
2658
2659 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002660 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2661 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2662 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002663 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 final ArrayList<ActivityRecord> activities = task.mActivities;
2665 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2666 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002667 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002668 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002669 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 }
2671 }
2672 }
2673 }
2674 return null;
2675 }
2676
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002677 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2678 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2679 boolean deferResume) {
2680
2681 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002682 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002683 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002684 return;
2685 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002686
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002687 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002688 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002689 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002690 // If the docked stack exists, don't resize non-floating stacks independently of the
2691 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002692 return;
2693 }
2694
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002695 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002696 mWindowManager.deferSurfaceLayout();
2697 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002698 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002699 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2700 // null bounds = fullscreen windowing mode...at least for now.
2701 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2702 } else if (splitScreenActive) {
2703 // If we are in split-screen mode and this stack support split-screen, then
2704 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2705 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2706 }
2707 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002708 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002709 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002710 stack.ensureVisibleActivitiesConfigurationLocked(
2711 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002712 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002713 } finally {
2714 mWindowManager.continueSurfaceLayout();
2715 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002716 }
2717 }
2718
Winson Chungc1674272018-02-21 10:15:17 -08002719 void deferUpdateRecentsHomeStackBounds() {
2720 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
2721 deferUpdateBounds(ACTIVITY_TYPE_HOME);
2722 }
2723
Bryce Leed3624e12017-11-30 08:51:45 -08002724 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002725 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002726 if (stack != null) {
2727 stack.deferUpdateBounds();
2728 }
2729 }
2730
Winson Chungc1674272018-02-21 10:15:17 -08002731 void continueUpdateRecentsHomeStackBounds() {
2732 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
2733 continueUpdateBounds(ACTIVITY_TYPE_HOME);
2734 }
2735
Bryce Leed3624e12017-11-30 08:51:45 -08002736 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002737 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002738 if (stack != null) {
2739 stack.continueUpdateBounds();
2740 }
2741 }
2742
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002743 void notifyAppTransitionDone() {
Winson Chungc1674272018-02-21 10:15:17 -08002744 continueUpdateRecentsHomeStackBounds();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002745 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2746 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002747 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002748 if (task != null) {
2749 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002750 }
2751 }
2752 mResizingTasksDuringAnimation.clear();
2753 }
2754
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002755 /**
2756 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002757 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002758 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002759 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002760 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002761
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002762 mWindowManager.deferSurfaceLayout();
2763 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002764 final int windowingMode = fromStack.getWindowingMode();
2765 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2766 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2767
2768 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002769 // Tell the display we are exiting split-screen mode.
2770 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002771 // We are moving all tasks from the docked stack to the fullscreen stack,
2772 // which is dismissing the docked stack, so resize all other stacks to
2773 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002774 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2775 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002776 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2777 continue;
2778 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002779 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002780 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002781 }
2782
2783 // Also disable docked stack resizing since we have manually adjusted the
2784 // size of other stacks above and we don't want to trigger a docked stack
2785 // resize when we remove task from it below and it is detached from the
2786 // display because it no longer contains any tasks.
2787 mAllowDockedStackResize = false;
2788 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002789
Winson Chung5af42fc2017-03-24 17:11:33 -07002790 // If we are moving from the pinned stack, then the animation takes care of updating
2791 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002792 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002793 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002794
Wale Ogunwale388945c2017-10-04 12:13:46 -07002795 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002796 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002797 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002798 for (int i = 0; i < size; ++i) {
2799 final TaskRecord task = tasks.get(i);
2800 final ActivityStack toStack = toDisplay.getOrCreateStack(
2801 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002802
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002803 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002804 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002805 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002806 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002807 isTopTask /* animate */, DEFER_RESUME,
2808 schedulePictureInPictureModeChange,
2809 "moveTasksToFullscreenStack - onTop");
Chenjie Yuae9fdf042018-02-15 10:19:32 -08002810 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext,
2811 task.effectiveUid, task.realActivity.flattenToString());
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002812 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002813 // Position the tasks in the fullscreen stack in order at the bottom of the
2814 // stack. Also defer resume until all the tasks have been moved to the
2815 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002816 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002817 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2818 schedulePictureInPictureModeChange,
2819 "moveTasksToFullscreenStack - NOT_onTop");
2820 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002821 }
2822 }
Winson Chung74666102017-02-22 17:49:24 -08002823
2824 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2825 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002826 } finally {
2827 mAllowDockedStackResize = true;
2828 mWindowManager.continueSurfaceLayout();
2829 }
2830 }
2831
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002832 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002833 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2834 }
2835
2836 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2837 mWindowManager.inSurfaceTransaction(() ->
2838 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002839 }
2840
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002841 void setSplitScreenResizing(boolean resizing) {
2842 if (resizing == mDockedStackResizing) {
2843 return;
2844 }
2845
2846 mDockedStackResizing = resizing;
2847 mWindowManager.setDockedStackResizing(resizing);
2848
2849 if (!resizing && mHasPendingDockedBounds) {
2850 resizeDockedStackLocked(mPendingDockedBounds, mPendingTempDockedTaskBounds,
2851 mPendingTempDockedTaskInsetBounds, mPendingTempOtherTaskBounds,
2852 mPendingTempOtherTaskInsetBounds, PRESERVE_WINDOWS);
2853
2854 mHasPendingDockedBounds = false;
2855 mPendingDockedBounds = null;
2856 mPendingTempDockedTaskBounds = null;
2857 mPendingTempDockedTaskInsetBounds = null;
2858 mPendingTempOtherTaskBounds = null;
2859 mPendingTempOtherTaskInsetBounds = null;
2860 }
2861 }
2862
Jorim Jaggidc249c42015-12-15 14:57:31 -08002863 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002864 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2865 boolean preserveWindows) {
2866 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2867 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2868 false /* deferResume */);
2869 }
2870
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002871 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002872 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2873 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002874
2875 if (!mAllowDockedStackResize) {
2876 // Docked stack resize currently disabled.
2877 return;
2878 }
2879
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002880 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002881 if (stack == null) {
2882 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2883 return;
2884 }
2885
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002886 if (mDockedStackResizing) {
2887 mHasPendingDockedBounds = true;
2888 mPendingDockedBounds = copyOrNull(dockedBounds);
2889 mPendingTempDockedTaskBounds = copyOrNull(tempDockedTaskBounds);
2890 mPendingTempDockedTaskInsetBounds = copyOrNull(tempDockedTaskInsetBounds);
2891 mPendingTempOtherTaskBounds = copyOrNull(tempOtherTaskBounds);
2892 mPendingTempOtherTaskInsetBounds = copyOrNull(tempOtherTaskInsetBounds);
2893 }
2894
Jorim Jaggidc249c42015-12-15 14:57:31 -08002895 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2896 mWindowManager.deferSurfaceLayout();
2897 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002898 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2899 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002900 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002901 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002902
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002903 // TODO: Checking for isAttached might not be needed as if the user passes in null
2904 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002905 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2906 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002907 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002908 // In this case we make all other static stacks fullscreen and move all
2909 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002910 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002911
2912 // stack shouldn't contain anymore activities, so nothing to resume.
2913 r = null;
2914 } else {
2915 // Docked stacks occupy a dedicated region on screen so the size of all other
2916 // static stacks need to be adjusted so they don't overlap with the docked stack.
2917 // We get the bounds to use from window manager which has been adjusted for any
2918 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002919 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002920 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002921 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2922 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002923 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002924 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002925 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002926 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002927 continue;
2928 }
Adrian Roosa6d6aab2018-04-19 18:58:22 +02002929 if (mDockedStackResizing && !current.isTopActivityVisible()) {
2930 // Non-visible stacks get resized once we're done with the resize
2931 // interaction.
2932 continue;
2933 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002934 // Need to set windowing mode here before we try to get the dock bounds.
2935 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2936 current.getStackDockedModeBounds(
2937 tempOtherTaskBounds /* currentTempTaskBounds */,
2938 tempRect /* outStackBounds */,
2939 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2940
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002941 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002942 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2943 tempOtherTaskInsetBounds, preserveWindows,
2944 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002945 }
2946 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002947 if (!deferResume) {
2948 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2949 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002950 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002951 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002952 mWindowManager.continueSurfaceLayout();
2953 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2954 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002955 }
2956
Robert Carr0d00c2e2016-02-29 17:45:02 -08002957 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002958 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002959 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002960 if (stack == null) {
2961 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2962 return;
2963 }
Winson Chung19953ca2017-04-11 11:19:23 -07002964
2965 // It is possible for the bounds animation from the WM to call this but be delayed by
2966 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2967 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2968 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2969 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002970 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002971 return;
2972 }
2973
Robert Carr0d00c2e2016-02-29 17:45:02 -08002974 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2975 mWindowManager.deferSurfaceLayout();
2976 try {
2977 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002978 Rect insetBounds = null;
Adrian Roos3e80b602018-04-30 12:42:36 +02002979 if (tempPinnedTaskBounds != null && stack.isAnimatingBoundsToFullscreen()) {
2980 // Use 0,0 as the position for the inset rect because we are headed for fullscreen.
Robert Carr7e4c90e2017-02-15 19:52:38 -08002981 insetBounds = tempRect;
2982 insetBounds.top = 0;
2983 insetBounds.left = 0;
2984 insetBounds.right = tempPinnedTaskBounds.width();
2985 insetBounds.bottom = tempPinnedTaskBounds.height();
2986 }
Winson Chung15036ca2018-05-31 15:51:47 -07002987 if (pinnedBounds != null && tempPinnedTaskBounds == null) {
2988 // We have finished the animation into PiP, and are resizing the tasks to match the
2989 // stack bounds, while layouts are deferred, update any task state as a part of
2990 // transitioning it from fullscreen into a floating state.
2991 stack.onPipAnimationEndResize();
2992 }
Robert Carr7e4c90e2017-02-15 19:52:38 -08002993 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002994 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002995 } finally {
2996 mWindowManager.continueSurfaceLayout();
2997 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2998 }
2999 }
3000
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003001 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08003002 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003003 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07003004 /**
3005 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
3006 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
3007 * that the client receives onStop() before it is reparented. We do this by detaching
3008 * the stack from the display so that it will be considered invisible when
3009 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
3010 * invisible as well and added to the stopping list. After which we process the
3011 * stopping list by handling the idle.
3012 */
3013 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
3014 pinnedStack.mForceHidden = true;
3015 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
3016 pinnedStack.mForceHidden = false;
3017 activityIdleInternalLocked(null, false /* fromTimeout */,
3018 true /* processPausingActivites */, null /* configuration */);
3019
3020 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003021 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08003022 } else {
3023 for (int i = tasks.size() - 1; i >= 0; i--) {
3024 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07003025 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08003026 }
3027 }
3028 }
3029
3030 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003031 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07003032 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
3033 * instead moved back onto the fullscreen stack.
3034 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003035 void removeStack(ActivityStack stack) {
3036 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003037 }
3038
Wale Ogunwale68278562017-09-23 17:13:55 -07003039 /**
3040 * Removes stacks in the input windowing modes from the system if they are of activity type
3041 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
3042 */
3043 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003044 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003045 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
3046 }
3047 }
3048
3049 void removeStacksWithActivityTypes(int... activityTypes) {
3050 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3051 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003052 }
Robert Carr6914f082017-03-20 19:04:30 -07003053 }
3054
3055 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003056 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
3057 */
Winson Chung0ec2a352017-10-26 11:38:30 -07003058 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
3059 String reason) {
3060 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
3061 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07003062 }
3063
3064 /**
Winson Chung010927a2016-12-15 16:12:35 -08003065 * Removes the task with the specified task id.
3066 *
3067 * @param taskId Identifier of the task to be removed.
3068 * @param killProcess Kill any process associated with the task if possible.
3069 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07003070 * @param pauseImmediately Pauses all task activities immediately without waiting for the
3071 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08003072 * @return Returns true if the given task was found and removed.
3073 */
Winson Chung6954fc92017-03-24 16:22:12 -07003074 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07003075 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003076 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08003077 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003078 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08003079 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Bryce Lee2b8e0372018-04-05 17:01:37 -07003080 mService.getLockTaskController().clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003081 if (tr.isPersistable) {
3082 mService.notifyTaskPersisterLocked(null, true);
3083 }
3084 return true;
3085 }
3086 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
3087 return false;
3088 }
3089
3090 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
3091 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07003092 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08003093 }
3094 ComponentName component = tr.getBaseIntent().getComponent();
3095 if (component == null) {
3096 Slog.w(TAG, "No component for base intent of task: " + tr);
3097 return;
3098 }
3099
3100 // Find any running services associated with this app and stop if needed.
3101 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
3102
3103 if (!killProcess) {
3104 return;
3105 }
3106
3107 // Determine if the process(es) for this task should be killed.
3108 final String pkg = component.getPackageName();
3109 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
3110 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
3111 for (int i = 0; i < pmap.size(); i++) {
3112
3113 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
3114 for (int j = 0; j < uids.size(); j++) {
3115 ProcessRecord proc = uids.valueAt(j);
3116 if (proc.userId != tr.userId) {
3117 // Don't kill process for a different user.
3118 continue;
3119 }
3120 if (proc == mService.mHomeProcess) {
3121 // Don't kill the home process along with tasks from the same package.
3122 continue;
3123 }
3124 if (!proc.pkgList.containsKey(pkg)) {
3125 // Don't kill process that is not associated with this task.
3126 continue;
3127 }
3128
3129 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003130 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08003131 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
3132 // Don't kill process(es) that has an activity in a different task that is
3133 // also in recents.
3134 return;
3135 }
3136 }
3137
3138 if (proc.foregroundServices) {
3139 // Don't kill process(es) with foreground service.
3140 return;
3141 }
3142
3143 // Add process to kill list.
3144 procsToKill.add(proc);
3145 }
3146 }
3147
3148 // Kill the running processes.
3149 for (int i = 0; i < procsToKill.size(); i++) {
3150 ProcessRecord pr = procsToKill.get(i);
3151 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
3152 && pr.curReceivers.isEmpty()) {
3153 pr.kill("remove task", true);
3154 } else {
3155 // We delay killing processes that are not in the background or running a receiver.
3156 pr.waitingToKill = "remove task";
3157 }
3158 }
3159 }
3160
Chong Zhang5dcb2752015-08-18 13:50:26 -07003161 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07003162 * Called to restore the state of the task into the stack that it's supposed to go into.
3163 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07003164 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07003165 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003166 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003167 * @return true if the task has been restored successfully.
3168 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003169 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
3170 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07003171 final ActivityStack currentStack = task.getStack();
3172 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003173 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003174 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003175 // Nothing else to do since it is already restored in the right stack.
3176 return true;
3177 }
3178 // Remove current stack association, so we can re-associate the task with the
3179 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003180 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003181 }
3182
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003183 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003184 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07003185 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003186 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3187 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003188 final ArrayList<ActivityRecord> activities = task.mActivities;
3189 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003190 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003191 }
3192 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003193 }
3194
Winson Chung1dbc8112017-09-28 18:05:31 -07003195 @Override
3196 public void onRecentTaskAdded(TaskRecord task) {
3197 task.touchActiveTime();
3198 }
3199
3200 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003201 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07003202 if (wasTrimmed) {
3203 // Task was trimmed from the recent tasks list -- remove the active task record as well
3204 // since the user won't really be able to go back to it
3205 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3206 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
3207 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003208 task.removedFromRecents();
3209 }
3210
Wale Ogunwale040b4702015-08-06 18:10:50 -07003211 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003212 * Move stack with all its existing content to specified display.
3213 * @param stackId Id of stack to move.
3214 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003215 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003216 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003217 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003218 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003219 if (activityDisplay == null) {
3220 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3221 + displayId);
3222 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003223 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003224 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003225 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3226 + stackId);
3227 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003228
3229 final ActivityDisplay currentDisplay = stack.getDisplay();
3230 if (currentDisplay == null) {
3231 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3232 + " is not attached to any display.");
3233 }
3234
3235 if (currentDisplay.mDisplayId == displayId) {
3236 throw new IllegalArgumentException("Trying to move stack=" + stack
3237 + " to its current displayId=" + displayId);
3238 }
3239
3240 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003241 // TODO(multi-display): resize stacks properly if moved from split-screen.
3242 }
3243
3244 /**
Winson Chung74666102017-02-22 17:49:24 -08003245 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3246 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003247 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003248 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003249 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003250 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003251 final int stackId = stack.mStackId;
3252 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003253
Winson Chung74666102017-02-22 17:49:24 -08003254 // Check that we aren't reparenting to the same stack that the task is already in
3255 if (prevStack != null && prevStack.mStackId == stackId) {
3256 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3257 + " already in stackId=" + stackId);
3258 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003259 }
3260
Winson Chung74666102017-02-22 17:49:24 -08003261 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3262 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003263 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003264 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003265 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003266 }
3267
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003268 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3269 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003270 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003271 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3272 + " reparent task=" + task + " to stackId=" + stackId);
3273 }
3274
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003275 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003276 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3277 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003278 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3279 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003280 }
3281
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003282 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3283 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003284 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003285 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3286 + " Moving to a fullscreen stack instead.");
3287 if (prevStack != null) {
3288 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003289 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003290 stack = stack.getDisplay().createStack(
3291 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003292 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003293 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003294 }
3295
Winson Chung08f81892017-03-02 15:40:51 -08003296 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003297 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003298 if (stack == null) {
3299 throw new IllegalArgumentException(
3300 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3301 }
3302
3303 final ActivityRecord r = stack.topRunningActivityLocked();
3304 if (r == null) {
3305 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3306 + " in stack=" + stack);
3307 return false;
3308 }
3309
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003310 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003311 Slog.w(TAG,
3312 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003313 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003314 return false;
3315 }
3316
Winson Chung3a682872017-04-10 14:38:05 -07003317 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003318 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003319 return true;
3320 }
3321
Winson Chung8bca9e42017-04-16 15:59:43 -07003322 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003323 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003324
Wale Ogunwale480dca02016-02-06 13:58:29 -08003325 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003326
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003327 final ActivityDisplay display = r.getStack().getDisplay();
3328 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003329
Bryce Lee04ab3462017-04-10 15:06:33 -07003330 // This will clear the pinned stack by moving an existing task to the full screen stack,
3331 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003332 if (stack != null) {
3333 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3334 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003335
Wale Ogunwale1666e312016-12-16 11:27:18 -08003336 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003337 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003338
Winson Chunge55c0192017-08-24 14:50:48 -07003339 // Calculate the target bounds here before the task is reparented back into pinned windowing
3340 // mode (which will reset the saved bounds)
3341 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3342
Wale Ogunwale480dca02016-02-06 13:58:29 -08003343 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003344 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003345 // Resize the pinned stack to match the current size of the task the activity we are
3346 // going to be moving is currently contained in. We do this to have the right starting
3347 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003348 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003349 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003350 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003351
3352 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003353 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003354 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3355 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003356 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003357 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003358 // reveal/leave the other activities in their original task.
3359
3360 // Currently, we don't support reparenting activities across tasks in two different
3361 // stacks, so instead, just create a new task in the same stack, reparent the
3362 // activity into that task, and then reparent the whole task to the new stack. This
3363 // ensures that all the necessary work to migrate states in the old and new stacks
3364 // is also done.
3365 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003366 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003367 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3368
Winson Chung5af42fc2017-03-24 17:11:33 -07003369 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003370 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003371 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003372 }
Winson Chungc2baac02017-01-11 13:34:47 -08003373
3374 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3375 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003376 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003377 } finally {
3378 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003379 }
3380
Winson Chunge7ba6862017-05-24 12:13:33 -07003381 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3382 true /* fromFullscreen */);
3383
3384 // Update the visibility of all activities after the they have been reparented to the new
3385 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3386 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3387 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003388 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003389 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003390
Wale Ogunwale89be5762017-10-04 13:27:49 -07003391 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003392 }
3393
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003394 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003395 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3396 if (r == null || !r.isFocusable()) {
3397 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3398 "moveActivityStackToFront: unfocusable r=" + r);
3399 return false;
3400 }
3401
Bryce Leeaf691c02017-03-20 14:20:22 -07003402 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003403 final ActivityStack stack = r.getStack();
3404 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003405 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3406 + r + " task=" + task);
3407 return false;
3408 }
3409
Chong Zhang6cda19c2016-06-14 19:07:56 -07003410 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3411 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3412 "moveActivityStackToFront: already on top, r=" + r);
3413 return false;
3414 }
3415
3416 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3417 "moveActivityStackToFront: r=" + r);
3418
3419 stack.moveToFront(reason, task);
3420 return true;
3421 }
3422
David Stevensc6b91c62017-02-08 14:23:58 -08003423 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003424 mTmpFindTaskResult.r = null;
3425 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003426 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003427 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003428 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003429 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3430 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3431 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003432 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003433 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3434 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003435 continue;
3436 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003437 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003438 // It is possible to have tasks in multiple stacks with the same root affinity, so
3439 // we should keep looking after finding an affinity match to see if there is a
3440 // better match in another stack. Also, task affinity isn't a good enough reason
3441 // to target a display which isn't the source of the intent, so skip any affinity
3442 // matches not on the specified display.
3443 if (mTmpFindTaskResult.r != null) {
3444 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3445 return mTmpFindTaskResult.r;
3446 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003447 // Note: since the traversing through the stacks is top down, the floating
3448 // tasks should always have lower priority than any affinity-matching tasks
3449 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003450 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003451 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3452 Slog.d(TAG_TASKS, "Skipping match on different display "
3453 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003454 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003455 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003456 }
3457 }
Winson Chung5b895b72017-05-01 13:46:25 -07003458
David Stevensc6b91c62017-02-08 14:23:58 -08003459 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3460 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003461 }
3462
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003463 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003464 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003465 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003466 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3467 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3468 final ActivityStack stack = display.getChildAt(stackNdx);
3469 final ActivityRecord ar = stack.findActivityLocked(
3470 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 if (ar != null) {
3472 return ar;
3473 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003474 }
3475 }
3476 return null;
3477 }
3478
David Stevens9440dc82017-03-16 19:00:20 -07003479 boolean hasAwakeDisplay() {
3480 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3481 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3482 if (!display.shouldSleep()) {
3483 return true;
3484 }
3485 }
3486 return false;
3487 }
3488
Craig Mautner8d341ef2013-03-26 09:03:27 -07003489 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003490 scheduleSleepTimeout();
3491 if (!mGoingToSleep.isHeld()) {
3492 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003493 if (mLaunchingActivity.isHeld()) {
3494 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3495 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003496 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003497 mLaunchingActivity.release();
3498 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003499 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003500 }
David Stevens9440dc82017-03-16 19:00:20 -07003501
3502 applySleepTokensLocked(false /* applyToStacks */);
3503
3504 checkReadyForSleepLocked(true /* allowDelay */);
3505 }
3506
3507 void prepareForShutdownLocked() {
3508 for (int i = 0; i < mActivityDisplays.size(); i++) {
3509 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3510 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003511 }
3512
3513 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003514 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003515
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003516 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003517 final long endTime = System.currentTimeMillis() + timeout;
3518 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003519 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003520 long timeRemaining = endTime - System.currentTimeMillis();
3521 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003522 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003523 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003524 } catch (InterruptedException e) {
3525 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526 } else {
3527 Slog.w(TAG, "Activity manager shutdown timed out");
3528 timedout = true;
3529 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003530 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531 } else {
3532 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003533 }
3534 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003535
3536 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003537 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003538
Craig Mautner8d341ef2013-03-26 09:03:27 -07003539 return timedout;
3540 }
3541
3542 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003543 removeSleepTimeouts();
3544 if (mGoingToSleep.isHeld()) {
3545 mGoingToSleep.release();
3546 }
David Stevens9440dc82017-03-16 19:00:20 -07003547 }
3548
3549 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003551 // Set the sleeping state of the display.
3552 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3553 final boolean displayShouldSleep = display.shouldSleep();
3554 if (displayShouldSleep == display.isSleeping()) {
3555 continue;
3556 }
3557 display.setIsSleeping(displayShouldSleep);
3558
3559 if (!applyToStacks) {
3560 continue;
3561 }
3562
3563 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003564 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3565 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003566 if (displayShouldSleep) {
3567 stack.goToSleepIfPossible(false /* shuttingDown */);
3568 } else {
3569 stack.awakeFromSleepingLocked();
Lucas Dupin47a65c72018-02-15 14:16:18 -08003570 if (isFocusedStack(stack) && !getKeyguardController().isKeyguardOrAodShowing(
3571 display.mDisplayId)) {
Bryce Leed939cf02018-03-12 09:04:44 -07003572 // If the keyguard is unlocked - resume immediately.
3573 // It is possible that the display will not be awake at the time we
3574 // process the keyguard going away, which can happen before the sleep token
3575 // is released. As a result, it is important we resume the activity here.
David Stevens9440dc82017-03-16 19:00:20 -07003576 resumeFocusedStackTopActivityLocked();
3577 }
3578 }
3579 }
3580
3581 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3582 continue;
3583 }
3584 // The display is awake now, so clean up the going to sleep list.
3585 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3586 final ActivityRecord r = it.next();
3587 if (r.getDisplayId() == display.mDisplayId) {
3588 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003589 }
Craig Mautner5314a402013-09-26 12:40:16 -07003590 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003591 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003592 }
3593
3594 void activitySleptLocked(ActivityRecord r) {
3595 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003596 final ActivityStack s = r.getStack();
3597 if (s != null) {
3598 s.checkReadyForSleep();
3599 } else {
3600 checkReadyForSleepLocked(true);
3601 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003602 }
3603
David Stevens9440dc82017-03-16 19:00:20 -07003604 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003605 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003606 // Do not care.
3607 return;
3608 }
3609
David Stevens18abd0e2017-08-17 14:55:47 -07003610 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3611 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003612 }
3613
Wei Wang65c7a152016-06-02 18:51:22 -07003614 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003615 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003616
Craig Mautner0eea92c2013-05-16 13:35:39 -07003617 removeSleepTimeouts();
3618
3619 if (mGoingToSleep.isHeld()) {
3620 mGoingToSleep.release();
3621 }
3622 if (mService.mShuttingDown) {
3623 mService.notifyAll();
3624 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003625 }
3626
David Stevens18abd0e2017-08-17 14:55:47 -07003627 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3628 // successfully put to sleep.
3629 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3630 boolean allSleep = true;
3631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003632 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3633 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3634 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003635 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003636 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003637 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003638 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003639 }
3640 }
3641 }
3642 return allSleep;
3643 }
3644
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003645 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003646 // A resumed activity cannot be stopping. remove from list
3647 mStoppingActivities.remove(r);
3648
Andrii Kulian02b7a832016-10-06 23:11:56 -07003649 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003650 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003651 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003652 }
3653 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003654 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003655 mWindowManager.executeAppTransition();
3656 return true;
3657 }
3658 return false;
3659 }
3660
Craig Mautner8d341ef2013-03-26 09:03:27 -07003661 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003662 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003663 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3664 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3665 final ActivityStack stack = display.getChildAt(stackNdx);
3666 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003667 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003668 }
3669 }
3670
Craig Mautnerbb742462014-07-07 15:28:55 -07003671 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003672 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003673 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003674 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003675
3676 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003677 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003678 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003679 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003680
3681 // When launching tasks behind, update the last active time of the top task after the new
3682 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003683 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003684 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003685 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003686 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003687 }
3688
3689 void scheduleLaunchTaskBehindComplete(IBinder token) {
3690 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3691 }
3692
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003693 /**
3694 * Make sure that all activities that need to be visible in the system actually are and update
3695 * their configuration.
3696 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003697 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3698 boolean preserveWindows) {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003699 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003700 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003701 }
3702
3703 /**
3704 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
3705 */
3706 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003707 boolean preserveWindows, boolean notifyClients) {
Bryce Lee459c0622018-03-19 11:04:01 -07003708 getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02003709 try {
3710 // First the front stacks. In case any are not fullscreen and are in front of home.
3711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003712 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3713 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3714 final ActivityStack stack = display.getChildAt(stackNdx);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07003715 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07003716 notifyClients);
Jorim Jaggife762342016-10-13 14:33:27 +02003717 }
Craig Mautner580ea812013-04-25 12:58:38 -07003718 }
Jorim Jaggife762342016-10-13 14:33:27 +02003719 } finally {
Bryce Lee459c0622018-03-19 11:04:01 -07003720 getKeyguardController().endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003721 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003722 }
3723
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003724 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3725 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003726 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3727 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3728 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003729 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3730 }
3731 }
3732 }
3733
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003734 void invalidateTaskLayers() {
3735 mTaskLayersChanged = true;
3736 }
3737
3738 void rankTaskLayersIfNeeded() {
3739 if (!mTaskLayersChanged) {
3740 return;
3741 }
3742 mTaskLayersChanged = false;
3743 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003744 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003745 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003746 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3747 final ActivityStack stack = display.getChildAt(stackNdx);
3748 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003749 }
3750 }
3751 }
3752
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003753 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003755 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3756 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3757 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003758 stack.clearOtherAppTimeTrackers(except);
3759 }
3760 }
3761 }
3762
Craig Mautner8d341ef2013-03-26 09:03:27 -07003763 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003765 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3766 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3767 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003768 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003770 }
3771 }
3772
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003773 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3774 // Examine all activities currently running in the process.
3775 TaskRecord firstTask = null;
3776 // Tasks is non-null only if two or more tasks are found.
3777 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003778 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3779 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003780 ActivityRecord r = app.activities.get(i);
3781 // First, if we find an activity that is in the process of being destroyed,
3782 // then we just aren't going to do anything for now; we want things to settle
3783 // down before we try to prune more activities.
Bryce Lee7ace3952018-02-16 14:34:32 -08003784 if (r.finishing || r.isState(DESTROYING, DESTROYED)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003785 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003786 return;
3787 }
3788 // Don't consider any activies that are currently not in a state where they
3789 // can be destroyed.
Bryce Lee7ace3952018-02-16 14:34:32 -08003790 if (r.visible || !r.stopped || !r.haveState
3791 || r.isState(RESUMED, PAUSING, PAUSED, STOPPING)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003793 continue;
3794 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003795
3796 final TaskRecord task = r.getTask();
3797 if (task != null) {
3798 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003799 + " from " + r);
3800 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003801 firstTask = task;
3802 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003803 if (tasks == null) {
3804 tasks = new ArraySet<>();
3805 tasks.add(firstTask);
3806 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003807 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003808 }
3809 }
3810 }
3811 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003812 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003813 return;
3814 }
3815 // If we have activities in multiple tasks that are in a position to be destroyed,
3816 // let's iterate through the tasks and release the oldest one.
3817 final int numDisplays = mActivityDisplays.size();
3818 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003819 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3820 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003821 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003822 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3823 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003824 // Try to release activities in this stack; if we manage to, we are done.
3825 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3826 return;
3827 }
3828 }
3829 }
3830 }
3831
Amith Yamasani37a40c22015-06-17 13:25:42 -07003832 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003833 final int focusStackId = mFocusedStack.getStackId();
3834 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003835 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003836 if (dockedStack != null) {
3837 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3838 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003839 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3840 // also cause all tasks to be moved to the fullscreen stack at a position that is
3841 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003842 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003843
3844 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003845 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003846 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003847
Craig Mautner858d8a62013-04-23 17:08:34 -07003848 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003849 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003850 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3851 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3852 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003853 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003854 TaskRecord task = stack.topTask();
3855 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003856 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003857 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003858 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003859 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003860
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003861 ActivityStack stack = getStack(restoreStackId);
3862 if (stack == null) {
3863 stack = mHomeStack;
3864 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003865 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003866 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003867 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003868 } else {
3869 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003870 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003871 }
Craig Mautner93529a42013-10-04 15:03:13 -07003872 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003873 }
3874
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003875 /** Checks whether the userid is a profile of the current user. */
3876 boolean isCurrentProfileLocked(int userId) {
3877 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003878 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003879 }
3880
Bryce Leeb7c9b802017-05-02 14:20:24 -07003881 /**
3882 * Returns whether a stopping activity is present that should be stopped after visible, rather
3883 * than idle.
3884 * @return {@code true} if such activity is present. {@code false} otherwise.
3885 */
3886 boolean isStoppingNoHistoryActivity() {
3887 // Activities that are marked as nohistory should be stopped immediately after the resumed
3888 // activity has become visible.
3889 for (ActivityRecord record : mStoppingActivities) {
3890 if (record.isNoHistory()) {
3891 return true;
3892 }
3893 }
3894
3895 return false;
3896 }
3897
Winson Chung4dabf232017-01-25 13:25:22 -08003898 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3899 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003900 ArrayList<ActivityRecord> stops = null;
3901
3902 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003903 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3904 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003905 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003906 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003907 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3908 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003909 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003910 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003911 if (s.finishing) {
3912 // If this activity is finishing, it is sitting on top of
3913 // everyone else but we now know it is no longer needed...
3914 // so get rid of it. Otherwise, we need to go through the
3915 // normal flow and hide it once we determine that it is
3916 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003917 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003918 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003919 }
3920 }
David Stevens9440dc82017-03-16 19:00:20 -07003921 if (remove) {
3922 final ActivityStack stack = s.getStack();
3923 final boolean shouldSleepOrShutDown = stack != null
3924 ? stack.shouldSleepOrShutDownActivities()
3925 : mService.isSleepingOrShuttingDownLocked();
3926 if (!waitingVisible || shouldSleepOrShutDown) {
Bryce Lee7ace3952018-02-16 14:34:32 -08003927 if (!processPausingActivities && s.isState(PAUSING)) {
David Stevens9440dc82017-03-16 19:00:20 -07003928 // Defer processing pausing activities in this iteration and reschedule
3929 // a delayed idle to reprocess it again
3930 removeTimeoutsForActivityLocked(idleActivity);
3931 scheduleIdleTimeoutLocked(idleActivity);
3932 continue;
3933 }
Winson Chung4dabf232017-01-25 13:25:22 -08003934
David Stevens9440dc82017-03-16 19:00:20 -07003935 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3936 if (stops == null) {
3937 stops = new ArrayList<>();
3938 }
3939 stops.add(s);
Jorim Jaggi5db9ae42018-06-25 16:14:50 +02003940
3941 // Make sure to remove it in all cases in case we entered this block with
3942 // shouldSleepOrShutDown
3943 mActivitiesWaitingForVisibleActivity.remove(s);
David Stevens9440dc82017-03-16 19:00:20 -07003944 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003945 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003946 }
3947 }
3948
3949 return stops;
3950 }
3951
Craig Mautnercf910b02013-04-23 11:23:27 -07003952 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003953 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003954 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3955 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3956 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003957 final ActivityRecord r = stack.topRunningActivityLocked();
Bryce Lee7ace3952018-02-16 14:34:32 -08003958 final ActivityState state = r == null ? DESTROYED : r.getState();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003959 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003960 if (r == null) Slog.e(TAG,
3961 "validateTop...: null top activity, stack=" + stack);
3962 else {
3963 final ActivityRecord pausing = stack.mPausingActivity;
3964 if (pausing != null && pausing == r) Slog.e(TAG,
3965 "validateTop...: top stack has pausing activity r=" + r
3966 + " state=" + state);
3967 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3968 "validateTop...: activity in front not resumed r=" + r
3969 + " state=" + state);
3970 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003971 } else {
Bryce Leec4ab62a2018-03-05 14:19:26 -08003972 final ActivityRecord resumed = stack.getResumedActivity();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003973 if (resumed != null && resumed == r) Slog.e(TAG,
3974 "validateTop...: back stack has resumed activity r=" + r
3975 + " state=" + state);
3976 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3977 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003978 }
3979 }
3980 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003981 }
3982
Bryce Lee77a7dd62018-01-22 15:47:09 -08003983 public void dumpDisplays(PrintWriter pw) {
3984 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3985 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3986 pw.print("[id:" + display.mDisplayId + " stacks:");
3987 display.dumpStacks(pw);
3988 pw.print("]");
3989 }
3990 }
3991
Craig Mautner27084302013-03-25 08:05:25 -07003992 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003993 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003994 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003995 pw.print(prefix);
3996 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003997 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003998 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3999 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004000 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004001 }
Bryce Lee4a194382017-04-04 14:32:48 -07004002 if (!mWaitingForActivityVisible.isEmpty()) {
4003 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
4004 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
4005 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
4006 }
4007 }
Winson Chungc1674272018-02-21 10:15:17 -08004008 pw.print(prefix); pw.print("isHomeRecentsComponent=");
4009 pw.print(mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Bryce Lee4a194382017-04-04 14:32:48 -07004010
Bryce Lee459c0622018-03-19 11:04:01 -07004011 getKeyguardController().dump(pw, prefix);
Bryce Lee2b8e0372018-04-05 17:01:37 -07004012 mService.getLockTaskController().dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07004013 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004014
Yi Jin2b30f322018-02-20 15:41:47 -08004015 public void writeToProto(ProtoOutputStream proto, long fieldId) {
4016 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02004017 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004018 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4019 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
4020 activityDisplay.writeToProto(proto, DISPLAYS);
4021 }
Bryce Lee459c0622018-03-19 11:04:01 -07004022 getKeyguardController().writeToProto(proto, KEYGUARD_CONTROLLER);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004023 if (mFocusedStack != null) {
4024 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
4025 ActivityRecord focusedActivity = getResumedActivityLocked();
4026 if (focusedActivity != null) {
4027 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
4028 }
4029 } else {
4030 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
4031 }
Winson Chungc1674272018-02-21 10:15:17 -08004032 proto.write(IS_HOME_RECENTS_COMPONENT,
4033 mRecentTasks.isRecentsComponentHomeActivity(mCurrentUser));
Yi Jin2b30f322018-02-20 15:41:47 -08004034 proto.end(token);
Steven Timotius4346f0a2017-09-12 11:07:21 -07004035 }
4036
Winson43d1f262016-06-14 16:05:55 -07004037 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07004038 * Dump all connected displays' configurations.
4039 * @param prefix Prefix to apply to each line of the dump.
4040 */
4041 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
4042 pw.print(prefix); pw.println("Display override configurations:");
4043 final int displayCount = mActivityDisplays.size();
4044 for (int i = 0; i < displayCount; i++) {
4045 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
4046 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
4047 pw.println(activityDisplay.getOverrideConfiguration());
4048 }
4049 }
4050
4051 /**
Winson43d1f262016-06-14 16:05:55 -07004052 * Dumps the activities matching the given {@param name} in the either the focused stack
4053 * or all visible stacks if {@param dumpVisibleStacks} is true.
4054 */
Winson Chung6998bc42017-02-28 17:07:05 -08004055 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
4056 boolean dumpFocusedStackOnly) {
4057 if (dumpFocusedStackOnly) {
4058 return mFocusedStack.getDumpActivitiesLocked(name);
4059 } else {
Winson43d1f262016-06-14 16:05:55 -07004060 ArrayList<ActivityRecord> activities = new ArrayList<>();
4061 int numDisplays = mActivityDisplays.size();
4062 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004063 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4064 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4065 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004066 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07004067 activities.addAll(stack.getDumpActivitiesLocked(name));
4068 }
4069 }
4070 }
4071 return activities;
Winson43d1f262016-06-14 16:05:55 -07004072 }
Craig Mautner20e72272013-04-01 13:45:53 -07004073 }
4074
Dianne Hackborn390517b2013-05-30 15:03:32 -07004075 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4076 boolean needSep, String prefix) {
4077 if (activity != null) {
4078 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4079 if (needSep) {
4080 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004081 }
4082 pw.print(prefix);
4083 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004084 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004085 }
4086 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004087 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004088 }
4089
Craig Mautner8d341ef2013-03-26 09:03:27 -07004090 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4091 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004092 boolean printed = false;
4093 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004094 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4095 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004096 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004097 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004098 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4099 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4100 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004101 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004102 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07004103 + ": type=" + activityTypeToString(stack.getActivityType())
4104 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004105 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08004106 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07004107
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004108 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4109 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07004110
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4112 !dumpAll, false, dumpPackage, true,
4113 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004114
Craig Mautner4a1cb222013-12-04 16:14:06 -08004115 needSep = printed;
4116 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4117 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004118 if (pr) {
4119 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004120 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004121 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08004122 pr = printThisActivity(pw, stack.getResumedActivity(), dumpPackage, needSep,
Craig Mautner4a1cb222013-12-04 16:14:06 -08004123 " mResumedActivity: ");
4124 if (pr) {
4125 printed = true;
4126 needSep = false;
4127 }
4128 if (dumpAll) {
4129 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4130 " mLastPausedActivity: ");
4131 if (pr) {
4132 printed = true;
4133 needSep = true;
4134 }
4135 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4136 needSep, " mLastNoHistoryActivity: ");
4137 }
4138 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004139 }
4140 }
4141
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004142 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4143 false, dumpPackage, true, " Activities waiting to finish:", null);
4144 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4145 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07004146 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
4147 false, !dumpAll, false, dumpPackage, true,
4148 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004149 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4150 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004151
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004152 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004153 }
4154
Dianne Hackborn390517b2013-05-30 15:03:32 -07004155 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004156 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004157 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004158 String innerPrefix = null;
4159 String[] args = null;
4160 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004161 for (int i=list.size()-1; i>=0; i--) {
4162 final ActivityRecord r = list.get(i);
4163 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4164 continue;
4165 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004166 if (innerPrefix == null) {
4167 innerPrefix = prefix + " ";
4168 args = new String[0];
4169 }
4170 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004171 final boolean full = !brief && (complete || !r.isInHistory());
4172 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004173 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004174 needNL = false;
4175 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07004176 if (header != null) {
4177 pw.println(header);
4178 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004179 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004180 if (lastTask != r.getTask()) {
4181 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07004182 pw.print(prefix);
4183 pw.print(full ? "* " : " ");
4184 pw.println(lastTask);
4185 if (full) {
4186 lastTask.dump(pw, prefix + " ");
4187 } else if (complete) {
4188 // Complete + brief == give a summary. Isn't that obvious?!?
4189 if (lastTask.intent != null) {
4190 pw.print(prefix); pw.print(" ");
4191 pw.println(lastTask.intent.toInsecureStringWithClip());
4192 }
4193 }
4194 }
4195 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4196 pw.print(" #"); pw.print(i); pw.print(": ");
4197 pw.println(r);
4198 if (full) {
4199 r.dump(pw, innerPrefix);
4200 } else if (complete) {
4201 // Complete + brief == give a summary. Isn't that obvious?!?
4202 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4203 if (r.app != null) {
4204 pw.print(innerPrefix); pw.println(r.app);
4205 }
4206 }
4207 if (client && r.app != null && r.app.thread != null) {
4208 // flush anything that is already in the PrintWriter since the thread is going
4209 // to write to the file descriptor directly
4210 pw.flush();
4211 try {
4212 TransferPipe tp = new TransferPipe();
4213 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004214 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004215 // Short timeout, since blocking here can
4216 // deadlock with the application.
4217 tp.go(fd, 2000);
4218 } finally {
4219 tp.kill();
4220 }
4221 } catch (IOException e) {
4222 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4223 } catch (RemoteException e) {
4224 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4225 }
4226 needNL = true;
4227 }
4228 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004229 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004230 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004231
Craig Mautnerf3333272013-04-22 10:55:53 -07004232 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004233 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4234 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004235 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4236 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004237 }
4238
4239 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004240 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004241 }
4242
4243 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004244 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4245 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004246 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4247 }
4248
Craig Mautner05d29032013-05-03 13:40:13 -07004249 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004250 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4251 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4252 }
Craig Mautner05d29032013-05-03 13:40:13 -07004253 }
4254
Craig Mautner0eea92c2013-05-16 13:35:39 -07004255 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004256 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4257 }
4258
4259 final void scheduleSleepTimeout() {
4260 removeSleepTimeouts();
4261 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4262 }
4263
Craig Mautner4a1cb222013-12-04 16:14:06 -08004264 @Override
4265 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004266 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4268 }
4269
4270 @Override
4271 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004272 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004273 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4274 }
4275
4276 @Override
4277 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004278 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004279 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4280 }
4281
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004282 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004284 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004285 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004286 }
4287
Andrii Kulianca007a62016-11-22 16:33:28 -08004288 /** Check if display with specified id is added to the list. */
4289 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004290 return getActivityDisplayOrCreateLocked(displayId) != null;
4291 }
4292
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004293 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004294 ActivityDisplay getActivityDisplay(int displayId) {
4295 return mActivityDisplays.get(displayId);
4296 }
4297
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004298 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4299 ActivityDisplay getDefaultDisplay() {
4300 return mActivityDisplays.get(DEFAULT_DISPLAY);
4301 }
4302
Andrii Kulian62e6f252017-05-30 22:46:53 -07004303 /**
4304 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4305 * corresponding record in display manager.
4306 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004307 // TODO: Look into consolidating with getActivityDisplay()
4308 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004309 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4310 if (activityDisplay != null) {
4311 return activityDisplay;
4312 }
4313 if (mDisplayManager == null) {
4314 // The system isn't fully initialized yet.
4315 return null;
4316 }
4317 final Display display = mDisplayManager.getDisplay(displayId);
4318 if (display == null) {
4319 // The display is not registered in DisplayManager.
4320 return null;
4321 }
4322 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale45477b52018-03-06 12:24:19 -08004323 activityDisplay = new ActivityDisplay(this, display);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004324 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004325 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4326 mWindowManager.onDisplayAdded(displayId);
4327 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004328 }
4329
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004330 @VisibleForTesting
4331 void attachDisplay(ActivityDisplay display) {
4332 mActivityDisplays.put(display.mDisplayId, display);
4333 }
4334
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004335 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004336 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004337 mService.mContext.getResources().getDimensionPixelSize(
4338 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004339 }
4340
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004341 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004342 if (displayId == DEFAULT_DISPLAY) {
4343 throw new IllegalArgumentException("Can't remove the primary display.");
4344 }
4345
Craig Mautner4a1cb222013-12-04 16:14:06 -08004346 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004347 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4348 if (activityDisplay == null) {
4349 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004350 }
Bryce Leef19cbe22018-02-02 15:09:21 -08004351
4352 activityDisplay.remove();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004353
4354 releaseSleepTokens(activityDisplay);
4355
4356 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004357 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004358 }
4359
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004360 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004361 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004362 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004363 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004364 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004365 // The window policy is responsible for stopping activities on the default display
4366 if (displayId != Display.DEFAULT_DISPLAY) {
4367 int displayState = activityDisplay.mDisplay.getState();
4368 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4369 activityDisplay.mOffToken =
4370 mService.acquireSleepToken("Display-off", displayId);
4371 } else if (displayState == Display.STATE_ON
4372 && activityDisplay.mOffToken != null) {
4373 activityDisplay.mOffToken.release();
4374 activityDisplay.mOffToken = null;
4375 }
4376 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004377
4378 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004379 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004380 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004381 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004382 }
4383
David Stevens9440dc82017-03-16 19:00:20 -07004384 SleepToken createSleepTokenLocked(String tag, int displayId) {
4385 ActivityDisplay display = mActivityDisplays.get(displayId);
4386 if (display == null) {
4387 throw new IllegalArgumentException("Invalid display: " + displayId);
4388 }
4389
4390 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4391 mSleepTokens.add(token);
4392 display.mAllSleepTokens.add(token);
4393 return token;
4394 }
4395
4396 private void removeSleepTokenLocked(SleepTokenImpl token) {
4397 mSleepTokens.remove(token);
4398
4399 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4400 if (display != null) {
4401 display.mAllSleepTokens.remove(token);
4402 if (display.mAllSleepTokens.isEmpty()) {
4403 mService.updateSleepIfNeededLocked();
4404 }
4405 }
4406 }
4407
4408 private void releaseSleepTokens(ActivityDisplay display) {
4409 if (display.mAllSleepTokens.isEmpty()) {
4410 return;
4411 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004412 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004413 mSleepTokens.remove(token);
4414 }
4415 display.mAllSleepTokens.clear();
4416
4417 mService.updateSleepIfNeededLocked();
4418 }
4419
Wale Ogunwale68278562017-09-23 17:13:55 -07004420 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004421 final int displayId = stack.mDisplayId;
4422 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004423 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004424 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004425 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004426 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004427 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004428 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004429 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004430 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004431 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004432
4433 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4434 final int numTasks = tasks.size();
4435 int[] taskIds = new int[numTasks];
4436 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004437 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004438 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 for (int i = 0; i < numTasks; ++i) {
4440 final TaskRecord task = tasks.get(i);
4441 taskIds[i] = task.taskId;
4442 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4443 : task.realActivity != null ? task.realActivity.flattenToString()
4444 : task.getTopActivity() != null ? task.getTopActivity().packageName
4445 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004446 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004447 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004448 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449 }
4450 info.taskIds = taskIds;
4451 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004452 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004453 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004454
4455 final ActivityRecord top = stack.topRunningActivityLocked();
4456 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004457 return info;
4458 }
4459
Wale Ogunwale68278562017-09-23 17:13:55 -07004460 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004461 ActivityStack stack = getStack(stackId);
4462 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004463 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 }
4465 return null;
4466 }
4467
Wale Ogunwale68278562017-09-23 17:13:55 -07004468 StackInfo getStackInfo(int windowingMode, int activityType) {
4469 final ActivityStack stack = getStack(windowingMode, activityType);
4470 return (stack != null) ? getStackInfo(stack) : null;
4471 }
4472
Craig Mautner4a1cb222013-12-04 16:14:06 -08004473 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004474 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004475 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004476 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4477 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4478 final ActivityStack stack = display.getChildAt(stackNdx);
4479 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 }
4481 }
4482 return list;
4483 }
4484
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004485 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004486 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004487 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004488 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004489 }
4490
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004491 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004492 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004493 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004494 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004495 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004496 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004497 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004498 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004499 return;
4500 }
4501
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004502 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004503 if (isSecondaryDisplayPreferred) {
4504 final int actualDisplayId = task.getStack().mDisplayId;
4505 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004506 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004507 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004508 // The task might have landed on a display different from requested.
4509 // TODO(multi-display): Find proper stack for the task on the default display.
4510 mService.setTaskWindowingMode(task.taskId,
4511 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4512 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004513 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4514 // display with config different from global config.
4515 mService.mTaskChangeNotificationController
4516 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004517 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004518 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004519 }
4520
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004521 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004522 // Display a warning toast that we tried to put an app that doesn't support split-screen
4523 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004524 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004525
Andrii Kulianc27916642016-04-12 17:59:27 -07004526 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4527 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004528
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004529 final ActivityStack dockedStack =
4530 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004531 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004532 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004533 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004534 return;
4535 }
4536
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004537 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004538 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4539 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004540 final String packageName = topActivity.appInfo.packageName;
4541 final int reason = isSecondaryDisplayPreferred
4542 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4543 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004544 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004545 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004546 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004547 }
4548
Jorim Jaggife89d122015-12-22 16:28:44 +01004549 void activityRelaunchedLocked(IBinder token) {
4550 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004551 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4552 if (r != null) {
4553 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004554 r.setSleeping(true, true);
4555 }
4556 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004557 }
4558
4559 void activityRelaunchingLocked(ActivityRecord r) {
4560 mWindowManager.notifyAppRelaunching(r.appToken);
4561 }
4562
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004563 void logStackState() {
4564 mActivityMetricsLogger.logWindowState();
4565 }
4566
Winson Chung5af42fc2017-03-24 17:11:33 -07004567 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004568 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4569 // end, then ensure that we defer all in between multi-window mode changes
4570 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4571 return;
4572 }
4573
Wale Ogunwale22e25262016-02-01 10:32:02 -08004574 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4575 final ActivityRecord r = task.mActivities.get(i);
4576 if (r.app != null && r.app.thread != null) {
4577 mMultiWindowModeChangedActivities.add(r);
4578 }
4579 }
4580
4581 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4582 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4583 }
4584 }
4585
Winson Chung5af42fc2017-03-24 17:11:33 -07004586 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004587 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004588 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004589 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004590 return;
4591 }
4592
Bryce Leef3c6a472017-11-14 14:53:06 -08004593 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004594 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004595
Winson Chungab76bbc2017-08-14 13:33:51 -07004596 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4597 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4598 final ActivityRecord r = task.mActivities.get(i);
4599 if (r.app != null && r.app.thread != null) {
4600 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004601 // If we are scheduling pip change, then remove this activity from multi-window
4602 // change list as the processing of pip change will make sure multi-window changed
4603 // message is processed in the right order relative to pip changed.
4604 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004605 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004606 }
4607 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004608
Winson Chungab76bbc2017-08-14 13:33:51 -07004609 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4610 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4611 }
4612 }
4613
4614 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4615 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4616 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4617 final ActivityRecord r = task.mActivities.get(i);
4618 if (r.app != null && r.app.thread != null) {
4619 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004620 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004621 }
4622 }
4623
Tony Mak853304c2016-04-18 15:17:41 +01004624 void setDockedStackMinimized(boolean minimized) {
4625 mIsDockMinimized = minimized;
Wale Ogunwale17696de2018-03-15 16:41:05 -07004626 if (mIsDockMinimized) {
4627 final ActivityStack current = getFocusedStack();
4628 if (current.inSplitScreenPrimaryWindowingMode()) {
4629 // The primary split-screen stack can't be focused while it is minimize, so move
4630 // focus to something else.
4631 current.adjustFocusToNextFocusableStack("setDockedStackMinimized");
4632 }
4633 }
Tony Mak853304c2016-04-18 15:17:41 +01004634 }
4635
chaviw59b98852017-06-13 12:05:44 -07004636 void wakeUp(String reason) {
4637 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4638 }
4639
4640 /**
4641 * Begin deferring resume to avoid duplicate resumes in one pass.
4642 */
4643 private void beginDeferResume() {
4644 mDeferResumeCount++;
4645 }
4646
4647 /**
4648 * End deferring resume and determine if resume can be called.
4649 */
4650 private void endDeferResume() {
4651 mDeferResumeCount--;
4652 }
4653
4654 /**
4655 * @return True if resume can be called.
4656 */
4657 private boolean readyToResume() {
4658 return mDeferResumeCount == 0;
4659 }
4660
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004661 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004662
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004663 public ActivityStackSupervisorHandler(Looper looper) {
4664 super(looper);
4665 }
4666
Winson Chung4dabf232017-01-25 13:25:22 -08004667 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004668 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004669 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4670 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004671 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004672 }
4673
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004674 @Override
4675 public void handleMessage(Message msg) {
4676 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004677 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4678 synchronized (mService) {
4679 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4680 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004681 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004682 }
4683 }
4684 } break;
4685 case REPORT_PIP_MODE_CHANGED_MSG: {
4686 synchronized (mService) {
4687 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4688 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004689 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4690 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004691 }
4692 }
4693 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004694 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004695 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4696 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004697 // We don't at this point know if the activity is fullscreen,
4698 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004699 activityIdleInternal((ActivityRecord) msg.obj,
4700 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004701 } break;
4702 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004703 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004704 activityIdleInternal((ActivityRecord) msg.obj,
4705 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004706 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004707 case RESUME_TOP_ACTIVITY_MSG: {
4708 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004709 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004710 }
4711 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004712 case SLEEP_TIMEOUT_MSG: {
4713 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004714 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004715 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004716 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004717 }
4718 }
4719 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004720 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004721 synchronized (mService) {
4722 if (mLaunchingActivity.isHeld()) {
4723 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4724 if (VALIDATE_WAKE_LOCK_CALLER
4725 && Binder.getCallingUid() != Process.myUid()) {
4726 throw new IllegalStateException("Calling must be system uid");
4727 }
4728 mLaunchingActivity.release();
4729 }
4730 }
4731 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004732 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004733 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004734 } break;
4735 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004736 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004737 } break;
4738 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004739 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004740 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004741 case LAUNCH_TASK_BEHIND_COMPLETE: {
4742 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004743 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004744 if (r != null) {
4745 handleLaunchTaskBehindCompleteLocked(r);
4746 }
4747 }
4748 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004749
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004750 }
4751 }
4752 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004753
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004754 ActivityStack findStackBehind(ActivityStack stack) {
4755 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004756 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004757 if (display == null) {
4758 return null;
4759 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004760 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4761 if (display.getChildAt(i) == stack && i > 0) {
4762 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004763 }
4764 }
4765 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004766 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004767 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004768
Jorim Jaggic69bd222016-03-15 14:38:37 +01004769 /**
4770 * Puts a task into resizing mode during the next app transition.
4771 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004772 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004773 */
Bryce Leed3624e12017-11-30 08:51:45 -08004774 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004775 mResizingTasksDuringAnimation.add(task.taskId);
4776 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004777 }
4778
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004779 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4780 SafeActivityOptions options) {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004781 TaskRecord task = null;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004782 final String callingPackage;
4783 final Intent intent;
4784 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004785 int activityType = ACTIVITY_TYPE_UNDEFINED;
4786 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004787 final ActivityOptions activityOptions = options != null
4788 ? options.getOptions(this)
4789 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004790 if (activityOptions != null) {
4791 activityType = activityOptions.getLaunchActivityType();
4792 windowingMode = activityOptions.getLaunchWindowingMode();
4793 }
4794 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004795 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004796 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004797 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004798
Matthew Ng606dd802017-06-05 14:06:32 -07004799 mWindowManager.deferSurfaceLayout();
4800 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004801 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004802 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004803 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004804
Matthew Ng606dd802017-06-05 14:06:32 -07004805 // Defer updating the stack in which recents is until the app transition is done, to
4806 // not run into issues where we still need to draw the task in recents but the
4807 // docked stack is already created.
Winson Chungc1674272018-02-21 10:15:17 -08004808 deferUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004809 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004810 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004811
Matthew Ng606dd802017-06-05 14:06:32 -07004812 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004813 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004814 if (task == null) {
Winson Chungc1674272018-02-21 10:15:17 -08004815 continueUpdateRecentsHomeStackBounds();
Matthew Ng606dd802017-06-05 14:06:32 -07004816 mWindowManager.executeAppTransition();
4817 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004818 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004819 }
4820
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004821 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4822 // We always want to return to the home activity instead of the recents activity
4823 // from whatever is started from the recents activity, so move the home stack
4824 // forward.
4825 moveHomeStackToFront("startActivityFromRecents");
4826 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004827
Matthew Ng606dd802017-06-05 14:06:32 -07004828 // If the user must confirm credentials (e.g. when first launching a work app and the
4829 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4830 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4831 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004832 final ActivityRecord targetActivity = task.getTopActivity();
4833
Bryce Leed3624e12017-11-30 08:51:45 -08004834 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004835 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004836 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004837 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004838 true /* fromRecents */);
4839 } finally {
4840 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4841 targetActivity);
4842 }
Matthew Ng606dd802017-06-05 14:06:32 -07004843
Bryce Leed3624e12017-11-30 08:51:45 -08004844 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4845 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4846 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004847 return ActivityManager.START_TASK_TO_FRONT;
4848 }
Matthew Ng606dd802017-06-05 14:06:32 -07004849 callingPackage = task.mCallingPackage;
4850 intent = task.intent;
4851 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4852 userId = task.userId;
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004853 return mService.getActivityStartController().startActivityInPackage(
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004854 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
Makoto Onukic00ea712018-04-13 12:06:39 -07004855 null, 0, 0, options, userId, task, "startActivityFromRecents",
Michal Karpinski3eab9512018-07-20 15:32:00 +01004856 false /* validateIncomingUser */, null /* originatingPendingIntent */);
Matthew Ng606dd802017-06-05 14:06:32 -07004857 } finally {
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004858 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && task != null) {
4859 // If we are launching the task in the docked stack, put it into resizing mode so
4860 // the window renders full-screen with the background filling the void. Also only
4861 // call this at the end to make sure that tasks exists on the window manager side.
4862 setResizingDuringAnimation(task);
4863
4864 final ActivityDisplay display = task.getStack().getDisplay();
4865 final ActivityStack topSecondaryStack =
4866 display.getTopStackInWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
4867 if (topSecondaryStack.isActivityTypeHome()) {
4868 // If the home activity if the top split-screen secondary stack, then the
4869 // primary split-screen stack is in the minimized mode which means it can't
4870 // receive input keys, so we should move the focused app to the home app so that
4871 // window manager can correctly calculate the focus window that can receive
4872 // input keys.
4873 moveHomeStackToFront("startActivityFromRecents: homeVisibleInSplitScreen");
Winson Chungc1674272018-02-21 10:15:17 -08004874
4875 // Immediately update the minimized docked stack mode, the upcoming animation
4876 // for the docked activity (WMS.overridePendingAppTransitionMultiThumbFuture)
4877 // will do the animation to the target bounds
4878 mWindowManager.checkSplitScreenMinimizedChanged(false /* animate */);
Wale Ogunwaledd2ae3d2018-02-15 13:58:46 -08004879 }
4880 }
Matthew Ng606dd802017-06-05 14:06:32 -07004881 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004882 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004883 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004884
4885 /**
4886 * @return a list of activities which are the top ones in each visible stack. The first
4887 * entry will be the focused activity.
4888 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004889 List<IBinder> getTopVisibleActivities() {
4890 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4891 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004892 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004893 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4894 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004895 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4896 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004897 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004898 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004899 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004900 if (top != null) {
4901 if (stack == mFocusedStack) {
4902 topActivityTokens.add(0, top.appToken);
4903 } else {
4904 topActivityTokens.add(top.appToken);
4905 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004906 }
4907 }
4908 }
4909 }
4910 return topActivityTokens;
4911 }
Bryce Lee4a194382017-04-04 14:32:48 -07004912
4913 /**
4914 * Internal container to store a match qualifier alongside a WaitResult.
4915 */
4916 static class WaitInfo {
4917 private final ComponentName mTargetComponent;
4918 private final WaitResult mResult;
4919
4920 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4921 this.mTargetComponent = targetComponent;
4922 this.mResult = result;
4923 }
4924
Wale Ogunwale3270f172017-04-26 07:29:42 -07004925 public boolean matches(ComponentName targetComponent) {
4926 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004927 }
4928
4929 public WaitResult getResult() {
4930 return mResult;
4931 }
4932
4933 public ComponentName getComponent() {
4934 return mTargetComponent;
4935 }
4936
4937 public void dump(PrintWriter pw, String prefix) {
4938 pw.println(prefix + "WaitInfo:");
4939 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4940 pw.println(prefix + " mResult=");
4941 mResult.dump(pw, prefix);
4942 }
4943 }
David Stevens9440dc82017-03-16 19:00:20 -07004944
4945 private final class SleepTokenImpl extends SleepToken {
4946 private final String mTag;
4947 private final long mAcquireTime;
4948 private final int mDisplayId;
4949
4950 public SleepTokenImpl(String tag, int displayId) {
4951 mTag = tag;
4952 mDisplayId = displayId;
4953 mAcquireTime = SystemClock.uptimeMillis();
4954 }
4955
4956 @Override
4957 public void release() {
4958 synchronized (mService) {
4959 removeSleepTokenLocked(this);
4960 }
4961 }
4962
4963 @Override
4964 public String toString() {
4965 return "{\"" + mTag + "\", display " + mDisplayId
4966 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4967 }
4968 }
4969
Craig Mautner27084302013-03-25 08:05:25 -07004970}