blob: a4e825117e04286da4cf522e23614d091a5711ac [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;
Andrii Kulian3a95edc2017-06-28 16:21:07 -070020import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Jorim Jaggife762342016-10-13 14:33:27 +020021import static android.Manifest.permission.START_ANY_ACTIVITY;
22import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
Jorim Jaggife762342016-10-13 14:33:27 +020023import static android.app.ActivityManager.START_TASK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +020024import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070025import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
26import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070027import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070028import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070029import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
31import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070032import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070033import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070034import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
36import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070037import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070038import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070039import static android.app.WindowConfiguration.activityTypeToString;
40import static android.app.WindowConfiguration.windowingModeToString;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070041import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020042import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070043import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010044import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020045import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
46import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070047import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070048import static android.view.Display.TYPE_VIRTUAL;
Winson Chung1dbc8112017-09-28 18:05:31 -070049
Winson Chung47900652017-04-06 18:44:25 -070050import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020061import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020063import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020070import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
71import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
72import static com.android.server.am.ActivityManagerService.ANIMATE;
73import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
75import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
76import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
77import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
78import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
79import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
80import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
81import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
82import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070083import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020084import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
85import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Winson Chung74666102017-02-22 17:49:24 -080086import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
87import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
88import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Winson Chung1dbc8112017-09-28 18:05:31 -070089import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Steven Timotius4346f0a2017-09-12 11:07:21 -070090import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
91import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
92import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
93import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
Jorim Jaggife762342016-10-13 14:33:27 +020094import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Winson Chung1dbc8112017-09-28 18:05:31 -070095
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080096import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020097
Svetoslav7008b512015-06-24 18:47:07 -070098import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080099import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700100import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700101import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100102import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700103import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800105import android.app.ActivityManager.RunningTaskInfo;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700107import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700110import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700111import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700112import android.app.WaitResult;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700113import android.app.WindowConfiguration.ActivityType;
114import android.app.WindowConfiguration.WindowingMode;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700115import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700116import android.content.Context;
117import android.content.Intent;
118import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700120import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.PackageManager;
122import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100123import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800125import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800126import android.hardware.display.DisplayManager;
127import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800128import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800129import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700130import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100131import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700132import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700135import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700136import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700137import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700138import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700139import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700140import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700141import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700142import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100143import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700144import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700145import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700146import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700147import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700148import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700149import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800150import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700151import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700152import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800153import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800154import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700155import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700156import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800157import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800158
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700159import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800160import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800161import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700162import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700163import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800164import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700165import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700166import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700167import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700168import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700169
Craig Mautner8d341ef2013-03-26 09:03:27 -0700170import java.io.FileDescriptor;
171import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700172import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800173import java.lang.annotation.Retention;
174import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700175import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700176import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700177import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700178import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700179
Winson Chung1dbc8112017-09-28 18:05:31 -0700180public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
181 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800182 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700183 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700187 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800191 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800192
Craig Mautnerf3333272013-04-22 10:55:53 -0700193 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700195
Craig Mautner0eea92c2013-05-16 13:35:39 -0700196 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700199 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700200 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201
Craig Mautner05d29032013-05-03 13:40:13 -0700202 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
203 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
204 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700205 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700206 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800207 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
208 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
209 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700210 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800211 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
212 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800213
Wale Ogunwale040b4702015-08-06 18:10:50 -0700214 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 // Used to indicate if an object (e.g. stack) that we are trying to get
217 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800218 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700219
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700220 // Used to indicate that windows of activities should be preserved during the resize.
221 static final boolean PRESERVE_WINDOWS = true;
222
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223 // Used to indicate if an object (e.g. task) should be moved/created
224 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700225 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700226
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700227 // Don't execute any calls to resume.
228 static final boolean DEFER_RESUME = true;
229
Winson Chung010927a2016-12-15 16:12:35 -0800230 // Used to indicate that a task is removed it should also be removed from recents.
231 static final boolean REMOVE_FROM_RECENTS = true;
232
Winson Chung6954fc92017-03-24 16:22:12 -0700233 // Used to indicate that pausing an activity should occur immediately without waiting for
234 // the activity callback indicating that it has completed pausing
235 static final boolean PAUSE_IMMEDIATELY = true;
236
Winson Chung7900bee2017-03-10 08:59:25 -0800237 /**
238 * The modes which affect which tasks are returned when calling
239 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
240 */
241 @Retention(RetentionPolicy.SOURCE)
242 @IntDef({
243 MATCH_TASK_IN_STACKS_ONLY,
244 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
245 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
246 })
247 public @interface AnyTaskForIdMatchTaskMode {}
248 // Match only tasks in the current stacks
249 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
250 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
251 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
252 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
253 // provided stack id
254 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
255
Svetoslav7008b512015-06-24 18:47:07 -0700256 // Activity actions an app cannot start if it uses a permission which is not granted.
257 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
258 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700259
Svetoslav7008b512015-06-24 18:47:07 -0700260 static {
261 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
262 Manifest.permission.CAMERA);
263 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
264 Manifest.permission.CAMERA);
265 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
266 Manifest.permission.CALL_PHONE);
267 }
268
Svet Ganov99b60432015-06-27 13:15:22 -0700269 /** Action restriction: launching the activity is not restricted. */
270 private static final int ACTIVITY_RESTRICTION_NONE = 0;
271 /** Action restriction: launching the activity is restricted by a permission. */
272 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
273 /** Action restriction: launching the activity is restricted by an app op. */
274 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700275
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700276 // For debugging to make sure the caller when acquiring/releasing our
277 // wake lock is the system process.
278 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800279 /** The number of distinct task ids that can be assigned to the tasks of a single user */
280 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner27084302013-03-25 08:05:25 -0700282 final ActivityManagerService mService;
283
Winson Chung61c9e5a2017-10-11 10:39:32 -0700284 /** The historial list of recent tasks including inactive tasks */
Winson Chung1dbc8112017-09-28 18:05:31 -0700285 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800286
Winson Chung61c9e5a2017-10-11 10:39:32 -0700287 /** Helper class to abstract out logic for fetching the set of currently running tasks */
Winson Chung3f0e59a2017-10-25 10:19:05 -0700288 private RunningTasks mRunningTasks;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Bryce Leedacefc42017-10-10 12:56:02 -0700296 private final LaunchingBoundsController mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700297
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700298 /** Counter for next free stack ID to use for dynamic activity stacks. */
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700299 private int mNextFreeStackId = 0;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800301 /**
302 * Maps the task identifier that activities are currently being started in to the userId of the
303 * task. Each time a new task is created, the entry for the userId of the task is incremented
304 */
305 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner2420ead2013-04-01 17:13:20 -0700307 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack containing the launcher app. Assumed to always be attached to
311 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800312 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700313
Craig Mautnere0a38842013-12-16 16:14:02 -0800314 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700316
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
318 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
322 /** List of activities that are waiting for a new activity to become visible before completing
323 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700324 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
325 // mStoppingActivities when something else comes up.
326 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700327
Bryce Lee4a194382017-04-04 14:32:48 -0700328 /** List of processes waiting to find out when a specific activity becomes visible. */
329 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
331 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700332 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334 /** List of activities that are ready to be stopped, but waiting for the next activity to
335 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337
Craig Mautnerf3333272013-04-22 10:55:53 -0700338 /** List of activities that are ready to be finished, but waiting for the previous activity to
339 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700341
Craig Mautner0eea92c2013-05-16 13:35:39 -0700342 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800343 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344
Wale Ogunwale22e25262016-02-01 10:32:02 -0800345 /** List of activities whose multi-window mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
348
349 /** List of activities whose picture-in-picture mode changed that we need to report to the
350 * application */
351 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
352
Winson Chung5af42fc2017-03-24 17:11:33 -0700353 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
354 * the application */
355 Rect mPipModeChangedTargetStackBounds;
356
Craig Mautnerf3333272013-04-22 10:55:53 -0700357 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700358 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700359
Craig Mautnerde4ef022013-04-07 19:01:33 -0700360 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
361 * is being brought in front of us. */
362 boolean mUserLeaving = false;
363
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700365 * We don't want to allow the device to go to sleep while in the process
366 * of launching an activity. This is primarily to allow alarm intent
367 * receivers to launch an activity and get that to run before the device
368 * goes back to sleep.
369 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700370 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700371
372 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373 * Set when the system is going to sleep, until we have
374 * successfully paused the current activity and released our wake lock.
375 * At that point the system is allowed to actually sleep.
376 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700377 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700378
David Stevens9440dc82017-03-16 19:00:20 -0700379 /**
380 * A list of tokens that cause the top activity to be put to sleep.
381 * They are used by components that may hide and block interaction with underlying
382 * activities.
383 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700384 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700385
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700386 /** Stack id of the front stack when user switched, indexed by userId. */
387 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700388
Bryce Leeaf3a4002017-03-20 10:37:12 -0700389 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700391 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800393 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
394
395 private DisplayManagerInternal mDisplayManagerInternal;
396 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800397
Wale Ogunwaled046a012015-12-24 13:05:59 -0800398 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800399 boolean inResumeTopActivity;
400
Andrii Kulian1e32e022016-09-16 15:29:34 -0700401 /**
402 * Temporary rect used during docked stack resize calculation so we don't need to create a new
403 * object each time.
404 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700405 private final Rect tempRect = new Rect();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700406 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700407
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700408 // The default minimal size that will be used if the activity doesn't specify its minimal size.
409 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700410 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700411
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700412 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
413 private boolean mTaskLayersChanged = true;
414
Jorim Jaggi275561a2016-02-23 10:11:02 -0500415 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800416
Jorim Jaggiea039a82017-08-02 14:37:49 +0200417 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
418
Andrii Kulian1779e612016-10-12 21:58:25 -0700419 @Override
420 protected int getChildCount() {
421 return mActivityDisplays.size();
422 }
423
424 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700425 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700426 return mActivityDisplays.valueAt(index);
427 }
428
429 @Override
430 protected ConfigurationContainer getParent() {
431 return null;
432 }
433
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700434 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700435 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700436 if (activityDisplay == null) {
437 throw new IllegalArgumentException("No display found with id: " + displayId);
438 }
439
440 return activityDisplay.getOverrideConfiguration();
441 }
442
443 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700444 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700445 if (activityDisplay == null) {
446 throw new IllegalArgumentException("No display found with id: " + displayId);
447 }
448
449 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
450 }
451
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700452 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700453 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
454 int callingUid, ActivityInfo activityInfo) {
455 if (displayId == DEFAULT_DISPLAY) {
456 // No restrictions for the default display.
457 return true;
458 }
459 if (!mService.mSupportsMultiDisplay) {
460 // Can't launch on secondary displays if feature is not supported.
461 return false;
462 }
463 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
464 // Can't apply wrong configuration to non-resizeable activities.
465 return false;
466 }
467 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
468 // Can't place activities to a display that has restricted launch rules.
469 // In this case the request should be made by explicitly adding target display id and
470 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
471 return false;
472 }
473 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700474 }
475
476 /**
477 * Check if configuration of specified display matches current global config.
478 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
479 * the same config as on the default display.
480 * @param displayId Id of the display to check.
481 * @return {@code true} if configuration matches.
482 */
483 private boolean displayConfigMatchesGlobal(int displayId) {
484 if (displayId == DEFAULT_DISPLAY) {
485 return true;
486 }
487 if (displayId == INVALID_DISPLAY) {
488 return false;
489 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700490 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700491 if (targetDisplay == null) {
492 throw new IllegalArgumentException("No display found with id: " + displayId);
493 }
494 return getConfiguration().equals(targetDisplay.getConfiguration());
495 }
496
Wale Ogunwale39381972015-12-17 17:15:29 -0800497 static class FindTaskResult {
498 ActivityRecord r;
499 boolean matchedByRootAffinity;
500 }
501 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
502
Craig Mautneree36c772014-07-16 14:56:05 -0700503 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800504 * Temp storage for display ids sorted in focus order.
505 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
506 * it's more efficient, as the number of displays is usually small.
507 */
508 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
509
510 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100511 * Used to keep track whether app visibilities got changed since the last pause. Useful to
512 * determine whether to invoke the task stack change listener after pausing.
513 */
514 boolean mAppVisibilitiesChangedSinceLastPause;
515
516 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100517 * Set of tasks that are in resizing mode during an app transition to fill the "void".
518 */
519 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
520
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700521
522 /**
523 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
524 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
525 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
526 * like the docked stack going empty.
527 */
528 private boolean mAllowDockedStackResize = true;
529
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100530 /**
Tony Mak853304c2016-04-18 15:17:41 +0100531 * Is dock currently minimized.
532 */
533 boolean mIsDockMinimized;
534
Jorim Jaggife762342016-10-13 14:33:27 +0200535 final KeyguardController mKeyguardController;
536
chaviw59b98852017-06-13 12:05:44 -0700537 private PowerManager mPowerManager;
538 private int mDeferResumeCount;
539
Tony Mak853304c2016-04-18 15:17:41 +0100540 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700541 * Description of a request to start a new activity, which has been held
542 * due to app switches being disabled.
543 */
544 static class PendingActivityLaunch {
545 final ActivityRecord r;
546 final ActivityRecord sourceRecord;
547 final int startFlags;
548 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700549 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700550
551 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700552 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700553 r = _r;
554 sourceRecord = _sourceRecord;
555 startFlags = _startFlags;
556 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700557 callerApp = _callerApp;
558 }
559
560 void sendErrorResult(String message) {
561 try {
562 if (callerApp.thread != null) {
563 callerApp.thread.scheduleCrash(message);
564 }
565 } catch (RemoteException e) {
566 Slog.e(TAG, "Exception scheduling crash of failed "
567 + "activity launcher sourceRecord=" + sourceRecord, e);
568 }
Craig Mautneree36c772014-07-16 14:56:05 -0700569 }
570 }
571
Bryce Leeaf691c02017-03-20 14:20:22 -0700572 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700573 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700574 mHandler = new ActivityStackSupervisorHandler(looper);
Winson Chung3f0e59a2017-10-25 10:19:05 -0700575 mRunningTasks = createRunningTasks();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200576 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext, looper);
Jorim Jaggife762342016-10-13 14:33:27 +0200577 mKeyguardController = new KeyguardController(service, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700578
579 mLaunchingBoundsController = new LaunchingBoundsController();
580 mLaunchingBoundsController.registerDefaultPositioners(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700581 }
582
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800583 void setRecentTasks(RecentTasks recentTasks) {
584 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700585 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800586 }
587
Winson Chung3f0e59a2017-10-25 10:19:05 -0700588 @VisibleForTesting
589 RunningTasks createRunningTasks() {
590 return new RunningTasks();
591 }
592
Jeff Brown2c43c332014-06-12 22:38:59 -0700593 /**
594 * At the time when the constructor runs, the power manager has not yet been
595 * initialized. So we initialize our wakelocks afterwards.
596 */
597 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700598 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
599 mGoingToSleep = mPowerManager
600 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
601 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700602 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700603 }
604
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700605 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 synchronized (mService) {
607 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200608 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609
610 mDisplayManager =
611 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
612 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800613 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614
615 Display[] displays = mDisplayManager.getDisplays();
616 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
617 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700618 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800619 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700620 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 }
622
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700623 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
624 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800625
626 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 }
Craig Mautner27084302013-03-25 08:05:25 -0700628 }
629
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700630 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
633
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800635 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700636 }
637
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700638 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700639 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700640 }
641
Wale Ogunwaled046a012015-12-24 13:05:59 -0800642 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800643 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
644 if (!focusCandidate.isFocusable()) {
645 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800646 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 }
648
649 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800650 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800652
Wale Ogunwaled046a012015-12-24 13:05:59 -0800653 EventLogTags.writeAmFocusedStack(
654 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
655 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
656 }
657
658 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800659 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800660 if (r != null && r.idle) {
661 checkFinishBootingLocked();
662 }
663 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700664 }
665
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700666 void moveHomeStackToFront(String reason) {
667 mHomeStack.moveToFront(reason);
668 }
669
Matthew Ng330757d2017-02-28 14:19:17 -0800670 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700671 final ActivityStack recentsStack = getDefaultDisplay().getStack(
672 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800673 if (recentsStack != null) {
674 recentsStack.moveToFront(reason);
675 }
676 }
677
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800679 boolean moveHomeStackTaskToTop(String reason) {
680 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700682 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683 if (top == null) {
684 return false;
685 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700686 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700687 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700688 }
689
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800690 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700691 if (!mService.mBooting && !mService.mBooted) {
692 // Not ready yet!
693 return false;
694 }
695
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800696 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700697 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800698 final String myReason = reason + " resumeHomeStackTask";
699
Mark Lua56ea122015-10-08 13:31:01 +0800700 // Only resume home activity if isn't finishing.
701 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700702 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800703 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700704 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700706 }
707
Craig Mautner8d341ef2013-03-26 09:03:27 -0700708 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700709 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
710 }
711
712 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700713 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700714 }
715
716 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700717 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700718 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800719 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700720 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700721 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700722 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700723 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700724 @Nullable ActivityOptions aOptions, boolean onTop) {
725 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700726 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
727 throw new IllegalArgumentException("Should not specify activity options for non-restore"
728 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800729 }
730
Craig Mautnere0a38842013-12-16 16:14:02 -0800731 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700733 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
734 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
735 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700736 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700737 if (task == null) {
738 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700740 if (aOptions != null) {
741 // Resolve the stack the task should be placed in now based on options
742 // and reparent if needed.
743 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
744 if (launchStack != null && stack != launchStack) {
745 final int reparentMode = onTop
746 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
747 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
748 "anyTaskForIdLocked");
749 }
750 }
751 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700752 }
753 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800754
Winson Chung7900bee2017-03-10 08:59:25 -0800755 // If we are matching stack tasks only, return now
756 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800757 return null;
758 }
759
Winson Chung7900bee2017-03-10 08:59:25 -0800760 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
761 // the task from recents
762 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700763 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700764
765 if (task == null) {
766 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800767 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
768 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700769
770 return null;
771 }
772
773 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700774 return task;
775 }
776
Winson Chung7900bee2017-03-10 08:59:25 -0800777 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700778 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700779 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
780 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800781 return null;
782 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700783 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800784 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700785 }
786
Craig Mautner6170f732013-04-02 13:05:23 -0700787 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800788 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800789 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700790 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
791 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
792 final ActivityStack stack = display.getChildAt(stackNdx);
793 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 if (r != null) {
795 return r;
796 }
Craig Mautner6170f732013-04-02 13:05:23 -0700797 }
798 }
799 return null;
800 }
801
Tony Mak853304c2016-04-18 15:17:41 +0100802 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000803 * Detects whether we should show a lock screen in front of this task for a locked user.
804 * <p>
805 * We'll do this if either of the following holds:
806 * <ul>
807 * <li>The top activity explicitly belongs to {@param userId}.</li>
808 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
809 * </ul>
810 *
811 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100812 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000813 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
814 // To handle the case that work app is in the task but just is not the top one.
815 final ActivityRecord activityRecord = task.getTopActivity();
816 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
817
818 return (activityRecord != null && activityRecord.userId == userId)
819 || (resultTo != null && resultTo.userId == userId);
820 }
821
822 /**
823 * Find all visible task stacks containing {@param userId} and intercept them with an activity
824 * to block out the contents and possibly start a credential-confirming intent.
825 *
826 * @param userId user handle for the locked managed profile.
827 */
828 void lockAllProfileTasks(@UserIdInt int userId) {
829 mWindowManager.deferSurfaceLayout();
830 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700831 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
832 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
833 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
834 final ActivityStack stack = display.getChildAt(stackNdx);
835 final List<TaskRecord> tasks = stack.getAllTasks();
836 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
837 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000838
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700839 // Check the task for a top activity belonging to userId, or returning a
840 // result to an activity belonging to userId. Example case: a document
841 // picker for personal files, opened by a work app, should still get locked.
842 if (taskTopActivityIsUser(task, userId)) {
843 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
844 task.taskId, userId);
845 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000846 }
Tony Mak853304c2016-04-18 15:17:41 +0100847 }
848 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000849 } finally {
850 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100851 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000852 }
853
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800854 void setNextTaskIdForUserLocked(int taskId, int userId) {
855 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
856 if (taskId > currentTaskId) {
857 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700858 }
859 }
860
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700861 static int nextTaskIdForUser(int taskId, int userId) {
862 int nextTaskId = taskId + 1;
863 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
864 // Wrap around as there will be smaller task ids that are available now.
865 nextTaskId -= MAX_TASK_IDS_PER_USER;
866 }
867 return nextTaskId;
868 }
869
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800870 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800871 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
872 // for a userId u, a taskId can only be in the range
873 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
874 // 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 -0700875 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700876 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700877 || anyTaskForIdLocked(
878 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700879 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800880 if (candidateTaskId == currentTaskId) {
881 // Something wrong!
882 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
883 throw new IllegalStateException("Cannot get an available task id."
884 + " Reached limit of " + MAX_TASK_IDS_PER_USER
885 + " running tasks per user.");
886 }
887 }
888 mCurTaskIdForUser.put(userId, candidateTaskId);
889 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700890 }
891
Chong Zhang6cda19c2016-06-14 19:07:56 -0700892 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800893 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 if (stack == null) {
895 return null;
896 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 ActivityRecord resumedActivity = stack.mResumedActivity;
898 if (resumedActivity == null || resumedActivity.app == null) {
899 resumedActivity = stack.mPausingActivity;
900 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700901 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 }
903 }
904 return resumedActivity;
905 }
906
Dianne Hackbornff072722014-09-24 10:56:28 -0700907 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700908 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800910 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700911 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
912 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
913 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700914 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 continue;
916 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200917 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
918 final ActivityRecord top = stack.topRunningActivityLocked();
919 final int size = mTmpActivityList.size();
920 for (int i = 0; i < size; i++) {
921 final ActivityRecord activity = mTmpActivityList.get(i);
922 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
923 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200925 if (realStartActivityLocked(activity, app,
926 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 didSomething = true;
928 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700929 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800930 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200931 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800932 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700933 }
Craig Mautner20e72272013-04-01 13:45:53 -0700934 }
Craig Mautner20e72272013-04-01 13:45:53 -0700935 }
936 }
937 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700938 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700939 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700940 }
Craig Mautner20e72272013-04-01 13:45:53 -0700941 return didSomething;
942 }
943
944 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800945 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700946 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
947 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
948 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700949 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 continue;
951 }
952 final ActivityRecord resumedActivity = stack.mResumedActivity;
953 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700954 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800955 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 return false;
957 }
Craig Mautner20e72272013-04-01 13:45:53 -0700958 }
959 }
Wei Wang65c7a152016-06-02 18:51:22 -0700960 // Send launch end powerhint when idle
961 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700962 return true;
963 }
964
Craig Mautnerde4ef022013-04-07 19:01:33 -0700965 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800966 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700967 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
968 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
969 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700970 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800971 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800973 return false;
974 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 }
976 }
977 }
978 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700979 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800980 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
981 mLastFocusedStack + " to=" + mFocusedStack);
982 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700983 return true;
984 }
985
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700986 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800987 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700989 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
990 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
991 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800993 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700994 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800995 return false;
996 }
997 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800998 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700999 }
1000 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001001 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001002 }
1003
Craig Mautner2acc3892013-09-23 10:28:14 -07001004 /**
1005 * Pause all activities in either all of the stacks or just the back stacks.
1006 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001007 * @param resuming The resuming activity.
1008 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1009 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001010 * @return true if any activity was paused as a result of this call.
1011 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001012 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001013 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001015 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1016 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1017 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001018 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001019 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001020 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001021 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1022 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001024 }
1025 }
1026 return someActivityPaused;
1027 }
1028
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001030 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001031 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001032 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1033 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1034 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001035 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001036 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001037 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 Slog.d(TAG_STATES,
1039 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001040 pausing = false;
1041 } else {
1042 return false;
1043 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001044 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001045 }
1046 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001047 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001048 }
1049
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001050 void cancelInitializingActivities() {
1051 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001052 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1053 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1054 final ActivityStack stack = display.getChildAt(stackNdx);
1055 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001056 }
1057 }
1058 }
1059
Bryce Lee4a194382017-04-04 14:32:48 -07001060 void waitActivityVisible(ComponentName name, WaitResult result) {
1061 final WaitInfo waitInfo = new WaitInfo(name, result);
1062 mWaitingForActivityVisible.add(waitInfo);
1063 }
1064
1065 void cleanupActivity(ActivityRecord r) {
1066 // Make sure this record is no longer in the pending finishes list.
1067 // This could happen, for example, if we are trimming activities
1068 // down to the max limit while they are still waiting to finish.
1069 mFinishingActivities.remove(r);
1070 mActivitiesWaitingForVisibleActivity.remove(r);
1071
1072 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001073 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001074 mWaitingForActivityVisible.remove(i);
1075 }
1076 }
1077 }
1078
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001079 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001080 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001081 }
1082
1083 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1084 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001085 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1086 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001087 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001088 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001089 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001090 result.timeout = false;
1091 result.who = w.getComponent();
1092 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1093 result.thisTime = result.totalTime;
1094 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001097 if (changed) {
1098 mService.notifyAll();
1099 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 }
1101
Chong Zhang5022da32016-06-21 16:31:37 -07001102 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1103 boolean changed = false;
1104 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1105 WaitResult w = mWaitingActivityLaunched.remove(i);
1106 if (w.who == null) {
1107 changed = true;
1108 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1109 // the starting activity ends up moving another activity to front, and it should
1110 // wait for this new activity to become visible instead.
1111 // Do not modify other fields.
1112 w.result = START_TASK_TO_FRONT;
1113 }
1114 }
1115 if (changed) {
1116 mService.notifyAll();
1117 }
1118 }
1119
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001120 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1121 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001122 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001124 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001125 if (w.who == null) {
1126 changed = true;
1127 w.timeout = timeout;
1128 if (r != null) {
1129 w.who = new ComponentName(r.info.packageName, r.info.name);
1130 }
1131 w.thisTime = thisTime;
1132 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001133 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001135 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001136 if (changed) {
1137 mService.notifyAll();
1138 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 }
1140
Craig Mautner29219d92013-04-16 20:19:12 -07001141 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001142 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001143 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001144 if (r != null) {
1145 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001146 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001147
Andrii Kulian7318d632016-07-20 18:59:28 -07001148 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001149 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1150
1151 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1152 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001153 final ActivityDisplay display = mActivityDisplays.get(displayId);
1154 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1155 final ActivityStack stack = display.getChildAt(j);
1156 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001157 r = stack.topRunningActivityLocked();
1158 if (r != null) {
1159 return r;
1160 }
Craig Mautner29219d92013-04-16 20:19:12 -07001161 }
1162 }
1163 }
1164 return null;
1165 }
1166
Winson Chung61c9e5a2017-10-11 10:39:32 -07001167 @VisibleForTesting
1168 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1169 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1170 int callingUid, boolean allowed) {
1171 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1172 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001173 }
1174
Todd Kennedy7440f172015-12-09 14:31:22 -08001175 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1176 ProfilerInfo profilerInfo) {
1177 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001178 if (aInfo != null) {
1179 // Store the found target back into the intent, because now that
1180 // we have it we never want to do this again. For example, if the
1181 // user navigates back to this point in the history, we should
1182 // always restart the exact same activity.
1183 intent.setComponent(new ComponentName(
1184 aInfo.applicationInfo.packageName, aInfo.name));
1185
1186 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001187 if (!aInfo.processName.equals("system")) {
1188 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001189 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001190 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001191
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001192 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1193 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1194 }
1195
Man Caocfa78b22015-06-11 20:14:34 -07001196 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1197 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1198 }
1199
1200 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001201 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001202 }
1203 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001204 final String intentLaunchToken = intent.getLaunchToken();
1205 if (aInfo.launchToken == null && intentLaunchToken != null) {
1206 aInfo.launchToken = intentLaunchToken;
1207 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001208 }
1209 return aInfo;
1210 }
1211
Todd Kennedy7440f172015-12-09 14:31:22 -08001212 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001213 return resolveIntent(intent, resolvedType, userId, 0);
1214 }
1215
1216 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001217 synchronized (mService) {
1218 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001219 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001220 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001221 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001222 }
1223
1224 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1225 ProfilerInfo profilerInfo, int userId) {
1226 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1227 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1228 }
1229
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001230 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1231 boolean andResume, boolean checkConfig) throws RemoteException {
1232
1233 if (!allPausedActivitiesComplete()) {
1234 // While there are activities pausing we skipping starting any new activities until
1235 // pauses are complete. NOTE: that we also do this for activities that are starting in
1236 // the paused state because they will first be resumed then paused on the client side.
1237 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1238 "realStartActivityLocked: Skipping start of r=" + r
1239 + " some activities pausing...");
1240 return false;
1241 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001242
Bryce Leeaf691c02017-03-20 14:20:22 -07001243 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001244 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001245
1246 beginDeferResume();
1247
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 try {
chaviw59b98852017-06-13 12:05:44 -07001249 r.startFreezingScreenLocked(app, 0);
1250
1251 // schedule launch ticks to collect information about slow apps.
1252 r.startLaunchTickingLocked();
1253
1254 r.app = app;
1255
Jorim Jaggi838c2452017-08-28 15:44:43 +02001256 if (mKeyguardController.isKeyguardLocked()) {
1257 r.notifyUnknownVisibilityLaunched();
1258 }
1259
chaviw59b98852017-06-13 12:05:44 -07001260 // Have the window manager re-evaluate the orientation of the screen based on the new
1261 // activity order. Note that as a result of this, it can call back into the activity
1262 // manager with a new orientation. We don't care about that, because the activity is
1263 // not currently running so we are just restarting it anyway.
1264 if (checkConfig) {
1265 final int displayId = r.getDisplayId();
1266 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1267 getDisplayOverrideConfiguration(displayId),
1268 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1269 // Deferring resume here because we're going to launch new activity shortly.
1270 // We don't want to perform a redundant launch of the same record while ensuring
1271 // configurations and trying to resume top activity of focused stack.
1272 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1273 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001274 }
chaviw59b98852017-06-13 12:05:44 -07001275
1276 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1277 true /* isTop */)) {
1278 // We only set the visibility to true if the activity is allowed to be visible
1279 // based on
1280 // keyguard state. This avoids setting this into motion in window manager that is
1281 // later cancelled due to later calls to ensure visible activities that set
1282 // visibility back to false.
1283 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 }
chaviw59b98852017-06-13 12:05:44 -07001285
chaviw59b98852017-06-13 12:05:44 -07001286 final int applicationInfoUid =
1287 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1288 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1289 Slog.wtf(TAG,
1290 "User ID for activity changing for " + r
1291 + " appInfo.uid=" + r.appInfo.uid
1292 + " info.ai.uid=" + applicationInfoUid
1293 + " old=" + r.app + " new=" + app);
1294 }
1295
1296 app.waitingToKill = null;
1297 r.launchCount++;
1298 r.lastLaunchTime = SystemClock.uptimeMillis();
1299
1300 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1301
1302 int idx = app.activities.indexOf(r);
1303 if (idx < 0) {
1304 app.activities.add(r);
1305 }
1306 mService.updateLruProcessLocked(app, true, null);
1307 mService.updateOomAdjLocked();
1308
1309 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1310 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001311 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001312 }
1313
1314 try {
1315 if (app.thread == null) {
1316 throw new RemoteException();
1317 }
1318 List<ResultInfo> results = null;
1319 List<ReferrerIntent> newIntents = null;
1320 if (andResume) {
1321 // We don't need to deliver new intents and/or set results if activity is going
1322 // to pause immediately after launch.
1323 results = r.results;
1324 newIntents = r.newIntents;
1325 }
1326 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1327 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1328 + " newIntents=" + newIntents + " andResume=" + andResume);
1329 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1330 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001331 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001332 // Home process is the root process of the task.
1333 mService.mHomeProcess = task.mActivities.get(0).app;
1334 }
1335 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1336 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1337 r.sleeping = false;
1338 r.forceNewConfig = false;
1339 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1340 mService.showAskCompatModeDialogLocked(r);
1341 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1342 ProfilerInfo profilerInfo = null;
1343 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1344 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1345 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001346 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1347 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1348 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001349 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001350 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001351 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001352 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001353 }
1354 }
chaviw59b98852017-06-13 12:05:44 -07001355
Andreas Gampe2b073a02017-06-22 17:28:57 -07001356 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001357 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 }
1359 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001360
chaviw59b98852017-06-13 12:05:44 -07001361 app.hasShownUi = true;
1362 app.pendingUiClean = true;
1363 app.forceProcessStateUpTo(mService.mTopProcessState);
1364 // Because we could be starting an Activity in the system process this may not go
1365 // across a Binder interface which would create a new Configuration. Consequently
1366 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001367
chaviw59b98852017-06-13 12:05:44 -07001368 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1369 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1370 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001371
chaviw59b98852017-06-13 12:05:44 -07001372 logIfTransactionTooLarge(r.intent, r.icicle);
1373 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1374 System.identityHashCode(r), r.info,
1375 // TODO: Have this take the merged configuration instead of separate global
1376 // and override configs.
1377 mergedConfiguration.getGlobalConfiguration(),
1378 mergedConfiguration.getOverrideConfiguration(), r.compat,
1379 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1380 r.persistentState, results, newIntents, !andResume,
1381 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001382
chaviw59b98852017-06-13 12:05:44 -07001383 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1384 // This may be a heavy-weight process! Note that the package
1385 // manager will ensure that only activity can run in the main
1386 // process of the .apk, which is the only thing that will be
1387 // considered heavy-weight.
1388 if (app.processName.equals(app.info.packageName)) {
1389 if (mService.mHeavyWeightProcess != null
1390 && mService.mHeavyWeightProcess != app) {
1391 Slog.w(TAG, "Starting new heavy weight process " + app
1392 + " when already running "
1393 + mService.mHeavyWeightProcess);
1394 }
1395 mService.mHeavyWeightProcess = app;
1396 Message msg = mService.mHandler.obtainMessage(
1397 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1398 msg.obj = r;
1399 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001400 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001401 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001402
chaviw59b98852017-06-13 12:05:44 -07001403 } catch (RemoteException e) {
1404 if (r.launchFailed) {
1405 // This is the second time we failed -- finish activity
1406 // and give up.
1407 Slog.e(TAG, "Second failure launching "
1408 + r.intent.getComponent().flattenToShortString()
1409 + ", giving up", e);
1410 mService.appDiedLocked(app);
1411 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1412 "2nd-crash", false);
1413 return false;
1414 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001415
chaviw59b98852017-06-13 12:05:44 -07001416 // This is the first time we failed -- restart process and
1417 // retry.
1418 r.launchFailed = true;
1419 app.activities.remove(r);
1420 throw e;
1421 }
1422 } finally {
1423 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001424 }
1425
1426 r.launchFailed = false;
1427 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001428 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001429 }
1430
chaviw59b98852017-06-13 12:05:44 -07001431 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001432 // As part of the process of launching, ActivityThread also performs
1433 // a resume.
1434 stack.minimalResumeActivityLocked(r);
1435 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001436 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001437 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001438 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001439 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001440 "Moving to PAUSED: " + r + " (starting in paused state)");
1441 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001442 }
1443
1444 // Launch the new version setup screen if needed. We do this -after-
1445 // launching the initial activity (that is, home), so that it can have
1446 // a chance to initialize itself while in the background, making the
1447 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001448 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001449 mService.startSetupActivityLocked();
1450 }
1451
Dianne Hackborn465fa392014-09-14 14:21:18 -07001452 // Update any services we are bound to that might care about whether
1453 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001454 if (r.app != null) {
1455 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1456 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001457
Craig Mautner2420ead2013-04-01 17:13:20 -07001458 return true;
1459 }
1460
Sudheer Shankafab200f2017-05-17 20:41:53 -07001461 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1462 int extrasSize = 0;
1463 if (intent != null) {
1464 final Bundle extras = intent.getExtras();
1465 if (extras != null) {
1466 extrasSize = extras.getSize();
1467 }
1468 }
1469 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1470 if (extrasSize + icicleSize > 200000) {
1471 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1472 + ", icicle size: " + icicleSize);
1473 }
1474 }
1475
Craig Mautnere79d42682013-04-01 19:01:53 -07001476 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001477 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001478 // Is this activity's application already running?
1479 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001480 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001481
Andrii Kulian02b7a832016-10-06 23:11:56 -07001482 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001483
1484 if (app != null && app.thread != null) {
1485 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001486 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1487 || !"android".equals(r.info.packageName)) {
1488 // Don't add this if it is a platform component that is marked
1489 // to run in multiple processes, because this is actually
1490 // part of the framework so doesn't make sense to track as a
1491 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001492 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1493 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001494 }
George Mount2c92c972014-03-20 09:38:23 -07001495 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001496 return;
1497 } catch (RemoteException e) {
1498 Slog.w(TAG, "Exception when starting activity "
1499 + r.intent.getComponent().flattenToShortString(), e);
1500 }
1501
1502 // If a dead object exception was thrown -- fall through to
1503 // restart the application.
1504 }
1505
1506 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001507 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001508 }
1509
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001510 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001511 String resultWho, int requestCode, int callingPid, int callingUid,
1512 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001513 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001514 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1515 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001516 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001517 return true;
1518 }
1519 final int componentRestriction = getComponentRestrictionForCallingPackage(
1520 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1521 final int actionRestriction = getActionRestrictionForCallingPackage(
1522 intent.getAction(), callingPackage, callingPid, callingUid);
1523 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1524 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1525 if (resultRecord != null) {
1526 resultStack.sendActivityResultLocked(-1,
1527 resultRecord, resultWho, requestCode,
1528 Activity.RESULT_CANCELED, null);
1529 }
1530 final String msg;
1531 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1532 msg = "Permission Denial: starting " + intent.toString()
1533 + " from " + callerApp + " (pid=" + callingPid
1534 + ", uid=" + callingUid + ")" + " with revoked permission "
1535 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1536 } else if (!aInfo.exported) {
1537 msg = "Permission Denial: starting " + intent.toString()
1538 + " from " + callerApp + " (pid=" + callingPid
1539 + ", uid=" + callingUid + ")"
1540 + " not exported from uid " + aInfo.applicationInfo.uid;
1541 } else {
1542 msg = "Permission Denial: starting " + intent.toString()
1543 + " from " + callerApp + " (pid=" + callingPid
1544 + ", uid=" + callingUid + ")"
1545 + " requires " + aInfo.permission;
1546 }
1547 Slog.w(TAG, msg);
1548 throw new SecurityException(msg);
1549 }
1550
1551 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1552 final String message = "Appop Denial: starting " + intent.toString()
1553 + " from " + callerApp + " (pid=" + callingPid
1554 + ", uid=" + callingUid + ")"
1555 + " requires " + AppOpsManager.permissionToOp(
1556 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1557 Slog.w(TAG, message);
1558 return false;
1559 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1560 final String message = "Appop Denial: starting " + intent.toString()
1561 + " from " + callerApp + " (pid=" + callingPid
1562 + ", uid=" + callingUid + ")"
1563 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1564 Slog.w(TAG, message);
1565 return false;
1566 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001567 if (options != null) {
Winson Chung3f0e59a2017-10-25 10:19:05 -07001568 // If a launch task id is specified, then ensure that the caller is the recents
1569 // component or has the START_TASKS_FROM_RECENTS permission
1570 if (options.getLaunchTaskId() != INVALID_TASK_ID
1571 && !mRecentTasks.isCallerRecents(callingUid)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001572 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1573 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001574 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001575 final String msg = "Permission Denial: starting " + intent.toString()
1576 + " from " + callerApp + " (pid=" + callingPid
1577 + ", uid=" + callingUid + ") with launchTaskId="
1578 + options.getLaunchTaskId();
1579 Slog.w(TAG, msg);
1580 throw new SecurityException(msg);
1581 }
1582 }
1583 // Check if someone tries to launch an activity on a private display with a different
1584 // owner.
1585 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001586 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1587 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001588 final String msg = "Permission Denial: starting " + intent.toString()
1589 + " from " + callerApp + " (pid=" + callingPid
1590 + ", uid=" + callingUid + ") with launchDisplayId="
1591 + launchDisplayId;
1592 Slog.w(TAG, msg);
1593 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001594 }
Charles He2bf28322017-10-12 22:24:49 +01001595 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1596 final boolean lockTaskMode = options.getLockTaskMode();
1597 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1598 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1599 final String msg = "Permission Denial: starting " + intent.toString()
1600 + " from " + callerApp + " (pid=" + callingPid
1601 + ", uid=" + callingUid + ") with lockTaskMode=true";
1602 Slog.w(TAG, msg);
1603 throw new SecurityException(msg);
1604 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001605 }
1606
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001607 return true;
1608 }
1609
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001610 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001611 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1612 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001613 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1614 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1615
Andrii Kulian02689a72017-07-06 14:28:59 -07001616 if (callingPid == -1 && callingUid == -1) {
1617 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1618 return true;
1619 }
1620
Andrii Kulian62e6f252017-05-30 22:46:53 -07001621 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001622 if (activityDisplay == null) {
1623 Slog.w(TAG, "Launch on display check: display not found");
1624 return false;
1625 }
1626
Andrii Kulian02689a72017-07-06 14:28:59 -07001627 // Check if the caller has enough privileges to embed activities and launch to private
1628 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001629 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001630 callingUid);
1631 if (startAnyPerm == PERMISSION_GRANTED) {
1632 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1633 + " allow launch any on display");
1634 return true;
1635 }
1636
Andrii Kulian8f070292017-09-12 22:56:49 -07001637 // Check if caller is already present on display
1638 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1639
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001640 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1641 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1642 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001643 // Limit launching on virtual displays, because their contents can be read from Surface
1644 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001645 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1646 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1647 + " disallow launch on virtual display for not-embedded activity.");
1648 return false;
1649 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001650 // Check if the caller is allowed to embed activities from other apps.
1651 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001652 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001653 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1654 + " disallow activity embedding without permission.");
1655 return false;
1656 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001657 }
1658
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001659 if (!activityDisplay.isPrivate()) {
1660 // Anyone can launch on a public display.
1661 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1662 + " allow launch on public display");
1663 return true;
1664 }
1665
1666 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001667 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001668 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1669 + " allow launch for owner of the display");
1670 return true;
1671 }
1672
Andrii Kulian8f070292017-09-12 22:56:49 -07001673 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001674 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1675 + " allow launch for caller present on the display");
1676 return true;
1677 }
1678
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001679 Slog.w(TAG, "Launch on display check: denied");
1680 return false;
1681 }
1682
1683 /** Update lists of UIDs that are present on displays and have access to them. */
1684 void updateUIDsPresentOnDisplay() {
1685 mDisplayAccessUIDs.clear();
1686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1687 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001688 // Only bother calculating the whitelist for private displays
1689 if (activityDisplay.isPrivate()) {
1690 mDisplayAccessUIDs.append(
1691 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1692 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001693 }
1694 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1695 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1696 }
1697
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001698 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001699 final long identity = Binder.clearCallingIdentity();
1700 try {
1701 return UserManager.get(mService.mContext).getUserInfo(userId);
1702 } finally {
1703 Binder.restoreCallingIdentity(identity);
1704 }
1705 }
1706
Svet Ganov99b60432015-06-27 13:15:22 -07001707 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001708 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001709 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1710 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001711 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001712 return ACTIVITY_RESTRICTION_PERMISSION;
1713 }
1714
Christopher Tateff7add02015-08-17 10:23:22 -07001715 if (activityInfo.permission == null) {
1716 return ACTIVITY_RESTRICTION_NONE;
1717 }
1718
Svet Ganov99b60432015-06-27 13:15:22 -07001719 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1720 if (opCode == AppOpsManager.OP_NONE) {
1721 return ACTIVITY_RESTRICTION_NONE;
1722 }
1723
1724 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1725 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001726 if (!ignoreTargetSecurity) {
1727 return ACTIVITY_RESTRICTION_APPOP;
1728 }
Svet Ganov99b60432015-06-27 13:15:22 -07001729 }
1730
1731 return ACTIVITY_RESTRICTION_NONE;
1732 }
1733
Svetoslav7008b512015-06-24 18:47:07 -07001734 private int getActionRestrictionForCallingPackage(String action,
1735 String callingPackage, int callingPid, int callingUid) {
1736 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001737 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001738 }
1739
1740 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1741 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 final PackageInfo packageInfo;
1746 try {
1747 packageInfo = mService.mContext.getPackageManager()
1748 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1749 } catch (PackageManager.NameNotFoundException e) {
1750 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001751 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001752 }
1753
1754 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001758 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 final int opCode = AppOpsManager.permissionToOpCode(permission);
1763 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1768 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
Svet Ganov99b60432015-06-27 13:15:22 -07001772 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001773 }
1774
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001775 void setLaunchSource(int uid) {
1776 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1777 }
1778
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001779 void acquireLaunchWakelock() {
1780 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1781 throw new IllegalStateException("Calling must be system uid");
1782 }
1783 mLaunchingActivity.acquire();
1784 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1785 // To be safe, don't allow the wake lock to be held for too long.
1786 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1787 }
1788 }
1789
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001790 /**
1791 * Called when the frontmost task is idle.
1792 * @return the state of mService.mBooting before this was called.
1793 */
1794 private boolean checkFinishBootingLocked() {
1795 final boolean booting = mService.mBooting;
1796 boolean enableScreen = false;
1797 mService.mBooting = false;
1798 if (!mService.mBooted) {
1799 mService.mBooted = true;
1800 enableScreen = true;
1801 }
1802 if (booting || enableScreen) {
1803 mService.postFinishBooting(booting, enableScreen);
1804 }
1805 return booting;
1806 }
1807
Craig Mautnerf3333272013-04-22 10:55:53 -07001808 // Checked.
1809 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001810 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001811 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001812
Craig Mautnerf3333272013-04-22 10:55:53 -07001813 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001814 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001815 int NS = 0;
1816 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001817 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001818 boolean activityRemoved = false;
1819
Wale Ogunwale7d701172015-03-11 15:36:30 -07001820 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001821 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001822 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1823 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1825 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001826 if (fromTimeout) {
1827 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001828 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001829
1830 // This is a hack to semi-deal with a race condition
1831 // in the client where it can be constructed with a
1832 // newer configuration from when we asked it to launch.
1833 // We'll update with whatever configuration it now says
1834 // it used to launch.
1835 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001836 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001837 }
1838
1839 // We are now idle. If someone is waiting for a thumbnail from
1840 // us, we can now deliver.
1841 r.idle = true;
1842
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001843 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001844 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001845 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001846 }
1847 }
1848
1849 if (allResumedActivitiesIdle()) {
1850 if (r != null) {
1851 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001852 }
1853
1854 if (mLaunchingActivity.isHeld()) {
1855 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1856 if (VALIDATE_WAKE_LOCK_CALLER &&
1857 Binder.getCallingUid() != Process.myUid()) {
1858 throw new IllegalStateException("Calling must be system uid");
1859 }
1860 mLaunchingActivity.release();
1861 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001862 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001863 }
1864
1865 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001866 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1867 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001868 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001869 if ((NF = mFinishingActivities.size()) > 0) {
1870 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001871 mFinishingActivities.clear();
1872 }
1873
Craig Mautnerf3333272013-04-22 10:55:53 -07001874 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001875 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 mStartingUsers.clear();
1877 }
1878
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 // Stop any activities that are scheduled to do so but have been
1880 // waiting for the next one to start.
1881 for (int i = 0; i < NS; i++) {
1882 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001883 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001884 if (stack != null) {
1885 if (r.finishing) {
1886 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1887 } else {
1888 stack.stopActivityLocked(r);
1889 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 }
1891 }
1892
1893 // Finish any activities that are scheduled to do so but have been
1894 // waiting for the next one to start.
1895 for (int i = 0; i < NF; i++) {
1896 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001897 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001898 if (stack != null) {
1899 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1900 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 }
1902
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001903 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001904 // Complete user switch
1905 if (startingUsers != null) {
1906 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001907 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001908 }
1909 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001910 }
1911
1912 mService.trimApplications();
1913 //dump();
1914 //mWindowManager.dump();
1915
Craig Mautnerf3333272013-04-22 10:55:53 -07001916 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001917 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001918 }
1919
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001920 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001921 }
1922
Craig Mautner8e569572013-10-11 17:36:59 -07001923 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001924 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001926 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1927 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1928 final ActivityStack stack = display.getChildAt(stackNdx);
1929 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001930 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001931 }
Craig Mautner19091252013-10-05 00:03:53 -07001932 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001933 }
1934
1935 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001936 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001937 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1938 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1939 final ActivityStack stack = display.getChildAt(stackNdx);
1940 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001941 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001942 }
1943 }
1944
Craig Mautner93529a42013-10-04 15:03:13 -07001945 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001946 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001947 }
1948
Craig Mautner8d341ef2013-03-26 09:03:27 -07001949 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001950 * Update the last used stack id for non-current user (current user's last
1951 * used stack is the focused stack)
1952 */
1953 void updateUserStackLocked(int userId, ActivityStack stack) {
1954 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001955 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001956 }
1957 }
1958
1959 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001960 * @return true if some activity was finished (or would have finished if doit were true).
1961 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001962 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1963 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001964 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001965 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001966 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1967 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1968 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001969 if (stack.finishDisabledPackageActivitiesLocked(
1970 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001971 didSomething = true;
1972 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001973 }
1974 }
1975 return didSomething;
1976 }
1977
Dianne Hackborna413dc02013-07-12 12:02:55 -07001978 void updatePreviousProcessLocked(ActivityRecord r) {
1979 // Now that this process has stopped, we may want to consider
1980 // it to be the previous app to try to keep around in case
1981 // the user wants to return to it.
1982
1983 // First, found out what is currently the foreground app, so that
1984 // we don't blow away the previous app if this activity is being
1985 // hosted by the process that is actually still the foreground.
1986 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001987 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001988 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1989 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1990 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001991 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001992 if (stack.mResumedActivity != null) {
1993 fgApp = stack.mResumedActivity.app;
1994 } else if (stack.mPausingActivity != null) {
1995 fgApp = stack.mPausingActivity.app;
1996 }
1997 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001998 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001999 }
2000 }
2001
2002 // Now set this one as the previous process, only if that really
2003 // makes sense to.
2004 if (r.app != null && fgApp != null && r.app != fgApp
2005 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002006 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002007 mService.mPreviousProcess = r.app;
2008 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2009 }
2010 }
2011
Wale Ogunwaled046a012015-12-24 13:05:59 -08002012 boolean resumeFocusedStackTopActivityLocked() {
2013 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002014 }
2015
Wale Ogunwaled046a012015-12-24 13:05:59 -08002016 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002017 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002018
2019 if (!readyToResume()) {
2020 return false;
2021 }
2022
Wale Ogunwaled046a012015-12-24 13:05:59 -08002023 if (targetStack != null && isFocusedStack(targetStack)) {
2024 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002025 }
chaviw59b98852017-06-13 12:05:44 -07002026
Wale Ogunwale06579d62016-04-30 15:29:06 -07002027 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2028 if (r == null || r.state != RESUMED) {
2029 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002030 } else if (r.state == RESUMED) {
2031 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2032 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002033 }
chaviw59b98852017-06-13 12:05:44 -07002034
Wale Ogunwaled046a012015-12-24 13:05:59 -08002035 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002036 }
2037
Todd Kennedy39bfee52016-02-24 10:28:21 -08002038 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2039 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002040 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2041 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2042 final ActivityStack stack = display.getChildAt(stackNdx);
2043 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002044 }
2045 }
2046 }
2047
Adrian Roos20d7df32016-01-12 18:59:43 +01002048 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2049 TaskRecord finishedTask = null;
2050 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002051 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002052 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2053 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002054 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002055 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002056 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2057 if (stack == focusedStack || finishedTask == null) {
2058 finishedTask = t;
2059 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002060 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002061 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002062 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002063 }
2064
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002065 void finishVoiceTask(IVoiceInteractionSession session) {
2066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002067 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2068 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002069 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002070 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002071 stack.finishVoiceTask(session);
2072 }
2073 }
2074 }
2075
Wale Ogunwale66e16852017-10-19 13:35:52 -07002076 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options,
Andrii Kulianc27916642016-04-12 17:59:27 -07002077 String reason, boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002078 final ActivityStack currentStack = task.getStack();
2079 if (currentStack == null) {
2080 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2081 + task + " to front. Stack is null");
2082 return;
2083 }
2084
Craig Mautneraea74a52014-03-08 14:23:10 -08002085 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2086 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002087 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002088
2089 final ActivityRecord prev = topRunningActivityLocked();
2090
2091 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2092 || (prev != null && prev.isActivityTypeRecents())) {
2093 // Caller wants the home activity moved with it or the previous task is recents in which
2094 // case we always return home from the task we are moving to the front.
2095 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002096 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002097
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002098 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2099 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2100 task.updateOverrideConfiguration(bounds);
2101
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002102 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002103
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002104 if (stack != currentStack) {
2105 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002106 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002107 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002108 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2109 // still need moveTaskToFrontLocked() below for any transition settings.
2110 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002111 if (stack.resizeStackWithLaunchBounds()) {
2112 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002113 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2114 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002115 } else {
2116 // WM resizeTask must be done after the task is moved to the correct stack,
2117 // because Task's setBounds() also updates dim layer's bounds, but that has
2118 // dependency on the stack.
2119 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002120 }
2121 }
2122
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002123 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002124 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002125 r == null ? null : r.appTimeTracker, reason);
2126
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002127 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002128 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002129
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002130 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002131 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002132 }
2133
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002134 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002135 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002136 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002137 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002138 return false;
2139 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002140 return (mService.mSupportsPictureInPicture
2141 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002142 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002143 }
2144
Bryce Leedacefc42017-10-10 12:56:02 -07002145 LaunchingBoundsController getLaunchingBoundsController() {
2146 return mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002147 }
2148
Winson Chung55893332017-02-17 17:13:10 -08002149 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002150 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2151 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2152 if (stack != null) {
2153 return stack;
2154 }
2155 }
2156 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002157 }
2158
Wale Ogunwale68278562017-09-23 17:13:55 -07002159 /** @see ActivityDisplay#getStack(int, int) */
2160 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2161 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2162 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2163 if (stack != null) {
2164 return stack;
2165 }
2166 }
2167 return null;
2168 }
2169
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002170 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002171 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002172 // Preference is given to the activity type for the activity then the task since the type
2173 // once set shouldn't change.
2174 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2175 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2176 activityType = task.getActivityType();
2177 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002178 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2179 return activityType;
2180 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002181 if (options != null) {
2182 activityType = options.getLaunchActivityType();
2183 }
2184 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002185 }
2186
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002187 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2188 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2189 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002190 }
2191
2192 /**
2193 * Returns the right stack to use for launching factoring in all the input parameters.
2194 *
2195 * @param r The activity we are trying to launch. Can be null.
2196 * @param options The activity options used to the launch. Can be null.
2197 * @param candidateTask The possible task the activity might be launched in. Can be null.
2198 *
2199 * @return The stack to use for the launch or INVALID_STACK_ID.
2200 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002201 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2202 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2203 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002204 int taskId = INVALID_TASK_ID;
2205 int displayId = INVALID_DISPLAY;
2206 //Rect bounds = null;
2207
2208 // We give preference to the launch preference in activity options.
2209 if (options != null) {
2210 taskId = options.getLaunchTaskId();
2211 displayId = options.getLaunchDisplayId();
2212 // TODO: Need to work this into the equation...
2213 //bounds = options.getLaunchBounds();
2214 }
2215
2216 // First preference for stack goes to the task Id set in the activity options. Use the stack
2217 // associated with that if possible.
2218 if (taskId != INVALID_TASK_ID) {
2219 // Temporarily set the task id to invalid in case in re-entry.
2220 options.setLaunchTaskId(INVALID_TASK_ID);
2221 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002222 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002223 options.setLaunchTaskId(taskId);
2224 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002225 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002226 }
2227 }
2228
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002229 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002230 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002231
2232 // Next preference for stack goes to the display Id set in the activity options or the
2233 // candidate display.
2234 if (displayId == INVALID_DISPLAY) {
2235 displayId = candidateDisplayId;
2236 }
2237 if (displayId != INVALID_DISPLAY) {
2238 if (r != null) {
2239 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002240 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002241 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002242 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002243 }
2244 }
2245 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2246 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002247 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002248 if (stack != null) {
2249 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002250 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002251 }
2252 }
2253
2254 // Give preference to the stack and display of the input task and activity if they match the
2255 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002256 stack = null;
2257 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002258 if (candidateTask != null) {
2259 stack = candidateTask.getStack();
2260 }
2261 if (stack == null && r != null) {
2262 stack = r.getStack();
2263 }
2264 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002265 display = stack.getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002266 if (display != null) {
2267 final int windowingMode =
2268 display.resolveWindowingMode(r, options, candidateTask, activityType);
2269 if (stack.isCompatible(windowingMode, activityType)) {
2270 return stack;
2271 }
2272 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002273 }
2274
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002275 if (display == null
2276 // TODO: Can be removed once we figure-out how non-standard types should launch
2277 // outside the default display.
2278 || (activityType != ACTIVITY_TYPE_STANDARD
2279 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2280 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002281 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002282
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002283 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002284 }
2285
Andrii Kulian16802aa2016-11-02 12:21:33 -07002286 /**
2287 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2288 * If there is no such stack, new dynamic stack can be created.
2289 * @param displayId Target display.
2290 * @param r Activity that should be launched there.
2291 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2292 */
2293 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002294 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002295 if (activityDisplay == null) {
2296 throw new IllegalArgumentException(
2297 "Display with displayId=" + displayId + " not found.");
2298 }
2299
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002300 if (!r.canBeLaunchedOnDisplay(displayId)) {
2301 return null;
2302 }
2303
Andrii Kulian16802aa2016-11-02 12:21:33 -07002304 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002305 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2306 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002307 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002308 return stack;
2309 }
2310 }
2311
2312 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002313 if (displayId != DEFAULT_DISPLAY) {
2314 return activityDisplay.createStack(
2315 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002316 }
2317
2318 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2319 return null;
2320 }
2321
Wale Ogunwale68278562017-09-23 17:13:55 -07002322 // TODO: Can probably be consolidated into getLaunchStack()...
2323 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2324 switch (stack.getActivityType()) {
2325 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2326 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2327 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002328 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002329 switch (stack.getWindowingMode()) {
2330 case WINDOWING_MODE_FULLSCREEN: return true;
2331 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2332 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2333 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2334 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2335 }
2336
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002337 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002338 return r.canBeLaunchedOnDisplay(displayId);
2339 }
2340 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2341 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002342 }
2343
Andrii Kulian7fc22812016-12-28 13:04:11 -08002344 /**
2345 * Get next focusable stack in the system. This will search across displays and stacks
2346 * in last-focused order for a focusable and visible stack, different from the target stack.
2347 *
2348 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2349 * searching for next candidate.
2350 * @return Next focusable {@link ActivityStack}, null if not found.
2351 */
2352 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2353 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2354
2355 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2356 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002357 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002358 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2359 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2360 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002361 if (stack != currentFocus && stack.isFocusable()
2362 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002363 return stack;
2364 }
2365 }
2366 }
2367
2368 return null;
2369 }
2370
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002371 /**
2372 * Get next valid stack for launching provided activity in the system. This will search across
2373 * displays and stacks in last-focused order for a focusable and visible stack, except those
2374 * that are on a currently focused display.
2375 *
2376 * @param r The activity that is being launched.
2377 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2378 * searching for the next candidate.
2379 * @return Next valid {@link ActivityStack}, null if not found.
2380 */
2381 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2382 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2383 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2384 final int displayId = mTmpOrderedDisplayIds.get(i);
2385 if (displayId == currentFocus) {
2386 continue;
2387 }
2388 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2389 if (stack != null) {
2390 return stack;
2391 }
2392 }
2393 return null;
2394 }
2395
Craig Mautneree2e45a2014-06-27 12:10:03 -07002396 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002397 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002398 }
2399
2400 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002401 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2402 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2403 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002404 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002405 final ArrayList<ActivityRecord> activities = task.mActivities;
2406 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2407 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002408 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002409 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002410 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002411 }
2412 }
2413 }
2414 }
2415 return null;
2416 }
2417
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002418 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2419 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2420 boolean deferResume) {
2421
2422 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002423 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002424 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002425 return;
2426 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002427
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002428 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002429 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002430 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002431 // If the docked stack exists, don't resize non-floating stacks independently of the
2432 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002433 return;
2434 }
2435
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002436 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002437 mWindowManager.deferSurfaceLayout();
2438 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002439 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002440 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2441 // null bounds = fullscreen windowing mode...at least for now.
2442 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2443 } else if (splitScreenActive) {
2444 // If we are in split-screen mode and this stack support split-screen, then
2445 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2446 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2447 }
2448 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002449 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002450 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002451 stack.ensureVisibleActivitiesConfigurationLocked(
2452 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002453 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002454 } finally {
2455 mWindowManager.continueSurfaceLayout();
2456 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002457 }
2458 }
2459
Wale Ogunwale68278562017-09-23 17:13:55 -07002460 private void deferUpdateBounds(int activityType) {
2461 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002462 if (stack != null) {
2463 stack.deferUpdateBounds();
2464 }
2465 }
2466
Wale Ogunwale68278562017-09-23 17:13:55 -07002467 private void continueUpdateBounds(int activityType) {
2468 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002469 if (stack != null) {
2470 stack.continueUpdateBounds();
2471 }
2472 }
2473
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002474 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002475 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002476 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2477 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002478 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002479 if (task != null) {
2480 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002481 }
2482 }
2483 mResizingTasksDuringAnimation.clear();
2484 }
2485
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002486 /**
2487 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002488 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002489 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002490 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002491 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002492
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002493 mWindowManager.deferSurfaceLayout();
2494 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002495 final int windowingMode = fromStack.getWindowingMode();
2496 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2497 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2498
2499 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002500 // Tell the display we are exiting split-screen mode.
2501 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002502 // We are moving all tasks from the docked stack to the fullscreen stack,
2503 // which is dismissing the docked stack, so resize all other stacks to
2504 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002505 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2506 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002507 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2508 continue;
2509 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002510 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002511 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002512 }
2513
2514 // Also disable docked stack resizing since we have manually adjusted the
2515 // size of other stacks above and we don't want to trigger a docked stack
2516 // resize when we remove task from it below and it is detached from the
2517 // display because it no longer contains any tasks.
2518 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002519 } else if (inPinnedWindowingMode && onTop) {
2520 // Log if we are expanding the PiP to fullscreen
2521 MetricsLogger.action(mService.mContext,
2522 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002523 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002524
Winson Chung5af42fc2017-03-24 17:11:33 -07002525 // If we are moving from the pinned stack, then the animation takes care of updating
2526 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002527 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002528 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002529
Wale Ogunwale388945c2017-10-04 12:13:46 -07002530 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002531 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002532 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002533 for (int i = 0; i < size; ++i) {
2534 final TaskRecord task = tasks.get(i);
2535 final ActivityStack toStack = toDisplay.getOrCreateStack(
2536 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002537
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002538 if (onTop) {
2539 final int returnToType =
2540 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002541 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002542 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002543 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002544 isTopTask /* animate */, DEFER_RESUME,
2545 schedulePictureInPictureModeChange,
2546 "moveTasksToFullscreenStack - onTop");
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002547 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002548 // Position the tasks in the fullscreen stack in order at the bottom of the
2549 // stack. Also defer resume until all the tasks have been moved to the
2550 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002551 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002552 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2553 schedulePictureInPictureModeChange,
2554 "moveTasksToFullscreenStack - NOT_onTop");
2555 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002556 }
2557 }
Winson Chung74666102017-02-22 17:49:24 -08002558
2559 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2560 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002561 } finally {
2562 mAllowDockedStackResize = true;
2563 mWindowManager.continueSurfaceLayout();
2564 }
2565 }
2566
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002567 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002568 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2569 }
2570
2571 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2572 mWindowManager.inSurfaceTransaction(() ->
2573 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002574 }
2575
Jorim Jaggidc249c42015-12-15 14:57:31 -08002576 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002577 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2578 boolean preserveWindows) {
2579 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2580 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2581 false /* deferResume */);
2582 }
2583
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002584 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002585 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2586 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002587
2588 if (!mAllowDockedStackResize) {
2589 // Docked stack resize currently disabled.
2590 return;
2591 }
2592
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002593 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002594 if (stack == null) {
2595 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2596 return;
2597 }
2598
2599 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2600 mWindowManager.deferSurfaceLayout();
2601 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002602 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2603 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002604 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002605 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002606
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002607 // TODO: Checking for isAttached might not be needed as if the user passes in null
2608 // dockedBounds then they want the docked stack to be dismissed.
2609 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2610 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002611 // In this case we make all other static stacks fullscreen and move all
2612 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002613 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002614
2615 // stack shouldn't contain anymore activities, so nothing to resume.
2616 r = null;
2617 } else {
2618 // Docked stacks occupy a dedicated region on screen so the size of all other
2619 // static stacks need to be adjusted so they don't overlap with the docked stack.
2620 // We get the bounds to use from window manager which has been adjusted for any
2621 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002622 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002623 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002624 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2625 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002626 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002627 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002628 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002629 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002630 continue;
2631 }
2632 // Need to set windowing mode here before we try to get the dock bounds.
2633 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2634 current.getStackDockedModeBounds(
2635 tempOtherTaskBounds /* currentTempTaskBounds */,
2636 tempRect /* outStackBounds */,
2637 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2638
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002639 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002640 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2641 tempOtherTaskInsetBounds, preserveWindows,
2642 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002643 }
2644 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002645 if (!deferResume) {
2646 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2647 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002648 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002649 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002650 mWindowManager.continueSurfaceLayout();
2651 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2652 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002653 }
2654
Robert Carr0d00c2e2016-02-29 17:45:02 -08002655 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002656 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002657 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002658 if (stack == null) {
2659 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2660 return;
2661 }
Winson Chung19953ca2017-04-11 11:19:23 -07002662
2663 // It is possible for the bounds animation from the WM to call this but be delayed by
2664 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2665 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2666 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2667 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002668 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002669 return;
2670 }
2671
Robert Carr0d00c2e2016-02-29 17:45:02 -08002672 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2673 mWindowManager.deferSurfaceLayout();
2674 try {
2675 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002676 Rect insetBounds = null;
2677 if (tempPinnedTaskBounds != null) {
2678 // We always use 0,0 as the position for the inset rect because
2679 // if we are getting insets at all in the pinned stack it must mean
2680 // we are headed for fullscreen.
2681 insetBounds = tempRect;
2682 insetBounds.top = 0;
2683 insetBounds.left = 0;
2684 insetBounds.right = tempPinnedTaskBounds.width();
2685 insetBounds.bottom = tempPinnedTaskBounds.height();
2686 }
2687 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002688 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002689 } finally {
2690 mWindowManager.continueSurfaceLayout();
2691 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2692 }
2693 }
2694
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002695 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002696 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002697 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002698 /**
2699 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2700 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2701 * that the client receives onStop() before it is reparented. We do this by detaching
2702 * the stack from the display so that it will be considered invisible when
2703 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2704 * invisible as well and added to the stopping list. After which we process the
2705 * stopping list by handling the idle.
2706 */
2707 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2708 pinnedStack.mForceHidden = true;
2709 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2710 pinnedStack.mForceHidden = false;
2711 activityIdleInternalLocked(null, false /* fromTimeout */,
2712 true /* processPausingActivites */, null /* configuration */);
2713
2714 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002715 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002716 } else {
2717 for (int i = tasks.size() - 1; i >= 0; i--) {
2718 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2719 REMOVE_FROM_RECENTS);
2720 }
2721 }
2722 }
2723
2724 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002725 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002726 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2727 * instead moved back onto the fullscreen stack.
2728 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002729 void removeStack(ActivityStack stack) {
2730 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002731 }
2732
Wale Ogunwale68278562017-09-23 17:13:55 -07002733 /**
2734 * Removes stacks in the input windowing modes from the system if they are of activity type
2735 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2736 */
2737 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002738 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002739 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2740 }
2741 }
2742
2743 void removeStacksWithActivityTypes(int... activityTypes) {
2744 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2745 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002746 }
Robert Carr6914f082017-03-20 19:04:30 -07002747 }
2748
2749 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002750 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2751 */
2752 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2753 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2754 }
2755
2756 /**
Winson Chung010927a2016-12-15 16:12:35 -08002757 * Removes the task with the specified task id.
2758 *
2759 * @param taskId Identifier of the task to be removed.
2760 * @param killProcess Kill any process associated with the task if possible.
2761 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002762 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2763 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002764 * @return Returns true if the given task was found and removed.
2765 */
Winson Chung6954fc92017-03-24 16:22:12 -07002766 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2767 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002768 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002769 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002770 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002771 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2772 if (tr.isPersistable) {
2773 mService.notifyTaskPersisterLocked(null, true);
2774 }
2775 return true;
2776 }
2777 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2778 return false;
2779 }
2780
2781 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2782 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002783 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002784 }
2785 ComponentName component = tr.getBaseIntent().getComponent();
2786 if (component == null) {
2787 Slog.w(TAG, "No component for base intent of task: " + tr);
2788 return;
2789 }
2790
2791 // Find any running services associated with this app and stop if needed.
2792 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2793
2794 if (!killProcess) {
2795 return;
2796 }
2797
2798 // Determine if the process(es) for this task should be killed.
2799 final String pkg = component.getPackageName();
2800 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2801 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2802 for (int i = 0; i < pmap.size(); i++) {
2803
2804 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2805 for (int j = 0; j < uids.size(); j++) {
2806 ProcessRecord proc = uids.valueAt(j);
2807 if (proc.userId != tr.userId) {
2808 // Don't kill process for a different user.
2809 continue;
2810 }
2811 if (proc == mService.mHomeProcess) {
2812 // Don't kill the home process along with tasks from the same package.
2813 continue;
2814 }
2815 if (!proc.pkgList.containsKey(pkg)) {
2816 // Don't kill process that is not associated with this task.
2817 continue;
2818 }
2819
2820 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002821 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002822 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2823 // Don't kill process(es) that has an activity in a different task that is
2824 // also in recents.
2825 return;
2826 }
2827 }
2828
2829 if (proc.foregroundServices) {
2830 // Don't kill process(es) with foreground service.
2831 return;
2832 }
2833
2834 // Add process to kill list.
2835 procsToKill.add(proc);
2836 }
2837 }
2838
2839 // Kill the running processes.
2840 for (int i = 0; i < procsToKill.size(); i++) {
2841 ProcessRecord pr = procsToKill.get(i);
2842 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2843 && pr.curReceivers.isEmpty()) {
2844 pr.kill("remove task", true);
2845 } else {
2846 // We delay killing processes that are not in the background or running a receiver.
2847 pr.waitingToKill = "remove task";
2848 }
2849 }
2850 }
2851
Craig Mautner4a1cb222013-12-04 16:14:06 -08002852 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002853 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002854 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002855 break;
2856 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002857 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002858 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002859 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002860 }
2861
Chong Zhang5dcb2752015-08-18 13:50:26 -07002862 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002863 * Called to restore the state of the task into the stack that it's supposed to go into.
2864 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002865 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002866 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002867 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002868 * @return true if the task has been restored successfully.
2869 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002870 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2871 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002872 final ActivityStack currentStack = task.getStack();
2873 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002874 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002875 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002876 // Nothing else to do since it is already restored in the right stack.
2877 return true;
2878 }
2879 // Remove current stack association, so we can re-associate the task with the
2880 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002881 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002882 }
2883
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002884 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002885 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002886 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002887 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2888 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002889 final ArrayList<ActivityRecord> activities = task.mActivities;
2890 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002891 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002892 }
2893 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002894 }
2895
Winson Chung1dbc8112017-09-28 18:05:31 -07002896 @Override
2897 public void onRecentTaskAdded(TaskRecord task) {
2898 task.touchActiveTime();
2899 }
2900
2901 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07002902 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung14cfbb42017-10-20 13:54:39 -07002903 // TODO: Trim active task once b/68045330 is fixed
Winson Chung1dbc8112017-09-28 18:05:31 -07002904 task.removedFromRecents();
2905 }
2906
Wale Ogunwale040b4702015-08-06 18:10:50 -07002907 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002908 * Move stack with all its existing content to specified display.
2909 * @param stackId Id of stack to move.
2910 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002911 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002912 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002913 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002914 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002915 if (activityDisplay == null) {
2916 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2917 + displayId);
2918 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002919 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002920 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002921 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2922 + stackId);
2923 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002924
2925 final ActivityDisplay currentDisplay = stack.getDisplay();
2926 if (currentDisplay == null) {
2927 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
2928 + " is not attached to any display.");
2929 }
2930
2931 if (currentDisplay.mDisplayId == displayId) {
2932 throw new IllegalArgumentException("Trying to move stack=" + stack
2933 + " to its current displayId=" + displayId);
2934 }
2935
2936 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002937 // TODO(multi-display): resize stacks properly if moved from split-screen.
2938 }
2939
2940 /**
Winson Chung74666102017-02-22 17:49:24 -08002941 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2942 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002943 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002944 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002945 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002946 final int stackId = stack.mStackId;
2947 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07002948
Winson Chung74666102017-02-22 17:49:24 -08002949 // Check that we aren't reparenting to the same stack that the task is already in
2950 if (prevStack != null && prevStack.mStackId == stackId) {
2951 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2952 + " already in stackId=" + stackId);
2953 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002954 }
2955
Winson Chung74666102017-02-22 17:49:24 -08002956 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2957 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002958 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08002959 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002960 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08002961 }
2962
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002963 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2964 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002965 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002966 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2967 + " reparent task=" + task + " to stackId=" + stackId);
2968 }
2969
Winson Chung74666102017-02-22 17:49:24 -08002970 // Ensure that we aren't trying to move into a freeform stack without freeform
2971 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002972 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
2973 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002974 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2975 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002976 }
2977
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002978 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
2979 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002980 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002981 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
2982 + " Moving to a fullscreen stack instead.");
2983 if (prevStack != null) {
2984 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002985 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002986 stack = stack.getDisplay().createStack(
2987 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002988 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002989 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002990 }
2991
Winson Chung08f81892017-03-02 15:40:51 -08002992 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002993 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002994 if (stack == null) {
2995 throw new IllegalArgumentException(
2996 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2997 }
2998
2999 final ActivityRecord r = stack.topRunningActivityLocked();
3000 if (r == null) {
3001 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3002 + " in stack=" + stack);
3003 return false;
3004 }
3005
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003006 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003007 Slog.w(TAG,
3008 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003009 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003010 return false;
3011 }
3012
Winson Chung3a682872017-04-10 14:38:05 -07003013 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003014 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003015 return true;
3016 }
3017
Winson Chung8bca9e42017-04-16 15:59:43 -07003018 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003019 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003020
Wale Ogunwale480dca02016-02-06 13:58:29 -08003021 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003022
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003023 final ActivityDisplay display = r.getStack().getDisplay();
3024 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003025
Bryce Lee04ab3462017-04-10 15:06:33 -07003026 // This will clear the pinned stack by moving an existing task to the full screen stack,
3027 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003028 if (stack != null) {
3029 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3030 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003031
Wale Ogunwale1666e312016-12-16 11:27:18 -08003032 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003033 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003034
Wale Ogunwale480dca02016-02-06 13:58:29 -08003035 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003036 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003037 // Resize the pinned stack to match the current size of the task the activity we are
3038 // going to be moving is currently contained in. We do this to have the right starting
3039 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003040 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003041 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003042 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003043
3044 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003045 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003046 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3047 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003048 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003049 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003050 // reveal/leave the other activities in their original task.
3051
3052 // Currently, we don't support reparenting activities across tasks in two different
3053 // stacks, so instead, just create a new task in the same stack, reparent the
3054 // activity into that task, and then reparent the whole task to the new stack. This
3055 // ensures that all the necessary work to migrate states in the old and new stacks
3056 // is also done.
3057 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003058 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003059 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3060
Winson Chung5af42fc2017-03-24 17:11:33 -07003061 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003062 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003063 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003064 }
Winson Chungc2baac02017-01-11 13:34:47 -08003065
3066 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3067 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003068 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003069 } finally {
3070 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003071 }
3072
Winson Chunge7ba6862017-05-24 12:13:33 -07003073 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3074 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3075 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003076 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3077
Winson Chunge7ba6862017-05-24 12:13:33 -07003078 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3079 true /* fromFullscreen */);
3080
3081 // Update the visibility of all activities after the they have been reparented to the new
3082 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3083 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3084 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003085 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003086 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003087
Wale Ogunwale89be5762017-10-04 13:27:49 -07003088 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003089 }
3090
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003091 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003092 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3093 if (r == null || !r.isFocusable()) {
3094 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3095 "moveActivityStackToFront: unfocusable r=" + r);
3096 return false;
3097 }
3098
Bryce Leeaf691c02017-03-20 14:20:22 -07003099 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003100 final ActivityStack stack = r.getStack();
3101 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003102 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3103 + r + " task=" + task);
3104 return false;
3105 }
3106
Chong Zhang6cda19c2016-06-14 19:07:56 -07003107 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3108 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3109 "moveActivityStackToFront: already on top, r=" + r);
3110 return false;
3111 }
3112
3113 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3114 "moveActivityStackToFront: r=" + r);
3115
3116 stack.moveToFront(reason, task);
3117 return true;
3118 }
3119
David Stevensc6b91c62017-02-08 14:23:58 -08003120 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003121 mTmpFindTaskResult.r = null;
3122 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003123 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003124 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003125 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003126 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3127 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3128 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003129 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003130 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3131 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003132 continue;
3133 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003134 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003135 // It is possible to have tasks in multiple stacks with the same root affinity, so
3136 // we should keep looking after finding an affinity match to see if there is a
3137 // better match in another stack. Also, task affinity isn't a good enough reason
3138 // to target a display which isn't the source of the intent, so skip any affinity
3139 // matches not on the specified display.
3140 if (mTmpFindTaskResult.r != null) {
3141 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3142 return mTmpFindTaskResult.r;
3143 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003144 // Note: since the traversing through the stacks is top down, the floating
3145 // tasks should always have lower priority than any affinity-matching tasks
3146 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003147 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003148 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3149 Slog.d(TAG_TASKS, "Skipping match on different display "
3150 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003151 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003152 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003153 }
3154 }
Winson Chung5b895b72017-05-01 13:46:25 -07003155
David Stevensc6b91c62017-02-08 14:23:58 -08003156 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3157 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003158 }
3159
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003160 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003161 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003162 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003163 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3164 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3165 final ActivityStack stack = display.getChildAt(stackNdx);
3166 final ActivityRecord ar = stack.findActivityLocked(
3167 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003168 if (ar != null) {
3169 return ar;
3170 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003171 }
3172 }
3173 return null;
3174 }
3175
David Stevens9440dc82017-03-16 19:00:20 -07003176 boolean hasAwakeDisplay() {
3177 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3178 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3179 if (!display.shouldSleep()) {
3180 return true;
3181 }
3182 }
3183 return false;
3184 }
3185
Craig Mautner8d341ef2013-03-26 09:03:27 -07003186 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003187 scheduleSleepTimeout();
3188 if (!mGoingToSleep.isHeld()) {
3189 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003190 if (mLaunchingActivity.isHeld()) {
3191 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3192 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003194 mLaunchingActivity.release();
3195 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003196 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003197 }
David Stevens9440dc82017-03-16 19:00:20 -07003198
3199 applySleepTokensLocked(false /* applyToStacks */);
3200
3201 checkReadyForSleepLocked(true /* allowDelay */);
3202 }
3203
3204 void prepareForShutdownLocked() {
3205 for (int i = 0; i < mActivityDisplays.size(); i++) {
3206 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3207 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003208 }
3209
3210 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003211 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003212
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003213 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003214 final long endTime = System.currentTimeMillis() + timeout;
3215 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003216 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003217 long timeRemaining = endTime - System.currentTimeMillis();
3218 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003219 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003220 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003221 } catch (InterruptedException e) {
3222 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003223 } else {
3224 Slog.w(TAG, "Activity manager shutdown timed out");
3225 timedout = true;
3226 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003228 } else {
3229 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003230 }
3231 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003232
3233 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003234 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003235
Craig Mautner8d341ef2013-03-26 09:03:27 -07003236 return timedout;
3237 }
3238
3239 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003240 removeSleepTimeouts();
3241 if (mGoingToSleep.isHeld()) {
3242 mGoingToSleep.release();
3243 }
David Stevens9440dc82017-03-16 19:00:20 -07003244 }
3245
3246 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003247 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003248 // Set the sleeping state of the display.
3249 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3250 final boolean displayShouldSleep = display.shouldSleep();
3251 if (displayShouldSleep == display.isSleeping()) {
3252 continue;
3253 }
3254 display.setIsSleeping(displayShouldSleep);
3255
3256 if (!applyToStacks) {
3257 continue;
3258 }
3259
3260 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003261 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3262 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003263 if (displayShouldSleep) {
3264 stack.goToSleepIfPossible(false /* shuttingDown */);
3265 } else {
3266 stack.awakeFromSleepingLocked();
3267 if (isFocusedStack(stack)) {
3268 resumeFocusedStackTopActivityLocked();
3269 }
3270 }
3271 }
3272
3273 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3274 continue;
3275 }
3276 // The display is awake now, so clean up the going to sleep list.
3277 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3278 final ActivityRecord r = it.next();
3279 if (r.getDisplayId() == display.mDisplayId) {
3280 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003281 }
Craig Mautner5314a402013-09-26 12:40:16 -07003282 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003283 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003284 }
3285
3286 void activitySleptLocked(ActivityRecord r) {
3287 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003288 final ActivityStack s = r.getStack();
3289 if (s != null) {
3290 s.checkReadyForSleep();
3291 } else {
3292 checkReadyForSleepLocked(true);
3293 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003294 }
3295
David Stevens9440dc82017-03-16 19:00:20 -07003296 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003297 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003298 // Do not care.
3299 return;
3300 }
3301
David Stevens18abd0e2017-08-17 14:55:47 -07003302 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3303 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003304 }
3305
Wei Wang65c7a152016-06-02 18:51:22 -07003306 // Send launch end powerhint before going sleep
3307 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3308
Craig Mautner0eea92c2013-05-16 13:35:39 -07003309 removeSleepTimeouts();
3310
3311 if (mGoingToSleep.isHeld()) {
3312 mGoingToSleep.release();
3313 }
3314 if (mService.mShuttingDown) {
3315 mService.notifyAll();
3316 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003317 }
3318
David Stevens18abd0e2017-08-17 14:55:47 -07003319 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3320 // successfully put to sleep.
3321 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3322 boolean allSleep = true;
3323 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003324 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3325 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3326 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003327 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003328 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003329 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003330 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003331 }
3332 }
3333 }
3334 return allSleep;
3335 }
3336
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003337 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003338 // A resumed activity cannot be stopping. remove from list
3339 mStoppingActivities.remove(r);
3340
Andrii Kulian02b7a832016-10-06 23:11:56 -07003341 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003342 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003343 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003344 }
3345 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003346 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003347 mWindowManager.executeAppTransition();
3348 return true;
3349 }
3350 return false;
3351 }
3352
Craig Mautner8d341ef2013-03-26 09:03:27 -07003353 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003354 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003355 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3356 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3357 final ActivityStack stack = display.getChildAt(stackNdx);
3358 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003359 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 }
3361 }
3362
Craig Mautnerbb742462014-07-07 15:28:55 -07003363 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003364 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003365 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003366 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003367
3368 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003369 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003370 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003371 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003372
3373 // When launching tasks behind, update the last active time of the top task after the new
3374 // task has been shown briefly
3375 final ActivityRecord top = stack.topActivity();
3376 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003377 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003378 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003379 }
3380
3381 void scheduleLaunchTaskBehindComplete(IBinder token) {
3382 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3383 }
3384
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003385 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3386 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003387 mKeyguardController.beginActivityVisibilityUpdate();
3388 try {
3389 // First the front stacks. In case any are not fullscreen and are in front of home.
3390 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003391 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3392 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3393 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003394 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3395 }
Craig Mautner580ea812013-04-25 12:58:38 -07003396 }
Jorim Jaggife762342016-10-13 14:33:27 +02003397 } finally {
3398 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003399 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003400 }
3401
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003402 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3403 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003404 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3405 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3406 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003407 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3408 }
3409 }
3410 }
3411
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003412 void invalidateTaskLayers() {
3413 mTaskLayersChanged = true;
3414 }
3415
3416 void rankTaskLayersIfNeeded() {
3417 if (!mTaskLayersChanged) {
3418 return;
3419 }
3420 mTaskLayersChanged = false;
3421 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003422 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003423 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003424 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3425 final ActivityStack stack = display.getChildAt(stackNdx);
3426 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003427 }
3428 }
3429 }
3430
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003431 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3432 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003433 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3434 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3435 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003436 stack.clearOtherAppTimeTrackers(except);
3437 }
3438 }
3439 }
3440
Craig Mautner8d341ef2013-03-26 09:03:27 -07003441 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003443 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3444 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3445 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003446 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003447 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003448 }
3449 }
3450
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003451 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3452 // Examine all activities currently running in the process.
3453 TaskRecord firstTask = null;
3454 // Tasks is non-null only if two or more tasks are found.
3455 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003456 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3457 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003458 ActivityRecord r = app.activities.get(i);
3459 // First, if we find an activity that is in the process of being destroyed,
3460 // then we just aren't going to do anything for now; we want things to settle
3461 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003462 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003463 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003464 return;
3465 }
3466 // Don't consider any activies that are currently not in a state where they
3467 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003468 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3469 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003470 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003471 continue;
3472 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003473
3474 final TaskRecord task = r.getTask();
3475 if (task != null) {
3476 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003477 + " from " + r);
3478 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003479 firstTask = task;
3480 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003481 if (tasks == null) {
3482 tasks = new ArraySet<>();
3483 tasks.add(firstTask);
3484 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003485 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003486 }
3487 }
3488 }
3489 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003490 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003491 return;
3492 }
3493 // If we have activities in multiple tasks that are in a position to be destroyed,
3494 // let's iterate through the tasks and release the oldest one.
3495 final int numDisplays = mActivityDisplays.size();
3496 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003497 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3498 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003499 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003500 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3501 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003502 // Try to release activities in this stack; if we manage to, we are done.
3503 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3504 return;
3505 }
3506 }
3507 }
3508 }
3509
Amith Yamasani37a40c22015-06-17 13:25:42 -07003510 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003511 final int focusStackId = mFocusedStack.getStackId();
3512 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003513 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003514 if (dockedStack != null) {
3515 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3516 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003517 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3518 // also cause all tasks to be moved to the fullscreen stack at a position that is
3519 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003520 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003521
3522 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003523 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003524 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003525
Craig Mautner858d8a62013-04-23 17:08:34 -07003526 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003527 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003528 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3529 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3530 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003532 TaskRecord task = stack.topTask();
3533 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003534 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003535 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003536 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003537 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003538
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003539 ActivityStack stack = getStack(restoreStackId);
3540 if (stack == null) {
3541 stack = mHomeStack;
3542 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003543 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003544 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003545 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 } else {
3547 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003548 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003549 }
Craig Mautner93529a42013-10-04 15:03:13 -07003550 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003551 }
3552
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003553 /** Checks whether the userid is a profile of the current user. */
3554 boolean isCurrentProfileLocked(int userId) {
3555 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003556 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003557 }
3558
Bryce Leeb7c9b802017-05-02 14:20:24 -07003559 /**
3560 * Returns whether a stopping activity is present that should be stopped after visible, rather
3561 * than idle.
3562 * @return {@code true} if such activity is present. {@code false} otherwise.
3563 */
3564 boolean isStoppingNoHistoryActivity() {
3565 // Activities that are marked as nohistory should be stopped immediately after the resumed
3566 // activity has become visible.
3567 for (ActivityRecord record : mStoppingActivities) {
3568 if (record.isNoHistory()) {
3569 return true;
3570 }
3571 }
3572
3573 return false;
3574 }
3575
Winson Chung4dabf232017-01-25 13:25:22 -08003576 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3577 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003578 ArrayList<ActivityRecord> stops = null;
3579
3580 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003581 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3582 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003583 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003584 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003585 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3586 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003587 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003588 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003589 if (s.finishing) {
3590 // If this activity is finishing, it is sitting on top of
3591 // everyone else but we now know it is no longer needed...
3592 // so get rid of it. Otherwise, we need to go through the
3593 // normal flow and hide it once we determine that it is
3594 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003595 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003596 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003597 }
3598 }
David Stevens9440dc82017-03-16 19:00:20 -07003599 if (remove) {
3600 final ActivityStack stack = s.getStack();
3601 final boolean shouldSleepOrShutDown = stack != null
3602 ? stack.shouldSleepOrShutDownActivities()
3603 : mService.isSleepingOrShuttingDownLocked();
3604 if (!waitingVisible || shouldSleepOrShutDown) {
3605 if (!processPausingActivities && s.state == PAUSING) {
3606 // Defer processing pausing activities in this iteration and reschedule
3607 // a delayed idle to reprocess it again
3608 removeTimeoutsForActivityLocked(idleActivity);
3609 scheduleIdleTimeoutLocked(idleActivity);
3610 continue;
3611 }
Winson Chung4dabf232017-01-25 13:25:22 -08003612
David Stevens9440dc82017-03-16 19:00:20 -07003613 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3614 if (stops == null) {
3615 stops = new ArrayList<>();
3616 }
3617 stops.add(s);
3618 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003619 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003620 }
3621 }
3622
3623 return stops;
3624 }
3625
Craig Mautnercf910b02013-04-23 11:23:27 -07003626 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003628 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3629 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3630 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003631 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003632 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003633 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003634 if (r == null) Slog.e(TAG,
3635 "validateTop...: null top activity, stack=" + stack);
3636 else {
3637 final ActivityRecord pausing = stack.mPausingActivity;
3638 if (pausing != null && pausing == r) Slog.e(TAG,
3639 "validateTop...: top stack has pausing activity r=" + r
3640 + " state=" + state);
3641 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3642 "validateTop...: activity in front not resumed r=" + r
3643 + " state=" + state);
3644 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003645 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003646 final ActivityRecord resumed = stack.mResumedActivity;
3647 if (resumed != null && resumed == r) Slog.e(TAG,
3648 "validateTop...: back stack has resumed activity r=" + r
3649 + " state=" + state);
3650 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3651 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003652 }
3653 }
3654 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003655 }
3656
Craig Mautner27084302013-03-25 08:05:25 -07003657 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003658 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003659 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003660 pw.print(prefix);
3661 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003662 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003663 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3664 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003665 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003666 }
Bryce Lee4a194382017-04-04 14:32:48 -07003667 if (!mWaitingForActivityVisible.isEmpty()) {
3668 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3669 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3670 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3671 }
3672 }
3673
Jorim Jaggi8d786932016-10-26 19:08:36 -07003674 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003675 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003676 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003677
Yi Jin129fc6c2017-09-28 15:48:38 -07003678 public void writeToProto(ProtoOutputStream proto) {
Adrian Roos4921ccf2017-09-28 16:54:06 +02003679 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003680 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3681 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3682 activityDisplay.writeToProto(proto, DISPLAYS);
3683 }
3684 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3685 if (mFocusedStack != null) {
3686 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3687 ActivityRecord focusedActivity = getResumedActivityLocked();
3688 if (focusedActivity != null) {
3689 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3690 }
3691 } else {
3692 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3693 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003694 }
3695
Winson43d1f262016-06-14 16:05:55 -07003696 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003697 * Dump all connected displays' configurations.
3698 * @param prefix Prefix to apply to each line of the dump.
3699 */
3700 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3701 pw.print(prefix); pw.println("Display override configurations:");
3702 final int displayCount = mActivityDisplays.size();
3703 for (int i = 0; i < displayCount; i++) {
3704 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3705 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3706 pw.println(activityDisplay.getOverrideConfiguration());
3707 }
3708 }
3709
3710 /**
Winson43d1f262016-06-14 16:05:55 -07003711 * Dumps the activities matching the given {@param name} in the either the focused stack
3712 * or all visible stacks if {@param dumpVisibleStacks} is true.
3713 */
Winson Chung6998bc42017-02-28 17:07:05 -08003714 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3715 boolean dumpFocusedStackOnly) {
3716 if (dumpFocusedStackOnly) {
3717 return mFocusedStack.getDumpActivitiesLocked(name);
3718 } else {
Winson43d1f262016-06-14 16:05:55 -07003719 ArrayList<ActivityRecord> activities = new ArrayList<>();
3720 int numDisplays = mActivityDisplays.size();
3721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003722 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3723 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3724 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003725 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003726 activities.addAll(stack.getDumpActivitiesLocked(name));
3727 }
3728 }
3729 }
3730 return activities;
Winson43d1f262016-06-14 16:05:55 -07003731 }
Craig Mautner20e72272013-04-01 13:45:53 -07003732 }
3733
Dianne Hackborn390517b2013-05-30 15:03:32 -07003734 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3735 boolean needSep, String prefix) {
3736 if (activity != null) {
3737 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3738 if (needSep) {
3739 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003740 }
3741 pw.print(prefix);
3742 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003743 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003744 }
3745 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003746 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003747 }
3748
Craig Mautner8d341ef2013-03-26 09:03:27 -07003749 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3750 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003751 boolean printed = false;
3752 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003753 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3754 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003755 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003756 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003757 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3758 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3759 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003760 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003761 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003762 + ": type=" + activityTypeToString(stack.getActivityType())
3763 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003764 pw.println(" mFullscreen=" + stack.mFullscreen);
3765 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3766 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003767
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003768 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3769 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003770
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3772 !dumpAll, false, dumpPackage, true,
3773 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003774
Craig Mautner4a1cb222013-12-04 16:14:06 -08003775 needSep = printed;
3776 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3777 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003778 if (pr) {
3779 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003781 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3783 " mResumedActivity: ");
3784 if (pr) {
3785 printed = true;
3786 needSep = false;
3787 }
3788 if (dumpAll) {
3789 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3790 " mLastPausedActivity: ");
3791 if (pr) {
3792 printed = true;
3793 needSep = true;
3794 }
3795 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3796 needSep, " mLastNoHistoryActivity: ");
3797 }
3798 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003799 }
3800 }
3801
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003802 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3803 false, dumpPackage, true, " Activities waiting to finish:", null);
3804 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3805 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003806 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3807 false, !dumpAll, false, dumpPackage, true,
3808 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003809 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3810 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003811
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003812 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003813 }
3814
Dianne Hackborn390517b2013-05-30 15:03:32 -07003815 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003816 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003817 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003818 String innerPrefix = null;
3819 String[] args = null;
3820 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003821 for (int i=list.size()-1; i>=0; i--) {
3822 final ActivityRecord r = list.get(i);
3823 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3824 continue;
3825 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003826 if (innerPrefix == null) {
3827 innerPrefix = prefix + " ";
3828 args = new String[0];
3829 }
3830 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003831 final boolean full = !brief && (complete || !r.isInHistory());
3832 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003833 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003834 needNL = false;
3835 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003836 if (header != null) {
3837 pw.println(header);
3838 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003839 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003840 if (lastTask != r.getTask()) {
3841 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003842 pw.print(prefix);
3843 pw.print(full ? "* " : " ");
3844 pw.println(lastTask);
3845 if (full) {
3846 lastTask.dump(pw, prefix + " ");
3847 } else if (complete) {
3848 // Complete + brief == give a summary. Isn't that obvious?!?
3849 if (lastTask.intent != null) {
3850 pw.print(prefix); pw.print(" ");
3851 pw.println(lastTask.intent.toInsecureStringWithClip());
3852 }
3853 }
3854 }
3855 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3856 pw.print(" #"); pw.print(i); pw.print(": ");
3857 pw.println(r);
3858 if (full) {
3859 r.dump(pw, innerPrefix);
3860 } else if (complete) {
3861 // Complete + brief == give a summary. Isn't that obvious?!?
3862 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3863 if (r.app != null) {
3864 pw.print(innerPrefix); pw.println(r.app);
3865 }
3866 }
3867 if (client && r.app != null && r.app.thread != null) {
3868 // flush anything that is already in the PrintWriter since the thread is going
3869 // to write to the file descriptor directly
3870 pw.flush();
3871 try {
3872 TransferPipe tp = new TransferPipe();
3873 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003874 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003875 // Short timeout, since blocking here can
3876 // deadlock with the application.
3877 tp.go(fd, 2000);
3878 } finally {
3879 tp.kill();
3880 }
3881 } catch (IOException e) {
3882 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3883 } catch (RemoteException e) {
3884 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3885 }
3886 needNL = true;
3887 }
3888 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003889 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003890 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003891
Craig Mautnerf3333272013-04-22 10:55:53 -07003892 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003893 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3894 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003895 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3896 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003897 }
3898
3899 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003900 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003901 }
3902
3903 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003904 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3905 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003906 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3907 }
3908
Craig Mautner05d29032013-05-03 13:40:13 -07003909 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003910 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3911 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3912 }
Craig Mautner05d29032013-05-03 13:40:13 -07003913 }
3914
Craig Mautner0eea92c2013-05-16 13:35:39 -07003915 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003916 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3917 }
3918
3919 final void scheduleSleepTimeout() {
3920 removeSleepTimeouts();
3921 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3922 }
3923
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 @Override
3925 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003926 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3928 }
3929
3930 @Override
3931 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003932 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3934 }
3935
3936 @Override
3937 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003938 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3940 }
3941
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003942 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003943 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003944 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003945 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 }
3947
Andrii Kulianca007a62016-11-22 16:33:28 -08003948 /** Check if display with specified id is added to the list. */
3949 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003950 return getActivityDisplayOrCreateLocked(displayId) != null;
3951 }
3952
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003953 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003954 ActivityDisplay getActivityDisplay(int displayId) {
3955 return mActivityDisplays.get(displayId);
3956 }
3957
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003958 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
3959 ActivityDisplay getDefaultDisplay() {
3960 return mActivityDisplays.get(DEFAULT_DISPLAY);
3961 }
3962
Andrii Kulian62e6f252017-05-30 22:46:53 -07003963 /**
3964 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3965 * corresponding record in display manager.
3966 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003967 // TODO: Look into consolidating with getActivityDisplay()
3968 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003969 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3970 if (activityDisplay != null) {
3971 return activityDisplay;
3972 }
3973 if (mDisplayManager == null) {
3974 // The system isn't fully initialized yet.
3975 return null;
3976 }
3977 final Display display = mDisplayManager.getDisplay(displayId);
3978 if (display == null) {
3979 // The display is not registered in DisplayManager.
3980 return null;
3981 }
3982 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003983 activityDisplay = new ActivityDisplay(this, displayId);
3984 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07003985 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3986 mWindowManager.onDisplayAdded(displayId);
3987 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003988 }
3989
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003990 @VisibleForTesting
3991 void attachDisplay(ActivityDisplay display) {
3992 mActivityDisplays.put(display.mDisplayId, display);
3993 }
3994
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003995 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003996 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003997 mService.mContext.getResources().getDimensionPixelSize(
3998 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003999 }
4000
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004001 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004002 if (displayId == DEFAULT_DISPLAY) {
4003 throw new IllegalArgumentException("Can't remove the primary display.");
4004 }
4005
Craig Mautner4a1cb222013-12-04 16:14:06 -08004006 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004007 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4008 if (activityDisplay == null) {
4009 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004011 final boolean destroyContentOnRemoval
4012 = activityDisplay.shouldDestroyContentOnRemove();
4013 while (activityDisplay.getChildCount() > 0) {
4014 final ActivityStack stack = activityDisplay.getChildAt(0);
4015 if (destroyContentOnRemoval) {
4016 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4017 stack.finishAllActivitiesLocked(true /* immediately */);
4018 } else {
4019 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4020 // a valid launch stack for all activities. This way the task history from
4021 // external display will be preserved on primary after move.
4022 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4023 }
4024 }
4025
4026 releaseSleepTokens(activityDisplay);
4027
4028 mActivityDisplays.remove(displayId);
4029 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004031 }
4032
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004033 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004034 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004035 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4036 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004037 // The window policy is responsible for stopping activities on the default display
4038 if (displayId != Display.DEFAULT_DISPLAY) {
4039 int displayState = activityDisplay.mDisplay.getState();
4040 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4041 activityDisplay.mOffToken =
4042 mService.acquireSleepToken("Display-off", displayId);
4043 } else if (displayState == Display.STATE_ON
4044 && activityDisplay.mOffToken != null) {
4045 activityDisplay.mOffToken.release();
4046 activityDisplay.mOffToken = null;
4047 }
4048 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004049 // TODO: Update the bounds.
4050 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004051 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004052 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 }
4054
David Stevens9440dc82017-03-16 19:00:20 -07004055 SleepToken createSleepTokenLocked(String tag, int displayId) {
4056 ActivityDisplay display = mActivityDisplays.get(displayId);
4057 if (display == null) {
4058 throw new IllegalArgumentException("Invalid display: " + displayId);
4059 }
4060
4061 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4062 mSleepTokens.add(token);
4063 display.mAllSleepTokens.add(token);
4064 return token;
4065 }
4066
4067 private void removeSleepTokenLocked(SleepTokenImpl token) {
4068 mSleepTokens.remove(token);
4069
4070 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4071 if (display != null) {
4072 display.mAllSleepTokens.remove(token);
4073 if (display.mAllSleepTokens.isEmpty()) {
4074 mService.updateSleepIfNeededLocked();
4075 }
4076 }
4077 }
4078
4079 private void releaseSleepTokens(ActivityDisplay display) {
4080 if (display.mAllSleepTokens.isEmpty()) {
4081 return;
4082 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004083 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004084 mSleepTokens.remove(token);
4085 }
4086 display.mAllSleepTokens.clear();
4087
4088 mService.updateSleepIfNeededLocked();
4089 }
4090
Wale Ogunwale68278562017-09-23 17:13:55 -07004091 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004092 final int displayId = stack.mDisplayId;
4093 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004095 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004096 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004098 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004099 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004100 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004101 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004102 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103
4104 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4105 final int numTasks = tasks.size();
4106 int[] taskIds = new int[numTasks];
4107 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004108 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004109 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 for (int i = 0; i < numTasks; ++i) {
4111 final TaskRecord task = tasks.get(i);
4112 taskIds[i] = task.taskId;
4113 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4114 : task.realActivity != null ? task.realActivity.flattenToString()
4115 : task.getTopActivity() != null ? task.getTopActivity().packageName
4116 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004117 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004118 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004119 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004120 }
4121 info.taskIds = taskIds;
4122 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004123 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004124 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004125
4126 final ActivityRecord top = stack.topRunningActivityLocked();
4127 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004128 return info;
4129 }
4130
Wale Ogunwale68278562017-09-23 17:13:55 -07004131 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 ActivityStack stack = getStack(stackId);
4133 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004134 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004135 }
4136 return null;
4137 }
4138
Wale Ogunwale68278562017-09-23 17:13:55 -07004139 StackInfo getStackInfo(int windowingMode, int activityType) {
4140 final ActivityStack stack = getStack(windowingMode, activityType);
4141 return (stack != null) ? getStackInfo(stack) : null;
4142 }
4143
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004145 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004146 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004147 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4148 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4149 final ActivityStack stack = display.getChildAt(stackNdx);
4150 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 }
4152 }
4153 return list;
4154 }
4155
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004156 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004157 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004158 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004159 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004160 }
4161
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004162 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004163 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004164 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004165 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004166 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004167 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004168 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004169 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004170 return;
4171 }
4172
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004173 // Handle incorrect launch/move to secondary display if needed.
4174 final boolean launchOnSecondaryDisplayFailed;
4175 if (isSecondaryDisplayPreferred) {
4176 final int actualDisplayId = task.getStack().mDisplayId;
4177 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4178 // The task landed on an inappropriate display somehow, move it to the default
4179 // display.
4180 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004181 mService.setTaskWindowingMode(task.taskId,
4182 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004183 launchOnSecondaryDisplayFailed = true;
4184 } else {
4185 // The task might have landed on a display different from requested.
4186 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4187 || (preferredDisplayId != INVALID_DISPLAY
4188 && preferredDisplayId != actualDisplayId);
4189 }
4190 } else {
4191 // The task wasn't requested to be on a secondary display.
4192 launchOnSecondaryDisplayFailed = false;
4193 }
4194
Jorim Jaggicd13d332016-04-27 15:40:20 -07004195 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004196 if (launchOnSecondaryDisplayFailed
4197 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004198 if (launchOnSecondaryDisplayFailed) {
4199 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4200 // display with config different from global config.
4201 mService.mTaskChangeNotificationController
4202 .notifyActivityLaunchOnSecondaryDisplayFailed();
4203 } else {
4204 // Display a warning toast that we tried to put a non-dockable task in the docked
4205 // stack.
4206 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4207 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004208
Andrii Kulianc27916642016-04-12 17:59:27 -07004209 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4210 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004211
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004212 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004213 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004214 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004215 }
Winson Chungd3395382016-12-13 11:49:09 -08004216 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004217 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004218 final String packageName = topActivity.appInfo.packageName;
4219 final int reason = isSecondaryDisplayPreferred
4220 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4221 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004222 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004223 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004224 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004225 }
4226
Jorim Jaggife89d122015-12-22 16:28:44 +01004227 void activityRelaunchedLocked(IBinder token) {
4228 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004229 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4230 if (r != null) {
4231 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004232 r.setSleeping(true, true);
4233 }
4234 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004235 }
4236
4237 void activityRelaunchingLocked(ActivityRecord r) {
4238 mWindowManager.notifyAppRelaunching(r.appToken);
4239 }
4240
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004241 void logStackState() {
4242 mActivityMetricsLogger.logWindowState();
4243 }
4244
Winson Chung5af42fc2017-03-24 17:11:33 -07004245 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004246 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4247 // end, then ensure that we defer all in between multi-window mode changes
4248 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4249 return;
4250 }
4251
Wale Ogunwale22e25262016-02-01 10:32:02 -08004252 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4253 final ActivityRecord r = task.mActivities.get(i);
4254 if (r.app != null && r.app.thread != null) {
4255 mMultiWindowModeChangedActivities.add(r);
4256 }
4257 }
4258
4259 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4260 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4261 }
4262 }
4263
Winson Chung5af42fc2017-03-24 17:11:33 -07004264 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004265 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004266 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004267 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004268 return;
4269 }
4270
Winson Chungab76bbc2017-08-14 13:33:51 -07004271 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004272 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004273
Winson Chungab76bbc2017-08-14 13:33:51 -07004274 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4275 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4276 final ActivityRecord r = task.mActivities.get(i);
4277 if (r.app != null && r.app.thread != null) {
4278 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004279 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004280 }
4281 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004282
Winson Chungab76bbc2017-08-14 13:33:51 -07004283 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4284 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4285 }
4286 }
4287
4288 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4289 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4290 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4291 final ActivityRecord r = task.mActivities.get(i);
4292 if (r.app != null && r.app.thread != null) {
4293 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004294 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004295 }
4296 }
4297
Tony Mak853304c2016-04-18 15:17:41 +01004298 void setDockedStackMinimized(boolean minimized) {
4299 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004300 }
4301
chaviw59b98852017-06-13 12:05:44 -07004302 void wakeUp(String reason) {
4303 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4304 }
4305
4306 /**
4307 * Begin deferring resume to avoid duplicate resumes in one pass.
4308 */
4309 private void beginDeferResume() {
4310 mDeferResumeCount++;
4311 }
4312
4313 /**
4314 * End deferring resume and determine if resume can be called.
4315 */
4316 private void endDeferResume() {
4317 mDeferResumeCount--;
4318 }
4319
4320 /**
4321 * @return True if resume can be called.
4322 */
4323 private boolean readyToResume() {
4324 return mDeferResumeCount == 0;
4325 }
4326
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004327 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004328
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004329 public ActivityStackSupervisorHandler(Looper looper) {
4330 super(looper);
4331 }
4332
Winson Chung4dabf232017-01-25 13:25:22 -08004333 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004334 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004335 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4336 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004337 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004338 }
4339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004340 @Override
4341 public void handleMessage(Message msg) {
4342 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004343 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4344 synchronized (mService) {
4345 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4346 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004347 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004348 }
4349 }
4350 } break;
4351 case REPORT_PIP_MODE_CHANGED_MSG: {
4352 synchronized (mService) {
4353 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4354 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004355 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4356 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004357 }
4358 }
4359 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004360 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004361 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4362 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004363 // We don't at this point know if the activity is fullscreen,
4364 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004365 activityIdleInternal((ActivityRecord) msg.obj,
4366 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004367 } break;
4368 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004369 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004370 activityIdleInternal((ActivityRecord) msg.obj,
4371 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004372 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004373 case RESUME_TOP_ACTIVITY_MSG: {
4374 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004375 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004376 }
4377 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004378 case SLEEP_TIMEOUT_MSG: {
4379 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004380 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004381 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004382 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004383 }
4384 }
4385 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004386 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004387 synchronized (mService) {
4388 if (mLaunchingActivity.isHeld()) {
4389 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4390 if (VALIDATE_WAKE_LOCK_CALLER
4391 && Binder.getCallingUid() != Process.myUid()) {
4392 throw new IllegalStateException("Calling must be system uid");
4393 }
4394 mLaunchingActivity.release();
4395 }
4396 }
4397 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004398 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004399 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004400 } break;
4401 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004402 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004403 } break;
4404 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004405 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004406 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004407 case LAUNCH_TASK_BEHIND_COMPLETE: {
4408 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004409 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004410 if (r != null) {
4411 handleLaunchTaskBehindCompleteLocked(r);
4412 }
4413 }
4414 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004415
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004416 }
4417 }
4418 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004419
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004420 ActivityStack findStackBehind(ActivityStack stack) {
4421 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004422 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004423 if (display == null) {
4424 return null;
4425 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004426 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4427 if (display.getChildAt(i) == stack && i > 0) {
4428 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004429 }
4430 }
4431 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004432 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004433 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004434
Jorim Jaggic69bd222016-03-15 14:38:37 +01004435 /**
4436 * Puts a task into resizing mode during the next app transition.
4437 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004438 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004439 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004440 private void setResizingDuringAnimation(TaskRecord task) {
4441 mResizingTasksDuringAnimation.add(task.taskId);
4442 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004443 }
4444
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004445 int startActivityFromRecents(int taskId, Bundle bOptions) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004446 final TaskRecord task;
4447 final int callingUid;
4448 final String callingPackage;
4449 final Intent intent;
4450 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004451 int activityType = ACTIVITY_TYPE_UNDEFINED;
4452 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004453 final ActivityOptions activityOptions = (bOptions != null)
4454 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004455 if (activityOptions != null) {
4456 activityType = activityOptions.getLaunchActivityType();
4457 windowingMode = activityOptions.getLaunchWindowingMode();
4458 }
4459 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004460 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004461 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004462 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004463
Matthew Ng606dd802017-06-05 14:06:32 -07004464 mWindowManager.deferSurfaceLayout();
4465 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004466 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004467 mWindowManager.setDockedStackCreateState(
Matthew Ngbf155872017-10-27 15:24:39 -07004468 activityOptions.getSplitScreenCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004469
Matthew Ng606dd802017-06-05 14:06:32 -07004470 // Defer updating the stack in which recents is until the app transition is done, to
4471 // not run into issues where we still need to draw the task in recents but the
4472 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004473 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004474 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004475 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004476
Matthew Ng606dd802017-06-05 14:06:32 -07004477 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004478 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004479 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004480 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004481 mWindowManager.executeAppTransition();
4482 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004483 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004484 }
4485
Wale Ogunwale66e16852017-10-19 13:35:52 -07004486 // We always want to return to the home activity instead of the recents activity from
4487 // whatever is started from the recents activity, so move the home stack forward.
4488 moveHomeStackToFront("startActivityFromRecents");
4489
Matthew Ng606dd802017-06-05 14:06:32 -07004490 // If the user must confirm credentials (e.g. when first launching a work app and the
4491 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4492 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4493 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004494 final ActivityRecord targetActivity = task.getTopActivity();
4495
4496 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4497 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004498 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi172e99f2017-10-20 14:33:18 +02004499 try {
4500 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions,
4501 true /* fromRecents */);
4502 } finally {
4503 mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT,
4504 targetActivity);
4505 }
Matthew Ng606dd802017-06-05 14:06:32 -07004506
4507 // If we are launching the task in the docked stack, put it into resizing mode so
4508 // the window renders full-screen with the background filling the void. Also only
4509 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004510 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004511 setResizingDuringAnimation(task);
4512 }
4513
4514 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004515 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004516 return ActivityManager.START_TASK_TO_FRONT;
4517 }
4518 callingUid = task.mCallingUid;
4519 callingPackage = task.mCallingPackage;
4520 intent = task.intent;
4521 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4522 userId = task.userId;
4523 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004524 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004525 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004526 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004527 }
Matthew Ng606dd802017-06-05 14:06:32 -07004528 return result;
4529 } finally {
4530 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004531 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004532 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004533
4534 /**
4535 * @return a list of activities which are the top ones in each visible stack. The first
4536 * entry will be the focused activity.
4537 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004538 List<IBinder> getTopVisibleActivities() {
4539 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4540 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004541 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004542 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4543 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004544 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4545 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004546 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004547 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004548 final ActivityRecord top = stack.topActivity();
4549 if (top != null) {
4550 if (stack == mFocusedStack) {
4551 topActivityTokens.add(0, top.appToken);
4552 } else {
4553 topActivityTokens.add(top.appToken);
4554 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004555 }
4556 }
4557 }
4558 }
4559 return topActivityTokens;
4560 }
Bryce Lee4a194382017-04-04 14:32:48 -07004561
4562 /**
4563 * Internal container to store a match qualifier alongside a WaitResult.
4564 */
4565 static class WaitInfo {
4566 private final ComponentName mTargetComponent;
4567 private final WaitResult mResult;
4568
4569 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4570 this.mTargetComponent = targetComponent;
4571 this.mResult = result;
4572 }
4573
Wale Ogunwale3270f172017-04-26 07:29:42 -07004574 public boolean matches(ComponentName targetComponent) {
4575 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004576 }
4577
4578 public WaitResult getResult() {
4579 return mResult;
4580 }
4581
4582 public ComponentName getComponent() {
4583 return mTargetComponent;
4584 }
4585
4586 public void dump(PrintWriter pw, String prefix) {
4587 pw.println(prefix + "WaitInfo:");
4588 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4589 pw.println(prefix + " mResult=");
4590 mResult.dump(pw, prefix);
4591 }
4592 }
David Stevens9440dc82017-03-16 19:00:20 -07004593
4594 private final class SleepTokenImpl extends SleepToken {
4595 private final String mTag;
4596 private final long mAcquireTime;
4597 private final int mDisplayId;
4598
4599 public SleepTokenImpl(String tag, int displayId) {
4600 mTag = tag;
4601 mDisplayId = displayId;
4602 mAcquireTime = SystemClock.uptimeMillis();
4603 }
4604
4605 @Override
4606 public void release() {
4607 synchronized (mService) {
4608 removeSleepTokenLocked(this);
4609 }
4610 }
4611
4612 @Override
4613 public String toString() {
4614 return "{\"" + mTag + "\", display " + mDisplayId
4615 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4616 }
4617 }
4618
Craig Mautner27084302013-03-25 08:05:25 -07004619}