blob: c5065f1084ce2ca588df727b79578471cf7ade18 [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;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700113import android.app.WindowConfiguration;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700115import android.content.Context;
116import android.content.Intent;
117import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700119import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120import android.content.pm.PackageManager;
121import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100122import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800124import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800125import android.hardware.display.DisplayManager;
126import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800127import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800128import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100130import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700131import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700132import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700134import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700135import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700136import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700137import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700138import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700139import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700140import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700141import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100142import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700143import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700144import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700145import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700146import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700147import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700148import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800149import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700150import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700151import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800152import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800153import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700154import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700155import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800156import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800157
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700158import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800159import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800160import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700161import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700162import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800163import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700164import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700165import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700166import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700168
Craig Mautner8d341ef2013-03-26 09:03:27 -0700169import java.io.FileDescriptor;
170import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700171import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800172import java.lang.annotation.Retention;
173import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700174import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700175import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700176import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700177import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700178
Winson Chung1dbc8112017-09-28 18:05:31 -0700179public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,
180 RecentTasks.Callbacks {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800181 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700182 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700183 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700184 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800190 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800191
Craig Mautnerf3333272013-04-22 10:55:53 -0700192 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700193 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700194
Craig Mautner0eea92c2013-05-16 13:35:39 -0700195 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700198 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700200
Craig Mautner05d29032013-05-03 13:40:13 -0700201 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
202 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
203 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700204 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700205 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800206 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
207 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
208 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700209 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800210 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
211 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800212
Wale Ogunwale040b4702015-08-06 18:10:50 -0700213 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 // Used to indicate if an object (e.g. stack) that we are trying to get
216 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800217 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700219 // Used to indicate that windows of activities should be preserved during the resize.
220 static final boolean PRESERVE_WINDOWS = true;
221
Wale Ogunwale040b4702015-08-06 18:10:50 -0700222 // Used to indicate if an object (e.g. task) should be moved/created
223 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700224 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225
226 // Used to indicate that an objects (e.g. task) removal from its container
227 // (e.g. stack) is due to it moving to another container.
228 static final boolean MOVING = true;
229
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700230 // Force the focus to change to the stack we are moving a task to..
231 static final boolean FORCE_FOCUS = true;
232
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700233 // Don't execute any calls to resume.
234 static final boolean DEFER_RESUME = true;
235
Winson Chung010927a2016-12-15 16:12:35 -0800236 // Used to indicate that a task is removed it should also be removed from recents.
237 static final boolean REMOVE_FROM_RECENTS = true;
238
Winson Chung6954fc92017-03-24 16:22:12 -0700239 // Used to indicate that pausing an activity should occur immediately without waiting for
240 // the activity callback indicating that it has completed pausing
241 static final boolean PAUSE_IMMEDIATELY = true;
242
Winson Chung7900bee2017-03-10 08:59:25 -0800243 /**
244 * The modes which affect which tasks are returned when calling
245 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
246 */
247 @Retention(RetentionPolicy.SOURCE)
248 @IntDef({
249 MATCH_TASK_IN_STACKS_ONLY,
250 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
251 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
252 })
253 public @interface AnyTaskForIdMatchTaskMode {}
254 // Match only tasks in the current stacks
255 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
256 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
257 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
258 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
259 // provided stack id
260 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
261
Svetoslav7008b512015-06-24 18:47:07 -0700262 // Activity actions an app cannot start if it uses a permission which is not granted.
263 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
264 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700265
Svetoslav7008b512015-06-24 18:47:07 -0700266 static {
267 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
268 Manifest.permission.CAMERA);
269 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
270 Manifest.permission.CAMERA);
271 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
272 Manifest.permission.CALL_PHONE);
273 }
274
Svet Ganov99b60432015-06-27 13:15:22 -0700275 /** Action restriction: launching the activity is not restricted. */
276 private static final int ACTIVITY_RESTRICTION_NONE = 0;
277 /** Action restriction: launching the activity is restricted by a permission. */
278 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
279 /** Action restriction: launching the activity is restricted by an app op. */
280 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700281
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700282 // For debugging to make sure the caller when acquiring/releasing our
283 // wake lock is the system process.
284 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800285 /** The number of distinct task ids that can be assigned to the tasks of a single user */
286 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Craig Mautner27084302013-03-25 08:05:25 -0700288 final ActivityManagerService mService;
289
Winson Chung1dbc8112017-09-28 18:05:31 -0700290 RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800291
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700292 final ActivityStackSupervisorHandler mHandler;
293
294 /** Short cut */
295 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700297
Bryce Leedacefc42017-10-10 12:56:02 -0700298 private final LaunchingBoundsController mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700299
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700300 /** Counter for next free stack ID to use for dynamic activity stacks. */
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700301 private int mNextFreeStackId = 0;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800303 /**
304 * Maps the task identifier that activities are currently being started in to the userId of the
305 * task. Each time a new task is created, the entry for the userId of the task is incremented
306 */
307 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700308
Craig Mautner2420ead2013-04-01 17:13:20 -0700309 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack containing the launcher app. Assumed to always be attached to
313 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700315
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700318
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
320 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800321 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
324 /** List of activities that are waiting for a new activity to become visible before completing
325 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700326 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
327 // mStoppingActivities when something else comes up.
328 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Bryce Lee4a194382017-04-04 14:32:48 -0700330 /** List of processes waiting to find out when a specific activity becomes visible. */
331 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700332
333 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700334 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700335
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336 /** List of activities that are ready to be stopped, but waiting for the next activity to
337 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700339
Craig Mautnerf3333272013-04-22 10:55:53 -0700340 /** List of activities that are ready to be finished, but waiting for the previous activity to
341 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800342 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700343
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800345 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Wale Ogunwale22e25262016-02-01 10:32:02 -0800347 /** List of activities whose multi-window mode changed that we need to report to the
348 * application */
349 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
350
351 /** List of activities whose picture-in-picture mode changed that we need to report to the
352 * application */
353 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
354
Winson Chung5af42fc2017-03-24 17:11:33 -0700355 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
356 * the application */
357 Rect mPipModeChangedTargetStackBounds;
358
Craig Mautnerf3333272013-04-22 10:55:53 -0700359 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700360 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700361
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
363 * is being brought in front of us. */
364 boolean mUserLeaving = false;
365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367 * We don't want to allow the device to go to sleep while in the process
368 * of launching an activity. This is primarily to allow alarm intent
369 * receivers to launch an activity and get that to run before the device
370 * goes back to sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700373
374 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700375 * Set when the system is going to sleep, until we have
376 * successfully paused the current activity and released our wake lock.
377 * At that point the system is allowed to actually sleep.
378 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700379 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700380
David Stevens9440dc82017-03-16 19:00:20 -0700381 /**
382 * A list of tokens that cause the top activity to be put to sleep.
383 * They are used by components that may hide and block interaction with underlying
384 * activities.
385 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700386 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700387
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700388 /** Stack id of the front stack when user switched, indexed by userId. */
389 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700390
Bryce Leeaf3a4002017-03-20 10:37:12 -0700391 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700393 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800395 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
396
397 private DisplayManagerInternal mDisplayManagerInternal;
398 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Andrii Kulian1e32e022016-09-16 15:29:34 -0700403 /**
404 * Temporary rect used during docked stack resize calculation so we don't need to create a new
405 * object each time.
406 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700408
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409 // The default minimal size that will be used if the activity doesn't specify its minimal size.
410 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700411 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700413 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
414 private boolean mTaskLayersChanged = true;
415
Jorim Jaggi275561a2016-02-23 10:11:02 -0500416 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800417
Jorim Jaggiea039a82017-08-02 14:37:49 +0200418 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
419
Andrii Kulian1779e612016-10-12 21:58:25 -0700420 @Override
421 protected int getChildCount() {
422 return mActivityDisplays.size();
423 }
424
425 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700426 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700427 return mActivityDisplays.valueAt(index);
428 }
429
430 @Override
431 protected ConfigurationContainer getParent() {
432 return null;
433 }
434
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700435 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700436 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700437 if (activityDisplay == null) {
438 throw new IllegalArgumentException("No display found with id: " + displayId);
439 }
440
441 return activityDisplay.getOverrideConfiguration();
442 }
443
444 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700445 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700446 if (activityDisplay == null) {
447 throw new IllegalArgumentException("No display found with id: " + displayId);
448 }
449
450 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
451 }
452
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700453 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700454 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
455 int callingUid, ActivityInfo activityInfo) {
456 if (displayId == DEFAULT_DISPLAY) {
457 // No restrictions for the default display.
458 return true;
459 }
460 if (!mService.mSupportsMultiDisplay) {
461 // Can't launch on secondary displays if feature is not supported.
462 return false;
463 }
464 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
465 // Can't apply wrong configuration to non-resizeable activities.
466 return false;
467 }
468 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
469 // Can't place activities to a display that has restricted launch rules.
470 // In this case the request should be made by explicitly adding target display id and
471 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
472 return false;
473 }
474 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700475 }
476
477 /**
478 * Check if configuration of specified display matches current global config.
479 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
480 * the same config as on the default display.
481 * @param displayId Id of the display to check.
482 * @return {@code true} if configuration matches.
483 */
484 private boolean displayConfigMatchesGlobal(int displayId) {
485 if (displayId == DEFAULT_DISPLAY) {
486 return true;
487 }
488 if (displayId == INVALID_DISPLAY) {
489 return false;
490 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700491 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700492 if (targetDisplay == null) {
493 throw new IllegalArgumentException("No display found with id: " + displayId);
494 }
495 return getConfiguration().equals(targetDisplay.getConfiguration());
496 }
497
Wale Ogunwale39381972015-12-17 17:15:29 -0800498 static class FindTaskResult {
499 ActivityRecord r;
500 boolean matchedByRootAffinity;
501 }
502 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
503
Craig Mautneree36c772014-07-16 14:56:05 -0700504 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800505 * Temp storage for display ids sorted in focus order.
506 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
507 * it's more efficient, as the number of displays is usually small.
508 */
509 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
510
511 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100512 * Used to keep track whether app visibilities got changed since the last pause. Useful to
513 * determine whether to invoke the task stack change listener after pausing.
514 */
515 boolean mAppVisibilitiesChangedSinceLastPause;
516
517 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100518 * Set of tasks that are in resizing mode during an app transition to fill the "void".
519 */
520 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
521
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700522
523 /**
524 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
525 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
526 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
527 * like the docked stack going empty.
528 */
529 private boolean mAllowDockedStackResize = true;
530
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100531 /**
Tony Mak853304c2016-04-18 15:17:41 +0100532 * Is dock currently minimized.
533 */
534 boolean mIsDockMinimized;
535
Jorim Jaggife762342016-10-13 14:33:27 +0200536 final KeyguardController mKeyguardController;
537
chaviw59b98852017-06-13 12:05:44 -0700538 private PowerManager mPowerManager;
539 private int mDeferResumeCount;
540
Tony Mak853304c2016-04-18 15:17:41 +0100541 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700542 * Description of a request to start a new activity, which has been held
543 * due to app switches being disabled.
544 */
545 static class PendingActivityLaunch {
546 final ActivityRecord r;
547 final ActivityRecord sourceRecord;
548 final int startFlags;
549 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700550 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700551
552 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700553 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700554 r = _r;
555 sourceRecord = _sourceRecord;
556 startFlags = _startFlags;
557 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700558 callerApp = _callerApp;
559 }
560
561 void sendErrorResult(String message) {
562 try {
563 if (callerApp.thread != null) {
564 callerApp.thread.scheduleCrash(message);
565 }
566 } catch (RemoteException e) {
567 Slog.e(TAG, "Exception scheduling crash of failed "
568 + "activity launcher sourceRecord=" + sourceRecord, e);
569 }
Craig Mautneree36c772014-07-16 14:56:05 -0700570 }
571 }
572
Bryce Leeaf691c02017-03-20 14:20:22 -0700573 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700574 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700575 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800576 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200577 mKeyguardController = new KeyguardController(service, this);
Bryce Leedacefc42017-10-10 12:56:02 -0700578
579 mLaunchingBoundsController = new LaunchingBoundsController();
580 mLaunchingBoundsController.registerDefaultPositioners(this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700581 }
582
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800583 void setRecentTasks(RecentTasks recentTasks) {
584 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700585 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800586 }
587
Jeff Brown2c43c332014-06-12 22:38:59 -0700588 /**
589 * At the time when the constructor runs, the power manager has not yet been
590 * initialized. So we initialize our wakelocks afterwards.
591 */
592 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700593 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
594 mGoingToSleep = mPowerManager
595 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
596 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700597 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700598 }
599
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700600 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 synchronized (mService) {
602 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200603 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604
605 mDisplayManager =
606 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
607 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800608 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609
610 Display[] displays = mDisplayManager.getDisplays();
611 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
612 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700613 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800614 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700615 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 }
617
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700618 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
619 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800620
621 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 }
Craig Mautner27084302013-03-25 08:05:25 -0700623 }
624
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700625 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700627 }
628
Craig Mautnerde4ef022013-04-07 19:01:33 -0700629 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700631 }
632
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700633 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700634 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700635 }
636
Wale Ogunwaled046a012015-12-24 13:05:59 -0800637 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800638 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
639 if (!focusCandidate.isFocusable()) {
640 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800641 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800642 }
643
644 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800645 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800646 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800647
Wale Ogunwaled046a012015-12-24 13:05:59 -0800648 EventLogTags.writeAmFocusedStack(
649 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
650 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
651 }
652
653 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800654 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800655 if (r != null && r.idle) {
656 checkFinishBootingLocked();
657 }
658 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 }
660
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700661 void moveHomeStackToFront(String reason) {
662 mHomeStack.moveToFront(reason);
663 }
664
Matthew Ng330757d2017-02-28 14:19:17 -0800665 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700666 final ActivityStack recentsStack = getDefaultDisplay().getStack(
667 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800668 if (recentsStack != null) {
669 recentsStack.moveToFront(reason);
670 }
671 }
672
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700673 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800674 boolean moveHomeStackTaskToTop(String reason) {
675 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700676
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700677 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678 if (top == null) {
679 return false;
680 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700681 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700682 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700683 }
684
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800685 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700686 if (!mService.mBooting && !mService.mBooted) {
687 // Not ready yet!
688 return false;
689 }
690
Craig Mautner84984fa2014-06-19 11:19:20 -0700691 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700692 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700693 }
694
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800695 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700696 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800697 final String myReason = reason + " resumeHomeStackTask";
698
Mark Lua56ea122015-10-08 13:31:01 +0800699 // Only resume home activity if isn't finishing.
700 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700701 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700703 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800704 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700705 }
706
Craig Mautner8d341ef2013-03-26 09:03:27 -0700707 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700708 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
709 }
710
711 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700712 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700713 }
714
715 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700716 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700717 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800718 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700719 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700720 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700721 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700722 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700723 @Nullable ActivityOptions aOptions, boolean onTop) {
724 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700725 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
726 throw new IllegalArgumentException("Should not specify activity options for non-restore"
727 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800728 }
729
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800731 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700732 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
733 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
734 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700735 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700736 if (task == null) {
737 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700739 if (aOptions != null) {
740 // Resolve the stack the task should be placed in now based on options
741 // and reparent if needed.
742 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
743 if (launchStack != null && stack != launchStack) {
744 final int reparentMode = onTop
745 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
746 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
747 "anyTaskForIdLocked");
748 }
749 }
750 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700751 }
752 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800753
Winson Chung7900bee2017-03-10 08:59:25 -0800754 // If we are matching stack tasks only, return now
755 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800756 return null;
757 }
758
Winson Chung7900bee2017-03-10 08:59:25 -0800759 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
760 // the task from recents
761 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700762 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700763
764 if (task == null) {
765 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800766 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
767 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700768
769 return null;
770 }
771
772 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700773 return task;
774 }
775
Winson Chung7900bee2017-03-10 08:59:25 -0800776 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700777 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700778 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
779 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800780 return null;
781 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700782 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800783 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700784 }
785
Craig Mautner6170f732013-04-02 13:05:23 -0700786 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800787 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700789 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
790 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
791 final ActivityStack stack = display.getChildAt(stackNdx);
792 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800793 if (r != null) {
794 return r;
795 }
Craig Mautner6170f732013-04-02 13:05:23 -0700796 }
797 }
798 return null;
799 }
800
Tony Mak853304c2016-04-18 15:17:41 +0100801 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000802 * Detects whether we should show a lock screen in front of this task for a locked user.
803 * <p>
804 * We'll do this if either of the following holds:
805 * <ul>
806 * <li>The top activity explicitly belongs to {@param userId}.</li>
807 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
808 * </ul>
809 *
810 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100811 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000812 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
813 // To handle the case that work app is in the task but just is not the top one.
814 final ActivityRecord activityRecord = task.getTopActivity();
815 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
816
817 return (activityRecord != null && activityRecord.userId == userId)
818 || (resultTo != null && resultTo.userId == userId);
819 }
820
821 /**
822 * Find all visible task stacks containing {@param userId} and intercept them with an activity
823 * to block out the contents and possibly start a credential-confirming intent.
824 *
825 * @param userId user handle for the locked managed profile.
826 */
827 void lockAllProfileTasks(@UserIdInt int userId) {
828 mWindowManager.deferSurfaceLayout();
829 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
831 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
832 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
833 final ActivityStack stack = display.getChildAt(stackNdx);
834 final List<TaskRecord> tasks = stack.getAllTasks();
835 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
836 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000837
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700838 // Check the task for a top activity belonging to userId, or returning a
839 // result to an activity belonging to userId. Example case: a document
840 // picker for personal files, opened by a work app, should still get locked.
841 if (taskTopActivityIsUser(task, userId)) {
842 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
843 task.taskId, userId);
844 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000845 }
Tony Mak853304c2016-04-18 15:17:41 +0100846 }
847 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000848 } finally {
849 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100850 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000851 }
852
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800853 void setNextTaskIdForUserLocked(int taskId, int userId) {
854 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
855 if (taskId > currentTaskId) {
856 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700857 }
858 }
859
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700860 static int nextTaskIdForUser(int taskId, int userId) {
861 int nextTaskId = taskId + 1;
862 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
863 // Wrap around as there will be smaller task ids that are available now.
864 nextTaskId -= MAX_TASK_IDS_PER_USER;
865 }
866 return nextTaskId;
867 }
868
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800869 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800870 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
871 // for a userId u, a taskId can only be in the range
872 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
873 // 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 -0700874 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700875 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700876 || anyTaskForIdLocked(
877 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700878 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800879 if (candidateTaskId == currentTaskId) {
880 // Something wrong!
881 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
882 throw new IllegalStateException("Cannot get an available task id."
883 + " Reached limit of " + MAX_TASK_IDS_PER_USER
884 + " running tasks per user.");
885 }
886 }
887 mCurTaskIdForUser.put(userId, candidateTaskId);
888 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700889 }
890
Chong Zhang6cda19c2016-06-14 19:07:56 -0700891 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800892 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700893 if (stack == null) {
894 return null;
895 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700896 ActivityRecord resumedActivity = stack.mResumedActivity;
897 if (resumedActivity == null || resumedActivity.app == null) {
898 resumedActivity = stack.mPausingActivity;
899 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700900 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700901 }
902 }
903 return resumedActivity;
904 }
905
Dianne Hackbornff072722014-09-24 10:56:28 -0700906 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700907 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800908 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800909 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700910 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
911 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
912 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700913 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800914 continue;
915 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200916 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
917 final ActivityRecord top = stack.topRunningActivityLocked();
918 final int size = mTmpActivityList.size();
919 for (int i = 0; i < size; i++) {
920 final ActivityRecord activity = mTmpActivityList.get(i);
921 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
922 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200924 if (realStartActivityLocked(activity, app,
925 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 didSomething = true;
927 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700928 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200930 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700932 }
Craig Mautner20e72272013-04-01 13:45:53 -0700933 }
Craig Mautner20e72272013-04-01 13:45:53 -0700934 }
935 }
936 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700937 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700938 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700939 }
Craig Mautner20e72272013-04-01 13:45:53 -0700940 return didSomething;
941 }
942
943 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700945 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
946 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
947 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700948 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 continue;
950 }
951 final ActivityRecord resumedActivity = stack.mResumedActivity;
952 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700953 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800954 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800955 return false;
956 }
Craig Mautner20e72272013-04-01 13:45:53 -0700957 }
958 }
Wei Wang65c7a152016-06-02 18:51:22 -0700959 // Send launch end powerhint when idle
960 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700961 return true;
962 }
963
Craig Mautnerde4ef022013-04-07 19:01:33 -0700964 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800965 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700966 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
967 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
968 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700969 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800970 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700971 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800972 return false;
973 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700974 }
975 }
976 }
977 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700978 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
980 mLastFocusedStack + " to=" + mFocusedStack);
981 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700982 return true;
983 }
984
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700985 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800986 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800987 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700988 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
989 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
990 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800991 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800992 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700993 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800994 return false;
995 }
996 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800997 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700998 }
999 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001000 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 }
1002
Craig Mautner2acc3892013-09-23 10:28:14 -07001003 /**
1004 * Pause all activities in either all of the stacks or just the back stacks.
1005 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001006 * @param resuming The resuming activity.
1007 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1008 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001009 * @return true if any activity was paused as a result of this call.
1010 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001011 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001012 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001013 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001014 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1015 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1016 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001017 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001018 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001019 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001020 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1021 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001022 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001023 }
1024 }
1025 return someActivityPaused;
1026 }
1027
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001029 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001030 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001031 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1032 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1033 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001034 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001035 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001037 Slog.d(TAG_STATES,
1038 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001039 pausing = false;
1040 } else {
1041 return false;
1042 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001043 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 }
1045 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001046 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 }
1048
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001049 void cancelInitializingActivities() {
1050 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001051 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1052 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1053 final ActivityStack stack = display.getChildAt(stackNdx);
1054 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001055 }
1056 }
1057 }
1058
Bryce Lee4a194382017-04-04 14:32:48 -07001059 void waitActivityVisible(ComponentName name, WaitResult result) {
1060 final WaitInfo waitInfo = new WaitInfo(name, result);
1061 mWaitingForActivityVisible.add(waitInfo);
1062 }
1063
1064 void cleanupActivity(ActivityRecord r) {
1065 // Make sure this record is no longer in the pending finishes list.
1066 // This could happen, for example, if we are trimming activities
1067 // down to the max limit while they are still waiting to finish.
1068 mFinishingActivities.remove(r);
1069 mActivitiesWaitingForVisibleActivity.remove(r);
1070
1071 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001072 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001073 mWaitingForActivityVisible.remove(i);
1074 }
1075 }
1076 }
1077
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001078 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001079 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001080 }
1081
1082 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1083 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001084 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1085 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001086 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001087 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001088 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001089 result.timeout = false;
1090 result.who = w.getComponent();
1091 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1092 result.thisTime = result.totalTime;
1093 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001096 if (changed) {
1097 mService.notifyAll();
1098 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001099 }
1100
Chong Zhang5022da32016-06-21 16:31:37 -07001101 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1102 boolean changed = false;
1103 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1104 WaitResult w = mWaitingActivityLaunched.remove(i);
1105 if (w.who == null) {
1106 changed = true;
1107 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1108 // the starting activity ends up moving another activity to front, and it should
1109 // wait for this new activity to become visible instead.
1110 // Do not modify other fields.
1111 w.result = START_TASK_TO_FRONT;
1112 }
1113 }
1114 if (changed) {
1115 mService.notifyAll();
1116 }
1117 }
1118
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1120 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001121 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001123 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 if (w.who == null) {
1125 changed = true;
1126 w.timeout = timeout;
1127 if (r != null) {
1128 w.who = new ComponentName(r.info.packageName, r.info.name);
1129 }
1130 w.thisTime = thisTime;
1131 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001132 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001133 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001134 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001135 if (changed) {
1136 mService.notifyAll();
1137 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001138 }
1139
Craig Mautner29219d92013-04-16 20:19:12 -07001140 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001141 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001142 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001143 if (r != null) {
1144 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001145 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001146
Andrii Kulian7318d632016-07-20 18:59:28 -07001147 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001148 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1149
1150 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1151 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001152 final ActivityDisplay display = mActivityDisplays.get(displayId);
1153 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1154 final ActivityStack stack = display.getChildAt(j);
1155 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001156 r = stack.topRunningActivityLocked();
1157 if (r != null) {
1158 return r;
1159 }
Craig Mautner29219d92013-04-16 20:19:12 -07001160 }
1161 }
1162 }
1163 return null;
1164 }
1165
Dianne Hackborn09233282014-04-30 11:33:59 -07001166 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001167 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001168 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001169 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001170 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001171 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1172 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1173 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001174 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001175 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001176 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001177 }
1178 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001179
1180 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1181 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1182 while (maxNum > 0) {
1183 long mostRecentActiveTime = Long.MIN_VALUE;
1184 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001185 final int numTaskLists = runningTaskLists.size();
1186 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1187 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001188 if (!stackTaskList.isEmpty()) {
1189 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1190 if (lastActiveTime > mostRecentActiveTime) {
1191 mostRecentActiveTime = lastActiveTime;
1192 selectedStackList = stackTaskList;
1193 }
1194 }
1195 }
1196 if (selectedStackList != null) {
1197 list.add(selectedStackList.remove(0));
1198 --maxNum;
1199 } else {
1200 break;
1201 }
1202 }
Craig Mautner20e72272013-04-01 13:45:53 -07001203 }
1204
Todd Kennedy7440f172015-12-09 14:31:22 -08001205 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1206 ProfilerInfo profilerInfo) {
1207 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001208 if (aInfo != null) {
1209 // Store the found target back into the intent, because now that
1210 // we have it we never want to do this again. For example, if the
1211 // user navigates back to this point in the history, we should
1212 // always restart the exact same activity.
1213 intent.setComponent(new ComponentName(
1214 aInfo.applicationInfo.packageName, aInfo.name));
1215
1216 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001217 if (!aInfo.processName.equals("system")) {
1218 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001219 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001220 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001221
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001222 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1223 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1224 }
1225
Man Caocfa78b22015-06-11 20:14:34 -07001226 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1227 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1228 }
1229
1230 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001231 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001232 }
1233 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001234 final String intentLaunchToken = intent.getLaunchToken();
1235 if (aInfo.launchToken == null && intentLaunchToken != null) {
1236 aInfo.launchToken = intentLaunchToken;
1237 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001238 }
1239 return aInfo;
1240 }
1241
Todd Kennedy7440f172015-12-09 14:31:22 -08001242 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001243 return resolveIntent(intent, resolvedType, userId, 0);
1244 }
1245
1246 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001247 synchronized (mService) {
1248 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001249 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001250 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001251 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001252 }
1253
1254 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1255 ProfilerInfo profilerInfo, int userId) {
1256 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1257 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1258 }
1259
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001260 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1261 boolean andResume, boolean checkConfig) throws RemoteException {
1262
1263 if (!allPausedActivitiesComplete()) {
1264 // While there are activities pausing we skipping starting any new activities until
1265 // pauses are complete. NOTE: that we also do this for activities that are starting in
1266 // the paused state because they will first be resumed then paused on the client side.
1267 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1268 "realStartActivityLocked: Skipping start of r=" + r
1269 + " some activities pausing...");
1270 return false;
1271 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001272
Bryce Leeaf691c02017-03-20 14:20:22 -07001273 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001274 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001275
1276 beginDeferResume();
1277
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 try {
chaviw59b98852017-06-13 12:05:44 -07001279 r.startFreezingScreenLocked(app, 0);
1280
1281 // schedule launch ticks to collect information about slow apps.
1282 r.startLaunchTickingLocked();
1283
1284 r.app = app;
1285
Jorim Jaggi838c2452017-08-28 15:44:43 +02001286 if (mKeyguardController.isKeyguardLocked()) {
1287 r.notifyUnknownVisibilityLaunched();
1288 }
1289
chaviw59b98852017-06-13 12:05:44 -07001290 // Have the window manager re-evaluate the orientation of the screen based on the new
1291 // activity order. Note that as a result of this, it can call back into the activity
1292 // manager with a new orientation. We don't care about that, because the activity is
1293 // not currently running so we are just restarting it anyway.
1294 if (checkConfig) {
1295 final int displayId = r.getDisplayId();
1296 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1297 getDisplayOverrideConfiguration(displayId),
1298 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1299 // Deferring resume here because we're going to launch new activity shortly.
1300 // We don't want to perform a redundant launch of the same record while ensuring
1301 // configurations and trying to resume top activity of focused stack.
1302 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1303 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 }
chaviw59b98852017-06-13 12:05:44 -07001305
1306 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1307 true /* isTop */)) {
1308 // We only set the visibility to true if the activity is allowed to be visible
1309 // based on
1310 // keyguard state. This avoids setting this into motion in window manager that is
1311 // later cancelled due to later calls to ensure visible activities that set
1312 // visibility back to false.
1313 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001314 }
chaviw59b98852017-06-13 12:05:44 -07001315
chaviw59b98852017-06-13 12:05:44 -07001316 final int applicationInfoUid =
1317 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1318 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1319 Slog.wtf(TAG,
1320 "User ID for activity changing for " + r
1321 + " appInfo.uid=" + r.appInfo.uid
1322 + " info.ai.uid=" + applicationInfoUid
1323 + " old=" + r.app + " new=" + app);
1324 }
1325
1326 app.waitingToKill = null;
1327 r.launchCount++;
1328 r.lastLaunchTime = SystemClock.uptimeMillis();
1329
1330 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1331
1332 int idx = app.activities.indexOf(r);
1333 if (idx < 0) {
1334 app.activities.add(r);
1335 }
1336 mService.updateLruProcessLocked(app, true, null);
1337 mService.updateOomAdjLocked();
1338
1339 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1340 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001341 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001342 }
1343
1344 try {
1345 if (app.thread == null) {
1346 throw new RemoteException();
1347 }
1348 List<ResultInfo> results = null;
1349 List<ReferrerIntent> newIntents = null;
1350 if (andResume) {
1351 // We don't need to deliver new intents and/or set results if activity is going
1352 // to pause immediately after launch.
1353 results = r.results;
1354 newIntents = r.newIntents;
1355 }
1356 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1357 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1358 + " newIntents=" + newIntents + " andResume=" + andResume);
1359 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1360 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001361 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001362 // Home process is the root process of the task.
1363 mService.mHomeProcess = task.mActivities.get(0).app;
1364 }
1365 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1366 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1367 r.sleeping = false;
1368 r.forceNewConfig = false;
1369 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1370 mService.showAskCompatModeDialogLocked(r);
1371 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1372 ProfilerInfo profilerInfo = null;
1373 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1374 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1375 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001376 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1377 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1378 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001379 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001380 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001381 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001382 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001383 }
1384 }
chaviw59b98852017-06-13 12:05:44 -07001385
Andreas Gampe2b073a02017-06-22 17:28:57 -07001386 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001387 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001388 }
1389 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001390
chaviw59b98852017-06-13 12:05:44 -07001391 app.hasShownUi = true;
1392 app.pendingUiClean = true;
1393 app.forceProcessStateUpTo(mService.mTopProcessState);
1394 // Because we could be starting an Activity in the system process this may not go
1395 // across a Binder interface which would create a new Configuration. Consequently
1396 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001397
chaviw59b98852017-06-13 12:05:44 -07001398 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1399 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1400 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001401
chaviw59b98852017-06-13 12:05:44 -07001402 logIfTransactionTooLarge(r.intent, r.icicle);
1403 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1404 System.identityHashCode(r), r.info,
1405 // TODO: Have this take the merged configuration instead of separate global
1406 // and override configs.
1407 mergedConfiguration.getGlobalConfiguration(),
1408 mergedConfiguration.getOverrideConfiguration(), r.compat,
1409 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1410 r.persistentState, results, newIntents, !andResume,
1411 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001412
chaviw59b98852017-06-13 12:05:44 -07001413 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1414 // This may be a heavy-weight process! Note that the package
1415 // manager will ensure that only activity can run in the main
1416 // process of the .apk, which is the only thing that will be
1417 // considered heavy-weight.
1418 if (app.processName.equals(app.info.packageName)) {
1419 if (mService.mHeavyWeightProcess != null
1420 && mService.mHeavyWeightProcess != app) {
1421 Slog.w(TAG, "Starting new heavy weight process " + app
1422 + " when already running "
1423 + mService.mHeavyWeightProcess);
1424 }
1425 mService.mHeavyWeightProcess = app;
1426 Message msg = mService.mHandler.obtainMessage(
1427 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1428 msg.obj = r;
1429 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001430 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001431 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001432
chaviw59b98852017-06-13 12:05:44 -07001433 } catch (RemoteException e) {
1434 if (r.launchFailed) {
1435 // This is the second time we failed -- finish activity
1436 // and give up.
1437 Slog.e(TAG, "Second failure launching "
1438 + r.intent.getComponent().flattenToShortString()
1439 + ", giving up", e);
1440 mService.appDiedLocked(app);
1441 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1442 "2nd-crash", false);
1443 return false;
1444 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001445
chaviw59b98852017-06-13 12:05:44 -07001446 // This is the first time we failed -- restart process and
1447 // retry.
1448 r.launchFailed = true;
1449 app.activities.remove(r);
1450 throw e;
1451 }
1452 } finally {
1453 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001454 }
1455
1456 r.launchFailed = false;
1457 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001458 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001459 }
1460
chaviw59b98852017-06-13 12:05:44 -07001461 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001462 // As part of the process of launching, ActivityThread also performs
1463 // a resume.
1464 stack.minimalResumeActivityLocked(r);
1465 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001466 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001467 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001468 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001469 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001470 "Moving to PAUSED: " + r + " (starting in paused state)");
1471 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001472 }
1473
1474 // Launch the new version setup screen if needed. We do this -after-
1475 // launching the initial activity (that is, home), so that it can have
1476 // a chance to initialize itself while in the background, making the
1477 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001478 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001479 mService.startSetupActivityLocked();
1480 }
1481
Dianne Hackborn465fa392014-09-14 14:21:18 -07001482 // Update any services we are bound to that might care about whether
1483 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001484 if (r.app != null) {
1485 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1486 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001487
Craig Mautner2420ead2013-04-01 17:13:20 -07001488 return true;
1489 }
1490
Sudheer Shankafab200f2017-05-17 20:41:53 -07001491 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1492 int extrasSize = 0;
1493 if (intent != null) {
1494 final Bundle extras = intent.getExtras();
1495 if (extras != null) {
1496 extrasSize = extras.getSize();
1497 }
1498 }
1499 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1500 if (extrasSize + icicleSize > 200000) {
1501 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1502 + ", icicle size: " + icicleSize);
1503 }
1504 }
1505
Craig Mautnere79d42682013-04-01 19:01:53 -07001506 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001507 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001508 // Is this activity's application already running?
1509 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001510 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001511
Andrii Kulian02b7a832016-10-06 23:11:56 -07001512 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001513
1514 if (app != null && app.thread != null) {
1515 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001516 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1517 || !"android".equals(r.info.packageName)) {
1518 // Don't add this if it is a platform component that is marked
1519 // to run in multiple processes, because this is actually
1520 // part of the framework so doesn't make sense to track as a
1521 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001522 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1523 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001524 }
George Mount2c92c972014-03-20 09:38:23 -07001525 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001526 return;
1527 } catch (RemoteException e) {
1528 Slog.w(TAG, "Exception when starting activity "
1529 + r.intent.getComponent().flattenToShortString(), e);
1530 }
1531
1532 // If a dead object exception was thrown -- fall through to
1533 // restart the application.
1534 }
1535
1536 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001537 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001538 }
1539
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001540 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001541 String resultWho, int requestCode, int callingPid, int callingUid,
1542 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001543 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001544 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1545 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001546 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001547 return true;
1548 }
1549 final int componentRestriction = getComponentRestrictionForCallingPackage(
1550 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1551 final int actionRestriction = getActionRestrictionForCallingPackage(
1552 intent.getAction(), callingPackage, callingPid, callingUid);
1553 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1554 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1555 if (resultRecord != null) {
1556 resultStack.sendActivityResultLocked(-1,
1557 resultRecord, resultWho, requestCode,
1558 Activity.RESULT_CANCELED, null);
1559 }
1560 final String msg;
1561 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1562 msg = "Permission Denial: starting " + intent.toString()
1563 + " from " + callerApp + " (pid=" + callingPid
1564 + ", uid=" + callingUid + ")" + " with revoked permission "
1565 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1566 } else if (!aInfo.exported) {
1567 msg = "Permission Denial: starting " + intent.toString()
1568 + " from " + callerApp + " (pid=" + callingPid
1569 + ", uid=" + callingUid + ")"
1570 + " not exported from uid " + aInfo.applicationInfo.uid;
1571 } else {
1572 msg = "Permission Denial: starting " + intent.toString()
1573 + " from " + callerApp + " (pid=" + callingPid
1574 + ", uid=" + callingUid + ")"
1575 + " requires " + aInfo.permission;
1576 }
1577 Slog.w(TAG, msg);
1578 throw new SecurityException(msg);
1579 }
1580
1581 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1582 final String message = "Appop Denial: starting " + intent.toString()
1583 + " from " + callerApp + " (pid=" + callingPid
1584 + ", uid=" + callingUid + ")"
1585 + " requires " + AppOpsManager.permissionToOp(
1586 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1587 Slog.w(TAG, message);
1588 return false;
1589 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1590 final String message = "Appop Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ")"
1593 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1594 Slog.w(TAG, message);
1595 return false;
1596 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001597 if (options != null) {
1598 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1599 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1600 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001601 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001602 final String msg = "Permission Denial: starting " + intent.toString()
1603 + " from " + callerApp + " (pid=" + callingPid
1604 + ", uid=" + callingUid + ") with launchTaskId="
1605 + options.getLaunchTaskId();
1606 Slog.w(TAG, msg);
1607 throw new SecurityException(msg);
1608 }
1609 }
1610 // Check if someone tries to launch an activity on a private display with a different
1611 // owner.
1612 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001613 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1614 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001615 final String msg = "Permission Denial: starting " + intent.toString()
1616 + " from " + callerApp + " (pid=" + callingPid
1617 + ", uid=" + callingUid + ") with launchDisplayId="
1618 + launchDisplayId;
1619 Slog.w(TAG, msg);
1620 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001621 }
Charles He2bf28322017-10-12 22:24:49 +01001622 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1623 final boolean lockTaskMode = options.getLockTaskMode();
1624 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1625 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1626 final String msg = "Permission Denial: starting " + intent.toString()
1627 + " from " + callerApp + " (pid=" + callingPid
1628 + ", uid=" + callingUid + ") with lockTaskMode=true";
1629 Slog.w(TAG, msg);
1630 throw new SecurityException(msg);
1631 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001632 }
1633
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001634 return true;
1635 }
1636
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001637 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001638 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1639 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001640 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1641 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1642
Andrii Kulian02689a72017-07-06 14:28:59 -07001643 if (callingPid == -1 && callingUid == -1) {
1644 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1645 return true;
1646 }
1647
Andrii Kulian62e6f252017-05-30 22:46:53 -07001648 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001649 if (activityDisplay == null) {
1650 Slog.w(TAG, "Launch on display check: display not found");
1651 return false;
1652 }
1653
Andrii Kulian02689a72017-07-06 14:28:59 -07001654 // Check if the caller has enough privileges to embed activities and launch to private
1655 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001656 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001657 callingUid);
1658 if (startAnyPerm == PERMISSION_GRANTED) {
1659 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1660 + " allow launch any on display");
1661 return true;
1662 }
1663
Andrii Kulian8f070292017-09-12 22:56:49 -07001664 // Check if caller is already present on display
1665 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1666
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001667 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1668 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1669 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001670 // Limit launching on virtual displays, because their contents can be read from Surface
1671 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001672 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1673 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1674 + " disallow launch on virtual display for not-embedded activity.");
1675 return false;
1676 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001677 // Check if the caller is allowed to embed activities from other apps.
1678 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001679 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001680 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1681 + " disallow activity embedding without permission.");
1682 return false;
1683 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001684 }
1685
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001686 if (!activityDisplay.isPrivate()) {
1687 // Anyone can launch on a public display.
1688 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1689 + " allow launch on public display");
1690 return true;
1691 }
1692
1693 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001694 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001695 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1696 + " allow launch for owner of the display");
1697 return true;
1698 }
1699
Andrii Kulian8f070292017-09-12 22:56:49 -07001700 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001701 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1702 + " allow launch for caller present on the display");
1703 return true;
1704 }
1705
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001706 Slog.w(TAG, "Launch on display check: denied");
1707 return false;
1708 }
1709
1710 /** Update lists of UIDs that are present on displays and have access to them. */
1711 void updateUIDsPresentOnDisplay() {
1712 mDisplayAccessUIDs.clear();
1713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1714 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001715 // Only bother calculating the whitelist for private displays
1716 if (activityDisplay.isPrivate()) {
1717 mDisplayAccessUIDs.append(
1718 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1719 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001720 }
1721 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1722 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1723 }
1724
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001725 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001726 final long identity = Binder.clearCallingIdentity();
1727 try {
1728 return UserManager.get(mService.mContext).getUserInfo(userId);
1729 } finally {
1730 Binder.restoreCallingIdentity(identity);
1731 }
1732 }
1733
Svet Ganov99b60432015-06-27 13:15:22 -07001734 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001735 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001736 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1737 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001738 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001739 return ACTIVITY_RESTRICTION_PERMISSION;
1740 }
1741
Christopher Tateff7add02015-08-17 10:23:22 -07001742 if (activityInfo.permission == null) {
1743 return ACTIVITY_RESTRICTION_NONE;
1744 }
1745
Svet Ganov99b60432015-06-27 13:15:22 -07001746 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1747 if (opCode == AppOpsManager.OP_NONE) {
1748 return ACTIVITY_RESTRICTION_NONE;
1749 }
1750
1751 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1752 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001753 if (!ignoreTargetSecurity) {
1754 return ACTIVITY_RESTRICTION_APPOP;
1755 }
Svet Ganov99b60432015-06-27 13:15:22 -07001756 }
1757
1758 return ACTIVITY_RESTRICTION_NONE;
1759 }
1760
Svetoslav7008b512015-06-24 18:47:07 -07001761 private int getActionRestrictionForCallingPackage(String action,
1762 String callingPackage, int callingPid, int callingUid) {
1763 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1768 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 final PackageInfo packageInfo;
1773 try {
1774 packageInfo = mService.mContext.getPackageManager()
1775 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1776 } catch (PackageManager.NameNotFoundException e) {
1777 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001778 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001779 }
1780
1781 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001782 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001783 }
1784
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001785 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001786 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001787 }
1788
1789 final int opCode = AppOpsManager.permissionToOpCode(permission);
1790 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001791 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001792 }
1793
1794 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1795 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001796 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001797 }
1798
Svet Ganov99b60432015-06-27 13:15:22 -07001799 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001800 }
1801
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001802 void setLaunchSource(int uid) {
1803 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1804 }
1805
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001806 void acquireLaunchWakelock() {
1807 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1808 throw new IllegalStateException("Calling must be system uid");
1809 }
1810 mLaunchingActivity.acquire();
1811 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1812 // To be safe, don't allow the wake lock to be held for too long.
1813 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1814 }
1815 }
1816
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001817 /**
1818 * Called when the frontmost task is idle.
1819 * @return the state of mService.mBooting before this was called.
1820 */
1821 private boolean checkFinishBootingLocked() {
1822 final boolean booting = mService.mBooting;
1823 boolean enableScreen = false;
1824 mService.mBooting = false;
1825 if (!mService.mBooted) {
1826 mService.mBooted = true;
1827 enableScreen = true;
1828 }
1829 if (booting || enableScreen) {
1830 mService.postFinishBooting(booting, enableScreen);
1831 }
1832 return booting;
1833 }
1834
Craig Mautnerf3333272013-04-22 10:55:53 -07001835 // Checked.
1836 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001837 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001838 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001839
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001841 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001842 int NS = 0;
1843 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001844 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001845 boolean activityRemoved = false;
1846
Wale Ogunwale7d701172015-03-11 15:36:30 -07001847 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001848 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001849 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1850 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001851 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1852 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001853 if (fromTimeout) {
1854 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001855 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001856
1857 // This is a hack to semi-deal with a race condition
1858 // in the client where it can be constructed with a
1859 // newer configuration from when we asked it to launch.
1860 // We'll update with whatever configuration it now says
1861 // it used to launch.
1862 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001863 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001864 }
1865
1866 // We are now idle. If someone is waiting for a thumbnail from
1867 // us, we can now deliver.
1868 r.idle = true;
1869
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001870 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001871 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001872 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001873 }
1874 }
1875
1876 if (allResumedActivitiesIdle()) {
1877 if (r != null) {
1878 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001879 }
1880
1881 if (mLaunchingActivity.isHeld()) {
1882 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1883 if (VALIDATE_WAKE_LOCK_CALLER &&
1884 Binder.getCallingUid() != Process.myUid()) {
1885 throw new IllegalStateException("Calling must be system uid");
1886 }
1887 mLaunchingActivity.release();
1888 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001889 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 }
1891
1892 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001893 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1894 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001896 if ((NF = mFinishingActivities.size()) > 0) {
1897 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 mFinishingActivities.clear();
1899 }
1900
Craig Mautnerf3333272013-04-22 10:55:53 -07001901 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001902 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001903 mStartingUsers.clear();
1904 }
1905
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 // Stop any activities that are scheduled to do so but have been
1907 // waiting for the next one to start.
1908 for (int i = 0; i < NS; i++) {
1909 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001910 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001911 if (stack != null) {
1912 if (r.finishing) {
1913 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1914 } else {
1915 stack.stopActivityLocked(r);
1916 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001917 }
1918 }
1919
1920 // Finish any activities that are scheduled to do so but have been
1921 // waiting for the next one to start.
1922 for (int i = 0; i < NF; i++) {
1923 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001924 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001925 if (stack != null) {
1926 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1927 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001928 }
1929
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001930 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001931 // Complete user switch
1932 if (startingUsers != null) {
1933 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001934 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001935 }
1936 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 }
1938
1939 mService.trimApplications();
1940 //dump();
1941 //mWindowManager.dump();
1942
Craig Mautnerf3333272013-04-22 10:55:53 -07001943 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001944 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001945 }
1946
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001947 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001948 }
1949
Craig Mautner8e569572013-10-11 17:36:59 -07001950 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001951 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001953 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1954 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1955 final ActivityStack stack = display.getChildAt(stackNdx);
1956 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001957 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001958 }
Craig Mautner19091252013-10-05 00:03:53 -07001959 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001960 }
1961
1962 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001963 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001964 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1965 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1966 final ActivityStack stack = display.getChildAt(stackNdx);
1967 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001968 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001969 }
1970 }
1971
Craig Mautner93529a42013-10-04 15:03:13 -07001972 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001973 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001974 }
1975
Craig Mautner8d341ef2013-03-26 09:03:27 -07001976 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001977 * Update the last used stack id for non-current user (current user's last
1978 * used stack is the focused stack)
1979 */
1980 void updateUserStackLocked(int userId, ActivityStack stack) {
1981 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001982 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001983 }
1984 }
1985
1986 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001987 * @return true if some activity was finished (or would have finished if doit were true).
1988 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001989 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1990 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001991 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001992 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001993 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1994 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1995 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001996 if (stack.finishDisabledPackageActivitiesLocked(
1997 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001998 didSomething = true;
1999 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002000 }
2001 }
2002 return didSomething;
2003 }
2004
Dianne Hackborna413dc02013-07-12 12:02:55 -07002005 void updatePreviousProcessLocked(ActivityRecord r) {
2006 // Now that this process has stopped, we may want to consider
2007 // it to be the previous app to try to keep around in case
2008 // the user wants to return to it.
2009
2010 // First, found out what is currently the foreground app, so that
2011 // we don't blow away the previous app if this activity is being
2012 // hosted by the process that is actually still the foreground.
2013 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002014 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002015 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2016 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2017 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002018 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002019 if (stack.mResumedActivity != null) {
2020 fgApp = stack.mResumedActivity.app;
2021 } else if (stack.mPausingActivity != null) {
2022 fgApp = stack.mPausingActivity.app;
2023 }
2024 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002025 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002026 }
2027 }
2028
2029 // Now set this one as the previous process, only if that really
2030 // makes sense to.
2031 if (r.app != null && fgApp != null && r.app != fgApp
2032 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002033 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002034 mService.mPreviousProcess = r.app;
2035 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2036 }
2037 }
2038
Wale Ogunwaled046a012015-12-24 13:05:59 -08002039 boolean resumeFocusedStackTopActivityLocked() {
2040 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002041 }
2042
Wale Ogunwaled046a012015-12-24 13:05:59 -08002043 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002044 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002045
2046 if (!readyToResume()) {
2047 return false;
2048 }
2049
Wale Ogunwaled046a012015-12-24 13:05:59 -08002050 if (targetStack != null && isFocusedStack(targetStack)) {
2051 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002052 }
chaviw59b98852017-06-13 12:05:44 -07002053
Wale Ogunwale06579d62016-04-30 15:29:06 -07002054 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2055 if (r == null || r.state != RESUMED) {
2056 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002057 } else if (r.state == RESUMED) {
2058 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2059 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002060 }
chaviw59b98852017-06-13 12:05:44 -07002061
Wale Ogunwaled046a012015-12-24 13:05:59 -08002062 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002063 }
2064
Todd Kennedy39bfee52016-02-24 10:28:21 -08002065 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2066 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002067 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2068 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2069 final ActivityStack stack = display.getChildAt(stackNdx);
2070 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002071 }
2072 }
2073 }
2074
Adrian Roos20d7df32016-01-12 18:59:43 +01002075 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2076 TaskRecord finishedTask = null;
2077 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002078 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002079 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2080 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002081 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002082 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002083 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2084 if (stack == focusedStack || finishedTask == null) {
2085 finishedTask = t;
2086 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002087 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002088 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002089 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002090 }
2091
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002092 void finishVoiceTask(IVoiceInteractionSession session) {
2093 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002094 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2095 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002096 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002097 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002098 stack.finishVoiceTask(session);
2099 }
2100 }
2101 }
2102
Andrii Kulianc27916642016-04-12 17:59:27 -07002103 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2104 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002105 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2106 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002107 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002108 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2109 // Caller wants the home activity moved with it. To accomplish this,
2110 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002111 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002112 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002113 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002114 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002115 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2116 + task + " to front. Stack is null");
2117 return;
2118 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002119
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002120 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2121 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2122 task.updateOverrideConfiguration(bounds);
2123
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002124 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002125
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002126 if (stack != currentStack) {
2127 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2128 "findTaskToMoveToFrontLocked");
2129 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002130 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2131 // still need moveTaskToFrontLocked() below for any transition settings.
2132 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002133 if (stack.resizeStackWithLaunchBounds()) {
2134 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002135 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2136 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002137 } else {
2138 // WM resizeTask must be done after the task is moved to the correct stack,
2139 // because Task's setBounds() also updates dim layer's bounds, but that has
2140 // dependency on the stack.
2141 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002142 }
2143 }
2144
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002145 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002146 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002147 r == null ? null : r.appTimeTracker, reason);
2148
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002149 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002150 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002151
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002152 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002153 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002154 }
2155
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002156 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002157 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002158 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002159 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002160 return false;
2161 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002162 return (mService.mSupportsPictureInPicture
2163 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002164 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002165 }
2166
Bryce Leedacefc42017-10-10 12:56:02 -07002167 LaunchingBoundsController getLaunchingBoundsController() {
2168 return mLaunchingBoundsController;
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002169 }
2170
Winson Chung55893332017-02-17 17:13:10 -08002171 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002172 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2173 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2174 if (stack != null) {
2175 return stack;
2176 }
2177 }
2178 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002179 }
2180
Wale Ogunwale68278562017-09-23 17:13:55 -07002181 /** @see ActivityDisplay#getStack(int, int) */
2182 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2183 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2184 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2185 if (stack != null) {
2186 return stack;
2187 }
2188 }
2189 return null;
2190 }
2191
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002192 /**
2193 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2194 * @param windowingMode The windowing mode we are checking support for.
2195 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2196 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2197 * @param supportsFreeform If we should consider support for freeform multi-window.
2198 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2199 * @param activityType The activity type under consideration.
2200 * @return true if the windowing mode is supported.
2201 */
2202 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2203 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2204 int activityType) {
2205
2206 if (windowingMode == WINDOWING_MODE_UNDEFINED
2207 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2208 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002209 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002210 if (!supportsMultiWindow) {
2211 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002212 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002213
2214 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2215 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2216 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2217 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002218 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002219
2220 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2221 return false;
2222 }
2223
2224 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2225 return false;
2226 }
2227 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002228 }
2229
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002230 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002231 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002232
2233 // First preference if the windowing mode in the activity options if set.
2234 int windowingMode = (options != null)
2235 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2236
2237 // If windowing mode is unset, then next preference is the candidate task, then the
2238 // activity record.
2239 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2240 if (task != null) {
2241 windowingMode = task.getWindowingMode();
2242 }
2243 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2244 windowingMode = r.getWindowingMode();
2245 }
2246 }
2247
2248 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002249 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2250 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2251 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2252 boolean supportsPip = mService.mSupportsPictureInPicture;
2253 if (supportsMultiWindow) {
2254 if (task != null) {
2255 supportsMultiWindow = task.isResizeable();
2256 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2257 // TODO: Do we need to check for freeform and Pip support here?
2258 } else if (r != null) {
2259 supportsMultiWindow = r.isResizeable();
2260 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2261 supportsFreeform = r.supportsFreeform();
2262 supportsPip = r.supportsPictureInPicture();
2263 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002264 }
2265
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002266 if (windowingMode != WINDOWING_MODE_UNDEFINED
2267 && isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002268 supportsFreeform, supportsPip, activityType)) {
2269 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002270 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002271 // Return root/systems windowing mode
2272 return getWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002273 }
2274
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002275 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002276 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002277 // Preference is given to the activity type for the activity then the task since the type
2278 // once set shouldn't change.
2279 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2280 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2281 activityType = task.getActivityType();
2282 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002283 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2284 return activityType;
2285 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002286 if (options != null) {
2287 activityType = options.getLaunchActivityType();
2288 }
2289 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002290 }
2291
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002292 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2293 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2294 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002295 }
2296
2297 /**
2298 * Returns the right stack to use for launching factoring in all the input parameters.
2299 *
2300 * @param r The activity we are trying to launch. Can be null.
2301 * @param options The activity options used to the launch. Can be null.
2302 * @param candidateTask The possible task the activity might be launched in. Can be null.
2303 *
2304 * @return The stack to use for the launch or INVALID_STACK_ID.
2305 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002306 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2307 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2308 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002309 int taskId = INVALID_TASK_ID;
2310 int displayId = INVALID_DISPLAY;
2311 //Rect bounds = null;
2312
2313 // We give preference to the launch preference in activity options.
2314 if (options != null) {
2315 taskId = options.getLaunchTaskId();
2316 displayId = options.getLaunchDisplayId();
2317 // TODO: Need to work this into the equation...
2318 //bounds = options.getLaunchBounds();
2319 }
2320
2321 // First preference for stack goes to the task Id set in the activity options. Use the stack
2322 // associated with that if possible.
2323 if (taskId != INVALID_TASK_ID) {
2324 // Temporarily set the task id to invalid in case in re-entry.
2325 options.setLaunchTaskId(INVALID_TASK_ID);
2326 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002327 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002328 options.setLaunchTaskId(taskId);
2329 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002330 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002331 }
2332 }
2333
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002334 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002335 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2336 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002337
2338 // Next preference for stack goes to the display Id set in the activity options or the
2339 // candidate display.
2340 if (displayId == INVALID_DISPLAY) {
2341 displayId = candidateDisplayId;
2342 }
2343 if (displayId != INVALID_DISPLAY) {
2344 if (r != null) {
2345 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002346 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002347 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002348 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002349 }
2350 }
2351 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2352 if (display != null) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002353 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2354 if (stack != null) {
2355 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002356 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002357 }
2358 }
2359
2360 // Give preference to the stack and display of the input task and activity if they match the
2361 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002362 stack = null;
2363 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002364 if (candidateTask != null) {
2365 stack = candidateTask.getStack();
2366 }
2367 if (stack == null && r != null) {
2368 stack = r.getStack();
2369 }
2370 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002371 if (stack.isCompatible(windowingMode, activityType)) {
2372 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002373 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002374 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002375 }
2376
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002377 if (display == null
2378 // TODO: Can be removed once we figure-out how non-standard types should launch
2379 // outside the default display.
2380 || (activityType != ACTIVITY_TYPE_STANDARD
2381 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2382 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002384
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002385 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002386 }
2387
Andrii Kulian16802aa2016-11-02 12:21:33 -07002388 /**
2389 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2390 * If there is no such stack, new dynamic stack can be created.
2391 * @param displayId Target display.
2392 * @param r Activity that should be launched there.
2393 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2394 */
2395 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002396 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002397 if (activityDisplay == null) {
2398 throw new IllegalArgumentException(
2399 "Display with displayId=" + displayId + " not found.");
2400 }
2401
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002402 if (!r.canBeLaunchedOnDisplay(displayId)) {
2403 return null;
2404 }
2405
Andrii Kulian16802aa2016-11-02 12:21:33 -07002406 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002407 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2408 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002409 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002410 return stack;
2411 }
2412 }
2413
2414 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002415 if (displayId != DEFAULT_DISPLAY) {
2416 return activityDisplay.createStack(
2417 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002418 }
2419
2420 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2421 return null;
2422 }
2423
Wale Ogunwale68278562017-09-23 17:13:55 -07002424 // TODO: Can probably be consolidated into getLaunchStack()...
2425 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2426 switch (stack.getActivityType()) {
2427 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2428 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2429 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002430 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002431 switch (stack.getWindowingMode()) {
2432 case WINDOWING_MODE_FULLSCREEN: return true;
2433 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2434 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2435 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2436 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2437 }
2438
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002439 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002440 return r.canBeLaunchedOnDisplay(displayId);
2441 }
2442 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2443 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002444 }
2445
Andrii Kulian7fc22812016-12-28 13:04:11 -08002446 /**
2447 * Get next focusable stack in the system. This will search across displays and stacks
2448 * in last-focused order for a focusable and visible stack, different from the target stack.
2449 *
2450 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2451 * searching for next candidate.
2452 * @return Next focusable {@link ActivityStack}, null if not found.
2453 */
2454 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2455 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2456
2457 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2458 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002459 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002460 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2461 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2462 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002463 if (stack != currentFocus && stack.isFocusable()
2464 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002465 return stack;
2466 }
2467 }
2468 }
2469
2470 return null;
2471 }
2472
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002473 /**
2474 * Get next valid stack for launching provided activity in the system. This will search across
2475 * displays and stacks in last-focused order for a focusable and visible stack, except those
2476 * that are on a currently focused display.
2477 *
2478 * @param r The activity that is being launched.
2479 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2480 * searching for the next candidate.
2481 * @return Next valid {@link ActivityStack}, null if not found.
2482 */
2483 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2484 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2485 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2486 final int displayId = mTmpOrderedDisplayIds.get(i);
2487 if (displayId == currentFocus) {
2488 continue;
2489 }
2490 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2491 if (stack != null) {
2492 return stack;
2493 }
2494 }
2495 return null;
2496 }
2497
Craig Mautneree2e45a2014-06-27 12:10:03 -07002498 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002499 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002500 }
2501
2502 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002503 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2504 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2505 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002506 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002507 final ArrayList<ActivityRecord> activities = task.mActivities;
2508 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2509 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002510 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002511 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002512 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002513 }
2514 }
2515 }
2516 }
2517 return null;
2518 }
2519
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002520 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2521 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2522 boolean deferResume) {
2523
2524 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002525 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002526 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002527 return;
2528 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002529
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002530 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002531 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002532 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002533 // If the docked stack exists, don't resize non-floating stacks independently of the
2534 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002535 return;
2536 }
2537
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002538 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002539 mWindowManager.deferSurfaceLayout();
2540 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002541 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002542 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2543 // null bounds = fullscreen windowing mode...at least for now.
2544 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2545 } else if (splitScreenActive) {
2546 // If we are in split-screen mode and this stack support split-screen, then
2547 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2548 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2549 }
2550 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002551 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002552 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002553 stack.ensureVisibleActivitiesConfigurationLocked(
2554 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002555 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002556 } finally {
2557 mWindowManager.continueSurfaceLayout();
2558 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002559 }
2560 }
2561
Wale Ogunwale68278562017-09-23 17:13:55 -07002562 private void deferUpdateBounds(int activityType) {
2563 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002564 if (stack != null) {
2565 stack.deferUpdateBounds();
2566 }
2567 }
2568
Wale Ogunwale68278562017-09-23 17:13:55 -07002569 private void continueUpdateBounds(int activityType) {
2570 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002571 if (stack != null) {
2572 stack.continueUpdateBounds();
2573 }
2574 }
2575
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002576 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002577 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002578 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2579 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002580 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002581 if (task != null) {
2582 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002583 }
2584 }
2585 mResizingTasksDuringAnimation.clear();
2586 }
2587
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002588 /**
2589 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002590 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002591 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002592 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002593 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002594
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002595 mWindowManager.deferSurfaceLayout();
2596 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002597 final int windowingMode = fromStack.getWindowingMode();
2598 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2599 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2600
2601 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002602 // We are moving all tasks from the docked stack to the fullscreen stack,
2603 // which is dismissing the docked stack, so resize all other stacks to
2604 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002605 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2606 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002607 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2608 continue;
2609 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002610 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002611 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002612 }
2613
2614 // Also disable docked stack resizing since we have manually adjusted the
2615 // size of other stacks above and we don't want to trigger a docked stack
2616 // resize when we remove task from it below and it is detached from the
2617 // display because it no longer contains any tasks.
2618 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002619 } else if (inPinnedWindowingMode && onTop) {
2620 // Log if we are expanding the PiP to fullscreen
2621 MetricsLogger.action(mService.mContext,
2622 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002623 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002624
Winson Chung5af42fc2017-03-24 17:11:33 -07002625 // If we are moving from the pinned stack, then the animation takes care of updating
2626 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002627 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002628 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002629
Wale Ogunwale388945c2017-10-04 12:13:46 -07002630 if (!tasks.isEmpty()) {
2631 final int size = tasks.size();
2632 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2633 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2634
2635 if (onTop) {
2636 final int returnToType =
2637 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
2638 for (int i = 0; i < size; i++) {
2639 final TaskRecord task = tasks.get(i);
2640 final boolean isTopTask = i == (size - 1);
2641 if (inPinnedWindowingMode) {
2642 // Update the return-to to reflect where the pinned stack task was moved
2643 // from so that we retain the stack that was previously visible if the
2644 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2645 task.setTaskToReturnTo(returnToType);
2646 }
2647 // Defer resume until all the tasks have been moved to the fullscreen stack
2648 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2649 isTopTask /* animate */, DEFER_RESUME,
2650 schedulePictureInPictureModeChange,
2651 "moveTasksToFullscreenStack - onTop");
Winson Chung0c1ca092016-11-10 17:40:34 -08002652 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07002653 } else {
2654 for (int i = 0; i < size; i++) {
2655 final TaskRecord task = tasks.get(i);
2656 // Position the tasks in the fullscreen stack in order at the bottom of the
2657 // stack. Also defer resume until all the tasks have been moved to the
2658 // fullscreen stack.
2659 task.reparent(fullscreenStack, i /* position */,
2660 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2661 schedulePictureInPictureModeChange,
2662 "moveTasksToFullscreenStack - NOT_onTop");
2663 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002664 }
2665 }
Winson Chung74666102017-02-22 17:49:24 -08002666
2667 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2668 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002669 } finally {
2670 mAllowDockedStackResize = true;
2671 mWindowManager.continueSurfaceLayout();
2672 }
2673 }
2674
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002675 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002676 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2677 }
2678
2679 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2680 mWindowManager.inSurfaceTransaction(() ->
2681 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002682 }
2683
Jorim Jaggidc249c42015-12-15 14:57:31 -08002684 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002685 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2686 boolean preserveWindows) {
2687 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2688 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2689 false /* deferResume */);
2690 }
2691
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002692 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002693 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2694 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002695
2696 if (!mAllowDockedStackResize) {
2697 // Docked stack resize currently disabled.
2698 return;
2699 }
2700
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002701 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002702 if (stack == null) {
2703 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2704 return;
2705 }
2706
2707 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2708 mWindowManager.deferSurfaceLayout();
2709 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002710 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2711 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002712 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002713 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002714
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002715 // TODO: Checking for isAttached might not be needed as if the user passes in null
2716 // dockedBounds then they want the docked stack to be dismissed.
2717 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2718 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002719 // In this case we make all other static stacks fullscreen and move all
2720 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002721 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002722
2723 // stack shouldn't contain anymore activities, so nothing to resume.
2724 r = null;
2725 } else {
2726 // Docked stacks occupy a dedicated region on screen so the size of all other
2727 // static stacks need to be adjusted so they don't overlap with the docked stack.
2728 // We get the bounds to use from window manager which has been adjusted for any
2729 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002730 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002731 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002732 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2733 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002734 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002735 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002736 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002737 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002738 continue;
2739 }
2740 // Need to set windowing mode here before we try to get the dock bounds.
2741 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2742 current.getStackDockedModeBounds(
2743 tempOtherTaskBounds /* currentTempTaskBounds */,
2744 tempRect /* outStackBounds */,
2745 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2746
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002747 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002748 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2749 tempOtherTaskInsetBounds, preserveWindows,
2750 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002751 }
2752 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002753 if (!deferResume) {
2754 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2755 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002756 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002757 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002758 mWindowManager.continueSurfaceLayout();
2759 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2760 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002761 }
2762
Robert Carr0d00c2e2016-02-29 17:45:02 -08002763 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002764 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002765 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002766 if (stack == null) {
2767 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2768 return;
2769 }
Winson Chung19953ca2017-04-11 11:19:23 -07002770
2771 // It is possible for the bounds animation from the WM to call this but be delayed by
2772 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2773 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2774 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2775 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002776 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002777 return;
2778 }
2779
Robert Carr0d00c2e2016-02-29 17:45:02 -08002780 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2781 mWindowManager.deferSurfaceLayout();
2782 try {
2783 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002784 Rect insetBounds = null;
2785 if (tempPinnedTaskBounds != null) {
2786 // We always use 0,0 as the position for the inset rect because
2787 // if we are getting insets at all in the pinned stack it must mean
2788 // we are headed for fullscreen.
2789 insetBounds = tempRect;
2790 insetBounds.top = 0;
2791 insetBounds.left = 0;
2792 insetBounds.right = tempPinnedTaskBounds.width();
2793 insetBounds.bottom = tempPinnedTaskBounds.height();
2794 }
2795 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002796 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002797 } finally {
2798 mWindowManager.continueSurfaceLayout();
2799 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2800 }
2801 }
2802
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002803 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002804 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002805 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002806 /**
2807 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2808 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2809 * that the client receives onStop() before it is reparented. We do this by detaching
2810 * the stack from the display so that it will be considered invisible when
2811 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2812 * invisible as well and added to the stopping list. After which we process the
2813 * stopping list by handling the idle.
2814 */
2815 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2816 pinnedStack.mForceHidden = true;
2817 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2818 pinnedStack.mForceHidden = false;
2819 activityIdleInternalLocked(null, false /* fromTimeout */,
2820 true /* processPausingActivites */, null /* configuration */);
2821
2822 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002823 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002824 } else {
2825 for (int i = tasks.size() - 1; i >= 0; i--) {
2826 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2827 REMOVE_FROM_RECENTS);
2828 }
2829 }
2830 }
2831
2832 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002833 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002834 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2835 * instead moved back onto the fullscreen stack.
2836 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002837 void removeStack(ActivityStack stack) {
2838 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002839 }
2840
Wale Ogunwale68278562017-09-23 17:13:55 -07002841 /**
2842 * Removes stacks in the input windowing modes from the system if they are of activity type
2843 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2844 */
2845 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002846 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002847 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2848 }
2849 }
2850
2851 void removeStacksWithActivityTypes(int... activityTypes) {
2852 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2853 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002854 }
Robert Carr6914f082017-03-20 19:04:30 -07002855 }
2856
2857 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002858 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2859 */
2860 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2861 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2862 }
2863
2864 /**
Winson Chung010927a2016-12-15 16:12:35 -08002865 * Removes the task with the specified task id.
2866 *
2867 * @param taskId Identifier of the task to be removed.
2868 * @param killProcess Kill any process associated with the task if possible.
2869 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002870 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2871 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002872 * @return Returns true if the given task was found and removed.
2873 */
Winson Chung6954fc92017-03-24 16:22:12 -07002874 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2875 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002876 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002877 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002878 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002879 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2880 if (tr.isPersistable) {
2881 mService.notifyTaskPersisterLocked(null, true);
2882 }
2883 return true;
2884 }
2885 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2886 return false;
2887 }
2888
2889 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2890 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002891 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002892 }
2893 ComponentName component = tr.getBaseIntent().getComponent();
2894 if (component == null) {
2895 Slog.w(TAG, "No component for base intent of task: " + tr);
2896 return;
2897 }
2898
2899 // Find any running services associated with this app and stop if needed.
2900 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2901
2902 if (!killProcess) {
2903 return;
2904 }
2905
2906 // Determine if the process(es) for this task should be killed.
2907 final String pkg = component.getPackageName();
2908 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2909 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2910 for (int i = 0; i < pmap.size(); i++) {
2911
2912 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2913 for (int j = 0; j < uids.size(); j++) {
2914 ProcessRecord proc = uids.valueAt(j);
2915 if (proc.userId != tr.userId) {
2916 // Don't kill process for a different user.
2917 continue;
2918 }
2919 if (proc == mService.mHomeProcess) {
2920 // Don't kill the home process along with tasks from the same package.
2921 continue;
2922 }
2923 if (!proc.pkgList.containsKey(pkg)) {
2924 // Don't kill process that is not associated with this task.
2925 continue;
2926 }
2927
2928 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002929 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002930 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2931 // Don't kill process(es) that has an activity in a different task that is
2932 // also in recents.
2933 return;
2934 }
2935 }
2936
2937 if (proc.foregroundServices) {
2938 // Don't kill process(es) with foreground service.
2939 return;
2940 }
2941
2942 // Add process to kill list.
2943 procsToKill.add(proc);
2944 }
2945 }
2946
2947 // Kill the running processes.
2948 for (int i = 0; i < procsToKill.size(); i++) {
2949 ProcessRecord pr = procsToKill.get(i);
2950 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2951 && pr.curReceivers.isEmpty()) {
2952 pr.kill("remove task", true);
2953 } else {
2954 // We delay killing processes that are not in the background or running a receiver.
2955 pr.waitingToKill = "remove task";
2956 }
2957 }
2958 }
2959
Craig Mautner4a1cb222013-12-04 16:14:06 -08002960 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002961 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002962 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002963 break;
2964 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002965 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002967 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002968 }
2969
Chong Zhang5dcb2752015-08-18 13:50:26 -07002970 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002971 * Called to restore the state of the task into the stack that it's supposed to go into.
2972 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002973 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002974 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002975 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002976 * @return true if the task has been restored successfully.
2977 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002978 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2979 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002980 final ActivityStack currentStack = task.getStack();
2981 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002982 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002983 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002984 // Nothing else to do since it is already restored in the right stack.
2985 return true;
2986 }
2987 // Remove current stack association, so we can re-associate the task with the
2988 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002989 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002990 }
2991
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002992 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002993 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002994 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002995 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2996 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002997 final ArrayList<ActivityRecord> activities = task.mActivities;
2998 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002999 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003000 }
3001 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003002 }
3003
Winson Chung1dbc8112017-09-28 18:05:31 -07003004 @Override
3005 public void onRecentTaskAdded(TaskRecord task) {
3006 task.touchActiveTime();
3007 }
3008
3009 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003010 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
Winson Chung14cfbb42017-10-20 13:54:39 -07003011 // TODO: Trim active task once b/68045330 is fixed
Winson Chung1dbc8112017-09-28 18:05:31 -07003012 task.removedFromRecents();
3013 }
3014
Wale Ogunwale040b4702015-08-06 18:10:50 -07003015 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003016 * Move stack with all its existing content to specified display.
3017 * @param stackId Id of stack to move.
3018 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003019 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003020 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003021 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003022 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003023 if (activityDisplay == null) {
3024 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3025 + displayId);
3026 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003027 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003028 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003029 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3030 + stackId);
3031 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003032
3033 final ActivityDisplay currentDisplay = stack.getDisplay();
3034 if (currentDisplay == null) {
3035 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3036 + " is not attached to any display.");
3037 }
3038
3039 if (currentDisplay.mDisplayId == displayId) {
3040 throw new IllegalArgumentException("Trying to move stack=" + stack
3041 + " to its current displayId=" + displayId);
3042 }
3043
3044 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003045 // TODO(multi-display): resize stacks properly if moved from split-screen.
3046 }
3047
3048 /**
Winson Chung74666102017-02-22 17:49:24 -08003049 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3050 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003051 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003052 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003053 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003054 final int stackId = stack.mStackId;
3055 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003056
Winson Chung74666102017-02-22 17:49:24 -08003057 // Check that we aren't reparenting to the same stack that the task is already in
3058 if (prevStack != null && prevStack.mStackId == stackId) {
3059 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3060 + " already in stackId=" + stackId);
3061 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003062 }
3063
Winson Chung74666102017-02-22 17:49:24 -08003064 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3065 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003066 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003067 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003068 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003069 }
3070
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003071 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3072 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003073 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003074 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3075 + " reparent task=" + task + " to stackId=" + stackId);
3076 }
3077
Winson Chung74666102017-02-22 17:49:24 -08003078 // Ensure that we aren't trying to move into a freeform stack without freeform
3079 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003080 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3081 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003082 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3083 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003084 }
3085
Winson Chung74666102017-02-22 17:49:24 -08003086 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3087 // used for split-screen mode and will cause things like the docked divider to show up. We
3088 // instead leave the task in its current stack or move it to the fullscreen stack if it
3089 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003090 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003091 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3092 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003093 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3094 // for example, a docked stack is created which will lead to the stack we are moving
3095 // from being resized and and its resizeable tasks being resized.
3096 try {
3097 task.mTemporarilyUnresizable = true;
3098 stack = stack.getDisplay().createStack(
3099 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3100 } finally {
3101 task.mTemporarilyUnresizable = false;
3102 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003103 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003104 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003105 }
3106
Winson Chung08f81892017-03-02 15:40:51 -08003107 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003108 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003109 if (stack == null) {
3110 throw new IllegalArgumentException(
3111 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3112 }
3113
3114 final ActivityRecord r = stack.topRunningActivityLocked();
3115 if (r == null) {
3116 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3117 + " in stack=" + stack);
3118 return false;
3119 }
3120
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003121 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003122 Slog.w(TAG,
3123 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003124 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003125 return false;
3126 }
3127
Winson Chung3a682872017-04-10 14:38:05 -07003128 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003129 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003130 return true;
3131 }
3132
Winson Chung8bca9e42017-04-16 15:59:43 -07003133 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003134 boolean moveHomeStackToFront, String reason) {
3135
Wale Ogunwale480dca02016-02-06 13:58:29 -08003136 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003137
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003138 final ActivityDisplay display = r.getStack().getDisplay();
3139 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003140
Bryce Lee04ab3462017-04-10 15:06:33 -07003141 // This will clear the pinned stack by moving an existing task to the full screen stack,
3142 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003143 if (stack != null) {
3144 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3145 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003146
Wale Ogunwale1666e312016-12-16 11:27:18 -08003147 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003148 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003149
Wale Ogunwale480dca02016-02-06 13:58:29 -08003150 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003151 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003152 // Resize the pinned stack to match the current size of the task the activity we are
3153 // going to be moving is currently contained in. We do this to have the right starting
3154 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003155 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003156 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003157 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003158
3159 if (task.mActivities.size() == 1) {
3160 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003161 // the stack without re-parenting the activity in a different task. We don't
3162 // move the home stack forward if we are currently entering picture-in-picture
3163 // while pausing because that changes the focused stack and may prevent the new
3164 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003165 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003166 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003167 // Move the home stack forward if the task we just moved to the pinned stack
3168 // was launched from home so home should be visible behind it.
3169 moveHomeStackToFront(reason);
3170 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003171 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003172 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3173 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003174 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003175 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003176 // reveal/leave the other activities in their original task.
3177
3178 // Currently, we don't support reparenting activities across tasks in two different
3179 // stacks, so instead, just create a new task in the same stack, reparent the
3180 // activity into that task, and then reparent the whole task to the new stack. This
3181 // ensures that all the necessary work to migrate states in the old and new stacks
3182 // is also done.
3183 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003184 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003185 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3186
Winson Chung5af42fc2017-03-24 17:11:33 -07003187 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003188 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003189 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003190 }
Winson Chungc2baac02017-01-11 13:34:47 -08003191
3192 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3193 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003194 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003195 } finally {
3196 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003197 }
3198
Winson Chunge7ba6862017-05-24 12:13:33 -07003199 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3200 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3201 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003202 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3203
Winson Chunge7ba6862017-05-24 12:13:33 -07003204 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3205 true /* fromFullscreen */);
3206
3207 // Update the visibility of all activities after the they have been reparented to the new
3208 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3209 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3210 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003211 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003212 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003213
Wale Ogunwale89be5762017-10-04 13:27:49 -07003214 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003215 }
3216
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003217 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003218 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3219 if (r == null || !r.isFocusable()) {
3220 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3221 "moveActivityStackToFront: unfocusable r=" + r);
3222 return false;
3223 }
3224
Bryce Leeaf691c02017-03-20 14:20:22 -07003225 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003226 final ActivityStack stack = r.getStack();
3227 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003228 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3229 + r + " task=" + task);
3230 return false;
3231 }
3232
Chong Zhang6cda19c2016-06-14 19:07:56 -07003233 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3234 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3235 "moveActivityStackToFront: already on top, r=" + r);
3236 return false;
3237 }
3238
3239 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3240 "moveActivityStackToFront: r=" + r);
3241
3242 stack.moveToFront(reason, task);
3243 return true;
3244 }
3245
David Stevensc6b91c62017-02-08 14:23:58 -08003246 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003247 mTmpFindTaskResult.r = null;
3248 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003249 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003250 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003251 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003252 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3253 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3254 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003255 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003256 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3257 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003258 continue;
3259 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003260 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003261 // It is possible to have tasks in multiple stacks with the same root affinity, so
3262 // we should keep looking after finding an affinity match to see if there is a
3263 // better match in another stack. Also, task affinity isn't a good enough reason
3264 // to target a display which isn't the source of the intent, so skip any affinity
3265 // matches not on the specified display.
3266 if (mTmpFindTaskResult.r != null) {
3267 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3268 return mTmpFindTaskResult.r;
3269 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003270 // Note: since the traversing through the stacks is top down, the floating
3271 // tasks should always have lower priority than any affinity-matching tasks
3272 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003273 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003274 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3275 Slog.d(TAG_TASKS, "Skipping match on different display "
3276 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003277 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003278 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003279 }
3280 }
Winson Chung5b895b72017-05-01 13:46:25 -07003281
David Stevensc6b91c62017-02-08 14:23:58 -08003282 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3283 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003284 }
3285
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003286 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003287 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003288 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003289 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3290 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3291 final ActivityStack stack = display.getChildAt(stackNdx);
3292 final ActivityRecord ar = stack.findActivityLocked(
3293 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003294 if (ar != null) {
3295 return ar;
3296 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003297 }
3298 }
3299 return null;
3300 }
3301
David Stevens9440dc82017-03-16 19:00:20 -07003302 boolean hasAwakeDisplay() {
3303 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3304 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3305 if (!display.shouldSleep()) {
3306 return true;
3307 }
3308 }
3309 return false;
3310 }
3311
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003313 scheduleSleepTimeout();
3314 if (!mGoingToSleep.isHeld()) {
3315 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003316 if (mLaunchingActivity.isHeld()) {
3317 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3318 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003319 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003320 mLaunchingActivity.release();
3321 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003322 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003323 }
David Stevens9440dc82017-03-16 19:00:20 -07003324
3325 applySleepTokensLocked(false /* applyToStacks */);
3326
3327 checkReadyForSleepLocked(true /* allowDelay */);
3328 }
3329
3330 void prepareForShutdownLocked() {
3331 for (int i = 0; i < mActivityDisplays.size(); i++) {
3332 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3333 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003334 }
3335
3336 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003337 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003338
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003339 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003340 final long endTime = System.currentTimeMillis() + timeout;
3341 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003342 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003343 long timeRemaining = endTime - System.currentTimeMillis();
3344 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003345 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003346 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003347 } catch (InterruptedException e) {
3348 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003349 } else {
3350 Slog.w(TAG, "Activity manager shutdown timed out");
3351 timedout = true;
3352 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003353 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003354 } else {
3355 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 }
3357 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003358
3359 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003360 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003361
Craig Mautner8d341ef2013-03-26 09:03:27 -07003362 return timedout;
3363 }
3364
3365 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003366 removeSleepTimeouts();
3367 if (mGoingToSleep.isHeld()) {
3368 mGoingToSleep.release();
3369 }
David Stevens9440dc82017-03-16 19:00:20 -07003370 }
3371
3372 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003373 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003374 // Set the sleeping state of the display.
3375 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3376 final boolean displayShouldSleep = display.shouldSleep();
3377 if (displayShouldSleep == display.isSleeping()) {
3378 continue;
3379 }
3380 display.setIsSleeping(displayShouldSleep);
3381
3382 if (!applyToStacks) {
3383 continue;
3384 }
3385
3386 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003387 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3388 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003389 if (displayShouldSleep) {
3390 stack.goToSleepIfPossible(false /* shuttingDown */);
3391 } else {
3392 stack.awakeFromSleepingLocked();
3393 if (isFocusedStack(stack)) {
3394 resumeFocusedStackTopActivityLocked();
3395 }
3396 }
3397 }
3398
3399 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3400 continue;
3401 }
3402 // The display is awake now, so clean up the going to sleep list.
3403 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3404 final ActivityRecord r = it.next();
3405 if (r.getDisplayId() == display.mDisplayId) {
3406 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 }
Craig Mautner5314a402013-09-26 12:40:16 -07003408 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003410 }
3411
3412 void activitySleptLocked(ActivityRecord r) {
3413 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003414 final ActivityStack s = r.getStack();
3415 if (s != null) {
3416 s.checkReadyForSleep();
3417 } else {
3418 checkReadyForSleepLocked(true);
3419 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003420 }
3421
David Stevens9440dc82017-03-16 19:00:20 -07003422 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003423 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003424 // Do not care.
3425 return;
3426 }
3427
David Stevens18abd0e2017-08-17 14:55:47 -07003428 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3429 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003430 }
3431
Wei Wang65c7a152016-06-02 18:51:22 -07003432 // Send launch end powerhint before going sleep
3433 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3434
Craig Mautner0eea92c2013-05-16 13:35:39 -07003435 removeSleepTimeouts();
3436
3437 if (mGoingToSleep.isHeld()) {
3438 mGoingToSleep.release();
3439 }
3440 if (mService.mShuttingDown) {
3441 mService.notifyAll();
3442 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003443 }
3444
David Stevens18abd0e2017-08-17 14:55:47 -07003445 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3446 // successfully put to sleep.
3447 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3448 boolean allSleep = true;
3449 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003450 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3451 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3452 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003453 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003454 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003455 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003456 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003457 }
3458 }
3459 }
3460 return allSleep;
3461 }
3462
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003463 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003464 // A resumed activity cannot be stopping. remove from list
3465 mStoppingActivities.remove(r);
3466
Andrii Kulian02b7a832016-10-06 23:11:56 -07003467 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003468 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003469 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003470 }
3471 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003472 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003473 mWindowManager.executeAppTransition();
3474 return true;
3475 }
3476 return false;
3477 }
3478
Craig Mautner8d341ef2013-03-26 09:03:27 -07003479 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003480 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003481 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3482 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3483 final ActivityStack stack = display.getChildAt(stackNdx);
3484 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003485 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003486 }
3487 }
3488
Craig Mautnerbb742462014-07-07 15:28:55 -07003489 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003490 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003491 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003492 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003493
3494 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003495 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003496 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003497 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003498
3499 // When launching tasks behind, update the last active time of the top task after the new
3500 // task has been shown briefly
3501 final ActivityRecord top = stack.topActivity();
3502 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003503 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003504 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003505 }
3506
3507 void scheduleLaunchTaskBehindComplete(IBinder token) {
3508 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3509 }
3510
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003511 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3512 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003513 mKeyguardController.beginActivityVisibilityUpdate();
3514 try {
3515 // First the front stacks. In case any are not fullscreen and are in front of home.
3516 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003517 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3518 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3519 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003520 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3521 }
Craig Mautner580ea812013-04-25 12:58:38 -07003522 }
Jorim Jaggife762342016-10-13 14:33:27 +02003523 } finally {
3524 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003525 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003526 }
3527
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003528 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3529 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003530 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3531 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3532 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003533 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3534 }
3535 }
3536 }
3537
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003538 void invalidateTaskLayers() {
3539 mTaskLayersChanged = true;
3540 }
3541
3542 void rankTaskLayersIfNeeded() {
3543 if (!mTaskLayersChanged) {
3544 return;
3545 }
3546 mTaskLayersChanged = false;
3547 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003548 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003549 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003550 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3551 final ActivityStack stack = display.getChildAt(stackNdx);
3552 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003553 }
3554 }
3555 }
3556
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003557 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3558 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003559 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3560 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3561 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003562 stack.clearOtherAppTimeTrackers(except);
3563 }
3564 }
3565 }
3566
Craig Mautner8d341ef2013-03-26 09:03:27 -07003567 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003569 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3570 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3571 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003572 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003573 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003574 }
3575 }
3576
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003577 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3578 // Examine all activities currently running in the process.
3579 TaskRecord firstTask = null;
3580 // Tasks is non-null only if two or more tasks are found.
3581 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003582 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3583 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003584 ActivityRecord r = app.activities.get(i);
3585 // First, if we find an activity that is in the process of being destroyed,
3586 // then we just aren't going to do anything for now; we want things to settle
3587 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003588 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003590 return;
3591 }
3592 // Don't consider any activies that are currently not in a state where they
3593 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003594 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3595 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003596 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003597 continue;
3598 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003599
3600 final TaskRecord task = r.getTask();
3601 if (task != null) {
3602 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003603 + " from " + r);
3604 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003605 firstTask = task;
3606 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003607 if (tasks == null) {
3608 tasks = new ArraySet<>();
3609 tasks.add(firstTask);
3610 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003611 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003612 }
3613 }
3614 }
3615 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003616 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 return;
3618 }
3619 // If we have activities in multiple tasks that are in a position to be destroyed,
3620 // let's iterate through the tasks and release the oldest one.
3621 final int numDisplays = mActivityDisplays.size();
3622 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003623 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3624 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003625 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003626 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3627 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003628 // Try to release activities in this stack; if we manage to, we are done.
3629 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3630 return;
3631 }
3632 }
3633 }
3634 }
3635
Amith Yamasani37a40c22015-06-17 13:25:42 -07003636 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003637 final int focusStackId = mFocusedStack.getStackId();
3638 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003639 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003640 if (dockedStack != null) {
3641 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3642 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003643 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3644 // also cause all tasks to be moved to the fullscreen stack at a position that is
3645 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003646 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003647
3648 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003649 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003650 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003651
Craig Mautner858d8a62013-04-23 17:08:34 -07003652 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003653 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003654 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3655 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3656 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003657 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003658 TaskRecord task = stack.topTask();
3659 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003660 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003661 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003662 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003663 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003664
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003665 ActivityStack stack = getStack(restoreStackId);
3666 if (stack == null) {
3667 stack = mHomeStack;
3668 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003669 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003670 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003671 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003672 } else {
3673 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003674 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003675 }
Craig Mautner93529a42013-10-04 15:03:13 -07003676 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003677 }
3678
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003679 /** Checks whether the userid is a profile of the current user. */
3680 boolean isCurrentProfileLocked(int userId) {
3681 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003682 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003683 }
3684
Bryce Leeb7c9b802017-05-02 14:20:24 -07003685 /**
3686 * Returns whether a stopping activity is present that should be stopped after visible, rather
3687 * than idle.
3688 * @return {@code true} if such activity is present. {@code false} otherwise.
3689 */
3690 boolean isStoppingNoHistoryActivity() {
3691 // Activities that are marked as nohistory should be stopped immediately after the resumed
3692 // activity has become visible.
3693 for (ActivityRecord record : mStoppingActivities) {
3694 if (record.isNoHistory()) {
3695 return true;
3696 }
3697 }
3698
3699 return false;
3700 }
3701
Winson Chung4dabf232017-01-25 13:25:22 -08003702 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3703 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003704 ArrayList<ActivityRecord> stops = null;
3705
3706 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003707 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3708 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003709 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003710 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003711 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3712 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003713 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003714 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003715 if (s.finishing) {
3716 // If this activity is finishing, it is sitting on top of
3717 // everyone else but we now know it is no longer needed...
3718 // so get rid of it. Otherwise, we need to go through the
3719 // normal flow and hide it once we determine that it is
3720 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003721 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003722 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003723 }
3724 }
David Stevens9440dc82017-03-16 19:00:20 -07003725 if (remove) {
3726 final ActivityStack stack = s.getStack();
3727 final boolean shouldSleepOrShutDown = stack != null
3728 ? stack.shouldSleepOrShutDownActivities()
3729 : mService.isSleepingOrShuttingDownLocked();
3730 if (!waitingVisible || shouldSleepOrShutDown) {
3731 if (!processPausingActivities && s.state == PAUSING) {
3732 // Defer processing pausing activities in this iteration and reschedule
3733 // a delayed idle to reprocess it again
3734 removeTimeoutsForActivityLocked(idleActivity);
3735 scheduleIdleTimeoutLocked(idleActivity);
3736 continue;
3737 }
Winson Chung4dabf232017-01-25 13:25:22 -08003738
David Stevens9440dc82017-03-16 19:00:20 -07003739 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3740 if (stops == null) {
3741 stops = new ArrayList<>();
3742 }
3743 stops.add(s);
3744 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003745 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003746 }
3747 }
3748
3749 return stops;
3750 }
3751
Craig Mautnercf910b02013-04-23 11:23:27 -07003752 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003754 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3755 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3756 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003757 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003758 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003759 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003760 if (r == null) Slog.e(TAG,
3761 "validateTop...: null top activity, stack=" + stack);
3762 else {
3763 final ActivityRecord pausing = stack.mPausingActivity;
3764 if (pausing != null && pausing == r) Slog.e(TAG,
3765 "validateTop...: top stack has pausing activity r=" + r
3766 + " state=" + state);
3767 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3768 "validateTop...: activity in front not resumed r=" + r
3769 + " state=" + state);
3770 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003771 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003772 final ActivityRecord resumed = stack.mResumedActivity;
3773 if (resumed != null && resumed == r) Slog.e(TAG,
3774 "validateTop...: back stack has resumed activity r=" + r
3775 + " state=" + state);
3776 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3777 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003778 }
3779 }
3780 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003781 }
3782
Craig Mautner27084302013-03-25 08:05:25 -07003783 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003784 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003786 pw.print(prefix);
3787 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003788 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003789 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3790 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003791 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003792 }
Bryce Lee4a194382017-04-04 14:32:48 -07003793 if (!mWaitingForActivityVisible.isEmpty()) {
3794 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3795 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3796 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3797 }
3798 }
3799
Jorim Jaggi8d786932016-10-26 19:08:36 -07003800 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003801 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003802 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003803
Yi Jin129fc6c2017-09-28 15:48:38 -07003804 public void writeToProto(ProtoOutputStream proto) {
Steven Timotius4346f0a2017-09-12 11:07:21 -07003805 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3806 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3807 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3808 activityDisplay.writeToProto(proto, DISPLAYS);
3809 }
3810 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3811 if (mFocusedStack != null) {
3812 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3813 ActivityRecord focusedActivity = getResumedActivityLocked();
3814 if (focusedActivity != null) {
3815 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3816 }
3817 } else {
3818 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3819 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003820 }
3821
Winson43d1f262016-06-14 16:05:55 -07003822 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003823 * Dump all connected displays' configurations.
3824 * @param prefix Prefix to apply to each line of the dump.
3825 */
3826 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3827 pw.print(prefix); pw.println("Display override configurations:");
3828 final int displayCount = mActivityDisplays.size();
3829 for (int i = 0; i < displayCount; i++) {
3830 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3831 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3832 pw.println(activityDisplay.getOverrideConfiguration());
3833 }
3834 }
3835
3836 /**
Winson43d1f262016-06-14 16:05:55 -07003837 * Dumps the activities matching the given {@param name} in the either the focused stack
3838 * or all visible stacks if {@param dumpVisibleStacks} is true.
3839 */
Winson Chung6998bc42017-02-28 17:07:05 -08003840 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3841 boolean dumpFocusedStackOnly) {
3842 if (dumpFocusedStackOnly) {
3843 return mFocusedStack.getDumpActivitiesLocked(name);
3844 } else {
Winson43d1f262016-06-14 16:05:55 -07003845 ArrayList<ActivityRecord> activities = new ArrayList<>();
3846 int numDisplays = mActivityDisplays.size();
3847 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003848 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3849 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3850 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003851 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003852 activities.addAll(stack.getDumpActivitiesLocked(name));
3853 }
3854 }
3855 }
3856 return activities;
Winson43d1f262016-06-14 16:05:55 -07003857 }
Craig Mautner20e72272013-04-01 13:45:53 -07003858 }
3859
Dianne Hackborn390517b2013-05-30 15:03:32 -07003860 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3861 boolean needSep, String prefix) {
3862 if (activity != null) {
3863 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3864 if (needSep) {
3865 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003866 }
3867 pw.print(prefix);
3868 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003869 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003870 }
3871 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003872 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003873 }
3874
Craig Mautner8d341ef2013-03-26 09:03:27 -07003875 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3876 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003877 boolean printed = false;
3878 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003879 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3880 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003881 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003882 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003883 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3884 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3885 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003886 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003887 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003888 + ": type=" + activityTypeToString(stack.getActivityType())
3889 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003890 pw.println(" mFullscreen=" + stack.mFullscreen);
3891 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3892 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003893
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003894 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3895 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003896
Craig Mautner4a1cb222013-12-04 16:14:06 -08003897 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3898 !dumpAll, false, dumpPackage, true,
3899 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003900
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 needSep = printed;
3902 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3903 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003904 if (pr) {
3905 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003906 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003907 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3909 " mResumedActivity: ");
3910 if (pr) {
3911 printed = true;
3912 needSep = false;
3913 }
3914 if (dumpAll) {
3915 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3916 " mLastPausedActivity: ");
3917 if (pr) {
3918 printed = true;
3919 needSep = true;
3920 }
3921 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3922 needSep, " mLastNoHistoryActivity: ");
3923 }
3924 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003925 }
3926 }
3927
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003928 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3929 false, dumpPackage, true, " Activities waiting to finish:", null);
3930 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3931 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003932 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3933 false, !dumpAll, false, dumpPackage, true,
3934 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003935 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3936 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003937
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003938 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003939 }
3940
Dianne Hackborn390517b2013-05-30 15:03:32 -07003941 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003942 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003943 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003944 String innerPrefix = null;
3945 String[] args = null;
3946 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003947 for (int i=list.size()-1; i>=0; i--) {
3948 final ActivityRecord r = list.get(i);
3949 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3950 continue;
3951 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003952 if (innerPrefix == null) {
3953 innerPrefix = prefix + " ";
3954 args = new String[0];
3955 }
3956 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003957 final boolean full = !brief && (complete || !r.isInHistory());
3958 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003959 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003960 needNL = false;
3961 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003962 if (header != null) {
3963 pw.println(header);
3964 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003965 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003966 if (lastTask != r.getTask()) {
3967 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003968 pw.print(prefix);
3969 pw.print(full ? "* " : " ");
3970 pw.println(lastTask);
3971 if (full) {
3972 lastTask.dump(pw, prefix + " ");
3973 } else if (complete) {
3974 // Complete + brief == give a summary. Isn't that obvious?!?
3975 if (lastTask.intent != null) {
3976 pw.print(prefix); pw.print(" ");
3977 pw.println(lastTask.intent.toInsecureStringWithClip());
3978 }
3979 }
3980 }
3981 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3982 pw.print(" #"); pw.print(i); pw.print(": ");
3983 pw.println(r);
3984 if (full) {
3985 r.dump(pw, innerPrefix);
3986 } else if (complete) {
3987 // Complete + brief == give a summary. Isn't that obvious?!?
3988 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3989 if (r.app != null) {
3990 pw.print(innerPrefix); pw.println(r.app);
3991 }
3992 }
3993 if (client && r.app != null && r.app.thread != null) {
3994 // flush anything that is already in the PrintWriter since the thread is going
3995 // to write to the file descriptor directly
3996 pw.flush();
3997 try {
3998 TransferPipe tp = new TransferPipe();
3999 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004000 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004001 // Short timeout, since blocking here can
4002 // deadlock with the application.
4003 tp.go(fd, 2000);
4004 } finally {
4005 tp.kill();
4006 }
4007 } catch (IOException e) {
4008 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4009 } catch (RemoteException e) {
4010 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4011 }
4012 needNL = true;
4013 }
4014 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004015 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004017
Craig Mautnerf3333272013-04-22 10:55:53 -07004018 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004019 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4020 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004021 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4022 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004023 }
4024
4025 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004026 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004027 }
4028
4029 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004030 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4031 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004032 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4033 }
4034
Craig Mautner05d29032013-05-03 13:40:13 -07004035 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004036 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4037 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4038 }
Craig Mautner05d29032013-05-03 13:40:13 -07004039 }
4040
Craig Mautner0eea92c2013-05-16 13:35:39 -07004041 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004042 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4043 }
4044
4045 final void scheduleSleepTimeout() {
4046 removeSleepTimeouts();
4047 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4048 }
4049
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 @Override
4051 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004052 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4054 }
4055
4056 @Override
4057 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004058 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004059 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4060 }
4061
4062 @Override
4063 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004064 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4066 }
4067
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004068 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004069 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004070 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 }
4073
Andrii Kulianca007a62016-11-22 16:33:28 -08004074 /** Check if display with specified id is added to the list. */
4075 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004076 return getActivityDisplayOrCreateLocked(displayId) != null;
4077 }
4078
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004079 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004080 ActivityDisplay getActivityDisplay(int displayId) {
4081 return mActivityDisplays.get(displayId);
4082 }
4083
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004084 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4085 ActivityDisplay getDefaultDisplay() {
4086 return mActivityDisplays.get(DEFAULT_DISPLAY);
4087 }
4088
Andrii Kulian62e6f252017-05-30 22:46:53 -07004089 /**
4090 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4091 * corresponding record in display manager.
4092 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004093 // TODO: Look into consolidating with getActivityDisplay()
4094 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004095 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4096 if (activityDisplay != null) {
4097 return activityDisplay;
4098 }
4099 if (mDisplayManager == null) {
4100 // The system isn't fully initialized yet.
4101 return null;
4102 }
4103 final Display display = mDisplayManager.getDisplay(displayId);
4104 if (display == null) {
4105 // The display is not registered in DisplayManager.
4106 return null;
4107 }
4108 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004109 activityDisplay = new ActivityDisplay(this, displayId);
4110 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004111 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4112 mWindowManager.onDisplayAdded(displayId);
4113 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004114 }
4115
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004116 @VisibleForTesting
4117 void attachDisplay(ActivityDisplay display) {
4118 mActivityDisplays.put(display.mDisplayId, display);
4119 }
4120
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004121 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004122 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004123 mService.mContext.getResources().getDimensionPixelSize(
4124 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004125 }
4126
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004127 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004128 if (displayId == DEFAULT_DISPLAY) {
4129 throw new IllegalArgumentException("Can't remove the primary display.");
4130 }
4131
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004133 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4134 if (activityDisplay == null) {
4135 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004137 final boolean destroyContentOnRemoval
4138 = activityDisplay.shouldDestroyContentOnRemove();
4139 while (activityDisplay.getChildCount() > 0) {
4140 final ActivityStack stack = activityDisplay.getChildAt(0);
4141 if (destroyContentOnRemoval) {
4142 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4143 stack.finishAllActivitiesLocked(true /* immediately */);
4144 } else {
4145 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4146 // a valid launch stack for all activities. This way the task history from
4147 // external display will be preserved on primary after move.
4148 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4149 }
4150 }
4151
4152 releaseSleepTokens(activityDisplay);
4153
4154 mActivityDisplays.remove(displayId);
4155 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004156 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 }
4158
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004159 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004161 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4162 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004163 // The window policy is responsible for stopping activities on the default display
4164 if (displayId != Display.DEFAULT_DISPLAY) {
4165 int displayState = activityDisplay.mDisplay.getState();
4166 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4167 activityDisplay.mOffToken =
4168 mService.acquireSleepToken("Display-off", displayId);
4169 } else if (displayState == Display.STATE_ON
4170 && activityDisplay.mOffToken != null) {
4171 activityDisplay.mOffToken.release();
4172 activityDisplay.mOffToken = null;
4173 }
4174 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 // TODO: Update the bounds.
4176 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004177 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004178 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004179 }
4180
David Stevens9440dc82017-03-16 19:00:20 -07004181 SleepToken createSleepTokenLocked(String tag, int displayId) {
4182 ActivityDisplay display = mActivityDisplays.get(displayId);
4183 if (display == null) {
4184 throw new IllegalArgumentException("Invalid display: " + displayId);
4185 }
4186
4187 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4188 mSleepTokens.add(token);
4189 display.mAllSleepTokens.add(token);
4190 return token;
4191 }
4192
4193 private void removeSleepTokenLocked(SleepTokenImpl token) {
4194 mSleepTokens.remove(token);
4195
4196 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4197 if (display != null) {
4198 display.mAllSleepTokens.remove(token);
4199 if (display.mAllSleepTokens.isEmpty()) {
4200 mService.updateSleepIfNeededLocked();
4201 }
4202 }
4203 }
4204
4205 private void releaseSleepTokens(ActivityDisplay display) {
4206 if (display.mAllSleepTokens.isEmpty()) {
4207 return;
4208 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004209 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004210 mSleepTokens.remove(token);
4211 }
4212 display.mAllSleepTokens.clear();
4213
4214 mService.updateSleepIfNeededLocked();
4215 }
4216
Wale Ogunwale68278562017-09-23 17:13:55 -07004217 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004218 final int displayId = stack.mDisplayId;
4219 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004220 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004221 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004222 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004223 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004224 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004225 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004226 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004227 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004228 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004229
4230 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4231 final int numTasks = tasks.size();
4232 int[] taskIds = new int[numTasks];
4233 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004234 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004235 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004236 for (int i = 0; i < numTasks; ++i) {
4237 final TaskRecord task = tasks.get(i);
4238 taskIds[i] = task.taskId;
4239 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4240 : task.realActivity != null ? task.realActivity.flattenToString()
4241 : task.getTopActivity() != null ? task.getTopActivity().packageName
4242 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004243 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004244 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004245 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004246 }
4247 info.taskIds = taskIds;
4248 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004249 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004250 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004251
4252 final ActivityRecord top = stack.topRunningActivityLocked();
4253 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004254 return info;
4255 }
4256
Wale Ogunwale68278562017-09-23 17:13:55 -07004257 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004258 ActivityStack stack = getStack(stackId);
4259 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004260 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004261 }
4262 return null;
4263 }
4264
Wale Ogunwale68278562017-09-23 17:13:55 -07004265 StackInfo getStackInfo(int windowingMode, int activityType) {
4266 final ActivityStack stack = getStack(windowingMode, activityType);
4267 return (stack != null) ? getStackInfo(stack) : null;
4268 }
4269
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004271 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004272 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004273 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4274 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4275 final ActivityStack stack = display.getChildAt(stackNdx);
4276 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 }
4278 }
4279 return list;
4280 }
4281
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004282 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004283 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004284 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004285 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004286 }
4287
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004288 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004289 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004290 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004291 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004292 final boolean inSplitScreenMode = actualStack != null
4293 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004294 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004295 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004296 return;
4297 }
4298
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004299 // Handle incorrect launch/move to secondary display if needed.
4300 final boolean launchOnSecondaryDisplayFailed;
4301 if (isSecondaryDisplayPreferred) {
4302 final int actualDisplayId = task.getStack().mDisplayId;
4303 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4304 // The task landed on an inappropriate display somehow, move it to the default
4305 // display.
4306 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004307 mService.setTaskWindowingMode(task.taskId,
4308 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004309 launchOnSecondaryDisplayFailed = true;
4310 } else {
4311 // The task might have landed on a display different from requested.
4312 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4313 || (preferredDisplayId != INVALID_DISPLAY
4314 && preferredDisplayId != actualDisplayId);
4315 }
4316 } else {
4317 // The task wasn't requested to be on a secondary display.
4318 launchOnSecondaryDisplayFailed = false;
4319 }
4320
Jorim Jaggicd13d332016-04-27 15:40:20 -07004321 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004322 if (launchOnSecondaryDisplayFailed
4323 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004324 if (launchOnSecondaryDisplayFailed) {
4325 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4326 // display with config different from global config.
4327 mService.mTaskChangeNotificationController
4328 .notifyActivityLaunchOnSecondaryDisplayFailed();
4329 } else {
4330 // Display a warning toast that we tried to put a non-dockable task in the docked
4331 // stack.
4332 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4333 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004334
Andrii Kulianc27916642016-04-12 17:59:27 -07004335 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4336 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004337
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004338 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004339 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004340 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004341 }
Winson Chungd3395382016-12-13 11:49:09 -08004342 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004343 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004344 final String packageName = topActivity.appInfo.packageName;
4345 final int reason = isSecondaryDisplayPreferred
4346 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4347 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004348 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004349 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004350 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004351 }
4352
Jorim Jaggife89d122015-12-22 16:28:44 +01004353 void activityRelaunchedLocked(IBinder token) {
4354 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004355 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4356 if (r != null) {
4357 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004358 r.setSleeping(true, true);
4359 }
4360 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004361 }
4362
4363 void activityRelaunchingLocked(ActivityRecord r) {
4364 mWindowManager.notifyAppRelaunching(r.appToken);
4365 }
4366
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004367 void logStackState() {
4368 mActivityMetricsLogger.logWindowState();
4369 }
4370
Winson Chung5af42fc2017-03-24 17:11:33 -07004371 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004372 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4373 // end, then ensure that we defer all in between multi-window mode changes
4374 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4375 return;
4376 }
4377
Wale Ogunwale22e25262016-02-01 10:32:02 -08004378 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4379 final ActivityRecord r = task.mActivities.get(i);
4380 if (r.app != null && r.app.thread != null) {
4381 mMultiWindowModeChangedActivities.add(r);
4382 }
4383 }
4384
4385 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4386 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4387 }
4388 }
4389
Winson Chung5af42fc2017-03-24 17:11:33 -07004390 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004391 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004392 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004393 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004394 return;
4395 }
4396
Winson Chungab76bbc2017-08-14 13:33:51 -07004397 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004398 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004399
Winson Chungab76bbc2017-08-14 13:33:51 -07004400 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4401 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4402 final ActivityRecord r = task.mActivities.get(i);
4403 if (r.app != null && r.app.thread != null) {
4404 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004405 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004406 }
4407 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004408
Winson Chungab76bbc2017-08-14 13:33:51 -07004409 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4410 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4411 }
4412 }
4413
4414 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4415 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4416 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4417 final ActivityRecord r = task.mActivities.get(i);
4418 if (r.app != null && r.app.thread != null) {
4419 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004420 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004421 }
4422 }
4423
Tony Mak853304c2016-04-18 15:17:41 +01004424 void setDockedStackMinimized(boolean minimized) {
4425 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004426 }
4427
chaviw59b98852017-06-13 12:05:44 -07004428 void wakeUp(String reason) {
4429 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4430 }
4431
4432 /**
4433 * Begin deferring resume to avoid duplicate resumes in one pass.
4434 */
4435 private void beginDeferResume() {
4436 mDeferResumeCount++;
4437 }
4438
4439 /**
4440 * End deferring resume and determine if resume can be called.
4441 */
4442 private void endDeferResume() {
4443 mDeferResumeCount--;
4444 }
4445
4446 /**
4447 * @return True if resume can be called.
4448 */
4449 private boolean readyToResume() {
4450 return mDeferResumeCount == 0;
4451 }
4452
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004453 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004454
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004455 public ActivityStackSupervisorHandler(Looper looper) {
4456 super(looper);
4457 }
4458
Winson Chung4dabf232017-01-25 13:25:22 -08004459 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004460 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004461 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4462 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004463 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004464 }
4465
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004466 @Override
4467 public void handleMessage(Message msg) {
4468 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004469 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4470 synchronized (mService) {
4471 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4472 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004473 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004474 }
4475 }
4476 } break;
4477 case REPORT_PIP_MODE_CHANGED_MSG: {
4478 synchronized (mService) {
4479 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4480 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004481 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4482 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004483 }
4484 }
4485 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004486 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004487 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4488 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004489 // We don't at this point know if the activity is fullscreen,
4490 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004491 activityIdleInternal((ActivityRecord) msg.obj,
4492 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004493 } break;
4494 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004495 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004496 activityIdleInternal((ActivityRecord) msg.obj,
4497 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004498 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004499 case RESUME_TOP_ACTIVITY_MSG: {
4500 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004501 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004502 }
4503 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004504 case SLEEP_TIMEOUT_MSG: {
4505 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004506 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004507 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004508 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004509 }
4510 }
4511 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004512 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004513 synchronized (mService) {
4514 if (mLaunchingActivity.isHeld()) {
4515 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4516 if (VALIDATE_WAKE_LOCK_CALLER
4517 && Binder.getCallingUid() != Process.myUid()) {
4518 throw new IllegalStateException("Calling must be system uid");
4519 }
4520 mLaunchingActivity.release();
4521 }
4522 }
4523 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004524 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004525 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004526 } break;
4527 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004528 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004529 } break;
4530 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004531 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004532 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004533 case LAUNCH_TASK_BEHIND_COMPLETE: {
4534 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004535 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004536 if (r != null) {
4537 handleLaunchTaskBehindCompleteLocked(r);
4538 }
4539 }
4540 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004541
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004542 }
4543 }
4544 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004545
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004546 ActivityStack findStackBehind(ActivityStack stack) {
4547 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004548 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004549 if (display == null) {
4550 return null;
4551 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004552 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4553 if (display.getChildAt(i) == stack && i > 0) {
4554 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004555 }
4556 }
4557 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004558 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004559 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004560
Jorim Jaggic69bd222016-03-15 14:38:37 +01004561 /**
4562 * Puts a task into resizing mode during the next app transition.
4563 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004564 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004565 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004566 private void setResizingDuringAnimation(TaskRecord task) {
4567 mResizingTasksDuringAnimation.add(task.taskId);
4568 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004569 }
4570
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004571 int startActivityFromRecents(int taskId, Bundle bOptions) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004572 final TaskRecord task;
4573 final int callingUid;
4574 final String callingPackage;
4575 final Intent intent;
4576 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004577 int activityType = ACTIVITY_TYPE_UNDEFINED;
4578 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004579 final ActivityOptions activityOptions = (bOptions != null)
4580 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004581 if (activityOptions != null) {
4582 activityType = activityOptions.getLaunchActivityType();
4583 windowingMode = activityOptions.getLaunchWindowingMode();
4584 }
4585 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004586 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004587 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004588 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004589
Matthew Ng606dd802017-06-05 14:06:32 -07004590 mWindowManager.deferSurfaceLayout();
4591 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004592 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004593 mWindowManager.setDockedStackCreateState(
4594 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004595
Matthew Ng606dd802017-06-05 14:06:32 -07004596 // Defer updating the stack in which recents is until the app transition is done, to
4597 // not run into issues where we still need to draw the task in recents but the
4598 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004599 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004600 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004601 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004602
Matthew Ng606dd802017-06-05 14:06:32 -07004603 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004604 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004605 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004606 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004607 mWindowManager.executeAppTransition();
4608 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004609 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004610 }
4611
4612 // If the user must confirm credentials (e.g. when first launching a work app and the
4613 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4614 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4615 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004616 final ActivityRecord targetActivity = task.getTopActivity();
4617
4618 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4619 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004620 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004621 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004622 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004623 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004624
4625 // If we are launching the task in the docked stack, put it into resizing mode so
4626 // the window renders full-screen with the background filling the void. Also only
4627 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004628 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004629 setResizingDuringAnimation(task);
4630 }
4631
4632 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004633 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004634 return ActivityManager.START_TASK_TO_FRONT;
4635 }
4636 callingUid = task.mCallingUid;
4637 callingPackage = task.mCallingPackage;
4638 intent = task.intent;
4639 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4640 userId = task.userId;
4641 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004642 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004643 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004644 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004645 }
Matthew Ng606dd802017-06-05 14:06:32 -07004646 return result;
4647 } finally {
4648 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004649 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004650 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004651
4652 /**
4653 * @return a list of activities which are the top ones in each visible stack. The first
4654 * entry will be the focused activity.
4655 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004656 List<IBinder> getTopVisibleActivities() {
4657 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4658 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004659 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004660 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4661 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004662 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4663 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004664 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004665 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004666 final ActivityRecord top = stack.topActivity();
4667 if (top != null) {
4668 if (stack == mFocusedStack) {
4669 topActivityTokens.add(0, top.appToken);
4670 } else {
4671 topActivityTokens.add(top.appToken);
4672 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004673 }
4674 }
4675 }
4676 }
4677 return topActivityTokens;
4678 }
Bryce Lee4a194382017-04-04 14:32:48 -07004679
4680 /**
4681 * Internal container to store a match qualifier alongside a WaitResult.
4682 */
4683 static class WaitInfo {
4684 private final ComponentName mTargetComponent;
4685 private final WaitResult mResult;
4686
4687 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4688 this.mTargetComponent = targetComponent;
4689 this.mResult = result;
4690 }
4691
Wale Ogunwale3270f172017-04-26 07:29:42 -07004692 public boolean matches(ComponentName targetComponent) {
4693 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004694 }
4695
4696 public WaitResult getResult() {
4697 return mResult;
4698 }
4699
4700 public ComponentName getComponent() {
4701 return mTargetComponent;
4702 }
4703
4704 public void dump(PrintWriter pw, String prefix) {
4705 pw.println(prefix + "WaitInfo:");
4706 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4707 pw.println(prefix + " mResult=");
4708 mResult.dump(pw, prefix);
4709 }
4710 }
David Stevens9440dc82017-03-16 19:00:20 -07004711
4712 private final class SleepTokenImpl extends SleepToken {
4713 private final String mTag;
4714 private final long mAcquireTime;
4715 private final int mDisplayId;
4716
4717 public SleepTokenImpl(String tag, int displayId) {
4718 mTag = tag;
4719 mDisplayId = displayId;
4720 mAcquireTime = SystemClock.uptimeMillis();
4721 }
4722
4723 @Override
4724 public void release() {
4725 synchronized (mService) {
4726 removeSleepTokenLocked(this);
4727 }
4728 }
4729
4730 @Override
4731 public String toString() {
4732 return "{\"" + mTag + "\", display " + mDisplayId
4733 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4734 }
4735 }
4736
Craig Mautner27084302013-03-25 08:05:25 -07004737}