blob: 831f89a1bdfadd80f67469a8fd9d5d82e6d7fe72 [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;
chaviw59b98852017-06-13 12:05:44 -070046import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010047import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020048import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
49import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070050import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070051import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070052
Winson Chung47900652017-04-06 18:44:25 -070053import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020057import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020066import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
74import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
75import static com.android.server.am.ActivityManagerService.ANIMATE;
76import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070086import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020087import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Charles Heff9b4dff2017-09-22 10:18:37 +010089import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Winson Chung74666102017-02-22 17:49:24 -080090import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
91import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
92import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung1dbc8112017-09-28 18:05:31 -070093import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Steven Timotius4346f0a2017-09-12 11:07:21 -070094import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
95import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
96import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
97import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010098import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -070099
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800100import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200101
Svetoslav7008b512015-06-24 18:47:07 -0700102import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800103import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700104import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700105import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100106import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800109import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800110import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700111import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700112import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700114import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700116import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700117import android.app.WindowConfiguration.ActivityType;
118import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800119import android.app.servertransaction.ActivityLifecycleItem;
120import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700121import android.app.servertransaction.LaunchActivityItem;
Andrii Kulian88e05cb2017-12-05 17:21:10 -0800122import android.app.servertransaction.PauseActivityItem;
123import android.app.servertransaction.ResumeActivityItem;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700125import android.content.Context;
126import android.content.Intent;
127import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700129import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.content.pm.PackageManager;
131import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100132import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800134import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800135import android.hardware.display.DisplayManager;
136import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800137import android.hardware.display.DisplayManagerInternal;
Bryce Leed3624e12017-11-30 08:51:45 -0800138import android.hardware.power.V1_0.PowerHint;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100140import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700141import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700142import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700143import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700144import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700145import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700146import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700147import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700148import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700149import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700150import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700151import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100152import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700153import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700154import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700155import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700156import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700157import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700158import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800159import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700160import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700161import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800163import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700164import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700165import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800166import android.view.Display;
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100167import android.view.RemoteAnimationAdapter;
Chong Zhangb15758a2015-11-17 12:12:03 -0800168
Andreas Gampea36dc622018-02-05 17:19:22 -0800169import com.android.internal.annotations.GuardedBy;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700170import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800171import com.android.internal.content.ReferrerIntent;
Chenjie Yu52cacc62017-12-08 18:11:45 -0800172import com.android.internal.os.logging.MetricsLoggerWrapper;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700173import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700174import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800175import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700176import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700177import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700178import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700179import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700180
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181import java.io.FileDescriptor;
182import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700183import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800184import java.lang.annotation.Retention;
185import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700186import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700187import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700188import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700189import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700190
Winson Chung1dbc8112017-09-28 18:05:31 -0700191public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
192 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700194 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700196 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700197 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700199 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700201 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800202 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800203
Craig Mautnerf3333272013-04-22 10:55:53 -0700204 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700205 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700206
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700208 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700209
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700210 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700211 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Craig Mautner05d29032013-05-03 13:40:13 -0700213 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
214 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
215 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700216 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800218 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
219 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
220 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700221 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800222 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
223 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700226
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227 // Used to indicate if an object (e.g. stack) that we are trying to get
228 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800229 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700230
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700231 // Used to indicate that windows of activities should be preserved during the resize.
232 static final boolean PRESERVE_WINDOWS = true;
233
Wale Ogunwale040b4702015-08-06 18:10:50 -0700234 // Used to indicate if an object (e.g. task) should be moved/created
235 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700236 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700237
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700238 // Don't execute any calls to resume.
239 static final boolean DEFER_RESUME = true;
240
Winson Chung010927a2016-12-15 16:12:35 -0800241 // Used to indicate that a task is removed it should also be removed from recents.
242 static final boolean REMOVE_FROM_RECENTS = true;
243
Winson Chung6954fc92017-03-24 16:22:12 -0700244 // Used to indicate that pausing an activity should occur immediately without waiting for
245 // the activity callback indicating that it has completed pausing
246 static final boolean PAUSE_IMMEDIATELY = true;
247
Winson Chung7900bee2017-03-10 08:59:25 -0800248 /**
249 * The modes which affect which tasks are returned when calling
250 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
251 */
252 @Retention(RetentionPolicy.SOURCE)
253 @IntDef({
254 MATCH_TASK_IN_STACKS_ONLY,
255 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
256 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
257 })
258 public @interface AnyTaskForIdMatchTaskMode {}
259 // Match only tasks in the current stacks
260 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
261 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
262 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
263 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
264 // provided stack id
265 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
266
Svetoslav7008b512015-06-24 18:47:07 -0700267 // Activity actions an app cannot start if it uses a permission which is not granted.
268 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
269 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700270
Svetoslav7008b512015-06-24 18:47:07 -0700271 static {
272 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
273 Manifest.permission.CAMERA);
274 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
275 Manifest.permission.CAMERA);
276 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
277 Manifest.permission.CALL_PHONE);
278 }
279
Svet Ganov99b60432015-06-27 13:15:22 -0700280 /** Action restriction: launching the activity is not restricted. */
281 private static final int ACTIVITY_RESTRICTION_NONE = 0;
282 /** Action restriction: launching the activity is restricted by a permission. */
283 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
284 /** Action restriction: launching the activity is restricted by an app op. */
285 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700286
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700287 // For debugging to make sure the caller when acquiring/releasing our
288 // wake lock is the system process.
289 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800290 /** The number of distinct task ids that can be assigned to the tasks of a single user */
291 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700292
Craig Mautner27084302013-03-25 08:05:25 -0700293 final ActivityManagerService mService;
294
Winson Chung61c9e5a2017-10-11 10:39:32 -0700295 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700296 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800297
Winson Chung61c9e5a2017-10-11 10:39:32 -0700298 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700299 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700300
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700301 final ActivityStackSupervisorHandler mHandler;
Winson Chunge2d72172018-01-25 17:46:20 +0000302 final Looper mLooper;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700303
304 /** Short cut */
305 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700307
Bryce Leeec55eb02017-12-05 20:51:27 -0800308 private LaunchParamsController mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700309
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800310 /**
311 * Maps the task identifier that activities are currently being started in to the userId of the
312 * task. Each time a new task is created, the entry for the userId of the task is incremented
313 */
314 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700315
Craig Mautner2420ead2013-04-01 17:13:20 -0700316 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700318
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 /** The stack containing the launcher app. Assumed to always be attached to
320 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800321 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700322
Craig Mautnere0a38842013-12-16 16:14:02 -0800323 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800324 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700325
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
327 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800328 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800329 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330
331 /** List of activities that are waiting for a new activity to become visible before completing
332 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700333 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
334 // mStoppingActivities when something else comes up.
335 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336
Bryce Lee4a194382017-04-04 14:32:48 -0700337 /** List of processes waiting to find out when a specific activity becomes visible. */
338 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700339
340 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700341 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700342
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 /** List of activities that are ready to be stopped, but waiting for the next activity to
344 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800345 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700346
Craig Mautnerf3333272013-04-22 10:55:53 -0700347 /** List of activities that are ready to be finished, but waiting for the previous activity to
348 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800349 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700350
Craig Mautner0eea92c2013-05-16 13:35:39 -0700351 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800352 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700353
Wale Ogunwale22e25262016-02-01 10:32:02 -0800354 /** List of activities whose multi-window mode changed that we need to report to the
355 * application */
356 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
357
358 /** List of activities whose picture-in-picture mode changed that we need to report to the
359 * application */
360 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
361
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100362 /**
363 * Animations that for the current transition have requested not to
364 * be considered for the transition animation.
365 */
366 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
367
Winson Chung5af42fc2017-03-24 17:11:33 -0700368 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
369 * the application */
370 Rect mPipModeChangedTargetStackBounds;
371
Craig Mautnerf3333272013-04-22 10:55:53 -0700372 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700373 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700374
Craig Mautnerde4ef022013-04-07 19:01:33 -0700375 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
376 * is being brought in front of us. */
377 boolean mUserLeaving = false;
378
Bryce Leed3624e12017-11-30 08:51:45 -0800379 /** Set when a power hint has started, but not ended. */
380 private boolean mPowerHintSent;
381
Craig Mautner0eea92c2013-05-16 13:35:39 -0700382 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700383 * We don't want to allow the device to go to sleep while in the process
384 * of launching an activity. This is primarily to allow alarm intent
385 * receivers to launch an activity and get that to run before the device
386 * goes back to sleep.
387 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700388 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700389
390 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700391 * Set when the system is going to sleep, until we have
392 * successfully paused the current activity and released our wake lock.
393 * At that point the system is allowed to actually sleep.
394 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700395 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700396
David Stevens9440dc82017-03-16 19:00:20 -0700397 /**
398 * A list of tokens that cause the top activity to be put to sleep.
399 * They are used by components that may hide and block interaction with underlying
400 * activities.
401 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700402 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700403
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700404 /** Stack id of the front stack when user switched, indexed by userId. */
405 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700406
Bryce Leeaf3a4002017-03-20 10:37:12 -0700407 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800408 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700409 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800411 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
412
413 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800414
Wale Ogunwaled046a012015-12-24 13:05:59 -0800415 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800416 boolean inResumeTopActivity;
417
Andrii Kulian1e32e022016-09-16 15:29:34 -0700418 /**
419 * Temporary rect used during docked stack resize calculation so we don't need to create a new
420 * object each time.
421 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700422 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700423 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700424
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700425 // The default minimal size that will be used if the activity doesn't specify its minimal size.
426 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700427 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700428
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700429 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
430 private boolean mTaskLayersChanged = true;
431
Bryce Lee2a3cc462017-10-27 10:57:35 -0700432 private ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800433
Jorim Jaggiea039a82017-08-02 14:37:49 +0200434 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
435
Andrii Kulian1779e612016-10-12 21:58:25 -0700436 @Override
437 protected int getChildCount() {
438 return mActivityDisplays.size();
439 }
440
441 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700442 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700443 return mActivityDisplays.valueAt(index);
444 }
445
446 @Override
447 protected ConfigurationContainer getParent() {
448 return null;
449 }
450
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700451 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700452 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700453 if (activityDisplay == null) {
454 throw new IllegalArgumentException("No display found with id: " + displayId);
455 }
456
457 return activityDisplay.getOverrideConfiguration();
458 }
459
460 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700461 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700462 if (activityDisplay == null) {
463 throw new IllegalArgumentException("No display found with id: " + displayId);
464 }
465
466 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
467 }
468
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700469 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700470 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
471 int callingUid, ActivityInfo activityInfo) {
472 if (displayId == DEFAULT_DISPLAY) {
473 // No restrictions for the default display.
474 return true;
475 }
476 if (!mService.mSupportsMultiDisplay) {
477 // Can't launch on secondary displays if feature is not supported.
478 return false;
479 }
480 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
481 // Can't apply wrong configuration to non-resizeable activities.
482 return false;
483 }
484 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
485 // Can't place activities to a display that has restricted launch rules.
486 // In this case the request should be made by explicitly adding target display id and
487 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
488 return false;
489 }
490 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700491 }
492
493 /**
494 * Check if configuration of specified display matches current global config.
495 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
496 * the same config as on the default display.
497 * @param displayId Id of the display to check.
498 * @return {@code true} if configuration matches.
499 */
500 private boolean displayConfigMatchesGlobal(int displayId) {
501 if (displayId == DEFAULT_DISPLAY) {
502 return true;
503 }
504 if (displayId == INVALID_DISPLAY) {
505 return false;
506 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700507 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700508 if (targetDisplay == null) {
509 throw new IllegalArgumentException("No display found with id: " + displayId);
510 }
511 return getConfiguration().equals(targetDisplay.getConfiguration());
512 }
513
Wale Ogunwale39381972015-12-17 17:15:29 -0800514 static class FindTaskResult {
515 ActivityRecord r;
516 boolean matchedByRootAffinity;
517 }
518 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
519
Craig Mautneree36c772014-07-16 14:56:05 -0700520 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800521 * Temp storage for display ids sorted in focus order.
522 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
523 * it's more efficient, as the number of displays is usually small.
524 */
525 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
526
527 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100528 * Used to keep track whether app visibilities got changed since the last pause. Useful to
529 * determine whether to invoke the task stack change listener after pausing.
530 */
531 boolean mAppVisibilitiesChangedSinceLastPause;
532
533 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100534 * Set of tasks that are in resizing mode during an app transition to fill the "void".
535 */
536 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
537
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700538
539 /**
540 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
541 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
542 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
543 * like the docked stack going empty.
544 */
545 private boolean mAllowDockedStackResize = true;
546
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100547 /**
Tony Mak853304c2016-04-18 15:17:41 +0100548 * Is dock currently minimized.
549 */
550 boolean mIsDockMinimized;
551
Bryce Lee2a3cc462017-10-27 10:57:35 -0700552 private KeyguardController mKeyguardController;
Jorim Jaggife762342016-10-13 14:33:27 +0200553
chaviw59b98852017-06-13 12:05:44 -0700554 private PowerManager mPowerManager;
555 private int mDeferResumeCount;
556
Bryce Lee2a3cc462017-10-27 10:57:35 -0700557 private boolean mInitialized;
558
Tony Mak853304c2016-04-18 15:17:41 +0100559 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700560 * Description of a request to start a new activity, which has been held
561 * due to app switches being disabled.
562 */
563 static class PendingActivityLaunch {
564 final ActivityRecord r;
565 final ActivityRecord sourceRecord;
566 final int startFlags;
567 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700568 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700569
570 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700571 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700572 r = _r;
573 sourceRecord = _sourceRecord;
574 startFlags = _startFlags;
575 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700576 callerApp = _callerApp;
577 }
578
579 void sendErrorResult(String message) {
580 try {
581 if (callerApp.thread != null) {
582 callerApp.thread.scheduleCrash(message);
583 }
584 } catch (RemoteException e) {
585 Slog.e(TAG, "Exception scheduling crash of failed "
586 + "activity launcher sourceRecord=" + sourceRecord, e);
587 }
Craig Mautneree36c772014-07-16 14:56:05 -0700588 }
589 }
590
Bryce Leeaf691c02017-03-20 14:20:22 -0700591 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700592 mService = service;
Winson Chunge2d72172018-01-25 17:46:20 +0000593 mLooper = looper;
Bryce Leeaf691c02017-03-20 14:20:22 -0700594 mHandler = new ActivityStackSupervisorHandler(looper);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700595 }
596
597 public void initialize() {
598 if (mInitialized) {
599 return;
600 }
601
602 mInitialized = true;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700603 mRunningTasks = createRunningTasks();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700604 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext,
605 mHandler.getLooper());
606 mKeyguardController = new KeyguardController(mService, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700607
Bryce Leeec55eb02017-12-05 20:51:27 -0800608 mLaunchParamsController = new LaunchParamsController(mService);
609 mLaunchParamsController.registerDefaultModifiers(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700610 }
611
Bryce Lee2a3cc462017-10-27 10:57:35 -0700612
613 public ActivityMetricsLogger getActivityMetricsLogger() {
614 return mActivityMetricsLogger;
615 }
616
617 public KeyguardController getKeyguardController() {
618 return mKeyguardController;
619 }
620
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800621 void setRecentTasks(RecentTasks recentTasks) {
622 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700623 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800624 }
625
Winson Chung3f0e59a2017-10-25 10:19:05 -0700626 @VisibleForTesting
627 RunningTasks createRunningTasks() {
628 return new RunningTasks();
629 }
630
Jeff Brown2c43c332014-06-12 22:38:59 -0700631 /**
632 * At the time when the constructor runs, the power manager has not yet been
633 * initialized. So we initialize our wakelocks afterwards.
634 */
635 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700636 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
637 mGoingToSleep = mPowerManager
638 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
639 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700640 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700641 }
642
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700643 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 synchronized (mService) {
645 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200646 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647
648 mDisplayManager =
649 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
650 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800651 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652
653 Display[] displays = mDisplayManager.getDisplays();
654 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
655 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700656 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800657 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700658 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 }
660
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700661 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
662 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 }
Craig Mautner27084302013-03-25 08:05:25 -0700664 }
665
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700666 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800667 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700668 }
669
Craig Mautnerde4ef022013-04-07 19:01:33 -0700670 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700672 }
673
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700674 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700675 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700676 }
677
Wale Ogunwaled046a012015-12-24 13:05:59 -0800678 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800679 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
680 if (!focusCandidate.isFocusable()) {
681 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800682 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800683 }
684
685 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800686 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800687 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800688
Wale Ogunwaled046a012015-12-24 13:05:59 -0800689 EventLogTags.writeAmFocusedStack(
690 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
691 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
692 }
693
694 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800695 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800696 if (r != null && r.idle) {
697 checkFinishBootingLocked();
698 }
699 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700702 void moveHomeStackToFront(String reason) {
703 mHomeStack.moveToFront(reason);
704 }
705
Matthew Ng330757d2017-02-28 14:19:17 -0800706 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700707 final ActivityStack recentsStack = getDefaultDisplay().getStack(
708 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800709 if (recentsStack != null) {
710 recentsStack.moveToFront(reason);
711 }
712 }
713
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700714 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800715 boolean moveHomeStackTaskToTop(String reason) {
716 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700717
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700718 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700719 if (top == null) {
720 return false;
721 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700722 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700723 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700724 }
725
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800726 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700727 if (!mService.mBooting && !mService.mBooted) {
728 // Not ready yet!
729 return false;
730 }
731
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800732 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700733 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800734 final String myReason = reason + " resumeHomeStackTask";
735
Mark Lua56ea122015-10-08 13:31:01 +0800736 // Only resume home activity if isn't finishing.
737 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700738 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800739 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700740 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800741 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700742 }
743
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700745 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
746 }
747
748 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700749 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700750 }
751
752 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700753 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700754 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800755 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700756 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700757 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700758 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700759 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700760 @Nullable ActivityOptions aOptions, boolean onTop) {
761 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700762 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
763 throw new IllegalArgumentException("Should not specify activity options for non-restore"
764 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800765 }
766
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800768 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700769 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
770 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
771 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700772 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700773 if (task == null) {
774 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800775 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700776 if (aOptions != null) {
777 // Resolve the stack the task should be placed in now based on options
778 // and reparent if needed.
779 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
780 if (launchStack != null && stack != launchStack) {
781 final int reparentMode = onTop
782 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
783 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
784 "anyTaskForIdLocked");
785 }
786 }
787 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700788 }
789 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800790
Winson Chung7900bee2017-03-10 08:59:25 -0800791 // If we are matching stack tasks only, return now
792 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800793 return null;
794 }
795
Winson Chung7900bee2017-03-10 08:59:25 -0800796 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
797 // the task from recents
798 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700799 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700800
801 if (task == null) {
802 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800803 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
804 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700805
806 return null;
807 }
808
809 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700810 return task;
811 }
812
Winson Chung7900bee2017-03-10 08:59:25 -0800813 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700814 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700815 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
816 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800817 return null;
818 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700819 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800820 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700821 }
822
Craig Mautner6170f732013-04-02 13:05:23 -0700823 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800825 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700826 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
827 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = display.getChildAt(stackNdx);
829 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 if (r != null) {
831 return r;
832 }
Craig Mautner6170f732013-04-02 13:05:23 -0700833 }
834 }
835 return null;
836 }
837
Tony Mak853304c2016-04-18 15:17:41 +0100838 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000839 * Detects whether we should show a lock screen in front of this task for a locked user.
840 * <p>
841 * We'll do this if either of the following holds:
842 * <ul>
843 * <li>The top activity explicitly belongs to {@param userId}.</li>
844 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
845 * </ul>
846 *
847 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100848 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000849 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
850 // To handle the case that work app is in the task but just is not the top one.
851 final ActivityRecord activityRecord = task.getTopActivity();
852 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
853
854 return (activityRecord != null && activityRecord.userId == userId)
855 || (resultTo != null && resultTo.userId == userId);
856 }
857
858 /**
859 * Find all visible task stacks containing {@param userId} and intercept them with an activity
860 * to block out the contents and possibly start a credential-confirming intent.
861 *
862 * @param userId user handle for the locked managed profile.
863 */
864 void lockAllProfileTasks(@UserIdInt int userId) {
865 mWindowManager.deferSurfaceLayout();
866 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700867 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
868 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
869 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
870 final ActivityStack stack = display.getChildAt(stackNdx);
871 final List<TaskRecord> tasks = stack.getAllTasks();
872 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
873 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000874
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700875 // Check the task for a top activity belonging to userId, or returning a
876 // result to an activity belonging to userId. Example case: a document
877 // picker for personal files, opened by a work app, should still get locked.
878 if (taskTopActivityIsUser(task, userId)) {
879 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
880 task.taskId, userId);
881 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000882 }
Tony Mak853304c2016-04-18 15:17:41 +0100883 }
884 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000885 } finally {
886 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100887 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000888 }
889
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800890 void setNextTaskIdForUserLocked(int taskId, int userId) {
891 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
892 if (taskId > currentTaskId) {
893 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700894 }
895 }
896
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700897 static int nextTaskIdForUser(int taskId, int userId) {
898 int nextTaskId = taskId + 1;
899 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
900 // Wrap around as there will be smaller task ids that are available now.
901 nextTaskId -= MAX_TASK_IDS_PER_USER;
902 }
903 return nextTaskId;
904 }
905
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800906 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800907 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
908 // for a userId u, a taskId can only be in the range
909 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
910 // 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 -0700911 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700912 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700913 || anyTaskForIdLocked(
914 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700915 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800916 if (candidateTaskId == currentTaskId) {
917 // Something wrong!
918 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
919 throw new IllegalStateException("Cannot get an available task id."
920 + " Reached limit of " + MAX_TASK_IDS_PER_USER
921 + " running tasks per user.");
922 }
923 }
924 mCurTaskIdForUser.put(userId, candidateTaskId);
925 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700926 }
927
Chong Zhang6cda19c2016-06-14 19:07:56 -0700928 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800929 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700930 if (stack == null) {
931 return null;
932 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700933 ActivityRecord resumedActivity = stack.mResumedActivity;
934 if (resumedActivity == null || resumedActivity.app == null) {
935 resumedActivity = stack.mPausingActivity;
936 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700937 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700938 }
939 }
940 return resumedActivity;
941 }
942
Dianne Hackbornff072722014-09-24 10:56:28 -0700943 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700944 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700947 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
948 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
949 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700950 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800951 continue;
952 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200953 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
954 final ActivityRecord top = stack.topRunningActivityLocked();
955 final int size = mTmpActivityList.size();
956 for (int i = 0; i < size; i++) {
957 final ActivityRecord activity = mTmpActivityList.get(i);
958 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
959 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200961 if (realStartActivityLocked(activity, app,
962 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 didSomething = true;
964 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700965 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200967 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700969 }
Craig Mautner20e72272013-04-01 13:45:53 -0700970 }
Craig Mautner20e72272013-04-01 13:45:53 -0700971 }
972 }
973 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700974 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700975 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700976 }
Craig Mautner20e72272013-04-01 13:45:53 -0700977 return didSomething;
978 }
979
980 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800981 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700982 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
983 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
984 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700985 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 continue;
987 }
988 final ActivityRecord resumedActivity = stack.mResumedActivity;
989 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700990 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800991 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 return false;
993 }
Craig Mautner20e72272013-04-01 13:45:53 -0700994 }
995 }
Wei Wang65c7a152016-06-02 18:51:22 -0700996 // Send launch end powerhint when idle
Bryce Leed3624e12017-11-30 08:51:45 -0800997 sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700998 return true;
999 }
1000
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001003 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1004 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1005 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001006 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001007 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001008 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001009 return false;
1010 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001011 }
1012 }
1013 }
1014 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001015 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
1017 mLastFocusedStack + " to=" + mFocusedStack);
1018 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001019 return true;
1020 }
1021
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001022 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001023 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001024 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001025 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1026 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1027 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001028 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +08001029 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -07001030 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001031 return false;
1032 }
1033 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 }
1036 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001037 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001038 }
1039
Craig Mautner2acc3892013-09-23 10:28:14 -07001040 /**
1041 * Pause all activities in either all of the stacks or just the back stacks.
1042 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001043 * @param resuming The resuming activity.
1044 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1045 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001046 * @return true if any activity was paused as a result of this call.
1047 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001048 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001049 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001050 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001051 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1052 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1053 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001054 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001055 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001056 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001057 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1058 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001059 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001060 }
1061 }
1062 return someActivityPaused;
1063 }
1064
Craig Mautnerde4ef022013-04-07 19:01:33 -07001065 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001066 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001067 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001068 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1069 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1070 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001071 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001072 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001073 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001074 Slog.d(TAG_STATES,
1075 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001076 pausing = false;
1077 } else {
1078 return false;
1079 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001080 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001081 }
1082 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001083 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 }
1085
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001086 void cancelInitializingActivities() {
1087 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001088 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1089 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1090 final ActivityStack stack = display.getChildAt(stackNdx);
1091 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001092 }
1093 }
1094 }
1095
Bryce Lee4a194382017-04-04 14:32:48 -07001096 void waitActivityVisible(ComponentName name, WaitResult result) {
1097 final WaitInfo waitInfo = new WaitInfo(name, result);
1098 mWaitingForActivityVisible.add(waitInfo);
1099 }
1100
1101 void cleanupActivity(ActivityRecord r) {
1102 // Make sure this record is no longer in the pending finishes list.
1103 // This could happen, for example, if we are trimming activities
1104 // down to the max limit while they are still waiting to finish.
1105 mFinishingActivities.remove(r);
1106 mActivitiesWaitingForVisibleActivity.remove(r);
1107
1108 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001109 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001110 mWaitingForActivityVisible.remove(i);
1111 }
1112 }
1113 }
1114
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001115 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001117 }
1118
1119 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1120 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001121 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1122 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001123 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001124 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001126 result.timeout = false;
1127 result.who = w.getComponent();
1128 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1129 result.thisTime = result.totalTime;
1130 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001131 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001132 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001133 if (changed) {
1134 mService.notifyAll();
1135 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001136 }
1137
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001138 void reportWaitingActivityLaunchedIfNeeded(ActivityRecord r, int result) {
1139 if (mWaitingActivityLaunched.isEmpty()) {
1140 return;
1141 }
1142
1143 if (result != START_DELIVERED_TO_TOP && result != START_TASK_TO_FRONT) {
1144 return;
1145 }
1146
Chong Zhang5022da32016-06-21 16:31:37 -07001147 boolean changed = false;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001148
Chong Zhang5022da32016-06-21 16:31:37 -07001149 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1150 WaitResult w = mWaitingActivityLaunched.remove(i);
1151 if (w.who == null) {
1152 changed = true;
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001153 w.result = result;
1154
1155 // Unlike START_TASK_TO_FRONT, When an intent is delivered to top, there
1156 // will be no followup launch signals. Assign the result and launched component.
1157 if (result == START_DELIVERED_TO_TOP) {
1158 w.who = r.realActivity;
1159 }
Chong Zhang5022da32016-06-21 16:31:37 -07001160 }
1161 }
Bryce Lee5f0e28f2018-01-30 16:00:03 -08001162
Chong Zhang5022da32016-06-21 16:31:37 -07001163 if (changed) {
1164 mService.notifyAll();
1165 }
1166 }
1167
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001168 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1169 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001170 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001171 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001172 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001173 if (w.who == null) {
1174 changed = true;
1175 w.timeout = timeout;
1176 if (r != null) {
1177 w.who = new ComponentName(r.info.packageName, r.info.name);
1178 }
1179 w.thisTime = thisTime;
1180 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001181 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001182 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001183 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001184 if (changed) {
1185 mService.notifyAll();
1186 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 }
1188
Craig Mautner29219d92013-04-16 20:19:12 -07001189 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001190 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001191 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001192 if (r != null) {
1193 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001194 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001195
Andrii Kulian7318d632016-07-20 18:59:28 -07001196 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001197 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1198
1199 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1200 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001201 final ActivityDisplay display = mActivityDisplays.get(displayId);
1202 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1203 final ActivityStack stack = display.getChildAt(j);
1204 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001205 r = stack.topRunningActivityLocked();
1206 if (r != null) {
1207 return r;
1208 }
Craig Mautner29219d92013-04-16 20:19:12 -07001209 }
1210 }
1211 }
1212 return null;
1213 }
1214
Winson Chung61c9e5a2017-10-11 10:39:32 -07001215 @VisibleForTesting
1216 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1217 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1218 int callingUid, boolean allowed) {
1219 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1220 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001221 }
1222
Todd Kennedy7440f172015-12-09 14:31:22 -08001223 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1224 ProfilerInfo profilerInfo) {
1225 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001226 if (aInfo != null) {
1227 // Store the found target back into the intent, because now that
1228 // we have it we never want to do this again. For example, if the
1229 // user navigates back to this point in the history, we should
1230 // always restart the exact same activity.
1231 intent.setComponent(new ComponentName(
1232 aInfo.applicationInfo.packageName, aInfo.name));
1233
1234 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001235 if (!aInfo.processName.equals("system")) {
1236 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001237 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001238 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001239
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001240 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1241 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1242 }
1243
Man Caocfa78b22015-06-11 20:14:34 -07001244 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1245 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1246 }
1247
1248 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001249 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001250 }
1251 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001252 final String intentLaunchToken = intent.getLaunchToken();
1253 if (aInfo.launchToken == null && intentLaunchToken != null) {
1254 aInfo.launchToken = intentLaunchToken;
1255 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001256 }
1257 return aInfo;
1258 }
1259
Todd Kennedy7440f172015-12-09 14:31:22 -08001260 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001261 return resolveIntent(intent, resolvedType, userId, 0);
1262 }
1263
1264 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001265 synchronized (mService) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001266 try {
1267 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "resolveIntent");
Patrick Baumann577d4022018-01-31 16:55:10 +00001268 int modifiedFlags = flags
1269 | PackageManager.MATCH_DEFAULT_ONLY | ActivityManagerService.STOCK_PM_FLAGS;
Patrick Baumann0da85372018-02-02 16:07:35 -08001270 if (intent.isWebIntent()
Patrick Baumann577d4022018-01-31 16:55:10 +00001271 || (intent.getFlags() & Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) != 0) {
1272 modifiedFlags |= PackageManager.MATCH_INSTANT;
1273 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001274 return mService.getPackageManagerInternalLocked().resolveIntent(
Patrick Baumann577d4022018-01-31 16:55:10 +00001275 intent, resolvedType, modifiedFlags, userId, true);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001276
1277 } finally {
1278 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
1279 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001280 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001281 }
1282
1283 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1284 ProfilerInfo profilerInfo, int userId) {
1285 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1286 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1287 }
1288
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001289 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1290 boolean andResume, boolean checkConfig) throws RemoteException {
1291
1292 if (!allPausedActivitiesComplete()) {
1293 // While there are activities pausing we skipping starting any new activities until
1294 // pauses are complete. NOTE: that we also do this for activities that are starting in
1295 // the paused state because they will first be resumed then paused on the client side.
1296 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1297 "realStartActivityLocked: Skipping start of r=" + r
1298 + " some activities pausing...");
1299 return false;
1300 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001301
Bryce Leeaf691c02017-03-20 14:20:22 -07001302 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001303 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001304
1305 beginDeferResume();
1306
Craig Mautner2420ead2013-04-01 17:13:20 -07001307 try {
chaviw59b98852017-06-13 12:05:44 -07001308 r.startFreezingScreenLocked(app, 0);
1309
1310 // schedule launch ticks to collect information about slow apps.
1311 r.startLaunchTickingLocked();
1312
Dianne Hackborn68a06332017-11-15 17:54:18 -08001313 r.setProcess(app);
chaviw59b98852017-06-13 12:05:44 -07001314
Jorim Jaggi838c2452017-08-28 15:44:43 +02001315 if (mKeyguardController.isKeyguardLocked()) {
1316 r.notifyUnknownVisibilityLaunched();
1317 }
1318
chaviw59b98852017-06-13 12:05:44 -07001319 // Have the window manager re-evaluate the orientation of the screen based on the new
1320 // activity order. Note that as a result of this, it can call back into the activity
1321 // manager with a new orientation. We don't care about that, because the activity is
1322 // not currently running so we are just restarting it anyway.
1323 if (checkConfig) {
1324 final int displayId = r.getDisplayId();
1325 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1326 getDisplayOverrideConfiguration(displayId),
1327 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1328 // Deferring resume here because we're going to launch new activity shortly.
1329 // We don't want to perform a redundant launch of the same record while ensuring
1330 // configurations and trying to resume top activity of focused stack.
1331 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1332 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 }
chaviw59b98852017-06-13 12:05:44 -07001334
1335 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1336 true /* isTop */)) {
1337 // We only set the visibility to true if the activity is allowed to be visible
1338 // based on
1339 // keyguard state. This avoids setting this into motion in window manager that is
1340 // later cancelled due to later calls to ensure visible activities that set
1341 // visibility back to false.
1342 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 }
chaviw59b98852017-06-13 12:05:44 -07001344
chaviw59b98852017-06-13 12:05:44 -07001345 final int applicationInfoUid =
1346 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1347 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1348 Slog.wtf(TAG,
1349 "User ID for activity changing for " + r
1350 + " appInfo.uid=" + r.appInfo.uid
1351 + " info.ai.uid=" + applicationInfoUid
1352 + " old=" + r.app + " new=" + app);
1353 }
1354
1355 app.waitingToKill = null;
1356 r.launchCount++;
1357 r.lastLaunchTime = SystemClock.uptimeMillis();
1358
1359 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1360
1361 int idx = app.activities.indexOf(r);
1362 if (idx < 0) {
1363 app.activities.add(r);
1364 }
1365 mService.updateLruProcessLocked(app, true, null);
1366 mService.updateOomAdjLocked();
1367
Charles Heff9b4dff2017-09-22 10:18:37 +01001368 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE
1369 || task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV
1370 || (task.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED
1371 && mService.mLockTaskController.getLockTaskModeState()
1372 == LOCK_TASK_MODE_LOCKED)) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001373 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001374 }
1375
1376 try {
1377 if (app.thread == null) {
1378 throw new RemoteException();
1379 }
1380 List<ResultInfo> results = null;
1381 List<ReferrerIntent> newIntents = null;
1382 if (andResume) {
1383 // We don't need to deliver new intents and/or set results if activity is going
1384 // to pause immediately after launch.
1385 results = r.results;
1386 newIntents = r.newIntents;
1387 }
1388 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1389 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1390 + " newIntents=" + newIntents + " andResume=" + andResume);
1391 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1392 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001393 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001394 // Home process is the root process of the task.
1395 mService.mHomeProcess = task.mActivities.get(0).app;
1396 }
1397 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1398 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1399 r.sleeping = false;
1400 r.forceNewConfig = false;
Alan Viveretteb6a25732017-11-21 14:49:24 -05001401 mService.getAppWarningsLocked().onStartActivity(r);
chaviw59b98852017-06-13 12:05:44 -07001402 mService.showAskCompatModeDialogLocked(r);
1403 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1404 ProfilerInfo profilerInfo = null;
1405 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1406 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1407 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001408 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1409 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1410 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001411 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001412 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001413 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001414 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001415 }
1416 }
chaviw59b98852017-06-13 12:05:44 -07001417
Andreas Gampe2b073a02017-06-22 17:28:57 -07001418 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001419 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 }
1421 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001422
chaviw59b98852017-06-13 12:05:44 -07001423 app.hasShownUi = true;
1424 app.pendingUiClean = true;
1425 app.forceProcessStateUpTo(mService.mTopProcessState);
1426 // Because we could be starting an Activity in the system process this may not go
1427 // across a Binder interface which would create a new Configuration. Consequently
1428 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001429
chaviw59b98852017-06-13 12:05:44 -07001430 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1431 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1432 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001433
chaviw59b98852017-06-13 12:05:44 -07001434 logIfTransactionTooLarge(r.intent, r.icicle);
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001435
1436
1437 // Create activity launch transaction.
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001438 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001439 r.appToken);
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001440 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
chaviw59b98852017-06-13 12:05:44 -07001441 System.identityHashCode(r), r.info,
1442 // TODO: Have this take the merged configuration instead of separate global
1443 // and override configs.
1444 mergedConfiguration.getGlobalConfiguration(),
1445 mergedConfiguration.getOverrideConfiguration(), r.compat,
1446 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001447 r.persistentState, results, newIntents, mService.isNextTransitionForward(),
1448 profilerInfo));
1449
1450 // Set desired final state.
1451 final ActivityLifecycleItem lifecycleItem;
1452 if (andResume) {
Bryce Leed946f862018-01-16 15:59:47 -08001453 lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward())
1454 .setDescription(r.getLifecycleDescription("realStartActivityLocked"));
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001455 } else {
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001456 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulian88e05cb2017-12-05 17:21:10 -08001457 }
1458 clientTransaction.setLifecycleStateRequest(lifecycleItem);
1459
1460 // Schedule transaction.
1461 mService.mLifecycleManager.scheduleTransaction(clientTransaction);
1462
Craig Mautner2420ead2013-04-01 17:13:20 -07001463
chaviw59b98852017-06-13 12:05:44 -07001464 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1465 // This may be a heavy-weight process! Note that the package
1466 // manager will ensure that only activity can run in the main
1467 // process of the .apk, which is the only thing that will be
1468 // considered heavy-weight.
1469 if (app.processName.equals(app.info.packageName)) {
1470 if (mService.mHeavyWeightProcess != null
1471 && mService.mHeavyWeightProcess != app) {
1472 Slog.w(TAG, "Starting new heavy weight process " + app
1473 + " when already running "
1474 + mService.mHeavyWeightProcess);
1475 }
1476 mService.mHeavyWeightProcess = app;
1477 Message msg = mService.mHandler.obtainMessage(
1478 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1479 msg.obj = r;
1480 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001481 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001482 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001483
chaviw59b98852017-06-13 12:05:44 -07001484 } catch (RemoteException e) {
1485 if (r.launchFailed) {
1486 // This is the second time we failed -- finish activity
1487 // and give up.
1488 Slog.e(TAG, "Second failure launching "
1489 + r.intent.getComponent().flattenToShortString()
1490 + ", giving up", e);
1491 mService.appDiedLocked(app);
1492 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1493 "2nd-crash", false);
1494 return false;
1495 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001496
chaviw59b98852017-06-13 12:05:44 -07001497 // This is the first time we failed -- restart process and
1498 // retry.
1499 r.launchFailed = true;
1500 app.activities.remove(r);
1501 throw e;
1502 }
1503 } finally {
1504 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001505 }
1506
1507 r.launchFailed = false;
1508 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001509 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001510 }
1511
Andrii Kulianb372da62018-01-18 10:46:24 -08001512 // TODO(lifecycler): Resume or pause requests are done as part of launch transaction,
1513 // so updating the state should be done accordingly.
chaviw59b98852017-06-13 12:05:44 -07001514 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001515 // As part of the process of launching, ActivityThread also performs
1516 // a resume.
1517 stack.minimalResumeActivityLocked(r);
1518 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001519 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001520 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001521 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001522 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001523 "Moving to PAUSED: " + r + " (starting in paused state)");
1524 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001525 }
1526
1527 // Launch the new version setup screen if needed. We do this -after-
1528 // launching the initial activity (that is, home), so that it can have
1529 // a chance to initialize itself while in the background, making the
1530 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001531 if (isFocusedStack(stack)) {
Bryce Leed3624e12017-11-30 08:51:45 -08001532 mService.getActivityStartController().startSetupActivity();
Craig Mautner2420ead2013-04-01 17:13:20 -07001533 }
1534
Dianne Hackborn465fa392014-09-14 14:21:18 -07001535 // Update any services we are bound to that might care about whether
1536 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001537 if (r.app != null) {
1538 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1539 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001540
Craig Mautner2420ead2013-04-01 17:13:20 -07001541 return true;
1542 }
1543
Sudheer Shankafab200f2017-05-17 20:41:53 -07001544 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1545 int extrasSize = 0;
1546 if (intent != null) {
1547 final Bundle extras = intent.getExtras();
1548 if (extras != null) {
1549 extrasSize = extras.getSize();
1550 }
1551 }
1552 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1553 if (extrasSize + icicleSize > 200000) {
1554 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1555 + ", icicle size: " + icicleSize);
1556 }
1557 }
1558
Craig Mautnere79d42682013-04-01 19:01:53 -07001559 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001560 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001561 // Is this activity's application already running?
1562 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001563 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001564
Andrii Kulian02b7a832016-10-06 23:11:56 -07001565 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001566
1567 if (app != null && app.thread != null) {
1568 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001569 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1570 || !"android".equals(r.info.packageName)) {
1571 // Don't add this if it is a platform component that is marked
1572 // to run in multiple processes, because this is actually
1573 // part of the framework so doesn't make sense to track as a
1574 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001575 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1576 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001577 }
George Mount2c92c972014-03-20 09:38:23 -07001578 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001579 return;
1580 } catch (RemoteException e) {
1581 Slog.w(TAG, "Exception when starting activity "
1582 + r.intent.getComponent().flattenToShortString(), e);
1583 }
1584
1585 // If a dead object exception was thrown -- fall through to
1586 // restart the application.
1587 }
1588
1589 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001590 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001591 }
1592
Bryce Leed3624e12017-11-30 08:51:45 -08001593 void sendPowerHintForLaunchStartIfNeeded(boolean forceSend, ActivityRecord targetActivity) {
1594 boolean sendHint = forceSend;
1595
1596 if (!sendHint) {
1597 // If not forced, send power hint when the activity's process is different than the
1598 // current resumed activity.
1599 final ActivityRecord resumedActivity = getResumedActivityLocked();
1600 sendHint = resumedActivity == null
1601 || resumedActivity.app == null
1602 || !resumedActivity.app.equals(targetActivity.app);
1603 }
1604
1605 if (sendHint && mService.mLocalPowerManager != null) {
1606 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 1);
1607 mPowerHintSent = true;
1608 }
1609 }
1610
1611 void sendPowerHintForLaunchEndIfNeeded() {
1612 // Trigger launch power hint if activity is launched
1613 if (mPowerHintSent && mService.mLocalPowerManager != null) {
1614 mService.mLocalPowerManager.powerHint(PowerHint.LAUNCH, 0);
1615 mPowerHintSent = false;
1616 }
1617 }
1618
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001619 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001620 String resultWho, int requestCode, int callingPid, int callingUid,
1621 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01001622 ActivityRecord resultRecord, ActivityStack resultStack) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001623 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1624 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001625 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001626 return true;
1627 }
1628 final int componentRestriction = getComponentRestrictionForCallingPackage(
1629 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1630 final int actionRestriction = getActionRestrictionForCallingPackage(
1631 intent.getAction(), callingPackage, callingPid, callingUid);
1632 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1633 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1634 if (resultRecord != null) {
1635 resultStack.sendActivityResultLocked(-1,
1636 resultRecord, resultWho, requestCode,
1637 Activity.RESULT_CANCELED, null);
1638 }
1639 final String msg;
1640 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1641 msg = "Permission Denial: starting " + intent.toString()
1642 + " from " + callerApp + " (pid=" + callingPid
1643 + ", uid=" + callingUid + ")" + " with revoked permission "
1644 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1645 } else if (!aInfo.exported) {
1646 msg = "Permission Denial: starting " + intent.toString()
1647 + " from " + callerApp + " (pid=" + callingPid
1648 + ", uid=" + callingUid + ")"
1649 + " not exported from uid " + aInfo.applicationInfo.uid;
1650 } else {
1651 msg = "Permission Denial: starting " + intent.toString()
1652 + " from " + callerApp + " (pid=" + callingPid
1653 + ", uid=" + callingUid + ")"
1654 + " requires " + aInfo.permission;
1655 }
1656 Slog.w(TAG, msg);
1657 throw new SecurityException(msg);
1658 }
1659
1660 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1661 final String message = "Appop Denial: starting " + intent.toString()
1662 + " from " + callerApp + " (pid=" + callingPid
1663 + ", uid=" + callingUid + ")"
1664 + " requires " + AppOpsManager.permissionToOp(
1665 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1666 Slog.w(TAG, message);
1667 return false;
1668 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1669 final String message = "Appop Denial: starting " + intent.toString()
1670 + " from " + callerApp + " (pid=" + callingPid
1671 + ", uid=" + callingUid + ")"
1672 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1673 Slog.w(TAG, message);
1674 return false;
1675 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001676
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001677 return true;
1678 }
1679
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001680 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001681 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1682 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001683 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1684 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1685
Andrii Kulian02689a72017-07-06 14:28:59 -07001686 if (callingPid == -1 && callingUid == -1) {
1687 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1688 return true;
1689 }
1690
Andrii Kulian62e6f252017-05-30 22:46:53 -07001691 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001692 if (activityDisplay == null) {
1693 Slog.w(TAG, "Launch on display check: display not found");
1694 return false;
1695 }
1696
Andrii Kulian02689a72017-07-06 14:28:59 -07001697 // Check if the caller has enough privileges to embed activities and launch to private
1698 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001699 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001700 callingUid);
1701 if (startAnyPerm == PERMISSION_GRANTED) {
1702 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1703 + " allow launch any on display");
1704 return true;
1705 }
1706
Andrii Kulian8f070292017-09-12 22:56:49 -07001707 // Check if caller is already present on display
1708 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1709
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001710 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1711 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1712 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001713 // Limit launching on virtual displays, because their contents can be read from Surface
1714 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001715 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1716 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1717 + " disallow launch on virtual display for not-embedded activity.");
1718 return false;
1719 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001720 // Check if the caller is allowed to embed activities from other apps.
1721 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001722 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001723 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1724 + " disallow activity embedding without permission.");
1725 return false;
1726 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001727 }
1728
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001729 if (!activityDisplay.isPrivate()) {
1730 // Anyone can launch on a public display.
1731 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1732 + " allow launch on public display");
1733 return true;
1734 }
1735
1736 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001737 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001738 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1739 + " allow launch for owner of the display");
1740 return true;
1741 }
1742
Andrii Kulian8f070292017-09-12 22:56:49 -07001743 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001744 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1745 + " allow launch for caller present on the display");
1746 return true;
1747 }
1748
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001749 Slog.w(TAG, "Launch on display check: denied");
1750 return false;
1751 }
1752
1753 /** Update lists of UIDs that are present on displays and have access to them. */
1754 void updateUIDsPresentOnDisplay() {
1755 mDisplayAccessUIDs.clear();
1756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1757 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001758 // Only bother calculating the whitelist for private displays
1759 if (activityDisplay.isPrivate()) {
1760 mDisplayAccessUIDs.append(
1761 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1762 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001763 }
1764 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1765 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1766 }
1767
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001768 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001769 final long identity = Binder.clearCallingIdentity();
1770 try {
1771 return UserManager.get(mService.mContext).getUserInfo(userId);
1772 } finally {
1773 Binder.restoreCallingIdentity(identity);
1774 }
1775 }
1776
Svet Ganov99b60432015-06-27 13:15:22 -07001777 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001778 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001779 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1780 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001781 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001782 return ACTIVITY_RESTRICTION_PERMISSION;
1783 }
1784
Christopher Tateff7add02015-08-17 10:23:22 -07001785 if (activityInfo.permission == null) {
1786 return ACTIVITY_RESTRICTION_NONE;
1787 }
1788
Svet Ganov99b60432015-06-27 13:15:22 -07001789 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1790 if (opCode == AppOpsManager.OP_NONE) {
1791 return ACTIVITY_RESTRICTION_NONE;
1792 }
1793
1794 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1795 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001796 if (!ignoreTargetSecurity) {
1797 return ACTIVITY_RESTRICTION_APPOP;
1798 }
Svet Ganov99b60432015-06-27 13:15:22 -07001799 }
1800
1801 return ACTIVITY_RESTRICTION_NONE;
1802 }
1803
Svetoslav7008b512015-06-24 18:47:07 -07001804 private int getActionRestrictionForCallingPackage(String action,
1805 String callingPackage, int callingPid, int callingUid) {
1806 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001807 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001808 }
1809
1810 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1811 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001812 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001813 }
1814
1815 final PackageInfo packageInfo;
1816 try {
1817 packageInfo = mService.mContext.getPackageManager()
1818 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1819 } catch (PackageManager.NameNotFoundException e) {
1820 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001821 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001822 }
1823
1824 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001825 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001826 }
1827
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001828 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001829 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001830 }
1831
1832 final int opCode = AppOpsManager.permissionToOpCode(permission);
1833 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001834 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001835 }
1836
1837 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1838 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001839 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001840 }
1841
Svet Ganov99b60432015-06-27 13:15:22 -07001842 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001843 }
1844
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001845 void setLaunchSource(int uid) {
1846 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1847 }
1848
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001849 void acquireLaunchWakelock() {
1850 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1851 throw new IllegalStateException("Calling must be system uid");
1852 }
1853 mLaunchingActivity.acquire();
1854 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1855 // To be safe, don't allow the wake lock to be held for too long.
1856 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1857 }
1858 }
1859
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001860 /**
1861 * Called when the frontmost task is idle.
1862 * @return the state of mService.mBooting before this was called.
1863 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001864 @GuardedBy("mService")
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001865 private boolean checkFinishBootingLocked() {
1866 final boolean booting = mService.mBooting;
1867 boolean enableScreen = false;
1868 mService.mBooting = false;
1869 if (!mService.mBooted) {
1870 mService.mBooted = true;
1871 enableScreen = true;
1872 }
1873 if (booting || enableScreen) {
1874 mService.postFinishBooting(booting, enableScreen);
1875 }
1876 return booting;
1877 }
1878
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 // Checked.
Andreas Gampea36dc622018-02-05 17:19:22 -08001880 @GuardedBy("mService")
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001882 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001883 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001884
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001886 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 int NS = 0;
1888 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001889 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 boolean activityRemoved = false;
1891
Wale Ogunwale7d701172015-03-11 15:36:30 -07001892 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001893 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001894 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1895 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001896 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1897 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001898 if (fromTimeout) {
1899 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001900 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001901
1902 // This is a hack to semi-deal with a race condition
1903 // in the client where it can be constructed with a
1904 // newer configuration from when we asked it to launch.
1905 // We'll update with whatever configuration it now says
1906 // it used to launch.
1907 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001908 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001909 }
1910
1911 // We are now idle. If someone is waiting for a thumbnail from
1912 // us, we can now deliver.
1913 r.idle = true;
1914
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001915 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001916 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001917 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001918 }
1919 }
1920
1921 if (allResumedActivitiesIdle()) {
1922 if (r != null) {
1923 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001924 }
1925
1926 if (mLaunchingActivity.isHeld()) {
1927 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1928 if (VALIDATE_WAKE_LOCK_CALLER &&
1929 Binder.getCallingUid() != Process.myUid()) {
1930 throw new IllegalStateException("Calling must be system uid");
1931 }
1932 mLaunchingActivity.release();
1933 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001934 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 }
1936
1937 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001938 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1939 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001941 if ((NF = mFinishingActivities.size()) > 0) {
1942 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001943 mFinishingActivities.clear();
1944 }
1945
Craig Mautnerf3333272013-04-22 10:55:53 -07001946 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001947 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001948 mStartingUsers.clear();
1949 }
1950
Craig Mautnerf3333272013-04-22 10:55:53 -07001951 // Stop any activities that are scheduled to do so but have been
1952 // waiting for the next one to start.
1953 for (int i = 0; i < NS; i++) {
1954 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001955 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001956 if (stack != null) {
1957 if (r.finishing) {
1958 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1959 } else {
1960 stack.stopActivityLocked(r);
1961 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001962 }
1963 }
1964
1965 // Finish any activities that are scheduled to do so but have been
1966 // waiting for the next one to start.
1967 for (int i = 0; i < NF; i++) {
1968 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001969 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001970 if (stack != null) {
1971 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1972 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001973 }
1974
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001975 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001976 // Complete user switch
1977 if (startingUsers != null) {
1978 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001979 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001980 }
1981 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001982 }
1983
1984 mService.trimApplications();
1985 //dump();
1986 //mWindowManager.dump();
1987
Craig Mautnerf3333272013-04-22 10:55:53 -07001988 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001989 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001990 }
1991
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001992 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001993 }
1994
Craig Mautner8e569572013-10-11 17:36:59 -07001995 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001996 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001998 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1999 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2000 final ActivityStack stack = display.getChildAt(stackNdx);
2001 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002002 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002003 }
Craig Mautner19091252013-10-05 00:03:53 -07002004 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002005 }
2006
2007 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002009 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2010 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2011 final ActivityStack stack = display.getChildAt(stackNdx);
2012 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002013 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002014 }
2015 }
2016
Craig Mautner93529a42013-10-04 15:03:13 -07002017 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002018 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002019 }
2020
Craig Mautner8d341ef2013-03-26 09:03:27 -07002021 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002022 * Update the last used stack id for non-current user (current user's last
2023 * used stack is the focused stack)
2024 */
2025 void updateUserStackLocked(int userId, ActivityStack stack) {
2026 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002027 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002028 }
2029 }
2030
2031 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002032 * @return true if some activity was finished (or would have finished if doit were true).
2033 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002034 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2035 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002036 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002037 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002038 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2039 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2040 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002041 if (stack.finishDisabledPackageActivitiesLocked(
2042 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002043 didSomething = true;
2044 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002045 }
2046 }
2047 return didSomething;
2048 }
2049
Dianne Hackborna413dc02013-07-12 12:02:55 -07002050 void updatePreviousProcessLocked(ActivityRecord r) {
2051 // Now that this process has stopped, we may want to consider
2052 // it to be the previous app to try to keep around in case
2053 // the user wants to return to it.
2054
2055 // First, found out what is currently the foreground app, so that
2056 // we don't blow away the previous app if this activity is being
2057 // hosted by the process that is actually still the foreground.
2058 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002059 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002060 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2061 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2062 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002063 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002064 if (stack.mResumedActivity != null) {
2065 fgApp = stack.mResumedActivity.app;
2066 } else if (stack.mPausingActivity != null) {
2067 fgApp = stack.mPausingActivity.app;
2068 }
2069 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002070 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002071 }
2072 }
2073
2074 // Now set this one as the previous process, only if that really
2075 // makes sense to.
2076 if (r.app != null && fgApp != null && r.app != fgApp
2077 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002078 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002079 mService.mPreviousProcess = r.app;
2080 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2081 }
2082 }
2083
Wale Ogunwaled046a012015-12-24 13:05:59 -08002084 boolean resumeFocusedStackTopActivityLocked() {
2085 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002086 }
2087
Wale Ogunwaled046a012015-12-24 13:05:59 -08002088 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002089 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002090
2091 if (!readyToResume()) {
2092 return false;
2093 }
2094
Wale Ogunwaled046a012015-12-24 13:05:59 -08002095 if (targetStack != null && isFocusedStack(targetStack)) {
2096 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002097 }
chaviw59b98852017-06-13 12:05:44 -07002098
Wale Ogunwale06579d62016-04-30 15:29:06 -07002099 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2100 if (r == null || r.state != RESUMED) {
2101 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002102 } else if (r.state == RESUMED) {
2103 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2104 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002105 }
chaviw59b98852017-06-13 12:05:44 -07002106
Wale Ogunwaled046a012015-12-24 13:05:59 -08002107 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002108 }
2109
Todd Kennedy39bfee52016-02-24 10:28:21 -08002110 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2111 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002112 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2113 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2114 final ActivityStack stack = display.getChildAt(stackNdx);
2115 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002116 }
2117 }
2118 }
2119
Adrian Roos20d7df32016-01-12 18:59:43 +01002120 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2121 TaskRecord finishedTask = null;
2122 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002123 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002124 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2125 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002126 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002127 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002128 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2129 if (stack == focusedStack || finishedTask == null) {
2130 finishedTask = t;
2131 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002132 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002133 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002134 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002135 }
2136
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002137 void finishVoiceTask(IVoiceInteractionSession session) {
2138 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002139 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2140 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002141 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002142 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002143 stack.finishVoiceTask(session);
2144 }
2145 }
2146 }
2147
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01002148 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options, String reason,
2149 boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002150 final ActivityStack currentStack = task.getStack();
2151 if (currentStack == null) {
2152 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2153 + task + " to front. Stack is null");
2154 return;
2155 }
2156
Craig Mautneraea74a52014-03-08 14:23:10 -08002157 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2158 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002159 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002160
2161 final ActivityRecord prev = topRunningActivityLocked();
2162
2163 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2164 || (prev != null && prev.isActivityTypeRecents())) {
2165 // Caller wants the home activity moved with it or the previous task is recents in which
2166 // case we always return home from the task we are moving to the front.
2167 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002168 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002169
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002170 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002171 final Rect bounds = options.getLaunchBounds();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002172 task.updateOverrideConfiguration(bounds);
2173
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002174 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002175
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002176 if (stack != currentStack) {
2177 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002178 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002179 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002180 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2181 // still need moveTaskToFrontLocked() below for any transition settings.
2182 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002183 if (stack.resizeStackWithLaunchBounds()) {
2184 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002185 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2186 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002187 } else {
2188 // WM resizeTask must be done after the task is moved to the correct stack,
2189 // because Task's setBounds() also updates dim layer's bounds, but that has
2190 // dependency on the stack.
2191 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002192 }
2193 }
2194
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002195 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002196 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002197 r == null ? null : r.appTimeTracker, reason);
2198
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002199 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002200 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002201
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002202 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002203 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002204 }
2205
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002206 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002207 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002208 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002209 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002210 return false;
2211 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002212 return (mService.mSupportsPictureInPicture
2213 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002214 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002215 }
2216
Bryce Leeec55eb02017-12-05 20:51:27 -08002217 LaunchParamsController getLaunchParamsController() {
2218 return mLaunchParamsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002219 }
2220
Winson Chung55893332017-02-17 17:13:10 -08002221 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002222 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2223 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2224 if (stack != null) {
2225 return stack;
2226 }
2227 }
2228 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002229 }
2230
Wale Ogunwale68278562017-09-23 17:13:55 -07002231 /** @see ActivityDisplay#getStack(int, int) */
2232 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2233 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2234 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2235 if (stack != null) {
2236 return stack;
2237 }
2238 }
2239 return null;
2240 }
2241
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002242 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002243 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002244 // Preference is given to the activity type for the activity then the task since the type
2245 // once set shouldn't change.
2246 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2247 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2248 activityType = task.getActivityType();
2249 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002250 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2251 return activityType;
2252 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002253 if (options != null) {
2254 activityType = options.getLaunchActivityType();
2255 }
2256 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002257 }
2258
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002259 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2260 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2261 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002262 }
2263
2264 /**
2265 * Returns the right stack to use for launching factoring in all the input parameters.
2266 *
2267 * @param r The activity we are trying to launch. Can be null.
2268 * @param options The activity options used to the launch. Can be null.
2269 * @param candidateTask The possible task the activity might be launched in. Can be null.
2270 *
2271 * @return The stack to use for the launch or INVALID_STACK_ID.
2272 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002273 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2274 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2275 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002276 int taskId = INVALID_TASK_ID;
2277 int displayId = INVALID_DISPLAY;
2278 //Rect bounds = null;
2279
2280 // We give preference to the launch preference in activity options.
2281 if (options != null) {
2282 taskId = options.getLaunchTaskId();
2283 displayId = options.getLaunchDisplayId();
2284 // TODO: Need to work this into the equation...
2285 //bounds = options.getLaunchBounds();
2286 }
2287
2288 // First preference for stack goes to the task Id set in the activity options. Use the stack
2289 // associated with that if possible.
2290 if (taskId != INVALID_TASK_ID) {
2291 // Temporarily set the task id to invalid in case in re-entry.
2292 options.setLaunchTaskId(INVALID_TASK_ID);
2293 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002294 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002295 options.setLaunchTaskId(taskId);
2296 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002297 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002298 }
2299 }
2300
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002301 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002302 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002303
2304 // Next preference for stack goes to the display Id set in the activity options or the
2305 // candidate display.
2306 if (displayId == INVALID_DISPLAY) {
2307 displayId = candidateDisplayId;
2308 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08002309 if (displayId != INVALID_DISPLAY && canLaunchOnDisplay(r, displayId)) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002310 if (r != null) {
2311 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002312 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002313 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002314 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315 }
2316 }
2317 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2318 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002319 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002320 if (stack != null) {
2321 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002322 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002323 }
2324 }
2325
2326 // Give preference to the stack and display of the input task and activity if they match the
2327 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002328 stack = null;
2329 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002330 if (candidateTask != null) {
2331 stack = candidateTask.getStack();
2332 }
2333 if (stack == null && r != null) {
2334 stack = r.getStack();
2335 }
2336 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002337 display = stack.getDisplay();
Andrii Kulianb850ea52017-12-12 23:49:10 -08002338 if (display != null && canLaunchOnDisplay(r, display.mDisplayId)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002339 final int windowingMode =
2340 display.resolveWindowingMode(r, options, candidateTask, activityType);
2341 if (stack.isCompatible(windowingMode, activityType)) {
2342 return stack;
2343 }
2344 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002345 }
2346
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002347 if (display == null
Andrii Kulianb850ea52017-12-12 23:49:10 -08002348 || !canLaunchOnDisplay(r, display.mDisplayId)
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002349 // TODO: Can be removed once we figure-out how non-standard types should launch
2350 // outside the default display.
2351 || (activityType != ACTIVITY_TYPE_STANDARD
2352 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2353 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002354 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002355
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002356 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002357 }
2358
Andrii Kulianb850ea52017-12-12 23:49:10 -08002359 /** @return true if activity record is null or can be launched on provided display. */
2360 private boolean canLaunchOnDisplay(ActivityRecord r, int displayId) {
2361 if (r == null) {
2362 return true;
2363 }
2364 return r.canBeLaunchedOnDisplay(displayId);
2365 }
2366
Andrii Kulian16802aa2016-11-02 12:21:33 -07002367 /**
2368 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2369 * If there is no such stack, new dynamic stack can be created.
2370 * @param displayId Target display.
2371 * @param r Activity that should be launched there.
2372 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2373 */
2374 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002375 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002376 if (activityDisplay == null) {
2377 throw new IllegalArgumentException(
2378 "Display with displayId=" + displayId + " not found.");
2379 }
2380
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002381 if (!r.canBeLaunchedOnDisplay(displayId)) {
2382 return null;
2383 }
2384
Andrii Kulian16802aa2016-11-02 12:21:33 -07002385 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002386 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2387 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002388 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002389 return stack;
2390 }
2391 }
2392
2393 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002394 if (displayId != DEFAULT_DISPLAY) {
2395 return activityDisplay.createStack(
2396 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002397 }
2398
2399 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2400 return null;
2401 }
2402
Wale Ogunwale68278562017-09-23 17:13:55 -07002403 // TODO: Can probably be consolidated into getLaunchStack()...
2404 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2405 switch (stack.getActivityType()) {
2406 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2407 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2408 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002409 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002410 switch (stack.getWindowingMode()) {
2411 case WINDOWING_MODE_FULLSCREEN: return true;
2412 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2413 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2414 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2415 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2416 }
2417
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002418 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002419 return r.canBeLaunchedOnDisplay(displayId);
2420 }
2421 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2422 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002423 }
2424
Andrii Kulian7fc22812016-12-28 13:04:11 -08002425 /**
2426 * Get next focusable stack in the system. This will search across displays and stacks
2427 * in last-focused order for a focusable and visible stack, different from the target stack.
2428 *
2429 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2430 * searching for next candidate.
2431 * @return Next focusable {@link ActivityStack}, null if not found.
2432 */
2433 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2434 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2435
2436 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2437 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002438 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002439 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2440 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2441 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002442 if (stack != currentFocus && stack.isFocusable()
2443 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002444 return stack;
2445 }
2446 }
2447 }
2448
2449 return null;
2450 }
2451
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002452 /**
2453 * Get next valid stack for launching provided activity in the system. This will search across
2454 * displays and stacks in last-focused order for a focusable and visible stack, except those
2455 * that are on a currently focused display.
2456 *
2457 * @param r The activity that is being launched.
2458 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2459 * searching for the next candidate.
2460 * @return Next valid {@link ActivityStack}, null if not found.
2461 */
2462 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2463 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2464 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2465 final int displayId = mTmpOrderedDisplayIds.get(i);
2466 if (displayId == currentFocus) {
2467 continue;
2468 }
2469 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2470 if (stack != null) {
2471 return stack;
2472 }
2473 }
2474 return null;
2475 }
2476
Craig Mautneree2e45a2014-06-27 12:10:03 -07002477 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002478 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002479 }
2480
2481 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002482 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2483 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2484 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002485 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 final ArrayList<ActivityRecord> activities = task.mActivities;
2487 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2488 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002489 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002490 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002491 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002492 }
2493 }
2494 }
2495 }
2496 return null;
2497 }
2498
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002499 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2500 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2501 boolean deferResume) {
2502
2503 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002504 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002505 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002506 return;
2507 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002508
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002509 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002510 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002511 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002512 // If the docked stack exists, don't resize non-floating stacks independently of the
2513 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002514 return;
2515 }
2516
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002517 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002518 mWindowManager.deferSurfaceLayout();
2519 try {
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002520 if (stack.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002521 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2522 // null bounds = fullscreen windowing mode...at least for now.
2523 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2524 } else if (splitScreenActive) {
2525 // If we are in split-screen mode and this stack support split-screen, then
2526 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2527 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2528 }
2529 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002530 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002531 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002532 stack.ensureVisibleActivitiesConfigurationLocked(
2533 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002534 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002535 } finally {
2536 mWindowManager.continueSurfaceLayout();
2537 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002538 }
2539 }
2540
Bryce Leed3624e12017-11-30 08:51:45 -08002541 void deferUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002542 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002543 if (stack != null) {
2544 stack.deferUpdateBounds();
2545 }
2546 }
2547
Bryce Leed3624e12017-11-30 08:51:45 -08002548 void continueUpdateBounds(int activityType) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002549 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002550 if (stack != null) {
2551 stack.continueUpdateBounds();
2552 }
2553 }
2554
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002555 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002556 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002557 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2558 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002559 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002560 if (task != null) {
2561 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002562 }
2563 }
2564 mResizingTasksDuringAnimation.clear();
2565 }
2566
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002567 /**
2568 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002569 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002570 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002571 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002572 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002573
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002574 mWindowManager.deferSurfaceLayout();
2575 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002576 final int windowingMode = fromStack.getWindowingMode();
2577 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2578 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2579
2580 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002581 // Tell the display we are exiting split-screen mode.
2582 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002583 // We are moving all tasks from the docked stack to the fullscreen stack,
2584 // which is dismissing the docked stack, so resize all other stacks to
2585 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002586 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2587 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002588 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2589 continue;
2590 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002591 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002592 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002593 }
2594
2595 // Also disable docked stack resizing since we have manually adjusted the
2596 // size of other stacks above and we don't want to trigger a docked stack
2597 // resize when we remove task from it below and it is detached from the
2598 // display because it no longer contains any tasks.
2599 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002600 } else if (inPinnedWindowingMode && onTop) {
2601 // Log if we are expanding the PiP to fullscreen
Chenjie Yu52cacc62017-12-08 18:11:45 -08002602 MetricsLoggerWrapper.logPictureInPictureFullScreen(mService.mContext);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002603 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002604
Winson Chung5af42fc2017-03-24 17:11:33 -07002605 // If we are moving from the pinned stack, then the animation takes care of updating
2606 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002607 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002608 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002609
Wale Ogunwale388945c2017-10-04 12:13:46 -07002610 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002611 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002612 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002613 for (int i = 0; i < size; ++i) {
2614 final TaskRecord task = tasks.get(i);
2615 final ActivityStack toStack = toDisplay.getOrCreateStack(
2616 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002617
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002618 if (onTop) {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002619 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002620 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002621 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002622 isTopTask /* animate */, DEFER_RESUME,
2623 schedulePictureInPictureModeChange,
2624 "moveTasksToFullscreenStack - onTop");
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002625 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002626 // Position the tasks in the fullscreen stack in order at the bottom of the
2627 // stack. Also defer resume until all the tasks have been moved to the
2628 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002629 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002630 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2631 schedulePictureInPictureModeChange,
2632 "moveTasksToFullscreenStack - NOT_onTop");
2633 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002634 }
2635 }
Winson Chung74666102017-02-22 17:49:24 -08002636
2637 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2638 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002639 } finally {
2640 mAllowDockedStackResize = true;
2641 mWindowManager.continueSurfaceLayout();
2642 }
2643 }
2644
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002645 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002646 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2647 }
2648
2649 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2650 mWindowManager.inSurfaceTransaction(() ->
2651 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002652 }
2653
Jorim Jaggidc249c42015-12-15 14:57:31 -08002654 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002655 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2656 boolean preserveWindows) {
2657 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2658 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2659 false /* deferResume */);
2660 }
2661
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002662 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002663 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2664 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002665
2666 if (!mAllowDockedStackResize) {
2667 // Docked stack resize currently disabled.
2668 return;
2669 }
2670
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002671 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002672 if (stack == null) {
2673 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2674 return;
2675 }
2676
2677 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2678 mWindowManager.deferSurfaceLayout();
2679 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002680 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2681 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002682 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002683 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002684
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002685 // TODO: Checking for isAttached might not be needed as if the user passes in null
2686 // dockedBounds then they want the docked stack to be dismissed.
Bryce Leef3c6a472017-11-14 14:53:06 -08002687 if (stack.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
2688 || (dockedBounds == null && !stack.isAttached())) {
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002689 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002690 // In this case we make all other static stacks fullscreen and move all
2691 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002692 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002693
2694 // stack shouldn't contain anymore activities, so nothing to resume.
2695 r = null;
2696 } else {
2697 // Docked stacks occupy a dedicated region on screen so the size of all other
2698 // static stacks need to be adjusted so they don't overlap with the docked stack.
2699 // We get the bounds to use from window manager which has been adjusted for any
2700 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002701 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002702 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002703 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2704 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002705 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002706 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002707 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002708 if (!current.affectedBySplitScreenResize()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002709 continue;
2710 }
2711 // Need to set windowing mode here before we try to get the dock bounds.
2712 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2713 current.getStackDockedModeBounds(
2714 tempOtherTaskBounds /* currentTempTaskBounds */,
2715 tempRect /* outStackBounds */,
2716 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2717
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002718 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002719 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2720 tempOtherTaskInsetBounds, preserveWindows,
2721 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002722 }
2723 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002724 if (!deferResume) {
2725 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2726 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002727 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002728 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002729 mWindowManager.continueSurfaceLayout();
2730 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2731 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002732 }
2733
Robert Carr0d00c2e2016-02-29 17:45:02 -08002734 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002735 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002736 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002737 if (stack == null) {
2738 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2739 return;
2740 }
Winson Chung19953ca2017-04-11 11:19:23 -07002741
2742 // It is possible for the bounds animation from the WM to call this but be delayed by
2743 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2744 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2745 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2746 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002747 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002748 return;
2749 }
2750
Robert Carr0d00c2e2016-02-29 17:45:02 -08002751 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2752 mWindowManager.deferSurfaceLayout();
2753 try {
2754 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002755 Rect insetBounds = null;
2756 if (tempPinnedTaskBounds != null) {
2757 // We always use 0,0 as the position for the inset rect because
2758 // if we are getting insets at all in the pinned stack it must mean
2759 // we are headed for fullscreen.
2760 insetBounds = tempRect;
2761 insetBounds.top = 0;
2762 insetBounds.left = 0;
2763 insetBounds.right = tempPinnedTaskBounds.width();
2764 insetBounds.bottom = tempPinnedTaskBounds.height();
2765 }
2766 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002767 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002768 } finally {
2769 mWindowManager.continueSurfaceLayout();
2770 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2771 }
2772 }
2773
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002774 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002775 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002776 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002777 /**
2778 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2779 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2780 * that the client receives onStop() before it is reparented. We do this by detaching
2781 * the stack from the display so that it will be considered invisible when
2782 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2783 * invisible as well and added to the stopping list. After which we process the
2784 * stopping list by handling the idle.
2785 */
2786 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2787 pinnedStack.mForceHidden = true;
2788 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2789 pinnedStack.mForceHidden = false;
2790 activityIdleInternalLocked(null, false /* fromTimeout */,
2791 true /* processPausingActivites */, null /* configuration */);
2792
2793 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002794 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002795 } else {
2796 for (int i = tasks.size() - 1; i >= 0; i--) {
2797 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07002798 REMOVE_FROM_RECENTS, "remove-stack");
Winson Chung010927a2016-12-15 16:12:35 -08002799 }
2800 }
2801 }
2802
2803 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002804 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002805 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2806 * instead moved back onto the fullscreen stack.
2807 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002808 void removeStack(ActivityStack stack) {
2809 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002810 }
2811
Wale Ogunwale68278562017-09-23 17:13:55 -07002812 /**
2813 * Removes stacks in the input windowing modes from the system if they are of activity type
2814 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2815 */
2816 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002817 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002818 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2819 }
2820 }
2821
2822 void removeStacksWithActivityTypes(int... activityTypes) {
2823 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2824 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002825 }
Robert Carr6914f082017-03-20 19:04:30 -07002826 }
2827
2828 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002829 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2830 */
Winson Chung0ec2a352017-10-26 11:38:30 -07002831 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2832 String reason) {
2833 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY,
2834 reason);
Winson Chung6954fc92017-03-24 16:22:12 -07002835 }
2836
2837 /**
Winson Chung010927a2016-12-15 16:12:35 -08002838 * Removes the task with the specified task id.
2839 *
2840 * @param taskId Identifier of the task to be removed.
2841 * @param killProcess Kill any process associated with the task if possible.
2842 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002843 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2844 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002845 * @return Returns true if the given task was found and removed.
2846 */
Winson Chung6954fc92017-03-24 16:22:12 -07002847 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
Winson Chung0ec2a352017-10-26 11:38:30 -07002848 boolean pauseImmediately, String reason) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002849 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002850 if (tr != null) {
Winson Chung0ec2a352017-10-26 11:38:30 -07002851 tr.removeTaskActivitiesLocked(pauseImmediately, reason);
Winson Chung010927a2016-12-15 16:12:35 -08002852 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
Charles Heff9b4dff2017-09-22 10:18:37 +01002853 mService.mLockTaskController.clearLockedTask(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002854 if (tr.isPersistable) {
2855 mService.notifyTaskPersisterLocked(null, true);
2856 }
2857 return true;
2858 }
2859 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2860 return false;
2861 }
2862
2863 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2864 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002865 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002866 }
2867 ComponentName component = tr.getBaseIntent().getComponent();
2868 if (component == null) {
2869 Slog.w(TAG, "No component for base intent of task: " + tr);
2870 return;
2871 }
2872
2873 // Find any running services associated with this app and stop if needed.
2874 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2875
2876 if (!killProcess) {
2877 return;
2878 }
2879
2880 // Determine if the process(es) for this task should be killed.
2881 final String pkg = component.getPackageName();
2882 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2883 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2884 for (int i = 0; i < pmap.size(); i++) {
2885
2886 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2887 for (int j = 0; j < uids.size(); j++) {
2888 ProcessRecord proc = uids.valueAt(j);
2889 if (proc.userId != tr.userId) {
2890 // Don't kill process for a different user.
2891 continue;
2892 }
2893 if (proc == mService.mHomeProcess) {
2894 // Don't kill the home process along with tasks from the same package.
2895 continue;
2896 }
2897 if (!proc.pkgList.containsKey(pkg)) {
2898 // Don't kill process that is not associated with this task.
2899 continue;
2900 }
2901
2902 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002903 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002904 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2905 // Don't kill process(es) that has an activity in a different task that is
2906 // also in recents.
2907 return;
2908 }
2909 }
2910
2911 if (proc.foregroundServices) {
2912 // Don't kill process(es) with foreground service.
2913 return;
2914 }
2915
2916 // Add process to kill list.
2917 procsToKill.add(proc);
2918 }
2919 }
2920
2921 // Kill the running processes.
2922 for (int i = 0; i < procsToKill.size(); i++) {
2923 ProcessRecord pr = procsToKill.get(i);
2924 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2925 && pr.curReceivers.isEmpty()) {
2926 pr.kill("remove task", true);
2927 } else {
2928 // We delay killing processes that are not in the background or running a receiver.
2929 pr.waitingToKill = "remove task";
2930 }
2931 }
2932 }
2933
Chong Zhang5dcb2752015-08-18 13:50:26 -07002934 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002935 * Called to restore the state of the task into the stack that it's supposed to go into.
2936 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002937 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002938 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002939 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002940 * @return true if the task has been restored successfully.
2941 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002942 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2943 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002944 final ActivityStack currentStack = task.getStack();
2945 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002946 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002947 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002948 // Nothing else to do since it is already restored in the right stack.
2949 return true;
2950 }
2951 // Remove current stack association, so we can re-associate the task with the
2952 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002953 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002954 }
2955
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002956 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002957 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002958 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002959 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2960 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002961 final ArrayList<ActivityRecord> activities = task.mActivities;
2962 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002963 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002964 }
2965 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002966 }
2967
Winson Chung1dbc8112017-09-28 18:05:31 -07002968 @Override
2969 public void onRecentTaskAdded(TaskRecord task) {
2970 task.touchActiveTime();
2971 }
2972
2973 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07002974 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung0ec2a352017-10-26 11:38:30 -07002975 if (wasTrimmed) {
2976 // Task was trimmed from the recent tasks list -- remove the active task record as well
2977 // since the user won't really be able to go back to it
2978 removeTaskByIdLocked(task.taskId, false /* killProcess */,
2979 false /* removeFromRecents */, !PAUSE_IMMEDIATELY, "recent-task-trimmed");
2980 }
Winson Chung1dbc8112017-09-28 18:05:31 -07002981 task.removedFromRecents();
2982 }
2983
Wale Ogunwale040b4702015-08-06 18:10:50 -07002984 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002985 * Move stack with all its existing content to specified display.
2986 * @param stackId Id of stack to move.
2987 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002988 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002989 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002990 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002991 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002992 if (activityDisplay == null) {
2993 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2994 + displayId);
2995 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002996 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002997 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002998 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2999 + stackId);
3000 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003001
3002 final ActivityDisplay currentDisplay = stack.getDisplay();
3003 if (currentDisplay == null) {
3004 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3005 + " is not attached to any display.");
3006 }
3007
3008 if (currentDisplay.mDisplayId == displayId) {
3009 throw new IllegalArgumentException("Trying to move stack=" + stack
3010 + " to its current displayId=" + displayId);
3011 }
3012
3013 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003014 // TODO(multi-display): resize stacks properly if moved from split-screen.
3015 }
3016
3017 /**
Winson Chung74666102017-02-22 17:49:24 -08003018 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3019 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003020 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003021 // TODO: Look into changing users to this method to ActivityDisplay.resolveWindowingMode()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003022 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003023 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003024 final int stackId = stack.mStackId;
3025 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003026
Winson Chung74666102017-02-22 17:49:24 -08003027 // Check that we aren't reparenting to the same stack that the task is already in
3028 if (prevStack != null && prevStack.mStackId == stackId) {
3029 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3030 + " already in stackId=" + stackId);
3031 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003032 }
3033
Winson Chung74666102017-02-22 17:49:24 -08003034 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3035 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003036 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003037 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003038 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003039 }
3040
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003041 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3042 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003043 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003044 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3045 + " reparent task=" + task + " to stackId=" + stackId);
3046 }
3047
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003048 // Ensure that we aren't trying to move into a freeform stack without freeform support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003049 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3050 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003051 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3052 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003053 }
3054
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003055 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
3056 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003057 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003058 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
3059 + " Moving to a fullscreen stack instead.");
3060 if (prevStack != null) {
3061 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003062 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07003063 stack = stack.getDisplay().createStack(
3064 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003065 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003066 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003067 }
3068
Winson Chung08f81892017-03-02 15:40:51 -08003069 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003070 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003071 if (stack == null) {
3072 throw new IllegalArgumentException(
3073 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3074 }
3075
3076 final ActivityRecord r = stack.topRunningActivityLocked();
3077 if (r == null) {
3078 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3079 + " in stack=" + stack);
3080 return false;
3081 }
3082
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003083 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003084 Slog.w(TAG,
3085 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003086 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003087 return false;
3088 }
3089
Winson Chung3a682872017-04-10 14:38:05 -07003090 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003091 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003092 return true;
3093 }
3094
Winson Chung8bca9e42017-04-16 15:59:43 -07003095 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003096 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003097
Wale Ogunwale480dca02016-02-06 13:58:29 -08003098 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003099
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003100 final ActivityDisplay display = r.getStack().getDisplay();
3101 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003102
Bryce Lee04ab3462017-04-10 15:06:33 -07003103 // This will clear the pinned stack by moving an existing task to the full screen stack,
3104 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003105 if (stack != null) {
3106 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3107 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003108
Wale Ogunwale1666e312016-12-16 11:27:18 -08003109 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003110 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003111
Winson Chunge55c0192017-08-24 14:50:48 -07003112 // Calculate the target bounds here before the task is reparented back into pinned windowing
3113 // mode (which will reset the saved bounds)
3114 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3115
Wale Ogunwale480dca02016-02-06 13:58:29 -08003116 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003117 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003118 // Resize the pinned stack to match the current size of the task the activity we are
3119 // going to be moving is currently contained in. We do this to have the right starting
3120 // animation bounds for the pinned stack to the desired bounds the caller wants.
Bryce Leef3c6a472017-11-14 14:53:06 -08003121 resizeStackLocked(stack, task.getOverrideBounds(), null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003122 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003123 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003124
3125 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003126 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003127 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3128 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003129 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003130 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003131 // reveal/leave the other activities in their original task.
3132
3133 // Currently, we don't support reparenting activities across tasks in two different
3134 // stacks, so instead, just create a new task in the same stack, reparent the
3135 // activity into that task, and then reparent the whole task to the new stack. This
3136 // ensures that all the necessary work to migrate states in the old and new stacks
3137 // is also done.
3138 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003139 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003140 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3141
Winson Chung5af42fc2017-03-24 17:11:33 -07003142 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003143 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003144 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003145 }
Winson Chungc2baac02017-01-11 13:34:47 -08003146
3147 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3148 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003149 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003150 } finally {
3151 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003152 }
3153
Winson Chunge7ba6862017-05-24 12:13:33 -07003154 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3155 true /* fromFullscreen */);
3156
3157 // Update the visibility of all activities after the they have been reparented to the new
3158 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3159 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3160 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003161 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003162 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003163
Wale Ogunwale89be5762017-10-04 13:27:49 -07003164 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003165 }
3166
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003167 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003168 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3169 if (r == null || !r.isFocusable()) {
3170 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3171 "moveActivityStackToFront: unfocusable r=" + r);
3172 return false;
3173 }
3174
Bryce Leeaf691c02017-03-20 14:20:22 -07003175 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003176 final ActivityStack stack = r.getStack();
3177 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003178 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3179 + r + " task=" + task);
3180 return false;
3181 }
3182
Chong Zhang6cda19c2016-06-14 19:07:56 -07003183 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3184 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3185 "moveActivityStackToFront: already on top, r=" + r);
3186 return false;
3187 }
3188
3189 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3190 "moveActivityStackToFront: r=" + r);
3191
3192 stack.moveToFront(reason, task);
3193 return true;
3194 }
3195
David Stevensc6b91c62017-02-08 14:23:58 -08003196 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003197 mTmpFindTaskResult.r = null;
3198 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003199 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003200 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003202 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3203 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3204 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003205 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003206 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3207 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003208 continue;
3209 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003210 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003211 // It is possible to have tasks in multiple stacks with the same root affinity, so
3212 // we should keep looking after finding an affinity match to see if there is a
3213 // better match in another stack. Also, task affinity isn't a good enough reason
3214 // to target a display which isn't the source of the intent, so skip any affinity
3215 // matches not on the specified display.
3216 if (mTmpFindTaskResult.r != null) {
3217 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3218 return mTmpFindTaskResult.r;
3219 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003220 // Note: since the traversing through the stacks is top down, the floating
3221 // tasks should always have lower priority than any affinity-matching tasks
3222 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003223 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003224 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3225 Slog.d(TAG_TASKS, "Skipping match on different display "
3226 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003227 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003229 }
3230 }
Winson Chung5b895b72017-05-01 13:46:25 -07003231
David Stevensc6b91c62017-02-08 14:23:58 -08003232 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3233 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003234 }
3235
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003236 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003237 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003238 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003239 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3240 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3241 final ActivityStack stack = display.getChildAt(stackNdx);
3242 final ActivityRecord ar = stack.findActivityLocked(
3243 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003244 if (ar != null) {
3245 return ar;
3246 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003247 }
3248 }
3249 return null;
3250 }
3251
David Stevens9440dc82017-03-16 19:00:20 -07003252 boolean hasAwakeDisplay() {
3253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3254 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3255 if (!display.shouldSleep()) {
3256 return true;
3257 }
3258 }
3259 return false;
3260 }
3261
Craig Mautner8d341ef2013-03-26 09:03:27 -07003262 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003263 scheduleSleepTimeout();
3264 if (!mGoingToSleep.isHeld()) {
3265 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003266 if (mLaunchingActivity.isHeld()) {
3267 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3268 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003269 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003270 mLaunchingActivity.release();
3271 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003272 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003273 }
David Stevens9440dc82017-03-16 19:00:20 -07003274
3275 applySleepTokensLocked(false /* applyToStacks */);
3276
3277 checkReadyForSleepLocked(true /* allowDelay */);
3278 }
3279
3280 void prepareForShutdownLocked() {
3281 for (int i = 0; i < mActivityDisplays.size(); i++) {
3282 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3283 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003284 }
3285
3286 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003287 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003288
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003289 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003290 final long endTime = System.currentTimeMillis() + timeout;
3291 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003292 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003293 long timeRemaining = endTime - System.currentTimeMillis();
3294 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003295 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003296 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003297 } catch (InterruptedException e) {
3298 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003299 } else {
3300 Slog.w(TAG, "Activity manager shutdown timed out");
3301 timedout = true;
3302 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003303 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003304 } else {
3305 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003306 }
3307 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003308
3309 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003310 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003311
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 return timedout;
3313 }
3314
3315 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003316 removeSleepTimeouts();
3317 if (mGoingToSleep.isHeld()) {
3318 mGoingToSleep.release();
3319 }
David Stevens9440dc82017-03-16 19:00:20 -07003320 }
3321
3322 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003323 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003324 // Set the sleeping state of the display.
3325 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3326 final boolean displayShouldSleep = display.shouldSleep();
3327 if (displayShouldSleep == display.isSleeping()) {
3328 continue;
3329 }
3330 display.setIsSleeping(displayShouldSleep);
3331
3332 if (!applyToStacks) {
3333 continue;
3334 }
3335
3336 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003337 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3338 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003339 if (displayShouldSleep) {
3340 stack.goToSleepIfPossible(false /* shuttingDown */);
3341 } else {
3342 stack.awakeFromSleepingLocked();
3343 if (isFocusedStack(stack)) {
3344 resumeFocusedStackTopActivityLocked();
3345 }
3346 }
3347 }
3348
3349 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3350 continue;
3351 }
3352 // The display is awake now, so clean up the going to sleep list.
3353 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3354 final ActivityRecord r = it.next();
3355 if (r.getDisplayId() == display.mDisplayId) {
3356 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003357 }
Craig Mautner5314a402013-09-26 12:40:16 -07003358 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003359 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003360 }
3361
3362 void activitySleptLocked(ActivityRecord r) {
3363 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003364 final ActivityStack s = r.getStack();
3365 if (s != null) {
3366 s.checkReadyForSleep();
3367 } else {
3368 checkReadyForSleepLocked(true);
3369 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003370 }
3371
David Stevens9440dc82017-03-16 19:00:20 -07003372 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003373 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003374 // Do not care.
3375 return;
3376 }
3377
David Stevens18abd0e2017-08-17 14:55:47 -07003378 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3379 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003380 }
3381
Wei Wang65c7a152016-06-02 18:51:22 -07003382 // Send launch end powerhint before going sleep
Bryce Leed3624e12017-11-30 08:51:45 -08003383 sendPowerHintForLaunchEndIfNeeded();
Wei Wang65c7a152016-06-02 18:51:22 -07003384
Craig Mautner0eea92c2013-05-16 13:35:39 -07003385 removeSleepTimeouts();
3386
3387 if (mGoingToSleep.isHeld()) {
3388 mGoingToSleep.release();
3389 }
3390 if (mService.mShuttingDown) {
3391 mService.notifyAll();
3392 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003393 }
3394
David Stevens18abd0e2017-08-17 14:55:47 -07003395 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3396 // successfully put to sleep.
3397 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3398 boolean allSleep = true;
3399 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003400 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3401 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3402 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003403 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003404 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003405 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003406 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003407 }
3408 }
3409 }
3410 return allSleep;
3411 }
3412
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003413 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003414 // A resumed activity cannot be stopping. remove from list
3415 mStoppingActivities.remove(r);
3416
Andrii Kulian02b7a832016-10-06 23:11:56 -07003417 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003418 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003419 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003420 }
3421 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003422 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003423 mWindowManager.executeAppTransition();
3424 return true;
3425 }
3426 return false;
3427 }
3428
Craig Mautner8d341ef2013-03-26 09:03:27 -07003429 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003430 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003431 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3432 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3433 final ActivityStack stack = display.getChildAt(stackNdx);
3434 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003435 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 }
3437 }
3438
Craig Mautnerbb742462014-07-07 15:28:55 -07003439 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003440 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003441 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003442 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003443
3444 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003445 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003446 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003447 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003448
3449 // When launching tasks behind, update the last active time of the top task after the new
3450 // task has been shown briefly
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003451 final ActivityRecord top = stack.getTopActivity();
Winson730bf062016-03-31 18:04:56 -07003452 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003453 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003454 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003455 }
3456
3457 void scheduleLaunchTaskBehindComplete(IBinder token) {
3458 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3459 }
3460
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003461 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3462 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003463 mKeyguardController.beginActivityVisibilityUpdate();
3464 try {
3465 // First the front stacks. In case any are not fullscreen and are in front of home.
3466 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003467 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3468 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3469 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003470 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3471 }
Craig Mautner580ea812013-04-25 12:58:38 -07003472 }
Jorim Jaggife762342016-10-13 14:33:27 +02003473 } finally {
3474 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003475 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003476 }
3477
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003478 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3479 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003480 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3481 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3482 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003483 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3484 }
3485 }
3486 }
3487
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003488 void invalidateTaskLayers() {
3489 mTaskLayersChanged = true;
3490 }
3491
3492 void rankTaskLayersIfNeeded() {
3493 if (!mTaskLayersChanged) {
3494 return;
3495 }
3496 mTaskLayersChanged = false;
3497 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003498 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003499 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003500 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3501 final ActivityStack stack = display.getChildAt(stackNdx);
3502 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003503 }
3504 }
3505 }
3506
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003507 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3508 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003509 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3510 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3511 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003512 stack.clearOtherAppTimeTrackers(except);
3513 }
3514 }
3515 }
3516
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003519 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3520 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3521 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003522 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003523 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003524 }
3525 }
3526
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003527 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3528 // Examine all activities currently running in the process.
3529 TaskRecord firstTask = null;
3530 // Tasks is non-null only if two or more tasks are found.
3531 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003532 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3533 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003534 ActivityRecord r = app.activities.get(i);
3535 // First, if we find an activity that is in the process of being destroyed,
3536 // then we just aren't going to do anything for now; we want things to settle
3537 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003538 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003539 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003540 return;
3541 }
3542 // Don't consider any activies that are currently not in a state where they
3543 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003544 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3545 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003546 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003547 continue;
3548 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003549
3550 final TaskRecord task = r.getTask();
3551 if (task != null) {
3552 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003553 + " from " + r);
3554 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003555 firstTask = task;
3556 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003557 if (tasks == null) {
3558 tasks = new ArraySet<>();
3559 tasks.add(firstTask);
3560 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003561 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003562 }
3563 }
3564 }
3565 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003566 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003567 return;
3568 }
3569 // If we have activities in multiple tasks that are in a position to be destroyed,
3570 // let's iterate through the tasks and release the oldest one.
3571 final int numDisplays = mActivityDisplays.size();
3572 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003573 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3574 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003575 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003576 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3577 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003578 // Try to release activities in this stack; if we manage to, we are done.
3579 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3580 return;
3581 }
3582 }
3583 }
3584 }
3585
Amith Yamasani37a40c22015-06-17 13:25:42 -07003586 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003587 final int focusStackId = mFocusedStack.getStackId();
3588 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003589 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003590 if (dockedStack != null) {
3591 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3592 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003593 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3594 // also cause all tasks to be moved to the fullscreen stack at a position that is
3595 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003596 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003597
3598 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003599 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003600 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003601
Craig Mautner858d8a62013-04-23 17:08:34 -07003602 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003603 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003604 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3605 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3606 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003607 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003608 TaskRecord task = stack.topTask();
3609 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003610 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003611 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003612 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003613 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003614
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003615 ActivityStack stack = getStack(restoreStackId);
3616 if (stack == null) {
3617 stack = mHomeStack;
3618 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003619 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003620 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003621 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003622 } else {
3623 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003624 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003625 }
Craig Mautner93529a42013-10-04 15:03:13 -07003626 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003627 }
3628
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003629 /** Checks whether the userid is a profile of the current user. */
3630 boolean isCurrentProfileLocked(int userId) {
3631 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003632 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003633 }
3634
Bryce Leeb7c9b802017-05-02 14:20:24 -07003635 /**
3636 * Returns whether a stopping activity is present that should be stopped after visible, rather
3637 * than idle.
3638 * @return {@code true} if such activity is present. {@code false} otherwise.
3639 */
3640 boolean isStoppingNoHistoryActivity() {
3641 // Activities that are marked as nohistory should be stopped immediately after the resumed
3642 // activity has become visible.
3643 for (ActivityRecord record : mStoppingActivities) {
3644 if (record.isNoHistory()) {
3645 return true;
3646 }
3647 }
3648
3649 return false;
3650 }
3651
Winson Chung4dabf232017-01-25 13:25:22 -08003652 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3653 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003654 ArrayList<ActivityRecord> stops = null;
3655
3656 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003657 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3658 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003659 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003660 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003661 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3662 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003663 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003664 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003665 if (s.finishing) {
3666 // If this activity is finishing, it is sitting on top of
3667 // everyone else but we now know it is no longer needed...
3668 // so get rid of it. Otherwise, we need to go through the
3669 // normal flow and hide it once we determine that it is
3670 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003671 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003672 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003673 }
3674 }
David Stevens9440dc82017-03-16 19:00:20 -07003675 if (remove) {
3676 final ActivityStack stack = s.getStack();
3677 final boolean shouldSleepOrShutDown = stack != null
3678 ? stack.shouldSleepOrShutDownActivities()
3679 : mService.isSleepingOrShuttingDownLocked();
3680 if (!waitingVisible || shouldSleepOrShutDown) {
3681 if (!processPausingActivities && s.state == PAUSING) {
3682 // Defer processing pausing activities in this iteration and reschedule
3683 // a delayed idle to reprocess it again
3684 removeTimeoutsForActivityLocked(idleActivity);
3685 scheduleIdleTimeoutLocked(idleActivity);
3686 continue;
3687 }
Winson Chung4dabf232017-01-25 13:25:22 -08003688
David Stevens9440dc82017-03-16 19:00:20 -07003689 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3690 if (stops == null) {
3691 stops = new ArrayList<>();
3692 }
3693 stops.add(s);
3694 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003695 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003696 }
3697 }
3698
3699 return stops;
3700 }
3701
Craig Mautnercf910b02013-04-23 11:23:27 -07003702 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003704 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3705 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3706 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003707 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003708 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003709 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003710 if (r == null) Slog.e(TAG,
3711 "validateTop...: null top activity, stack=" + stack);
3712 else {
3713 final ActivityRecord pausing = stack.mPausingActivity;
3714 if (pausing != null && pausing == r) Slog.e(TAG,
3715 "validateTop...: top stack has pausing activity r=" + r
3716 + " state=" + state);
3717 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3718 "validateTop...: activity in front not resumed r=" + r
3719 + " state=" + state);
3720 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003721 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003722 final ActivityRecord resumed = stack.mResumedActivity;
3723 if (resumed != null && resumed == r) Slog.e(TAG,
3724 "validateTop...: back stack has resumed activity r=" + r
3725 + " state=" + state);
3726 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3727 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003728 }
3729 }
3730 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003731 }
3732
Bryce Lee77a7dd62018-01-22 15:47:09 -08003733 public void dumpDisplays(PrintWriter pw) {
3734 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3735 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3736 pw.print("[id:" + display.mDisplayId + " stacks:");
3737 display.dumpStacks(pw);
3738 pw.print("]");
3739 }
3740 }
3741
Craig Mautner27084302013-03-25 08:05:25 -07003742 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003743 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003744 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003745 pw.print(prefix);
3746 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003747 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003748 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3749 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003750 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003751 }
Bryce Lee4a194382017-04-04 14:32:48 -07003752 if (!mWaitingForActivityVisible.isEmpty()) {
3753 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3754 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3755 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3756 }
3757 }
3758
Jorim Jaggi8d786932016-10-26 19:08:36 -07003759 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003760 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003761 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003762
Yi Jin129fc6c2017-09-28 15:48:38 -07003763 public void writeToProto(ProtoOutputStream proto) {
Adrian Roos4921ccf2017-09-28 16:54:06 +02003764 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003765 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3766 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3767 activityDisplay.writeToProto(proto, DISPLAYS);
3768 }
3769 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3770 if (mFocusedStack != null) {
3771 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3772 ActivityRecord focusedActivity = getResumedActivityLocked();
3773 if (focusedActivity != null) {
3774 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3775 }
3776 } else {
3777 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3778 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003779 }
3780
Winson43d1f262016-06-14 16:05:55 -07003781 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003782 * Dump all connected displays' configurations.
3783 * @param prefix Prefix to apply to each line of the dump.
3784 */
3785 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3786 pw.print(prefix); pw.println("Display override configurations:");
3787 final int displayCount = mActivityDisplays.size();
3788 for (int i = 0; i < displayCount; i++) {
3789 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3790 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3791 pw.println(activityDisplay.getOverrideConfiguration());
3792 }
3793 }
3794
3795 /**
Winson43d1f262016-06-14 16:05:55 -07003796 * Dumps the activities matching the given {@param name} in the either the focused stack
3797 * or all visible stacks if {@param dumpVisibleStacks} is true.
3798 */
Winson Chung6998bc42017-02-28 17:07:05 -08003799 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3800 boolean dumpFocusedStackOnly) {
3801 if (dumpFocusedStackOnly) {
3802 return mFocusedStack.getDumpActivitiesLocked(name);
3803 } else {
Winson43d1f262016-06-14 16:05:55 -07003804 ArrayList<ActivityRecord> activities = new ArrayList<>();
3805 int numDisplays = mActivityDisplays.size();
3806 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003807 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3808 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3809 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003810 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003811 activities.addAll(stack.getDumpActivitiesLocked(name));
3812 }
3813 }
3814 }
3815 return activities;
Winson43d1f262016-06-14 16:05:55 -07003816 }
Craig Mautner20e72272013-04-01 13:45:53 -07003817 }
3818
Dianne Hackborn390517b2013-05-30 15:03:32 -07003819 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3820 boolean needSep, String prefix) {
3821 if (activity != null) {
3822 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3823 if (needSep) {
3824 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003825 }
3826 pw.print(prefix);
3827 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003828 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003829 }
3830 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003831 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003832 }
3833
Craig Mautner8d341ef2013-03-26 09:03:27 -07003834 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3835 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003836 boolean printed = false;
3837 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003838 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3839 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003840 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003841 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003842 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3843 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3844 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003845 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003846 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003847 + ": type=" + activityTypeToString(stack.getActivityType())
3848 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003849 pw.println(" isSleeping=" + stack.shouldSleepActivities());
Bryce Leef3c6a472017-11-14 14:53:06 -08003850 pw.println(" mBounds=" + stack.getOverrideBounds());
Winson Chungabb433b2017-03-24 09:35:42 -07003851
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003852 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3853 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003854
Craig Mautner4a1cb222013-12-04 16:14:06 -08003855 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3856 !dumpAll, false, dumpPackage, true,
3857 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003858
Craig Mautner4a1cb222013-12-04 16:14:06 -08003859 needSep = printed;
3860 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3861 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003862 if (pr) {
3863 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003864 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003865 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3867 " mResumedActivity: ");
3868 if (pr) {
3869 printed = true;
3870 needSep = false;
3871 }
3872 if (dumpAll) {
3873 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3874 " mLastPausedActivity: ");
3875 if (pr) {
3876 printed = true;
3877 needSep = true;
3878 }
3879 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3880 needSep, " mLastNoHistoryActivity: ");
3881 }
3882 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003883 }
3884 }
3885
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003886 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3887 false, dumpPackage, true, " Activities waiting to finish:", null);
3888 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3889 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003890 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3891 false, !dumpAll, false, dumpPackage, true,
3892 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003893 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3894 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003895
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003896 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003897 }
3898
Dianne Hackborn390517b2013-05-30 15:03:32 -07003899 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003900 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003901 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003902 String innerPrefix = null;
3903 String[] args = null;
3904 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003905 for (int i=list.size()-1; i>=0; i--) {
3906 final ActivityRecord r = list.get(i);
3907 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3908 continue;
3909 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003910 if (innerPrefix == null) {
3911 innerPrefix = prefix + " ";
3912 args = new String[0];
3913 }
3914 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003915 final boolean full = !brief && (complete || !r.isInHistory());
3916 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003917 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003918 needNL = false;
3919 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003920 if (header != null) {
3921 pw.println(header);
3922 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003923 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003924 if (lastTask != r.getTask()) {
3925 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003926 pw.print(prefix);
3927 pw.print(full ? "* " : " ");
3928 pw.println(lastTask);
3929 if (full) {
3930 lastTask.dump(pw, prefix + " ");
3931 } else if (complete) {
3932 // Complete + brief == give a summary. Isn't that obvious?!?
3933 if (lastTask.intent != null) {
3934 pw.print(prefix); pw.print(" ");
3935 pw.println(lastTask.intent.toInsecureStringWithClip());
3936 }
3937 }
3938 }
3939 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3940 pw.print(" #"); pw.print(i); pw.print(": ");
3941 pw.println(r);
3942 if (full) {
3943 r.dump(pw, innerPrefix);
3944 } else if (complete) {
3945 // Complete + brief == give a summary. Isn't that obvious?!?
3946 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3947 if (r.app != null) {
3948 pw.print(innerPrefix); pw.println(r.app);
3949 }
3950 }
3951 if (client && r.app != null && r.app.thread != null) {
3952 // flush anything that is already in the PrintWriter since the thread is going
3953 // to write to the file descriptor directly
3954 pw.flush();
3955 try {
3956 TransferPipe tp = new TransferPipe();
3957 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003958 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003959 // Short timeout, since blocking here can
3960 // deadlock with the application.
3961 tp.go(fd, 2000);
3962 } finally {
3963 tp.kill();
3964 }
3965 } catch (IOException e) {
3966 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3967 } catch (RemoteException e) {
3968 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3969 }
3970 needNL = true;
3971 }
3972 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003973 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003974 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003975
Craig Mautnerf3333272013-04-22 10:55:53 -07003976 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003977 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3978 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003979 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3980 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003981 }
3982
3983 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003984 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003985 }
3986
3987 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003988 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3989 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003990 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3991 }
3992
Craig Mautner05d29032013-05-03 13:40:13 -07003993 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003994 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3995 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3996 }
Craig Mautner05d29032013-05-03 13:40:13 -07003997 }
3998
Craig Mautner0eea92c2013-05-16 13:35:39 -07003999 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004000 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4001 }
4002
4003 final void scheduleSleepTimeout() {
4004 removeSleepTimeouts();
4005 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4006 }
4007
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 @Override
4009 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004010 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004011 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4012 }
4013
4014 @Override
4015 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004016 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004017 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4018 }
4019
4020 @Override
4021 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004022 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004023 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4024 }
4025
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004026 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004027 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004028 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
4031
Andrii Kulianca007a62016-11-22 16:33:28 -08004032 /** Check if display with specified id is added to the list. */
4033 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004034 return getActivityDisplayOrCreateLocked(displayId) != null;
4035 }
4036
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004037 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004038 ActivityDisplay getActivityDisplay(int displayId) {
4039 return mActivityDisplays.get(displayId);
4040 }
4041
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004042 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4043 ActivityDisplay getDefaultDisplay() {
4044 return mActivityDisplays.get(DEFAULT_DISPLAY);
4045 }
4046
Andrii Kulian62e6f252017-05-30 22:46:53 -07004047 /**
4048 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4049 * corresponding record in display manager.
4050 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004051 // TODO: Look into consolidating with getActivityDisplay()
4052 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004053 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4054 if (activityDisplay != null) {
4055 return activityDisplay;
4056 }
4057 if (mDisplayManager == null) {
4058 // The system isn't fully initialized yet.
4059 return null;
4060 }
4061 final Display display = mDisplayManager.getDisplay(displayId);
4062 if (display == null) {
4063 // The display is not registered in DisplayManager.
4064 return null;
4065 }
4066 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004067 activityDisplay = new ActivityDisplay(this, displayId);
4068 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004069 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4070 mWindowManager.onDisplayAdded(displayId);
4071 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004072 }
4073
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004074 @VisibleForTesting
4075 void attachDisplay(ActivityDisplay display) {
4076 mActivityDisplays.put(display.mDisplayId, display);
4077 }
4078
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004079 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004080 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004081 mService.mContext.getResources().getDimensionPixelSize(
4082 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004083 }
4084
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004085 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004086 if (displayId == DEFAULT_DISPLAY) {
4087 throw new IllegalArgumentException("Can't remove the primary display.");
4088 }
4089
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004091 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4092 if (activityDisplay == null) {
4093 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004095 final boolean destroyContentOnRemoval
4096 = activityDisplay.shouldDestroyContentOnRemove();
4097 while (activityDisplay.getChildCount() > 0) {
4098 final ActivityStack stack = activityDisplay.getChildAt(0);
4099 if (destroyContentOnRemoval) {
4100 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4101 stack.finishAllActivitiesLocked(true /* immediately */);
4102 } else {
4103 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4104 // a valid launch stack for all activities. This way the task history from
4105 // external display will be preserved on primary after move.
4106 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4107 }
4108 }
4109
4110 releaseSleepTokens(activityDisplay);
4111
4112 mActivityDisplays.remove(displayId);
4113 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004114 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004115 }
4116
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004117 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004118 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004119 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08004120 // TODO: The following code block should be moved into {@link ActivityDisplay}.
Craig Mautnere0a38842013-12-16 16:14:02 -08004121 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004122 // The window policy is responsible for stopping activities on the default display
4123 if (displayId != Display.DEFAULT_DISPLAY) {
4124 int displayState = activityDisplay.mDisplay.getState();
4125 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4126 activityDisplay.mOffToken =
4127 mService.acquireSleepToken("Display-off", displayId);
4128 } else if (displayState == Display.STATE_ON
4129 && activityDisplay.mOffToken != null) {
4130 activityDisplay.mOffToken.release();
4131 activityDisplay.mOffToken = null;
4132 }
4133 }
Bryce Leef3c6a472017-11-14 14:53:06 -08004134
4135 activityDisplay.updateBounds();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004137 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139 }
4140
David Stevens9440dc82017-03-16 19:00:20 -07004141 SleepToken createSleepTokenLocked(String tag, int displayId) {
4142 ActivityDisplay display = mActivityDisplays.get(displayId);
4143 if (display == null) {
4144 throw new IllegalArgumentException("Invalid display: " + displayId);
4145 }
4146
4147 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4148 mSleepTokens.add(token);
4149 display.mAllSleepTokens.add(token);
4150 return token;
4151 }
4152
4153 private void removeSleepTokenLocked(SleepTokenImpl token) {
4154 mSleepTokens.remove(token);
4155
4156 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4157 if (display != null) {
4158 display.mAllSleepTokens.remove(token);
4159 if (display.mAllSleepTokens.isEmpty()) {
4160 mService.updateSleepIfNeededLocked();
4161 }
4162 }
4163 }
4164
4165 private void releaseSleepTokens(ActivityDisplay display) {
4166 if (display.mAllSleepTokens.isEmpty()) {
4167 return;
4168 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004169 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004170 mSleepTokens.remove(token);
4171 }
4172 display.mAllSleepTokens.clear();
4173
4174 mService.updateSleepIfNeededLocked();
4175 }
4176
Wale Ogunwale68278562017-09-23 17:13:55 -07004177 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004178 final int displayId = stack.mDisplayId;
4179 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004181 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004182 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004183 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004184 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004185 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004186 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004187 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004188 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004189
4190 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4191 final int numTasks = tasks.size();
4192 int[] taskIds = new int[numTasks];
4193 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004194 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004195 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004196 for (int i = 0; i < numTasks; ++i) {
4197 final TaskRecord task = tasks.get(i);
4198 taskIds[i] = task.taskId;
4199 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4200 : task.realActivity != null ? task.realActivity.flattenToString()
4201 : task.getTopActivity() != null ? task.getTopActivity().packageName
4202 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004203 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004204 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004205 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 }
4207 info.taskIds = taskIds;
4208 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004209 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004210 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004211
4212 final ActivityRecord top = stack.topRunningActivityLocked();
4213 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 return info;
4215 }
4216
Wale Ogunwale68278562017-09-23 17:13:55 -07004217 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004218 ActivityStack stack = getStack(stackId);
4219 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004220 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004221 }
4222 return null;
4223 }
4224
Wale Ogunwale68278562017-09-23 17:13:55 -07004225 StackInfo getStackInfo(int windowingMode, int activityType) {
4226 final ActivityStack stack = getStack(windowingMode, activityType);
4227 return (stack != null) ? getStackInfo(stack) : null;
4228 }
4229
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004231 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004232 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004233 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4234 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4235 final ActivityStack stack = display.getChildAt(stackNdx);
4236 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 }
4238 }
4239 return list;
4240 }
4241
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004242 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004243 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004244 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004245 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004246 }
4247
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004248 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004249 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004250 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004251 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004252 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004253 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004254 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004255 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004256 return;
4257 }
4258
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004259 // Handle incorrect launch/move to secondary display if needed.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004260 if (isSecondaryDisplayPreferred) {
4261 final int actualDisplayId = task.getStack().mDisplayId;
4262 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
Andrii Kulianb850ea52017-12-12 23:49:10 -08004263 throw new IllegalStateException("Task resolved to incompatible display");
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004264 }
Andrii Kulianb850ea52017-12-12 23:49:10 -08004265 // The task might have landed on a display different from requested.
4266 // TODO(multi-display): Find proper stack for the task on the default display.
4267 mService.setTaskWindowingMode(task.taskId,
4268 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
4269 if (preferredDisplayId != actualDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004270 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4271 // display with config different from global config.
4272 mService.mTaskChangeNotificationController
4273 .notifyActivityLaunchOnSecondaryDisplayFailed();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004274 return;
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004275 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004276 }
4277
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004278 if (!task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004279 // Display a warning toast that we tried to put an app that doesn't support split-screen
4280 // in split-screen.
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004281 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
Jorim Jaggid53f0922016-04-06 22:16:23 -07004282
Andrii Kulianc27916642016-04-12 17:59:27 -07004283 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4284 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004285
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004286 final ActivityStack dockedStack =
4287 task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004288 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004289 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004290 }
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004291 return;
4292 }
4293
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004294 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale2b07da82017-11-08 14:52:40 -08004295 if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
4296 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004297 final String packageName = topActivity.appInfo.packageName;
4298 final int reason = isSecondaryDisplayPreferred
4299 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4300 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004301 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004302 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004303 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004304 }
4305
Jorim Jaggife89d122015-12-22 16:28:44 +01004306 void activityRelaunchedLocked(IBinder token) {
4307 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004308 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4309 if (r != null) {
4310 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004311 r.setSleeping(true, true);
4312 }
4313 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004314 }
4315
4316 void activityRelaunchingLocked(ActivityRecord r) {
4317 mWindowManager.notifyAppRelaunching(r.appToken);
4318 }
4319
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004320 void logStackState() {
4321 mActivityMetricsLogger.logWindowState();
4322 }
4323
Winson Chung5af42fc2017-03-24 17:11:33 -07004324 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004325 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4326 // end, then ensure that we defer all in between multi-window mode changes
4327 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4328 return;
4329 }
4330
Wale Ogunwale22e25262016-02-01 10:32:02 -08004331 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4332 final ActivityRecord r = task.mActivities.get(i);
4333 if (r.app != null && r.app.thread != null) {
4334 mMultiWindowModeChangedActivities.add(r);
4335 }
4336 }
4337
4338 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4339 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4340 }
4341 }
4342
Winson Chung5af42fc2017-03-24 17:11:33 -07004343 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004344 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004345 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004346 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004347 return;
4348 }
4349
Bryce Leef3c6a472017-11-14 14:53:06 -08004350 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.getOverrideBounds());
Winson Chung5af42fc2017-03-24 17:11:33 -07004351 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004352
Winson Chungab76bbc2017-08-14 13:33:51 -07004353 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4354 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4355 final ActivityRecord r = task.mActivities.get(i);
4356 if (r.app != null && r.app.thread != null) {
4357 mPipModeChangedActivities.add(r);
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08004358 // If we are scheduling pip change, then remove this activity from multi-window
4359 // change list as the processing of pip change will make sure multi-window changed
4360 // message is processed in the right order relative to pip changed.
4361 mMultiWindowModeChangedActivities.remove(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004362 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004363 }
4364 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004365
Winson Chungab76bbc2017-08-14 13:33:51 -07004366 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4367 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4368 }
4369 }
4370
4371 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4372 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4373 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4374 final ActivityRecord r = task.mActivities.get(i);
4375 if (r.app != null && r.app.thread != null) {
4376 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004377 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004378 }
4379 }
4380
Tony Mak853304c2016-04-18 15:17:41 +01004381 void setDockedStackMinimized(boolean minimized) {
4382 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004383 }
4384
chaviw59b98852017-06-13 12:05:44 -07004385 void wakeUp(String reason) {
4386 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4387 }
4388
4389 /**
4390 * Begin deferring resume to avoid duplicate resumes in one pass.
4391 */
4392 private void beginDeferResume() {
4393 mDeferResumeCount++;
4394 }
4395
4396 /**
4397 * End deferring resume and determine if resume can be called.
4398 */
4399 private void endDeferResume() {
4400 mDeferResumeCount--;
4401 }
4402
4403 /**
4404 * @return True if resume can be called.
4405 */
4406 private boolean readyToResume() {
4407 return mDeferResumeCount == 0;
4408 }
4409
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004410 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004411
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004412 public ActivityStackSupervisorHandler(Looper looper) {
4413 super(looper);
4414 }
4415
Winson Chung4dabf232017-01-25 13:25:22 -08004416 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004417 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004418 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4419 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004420 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004421 }
4422
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004423 @Override
4424 public void handleMessage(Message msg) {
4425 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004426 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4427 synchronized (mService) {
4428 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4429 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004430 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004431 }
4432 }
4433 } break;
4434 case REPORT_PIP_MODE_CHANGED_MSG: {
4435 synchronized (mService) {
4436 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4437 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004438 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4439 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004440 }
4441 }
4442 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004443 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004444 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4445 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004446 // We don't at this point know if the activity is fullscreen,
4447 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004448 activityIdleInternal((ActivityRecord) msg.obj,
4449 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004450 } break;
4451 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004452 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004453 activityIdleInternal((ActivityRecord) msg.obj,
4454 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004455 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004456 case RESUME_TOP_ACTIVITY_MSG: {
4457 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004458 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004459 }
4460 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004461 case SLEEP_TIMEOUT_MSG: {
4462 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004463 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004464 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004465 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004466 }
4467 }
4468 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004469 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004470 synchronized (mService) {
4471 if (mLaunchingActivity.isHeld()) {
4472 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4473 if (VALIDATE_WAKE_LOCK_CALLER
4474 && Binder.getCallingUid() != Process.myUid()) {
4475 throw new IllegalStateException("Calling must be system uid");
4476 }
4477 mLaunchingActivity.release();
4478 }
4479 }
4480 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004481 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004482 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004483 } break;
4484 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004485 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004486 } break;
4487 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004488 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004489 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004490 case LAUNCH_TASK_BEHIND_COMPLETE: {
4491 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004492 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004493 if (r != null) {
4494 handleLaunchTaskBehindCompleteLocked(r);
4495 }
4496 }
4497 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004498
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004499 }
4500 }
4501 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004502
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004503 ActivityStack findStackBehind(ActivityStack stack) {
4504 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004505 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004506 if (display == null) {
4507 return null;
4508 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004509 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4510 if (display.getChildAt(i) == stack && i > 0) {
4511 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004512 }
4513 }
4514 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004515 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004516 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004517
Jorim Jaggic69bd222016-03-15 14:38:37 +01004518 /**
4519 * Puts a task into resizing mode during the next app transition.
4520 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004521 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004522 */
Bryce Leed3624e12017-11-30 08:51:45 -08004523 void setResizingDuringAnimation(TaskRecord task) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004524 mResizingTasksDuringAnimation.add(task.taskId);
4525 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004526 }
4527
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004528 int startActivityFromRecents(int callingPid, int callingUid, int taskId,
4529 SafeActivityOptions options) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004530 final TaskRecord task;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004531 final String callingPackage;
4532 final Intent intent;
4533 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004534 int activityType = ACTIVITY_TYPE_UNDEFINED;
4535 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004536 final ActivityOptions activityOptions = options != null
4537 ? options.getOptions(this)
4538 : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004539 if (activityOptions != null) {
4540 activityType = activityOptions.getLaunchActivityType();
4541 windowingMode = activityOptions.getLaunchWindowingMode();
4542 }
4543 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004544 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004545 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004546 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004547
Matthew Ng606dd802017-06-05 14:06:32 -07004548 mWindowManager.deferSurfaceLayout();
4549 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004550 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004551 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004552 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004553
Matthew Ng606dd802017-06-05 14:06:32 -07004554 // Defer updating the stack in which recents is until the app transition is done, to
4555 // not run into issues where we still need to draw the task in recents but the
4556 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004557 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004558 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004559 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004560
Matthew Ng606dd802017-06-05 14:06:32 -07004561 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004562 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004563 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004564 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004565 mWindowManager.executeAppTransition();
4566 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004567 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004568 }
4569
Wale Ogunwaleabc44d02017-11-08 08:58:03 -08004570 if (windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4571 // We always want to return to the home activity instead of the recents activity
4572 // from whatever is started from the recents activity, so move the home stack
4573 // forward.
4574 moveHomeStackToFront("startActivityFromRecents");
4575 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07004576
Matthew Ng606dd802017-06-05 14:06:32 -07004577 // If the user must confirm credentials (e.g. when first launching a work app and the
4578 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4579 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4580 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004581 final ActivityRecord targetActivity = task.getTopActivity();
4582
Bryce Leed3624e12017-11-30 08:51:45 -08004583 sendPowerHintForLaunchStartIfNeeded(true /* forceSend */, targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004584 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004585 try {
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004586 mService.moveTaskToFrontLocked(task.taskId, 0, options,
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004587 true /* fromRecents */);
4588 } finally {
4589 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4590 targetActivity);
4591 }
Matthew Ng606dd802017-06-05 14:06:32 -07004592
4593 // If we are launching the task in the docked stack, put it into resizing mode so
4594 // the window renders full-screen with the background filling the void. Also only
4595 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004596 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004597 setResizingDuringAnimation(task);
4598 }
4599
Bryce Leed3624e12017-11-30 08:51:45 -08004600 mService.getActivityStartController().postStartActivityProcessingForLastStarter(
4601 task.getTopActivity(), ActivityManager.START_TASK_TO_FRONT,
4602 task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004603 return ActivityManager.START_TASK_TO_FRONT;
4604 }
Matthew Ng606dd802017-06-05 14:06:32 -07004605 callingPackage = task.mCallingPackage;
4606 intent = task.intent;
4607 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4608 userId = task.userId;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +01004609 int result = mService.getActivityStartController().startActivityInPackage(
4610 task.mCallingUid, callingPid, callingUid, callingPackage, intent, null, null,
4611 null, 0, 0, options, userId, task,
Bryce Leed3624e12017-11-30 08:51:45 -08004612 "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004613 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004614 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004615 }
Matthew Ng606dd802017-06-05 14:06:32 -07004616 return result;
4617 } finally {
4618 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004619 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004620 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004621
4622 /**
4623 * @return a list of activities which are the top ones in each visible stack. The first
4624 * entry will be the focused activity.
4625 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004626 List<IBinder> getTopVisibleActivities() {
4627 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4628 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004629 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004630 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4631 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004632 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4633 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004634 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004635 if (stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004636 final ActivityRecord top = stack.getTopActivity();
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004637 if (top != null) {
4638 if (stack == mFocusedStack) {
4639 topActivityTokens.add(0, top.appToken);
4640 } else {
4641 topActivityTokens.add(top.appToken);
4642 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004643 }
4644 }
4645 }
4646 }
4647 return topActivityTokens;
4648 }
Bryce Lee4a194382017-04-04 14:32:48 -07004649
4650 /**
4651 * Internal container to store a match qualifier alongside a WaitResult.
4652 */
4653 static class WaitInfo {
4654 private final ComponentName mTargetComponent;
4655 private final WaitResult mResult;
4656
4657 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4658 this.mTargetComponent = targetComponent;
4659 this.mResult = result;
4660 }
4661
Wale Ogunwale3270f172017-04-26 07:29:42 -07004662 public boolean matches(ComponentName targetComponent) {
4663 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004664 }
4665
4666 public WaitResult getResult() {
4667 return mResult;
4668 }
4669
4670 public ComponentName getComponent() {
4671 return mTargetComponent;
4672 }
4673
4674 public void dump(PrintWriter pw, String prefix) {
4675 pw.println(prefix + "WaitInfo:");
4676 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4677 pw.println(prefix + " mResult=");
4678 mResult.dump(pw, prefix);
4679 }
4680 }
David Stevens9440dc82017-03-16 19:00:20 -07004681
4682 private final class SleepTokenImpl extends SleepToken {
4683 private final String mTag;
4684 private final long mAcquireTime;
4685 private final int mDisplayId;
4686
4687 public SleepTokenImpl(String tag, int displayId) {
4688 mTag = tag;
4689 mDisplayId = displayId;
4690 mAcquireTime = SystemClock.uptimeMillis();
4691 }
4692
4693 @Override
4694 public void release() {
4695 synchronized (mService) {
4696 removeSleepTokenLocked(this);
4697 }
4698 }
4699
4700 @Override
4701 public String toString() {
4702 return "{\"" + mTag + "\", display " + mDisplayId
4703 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4704 }
4705 }
4706
Craig Mautner27084302013-03-25 08:05:25 -07004707}