blob: 0db65913889ad81e78ec762dbaa9f2403c5656db [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 */
288 private RunningTasks mRunningTasks = new RunningTasks();
289
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);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800575 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200576 mKeyguardController = new KeyguardController(service, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700577
578 mLaunchingBoundsController = new LaunchingBoundsController();
579 mLaunchingBoundsController.registerDefaultPositioners(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700580 }
581
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800582 void setRecentTasks(RecentTasks recentTasks) {
583 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700584 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800585 }
586
Jeff Brown2c43c332014-06-12 22:38:59 -0700587 /**
588 * At the time when the constructor runs, the power manager has not yet been
589 * initialized. So we initialize our wakelocks afterwards.
590 */
591 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700592 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
593 mGoingToSleep = mPowerManager
594 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
595 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700596 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700597 }
598
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700599 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600 synchronized (mService) {
601 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200602 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603
604 mDisplayManager =
605 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
606 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800607 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608
609 Display[] displays = mDisplayManager.getDisplays();
610 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
611 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700612 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800613 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700614 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800615 }
616
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700617 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
618 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800619
620 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800621 }
Craig Mautner27084302013-03-25 08:05:25 -0700622 }
623
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700624 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700626 }
627
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700630 }
631
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700632 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700633 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700634 }
635
Wale Ogunwaled046a012015-12-24 13:05:59 -0800636 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800637 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
638 if (!focusCandidate.isFocusable()) {
639 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800640 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800641 }
642
643 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800644 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800645 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800646
Wale Ogunwaled046a012015-12-24 13:05:59 -0800647 EventLogTags.writeAmFocusedStack(
648 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
649 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
650 }
651
652 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800653 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800654 if (r != null && r.idle) {
655 checkFinishBootingLocked();
656 }
657 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700658 }
659
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700660 void moveHomeStackToFront(String reason) {
661 mHomeStack.moveToFront(reason);
662 }
663
Matthew Ng330757d2017-02-28 14:19:17 -0800664 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700665 final ActivityStack recentsStack = getDefaultDisplay().getStack(
666 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800667 if (recentsStack != null) {
668 recentsStack.moveToFront(reason);
669 }
670 }
671
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700672 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800673 boolean moveHomeStackTaskToTop(String reason) {
674 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700675
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700676 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700677 if (top == null) {
678 return false;
679 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700680 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700682 }
683
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800684 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700685 if (!mService.mBooting && !mService.mBooted) {
686 // Not ready yet!
687 return false;
688 }
689
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800690 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700691 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800692 final String myReason = reason + " resumeHomeStackTask";
693
Mark Lua56ea122015-10-08 13:31:01 +0800694 // Only resume home activity if isn't finishing.
695 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700696 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700698 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800699 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700700 }
701
Craig Mautner8d341ef2013-03-26 09:03:27 -0700702 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700703 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
704 }
705
706 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700707 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700708 }
709
710 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700711 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700712 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800713 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700714 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700715 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700716 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700717 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700718 @Nullable ActivityOptions aOptions, boolean onTop) {
719 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700720 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
721 throw new IllegalArgumentException("Should not specify activity options for non-restore"
722 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800723 }
724
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700727 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
728 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
729 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700730 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700731 if (task == null) {
732 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700734 if (aOptions != null) {
735 // Resolve the stack the task should be placed in now based on options
736 // and reparent if needed.
737 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
738 if (launchStack != null && stack != launchStack) {
739 final int reparentMode = onTop
740 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
741 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
742 "anyTaskForIdLocked");
743 }
744 }
745 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700746 }
747 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800748
Winson Chung7900bee2017-03-10 08:59:25 -0800749 // If we are matching stack tasks only, return now
750 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800751 return null;
752 }
753
Winson Chung7900bee2017-03-10 08:59:25 -0800754 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
755 // the task from recents
756 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700757 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700758
759 if (task == null) {
760 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800761 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
762 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700763
764 return null;
765 }
766
767 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700768 return task;
769 }
770
Winson Chung7900bee2017-03-10 08:59:25 -0800771 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700772 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700773 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
774 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800775 return null;
776 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700777 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800778 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700779 }
780
Craig Mautner6170f732013-04-02 13:05:23 -0700781 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800782 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700784 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
785 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
786 final ActivityStack stack = display.getChildAt(stackNdx);
787 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 if (r != null) {
789 return r;
790 }
Craig Mautner6170f732013-04-02 13:05:23 -0700791 }
792 }
793 return null;
794 }
795
Tony Mak853304c2016-04-18 15:17:41 +0100796 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000797 * Detects whether we should show a lock screen in front of this task for a locked user.
798 * <p>
799 * We'll do this if either of the following holds:
800 * <ul>
801 * <li>The top activity explicitly belongs to {@param userId}.</li>
802 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
803 * </ul>
804 *
805 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100806 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000807 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
808 // To handle the case that work app is in the task but just is not the top one.
809 final ActivityRecord activityRecord = task.getTopActivity();
810 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
811
812 return (activityRecord != null && activityRecord.userId == userId)
813 || (resultTo != null && resultTo.userId == userId);
814 }
815
816 /**
817 * Find all visible task stacks containing {@param userId} and intercept them with an activity
818 * to block out the contents and possibly start a credential-confirming intent.
819 *
820 * @param userId user handle for the locked managed profile.
821 */
822 void lockAllProfileTasks(@UserIdInt int userId) {
823 mWindowManager.deferSurfaceLayout();
824 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
826 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
827 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
828 final ActivityStack stack = display.getChildAt(stackNdx);
829 final List<TaskRecord> tasks = stack.getAllTasks();
830 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
831 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000832
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700833 // Check the task for a top activity belonging to userId, or returning a
834 // result to an activity belonging to userId. Example case: a document
835 // picker for personal files, opened by a work app, should still get locked.
836 if (taskTopActivityIsUser(task, userId)) {
837 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
838 task.taskId, userId);
839 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000840 }
Tony Mak853304c2016-04-18 15:17:41 +0100841 }
842 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000843 } finally {
844 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100845 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000846 }
847
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800848 void setNextTaskIdForUserLocked(int taskId, int userId) {
849 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
850 if (taskId > currentTaskId) {
851 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700852 }
853 }
854
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700855 static int nextTaskIdForUser(int taskId, int userId) {
856 int nextTaskId = taskId + 1;
857 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
858 // Wrap around as there will be smaller task ids that are available now.
859 nextTaskId -= MAX_TASK_IDS_PER_USER;
860 }
861 return nextTaskId;
862 }
863
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800864 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800865 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
866 // for a userId u, a taskId can only be in the range
867 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
868 // 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 -0700869 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700870 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700871 || anyTaskForIdLocked(
872 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700873 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800874 if (candidateTaskId == currentTaskId) {
875 // Something wrong!
876 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
877 throw new IllegalStateException("Cannot get an available task id."
878 + " Reached limit of " + MAX_TASK_IDS_PER_USER
879 + " running tasks per user.");
880 }
881 }
882 mCurTaskIdForUser.put(userId, candidateTaskId);
883 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700884 }
885
Chong Zhang6cda19c2016-06-14 19:07:56 -0700886 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800887 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700888 if (stack == null) {
889 return null;
890 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700891 ActivityRecord resumedActivity = stack.mResumedActivity;
892 if (resumedActivity == null || resumedActivity.app == null) {
893 resumedActivity = stack.mPausingActivity;
894 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700895 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700896 }
897 }
898 return resumedActivity;
899 }
900
Dianne Hackbornff072722014-09-24 10:56:28 -0700901 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700902 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800904 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700905 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
906 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
907 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700908 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800909 continue;
910 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200911 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
912 final ActivityRecord top = stack.topRunningActivityLocked();
913 final int size = mTmpActivityList.size();
914 for (int i = 0; i < size; i++) {
915 final ActivityRecord activity = mTmpActivityList.get(i);
916 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
917 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200919 if (realStartActivityLocked(activity, app,
920 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800921 didSomething = true;
922 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700923 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800924 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200925 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700927 }
Craig Mautner20e72272013-04-01 13:45:53 -0700928 }
Craig Mautner20e72272013-04-01 13:45:53 -0700929 }
930 }
931 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700932 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700933 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700934 }
Craig Mautner20e72272013-04-01 13:45:53 -0700935 return didSomething;
936 }
937
938 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800939 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700940 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
941 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
942 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700943 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 continue;
945 }
946 final ActivityRecord resumedActivity = stack.mResumedActivity;
947 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700948 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800949 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800950 return false;
951 }
Craig Mautner20e72272013-04-01 13:45:53 -0700952 }
953 }
Wei Wang65c7a152016-06-02 18:51:22 -0700954 // Send launch end powerhint when idle
955 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700956 return true;
957 }
958
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700961 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
962 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
963 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700964 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800965 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 return false;
968 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 }
970 }
971 }
972 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800974 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
975 mLastFocusedStack + " to=" + mFocusedStack);
976 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 return true;
978 }
979
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700980 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800981 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700983 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
984 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
985 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800987 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700988 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 return false;
990 }
991 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800992 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700993 }
994 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800995 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 }
997
Craig Mautner2acc3892013-09-23 10:28:14 -0700998 /**
999 * Pause all activities in either all of the stacks or just the back stacks.
1000 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001001 * @param resuming The resuming activity.
1002 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1003 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001004 * @return true if any activity was paused as a result of this call.
1005 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001006 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001007 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001008 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001009 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1010 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1011 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001012 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001013 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001014 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001015 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1016 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001017 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001018 }
1019 }
1020 return someActivityPaused;
1021 }
1022
Craig Mautnerde4ef022013-04-07 19:01:33 -07001023 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001024 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001025 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001026 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1027 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1028 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001030 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001031 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001032 Slog.d(TAG_STATES,
1033 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 pausing = false;
1035 } else {
1036 return false;
1037 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 }
1040 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001041 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 }
1043
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001044 void cancelInitializingActivities() {
1045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001046 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1047 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1048 final ActivityStack stack = display.getChildAt(stackNdx);
1049 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001050 }
1051 }
1052 }
1053
Bryce Lee4a194382017-04-04 14:32:48 -07001054 void waitActivityVisible(ComponentName name, WaitResult result) {
1055 final WaitInfo waitInfo = new WaitInfo(name, result);
1056 mWaitingForActivityVisible.add(waitInfo);
1057 }
1058
1059 void cleanupActivity(ActivityRecord r) {
1060 // Make sure this record is no longer in the pending finishes list.
1061 // This could happen, for example, if we are trimming activities
1062 // down to the max limit while they are still waiting to finish.
1063 mFinishingActivities.remove(r);
1064 mActivitiesWaitingForVisibleActivity.remove(r);
1065
1066 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001067 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001068 mWaitingForActivityVisible.remove(i);
1069 }
1070 }
1071 }
1072
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001073 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001074 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001075 }
1076
1077 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1078 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001079 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1080 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001081 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001082 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001083 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001084 result.timeout = false;
1085 result.who = w.getComponent();
1086 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1087 result.thisTime = result.totalTime;
1088 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001089 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001091 if (changed) {
1092 mService.notifyAll();
1093 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 }
1095
Chong Zhang5022da32016-06-21 16:31:37 -07001096 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1097 boolean changed = false;
1098 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1099 WaitResult w = mWaitingActivityLaunched.remove(i);
1100 if (w.who == null) {
1101 changed = true;
1102 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1103 // the starting activity ends up moving another activity to front, and it should
1104 // wait for this new activity to become visible instead.
1105 // Do not modify other fields.
1106 w.result = START_TASK_TO_FRONT;
1107 }
1108 }
1109 if (changed) {
1110 mService.notifyAll();
1111 }
1112 }
1113
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1115 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001117 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001118 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001119 if (w.who == null) {
1120 changed = true;
1121 w.timeout = timeout;
1122 if (r != null) {
1123 w.who = new ComponentName(r.info.packageName, r.info.name);
1124 }
1125 w.thisTime = thisTime;
1126 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001127 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001129 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001130 if (changed) {
1131 mService.notifyAll();
1132 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001133 }
1134
Craig Mautner29219d92013-04-16 20:19:12 -07001135 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001136 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001137 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001138 if (r != null) {
1139 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001140 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001141
Andrii Kulian7318d632016-07-20 18:59:28 -07001142 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001143 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1144
1145 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1146 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001147 final ActivityDisplay display = mActivityDisplays.get(displayId);
1148 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1149 final ActivityStack stack = display.getChildAt(j);
1150 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001151 r = stack.topRunningActivityLocked();
1152 if (r != null) {
1153 return r;
1154 }
Craig Mautner29219d92013-04-16 20:19:12 -07001155 }
1156 }
1157 }
1158 return null;
1159 }
1160
Winson Chung61c9e5a2017-10-11 10:39:32 -07001161 @VisibleForTesting
1162 void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
1163 @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
1164 int callingUid, boolean allowed) {
1165 mRunningTasks.getTasks(maxNum, list, ignoreActivityType, ignoreWindowingMode,
1166 mActivityDisplays, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001167 }
1168
Todd Kennedy7440f172015-12-09 14:31:22 -08001169 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1170 ProfilerInfo profilerInfo) {
1171 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001172 if (aInfo != null) {
1173 // Store the found target back into the intent, because now that
1174 // we have it we never want to do this again. For example, if the
1175 // user navigates back to this point in the history, we should
1176 // always restart the exact same activity.
1177 intent.setComponent(new ComponentName(
1178 aInfo.applicationInfo.packageName, aInfo.name));
1179
1180 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001181 if (!aInfo.processName.equals("system")) {
1182 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001183 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001184 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001185
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001186 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1187 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1188 }
1189
Man Caocfa78b22015-06-11 20:14:34 -07001190 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1191 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1192 }
1193
1194 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001195 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001196 }
1197 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001198 final String intentLaunchToken = intent.getLaunchToken();
1199 if (aInfo.launchToken == null && intentLaunchToken != null) {
1200 aInfo.launchToken = intentLaunchToken;
1201 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001202 }
1203 return aInfo;
1204 }
1205
Todd Kennedy7440f172015-12-09 14:31:22 -08001206 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001207 return resolveIntent(intent, resolvedType, userId, 0);
1208 }
1209
1210 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001211 synchronized (mService) {
1212 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001213 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001214 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001215 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001216 }
1217
1218 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1219 ProfilerInfo profilerInfo, int userId) {
1220 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1221 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1222 }
1223
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001224 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1225 boolean andResume, boolean checkConfig) throws RemoteException {
1226
1227 if (!allPausedActivitiesComplete()) {
1228 // While there are activities pausing we skipping starting any new activities until
1229 // pauses are complete. NOTE: that we also do this for activities that are starting in
1230 // the paused state because they will first be resumed then paused on the client side.
1231 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1232 "realStartActivityLocked: Skipping start of r=" + r
1233 + " some activities pausing...");
1234 return false;
1235 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001236
Bryce Leeaf691c02017-03-20 14:20:22 -07001237 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001238 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001239
1240 beginDeferResume();
1241
Craig Mautner2420ead2013-04-01 17:13:20 -07001242 try {
chaviw59b98852017-06-13 12:05:44 -07001243 r.startFreezingScreenLocked(app, 0);
1244
1245 // schedule launch ticks to collect information about slow apps.
1246 r.startLaunchTickingLocked();
1247
1248 r.app = app;
1249
Jorim Jaggi838c2452017-08-28 15:44:43 +02001250 if (mKeyguardController.isKeyguardLocked()) {
1251 r.notifyUnknownVisibilityLaunched();
1252 }
1253
chaviw59b98852017-06-13 12:05:44 -07001254 // Have the window manager re-evaluate the orientation of the screen based on the new
1255 // activity order. Note that as a result of this, it can call back into the activity
1256 // manager with a new orientation. We don't care about that, because the activity is
1257 // not currently running so we are just restarting it anyway.
1258 if (checkConfig) {
1259 final int displayId = r.getDisplayId();
1260 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1261 getDisplayOverrideConfiguration(displayId),
1262 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1263 // Deferring resume here because we're going to launch new activity shortly.
1264 // We don't want to perform a redundant launch of the same record while ensuring
1265 // configurations and trying to resume top activity of focused stack.
1266 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1267 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
chaviw59b98852017-06-13 12:05:44 -07001269
1270 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1271 true /* isTop */)) {
1272 // We only set the visibility to true if the activity is allowed to be visible
1273 // based on
1274 // keyguard state. This avoids setting this into motion in window manager that is
1275 // later cancelled due to later calls to ensure visible activities that set
1276 // visibility back to false.
1277 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 }
chaviw59b98852017-06-13 12:05:44 -07001279
chaviw59b98852017-06-13 12:05:44 -07001280 final int applicationInfoUid =
1281 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1282 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1283 Slog.wtf(TAG,
1284 "User ID for activity changing for " + r
1285 + " appInfo.uid=" + r.appInfo.uid
1286 + " info.ai.uid=" + applicationInfoUid
1287 + " old=" + r.app + " new=" + app);
1288 }
1289
1290 app.waitingToKill = null;
1291 r.launchCount++;
1292 r.lastLaunchTime = SystemClock.uptimeMillis();
1293
1294 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1295
1296 int idx = app.activities.indexOf(r);
1297 if (idx < 0) {
1298 app.activities.add(r);
1299 }
1300 mService.updateLruProcessLocked(app, true, null);
1301 mService.updateOomAdjLocked();
1302
1303 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1304 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001305 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001306 }
1307
1308 try {
1309 if (app.thread == null) {
1310 throw new RemoteException();
1311 }
1312 List<ResultInfo> results = null;
1313 List<ReferrerIntent> newIntents = null;
1314 if (andResume) {
1315 // We don't need to deliver new intents and/or set results if activity is going
1316 // to pause immediately after launch.
1317 results = r.results;
1318 newIntents = r.newIntents;
1319 }
1320 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1321 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1322 + " newIntents=" + newIntents + " andResume=" + andResume);
1323 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1324 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001325 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001326 // Home process is the root process of the task.
1327 mService.mHomeProcess = task.mActivities.get(0).app;
1328 }
1329 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1330 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1331 r.sleeping = false;
1332 r.forceNewConfig = false;
1333 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1334 mService.showAskCompatModeDialogLocked(r);
1335 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1336 ProfilerInfo profilerInfo = null;
1337 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1338 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1339 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001340 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1341 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1342 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001343 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001344 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001345 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001346 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001347 }
1348 }
chaviw59b98852017-06-13 12:05:44 -07001349
Andreas Gampe2b073a02017-06-22 17:28:57 -07001350 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001351 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 }
1353 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001354
chaviw59b98852017-06-13 12:05:44 -07001355 app.hasShownUi = true;
1356 app.pendingUiClean = true;
1357 app.forceProcessStateUpTo(mService.mTopProcessState);
1358 // Because we could be starting an Activity in the system process this may not go
1359 // across a Binder interface which would create a new Configuration. Consequently
1360 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001361
chaviw59b98852017-06-13 12:05:44 -07001362 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1363 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1364 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001365
chaviw59b98852017-06-13 12:05:44 -07001366 logIfTransactionTooLarge(r.intent, r.icicle);
1367 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1368 System.identityHashCode(r), r.info,
1369 // TODO: Have this take the merged configuration instead of separate global
1370 // and override configs.
1371 mergedConfiguration.getGlobalConfiguration(),
1372 mergedConfiguration.getOverrideConfiguration(), r.compat,
1373 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1374 r.persistentState, results, newIntents, !andResume,
1375 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001376
chaviw59b98852017-06-13 12:05:44 -07001377 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1378 // This may be a heavy-weight process! Note that the package
1379 // manager will ensure that only activity can run in the main
1380 // process of the .apk, which is the only thing that will be
1381 // considered heavy-weight.
1382 if (app.processName.equals(app.info.packageName)) {
1383 if (mService.mHeavyWeightProcess != null
1384 && mService.mHeavyWeightProcess != app) {
1385 Slog.w(TAG, "Starting new heavy weight process " + app
1386 + " when already running "
1387 + mService.mHeavyWeightProcess);
1388 }
1389 mService.mHeavyWeightProcess = app;
1390 Message msg = mService.mHandler.obtainMessage(
1391 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1392 msg.obj = r;
1393 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001394 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001396
chaviw59b98852017-06-13 12:05:44 -07001397 } catch (RemoteException e) {
1398 if (r.launchFailed) {
1399 // This is the second time we failed -- finish activity
1400 // and give up.
1401 Slog.e(TAG, "Second failure launching "
1402 + r.intent.getComponent().flattenToShortString()
1403 + ", giving up", e);
1404 mService.appDiedLocked(app);
1405 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1406 "2nd-crash", false);
1407 return false;
1408 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001409
chaviw59b98852017-06-13 12:05:44 -07001410 // This is the first time we failed -- restart process and
1411 // retry.
1412 r.launchFailed = true;
1413 app.activities.remove(r);
1414 throw e;
1415 }
1416 } finally {
1417 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001418 }
1419
1420 r.launchFailed = false;
1421 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001422 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 }
1424
chaviw59b98852017-06-13 12:05:44 -07001425 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 // As part of the process of launching, ActivityThread also performs
1427 // a resume.
1428 stack.minimalResumeActivityLocked(r);
1429 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001430 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001431 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001432 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001433 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001434 "Moving to PAUSED: " + r + " (starting in paused state)");
1435 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001436 }
1437
1438 // Launch the new version setup screen if needed. We do this -after-
1439 // launching the initial activity (that is, home), so that it can have
1440 // a chance to initialize itself while in the background, making the
1441 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001442 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001443 mService.startSetupActivityLocked();
1444 }
1445
Dianne Hackborn465fa392014-09-14 14:21:18 -07001446 // Update any services we are bound to that might care about whether
1447 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001448 if (r.app != null) {
1449 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1450 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001451
Craig Mautner2420ead2013-04-01 17:13:20 -07001452 return true;
1453 }
1454
Sudheer Shankafab200f2017-05-17 20:41:53 -07001455 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1456 int extrasSize = 0;
1457 if (intent != null) {
1458 final Bundle extras = intent.getExtras();
1459 if (extras != null) {
1460 extrasSize = extras.getSize();
1461 }
1462 }
1463 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1464 if (extrasSize + icicleSize > 200000) {
1465 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1466 + ", icicle size: " + icicleSize);
1467 }
1468 }
1469
Craig Mautnere79d42682013-04-01 19:01:53 -07001470 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001471 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001472 // Is this activity's application already running?
1473 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001474 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001475
Andrii Kulian02b7a832016-10-06 23:11:56 -07001476 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001477
1478 if (app != null && app.thread != null) {
1479 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001480 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1481 || !"android".equals(r.info.packageName)) {
1482 // Don't add this if it is a platform component that is marked
1483 // to run in multiple processes, because this is actually
1484 // part of the framework so doesn't make sense to track as a
1485 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001486 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1487 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001488 }
George Mount2c92c972014-03-20 09:38:23 -07001489 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001490 return;
1491 } catch (RemoteException e) {
1492 Slog.w(TAG, "Exception when starting activity "
1493 + r.intent.getComponent().flattenToShortString(), e);
1494 }
1495
1496 // If a dead object exception was thrown -- fall through to
1497 // restart the application.
1498 }
1499
1500 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001501 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001502 }
1503
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001504 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001505 String resultWho, int requestCode, int callingPid, int callingUid,
1506 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001507 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001508 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1509 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001510 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001511 return true;
1512 }
1513 final int componentRestriction = getComponentRestrictionForCallingPackage(
1514 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1515 final int actionRestriction = getActionRestrictionForCallingPackage(
1516 intent.getAction(), callingPackage, callingPid, callingUid);
1517 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1518 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1519 if (resultRecord != null) {
1520 resultStack.sendActivityResultLocked(-1,
1521 resultRecord, resultWho, requestCode,
1522 Activity.RESULT_CANCELED, null);
1523 }
1524 final String msg;
1525 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1526 msg = "Permission Denial: starting " + intent.toString()
1527 + " from " + callerApp + " (pid=" + callingPid
1528 + ", uid=" + callingUid + ")" + " with revoked permission "
1529 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1530 } else if (!aInfo.exported) {
1531 msg = "Permission Denial: starting " + intent.toString()
1532 + " from " + callerApp + " (pid=" + callingPid
1533 + ", uid=" + callingUid + ")"
1534 + " not exported from uid " + aInfo.applicationInfo.uid;
1535 } else {
1536 msg = "Permission Denial: starting " + intent.toString()
1537 + " from " + callerApp + " (pid=" + callingPid
1538 + ", uid=" + callingUid + ")"
1539 + " requires " + aInfo.permission;
1540 }
1541 Slog.w(TAG, msg);
1542 throw new SecurityException(msg);
1543 }
1544
1545 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1546 final String message = "Appop Denial: starting " + intent.toString()
1547 + " from " + callerApp + " (pid=" + callingPid
1548 + ", uid=" + callingUid + ")"
1549 + " requires " + AppOpsManager.permissionToOp(
1550 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1551 Slog.w(TAG, message);
1552 return false;
1553 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1554 final String message = "Appop Denial: starting " + intent.toString()
1555 + " from " + callerApp + " (pid=" + callingPid
1556 + ", uid=" + callingUid + ")"
1557 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1558 Slog.w(TAG, message);
1559 return false;
1560 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001561 if (options != null) {
1562 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1563 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1564 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001565 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001566 final String msg = "Permission Denial: starting " + intent.toString()
1567 + " from " + callerApp + " (pid=" + callingPid
1568 + ", uid=" + callingUid + ") with launchTaskId="
1569 + options.getLaunchTaskId();
1570 Slog.w(TAG, msg);
1571 throw new SecurityException(msg);
1572 }
1573 }
1574 // Check if someone tries to launch an activity on a private display with a different
1575 // owner.
1576 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001577 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1578 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001579 final String msg = "Permission Denial: starting " + intent.toString()
1580 + " from " + callerApp + " (pid=" + callingPid
1581 + ", uid=" + callingUid + ") with launchDisplayId="
1582 + launchDisplayId;
1583 Slog.w(TAG, msg);
1584 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001585 }
Charles He2bf28322017-10-12 22:24:49 +01001586 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1587 final boolean lockTaskMode = options.getLockTaskMode();
1588 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1589 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1590 final String msg = "Permission Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ") with lockTaskMode=true";
1593 Slog.w(TAG, msg);
1594 throw new SecurityException(msg);
1595 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001596 }
1597
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001598 return true;
1599 }
1600
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001601 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001602 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1603 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001604 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1605 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1606
Andrii Kulian02689a72017-07-06 14:28:59 -07001607 if (callingPid == -1 && callingUid == -1) {
1608 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1609 return true;
1610 }
1611
Andrii Kulian62e6f252017-05-30 22:46:53 -07001612 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001613 if (activityDisplay == null) {
1614 Slog.w(TAG, "Launch on display check: display not found");
1615 return false;
1616 }
1617
Andrii Kulian02689a72017-07-06 14:28:59 -07001618 // Check if the caller has enough privileges to embed activities and launch to private
1619 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001620 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001621 callingUid);
1622 if (startAnyPerm == PERMISSION_GRANTED) {
1623 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1624 + " allow launch any on display");
1625 return true;
1626 }
1627
Andrii Kulian8f070292017-09-12 22:56:49 -07001628 // Check if caller is already present on display
1629 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1630
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001631 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1632 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1633 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001634 // Limit launching on virtual displays, because their contents can be read from Surface
1635 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001636 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1637 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1638 + " disallow launch on virtual display for not-embedded activity.");
1639 return false;
1640 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001641 // Check if the caller is allowed to embed activities from other apps.
1642 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001643 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001644 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1645 + " disallow activity embedding without permission.");
1646 return false;
1647 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001648 }
1649
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001650 if (!activityDisplay.isPrivate()) {
1651 // Anyone can launch on a public display.
1652 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1653 + " allow launch on public display");
1654 return true;
1655 }
1656
1657 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001658 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001659 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1660 + " allow launch for owner of the display");
1661 return true;
1662 }
1663
Andrii Kulian8f070292017-09-12 22:56:49 -07001664 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001665 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1666 + " allow launch for caller present on the display");
1667 return true;
1668 }
1669
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001670 Slog.w(TAG, "Launch on display check: denied");
1671 return false;
1672 }
1673
1674 /** Update lists of UIDs that are present on displays and have access to them. */
1675 void updateUIDsPresentOnDisplay() {
1676 mDisplayAccessUIDs.clear();
1677 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1678 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001679 // Only bother calculating the whitelist for private displays
1680 if (activityDisplay.isPrivate()) {
1681 mDisplayAccessUIDs.append(
1682 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1683 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 }
1685 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1686 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1687 }
1688
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001689 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001690 final long identity = Binder.clearCallingIdentity();
1691 try {
1692 return UserManager.get(mService.mContext).getUserInfo(userId);
1693 } finally {
1694 Binder.restoreCallingIdentity(identity);
1695 }
1696 }
1697
Svet Ganov99b60432015-06-27 13:15:22 -07001698 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001699 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001700 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1701 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001702 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001703 return ACTIVITY_RESTRICTION_PERMISSION;
1704 }
1705
Christopher Tateff7add02015-08-17 10:23:22 -07001706 if (activityInfo.permission == null) {
1707 return ACTIVITY_RESTRICTION_NONE;
1708 }
1709
Svet Ganov99b60432015-06-27 13:15:22 -07001710 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1711 if (opCode == AppOpsManager.OP_NONE) {
1712 return ACTIVITY_RESTRICTION_NONE;
1713 }
1714
1715 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1716 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001717 if (!ignoreTargetSecurity) {
1718 return ACTIVITY_RESTRICTION_APPOP;
1719 }
Svet Ganov99b60432015-06-27 13:15:22 -07001720 }
1721
1722 return ACTIVITY_RESTRICTION_NONE;
1723 }
1724
Svetoslav7008b512015-06-24 18:47:07 -07001725 private int getActionRestrictionForCallingPackage(String action,
1726 String callingPackage, int callingPid, int callingUid) {
1727 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001729 }
1730
1731 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1732 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 final PackageInfo packageInfo;
1737 try {
1738 packageInfo = mService.mContext.getPackageManager()
1739 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1740 } catch (PackageManager.NameNotFoundException e) {
1741 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001742 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001743 }
1744
1745 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001746 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001747 }
1748
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001749 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 final int opCode = AppOpsManager.permissionToOpCode(permission);
1754 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
1758 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1759 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001766 void setLaunchSource(int uid) {
1767 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1768 }
1769
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001770 void acquireLaunchWakelock() {
1771 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1772 throw new IllegalStateException("Calling must be system uid");
1773 }
1774 mLaunchingActivity.acquire();
1775 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1776 // To be safe, don't allow the wake lock to be held for too long.
1777 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1778 }
1779 }
1780
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001781 /**
1782 * Called when the frontmost task is idle.
1783 * @return the state of mService.mBooting before this was called.
1784 */
1785 private boolean checkFinishBootingLocked() {
1786 final boolean booting = mService.mBooting;
1787 boolean enableScreen = false;
1788 mService.mBooting = false;
1789 if (!mService.mBooted) {
1790 mService.mBooted = true;
1791 enableScreen = true;
1792 }
1793 if (booting || enableScreen) {
1794 mService.postFinishBooting(booting, enableScreen);
1795 }
1796 return booting;
1797 }
1798
Craig Mautnerf3333272013-04-22 10:55:53 -07001799 // Checked.
1800 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001801 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001802 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001803
Craig Mautnerf3333272013-04-22 10:55:53 -07001804 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001805 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001806 int NS = 0;
1807 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001808 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001809 boolean activityRemoved = false;
1810
Wale Ogunwale7d701172015-03-11 15:36:30 -07001811 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001812 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001813 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1814 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001815 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1816 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001817 if (fromTimeout) {
1818 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001819 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001820
1821 // This is a hack to semi-deal with a race condition
1822 // in the client where it can be constructed with a
1823 // newer configuration from when we asked it to launch.
1824 // We'll update with whatever configuration it now says
1825 // it used to launch.
1826 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001827 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001828 }
1829
1830 // We are now idle. If someone is waiting for a thumbnail from
1831 // us, we can now deliver.
1832 r.idle = true;
1833
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001835 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001836 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001837 }
1838 }
1839
1840 if (allResumedActivitiesIdle()) {
1841 if (r != null) {
1842 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001843 }
1844
1845 if (mLaunchingActivity.isHeld()) {
1846 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1847 if (VALIDATE_WAKE_LOCK_CALLER &&
1848 Binder.getCallingUid() != Process.myUid()) {
1849 throw new IllegalStateException("Calling must be system uid");
1850 }
1851 mLaunchingActivity.release();
1852 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001853 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001854 }
1855
1856 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001857 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1858 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001859 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001860 if ((NF = mFinishingActivities.size()) > 0) {
1861 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001862 mFinishingActivities.clear();
1863 }
1864
Craig Mautnerf3333272013-04-22 10:55:53 -07001865 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001866 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001867 mStartingUsers.clear();
1868 }
1869
Craig Mautnerf3333272013-04-22 10:55:53 -07001870 // Stop any activities that are scheduled to do so but have been
1871 // waiting for the next one to start.
1872 for (int i = 0; i < NS; i++) {
1873 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001874 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001875 if (stack != null) {
1876 if (r.finishing) {
1877 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1878 } else {
1879 stack.stopActivityLocked(r);
1880 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001881 }
1882 }
1883
1884 // Finish any activities that are scheduled to do so but have been
1885 // waiting for the next one to start.
1886 for (int i = 0; i < NF; i++) {
1887 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001888 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001889 if (stack != null) {
1890 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1891 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001892 }
1893
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001894 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001895 // Complete user switch
1896 if (startingUsers != null) {
1897 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001898 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001899 }
1900 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 }
1902
1903 mService.trimApplications();
1904 //dump();
1905 //mWindowManager.dump();
1906
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001908 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001911 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001912 }
1913
Craig Mautner8e569572013-10-11 17:36:59 -07001914 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001915 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001916 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001917 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1918 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1919 final ActivityStack stack = display.getChildAt(stackNdx);
1920 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001921 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001922 }
Craig Mautner19091252013-10-05 00:03:53 -07001923 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001924 }
1925
1926 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001927 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001928 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1929 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1930 final ActivityStack stack = display.getChildAt(stackNdx);
1931 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001932 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001933 }
1934 }
1935
Craig Mautner93529a42013-10-04 15:03:13 -07001936 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001937 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001938 }
1939
Craig Mautner8d341ef2013-03-26 09:03:27 -07001940 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001941 * Update the last used stack id for non-current user (current user's last
1942 * used stack is the focused stack)
1943 */
1944 void updateUserStackLocked(int userId, ActivityStack stack) {
1945 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001946 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001947 }
1948 }
1949
1950 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 * @return true if some activity was finished (or would have finished if doit were true).
1952 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001953 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1954 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001955 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001956 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001957 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1958 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1959 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001960 if (stack.finishDisabledPackageActivitiesLocked(
1961 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001962 didSomething = true;
1963 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001964 }
1965 }
1966 return didSomething;
1967 }
1968
Dianne Hackborna413dc02013-07-12 12:02:55 -07001969 void updatePreviousProcessLocked(ActivityRecord r) {
1970 // Now that this process has stopped, we may want to consider
1971 // it to be the previous app to try to keep around in case
1972 // the user wants to return to it.
1973
1974 // First, found out what is currently the foreground app, so that
1975 // we don't blow away the previous app if this activity is being
1976 // hosted by the process that is actually still the foreground.
1977 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001978 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001979 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1980 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1981 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001982 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001983 if (stack.mResumedActivity != null) {
1984 fgApp = stack.mResumedActivity.app;
1985 } else if (stack.mPausingActivity != null) {
1986 fgApp = stack.mPausingActivity.app;
1987 }
1988 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001989 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001990 }
1991 }
1992
1993 // Now set this one as the previous process, only if that really
1994 // makes sense to.
1995 if (r.app != null && fgApp != null && r.app != fgApp
1996 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07001997 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001998 mService.mPreviousProcess = r.app;
1999 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2000 }
2001 }
2002
Wale Ogunwaled046a012015-12-24 13:05:59 -08002003 boolean resumeFocusedStackTopActivityLocked() {
2004 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002005 }
2006
Wale Ogunwaled046a012015-12-24 13:05:59 -08002007 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002008 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002009
2010 if (!readyToResume()) {
2011 return false;
2012 }
2013
Wale Ogunwaled046a012015-12-24 13:05:59 -08002014 if (targetStack != null && isFocusedStack(targetStack)) {
2015 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002016 }
chaviw59b98852017-06-13 12:05:44 -07002017
Wale Ogunwale06579d62016-04-30 15:29:06 -07002018 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2019 if (r == null || r.state != RESUMED) {
2020 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002021 } else if (r.state == RESUMED) {
2022 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2023 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002024 }
chaviw59b98852017-06-13 12:05:44 -07002025
Wale Ogunwaled046a012015-12-24 13:05:59 -08002026 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002027 }
2028
Todd Kennedy39bfee52016-02-24 10:28:21 -08002029 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002031 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2032 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2033 final ActivityStack stack = display.getChildAt(stackNdx);
2034 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002035 }
2036 }
2037 }
2038
Adrian Roos20d7df32016-01-12 18:59:43 +01002039 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2040 TaskRecord finishedTask = null;
2041 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002042 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002043 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2044 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002045 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002046 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002047 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2048 if (stack == focusedStack || finishedTask == null) {
2049 finishedTask = t;
2050 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002051 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002052 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002053 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002054 }
2055
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002056 void finishVoiceTask(IVoiceInteractionSession session) {
2057 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002058 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2059 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002060 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002061 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002062 stack.finishVoiceTask(session);
2063 }
2064 }
2065 }
2066
Wale Ogunwale66e16852017-10-19 13:35:52 -07002067 void findTaskToMoveToFront(TaskRecord task, int flags, ActivityOptions options,
Andrii Kulianc27916642016-04-12 17:59:27 -07002068 String reason, boolean forceNonResizeable) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07002069 final ActivityStack currentStack = task.getStack();
2070 if (currentStack == null) {
2071 Slog.e(TAG, "findTaskToMoveToFront: can't move task="
2072 + task + " to front. Stack is null");
2073 return;
2074 }
2075
Craig Mautneraea74a52014-03-08 14:23:10 -08002076 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2077 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002078 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002079
2080 final ActivityRecord prev = topRunningActivityLocked();
2081
2082 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0
2083 || (prev != null && prev.isActivityTypeRecents())) {
2084 // Caller wants the home activity moved with it or the previous task is recents in which
2085 // case we always return home from the task we are moving to the front.
2086 moveHomeStackToFront("findTaskToMoveToFront");
Wale Ogunwale7d701172015-03-11 15:36:30 -07002087 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002088
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002089 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2090 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2091 task.updateOverrideConfiguration(bounds);
2092
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002093 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002094
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002095 if (stack != currentStack) {
2096 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002097 "findTaskToMoveToFront");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002098 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002099 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2100 // still need moveTaskToFrontLocked() below for any transition settings.
2101 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002102 if (stack.resizeStackWithLaunchBounds()) {
2103 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002104 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2105 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002106 } else {
2107 // WM resizeTask must be done after the task is moved to the correct stack,
2108 // because Task's setBounds() also updates dim layer's bounds, but that has
2109 // dependency on the stack.
2110 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002111 }
2112 }
2113
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002114 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002115 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002116 r == null ? null : r.appTimeTracker, reason);
2117
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002118 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002119 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002120
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002121 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002122 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002123 }
2124
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002125 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002126 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002127 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002128 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002129 return false;
2130 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002131 return (mService.mSupportsPictureInPicture
2132 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002133 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002134 }
2135
Bryce Leedacefc42017-10-10 12:56:02 -07002136 LaunchingBoundsController getLaunchingBoundsController() {
2137 return mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002138 }
2139
Winson Chung55893332017-02-17 17:13:10 -08002140 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002141 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2142 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2143 if (stack != null) {
2144 return stack;
2145 }
2146 }
2147 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002148 }
2149
Wale Ogunwale68278562017-09-23 17:13:55 -07002150 /** @see ActivityDisplay#getStack(int, int) */
2151 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2152 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2153 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2154 if (stack != null) {
2155 return stack;
2156 }
2157 }
2158 return null;
2159 }
2160
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002161 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002162 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002163 // Preference is given to the activity type for the activity then the task since the type
2164 // once set shouldn't change.
2165 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2166 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2167 activityType = task.getActivityType();
2168 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002169 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2170 return activityType;
2171 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002172 if (options != null) {
2173 activityType = options.getLaunchActivityType();
2174 }
2175 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002176 }
2177
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002178 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2179 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2180 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002181 }
2182
2183 /**
2184 * Returns the right stack to use for launching factoring in all the input parameters.
2185 *
2186 * @param r The activity we are trying to launch. Can be null.
2187 * @param options The activity options used to the launch. Can be null.
2188 * @param candidateTask The possible task the activity might be launched in. Can be null.
2189 *
2190 * @return The stack to use for the launch or INVALID_STACK_ID.
2191 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002192 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2193 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2194 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002195 int taskId = INVALID_TASK_ID;
2196 int displayId = INVALID_DISPLAY;
2197 //Rect bounds = null;
2198
2199 // We give preference to the launch preference in activity options.
2200 if (options != null) {
2201 taskId = options.getLaunchTaskId();
2202 displayId = options.getLaunchDisplayId();
2203 // TODO: Need to work this into the equation...
2204 //bounds = options.getLaunchBounds();
2205 }
2206
2207 // First preference for stack goes to the task Id set in the activity options. Use the stack
2208 // associated with that if possible.
2209 if (taskId != INVALID_TASK_ID) {
2210 // Temporarily set the task id to invalid in case in re-entry.
2211 options.setLaunchTaskId(INVALID_TASK_ID);
2212 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002213 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002214 options.setLaunchTaskId(taskId);
2215 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002216 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002217 }
2218 }
2219
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002220 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002221 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002222
2223 // Next preference for stack goes to the display Id set in the activity options or the
2224 // candidate display.
2225 if (displayId == INVALID_DISPLAY) {
2226 displayId = candidateDisplayId;
2227 }
2228 if (displayId != INVALID_DISPLAY) {
2229 if (r != null) {
2230 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002231 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002232 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002233 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002234 }
2235 }
2236 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2237 if (display != null) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002238 stack = display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002239 if (stack != null) {
2240 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002241 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002242 }
2243 }
2244
2245 // Give preference to the stack and display of the input task and activity if they match the
2246 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002247 stack = null;
2248 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002249 if (candidateTask != null) {
2250 stack = candidateTask.getStack();
2251 }
2252 if (stack == null && r != null) {
2253 stack = r.getStack();
2254 }
2255 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002256 display = stack.getDisplay();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002257 if (display != null) {
2258 final int windowingMode =
2259 display.resolveWindowingMode(r, options, candidateTask, activityType);
2260 if (stack.isCompatible(windowingMode, activityType)) {
2261 return stack;
2262 }
2263 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002264 }
2265
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002266 if (display == null
2267 // TODO: Can be removed once we figure-out how non-standard types should launch
2268 // outside the default display.
2269 || (activityType != ACTIVITY_TYPE_STANDARD
2270 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2271 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002272 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002273
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002274 return display.getOrCreateStack(r, options, candidateTask, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002275 }
2276
Andrii Kulian16802aa2016-11-02 12:21:33 -07002277 /**
2278 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2279 * If there is no such stack, new dynamic stack can be created.
2280 * @param displayId Target display.
2281 * @param r Activity that should be launched there.
2282 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2283 */
2284 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002285 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002286 if (activityDisplay == null) {
2287 throw new IllegalArgumentException(
2288 "Display with displayId=" + displayId + " not found.");
2289 }
2290
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002291 if (!r.canBeLaunchedOnDisplay(displayId)) {
2292 return null;
2293 }
2294
Andrii Kulian16802aa2016-11-02 12:21:33 -07002295 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002296 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2297 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002298 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002299 return stack;
2300 }
2301 }
2302
2303 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002304 if (displayId != DEFAULT_DISPLAY) {
2305 return activityDisplay.createStack(
2306 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002307 }
2308
2309 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2310 return null;
2311 }
2312
Wale Ogunwale68278562017-09-23 17:13:55 -07002313 // TODO: Can probably be consolidated into getLaunchStack()...
2314 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2315 switch (stack.getActivityType()) {
2316 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2317 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2318 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002319 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002320 switch (stack.getWindowingMode()) {
2321 case WINDOWING_MODE_FULLSCREEN: return true;
2322 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2323 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2324 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2325 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2326 }
2327
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002328 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002329 return r.canBeLaunchedOnDisplay(displayId);
2330 }
2331 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2332 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002333 }
2334
Andrii Kulian7fc22812016-12-28 13:04:11 -08002335 /**
2336 * Get next focusable stack in the system. This will search across displays and stacks
2337 * in last-focused order for a focusable and visible stack, different from the target stack.
2338 *
2339 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2340 * searching for next candidate.
2341 * @return Next focusable {@link ActivityStack}, null if not found.
2342 */
2343 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2344 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2345
2346 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2347 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002348 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002349 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2350 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2351 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002352 if (stack != currentFocus && stack.isFocusable()
2353 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002354 return stack;
2355 }
2356 }
2357 }
2358
2359 return null;
2360 }
2361
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002362 /**
2363 * Get next valid stack for launching provided activity in the system. This will search across
2364 * displays and stacks in last-focused order for a focusable and visible stack, except those
2365 * that are on a currently focused display.
2366 *
2367 * @param r The activity that is being launched.
2368 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2369 * searching for the next candidate.
2370 * @return Next valid {@link ActivityStack}, null if not found.
2371 */
2372 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2373 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2374 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2375 final int displayId = mTmpOrderedDisplayIds.get(i);
2376 if (displayId == currentFocus) {
2377 continue;
2378 }
2379 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2380 if (stack != null) {
2381 return stack;
2382 }
2383 }
2384 return null;
2385 }
2386
Craig Mautneree2e45a2014-06-27 12:10:03 -07002387 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002388 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002389 }
2390
2391 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002392 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2393 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2394 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002395 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002396 final ArrayList<ActivityRecord> activities = task.mActivities;
2397 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2398 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002399 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002400 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002401 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002402 }
2403 }
2404 }
2405 }
2406 return null;
2407 }
2408
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002409 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2410 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2411 boolean deferResume) {
2412
2413 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002414 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002415 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002416 return;
2417 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002418
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002419 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002420 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002421 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002422 // If the docked stack exists, don't resize non-floating stacks independently of the
2423 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002424 return;
2425 }
2426
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002427 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002428 mWindowManager.deferSurfaceLayout();
2429 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002430 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002431 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2432 // null bounds = fullscreen windowing mode...at least for now.
2433 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2434 } else if (splitScreenActive) {
2435 // If we are in split-screen mode and this stack support split-screen, then
2436 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2437 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2438 }
2439 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002440 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002441 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002442 stack.ensureVisibleActivitiesConfigurationLocked(
2443 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002444 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002445 } finally {
2446 mWindowManager.continueSurfaceLayout();
2447 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002448 }
2449 }
2450
Wale Ogunwale68278562017-09-23 17:13:55 -07002451 private void deferUpdateBounds(int activityType) {
2452 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002453 if (stack != null) {
2454 stack.deferUpdateBounds();
2455 }
2456 }
2457
Wale Ogunwale68278562017-09-23 17:13:55 -07002458 private void continueUpdateBounds(int activityType) {
2459 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002460 if (stack != null) {
2461 stack.continueUpdateBounds();
2462 }
2463 }
2464
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002465 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002466 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002467 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2468 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002469 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002470 if (task != null) {
2471 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002472 }
2473 }
2474 mResizingTasksDuringAnimation.clear();
2475 }
2476
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002477 /**
2478 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002479 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002480 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002481 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002482 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002483
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002484 mWindowManager.deferSurfaceLayout();
2485 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002486 final int windowingMode = fromStack.getWindowingMode();
2487 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2488 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2489
2490 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002491 // Tell the display we are exiting split-screen mode.
2492 toDisplay.onExitingSplitScreenMode();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002493 // We are moving all tasks from the docked stack to the fullscreen stack,
2494 // which is dismissing the docked stack, so resize all other stacks to
2495 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002496 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2497 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002498 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2499 continue;
2500 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002501 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002502 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002503 }
2504
2505 // Also disable docked stack resizing since we have manually adjusted the
2506 // size of other stacks above and we don't want to trigger a docked stack
2507 // resize when we remove task from it below and it is detached from the
2508 // display because it no longer contains any tasks.
2509 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002510 } else if (inPinnedWindowingMode && onTop) {
2511 // Log if we are expanding the PiP to fullscreen
2512 MetricsLogger.action(mService.mContext,
2513 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002514 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002515
Winson Chung5af42fc2017-03-24 17:11:33 -07002516 // If we are moving from the pinned stack, then the animation takes care of updating
2517 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002518 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002519 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002520
Wale Ogunwale388945c2017-10-04 12:13:46 -07002521 if (!tasks.isEmpty()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002522 mTmpOptions.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002523 final int size = tasks.size();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002524 for (int i = 0; i < size; ++i) {
2525 final TaskRecord task = tasks.get(i);
2526 final ActivityStack toStack = toDisplay.getOrCreateStack(
2527 null, mTmpOptions, task, task.getActivityType(), onTop);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002528
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002529 if (onTop) {
2530 final int returnToType =
2531 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002532 final boolean isTopTask = i == (size - 1);
Wale Ogunwale388945c2017-10-04 12:13:46 -07002533 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002534 task.reparent(toStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002535 isTopTask /* animate */, DEFER_RESUME,
2536 schedulePictureInPictureModeChange,
2537 "moveTasksToFullscreenStack - onTop");
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002538 } else {
Wale Ogunwale388945c2017-10-04 12:13:46 -07002539 // Position the tasks in the fullscreen stack in order at the bottom of the
2540 // stack. Also defer resume until all the tasks have been moved to the
2541 // fullscreen stack.
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002542 task.reparent(toStack, ON_TOP,
Wale Ogunwale388945c2017-10-04 12:13:46 -07002543 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2544 schedulePictureInPictureModeChange,
2545 "moveTasksToFullscreenStack - NOT_onTop");
2546 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002547 }
2548 }
Winson Chung74666102017-02-22 17:49:24 -08002549
2550 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2551 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002552 } finally {
2553 mAllowDockedStackResize = true;
2554 mWindowManager.continueSurfaceLayout();
2555 }
2556 }
2557
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002558 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002559 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2560 }
2561
2562 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2563 mWindowManager.inSurfaceTransaction(() ->
2564 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002565 }
2566
Jorim Jaggidc249c42015-12-15 14:57:31 -08002567 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002568 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2569 boolean preserveWindows) {
2570 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2571 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2572 false /* deferResume */);
2573 }
2574
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002575 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002576 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2577 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002578
2579 if (!mAllowDockedStackResize) {
2580 // Docked stack resize currently disabled.
2581 return;
2582 }
2583
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002584 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002585 if (stack == null) {
2586 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2587 return;
2588 }
2589
2590 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2591 mWindowManager.deferSurfaceLayout();
2592 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002593 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2594 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002595 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002596 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002597
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002598 // TODO: Checking for isAttached might not be needed as if the user passes in null
2599 // dockedBounds then they want the docked stack to be dismissed.
2600 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2601 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002602 // In this case we make all other static stacks fullscreen and move all
2603 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002604 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002605
2606 // stack shouldn't contain anymore activities, so nothing to resume.
2607 r = null;
2608 } else {
2609 // Docked stacks occupy a dedicated region on screen so the size of all other
2610 // static stacks need to be adjusted so they don't overlap with the docked stack.
2611 // We get the bounds to use from window manager which has been adjusted for any
2612 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002613 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002614 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002615 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2616 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002617 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002618 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002619 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002620 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002621 continue;
2622 }
2623 // Need to set windowing mode here before we try to get the dock bounds.
2624 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2625 current.getStackDockedModeBounds(
2626 tempOtherTaskBounds /* currentTempTaskBounds */,
2627 tempRect /* outStackBounds */,
2628 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2629
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002630 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002631 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2632 tempOtherTaskInsetBounds, preserveWindows,
2633 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002634 }
2635 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002636 if (!deferResume) {
2637 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2638 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002639 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002640 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002641 mWindowManager.continueSurfaceLayout();
2642 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2643 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002644 }
2645
Robert Carr0d00c2e2016-02-29 17:45:02 -08002646 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002647 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002648 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002649 if (stack == null) {
2650 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2651 return;
2652 }
Winson Chung19953ca2017-04-11 11:19:23 -07002653
2654 // It is possible for the bounds animation from the WM to call this but be delayed by
2655 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2656 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2657 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2658 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002659 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002660 return;
2661 }
2662
Robert Carr0d00c2e2016-02-29 17:45:02 -08002663 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2664 mWindowManager.deferSurfaceLayout();
2665 try {
2666 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002667 Rect insetBounds = null;
2668 if (tempPinnedTaskBounds != null) {
2669 // We always use 0,0 as the position for the inset rect because
2670 // if we are getting insets at all in the pinned stack it must mean
2671 // we are headed for fullscreen.
2672 insetBounds = tempRect;
2673 insetBounds.top = 0;
2674 insetBounds.left = 0;
2675 insetBounds.right = tempPinnedTaskBounds.width();
2676 insetBounds.bottom = tempPinnedTaskBounds.height();
2677 }
2678 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002679 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002680 } finally {
2681 mWindowManager.continueSurfaceLayout();
2682 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2683 }
2684 }
2685
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002686 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002687 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002688 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002689 /**
2690 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2691 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2692 * that the client receives onStop() before it is reparented. We do this by detaching
2693 * the stack from the display so that it will be considered invisible when
2694 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2695 * invisible as well and added to the stopping list. After which we process the
2696 * stopping list by handling the idle.
2697 */
2698 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2699 pinnedStack.mForceHidden = true;
2700 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2701 pinnedStack.mForceHidden = false;
2702 activityIdleInternalLocked(null, false /* fromTimeout */,
2703 true /* processPausingActivites */, null /* configuration */);
2704
2705 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002706 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002707 } else {
2708 for (int i = tasks.size() - 1; i >= 0; i--) {
2709 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2710 REMOVE_FROM_RECENTS);
2711 }
2712 }
2713 }
2714
2715 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002716 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002717 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2718 * instead moved back onto the fullscreen stack.
2719 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002720 void removeStack(ActivityStack stack) {
2721 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002722 }
2723
Wale Ogunwale68278562017-09-23 17:13:55 -07002724 /**
2725 * Removes stacks in the input windowing modes from the system if they are of activity type
2726 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2727 */
2728 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002729 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002730 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2731 }
2732 }
2733
2734 void removeStacksWithActivityTypes(int... activityTypes) {
2735 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2736 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002737 }
Robert Carr6914f082017-03-20 19:04:30 -07002738 }
2739
2740 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002741 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2742 */
2743 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2744 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2745 }
2746
2747 /**
Winson Chung010927a2016-12-15 16:12:35 -08002748 * Removes the task with the specified task id.
2749 *
2750 * @param taskId Identifier of the task to be removed.
2751 * @param killProcess Kill any process associated with the task if possible.
2752 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002753 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2754 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002755 * @return Returns true if the given task was found and removed.
2756 */
Winson Chung6954fc92017-03-24 16:22:12 -07002757 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2758 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002759 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002760 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002761 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002762 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2763 if (tr.isPersistable) {
2764 mService.notifyTaskPersisterLocked(null, true);
2765 }
2766 return true;
2767 }
2768 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2769 return false;
2770 }
2771
2772 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2773 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002774 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002775 }
2776 ComponentName component = tr.getBaseIntent().getComponent();
2777 if (component == null) {
2778 Slog.w(TAG, "No component for base intent of task: " + tr);
2779 return;
2780 }
2781
2782 // Find any running services associated with this app and stop if needed.
2783 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2784
2785 if (!killProcess) {
2786 return;
2787 }
2788
2789 // Determine if the process(es) for this task should be killed.
2790 final String pkg = component.getPackageName();
2791 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2792 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2793 for (int i = 0; i < pmap.size(); i++) {
2794
2795 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2796 for (int j = 0; j < uids.size(); j++) {
2797 ProcessRecord proc = uids.valueAt(j);
2798 if (proc.userId != tr.userId) {
2799 // Don't kill process for a different user.
2800 continue;
2801 }
2802 if (proc == mService.mHomeProcess) {
2803 // Don't kill the home process along with tasks from the same package.
2804 continue;
2805 }
2806 if (!proc.pkgList.containsKey(pkg)) {
2807 // Don't kill process that is not associated with this task.
2808 continue;
2809 }
2810
2811 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002812 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002813 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2814 // Don't kill process(es) that has an activity in a different task that is
2815 // also in recents.
2816 return;
2817 }
2818 }
2819
2820 if (proc.foregroundServices) {
2821 // Don't kill process(es) with foreground service.
2822 return;
2823 }
2824
2825 // Add process to kill list.
2826 procsToKill.add(proc);
2827 }
2828 }
2829
2830 // Kill the running processes.
2831 for (int i = 0; i < procsToKill.size(); i++) {
2832 ProcessRecord pr = procsToKill.get(i);
2833 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2834 && pr.curReceivers.isEmpty()) {
2835 pr.kill("remove task", true);
2836 } else {
2837 // We delay killing processes that are not in the background or running a receiver.
2838 pr.waitingToKill = "remove task";
2839 }
2840 }
2841 }
2842
Craig Mautner4a1cb222013-12-04 16:14:06 -08002843 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002844 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002845 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002846 break;
2847 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002848 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002849 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002850 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002851 }
2852
Chong Zhang5dcb2752015-08-18 13:50:26 -07002853 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002854 * Called to restore the state of the task into the stack that it's supposed to go into.
2855 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002856 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002857 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002858 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002859 * @return true if the task has been restored successfully.
2860 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002861 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2862 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002863 final ActivityStack currentStack = task.getStack();
2864 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002865 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002866 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002867 // Nothing else to do since it is already restored in the right stack.
2868 return true;
2869 }
2870 // Remove current stack association, so we can re-associate the task with the
2871 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002872 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002873 }
2874
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002875 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002876 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002877 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002878 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2879 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002880 final ArrayList<ActivityRecord> activities = task.mActivities;
2881 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002882 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002883 }
2884 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002885 }
2886
Winson Chung1dbc8112017-09-28 18:05:31 -07002887 @Override
2888 public void onRecentTaskAdded(TaskRecord task) {
2889 task.touchActiveTime();
2890 }
2891
2892 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07002893 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung14cfbb42017-10-20 13:54:39 -07002894 // TODO: Trim active task once b/68045330 is fixed
Winson Chung1dbc8112017-09-28 18:05:31 -07002895 task.removedFromRecents();
2896 }
2897
Wale Ogunwale040b4702015-08-06 18:10:50 -07002898 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002899 * Move stack with all its existing content to specified display.
2900 * @param stackId Id of stack to move.
2901 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002902 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002903 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002904 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002905 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002906 if (activityDisplay == null) {
2907 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2908 + displayId);
2909 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002910 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002911 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002912 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2913 + stackId);
2914 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002915
2916 final ActivityDisplay currentDisplay = stack.getDisplay();
2917 if (currentDisplay == null) {
2918 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
2919 + " is not attached to any display.");
2920 }
2921
2922 if (currentDisplay.mDisplayId == displayId) {
2923 throw new IllegalArgumentException("Trying to move stack=" + stack
2924 + " to its current displayId=" + displayId);
2925 }
2926
2927 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002928 // TODO(multi-display): resize stacks properly if moved from split-screen.
2929 }
2930
2931 /**
Winson Chung74666102017-02-22 17:49:24 -08002932 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
2933 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07002934 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002935 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002936 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002937 final int stackId = stack.mStackId;
2938 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07002939
Winson Chung74666102017-02-22 17:49:24 -08002940 // Check that we aren't reparenting to the same stack that the task is already in
2941 if (prevStack != null && prevStack.mStackId == stackId) {
2942 Slog.w(TAG, "Can not reparent to same stack, task=" + task
2943 + " already in stackId=" + stackId);
2944 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08002945 }
2946
Winson Chung74666102017-02-22 17:49:24 -08002947 // Ensure that we aren't trying to move into a multi-window stack without multi-window
2948 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002949 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08002950 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002951 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08002952 }
2953
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002954 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
2955 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002956 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07002957 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
2958 + " reparent task=" + task + " to stackId=" + stackId);
2959 }
2960
Winson Chung74666102017-02-22 17:49:24 -08002961 // Ensure that we aren't trying to move into a freeform stack without freeform
2962 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002963 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
2964 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08002965 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
2966 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08002967 }
2968
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002969 // Leave the task in its current stack or a fullscreen stack if it isn't resizeable and the
2970 // preferred stack is in multi-window mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002971 if (inMultiWindowMode && !task.isResizeable()) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002972 Slog.w(TAG, "Can not move unresizeable task=" + task + " to multi-window stack=" + stack
2973 + " Moving to a fullscreen stack instead.");
2974 if (prevStack != null) {
2975 return prevStack;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002976 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002977 stack = stack.getDisplay().createStack(
2978 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07002979 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002980 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002981 }
2982
Winson Chung08f81892017-03-02 15:40:51 -08002983 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002984 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07002985 if (stack == null) {
2986 throw new IllegalArgumentException(
2987 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
2988 }
2989
2990 final ActivityRecord r = stack.topRunningActivityLocked();
2991 if (r == null) {
2992 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
2993 + " in stack=" + stack);
2994 return false;
2995 }
2996
Wale Ogunwale6cae7652015-12-26 07:36:26 -08002997 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07002998 Slog.w(TAG,
2999 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003000 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003001 return false;
3002 }
3003
Winson Chung3a682872017-04-10 14:38:05 -07003004 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003005 "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003006 return true;
3007 }
3008
Winson Chung8bca9e42017-04-16 15:59:43 -07003009 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Wale Ogunwale66e16852017-10-19 13:35:52 -07003010 String reason) {
Winson Chung08f81892017-03-02 15:40:51 -08003011
Wale Ogunwale480dca02016-02-06 13:58:29 -08003012 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003013
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003014 final ActivityDisplay display = r.getStack().getDisplay();
3015 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003016
Bryce Lee04ab3462017-04-10 15:06:33 -07003017 // This will clear the pinned stack by moving an existing task to the full screen stack,
3018 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003019 if (stack != null) {
3020 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3021 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003022
Wale Ogunwale1666e312016-12-16 11:27:18 -08003023 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003024 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003025
Wale Ogunwale480dca02016-02-06 13:58:29 -08003026 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003027 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003028 // Resize the pinned stack to match the current size of the task the activity we are
3029 // going to be moving is currently contained in. We do this to have the right starting
3030 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003031 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003032 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003033 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003034
3035 if (task.mActivities.size() == 1) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003036 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003037 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3038 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003039 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003040 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003041 // reveal/leave the other activities in their original task.
3042
3043 // Currently, we don't support reparenting activities across tasks in two different
3044 // stacks, so instead, just create a new task in the same stack, reparent the
3045 // activity into that task, and then reparent the whole task to the new stack. This
3046 // ensures that all the necessary work to migrate states in the old and new stacks
3047 // is also done.
3048 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003049 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003050 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3051
Winson Chung5af42fc2017-03-24 17:11:33 -07003052 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003053 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003054 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003055 }
Winson Chungc2baac02017-01-11 13:34:47 -08003056
3057 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3058 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003059 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003060 } finally {
3061 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003062 }
3063
Winson Chunge7ba6862017-05-24 12:13:33 -07003064 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3065 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3066 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003067 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3068
Winson Chunge7ba6862017-05-24 12:13:33 -07003069 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3070 true /* fromFullscreen */);
3071
3072 // Update the visibility of all activities after the they have been reparented to the new
3073 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3074 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3075 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003076 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003077 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003078
Wale Ogunwale89be5762017-10-04 13:27:49 -07003079 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003080 }
3081
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003082 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003083 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3084 if (r == null || !r.isFocusable()) {
3085 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3086 "moveActivityStackToFront: unfocusable r=" + r);
3087 return false;
3088 }
3089
Bryce Leeaf691c02017-03-20 14:20:22 -07003090 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003091 final ActivityStack stack = r.getStack();
3092 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003093 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3094 + r + " task=" + task);
3095 return false;
3096 }
3097
Chong Zhang6cda19c2016-06-14 19:07:56 -07003098 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3099 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3100 "moveActivityStackToFront: already on top, r=" + r);
3101 return false;
3102 }
3103
3104 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3105 "moveActivityStackToFront: r=" + r);
3106
3107 stack.moveToFront(reason, task);
3108 return true;
3109 }
3110
David Stevensc6b91c62017-02-08 14:23:58 -08003111 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003112 mTmpFindTaskResult.r = null;
3113 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003114 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003115 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003117 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3118 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3119 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003120 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003121 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3122 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003123 continue;
3124 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003125 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003126 // It is possible to have tasks in multiple stacks with the same root affinity, so
3127 // we should keep looking after finding an affinity match to see if there is a
3128 // better match in another stack. Also, task affinity isn't a good enough reason
3129 // to target a display which isn't the source of the intent, so skip any affinity
3130 // matches not on the specified display.
3131 if (mTmpFindTaskResult.r != null) {
3132 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3133 return mTmpFindTaskResult.r;
3134 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003135 // Note: since the traversing through the stacks is top down, the floating
3136 // tasks should always have lower priority than any affinity-matching tasks
3137 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003138 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003139 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3140 Slog.d(TAG_TASKS, "Skipping match on different display "
3141 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003142 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003143 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003144 }
3145 }
Winson Chung5b895b72017-05-01 13:46:25 -07003146
David Stevensc6b91c62017-02-08 14:23:58 -08003147 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3148 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003149 }
3150
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003151 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003152 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003153 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003154 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3155 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3156 final ActivityStack stack = display.getChildAt(stackNdx);
3157 final ActivityRecord ar = stack.findActivityLocked(
3158 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003159 if (ar != null) {
3160 return ar;
3161 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003162 }
3163 }
3164 return null;
3165 }
3166
David Stevens9440dc82017-03-16 19:00:20 -07003167 boolean hasAwakeDisplay() {
3168 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3169 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3170 if (!display.shouldSleep()) {
3171 return true;
3172 }
3173 }
3174 return false;
3175 }
3176
Craig Mautner8d341ef2013-03-26 09:03:27 -07003177 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178 scheduleSleepTimeout();
3179 if (!mGoingToSleep.isHeld()) {
3180 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003181 if (mLaunchingActivity.isHeld()) {
3182 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3183 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003185 mLaunchingActivity.release();
3186 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003187 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003188 }
David Stevens9440dc82017-03-16 19:00:20 -07003189
3190 applySleepTokensLocked(false /* applyToStacks */);
3191
3192 checkReadyForSleepLocked(true /* allowDelay */);
3193 }
3194
3195 void prepareForShutdownLocked() {
3196 for (int i = 0; i < mActivityDisplays.size(); i++) {
3197 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3198 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 }
3200
3201 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003202 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003203
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003204 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003205 final long endTime = System.currentTimeMillis() + timeout;
3206 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003207 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003208 long timeRemaining = endTime - System.currentTimeMillis();
3209 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003210 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003211 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003212 } catch (InterruptedException e) {
3213 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003214 } else {
3215 Slog.w(TAG, "Activity manager shutdown timed out");
3216 timedout = true;
3217 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003218 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003219 } else {
3220 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003221 }
3222 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003223
3224 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003225 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003226
Craig Mautner8d341ef2013-03-26 09:03:27 -07003227 return timedout;
3228 }
3229
3230 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003231 removeSleepTimeouts();
3232 if (mGoingToSleep.isHeld()) {
3233 mGoingToSleep.release();
3234 }
David Stevens9440dc82017-03-16 19:00:20 -07003235 }
3236
3237 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003238 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003239 // Set the sleeping state of the display.
3240 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3241 final boolean displayShouldSleep = display.shouldSleep();
3242 if (displayShouldSleep == display.isSleeping()) {
3243 continue;
3244 }
3245 display.setIsSleeping(displayShouldSleep);
3246
3247 if (!applyToStacks) {
3248 continue;
3249 }
3250
3251 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003252 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3253 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003254 if (displayShouldSleep) {
3255 stack.goToSleepIfPossible(false /* shuttingDown */);
3256 } else {
3257 stack.awakeFromSleepingLocked();
3258 if (isFocusedStack(stack)) {
3259 resumeFocusedStackTopActivityLocked();
3260 }
3261 }
3262 }
3263
3264 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3265 continue;
3266 }
3267 // The display is awake now, so clean up the going to sleep list.
3268 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3269 final ActivityRecord r = it.next();
3270 if (r.getDisplayId() == display.mDisplayId) {
3271 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003272 }
Craig Mautner5314a402013-09-26 12:40:16 -07003273 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003274 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003275 }
3276
3277 void activitySleptLocked(ActivityRecord r) {
3278 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003279 final ActivityStack s = r.getStack();
3280 if (s != null) {
3281 s.checkReadyForSleep();
3282 } else {
3283 checkReadyForSleepLocked(true);
3284 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003285 }
3286
David Stevens9440dc82017-03-16 19:00:20 -07003287 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003288 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003289 // Do not care.
3290 return;
3291 }
3292
David Stevens18abd0e2017-08-17 14:55:47 -07003293 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3294 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003295 }
3296
Wei Wang65c7a152016-06-02 18:51:22 -07003297 // Send launch end powerhint before going sleep
3298 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3299
Craig Mautner0eea92c2013-05-16 13:35:39 -07003300 removeSleepTimeouts();
3301
3302 if (mGoingToSleep.isHeld()) {
3303 mGoingToSleep.release();
3304 }
3305 if (mService.mShuttingDown) {
3306 mService.notifyAll();
3307 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003308 }
3309
David Stevens18abd0e2017-08-17 14:55:47 -07003310 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3311 // successfully put to sleep.
3312 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3313 boolean allSleep = true;
3314 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003315 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3316 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3317 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003318 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003319 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003320 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003321 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003322 }
3323 }
3324 }
3325 return allSleep;
3326 }
3327
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003328 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003329 // A resumed activity cannot be stopping. remove from list
3330 mStoppingActivities.remove(r);
3331
Andrii Kulian02b7a832016-10-06 23:11:56 -07003332 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003333 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003334 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003335 }
3336 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003337 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003338 mWindowManager.executeAppTransition();
3339 return true;
3340 }
3341 return false;
3342 }
3343
Craig Mautner8d341ef2013-03-26 09:03:27 -07003344 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003345 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003346 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3347 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3348 final ActivityStack stack = display.getChildAt(stackNdx);
3349 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003350 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003351 }
3352 }
3353
Craig Mautnerbb742462014-07-07 15:28:55 -07003354 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003355 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003356 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003357 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003358
3359 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003360 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003361 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003362 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003363
3364 // When launching tasks behind, update the last active time of the top task after the new
3365 // task has been shown briefly
3366 final ActivityRecord top = stack.topActivity();
3367 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003368 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003369 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003370 }
3371
3372 void scheduleLaunchTaskBehindComplete(IBinder token) {
3373 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3374 }
3375
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003376 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3377 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003378 mKeyguardController.beginActivityVisibilityUpdate();
3379 try {
3380 // First the front stacks. In case any are not fullscreen and are in front of home.
3381 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003382 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3383 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3384 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003385 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3386 }
Craig Mautner580ea812013-04-25 12:58:38 -07003387 }
Jorim Jaggife762342016-10-13 14:33:27 +02003388 } finally {
3389 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003390 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003391 }
3392
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003393 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3394 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003395 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3396 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3397 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003398 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3399 }
3400 }
3401 }
3402
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003403 void invalidateTaskLayers() {
3404 mTaskLayersChanged = true;
3405 }
3406
3407 void rankTaskLayersIfNeeded() {
3408 if (!mTaskLayersChanged) {
3409 return;
3410 }
3411 mTaskLayersChanged = false;
3412 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003413 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003414 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003415 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3416 final ActivityStack stack = display.getChildAt(stackNdx);
3417 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003418 }
3419 }
3420 }
3421
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003422 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3423 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003424 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3425 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3426 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003427 stack.clearOtherAppTimeTrackers(except);
3428 }
3429 }
3430 }
3431
Craig Mautner8d341ef2013-03-26 09:03:27 -07003432 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003433 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003434 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3435 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3436 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003437 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003438 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 }
3440 }
3441
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003442 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3443 // Examine all activities currently running in the process.
3444 TaskRecord firstTask = null;
3445 // Tasks is non-null only if two or more tasks are found.
3446 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003447 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3448 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003449 ActivityRecord r = app.activities.get(i);
3450 // First, if we find an activity that is in the process of being destroyed,
3451 // then we just aren't going to do anything for now; we want things to settle
3452 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003453 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003454 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003455 return;
3456 }
3457 // Don't consider any activies that are currently not in a state where they
3458 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003459 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3460 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003461 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003462 continue;
3463 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003464
3465 final TaskRecord task = r.getTask();
3466 if (task != null) {
3467 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003468 + " from " + r);
3469 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003470 firstTask = task;
3471 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003472 if (tasks == null) {
3473 tasks = new ArraySet<>();
3474 tasks.add(firstTask);
3475 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003476 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003477 }
3478 }
3479 }
3480 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003481 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003482 return;
3483 }
3484 // If we have activities in multiple tasks that are in a position to be destroyed,
3485 // let's iterate through the tasks and release the oldest one.
3486 final int numDisplays = mActivityDisplays.size();
3487 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003488 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3489 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003490 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003491 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3492 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003493 // Try to release activities in this stack; if we manage to, we are done.
3494 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3495 return;
3496 }
3497 }
3498 }
3499 }
3500
Amith Yamasani37a40c22015-06-17 13:25:42 -07003501 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003502 final int focusStackId = mFocusedStack.getStackId();
3503 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003504 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003505 if (dockedStack != null) {
3506 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3507 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003508 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3509 // also cause all tasks to be moved to the fullscreen stack at a position that is
3510 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003511 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003512
3513 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003514 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003515 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003516
Craig Mautner858d8a62013-04-23 17:08:34 -07003517 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003519 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3520 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3521 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003522 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003523 TaskRecord task = stack.topTask();
3524 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003525 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003526 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003527 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003528 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003529
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003530 ActivityStack stack = getStack(restoreStackId);
3531 if (stack == null) {
3532 stack = mHomeStack;
3533 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003534 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003535 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003536 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003537 } else {
3538 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003539 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003540 }
Craig Mautner93529a42013-10-04 15:03:13 -07003541 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003542 }
3543
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003544 /** Checks whether the userid is a profile of the current user. */
3545 boolean isCurrentProfileLocked(int userId) {
3546 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003547 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003548 }
3549
Bryce Leeb7c9b802017-05-02 14:20:24 -07003550 /**
3551 * Returns whether a stopping activity is present that should be stopped after visible, rather
3552 * than idle.
3553 * @return {@code true} if such activity is present. {@code false} otherwise.
3554 */
3555 boolean isStoppingNoHistoryActivity() {
3556 // Activities that are marked as nohistory should be stopped immediately after the resumed
3557 // activity has become visible.
3558 for (ActivityRecord record : mStoppingActivities) {
3559 if (record.isNoHistory()) {
3560 return true;
3561 }
3562 }
3563
3564 return false;
3565 }
3566
Winson Chung4dabf232017-01-25 13:25:22 -08003567 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3568 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003569 ArrayList<ActivityRecord> stops = null;
3570
3571 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003572 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3573 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003574 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003575 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003576 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3577 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003578 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003579 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003580 if (s.finishing) {
3581 // If this activity is finishing, it is sitting on top of
3582 // everyone else but we now know it is no longer needed...
3583 // so get rid of it. Otherwise, we need to go through the
3584 // normal flow and hide it once we determine that it is
3585 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003586 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003587 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003588 }
3589 }
David Stevens9440dc82017-03-16 19:00:20 -07003590 if (remove) {
3591 final ActivityStack stack = s.getStack();
3592 final boolean shouldSleepOrShutDown = stack != null
3593 ? stack.shouldSleepOrShutDownActivities()
3594 : mService.isSleepingOrShuttingDownLocked();
3595 if (!waitingVisible || shouldSleepOrShutDown) {
3596 if (!processPausingActivities && s.state == PAUSING) {
3597 // Defer processing pausing activities in this iteration and reschedule
3598 // a delayed idle to reprocess it again
3599 removeTimeoutsForActivityLocked(idleActivity);
3600 scheduleIdleTimeoutLocked(idleActivity);
3601 continue;
3602 }
Winson Chung4dabf232017-01-25 13:25:22 -08003603
David Stevens9440dc82017-03-16 19:00:20 -07003604 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3605 if (stops == null) {
3606 stops = new ArrayList<>();
3607 }
3608 stops.add(s);
3609 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003610 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003611 }
3612 }
3613
3614 return stops;
3615 }
3616
Craig Mautnercf910b02013-04-23 11:23:27 -07003617 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003618 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003619 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3620 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3621 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003622 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003623 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003624 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003625 if (r == null) Slog.e(TAG,
3626 "validateTop...: null top activity, stack=" + stack);
3627 else {
3628 final ActivityRecord pausing = stack.mPausingActivity;
3629 if (pausing != null && pausing == r) Slog.e(TAG,
3630 "validateTop...: top stack has pausing activity r=" + r
3631 + " state=" + state);
3632 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3633 "validateTop...: activity in front not resumed r=" + r
3634 + " state=" + state);
3635 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003636 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003637 final ActivityRecord resumed = stack.mResumedActivity;
3638 if (resumed != null && resumed == r) Slog.e(TAG,
3639 "validateTop...: back stack has resumed activity r=" + r
3640 + " state=" + state);
3641 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3642 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003643 }
3644 }
3645 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003646 }
3647
Craig Mautner27084302013-03-25 08:05:25 -07003648 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003649 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003650 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003651 pw.print(prefix);
3652 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003653 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003654 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3655 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003656 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003657 }
Bryce Lee4a194382017-04-04 14:32:48 -07003658 if (!mWaitingForActivityVisible.isEmpty()) {
3659 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3660 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3661 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3662 }
3663 }
3664
Jorim Jaggi8d786932016-10-26 19:08:36 -07003665 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003666 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003667 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003668
Yi Jin129fc6c2017-09-28 15:48:38 -07003669 public void writeToProto(ProtoOutputStream proto) {
Steven Timotius4346f0a2017-09-12 11:07:21 -07003670 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3671 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3672 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3673 activityDisplay.writeToProto(proto, DISPLAYS);
3674 }
3675 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3676 if (mFocusedStack != null) {
3677 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3678 ActivityRecord focusedActivity = getResumedActivityLocked();
3679 if (focusedActivity != null) {
3680 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3681 }
3682 } else {
3683 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3684 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003685 }
3686
Winson43d1f262016-06-14 16:05:55 -07003687 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003688 * Dump all connected displays' configurations.
3689 * @param prefix Prefix to apply to each line of the dump.
3690 */
3691 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3692 pw.print(prefix); pw.println("Display override configurations:");
3693 final int displayCount = mActivityDisplays.size();
3694 for (int i = 0; i < displayCount; i++) {
3695 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3696 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3697 pw.println(activityDisplay.getOverrideConfiguration());
3698 }
3699 }
3700
3701 /**
Winson43d1f262016-06-14 16:05:55 -07003702 * Dumps the activities matching the given {@param name} in the either the focused stack
3703 * or all visible stacks if {@param dumpVisibleStacks} is true.
3704 */
Winson Chung6998bc42017-02-28 17:07:05 -08003705 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3706 boolean dumpFocusedStackOnly) {
3707 if (dumpFocusedStackOnly) {
3708 return mFocusedStack.getDumpActivitiesLocked(name);
3709 } else {
Winson43d1f262016-06-14 16:05:55 -07003710 ArrayList<ActivityRecord> activities = new ArrayList<>();
3711 int numDisplays = mActivityDisplays.size();
3712 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003713 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3714 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3715 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003716 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003717 activities.addAll(stack.getDumpActivitiesLocked(name));
3718 }
3719 }
3720 }
3721 return activities;
Winson43d1f262016-06-14 16:05:55 -07003722 }
Craig Mautner20e72272013-04-01 13:45:53 -07003723 }
3724
Dianne Hackborn390517b2013-05-30 15:03:32 -07003725 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3726 boolean needSep, String prefix) {
3727 if (activity != null) {
3728 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3729 if (needSep) {
3730 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003731 }
3732 pw.print(prefix);
3733 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003734 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003735 }
3736 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003737 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003738 }
3739
Craig Mautner8d341ef2013-03-26 09:03:27 -07003740 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3741 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003742 boolean printed = false;
3743 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003744 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3745 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003746 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003747 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003748 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3749 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3750 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003751 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003752 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003753 + ": type=" + activityTypeToString(stack.getActivityType())
3754 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003755 pw.println(" mFullscreen=" + stack.mFullscreen);
3756 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3757 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003758
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003759 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3760 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003761
Craig Mautner4a1cb222013-12-04 16:14:06 -08003762 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3763 !dumpAll, false, dumpPackage, true,
3764 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003765
Craig Mautner4a1cb222013-12-04 16:14:06 -08003766 needSep = printed;
3767 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3768 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003769 if (pr) {
3770 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003772 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3774 " mResumedActivity: ");
3775 if (pr) {
3776 printed = true;
3777 needSep = false;
3778 }
3779 if (dumpAll) {
3780 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3781 " mLastPausedActivity: ");
3782 if (pr) {
3783 printed = true;
3784 needSep = true;
3785 }
3786 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3787 needSep, " mLastNoHistoryActivity: ");
3788 }
3789 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003790 }
3791 }
3792
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003793 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3794 false, dumpPackage, true, " Activities waiting to finish:", null);
3795 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3796 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003797 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3798 false, !dumpAll, false, dumpPackage, true,
3799 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003800 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3801 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003802
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003804 }
3805
Dianne Hackborn390517b2013-05-30 15:03:32 -07003806 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003807 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003808 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003809 String innerPrefix = null;
3810 String[] args = null;
3811 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003812 for (int i=list.size()-1; i>=0; i--) {
3813 final ActivityRecord r = list.get(i);
3814 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3815 continue;
3816 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003817 if (innerPrefix == null) {
3818 innerPrefix = prefix + " ";
3819 args = new String[0];
3820 }
3821 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003822 final boolean full = !brief && (complete || !r.isInHistory());
3823 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003824 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003825 needNL = false;
3826 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003827 if (header != null) {
3828 pw.println(header);
3829 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003830 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003831 if (lastTask != r.getTask()) {
3832 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003833 pw.print(prefix);
3834 pw.print(full ? "* " : " ");
3835 pw.println(lastTask);
3836 if (full) {
3837 lastTask.dump(pw, prefix + " ");
3838 } else if (complete) {
3839 // Complete + brief == give a summary. Isn't that obvious?!?
3840 if (lastTask.intent != null) {
3841 pw.print(prefix); pw.print(" ");
3842 pw.println(lastTask.intent.toInsecureStringWithClip());
3843 }
3844 }
3845 }
3846 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3847 pw.print(" #"); pw.print(i); pw.print(": ");
3848 pw.println(r);
3849 if (full) {
3850 r.dump(pw, innerPrefix);
3851 } else if (complete) {
3852 // Complete + brief == give a summary. Isn't that obvious?!?
3853 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3854 if (r.app != null) {
3855 pw.print(innerPrefix); pw.println(r.app);
3856 }
3857 }
3858 if (client && r.app != null && r.app.thread != null) {
3859 // flush anything that is already in the PrintWriter since the thread is going
3860 // to write to the file descriptor directly
3861 pw.flush();
3862 try {
3863 TransferPipe tp = new TransferPipe();
3864 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003865 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003866 // Short timeout, since blocking here can
3867 // deadlock with the application.
3868 tp.go(fd, 2000);
3869 } finally {
3870 tp.kill();
3871 }
3872 } catch (IOException e) {
3873 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3874 } catch (RemoteException e) {
3875 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3876 }
3877 needNL = true;
3878 }
3879 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003880 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003881 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003882
Craig Mautnerf3333272013-04-22 10:55:53 -07003883 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003884 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3885 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003886 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3887 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003888 }
3889
3890 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003891 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003892 }
3893
3894 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003895 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3896 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003897 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3898 }
3899
Craig Mautner05d29032013-05-03 13:40:13 -07003900 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003901 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3902 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3903 }
Craig Mautner05d29032013-05-03 13:40:13 -07003904 }
3905
Craig Mautner0eea92c2013-05-16 13:35:39 -07003906 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003907 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3908 }
3909
3910 final void scheduleSleepTimeout() {
3911 removeSleepTimeouts();
3912 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3913 }
3914
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 @Override
3916 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003917 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003918 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3919 }
3920
3921 @Override
3922 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003923 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3925 }
3926
3927 @Override
3928 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003929 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3931 }
3932
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003933 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003935 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 }
3938
Andrii Kulianca007a62016-11-22 16:33:28 -08003939 /** Check if display with specified id is added to the list. */
3940 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003941 return getActivityDisplayOrCreateLocked(displayId) != null;
3942 }
3943
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003944 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003945 ActivityDisplay getActivityDisplay(int displayId) {
3946 return mActivityDisplays.get(displayId);
3947 }
3948
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003949 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
3950 ActivityDisplay getDefaultDisplay() {
3951 return mActivityDisplays.get(DEFAULT_DISPLAY);
3952 }
3953
Andrii Kulian62e6f252017-05-30 22:46:53 -07003954 /**
3955 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
3956 * corresponding record in display manager.
3957 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003958 // TODO: Look into consolidating with getActivityDisplay()
3959 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003960 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3961 if (activityDisplay != null) {
3962 return activityDisplay;
3963 }
3964 if (mDisplayManager == null) {
3965 // The system isn't fully initialized yet.
3966 return null;
3967 }
3968 final Display display = mDisplayManager.getDisplay(displayId);
3969 if (display == null) {
3970 // The display is not registered in DisplayManager.
3971 return null;
3972 }
3973 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003974 activityDisplay = new ActivityDisplay(this, displayId);
3975 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07003976 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
3977 mWindowManager.onDisplayAdded(displayId);
3978 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08003979 }
3980
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003981 @VisibleForTesting
3982 void attachDisplay(ActivityDisplay display) {
3983 mActivityDisplays.put(display.mDisplayId, display);
3984 }
3985
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003986 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07003987 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07003988 mService.mContext.getResources().getDimensionPixelSize(
3989 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003990 }
3991
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003992 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08003993 if (displayId == DEFAULT_DISPLAY) {
3994 throw new IllegalArgumentException("Can't remove the primary display.");
3995 }
3996
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003998 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3999 if (activityDisplay == null) {
4000 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004002 final boolean destroyContentOnRemoval
4003 = activityDisplay.shouldDestroyContentOnRemove();
4004 while (activityDisplay.getChildCount() > 0) {
4005 final ActivityStack stack = activityDisplay.getChildAt(0);
4006 if (destroyContentOnRemoval) {
4007 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4008 stack.finishAllActivitiesLocked(true /* immediately */);
4009 } else {
4010 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4011 // a valid launch stack for all activities. This way the task history from
4012 // external display will be preserved on primary after move.
4013 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4014 }
4015 }
4016
4017 releaseSleepTokens(activityDisplay);
4018
4019 mActivityDisplays.remove(displayId);
4020 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004021 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004022 }
4023
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004024 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004025 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004026 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4027 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004028 // The window policy is responsible for stopping activities on the default display
4029 if (displayId != Display.DEFAULT_DISPLAY) {
4030 int displayState = activityDisplay.mDisplay.getState();
4031 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4032 activityDisplay.mOffToken =
4033 mService.acquireSleepToken("Display-off", displayId);
4034 } else if (displayState == Display.STATE_ON
4035 && activityDisplay.mOffToken != null) {
4036 activityDisplay.mOffToken.release();
4037 activityDisplay.mOffToken = null;
4038 }
4039 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 // TODO: Update the bounds.
4041 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004042 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004044 }
4045
David Stevens9440dc82017-03-16 19:00:20 -07004046 SleepToken createSleepTokenLocked(String tag, int displayId) {
4047 ActivityDisplay display = mActivityDisplays.get(displayId);
4048 if (display == null) {
4049 throw new IllegalArgumentException("Invalid display: " + displayId);
4050 }
4051
4052 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4053 mSleepTokens.add(token);
4054 display.mAllSleepTokens.add(token);
4055 return token;
4056 }
4057
4058 private void removeSleepTokenLocked(SleepTokenImpl token) {
4059 mSleepTokens.remove(token);
4060
4061 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4062 if (display != null) {
4063 display.mAllSleepTokens.remove(token);
4064 if (display.mAllSleepTokens.isEmpty()) {
4065 mService.updateSleepIfNeededLocked();
4066 }
4067 }
4068 }
4069
4070 private void releaseSleepTokens(ActivityDisplay display) {
4071 if (display.mAllSleepTokens.isEmpty()) {
4072 return;
4073 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004074 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004075 mSleepTokens.remove(token);
4076 }
4077 display.mAllSleepTokens.clear();
4078
4079 mService.updateSleepIfNeededLocked();
4080 }
4081
Wale Ogunwale68278562017-09-23 17:13:55 -07004082 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004083 final int displayId = stack.mDisplayId;
4084 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004085 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004086 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004087 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004089 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004090 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004091 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004092 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004093 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094
4095 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4096 final int numTasks = tasks.size();
4097 int[] taskIds = new int[numTasks];
4098 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004099 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004100 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004101 for (int i = 0; i < numTasks; ++i) {
4102 final TaskRecord task = tasks.get(i);
4103 taskIds[i] = task.taskId;
4104 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4105 : task.realActivity != null ? task.realActivity.flattenToString()
4106 : task.getTopActivity() != null ? task.getTopActivity().packageName
4107 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004108 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004109 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004110 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 }
4112 info.taskIds = taskIds;
4113 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004114 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004115 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004116
4117 final ActivityRecord top = stack.topRunningActivityLocked();
4118 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 return info;
4120 }
4121
Wale Ogunwale68278562017-09-23 17:13:55 -07004122 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004123 ActivityStack stack = getStack(stackId);
4124 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004125 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004126 }
4127 return null;
4128 }
4129
Wale Ogunwale68278562017-09-23 17:13:55 -07004130 StackInfo getStackInfo(int windowingMode, int activityType) {
4131 final ActivityStack stack = getStack(windowingMode, activityType);
4132 return (stack != null) ? getStackInfo(stack) : null;
4133 }
4134
Craig Mautner4a1cb222013-12-04 16:14:06 -08004135 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004136 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004137 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004138 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4139 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4140 final ActivityStack stack = display.getChildAt(stackNdx);
4141 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004142 }
4143 }
4144 return list;
4145 }
4146
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004147 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004148 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004149 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004150 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004151 }
4152
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004153 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004154 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004155 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004156 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004157 final boolean inSplitScreenMode = actualStack != null
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004158 && actualStack.getDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004159 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07004160 && !isSecondaryDisplayPreferred) || !task.isActivityTypeStandardOrUndefined()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004161 return;
4162 }
4163
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004164 // Handle incorrect launch/move to secondary display if needed.
4165 final boolean launchOnSecondaryDisplayFailed;
4166 if (isSecondaryDisplayPreferred) {
4167 final int actualDisplayId = task.getStack().mDisplayId;
4168 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4169 // The task landed on an inappropriate display somehow, move it to the default
4170 // display.
4171 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004172 mService.setTaskWindowingMode(task.taskId,
4173 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004174 launchOnSecondaryDisplayFailed = true;
4175 } else {
4176 // The task might have landed on a display different from requested.
4177 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4178 || (preferredDisplayId != INVALID_DISPLAY
4179 && preferredDisplayId != actualDisplayId);
4180 }
4181 } else {
4182 // The task wasn't requested to be on a secondary display.
4183 launchOnSecondaryDisplayFailed = false;
4184 }
4185
Jorim Jaggicd13d332016-04-27 15:40:20 -07004186 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004187 if (launchOnSecondaryDisplayFailed
4188 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004189 if (launchOnSecondaryDisplayFailed) {
4190 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4191 // display with config different from global config.
4192 mService.mTaskChangeNotificationController
4193 .notifyActivityLaunchOnSecondaryDisplayFailed();
4194 } else {
4195 // Display a warning toast that we tried to put a non-dockable task in the docked
4196 // stack.
4197 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4198 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004199
Andrii Kulianc27916642016-04-12 17:59:27 -07004200 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4201 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004202
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004203 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004204 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004205 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004206 }
Winson Chungd3395382016-12-13 11:49:09 -08004207 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004208 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004209 final String packageName = topActivity.appInfo.packageName;
4210 final int reason = isSecondaryDisplayPreferred
4211 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4212 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004213 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004214 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004215 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004216 }
4217
Jorim Jaggife89d122015-12-22 16:28:44 +01004218 void activityRelaunchedLocked(IBinder token) {
4219 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004220 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4221 if (r != null) {
4222 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004223 r.setSleeping(true, true);
4224 }
4225 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004226 }
4227
4228 void activityRelaunchingLocked(ActivityRecord r) {
4229 mWindowManager.notifyAppRelaunching(r.appToken);
4230 }
4231
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004232 void logStackState() {
4233 mActivityMetricsLogger.logWindowState();
4234 }
4235
Winson Chung5af42fc2017-03-24 17:11:33 -07004236 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004237 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4238 // end, then ensure that we defer all in between multi-window mode changes
4239 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4240 return;
4241 }
4242
Wale Ogunwale22e25262016-02-01 10:32:02 -08004243 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4244 final ActivityRecord r = task.mActivities.get(i);
4245 if (r.app != null && r.app.thread != null) {
4246 mMultiWindowModeChangedActivities.add(r);
4247 }
4248 }
4249
4250 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4251 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4252 }
4253 }
4254
Winson Chung5af42fc2017-03-24 17:11:33 -07004255 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004256 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004257 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004258 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004259 return;
4260 }
4261
Winson Chungab76bbc2017-08-14 13:33:51 -07004262 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004263 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004264
Winson Chungab76bbc2017-08-14 13:33:51 -07004265 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4266 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4267 final ActivityRecord r = task.mActivities.get(i);
4268 if (r.app != null && r.app.thread != null) {
4269 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004270 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004271 }
4272 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004273
Winson Chungab76bbc2017-08-14 13:33:51 -07004274 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4275 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4276 }
4277 }
4278
4279 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4280 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4281 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4282 final ActivityRecord r = task.mActivities.get(i);
4283 if (r.app != null && r.app.thread != null) {
4284 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004285 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004286 }
4287 }
4288
Tony Mak853304c2016-04-18 15:17:41 +01004289 void setDockedStackMinimized(boolean minimized) {
4290 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004291 }
4292
chaviw59b98852017-06-13 12:05:44 -07004293 void wakeUp(String reason) {
4294 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4295 }
4296
4297 /**
4298 * Begin deferring resume to avoid duplicate resumes in one pass.
4299 */
4300 private void beginDeferResume() {
4301 mDeferResumeCount++;
4302 }
4303
4304 /**
4305 * End deferring resume and determine if resume can be called.
4306 */
4307 private void endDeferResume() {
4308 mDeferResumeCount--;
4309 }
4310
4311 /**
4312 * @return True if resume can be called.
4313 */
4314 private boolean readyToResume() {
4315 return mDeferResumeCount == 0;
4316 }
4317
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004318 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004319
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004320 public ActivityStackSupervisorHandler(Looper looper) {
4321 super(looper);
4322 }
4323
Winson Chung4dabf232017-01-25 13:25:22 -08004324 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004325 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004326 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4327 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004328 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004329 }
4330
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004331 @Override
4332 public void handleMessage(Message msg) {
4333 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004334 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4335 synchronized (mService) {
4336 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4337 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004338 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004339 }
4340 }
4341 } break;
4342 case REPORT_PIP_MODE_CHANGED_MSG: {
4343 synchronized (mService) {
4344 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4345 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004346 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4347 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004348 }
4349 }
4350 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004351 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004352 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4353 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004354 // We don't at this point know if the activity is fullscreen,
4355 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004356 activityIdleInternal((ActivityRecord) msg.obj,
4357 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004358 } break;
4359 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004360 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004361 activityIdleInternal((ActivityRecord) msg.obj,
4362 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004363 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004364 case RESUME_TOP_ACTIVITY_MSG: {
4365 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004366 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004367 }
4368 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004369 case SLEEP_TIMEOUT_MSG: {
4370 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004371 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004372 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004373 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004374 }
4375 }
4376 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004377 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004378 synchronized (mService) {
4379 if (mLaunchingActivity.isHeld()) {
4380 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4381 if (VALIDATE_WAKE_LOCK_CALLER
4382 && Binder.getCallingUid() != Process.myUid()) {
4383 throw new IllegalStateException("Calling must be system uid");
4384 }
4385 mLaunchingActivity.release();
4386 }
4387 }
4388 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004389 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004390 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004391 } break;
4392 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004393 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004394 } break;
4395 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004396 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004397 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004398 case LAUNCH_TASK_BEHIND_COMPLETE: {
4399 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004400 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004401 if (r != null) {
4402 handleLaunchTaskBehindCompleteLocked(r);
4403 }
4404 }
4405 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004406
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004407 }
4408 }
4409 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004410
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004411 ActivityStack findStackBehind(ActivityStack stack) {
4412 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004413 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004414 if (display == null) {
4415 return null;
4416 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004417 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4418 if (display.getChildAt(i) == stack && i > 0) {
4419 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004420 }
4421 }
4422 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004423 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004424 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004425
Jorim Jaggic69bd222016-03-15 14:38:37 +01004426 /**
4427 * Puts a task into resizing mode during the next app transition.
4428 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004429 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004430 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004431 private void setResizingDuringAnimation(TaskRecord task) {
4432 mResizingTasksDuringAnimation.add(task.taskId);
4433 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004434 }
4435
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004436 int startActivityFromRecents(int taskId, Bundle bOptions) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004437 final TaskRecord task;
4438 final int callingUid;
4439 final String callingPackage;
4440 final Intent intent;
4441 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004442 int activityType = ACTIVITY_TYPE_UNDEFINED;
4443 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004444 final ActivityOptions activityOptions = (bOptions != null)
4445 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004446 if (activityOptions != null) {
4447 activityType = activityOptions.getLaunchActivityType();
4448 windowingMode = activityOptions.getLaunchWindowingMode();
4449 }
4450 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004451 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004452 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004453 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004454
Matthew Ng606dd802017-06-05 14:06:32 -07004455 mWindowManager.deferSurfaceLayout();
4456 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004457 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004458 mWindowManager.setDockedStackCreateState(
4459 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004460
Matthew Ng606dd802017-06-05 14:06:32 -07004461 // Defer updating the stack in which recents is until the app transition is done, to
4462 // not run into issues where we still need to draw the task in recents but the
4463 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004464 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004465 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004466 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004467
Matthew Ng606dd802017-06-05 14:06:32 -07004468 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004469 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004470 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004471 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004472 mWindowManager.executeAppTransition();
4473 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004474 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004475 }
4476
Wale Ogunwale66e16852017-10-19 13:35:52 -07004477 // We always want to return to the home activity instead of the recents activity from
4478 // whatever is started from the recents activity, so move the home stack forward.
4479 moveHomeStackToFront("startActivityFromRecents");
4480
Matthew Ng606dd802017-06-05 14:06:32 -07004481 // If the user must confirm credentials (e.g. when first launching a work app and the
4482 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4483 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4484 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004485 final ActivityRecord targetActivity = task.getTopActivity();
4486
4487 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4488 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004489 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004490 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004491 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004492 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004493
4494 // If we are launching the task in the docked stack, put it into resizing mode so
4495 // the window renders full-screen with the background filling the void. Also only
4496 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004497 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004498 setResizingDuringAnimation(task);
4499 }
4500
4501 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004502 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004503 return ActivityManager.START_TASK_TO_FRONT;
4504 }
4505 callingUid = task.mCallingUid;
4506 callingPackage = task.mCallingPackage;
4507 intent = task.intent;
4508 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4509 userId = task.userId;
4510 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004511 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004512 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004513 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004514 }
Matthew Ng606dd802017-06-05 14:06:32 -07004515 return result;
4516 } finally {
4517 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004518 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004519 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004520
4521 /**
4522 * @return a list of activities which are the top ones in each visible stack. The first
4523 * entry will be the focused activity.
4524 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004525 List<IBinder> getTopVisibleActivities() {
4526 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4527 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004528 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004529 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4530 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004531 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4532 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004533 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004534 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004535 final ActivityRecord top = stack.topActivity();
4536 if (top != null) {
4537 if (stack == mFocusedStack) {
4538 topActivityTokens.add(0, top.appToken);
4539 } else {
4540 topActivityTokens.add(top.appToken);
4541 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004542 }
4543 }
4544 }
4545 }
4546 return topActivityTokens;
4547 }
Bryce Lee4a194382017-04-04 14:32:48 -07004548
4549 /**
4550 * Internal container to store a match qualifier alongside a WaitResult.
4551 */
4552 static class WaitInfo {
4553 private final ComponentName mTargetComponent;
4554 private final WaitResult mResult;
4555
4556 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4557 this.mTargetComponent = targetComponent;
4558 this.mResult = result;
4559 }
4560
Wale Ogunwale3270f172017-04-26 07:29:42 -07004561 public boolean matches(ComponentName targetComponent) {
4562 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004563 }
4564
4565 public WaitResult getResult() {
4566 return mResult;
4567 }
4568
4569 public ComponentName getComponent() {
4570 return mTargetComponent;
4571 }
4572
4573 public void dump(PrintWriter pw, String prefix) {
4574 pw.println(prefix + "WaitInfo:");
4575 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4576 pw.println(prefix + " mResult=");
4577 mResult.dump(pw, prefix);
4578 }
4579 }
David Stevens9440dc82017-03-16 19:00:20 -07004580
4581 private final class SleepTokenImpl extends SleepToken {
4582 private final String mTag;
4583 private final long mAcquireTime;
4584 private final int mDisplayId;
4585
4586 public SleepTokenImpl(String tag, int displayId) {
4587 mTag = tag;
4588 mDisplayId = displayId;
4589 mAcquireTime = SystemClock.uptimeMillis();
4590 }
4591
4592 @Override
4593 public void release() {
4594 synchronized (mService) {
4595 removeSleepTokenLocked(this);
4596 }
4597 }
4598
4599 @Override
4600 public String toString() {
4601 return "{\"" + mTag + "\", display " + mDisplayId
4602 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4603 }
4604 }
4605
Craig Mautner27084302013-03-25 08:05:25 -07004606}