blob: 2ed79600c72a7f6880c6e478f027d966ea4f8993 [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 Lee9ad3eb32017-10-10 10:10:31 -0700298 LaunchingTaskPositioner mTaskPositioner = new LaunchingTaskPositioner();
299
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);
Jeff Brown2c43c332014-06-12 22:38:59 -0700578 }
579
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800580 void setRecentTasks(RecentTasks recentTasks) {
581 mRecentTasks = recentTasks;
Winson Chung1dbc8112017-09-28 18:05:31 -0700582 mRecentTasks.registerCallback(this);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800583 }
584
Jeff Brown2c43c332014-06-12 22:38:59 -0700585 /**
586 * At the time when the constructor runs, the power manager has not yet been
587 * initialized. So we initialize our wakelocks afterwards.
588 */
589 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700590 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
591 mGoingToSleep = mPowerManager
592 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
593 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700594 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700595 }
596
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700597 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 synchronized (mService) {
599 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200600 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601
602 mDisplayManager =
603 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
604 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800605 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606
607 Display[] displays = mDisplayManager.getDisplays();
608 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
609 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700610 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800611 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700612 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800613 }
614
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700615 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
616 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800617
618 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619 }
Craig Mautner27084302013-03-25 08:05:25 -0700620 }
621
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700622 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700624 }
625
Craig Mautnerde4ef022013-04-07 19:01:33 -0700626 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700628 }
629
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700630 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700631 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700632 }
633
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800634 /** The top most stack on its display. */
635 boolean isFrontStackOnDisplay(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700636 return isFrontOfStackList(stack, stack.getDisplay().mStacks);
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800637 }
638
639 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800640 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700641 }
642
Wale Ogunwaled046a012015-12-24 13:05:59 -0800643 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800644 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
645 if (!focusCandidate.isFocusable()) {
646 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800647 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800648 }
649
650 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800651 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800652 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800653
Wale Ogunwaled046a012015-12-24 13:05:59 -0800654 EventLogTags.writeAmFocusedStack(
655 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
656 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
657 }
658
659 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800660 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800661 if (r != null && r.idle) {
662 checkFinishBootingLocked();
663 }
664 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700665 }
666
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700667 void moveHomeStackToFront(String reason) {
668 mHomeStack.moveToFront(reason);
669 }
670
Matthew Ng330757d2017-02-28 14:19:17 -0800671 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700672 final ActivityStack recentsStack = getDefaultDisplay().getStack(
673 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800674 if (recentsStack != null) {
675 recentsStack.moveToFront(reason);
676 }
677 }
678
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800680 boolean moveHomeStackTaskToTop(String reason) {
681 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700682
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700683 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700684 if (top == null) {
685 return false;
686 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700687 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700688 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700689 }
690
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800691 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700692 if (!mService.mBooting && !mService.mBooted) {
693 // Not ready yet!
694 return false;
695 }
696
Craig Mautner84984fa2014-06-19 11:19:20 -0700697 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700698 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700699 }
700
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800701 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700702 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800703 final String myReason = reason + " resumeHomeStackTask";
704
Mark Lua56ea122015-10-08 13:31:01 +0800705 // Only resume home activity if isn't finishing.
706 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700707 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800708 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700709 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800710 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700711 }
712
Craig Mautner8d341ef2013-03-26 09:03:27 -0700713 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700714 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
715 }
716
717 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
718 return anyTaskForIdLocked(id, matchMode, null);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700719 }
720
721 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700722 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700723 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800724 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700725 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700726 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700727 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
728 @Nullable ActivityOptions aOptions) {
Winson Chung7900bee2017-03-10 08:59:25 -0800729 // If there is a stack id set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700730 // TODO: Don't really know if this is needed...
731 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
732 throw new IllegalArgumentException("Should not specify activity options for non-restore"
733 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800734 }
735
Craig Mautnere0a38842013-12-16 16:14:02 -0800736 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800737 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800738 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
740 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700741 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 if (task != null) {
743 return task;
744 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700745 }
746 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800747
Winson Chung7900bee2017-03-10 08:59:25 -0800748 // If we are matching stack tasks only, return now
749 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800750 return null;
751 }
752
Winson Chung7900bee2017-03-10 08:59:25 -0800753 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
754 // the task from recents
755 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700756 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700757
758 if (task == null) {
759 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800760 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
761 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700762
763 return null;
764 }
765
766 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700767 return task;
768 }
769
Winson Chung7900bee2017-03-10 08:59:25 -0800770 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700771 if (!restoreRecentTaskLocked(task, aOptions)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700772 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
773 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800774 return null;
775 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700776 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800777 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700778 }
779
Craig Mautner6170f732013-04-02 13:05:23 -0700780 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800781 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800782 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800783 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
785 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
786 if (r != null) {
787 return r;
788 }
Craig Mautner6170f732013-04-02 13:05:23 -0700789 }
790 }
791 return null;
792 }
793
Tony Mak853304c2016-04-18 15:17:41 +0100794 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000795 * Detects whether we should show a lock screen in front of this task for a locked user.
796 * <p>
797 * We'll do this if either of the following holds:
798 * <ul>
799 * <li>The top activity explicitly belongs to {@param userId}.</li>
800 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
801 * </ul>
802 *
803 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100804 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000805 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
806 // To handle the case that work app is in the task but just is not the top one.
807 final ActivityRecord activityRecord = task.getTopActivity();
808 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
809
810 return (activityRecord != null && activityRecord.userId == userId)
811 || (resultTo != null && resultTo.userId == userId);
812 }
813
814 /**
815 * Find all visible task stacks containing {@param userId} and intercept them with an activity
816 * to block out the contents and possibly start a credential-confirming intent.
817 *
818 * @param userId user handle for the locked managed profile.
819 */
820 void lockAllProfileTasks(@UserIdInt int userId) {
821 mWindowManager.deferSurfaceLayout();
822 try {
823 final List<ActivityStack> stacks = getStacks();
824 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
825 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
826 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
827 final TaskRecord task = tasks.get(taskNdx);
828
829 // Check the task for a top activity belonging to userId, or returning a result
830 // to an activity belonging to userId. Example case: a document picker for
831 // personal files, opened by a work app, should still get locked.
832 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000833 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
834 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000835 }
Tony Mak853304c2016-04-18 15:17:41 +0100836 }
837 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000838 } finally {
839 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100840 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000841 }
842
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800843 void setNextTaskIdForUserLocked(int taskId, int userId) {
844 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
845 if (taskId > currentTaskId) {
846 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700847 }
848 }
849
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700850 static int nextTaskIdForUser(int taskId, int userId) {
851 int nextTaskId = taskId + 1;
852 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
853 // Wrap around as there will be smaller task ids that are available now.
854 nextTaskId -= MAX_TASK_IDS_PER_USER;
855 }
856 return nextTaskId;
857 }
858
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800859 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800860 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
861 // for a userId u, a taskId can only be in the range
862 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
863 // 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 -0700864 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700865 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700866 || anyTaskForIdLocked(
867 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700868 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800869 if (candidateTaskId == currentTaskId) {
870 // Something wrong!
871 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
872 throw new IllegalStateException("Cannot get an available task id."
873 + " Reached limit of " + MAX_TASK_IDS_PER_USER
874 + " running tasks per user.");
875 }
876 }
877 mCurTaskIdForUser.put(userId, candidateTaskId);
878 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700879 }
880
Chong Zhang6cda19c2016-06-14 19:07:56 -0700881 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800882 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700883 if (stack == null) {
884 return null;
885 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700886 ActivityRecord resumedActivity = stack.mResumedActivity;
887 if (resumedActivity == null || resumedActivity.app == null) {
888 resumedActivity = stack.mPausingActivity;
889 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700890 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700891 }
892 }
893 return resumedActivity;
894 }
895
Dianne Hackbornff072722014-09-24 10:56:28 -0700896 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700897 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800898 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800899 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
900 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800901 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
902 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700903 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800904 continue;
905 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200906 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
907 final ActivityRecord top = stack.topRunningActivityLocked();
908 final int size = mTmpActivityList.size();
909 for (int i = 0; i < size; i++) {
910 final ActivityRecord activity = mTmpActivityList.get(i);
911 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
912 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200914 if (realStartActivityLocked(activity, app,
915 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 didSomething = true;
917 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700918 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800919 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200920 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800921 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700922 }
Craig Mautner20e72272013-04-01 13:45:53 -0700923 }
Craig Mautner20e72272013-04-01 13:45:53 -0700924 }
925 }
926 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700927 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700928 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700929 }
Craig Mautner20e72272013-04-01 13:45:53 -0700930 return didSomething;
931 }
932
933 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800934 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
935 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
937 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700938 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800939 continue;
940 }
941 final ActivityRecord resumedActivity = stack.mResumedActivity;
942 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700943 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800944 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 return false;
946 }
Craig Mautner20e72272013-04-01 13:45:53 -0700947 }
948 }
Wei Wang65c7a152016-06-02 18:51:22 -0700949 // Send launch end powerhint when idle
950 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700951 return true;
952 }
953
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800955 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
956 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
958 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700959 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800960 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700961 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800962 return false;
963 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700964 }
965 }
966 }
967 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700968 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
970 mLastFocusedStack + " to=" + mFocusedStack);
971 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700972 return true;
973 }
974
975 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800976 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800977 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
978 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800979 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
980 final ActivityStack stack = stacks.get(stackNdx);
981 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800982 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700983 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800984 return false;
985 }
986 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800987 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 }
989 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800990 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700991 }
992
Craig Mautner2acc3892013-09-23 10:28:14 -0700993 /**
994 * Pause all activities in either all of the stacks or just the back stacks.
995 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700996 * @param resuming The resuming activity.
997 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
998 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700999 * @return true if any activity was paused as a result of this call.
1000 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001001 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001002 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001003 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1004 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001005 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1006 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001007 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001008 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001009 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001010 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1011 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001012 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001013 }
1014 }
1015 return someActivityPaused;
1016 }
1017
Craig Mautnerde4ef022013-04-07 19:01:33 -07001018 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001019 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001020 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1021 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001022 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1023 final ActivityStack stack = stacks.get(stackNdx);
1024 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001025 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001026 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001027 Slog.d(TAG_STATES,
1028 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001029 pausing = false;
1030 } else {
1031 return false;
1032 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001033 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001034 }
1035 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001036 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001037 }
1038
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001039 void cancelInitializingActivities() {
1040 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1041 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1042 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1043 stacks.get(stackNdx).cancelInitializingActivities();
1044 }
1045 }
1046 }
1047
Bryce Lee4a194382017-04-04 14:32:48 -07001048 void waitActivityVisible(ComponentName name, WaitResult result) {
1049 final WaitInfo waitInfo = new WaitInfo(name, result);
1050 mWaitingForActivityVisible.add(waitInfo);
1051 }
1052
1053 void cleanupActivity(ActivityRecord r) {
1054 // Make sure this record is no longer in the pending finishes list.
1055 // This could happen, for example, if we are trimming activities
1056 // down to the max limit while they are still waiting to finish.
1057 mFinishingActivities.remove(r);
1058 mActivitiesWaitingForVisibleActivity.remove(r);
1059
1060 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001061 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001062 mWaitingForActivityVisible.remove(i);
1063 }
1064 }
1065 }
1066
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001068 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001069 }
1070
1071 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1072 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001073 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1074 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001075 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001076 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001077 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001078 result.timeout = false;
1079 result.who = w.getComponent();
1080 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1081 result.thisTime = result.totalTime;
1082 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001083 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001084 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001085 if (changed) {
1086 mService.notifyAll();
1087 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001088 }
1089
Chong Zhang5022da32016-06-21 16:31:37 -07001090 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1091 boolean changed = false;
1092 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1093 WaitResult w = mWaitingActivityLaunched.remove(i);
1094 if (w.who == null) {
1095 changed = true;
1096 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1097 // the starting activity ends up moving another activity to front, and it should
1098 // wait for this new activity to become visible instead.
1099 // Do not modify other fields.
1100 w.result = START_TASK_TO_FRONT;
1101 }
1102 }
1103 if (changed) {
1104 mService.notifyAll();
1105 }
1106 }
1107
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001108 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1109 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001110 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001111 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001112 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001113 if (w.who == null) {
1114 changed = true;
1115 w.timeout = timeout;
1116 if (r != null) {
1117 w.who = new ComponentName(r.info.packageName, r.info.name);
1118 }
1119 w.thisTime = thisTime;
1120 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001121 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001124 if (changed) {
1125 mService.notifyAll();
1126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001127 }
1128
Craig Mautner29219d92013-04-16 20:19:12 -07001129 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001130 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001131 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001132 if (r != null) {
1133 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001134 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001135
Andrii Kulian7318d632016-07-20 18:59:28 -07001136 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001137 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1138
1139 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1140 final int displayId = mTmpOrderedDisplayIds.get(i);
1141 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1142 if (stacks == null) {
1143 continue;
1144 }
1145 for (int j = stacks.size() - 1; j >= 0; --j) {
1146 final ActivityStack stack = stacks.get(j);
1147 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1148 r = stack.topRunningActivityLocked();
1149 if (r != null) {
1150 return r;
1151 }
Craig Mautner29219d92013-04-16 20:19:12 -07001152 }
1153 }
1154 }
1155 return null;
1156 }
1157
Dianne Hackborn09233282014-04-30 11:33:59 -07001158 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001159 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001160 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001161 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001162 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001163 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001164 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1165 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001166 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001167 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001168 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001169 }
1170 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001171
1172 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1173 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1174 while (maxNum > 0) {
1175 long mostRecentActiveTime = Long.MIN_VALUE;
1176 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001177 final int numTaskLists = runningTaskLists.size();
1178 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1179 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001180 if (!stackTaskList.isEmpty()) {
1181 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1182 if (lastActiveTime > mostRecentActiveTime) {
1183 mostRecentActiveTime = lastActiveTime;
1184 selectedStackList = stackTaskList;
1185 }
1186 }
1187 }
1188 if (selectedStackList != null) {
1189 list.add(selectedStackList.remove(0));
1190 --maxNum;
1191 } else {
1192 break;
1193 }
1194 }
Craig Mautner20e72272013-04-01 13:45:53 -07001195 }
1196
Todd Kennedy7440f172015-12-09 14:31:22 -08001197 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1198 ProfilerInfo profilerInfo) {
1199 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001200 if (aInfo != null) {
1201 // Store the found target back into the intent, because now that
1202 // we have it we never want to do this again. For example, if the
1203 // user navigates back to this point in the history, we should
1204 // always restart the exact same activity.
1205 intent.setComponent(new ComponentName(
1206 aInfo.applicationInfo.packageName, aInfo.name));
1207
1208 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001209 if (!aInfo.processName.equals("system")) {
1210 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001211 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001212 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001213
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001214 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1215 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1216 }
1217
Man Caocfa78b22015-06-11 20:14:34 -07001218 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1219 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1220 }
1221
1222 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001223 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001224 }
1225 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001226 final String intentLaunchToken = intent.getLaunchToken();
1227 if (aInfo.launchToken == null && intentLaunchToken != null) {
1228 aInfo.launchToken = intentLaunchToken;
1229 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001230 }
1231 return aInfo;
1232 }
1233
Todd Kennedy7440f172015-12-09 14:31:22 -08001234 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001235 return resolveIntent(intent, resolvedType, userId, 0);
1236 }
1237
1238 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001239 synchronized (mService) {
1240 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001241 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001242 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001243 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001244 }
1245
1246 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1247 ProfilerInfo profilerInfo, int userId) {
1248 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1249 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1250 }
1251
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001252 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1253 boolean andResume, boolean checkConfig) throws RemoteException {
1254
1255 if (!allPausedActivitiesComplete()) {
1256 // While there are activities pausing we skipping starting any new activities until
1257 // pauses are complete. NOTE: that we also do this for activities that are starting in
1258 // the paused state because they will first be resumed then paused on the client side.
1259 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1260 "realStartActivityLocked: Skipping start of r=" + r
1261 + " some activities pausing...");
1262 return false;
1263 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001264
Bryce Leeaf691c02017-03-20 14:20:22 -07001265 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001266 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001267
1268 beginDeferResume();
1269
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 try {
chaviw59b98852017-06-13 12:05:44 -07001271 r.startFreezingScreenLocked(app, 0);
1272
1273 // schedule launch ticks to collect information about slow apps.
1274 r.startLaunchTickingLocked();
1275
1276 r.app = app;
1277
Jorim Jaggi838c2452017-08-28 15:44:43 +02001278 if (mKeyguardController.isKeyguardLocked()) {
1279 r.notifyUnknownVisibilityLaunched();
1280 }
1281
chaviw59b98852017-06-13 12:05:44 -07001282 // Have the window manager re-evaluate the orientation of the screen based on the new
1283 // activity order. Note that as a result of this, it can call back into the activity
1284 // manager with a new orientation. We don't care about that, because the activity is
1285 // not currently running so we are just restarting it anyway.
1286 if (checkConfig) {
1287 final int displayId = r.getDisplayId();
1288 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1289 getDisplayOverrideConfiguration(displayId),
1290 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1291 // Deferring resume here because we're going to launch new activity shortly.
1292 // We don't want to perform a redundant launch of the same record while ensuring
1293 // configurations and trying to resume top activity of focused stack.
1294 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1295 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
chaviw59b98852017-06-13 12:05:44 -07001297
1298 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1299 true /* isTop */)) {
1300 // We only set the visibility to true if the activity is allowed to be visible
1301 // based on
1302 // keyguard state. This avoids setting this into motion in window manager that is
1303 // later cancelled due to later calls to ensure visible activities that set
1304 // visibility back to false.
1305 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 }
chaviw59b98852017-06-13 12:05:44 -07001307
chaviw59b98852017-06-13 12:05:44 -07001308 final int applicationInfoUid =
1309 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1310 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1311 Slog.wtf(TAG,
1312 "User ID for activity changing for " + r
1313 + " appInfo.uid=" + r.appInfo.uid
1314 + " info.ai.uid=" + applicationInfoUid
1315 + " old=" + r.app + " new=" + app);
1316 }
1317
1318 app.waitingToKill = null;
1319 r.launchCount++;
1320 r.lastLaunchTime = SystemClock.uptimeMillis();
1321
1322 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1323
1324 int idx = app.activities.indexOf(r);
1325 if (idx < 0) {
1326 app.activities.add(r);
1327 }
1328 mService.updateLruProcessLocked(app, true, null);
1329 mService.updateOomAdjLocked();
1330
1331 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1332 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001333 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001334 }
1335
1336 try {
1337 if (app.thread == null) {
1338 throw new RemoteException();
1339 }
1340 List<ResultInfo> results = null;
1341 List<ReferrerIntent> newIntents = null;
1342 if (andResume) {
1343 // We don't need to deliver new intents and/or set results if activity is going
1344 // to pause immediately after launch.
1345 results = r.results;
1346 newIntents = r.newIntents;
1347 }
1348 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1349 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1350 + " newIntents=" + newIntents + " andResume=" + andResume);
1351 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1352 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001353 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001354 // Home process is the root process of the task.
1355 mService.mHomeProcess = task.mActivities.get(0).app;
1356 }
1357 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1358 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1359 r.sleeping = false;
1360 r.forceNewConfig = false;
1361 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1362 mService.showAskCompatModeDialogLocked(r);
1363 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1364 ProfilerInfo profilerInfo = null;
1365 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1366 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1367 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001368 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1369 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1370 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001371 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001372 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001373 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001374 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001375 }
1376 }
chaviw59b98852017-06-13 12:05:44 -07001377
Andreas Gampe2b073a02017-06-22 17:28:57 -07001378 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 }
1381 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001382
chaviw59b98852017-06-13 12:05:44 -07001383 app.hasShownUi = true;
1384 app.pendingUiClean = true;
1385 app.forceProcessStateUpTo(mService.mTopProcessState);
1386 // Because we could be starting an Activity in the system process this may not go
1387 // across a Binder interface which would create a new Configuration. Consequently
1388 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001389
chaviw59b98852017-06-13 12:05:44 -07001390 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1391 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1392 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001393
chaviw59b98852017-06-13 12:05:44 -07001394 logIfTransactionTooLarge(r.intent, r.icicle);
1395 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1396 System.identityHashCode(r), r.info,
1397 // TODO: Have this take the merged configuration instead of separate global
1398 // and override configs.
1399 mergedConfiguration.getGlobalConfiguration(),
1400 mergedConfiguration.getOverrideConfiguration(), r.compat,
1401 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1402 r.persistentState, results, newIntents, !andResume,
1403 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001404
chaviw59b98852017-06-13 12:05:44 -07001405 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1406 // This may be a heavy-weight process! Note that the package
1407 // manager will ensure that only activity can run in the main
1408 // process of the .apk, which is the only thing that will be
1409 // considered heavy-weight.
1410 if (app.processName.equals(app.info.packageName)) {
1411 if (mService.mHeavyWeightProcess != null
1412 && mService.mHeavyWeightProcess != app) {
1413 Slog.w(TAG, "Starting new heavy weight process " + app
1414 + " when already running "
1415 + mService.mHeavyWeightProcess);
1416 }
1417 mService.mHeavyWeightProcess = app;
1418 Message msg = mService.mHandler.obtainMessage(
1419 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1420 msg.obj = r;
1421 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001423 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001424
chaviw59b98852017-06-13 12:05:44 -07001425 } catch (RemoteException e) {
1426 if (r.launchFailed) {
1427 // This is the second time we failed -- finish activity
1428 // and give up.
1429 Slog.e(TAG, "Second failure launching "
1430 + r.intent.getComponent().flattenToShortString()
1431 + ", giving up", e);
1432 mService.appDiedLocked(app);
1433 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1434 "2nd-crash", false);
1435 return false;
1436 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001437
chaviw59b98852017-06-13 12:05:44 -07001438 // This is the first time we failed -- restart process and
1439 // retry.
1440 r.launchFailed = true;
1441 app.activities.remove(r);
1442 throw e;
1443 }
1444 } finally {
1445 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001446 }
1447
1448 r.launchFailed = false;
1449 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001450 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001451 }
1452
chaviw59b98852017-06-13 12:05:44 -07001453 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001454 // As part of the process of launching, ActivityThread also performs
1455 // a resume.
1456 stack.minimalResumeActivityLocked(r);
1457 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001458 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001459 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001460 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001461 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001462 "Moving to PAUSED: " + r + " (starting in paused state)");
1463 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001464 }
1465
1466 // Launch the new version setup screen if needed. We do this -after-
1467 // launching the initial activity (that is, home), so that it can have
1468 // a chance to initialize itself while in the background, making the
1469 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001470 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001471 mService.startSetupActivityLocked();
1472 }
1473
Dianne Hackborn465fa392014-09-14 14:21:18 -07001474 // Update any services we are bound to that might care about whether
1475 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001476 if (r.app != null) {
1477 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1478 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001479
Craig Mautner2420ead2013-04-01 17:13:20 -07001480 return true;
1481 }
1482
Sudheer Shankafab200f2017-05-17 20:41:53 -07001483 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1484 int extrasSize = 0;
1485 if (intent != null) {
1486 final Bundle extras = intent.getExtras();
1487 if (extras != null) {
1488 extrasSize = extras.getSize();
1489 }
1490 }
1491 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1492 if (extrasSize + icicleSize > 200000) {
1493 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1494 + ", icicle size: " + icicleSize);
1495 }
1496 }
1497
Craig Mautnere79d42682013-04-01 19:01:53 -07001498 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001499 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001500 // Is this activity's application already running?
1501 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001502 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001503
Andrii Kulian02b7a832016-10-06 23:11:56 -07001504 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001505
1506 if (app != null && app.thread != null) {
1507 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001508 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1509 || !"android".equals(r.info.packageName)) {
1510 // Don't add this if it is a platform component that is marked
1511 // to run in multiple processes, because this is actually
1512 // part of the framework so doesn't make sense to track as a
1513 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001514 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1515 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001516 }
George Mount2c92c972014-03-20 09:38:23 -07001517 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001518 return;
1519 } catch (RemoteException e) {
1520 Slog.w(TAG, "Exception when starting activity "
1521 + r.intent.getComponent().flattenToShortString(), e);
1522 }
1523
1524 // If a dead object exception was thrown -- fall through to
1525 // restart the application.
1526 }
1527
1528 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001529 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001530 }
1531
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001532 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001533 String resultWho, int requestCode, int callingPid, int callingUid,
1534 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001535 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001536 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1537 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001538 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001539 return true;
1540 }
1541 final int componentRestriction = getComponentRestrictionForCallingPackage(
1542 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1543 final int actionRestriction = getActionRestrictionForCallingPackage(
1544 intent.getAction(), callingPackage, callingPid, callingUid);
1545 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1546 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1547 if (resultRecord != null) {
1548 resultStack.sendActivityResultLocked(-1,
1549 resultRecord, resultWho, requestCode,
1550 Activity.RESULT_CANCELED, null);
1551 }
1552 final String msg;
1553 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1554 msg = "Permission Denial: starting " + intent.toString()
1555 + " from " + callerApp + " (pid=" + callingPid
1556 + ", uid=" + callingUid + ")" + " with revoked permission "
1557 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1558 } else if (!aInfo.exported) {
1559 msg = "Permission Denial: starting " + intent.toString()
1560 + " from " + callerApp + " (pid=" + callingPid
1561 + ", uid=" + callingUid + ")"
1562 + " not exported from uid " + aInfo.applicationInfo.uid;
1563 } else {
1564 msg = "Permission Denial: starting " + intent.toString()
1565 + " from " + callerApp + " (pid=" + callingPid
1566 + ", uid=" + callingUid + ")"
1567 + " requires " + aInfo.permission;
1568 }
1569 Slog.w(TAG, msg);
1570 throw new SecurityException(msg);
1571 }
1572
1573 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1574 final String message = "Appop Denial: starting " + intent.toString()
1575 + " from " + callerApp + " (pid=" + callingPid
1576 + ", uid=" + callingUid + ")"
1577 + " requires " + AppOpsManager.permissionToOp(
1578 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1579 Slog.w(TAG, message);
1580 return false;
1581 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1582 final String message = "Appop Denial: starting " + intent.toString()
1583 + " from " + callerApp + " (pid=" + callingPid
1584 + ", uid=" + callingUid + ")"
1585 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1586 Slog.w(TAG, message);
1587 return false;
1588 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001589 if (options != null) {
1590 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1591 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1592 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001593 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001594 final String msg = "Permission Denial: starting " + intent.toString()
1595 + " from " + callerApp + " (pid=" + callingPid
1596 + ", uid=" + callingUid + ") with launchTaskId="
1597 + options.getLaunchTaskId();
1598 Slog.w(TAG, msg);
1599 throw new SecurityException(msg);
1600 }
1601 }
1602 // Check if someone tries to launch an activity on a private display with a different
1603 // owner.
1604 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001605 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1606 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001607 final String msg = "Permission Denial: starting " + intent.toString()
1608 + " from " + callerApp + " (pid=" + callingPid
1609 + ", uid=" + callingUid + ") with launchDisplayId="
1610 + launchDisplayId;
1611 Slog.w(TAG, msg);
1612 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001613 }
Charles He2bf28322017-10-12 22:24:49 +01001614 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1615 final boolean lockTaskMode = options.getLockTaskMode();
1616 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1617 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1618 final String msg = "Permission Denial: starting " + intent.toString()
1619 + " from " + callerApp + " (pid=" + callingPid
1620 + ", uid=" + callingUid + ") with lockTaskMode=true";
1621 Slog.w(TAG, msg);
1622 throw new SecurityException(msg);
1623 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001624 }
1625
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001626 return true;
1627 }
1628
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001629 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001630 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1631 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001632 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1633 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1634
Andrii Kulian02689a72017-07-06 14:28:59 -07001635 if (callingPid == -1 && callingUid == -1) {
1636 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1637 return true;
1638 }
1639
Andrii Kulian62e6f252017-05-30 22:46:53 -07001640 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001641 if (activityDisplay == null) {
1642 Slog.w(TAG, "Launch on display check: display not found");
1643 return false;
1644 }
1645
Andrii Kulian02689a72017-07-06 14:28:59 -07001646 // Check if the caller has enough privileges to embed activities and launch to private
1647 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001648 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001649 callingUid);
1650 if (startAnyPerm == PERMISSION_GRANTED) {
1651 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1652 + " allow launch any on display");
1653 return true;
1654 }
1655
Andrii Kulian8f070292017-09-12 22:56:49 -07001656 // Check if caller is already present on display
1657 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1658
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001659 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1660 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1661 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001662 // Limit launching on virtual displays, because their contents can be read from Surface
1663 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001664 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1665 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1666 + " disallow launch on virtual display for not-embedded activity.");
1667 return false;
1668 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001669 // Check if the caller is allowed to embed activities from other apps.
1670 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001671 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001672 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1673 + " disallow activity embedding without permission.");
1674 return false;
1675 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001676 }
1677
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001678 if (!activityDisplay.isPrivate()) {
1679 // Anyone can launch on a public display.
1680 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1681 + " allow launch on public display");
1682 return true;
1683 }
1684
1685 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001686 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001687 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1688 + " allow launch for owner of the display");
1689 return true;
1690 }
1691
Andrii Kulian8f070292017-09-12 22:56:49 -07001692 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001693 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1694 + " allow launch for caller present on the display");
1695 return true;
1696 }
1697
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001698 Slog.w(TAG, "Launch on display check: denied");
1699 return false;
1700 }
1701
1702 /** Update lists of UIDs that are present on displays and have access to them. */
1703 void updateUIDsPresentOnDisplay() {
1704 mDisplayAccessUIDs.clear();
1705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1706 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001707 // Only bother calculating the whitelist for private displays
1708 if (activityDisplay.isPrivate()) {
1709 mDisplayAccessUIDs.append(
1710 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1711 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001712 }
1713 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1714 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1715 }
1716
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001717 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001718 final long identity = Binder.clearCallingIdentity();
1719 try {
1720 return UserManager.get(mService.mContext).getUserInfo(userId);
1721 } finally {
1722 Binder.restoreCallingIdentity(identity);
1723 }
1724 }
1725
Svet Ganov99b60432015-06-27 13:15:22 -07001726 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001727 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001728 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1729 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001730 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001731 return ACTIVITY_RESTRICTION_PERMISSION;
1732 }
1733
Christopher Tateff7add02015-08-17 10:23:22 -07001734 if (activityInfo.permission == null) {
1735 return ACTIVITY_RESTRICTION_NONE;
1736 }
1737
Svet Ganov99b60432015-06-27 13:15:22 -07001738 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1739 if (opCode == AppOpsManager.OP_NONE) {
1740 return ACTIVITY_RESTRICTION_NONE;
1741 }
1742
1743 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1744 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001745 if (!ignoreTargetSecurity) {
1746 return ACTIVITY_RESTRICTION_APPOP;
1747 }
Svet Ganov99b60432015-06-27 13:15:22 -07001748 }
1749
1750 return ACTIVITY_RESTRICTION_NONE;
1751 }
1752
Svetoslav7008b512015-06-24 18:47:07 -07001753 private int getActionRestrictionForCallingPackage(String action,
1754 String callingPackage, int callingPid, int callingUid) {
1755 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001756 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001757 }
1758
1759 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1760 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
1764 final PackageInfo packageInfo;
1765 try {
1766 packageInfo = mService.mContext.getPackageManager()
1767 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1768 } catch (PackageManager.NameNotFoundException e) {
1769 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001770 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001771 }
1772
1773 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001774 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001775 }
1776
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001777 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001778 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001779 }
1780
1781 final int opCode = AppOpsManager.permissionToOpCode(permission);
1782 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001784 }
1785
1786 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1787 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001788 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001789 }
1790
Svet Ganov99b60432015-06-27 13:15:22 -07001791 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001792 }
1793
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001794 void setLaunchSource(int uid) {
1795 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1796 }
1797
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001798 void acquireLaunchWakelock() {
1799 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1800 throw new IllegalStateException("Calling must be system uid");
1801 }
1802 mLaunchingActivity.acquire();
1803 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1804 // To be safe, don't allow the wake lock to be held for too long.
1805 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1806 }
1807 }
1808
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001809 /**
1810 * Called when the frontmost task is idle.
1811 * @return the state of mService.mBooting before this was called.
1812 */
1813 private boolean checkFinishBootingLocked() {
1814 final boolean booting = mService.mBooting;
1815 boolean enableScreen = false;
1816 mService.mBooting = false;
1817 if (!mService.mBooted) {
1818 mService.mBooted = true;
1819 enableScreen = true;
1820 }
1821 if (booting || enableScreen) {
1822 mService.postFinishBooting(booting, enableScreen);
1823 }
1824 return booting;
1825 }
1826
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 // Checked.
1828 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001829 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001830 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001831
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001833 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 int NS = 0;
1835 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001836 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001837 boolean activityRemoved = false;
1838
Wale Ogunwale7d701172015-03-11 15:36:30 -07001839 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001841 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1842 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001843 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1844 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001845 if (fromTimeout) {
1846 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001847 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001848
1849 // This is a hack to semi-deal with a race condition
1850 // in the client where it can be constructed with a
1851 // newer configuration from when we asked it to launch.
1852 // We'll update with whatever configuration it now says
1853 // it used to launch.
1854 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001855 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001856 }
1857
1858 // We are now idle. If someone is waiting for a thumbnail from
1859 // us, we can now deliver.
1860 r.idle = true;
1861
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001862 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001863 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001864 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001865 }
1866 }
1867
1868 if (allResumedActivitiesIdle()) {
1869 if (r != null) {
1870 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001871 }
1872
1873 if (mLaunchingActivity.isHeld()) {
1874 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1875 if (VALIDATE_WAKE_LOCK_CALLER &&
1876 Binder.getCallingUid() != Process.myUid()) {
1877 throw new IllegalStateException("Calling must be system uid");
1878 }
1879 mLaunchingActivity.release();
1880 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001881 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 }
1883
1884 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001885 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1886 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001888 if ((NF = mFinishingActivities.size()) > 0) {
1889 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 mFinishingActivities.clear();
1891 }
1892
Craig Mautnerf3333272013-04-22 10:55:53 -07001893 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001894 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 mStartingUsers.clear();
1896 }
1897
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 // Stop any activities that are scheduled to do so but have been
1899 // waiting for the next one to start.
1900 for (int i = 0; i < NS; i++) {
1901 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001902 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001903 if (stack != null) {
1904 if (r.finishing) {
1905 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1906 } else {
1907 stack.stopActivityLocked(r);
1908 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910 }
1911
1912 // Finish any activities that are scheduled to do so but have been
1913 // waiting for the next one to start.
1914 for (int i = 0; i < NF; i++) {
1915 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001916 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001917 if (stack != null) {
1918 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1919 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001920 }
1921
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001922 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001923 // Complete user switch
1924 if (startingUsers != null) {
1925 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001926 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001927 }
1928 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001929 }
1930
1931 mService.trimApplications();
1932 //dump();
1933 //mWindowManager.dump();
1934
Craig Mautnerf3333272013-04-22 10:55:53 -07001935 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001936 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 }
1938
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001939 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 }
1941
Craig Mautner8e569572013-10-11 17:36:59 -07001942 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001943 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1947 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1948 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001949 }
Craig Mautner19091252013-10-05 00:03:53 -07001950 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001951 }
1952
1953 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1955 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1957 stacks.get(stackNdx).closeSystemDialogsLocked();
1958 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001959 }
1960 }
1961
Craig Mautner93529a42013-10-04 15:03:13 -07001962 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001963 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001964 }
1965
Craig Mautner8d341ef2013-03-26 09:03:27 -07001966 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001967 * Update the last used stack id for non-current user (current user's last
1968 * used stack is the focused stack)
1969 */
1970 void updateUserStackLocked(int userId, ActivityStack stack) {
1971 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001972 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001973 }
1974 }
1975
1976 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001977 * @return true if some activity was finished (or would have finished if doit were true).
1978 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001979 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1980 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001981 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001982 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1983 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001984 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001985 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001986 if (stack.finishDisabledPackageActivitiesLocked(
1987 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001988 didSomething = true;
1989 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001990 }
1991 }
1992 return didSomething;
1993 }
1994
Dianne Hackborna413dc02013-07-12 12:02:55 -07001995 void updatePreviousProcessLocked(ActivityRecord r) {
1996 // Now that this process has stopped, we may want to consider
1997 // it to be the previous app to try to keep around in case
1998 // the user wants to return to it.
1999
2000 // First, found out what is currently the foreground app, so that
2001 // we don't blow away the previous app if this activity is being
2002 // hosted by the process that is actually still the foreground.
2003 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002004 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2005 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002006 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2007 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002008 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002009 if (stack.mResumedActivity != null) {
2010 fgApp = stack.mResumedActivity.app;
2011 } else if (stack.mPausingActivity != null) {
2012 fgApp = stack.mPausingActivity.app;
2013 }
2014 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002015 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002016 }
2017 }
2018
2019 // Now set this one as the previous process, only if that really
2020 // makes sense to.
2021 if (r.app != null && fgApp != null && r.app != fgApp
2022 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002023 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002024 mService.mPreviousProcess = r.app;
2025 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2026 }
2027 }
2028
Wale Ogunwaled046a012015-12-24 13:05:59 -08002029 boolean resumeFocusedStackTopActivityLocked() {
2030 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002031 }
2032
Wale Ogunwaled046a012015-12-24 13:05:59 -08002033 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002034 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002035
2036 if (!readyToResume()) {
2037 return false;
2038 }
2039
Wale Ogunwaled046a012015-12-24 13:05:59 -08002040 if (targetStack != null && isFocusedStack(targetStack)) {
2041 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002042 }
chaviw59b98852017-06-13 12:05:44 -07002043
Wale Ogunwale06579d62016-04-30 15:29:06 -07002044 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2045 if (r == null || r.state != RESUMED) {
2046 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002047 } else if (r.state == RESUMED) {
2048 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2049 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002050 }
chaviw59b98852017-06-13 12:05:44 -07002051
Wale Ogunwaled046a012015-12-24 13:05:59 -08002052 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002053 }
2054
Todd Kennedy39bfee52016-02-24 10:28:21 -08002055 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2056 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2057 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2058 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2059 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2060 }
2061 }
2062 }
2063
Adrian Roos20d7df32016-01-12 18:59:43 +01002064 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2065 TaskRecord finishedTask = null;
2066 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002067 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2068 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002069 final int numStacks = stacks.size();
2070 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2071 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002072 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2073 if (stack == focusedStack || finishedTask == null) {
2074 finishedTask = t;
2075 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002076 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002077 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002078 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002079 }
2080
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002081 void finishVoiceTask(IVoiceInteractionSession session) {
2082 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2083 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2084 final int numStacks = stacks.size();
2085 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2086 final ActivityStack stack = stacks.get(stackNdx);
2087 stack.finishVoiceTask(session);
2088 }
2089 }
2090 }
2091
Andrii Kulianc27916642016-04-12 17:59:27 -07002092 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2093 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002094 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2095 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002096 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002097 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2098 // Caller wants the home activity moved with it. To accomplish this,
2099 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002100 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002101 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002102 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002103 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002104 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2105 + task + " to front. Stack is null");
2106 return;
2107 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002108
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002109 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2110 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2111 task.updateOverrideConfiguration(bounds);
2112
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002113 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002114
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002115 if (stack != currentStack) {
2116 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2117 "findTaskToMoveToFrontLocked");
2118 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002119 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2120 // still need moveTaskToFrontLocked() below for any transition settings.
2121 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002122 if (stack.resizeStackWithLaunchBounds()) {
2123 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002124 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2125 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002126 } else {
2127 // WM resizeTask must be done after the task is moved to the correct stack,
2128 // because Task's setBounds() also updates dim layer's bounds, but that has
2129 // dependency on the stack.
2130 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002131 }
2132 }
2133
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002134 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002135 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002136 r == null ? null : r.appTimeTracker, reason);
2137
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002138 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002139 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002140
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002141 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002142 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002143 }
2144
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002145 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002146 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002147 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002148 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002149 return false;
2150 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002151 return (mService.mSupportsPictureInPicture
2152 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002153 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002154 }
2155
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002156 LaunchingTaskPositioner getLaunchingTaskPositioner() {
2157 return mTaskPositioner;
2158 }
2159
Winson Chung55893332017-02-17 17:13:10 -08002160 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002161 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2162 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2163 if (stack != null) {
2164 return stack;
2165 }
2166 }
2167 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002168 }
2169
Wale Ogunwale68278562017-09-23 17:13:55 -07002170 /** @see ActivityDisplay#getStack(int, int) */
2171 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2172 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2173 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2174 if (stack != null) {
2175 return stack;
2176 }
2177 }
2178 return null;
2179 }
2180
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002181 /**
2182 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2183 * @param windowingMode The windowing mode we are checking support for.
2184 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2185 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2186 * @param supportsFreeform If we should consider support for freeform multi-window.
2187 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2188 * @param activityType The activity type under consideration.
2189 * @return true if the windowing mode is supported.
2190 */
2191 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2192 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2193 int activityType) {
2194
2195 if (windowingMode == WINDOWING_MODE_UNDEFINED
2196 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2197 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002198 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002199 if (!supportsMultiWindow) {
2200 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002201 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002202
2203 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2204 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2205 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2206 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002207 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002208
2209 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2210 return false;
2211 }
2212
2213 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2214 return false;
2215 }
2216 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002217 }
2218
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002219 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002220 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002221
2222 // First preference if the windowing mode in the activity options if set.
2223 int windowingMode = (options != null)
2224 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2225
2226 // If windowing mode is unset, then next preference is the candidate task, then the
2227 // activity record.
2228 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2229 if (task != null) {
2230 windowingMode = task.getWindowingMode();
2231 }
2232 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2233 windowingMode = r.getWindowingMode();
2234 }
2235 }
2236
2237 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002238 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2239 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2240 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2241 boolean supportsPip = mService.mSupportsPictureInPicture;
2242 if (supportsMultiWindow) {
2243 if (task != null) {
2244 supportsMultiWindow = task.isResizeable();
2245 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2246 // TODO: Do we need to check for freeform and Pip support here?
2247 } else if (r != null) {
2248 supportsMultiWindow = r.isResizeable();
2249 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2250 supportsFreeform = r.supportsFreeform();
2251 supportsPip = r.supportsPictureInPicture();
2252 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002253 }
2254
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002255 if (isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
2256 supportsFreeform, supportsPip, activityType)) {
2257 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002258 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002259 return WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002260 }
2261
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002262 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002263 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002264 // Preference is given to the activity type for the activity then the task since the type
2265 // once set shouldn't change.
2266 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2267 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2268 activityType = task.getActivityType();
2269 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002270 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2271 return activityType;
2272 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002273 return options != null ? options.getLaunchActivityType() : ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002274 }
2275
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002276 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2277 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2278 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002279 }
2280
2281 /**
2282 * Returns the right stack to use for launching factoring in all the input parameters.
2283 *
2284 * @param r The activity we are trying to launch. Can be null.
2285 * @param options The activity options used to the launch. Can be null.
2286 * @param candidateTask The possible task the activity might be launched in. Can be null.
2287 *
2288 * @return The stack to use for the launch or INVALID_STACK_ID.
2289 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002290 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2291 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2292 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002293 int taskId = INVALID_TASK_ID;
2294 int displayId = INVALID_DISPLAY;
2295 //Rect bounds = null;
2296
2297 // We give preference to the launch preference in activity options.
2298 if (options != null) {
2299 taskId = options.getLaunchTaskId();
2300 displayId = options.getLaunchDisplayId();
2301 // TODO: Need to work this into the equation...
2302 //bounds = options.getLaunchBounds();
2303 }
2304
2305 // First preference for stack goes to the task Id set in the activity options. Use the stack
2306 // associated with that if possible.
2307 if (taskId != INVALID_TASK_ID) {
2308 // Temporarily set the task id to invalid in case in re-entry.
2309 options.setLaunchTaskId(INVALID_TASK_ID);
2310 final TaskRecord task = anyTaskForIdLocked(taskId,
2311 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options);
2312 options.setLaunchTaskId(taskId);
2313 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002314 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315 }
2316 }
2317
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002318 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002319 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2320 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002321
2322 // Next preference for stack goes to the display Id set in the activity options or the
2323 // candidate display.
2324 if (displayId == INVALID_DISPLAY) {
2325 displayId = candidateDisplayId;
2326 }
2327 if (displayId != INVALID_DISPLAY) {
2328 if (r != null) {
2329 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002330 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002331 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002332 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002333 }
2334 }
2335 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2336 if (display != null) {
2337 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002338 stack = (T) display.mStacks.get(i);
2339 if (stack.isCompatible(windowingMode, activityType)) {
2340 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002341 }
2342 }
2343 // TODO: We should create the stack we want on the display at this point.
2344 }
2345 }
2346
2347 // Give preference to the stack and display of the input task and activity if they match the
2348 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002349 stack = null;
2350 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002351 if (candidateTask != null) {
2352 stack = candidateTask.getStack();
2353 }
2354 if (stack == null && r != null) {
2355 stack = r.getStack();
2356 }
2357 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002358 if (stack.isCompatible(windowingMode, activityType)) {
2359 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002360 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002361 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002362 }
2363
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002364 if (display == null
2365 // TODO: Can be removed once we figure-out how non-standard types should launch
2366 // outside the default display.
2367 || (activityType != ACTIVITY_TYPE_STANDARD
2368 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2369 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002370 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002371
2372 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2373 if (stack != null) {
2374 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002375 }
2376
2377 // Whatever...return some default for now.
2378 if (candidateTask != null && candidateTask.mBounds != null
2379 && mService.mSupportsFreeformWindowManagement) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002380 windowingMode = WINDOWING_MODE_FREEFORM;
2381 } else {
2382 windowingMode = WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002384 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002385 }
2386
Andrii Kulian16802aa2016-11-02 12:21:33 -07002387 /**
2388 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2389 * If there is no such stack, new dynamic stack can be created.
2390 * @param displayId Target display.
2391 * @param r Activity that should be launched there.
2392 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2393 */
2394 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002395 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002396 if (activityDisplay == null) {
2397 throw new IllegalArgumentException(
2398 "Display with displayId=" + displayId + " not found.");
2399 }
2400
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002401 if (!r.canBeLaunchedOnDisplay(displayId)) {
2402 return null;
2403 }
2404
Andrii Kulian16802aa2016-11-02 12:21:33 -07002405 // Return the topmost valid stack on the display.
2406 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2407 final ActivityStack stack = activityDisplay.mStacks.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002408 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002409 return stack;
2410 }
2411 }
2412
2413 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002414 if (displayId != DEFAULT_DISPLAY) {
2415 return activityDisplay.createStack(
2416 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002417 }
2418
2419 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2420 return null;
2421 }
2422
Wale Ogunwale68278562017-09-23 17:13:55 -07002423 // TODO: Can probably be consolidated into getLaunchStack()...
2424 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2425 switch (stack.getActivityType()) {
2426 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2427 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2428 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002429 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002430 switch (stack.getWindowingMode()) {
2431 case WINDOWING_MODE_FULLSCREEN: return true;
2432 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2433 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2434 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2435 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2436 }
2437
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002438 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002439 return r.canBeLaunchedOnDisplay(displayId);
2440 }
2441 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2442 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002443 }
2444
Craig Mautner967212c2013-04-13 21:10:58 -07002445 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002446 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002447 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2448 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002449 }
2450 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002451 }
2452
Jorim Jaggife762342016-10-13 14:33:27 +02002453 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2454 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2455 }
2456
Andrii Kulian7fc22812016-12-28 13:04:11 -08002457 /**
2458 * Get next focusable stack in the system. This will search across displays and stacks
2459 * in last-focused order for a focusable and visible stack, different from the target stack.
2460 *
2461 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2462 * searching for next candidate.
2463 * @return Next focusable {@link ActivityStack}, null if not found.
2464 */
2465 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2466 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2467
2468 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2469 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002470 // If a display is registered in WM, it must also be available in AM.
2471 @SuppressWarnings("ConstantConditions")
2472 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002473 for (int j = stacks.size() - 1; j >= 0; --j) {
2474 final ActivityStack stack = stacks.get(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002475 if (stack != currentFocus && stack.isFocusable()
2476 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002477 return stack;
2478 }
2479 }
2480 }
2481
2482 return null;
2483 }
2484
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002485 /**
2486 * Get next valid stack for launching provided activity in the system. This will search across
2487 * displays and stacks in last-focused order for a focusable and visible stack, except those
2488 * that are on a currently focused display.
2489 *
2490 * @param r The activity that is being launched.
2491 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2492 * searching for the next candidate.
2493 * @return Next valid {@link ActivityStack}, null if not found.
2494 */
2495 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2496 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2497 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2498 final int displayId = mTmpOrderedDisplayIds.get(i);
2499 if (displayId == currentFocus) {
2500 continue;
2501 }
2502 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2503 if (stack != null) {
2504 return stack;
2505 }
2506 }
2507 return null;
2508 }
2509
Craig Mautneree2e45a2014-06-27 12:10:03 -07002510 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002511 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002512 }
2513
2514 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002515 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2516 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2517 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002518 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002519 final ArrayList<ActivityRecord> activities = task.mActivities;
2520 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2521 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002522 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002523 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002524 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002525 }
2526 }
2527 }
2528 }
2529 return null;
2530 }
2531
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002532 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2533 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2534 boolean deferResume) {
2535
2536 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002537 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002538 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002539 return;
2540 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002541
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002542 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002543 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002544 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002545 // If the docked stack exists, don't resize non-floating stacks independently of the
2546 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002547 return;
2548 }
2549
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002550 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002551 mWindowManager.deferSurfaceLayout();
2552 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002553 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002554 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2555 // null bounds = fullscreen windowing mode...at least for now.
2556 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2557 } else if (splitScreenActive) {
2558 // If we are in split-screen mode and this stack support split-screen, then
2559 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2560 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2561 }
2562 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002563 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002564 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002565 stack.ensureVisibleActivitiesConfigurationLocked(
2566 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002567 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002568 } finally {
2569 mWindowManager.continueSurfaceLayout();
2570 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002571 }
2572 }
2573
Wale Ogunwale68278562017-09-23 17:13:55 -07002574 private void deferUpdateBounds(int activityType) {
2575 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002576 if (stack != null) {
2577 stack.deferUpdateBounds();
2578 }
2579 }
2580
Wale Ogunwale68278562017-09-23 17:13:55 -07002581 private void continueUpdateBounds(int activityType) {
2582 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002583 if (stack != null) {
2584 stack.continueUpdateBounds();
2585 }
2586 }
2587
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002588 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002589 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002590 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2591 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002592 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002593 if (task != null) {
2594 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002595 }
2596 }
2597 mResizingTasksDuringAnimation.clear();
2598 }
2599
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002600 /**
2601 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002602 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002603 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002604 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002605 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002606
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002607 mWindowManager.deferSurfaceLayout();
2608 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002609 final int windowingMode = fromStack.getWindowingMode();
2610 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2611 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2612
2613 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002614 // We are moving all tasks from the docked stack to the fullscreen stack,
2615 // which is dismissing the docked stack, so resize all other stacks to
2616 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002617 final ArrayList<ActivityStack> displayStacks = toDisplay.mStacks;
2618 for (int i = displayStacks.size() - 1; i >= 0; --i) {
2619 final ActivityStack otherStack = displayStacks.get(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002620 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2621 continue;
2622 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002623 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002624 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002625 }
2626
2627 // Also disable docked stack resizing since we have manually adjusted the
2628 // size of other stacks above and we don't want to trigger a docked stack
2629 // resize when we remove task from it below and it is detached from the
2630 // display because it no longer contains any tasks.
2631 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002632 } else if (inPinnedWindowingMode && onTop) {
2633 // Log if we are expanding the PiP to fullscreen
2634 MetricsLogger.action(mService.mContext,
2635 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002636 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002637
Winson Chung5af42fc2017-03-24 17:11:33 -07002638 // If we are moving from the pinned stack, then the animation takes care of updating
2639 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002640 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002641 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002642
Wale Ogunwale388945c2017-10-04 12:13:46 -07002643 if (!tasks.isEmpty()) {
2644 final int size = tasks.size();
2645 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2646 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2647
2648 if (onTop) {
2649 final int returnToType =
2650 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
2651 for (int i = 0; i < size; i++) {
2652 final TaskRecord task = tasks.get(i);
2653 final boolean isTopTask = i == (size - 1);
2654 if (inPinnedWindowingMode) {
2655 // Update the return-to to reflect where the pinned stack task was moved
2656 // from so that we retain the stack that was previously visible if the
2657 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2658 task.setTaskToReturnTo(returnToType);
2659 }
2660 // Defer resume until all the tasks have been moved to the fullscreen stack
2661 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2662 isTopTask /* animate */, DEFER_RESUME,
2663 schedulePictureInPictureModeChange,
2664 "moveTasksToFullscreenStack - onTop");
Winson Chung0c1ca092016-11-10 17:40:34 -08002665 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07002666 } else {
2667 for (int i = 0; i < size; i++) {
2668 final TaskRecord task = tasks.get(i);
2669 // Position the tasks in the fullscreen stack in order at the bottom of the
2670 // stack. Also defer resume until all the tasks have been moved to the
2671 // fullscreen stack.
2672 task.reparent(fullscreenStack, i /* position */,
2673 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2674 schedulePictureInPictureModeChange,
2675 "moveTasksToFullscreenStack - NOT_onTop");
2676 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002677 }
2678 }
Winson Chung74666102017-02-22 17:49:24 -08002679
2680 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2681 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002682 } finally {
2683 mAllowDockedStackResize = true;
2684 mWindowManager.continueSurfaceLayout();
2685 }
2686 }
2687
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002688 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002689 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2690 }
2691
2692 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2693 mWindowManager.inSurfaceTransaction(() ->
2694 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002695 }
2696
Jorim Jaggidc249c42015-12-15 14:57:31 -08002697 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002698 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2699 boolean preserveWindows) {
2700 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2701 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2702 false /* deferResume */);
2703 }
2704
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002705 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002706 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2707 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002708
2709 if (!mAllowDockedStackResize) {
2710 // Docked stack resize currently disabled.
2711 return;
2712 }
2713
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002714 final ActivityStack stack = getDefaultDisplay().getStack(
2715 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002716 if (stack == null) {
2717 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2718 return;
2719 }
2720
2721 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2722 mWindowManager.deferSurfaceLayout();
2723 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002724 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2725 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002726 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002727 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002728
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002729 // TODO: Checking for isAttached might not be needed as if the user passes in null
2730 // dockedBounds then they want the docked stack to be dismissed.
2731 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2732 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002733 // In this case we make all other static stacks fullscreen and move all
2734 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002735 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002736
2737 // stack shouldn't contain anymore activities, so nothing to resume.
2738 r = null;
2739 } else {
2740 // Docked stacks occupy a dedicated region on screen so the size of all other
2741 // static stacks need to be adjusted so they don't overlap with the docked stack.
2742 // We get the bounds to use from window manager which has been adjusted for any
2743 // screen controls and is also the same for all stacks.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002744 final ArrayList<ActivityStack> stacks = getStacksOnDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002745 final Rect otherTaskRect = new Rect();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002746 for (int i = stacks.size() - 1; i >= 0; --i) {
2747 final ActivityStack current = stacks.get(i);
2748 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002749 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002750 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002751 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002752 continue;
2753 }
2754 // Need to set windowing mode here before we try to get the dock bounds.
2755 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2756 current.getStackDockedModeBounds(
2757 tempOtherTaskBounds /* currentTempTaskBounds */,
2758 tempRect /* outStackBounds */,
2759 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2760
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002761 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002762 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2763 tempOtherTaskInsetBounds, preserveWindows,
2764 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002765 }
2766 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002767 if (!deferResume) {
2768 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2769 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002770 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002771 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002772 mWindowManager.continueSurfaceLayout();
2773 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2774 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002775 }
2776
Robert Carr0d00c2e2016-02-29 17:45:02 -08002777 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002778 // TODO(multi-display): Pinned stack display should be passed in.
2779 final PinnedActivityStack stack = getDefaultDisplay().getStack(
2780 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002781 if (stack == null) {
2782 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2783 return;
2784 }
Winson Chung19953ca2017-04-11 11:19:23 -07002785
2786 // It is possible for the bounds animation from the WM to call this but be delayed by
2787 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2788 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2789 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2790 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002791 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002792 return;
2793 }
2794
Robert Carr0d00c2e2016-02-29 17:45:02 -08002795 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2796 mWindowManager.deferSurfaceLayout();
2797 try {
2798 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002799 Rect insetBounds = null;
2800 if (tempPinnedTaskBounds != null) {
2801 // We always use 0,0 as the position for the inset rect because
2802 // if we are getting insets at all in the pinned stack it must mean
2803 // we are headed for fullscreen.
2804 insetBounds = tempRect;
2805 insetBounds.top = 0;
2806 insetBounds.left = 0;
2807 insetBounds.right = tempPinnedTaskBounds.width();
2808 insetBounds.bottom = tempPinnedTaskBounds.height();
2809 }
2810 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002811 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002812 } finally {
2813 mWindowManager.continueSurfaceLayout();
2814 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2815 }
2816 }
2817
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002818 private void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002819 final ActivityStack stack = getStack(stackId);
2820 if (stack == null) {
2821 return;
2822 }
2823
2824 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002825 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002826 /**
2827 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2828 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2829 * that the client receives onStop() before it is reparented. We do this by detaching
2830 * the stack from the display so that it will be considered invisible when
2831 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2832 * invisible as well and added to the stopping list. After which we process the
2833 * stopping list by handling the idle.
2834 */
2835 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2836 pinnedStack.mForceHidden = true;
2837 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2838 pinnedStack.mForceHidden = false;
2839 activityIdleInternalLocked(null, false /* fromTimeout */,
2840 true /* processPausingActivites */, null /* configuration */);
2841
2842 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002843 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002844 } else {
2845 for (int i = tasks.size() - 1; i >= 0; i--) {
2846 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2847 REMOVE_FROM_RECENTS);
2848 }
2849 }
2850 }
2851
2852 /**
Robert Carr6914f082017-03-20 19:04:30 -07002853 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2854 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2855 * instead moved back onto the fullscreen stack.
2856 */
2857 void removeStackLocked(int stackId) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002858 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stackId));
2859 }
2860
Wale Ogunwale68278562017-09-23 17:13:55 -07002861 /**
2862 * Removes stacks in the input windowing modes from the system if they are of activity type
2863 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2864 */
2865 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002866 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002867 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2868 }
2869 }
2870
2871 void removeStacksWithActivityTypes(int... activityTypes) {
2872 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2873 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002874 }
Robert Carr6914f082017-03-20 19:04:30 -07002875 }
2876
2877 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002878 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2879 */
2880 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2881 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2882 }
2883
2884 /**
Winson Chung010927a2016-12-15 16:12:35 -08002885 * Removes the task with the specified task id.
2886 *
2887 * @param taskId Identifier of the task to be removed.
2888 * @param killProcess Kill any process associated with the task if possible.
2889 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002890 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2891 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002892 * @return Returns true if the given task was found and removed.
2893 */
Winson Chung6954fc92017-03-24 16:22:12 -07002894 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2895 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002896 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002897 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002898 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002899 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2900 if (tr.isPersistable) {
2901 mService.notifyTaskPersisterLocked(null, true);
2902 }
2903 return true;
2904 }
2905 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2906 return false;
2907 }
2908
2909 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2910 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002911 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002912 }
2913 ComponentName component = tr.getBaseIntent().getComponent();
2914 if (component == null) {
2915 Slog.w(TAG, "No component for base intent of task: " + tr);
2916 return;
2917 }
2918
2919 // Find any running services associated with this app and stop if needed.
2920 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2921
2922 if (!killProcess) {
2923 return;
2924 }
2925
2926 // Determine if the process(es) for this task should be killed.
2927 final String pkg = component.getPackageName();
2928 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2929 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2930 for (int i = 0; i < pmap.size(); i++) {
2931
2932 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2933 for (int j = 0; j < uids.size(); j++) {
2934 ProcessRecord proc = uids.valueAt(j);
2935 if (proc.userId != tr.userId) {
2936 // Don't kill process for a different user.
2937 continue;
2938 }
2939 if (proc == mService.mHomeProcess) {
2940 // Don't kill the home process along with tasks from the same package.
2941 continue;
2942 }
2943 if (!proc.pkgList.containsKey(pkg)) {
2944 // Don't kill process that is not associated with this task.
2945 continue;
2946 }
2947
2948 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002949 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002950 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2951 // Don't kill process(es) that has an activity in a different task that is
2952 // also in recents.
2953 return;
2954 }
2955 }
2956
2957 if (proc.foregroundServices) {
2958 // Don't kill process(es) with foreground service.
2959 return;
2960 }
2961
2962 // Add process to kill list.
2963 procsToKill.add(proc);
2964 }
2965 }
2966
2967 // Kill the running processes.
2968 for (int i = 0; i < procsToKill.size(); i++) {
2969 ProcessRecord pr = procsToKill.get(i);
2970 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2971 && pr.curReceivers.isEmpty()) {
2972 pr.kill("remove task", true);
2973 } else {
2974 // We delay killing processes that are not in the background or running a receiver.
2975 pr.waitingToKill = "remove task";
2976 }
2977 }
2978 }
2979
Craig Mautner4a1cb222013-12-04 16:14:06 -08002980 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002981 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002982 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002983 break;
2984 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002985 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002986 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002987 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002988 }
2989
Chong Zhang5dcb2752015-08-18 13:50:26 -07002990 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002991 * Called to restore the state of the task into the stack that it's supposed to go into.
2992 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002993 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002994 * @param aOptions The activity options to use for restoration.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002995 * @return true if the task has been restored successfully.
2996 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002997 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002998 final ActivityStack stack = getLaunchStack(null, aOptions, task, !ON_TOP);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002999 final ActivityStack currentStack = task.getStack();
3000 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003001 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003002 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003003 // Nothing else to do since it is already restored in the right stack.
3004 return true;
3005 }
3006 // Remove current stack association, so we can re-associate the task with the
3007 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003008 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003009 }
3010
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003011 stack.addTask(task, !ON_TOP, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003012 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003013 task.createWindowContainer(!ON_TOP, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3015 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003016 final ArrayList<ActivityRecord> activities = task.mActivities;
3017 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003018 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003019 }
3020 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003021 }
3022
Winson Chung1dbc8112017-09-28 18:05:31 -07003023 @Override
3024 public void onRecentTaskAdded(TaskRecord task) {
3025 task.touchActiveTime();
3026 }
3027
3028 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003029 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
3030 if (wasTrimmed) {
3031 // Task was trimmed from the recent tasks list -- remove the active task record as well
3032 // since the user won't really be able to go back to it
3033 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3034 false /* removeFromRecents */);
3035 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003036 task.removedFromRecents();
3037 }
3038
Wale Ogunwale040b4702015-08-06 18:10:50 -07003039 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003040 * Move stack with all its existing content to specified display.
3041 * @param stackId Id of stack to move.
3042 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003043 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003044 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003045 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003046 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003047 if (activityDisplay == null) {
3048 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3049 + displayId);
3050 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003051 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003052 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003053 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3054 + stackId);
3055 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003056
3057 final ActivityDisplay currentDisplay = stack.getDisplay();
3058 if (currentDisplay == null) {
3059 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3060 + " is not attached to any display.");
3061 }
3062
3063 if (currentDisplay.mDisplayId == displayId) {
3064 throw new IllegalArgumentException("Trying to move stack=" + stack
3065 + " to its current displayId=" + displayId);
3066 }
3067
3068 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003069 // TODO(multi-display): resize stacks properly if moved from split-screen.
3070 }
3071
3072 /**
Winson Chung74666102017-02-22 17:49:24 -08003073 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3074 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003075 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003076 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003077 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003078 final int stackId = stack.mStackId;
3079 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003080
Winson Chung74666102017-02-22 17:49:24 -08003081 // Check that we aren't reparenting to the same stack that the task is already in
3082 if (prevStack != null && prevStack.mStackId == stackId) {
3083 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3084 + " already in stackId=" + stackId);
3085 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003086 }
3087
Winson Chung74666102017-02-22 17:49:24 -08003088 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3089 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003090 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003091 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003092 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003093 }
3094
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003095 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3096 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003097 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003098 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3099 + " reparent task=" + task + " to stackId=" + stackId);
3100 }
3101
Winson Chung74666102017-02-22 17:49:24 -08003102 // Ensure that we aren't trying to move into a freeform stack without freeform
3103 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003104 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3105 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003106 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3107 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003108 }
3109
Winson Chung74666102017-02-22 17:49:24 -08003110 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3111 // used for split-screen mode and will cause things like the docked divider to show up. We
3112 // instead leave the task in its current stack or move it to the fullscreen stack if it
3113 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003114 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003115 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3116 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003117 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3118 // for example, a docked stack is created which will lead to the stack we are moving
3119 // from being resized and and its resizeable tasks being resized.
3120 try {
3121 task.mTemporarilyUnresizable = true;
3122 stack = stack.getDisplay().createStack(
3123 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3124 } finally {
3125 task.mTemporarilyUnresizable = false;
3126 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003127 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003128 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003129 }
3130
Winson Chung08f81892017-03-02 15:40:51 -08003131 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003132 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003133 if (stack == null) {
3134 throw new IllegalArgumentException(
3135 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3136 }
3137
3138 final ActivityRecord r = stack.topRunningActivityLocked();
3139 if (r == null) {
3140 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3141 + " in stack=" + stack);
3142 return false;
3143 }
3144
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003145 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003146 Slog.w(TAG,
3147 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003148 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003149 return false;
3150 }
3151
Winson Chung3a682872017-04-10 14:38:05 -07003152 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003153 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003154 return true;
3155 }
3156
Winson Chung8bca9e42017-04-16 15:59:43 -07003157 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003158 boolean moveHomeStackToFront, String reason) {
3159
Wale Ogunwale480dca02016-02-06 13:58:29 -08003160 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003161
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003162 final ActivityDisplay display = r.getStack().getDisplay();
3163 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003164
Bryce Lee04ab3462017-04-10 15:06:33 -07003165 // This will clear the pinned stack by moving an existing task to the full screen stack,
3166 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003167 if (stack != null) {
3168 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3169 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003170
Wale Ogunwale1666e312016-12-16 11:27:18 -08003171 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003172 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003173
Wale Ogunwale480dca02016-02-06 13:58:29 -08003174 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003175 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003176 // Resize the pinned stack to match the current size of the task the activity we are
3177 // going to be moving is currently contained in. We do this to have the right starting
3178 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003179 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003180 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003181 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003182
3183 if (task.mActivities.size() == 1) {
3184 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003185 // the stack without re-parenting the activity in a different task. We don't
3186 // move the home stack forward if we are currently entering picture-in-picture
3187 // while pausing because that changes the focused stack and may prevent the new
3188 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003189 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003190 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003191 // Move the home stack forward if the task we just moved to the pinned stack
3192 // was launched from home so home should be visible behind it.
3193 moveHomeStackToFront(reason);
3194 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003195 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003196 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3197 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003198 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003199 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003200 // reveal/leave the other activities in their original task.
3201
3202 // Currently, we don't support reparenting activities across tasks in two different
3203 // stacks, so instead, just create a new task in the same stack, reparent the
3204 // activity into that task, and then reparent the whole task to the new stack. This
3205 // ensures that all the necessary work to migrate states in the old and new stacks
3206 // is also done.
3207 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003208 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003209 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3210
Winson Chung5af42fc2017-03-24 17:11:33 -07003211 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003212 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003213 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003214 }
Winson Chungc2baac02017-01-11 13:34:47 -08003215
3216 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3217 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003218 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003219 } finally {
3220 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003221 }
3222
Winson Chunge7ba6862017-05-24 12:13:33 -07003223 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3224 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3225 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003226 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3227
Winson Chunge7ba6862017-05-24 12:13:33 -07003228 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3229 true /* fromFullscreen */);
3230
3231 // Update the visibility of all activities after the they have been reparented to the new
3232 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3233 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3234 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003235 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003236 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003237
Wale Ogunwale89be5762017-10-04 13:27:49 -07003238 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003239 }
3240
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003241 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003242 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3243 if (r == null || !r.isFocusable()) {
3244 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3245 "moveActivityStackToFront: unfocusable r=" + r);
3246 return false;
3247 }
3248
Bryce Leeaf691c02017-03-20 14:20:22 -07003249 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003250 final ActivityStack stack = r.getStack();
3251 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003252 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3253 + r + " task=" + task);
3254 return false;
3255 }
3256
Chong Zhang6cda19c2016-06-14 19:07:56 -07003257 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3258 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3259 "moveActivityStackToFront: already on top, r=" + r);
3260 return false;
3261 }
3262
3263 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3264 "moveActivityStackToFront: r=" + r);
3265
3266 stack.moveToFront(reason, task);
3267 return true;
3268 }
3269
David Stevensc6b91c62017-02-08 14:23:58 -08003270 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003271 mTmpFindTaskResult.r = null;
3272 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003273 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003274 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003275 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3276 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003277 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3278 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003279 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003280 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3281 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003282 continue;
3283 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003284 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003285 // It is possible to have tasks in multiple stacks with the same root affinity, so
3286 // we should keep looking after finding an affinity match to see if there is a
3287 // better match in another stack. Also, task affinity isn't a good enough reason
3288 // to target a display which isn't the source of the intent, so skip any affinity
3289 // matches not on the specified display.
3290 if (mTmpFindTaskResult.r != null) {
3291 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3292 return mTmpFindTaskResult.r;
3293 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003294 // Note: since the traversing through the stacks is top down, the floating
3295 // tasks should always have lower priority than any affinity-matching tasks
3296 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003297 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003298 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3299 Slog.d(TAG_TASKS, "Skipping match on different display "
3300 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003301 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003303 }
3304 }
Winson Chung5b895b72017-05-01 13:46:25 -07003305
David Stevensc6b91c62017-02-08 14:23:58 -08003306 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3307 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003308 }
3309
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003310 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3311 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003312 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3313 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003314 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003315 final ActivityRecord ar = stacks.get(stackNdx)
3316 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003317 if (ar != null) {
3318 return ar;
3319 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003320 }
3321 }
3322 return null;
3323 }
3324
David Stevens9440dc82017-03-16 19:00:20 -07003325 boolean hasAwakeDisplay() {
3326 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3327 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3328 if (!display.shouldSleep()) {
3329 return true;
3330 }
3331 }
3332 return false;
3333 }
3334
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003336 scheduleSleepTimeout();
3337 if (!mGoingToSleep.isHeld()) {
3338 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003339 if (mLaunchingActivity.isHeld()) {
3340 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3341 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003342 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003343 mLaunchingActivity.release();
3344 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003345 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 }
David Stevens9440dc82017-03-16 19:00:20 -07003347
3348 applySleepTokensLocked(false /* applyToStacks */);
3349
3350 checkReadyForSleepLocked(true /* allowDelay */);
3351 }
3352
3353 void prepareForShutdownLocked() {
3354 for (int i = 0; i < mActivityDisplays.size(); i++) {
3355 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3356 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003357 }
3358
3359 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003360 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003361
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003362 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003363 final long endTime = System.currentTimeMillis() + timeout;
3364 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003365 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003366 long timeRemaining = endTime - System.currentTimeMillis();
3367 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003368 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003369 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003370 } catch (InterruptedException e) {
3371 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003372 } else {
3373 Slog.w(TAG, "Activity manager shutdown timed out");
3374 timedout = true;
3375 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003376 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003377 } else {
3378 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003379 }
3380 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003381
3382 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003383 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003384
Craig Mautner8d341ef2013-03-26 09:03:27 -07003385 return timedout;
3386 }
3387
3388 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003389 removeSleepTimeouts();
3390 if (mGoingToSleep.isHeld()) {
3391 mGoingToSleep.release();
3392 }
David Stevens9440dc82017-03-16 19:00:20 -07003393 }
3394
3395 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003396 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003397 // Set the sleeping state of the display.
3398 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3399 final boolean displayShouldSleep = display.shouldSleep();
3400 if (displayShouldSleep == display.isSleeping()) {
3401 continue;
3402 }
3403 display.setIsSleeping(displayShouldSleep);
3404
3405 if (!applyToStacks) {
3406 continue;
3407 }
3408
3409 // Set the sleeping state of the stacks on the display.
3410 final ArrayList<ActivityStack> stacks = display.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3412 final ActivityStack stack = stacks.get(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003413 if (displayShouldSleep) {
3414 stack.goToSleepIfPossible(false /* shuttingDown */);
3415 } else {
3416 stack.awakeFromSleepingLocked();
3417 if (isFocusedStack(stack)) {
3418 resumeFocusedStackTopActivityLocked();
3419 }
3420 }
3421 }
3422
3423 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3424 continue;
3425 }
3426 // The display is awake now, so clean up the going to sleep list.
3427 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3428 final ActivityRecord r = it.next();
3429 if (r.getDisplayId() == display.mDisplayId) {
3430 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003431 }
Craig Mautner5314a402013-09-26 12:40:16 -07003432 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003433 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003434 }
3435
3436 void activitySleptLocked(ActivityRecord r) {
3437 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003438 final ActivityStack s = r.getStack();
3439 if (s != null) {
3440 s.checkReadyForSleep();
3441 } else {
3442 checkReadyForSleepLocked(true);
3443 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444 }
3445
David Stevens9440dc82017-03-16 19:00:20 -07003446 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003447 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003448 // Do not care.
3449 return;
3450 }
3451
David Stevens18abd0e2017-08-17 14:55:47 -07003452 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3453 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003454 }
3455
Wei Wang65c7a152016-06-02 18:51:22 -07003456 // Send launch end powerhint before going sleep
3457 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3458
Craig Mautner0eea92c2013-05-16 13:35:39 -07003459 removeSleepTimeouts();
3460
3461 if (mGoingToSleep.isHeld()) {
3462 mGoingToSleep.release();
3463 }
3464 if (mService.mShuttingDown) {
3465 mService.notifyAll();
3466 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003467 }
3468
David Stevens18abd0e2017-08-17 14:55:47 -07003469 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3470 // successfully put to sleep.
3471 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3472 boolean allSleep = true;
3473 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3474 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3475 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3476 if (allowDelay) {
3477 allSleep &= stacks.get(stackNdx).goToSleepIfPossible(shuttingDown);
3478 } else {
3479 stacks.get(stackNdx).goToSleep();
3480 }
3481 }
3482 }
3483 return allSleep;
3484 }
3485
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003486 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003487 // A resumed activity cannot be stopping. remove from list
3488 mStoppingActivities.remove(r);
3489
Andrii Kulian02b7a832016-10-06 23:11:56 -07003490 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003491 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003492 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003493 }
3494 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003495 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003496 mWindowManager.executeAppTransition();
3497 return true;
3498 }
3499 return false;
3500 }
3501
Craig Mautner8d341ef2013-03-26 09:03:27 -07003502 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003503 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3504 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003505 int stackNdx = stacks.size() - 1;
3506 while (stackNdx >= 0) {
3507 stacks.get(stackNdx).handleAppCrashLocked(app);
3508 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003509 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003510 }
3511 }
3512
Craig Mautnerbb742462014-07-07 15:28:55 -07003513 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003514 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003515 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003516 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003517
3518 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003519 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003520 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003521 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003522
3523 // When launching tasks behind, update the last active time of the top task after the new
3524 // task has been shown briefly
3525 final ActivityRecord top = stack.topActivity();
3526 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003527 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003528 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003529 }
3530
3531 void scheduleLaunchTaskBehindComplete(IBinder token) {
3532 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3533 }
3534
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003535 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3536 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003537 mKeyguardController.beginActivityVisibilityUpdate();
3538 try {
3539 // First the front stacks. In case any are not fullscreen and are in front of home.
3540 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3541 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3542 final int topStackNdx = stacks.size() - 1;
3543 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3544 final ActivityStack stack = stacks.get(stackNdx);
3545 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3546 }
Craig Mautner580ea812013-04-25 12:58:38 -07003547 }
Jorim Jaggife762342016-10-13 14:33:27 +02003548 } finally {
3549 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003550 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003551 }
3552
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003553 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3554 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3555 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3556 final int topStackNdx = stacks.size() - 1;
3557 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3558 final ActivityStack stack = stacks.get(stackNdx);
3559 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3560 }
3561 }
3562 }
3563
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003564 void invalidateTaskLayers() {
3565 mTaskLayersChanged = true;
3566 }
3567
3568 void rankTaskLayersIfNeeded() {
3569 if (!mTaskLayersChanged) {
3570 return;
3571 }
3572 mTaskLayersChanged = false;
3573 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3574 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3575 int baseLayer = 0;
3576 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3577 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3578 }
3579 }
3580 }
3581
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003582 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3585 final int topStackNdx = stacks.size() - 1;
3586 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3587 final ActivityStack stack = stacks.get(stackNdx);
3588 stack.clearOtherAppTimeTrackers(except);
3589 }
3590 }
3591 }
3592
Craig Mautner8d341ef2013-03-26 09:03:27 -07003593 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003594 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3595 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 final int numStacks = stacks.size();
3597 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3598 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003599 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003601 }
3602 }
3603
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003604 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3605 // Examine all activities currently running in the process.
3606 TaskRecord firstTask = null;
3607 // Tasks is non-null only if two or more tasks are found.
3608 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003609 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3610 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003611 ActivityRecord r = app.activities.get(i);
3612 // First, if we find an activity that is in the process of being destroyed,
3613 // then we just aren't going to do anything for now; we want things to settle
3614 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003615 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003616 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 return;
3618 }
3619 // Don't consider any activies that are currently not in a state where they
3620 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003621 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3622 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003623 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003624 continue;
3625 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003626
3627 final TaskRecord task = r.getTask();
3628 if (task != null) {
3629 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003630 + " from " + r);
3631 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003632 firstTask = task;
3633 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003634 if (tasks == null) {
3635 tasks = new ArraySet<>();
3636 tasks.add(firstTask);
3637 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003638 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003639 }
3640 }
3641 }
3642 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003643 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003644 return;
3645 }
3646 // If we have activities in multiple tasks that are in a position to be destroyed,
3647 // let's iterate through the tasks and release the oldest one.
3648 final int numDisplays = mActivityDisplays.size();
3649 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3650 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3651 // Step through all stacks starting from behind, to hit the oldest things first.
3652 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3653 final ActivityStack stack = stacks.get(stackNdx);
3654 // Try to release activities in this stack; if we manage to, we are done.
3655 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3656 return;
3657 }
3658 }
3659 }
3660 }
3661
Amith Yamasani37a40c22015-06-17 13:25:42 -07003662 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003663 final int focusStackId = mFocusedStack.getStackId();
3664 // We dismiss the docked stack whenever we switch users.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003665 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003666 if (dockedStack != null) {
3667 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3668 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003669 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3670 // also cause all tasks to be moved to the fullscreen stack at a position that is
3671 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003672 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003673
3674 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003675 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003676 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003677
Craig Mautner858d8a62013-04-23 17:08:34 -07003678 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003679 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3680 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003682 final ActivityStack stack = stacks.get(stackNdx);
3683 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003684 TaskRecord task = stack.topTask();
3685 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003686 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003687 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003688 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003689 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003690
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003691 ActivityStack stack = getStack(restoreStackId);
3692 if (stack == null) {
3693 stack = mHomeStack;
3694 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003695 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003696 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003697 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003698 } else {
3699 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003700 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003701 }
Craig Mautner93529a42013-10-04 15:03:13 -07003702 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003703 }
3704
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003705 /** Checks whether the userid is a profile of the current user. */
3706 boolean isCurrentProfileLocked(int userId) {
3707 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003708 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003709 }
3710
Bryce Leeb7c9b802017-05-02 14:20:24 -07003711 /**
3712 * Returns whether a stopping activity is present that should be stopped after visible, rather
3713 * than idle.
3714 * @return {@code true} if such activity is present. {@code false} otherwise.
3715 */
3716 boolean isStoppingNoHistoryActivity() {
3717 // Activities that are marked as nohistory should be stopped immediately after the resumed
3718 // activity has become visible.
3719 for (ActivityRecord record : mStoppingActivities) {
3720 if (record.isNoHistory()) {
3721 return true;
3722 }
3723 }
3724
3725 return false;
3726 }
3727
Winson Chung4dabf232017-01-25 13:25:22 -08003728 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3729 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003730 ArrayList<ActivityRecord> stops = null;
3731
3732 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003733 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3734 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003735 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003736 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003737 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3738 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003739 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003740 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003741 if (s.finishing) {
3742 // If this activity is finishing, it is sitting on top of
3743 // everyone else but we now know it is no longer needed...
3744 // so get rid of it. Otherwise, we need to go through the
3745 // normal flow and hide it once we determine that it is
3746 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003747 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003748 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003749 }
3750 }
David Stevens9440dc82017-03-16 19:00:20 -07003751 if (remove) {
3752 final ActivityStack stack = s.getStack();
3753 final boolean shouldSleepOrShutDown = stack != null
3754 ? stack.shouldSleepOrShutDownActivities()
3755 : mService.isSleepingOrShuttingDownLocked();
3756 if (!waitingVisible || shouldSleepOrShutDown) {
3757 if (!processPausingActivities && s.state == PAUSING) {
3758 // Defer processing pausing activities in this iteration and reschedule
3759 // a delayed idle to reprocess it again
3760 removeTimeoutsForActivityLocked(idleActivity);
3761 scheduleIdleTimeoutLocked(idleActivity);
3762 continue;
3763 }
Winson Chung4dabf232017-01-25 13:25:22 -08003764
David Stevens9440dc82017-03-16 19:00:20 -07003765 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3766 if (stops == null) {
3767 stops = new ArrayList<>();
3768 }
3769 stops.add(s);
3770 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003771 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003772 }
3773 }
3774
3775 return stops;
3776 }
3777
Craig Mautnercf910b02013-04-23 11:23:27 -07003778 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003779 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3780 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3781 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3782 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003783 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003784 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003785 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003786 if (r == null) Slog.e(TAG,
3787 "validateTop...: null top activity, stack=" + stack);
3788 else {
3789 final ActivityRecord pausing = stack.mPausingActivity;
3790 if (pausing != null && pausing == r) Slog.e(TAG,
3791 "validateTop...: top stack has pausing activity r=" + r
3792 + " state=" + state);
3793 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3794 "validateTop...: activity in front not resumed r=" + r
3795 + " state=" + state);
3796 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003797 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003798 final ActivityRecord resumed = stack.mResumedActivity;
3799 if (resumed != null && resumed == r) Slog.e(TAG,
3800 "validateTop...: back stack has resumed activity r=" + r
3801 + " state=" + state);
3802 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3803 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003804 }
3805 }
3806 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003807 }
3808
Craig Mautner27084302013-03-25 08:05:25 -07003809 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003810 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003811 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003812 pw.print(prefix);
3813 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003814 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003815 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3816 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3817 pw.println(prefix + "displayId=" + display.mDisplayId + " mStacks=" + display.mStacks);
3818 }
Bryce Lee4a194382017-04-04 14:32:48 -07003819 if (!mWaitingForActivityVisible.isEmpty()) {
3820 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3821 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3822 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3823 }
3824 }
3825
Jorim Jaggi8d786932016-10-26 19:08:36 -07003826 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003827 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003828 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003829
Steven Timotius4346f0a2017-09-12 11:07:21 -07003830 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3831 final long token = proto.start(fieldId);
3832 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3833 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3834 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3835 activityDisplay.writeToProto(proto, DISPLAYS);
3836 }
3837 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3838 if (mFocusedStack != null) {
3839 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3840 ActivityRecord focusedActivity = getResumedActivityLocked();
3841 if (focusedActivity != null) {
3842 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3843 }
3844 } else {
3845 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3846 }
3847 proto.end(token);
3848 }
3849
Winson43d1f262016-06-14 16:05:55 -07003850 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003851 * Dump all connected displays' configurations.
3852 * @param prefix Prefix to apply to each line of the dump.
3853 */
3854 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3855 pw.print(prefix); pw.println("Display override configurations:");
3856 final int displayCount = mActivityDisplays.size();
3857 for (int i = 0; i < displayCount; i++) {
3858 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3859 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3860 pw.println(activityDisplay.getOverrideConfiguration());
3861 }
3862 }
3863
3864 /**
Winson43d1f262016-06-14 16:05:55 -07003865 * Dumps the activities matching the given {@param name} in the either the focused stack
3866 * or all visible stacks if {@param dumpVisibleStacks} is true.
3867 */
Winson Chung6998bc42017-02-28 17:07:05 -08003868 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3869 boolean dumpFocusedStackOnly) {
3870 if (dumpFocusedStackOnly) {
3871 return mFocusedStack.getDumpActivitiesLocked(name);
3872 } else {
Winson43d1f262016-06-14 16:05:55 -07003873 ArrayList<ActivityRecord> activities = new ArrayList<>();
3874 int numDisplays = mActivityDisplays.size();
3875 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3876 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3877 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3878 ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003879 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003880 activities.addAll(stack.getDumpActivitiesLocked(name));
3881 }
3882 }
3883 }
3884 return activities;
Winson43d1f262016-06-14 16:05:55 -07003885 }
Craig Mautner20e72272013-04-01 13:45:53 -07003886 }
3887
Dianne Hackborn390517b2013-05-30 15:03:32 -07003888 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3889 boolean needSep, String prefix) {
3890 if (activity != null) {
3891 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3892 if (needSep) {
3893 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003894 }
3895 pw.print(prefix);
3896 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003897 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003898 }
3899 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003900 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003901 }
3902
Craig Mautner8d341ef2013-03-26 09:03:27 -07003903 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3904 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003905 boolean printed = false;
3906 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003907 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3908 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003909 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003910 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003911 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003912 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003914 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003915 pw.println(" Stack #" + stack.mStackId
3916 + ": type=" + activityTypeToString(getActivityType())
3917 + " mode=" + windowingModeToString(getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003918 pw.println(" mFullscreen=" + stack.mFullscreen);
3919 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3920 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003921
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003922 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3923 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003924
Craig Mautner4a1cb222013-12-04 16:14:06 -08003925 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3926 !dumpAll, false, dumpPackage, true,
3927 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003928
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 needSep = printed;
3930 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3931 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003932 if (pr) {
3933 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003935 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003936 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3937 " mResumedActivity: ");
3938 if (pr) {
3939 printed = true;
3940 needSep = false;
3941 }
3942 if (dumpAll) {
3943 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3944 " mLastPausedActivity: ");
3945 if (pr) {
3946 printed = true;
3947 needSep = true;
3948 }
3949 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3950 needSep, " mLastNoHistoryActivity: ");
3951 }
3952 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003953 }
3954 }
3955
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003956 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3957 false, dumpPackage, true, " Activities waiting to finish:", null);
3958 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3959 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003960 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3961 false, !dumpAll, false, dumpPackage, true,
3962 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003963 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3964 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003965
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003966 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003967 }
3968
Dianne Hackborn390517b2013-05-30 15:03:32 -07003969 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003971 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 String innerPrefix = null;
3973 String[] args = null;
3974 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003975 for (int i=list.size()-1; i>=0; i--) {
3976 final ActivityRecord r = list.get(i);
3977 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3978 continue;
3979 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003980 if (innerPrefix == null) {
3981 innerPrefix = prefix + " ";
3982 args = new String[0];
3983 }
3984 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003985 final boolean full = !brief && (complete || !r.isInHistory());
3986 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003987 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003988 needNL = false;
3989 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003990 if (header != null) {
3991 pw.println(header);
3992 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003993 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003994 if (lastTask != r.getTask()) {
3995 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003996 pw.print(prefix);
3997 pw.print(full ? "* " : " ");
3998 pw.println(lastTask);
3999 if (full) {
4000 lastTask.dump(pw, prefix + " ");
4001 } else if (complete) {
4002 // Complete + brief == give a summary. Isn't that obvious?!?
4003 if (lastTask.intent != null) {
4004 pw.print(prefix); pw.print(" ");
4005 pw.println(lastTask.intent.toInsecureStringWithClip());
4006 }
4007 }
4008 }
4009 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4010 pw.print(" #"); pw.print(i); pw.print(": ");
4011 pw.println(r);
4012 if (full) {
4013 r.dump(pw, innerPrefix);
4014 } else if (complete) {
4015 // Complete + brief == give a summary. Isn't that obvious?!?
4016 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4017 if (r.app != null) {
4018 pw.print(innerPrefix); pw.println(r.app);
4019 }
4020 }
4021 if (client && r.app != null && r.app.thread != null) {
4022 // flush anything that is already in the PrintWriter since the thread is going
4023 // to write to the file descriptor directly
4024 pw.flush();
4025 try {
4026 TransferPipe tp = new TransferPipe();
4027 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004028 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004029 // Short timeout, since blocking here can
4030 // deadlock with the application.
4031 tp.go(fd, 2000);
4032 } finally {
4033 tp.kill();
4034 }
4035 } catch (IOException e) {
4036 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4037 } catch (RemoteException e) {
4038 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4039 }
4040 needNL = true;
4041 }
4042 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004043 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004044 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004045
Craig Mautnerf3333272013-04-22 10:55:53 -07004046 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004047 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4048 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004049 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4050 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004051 }
4052
4053 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004054 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004055 }
4056
4057 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004058 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4059 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004060 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4061 }
4062
Craig Mautner05d29032013-05-03 13:40:13 -07004063 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004064 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4065 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4066 }
Craig Mautner05d29032013-05-03 13:40:13 -07004067 }
4068
Craig Mautner0eea92c2013-05-16 13:35:39 -07004069 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004070 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4071 }
4072
4073 final void scheduleSleepTimeout() {
4074 removeSleepTimeouts();
4075 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4076 }
4077
Craig Mautner4a1cb222013-12-04 16:14:06 -08004078 @Override
4079 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004080 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4082 }
4083
4084 @Override
4085 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004086 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004087 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4088 }
4089
4090 @Override
4091 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004092 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4094 }
4095
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004096 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004098 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 }
4101
Andrii Kulianca007a62016-11-22 16:33:28 -08004102 /** Check if display with specified id is added to the list. */
4103 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004104 return getActivityDisplayOrCreateLocked(displayId) != null;
4105 }
4106
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004107 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004108 ActivityDisplay getActivityDisplay(int displayId) {
4109 return mActivityDisplays.get(displayId);
4110 }
4111
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004112 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4113 ActivityDisplay getDefaultDisplay() {
4114 return mActivityDisplays.get(DEFAULT_DISPLAY);
4115 }
4116
Andrii Kulian62e6f252017-05-30 22:46:53 -07004117 /**
4118 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4119 * corresponding record in display manager.
4120 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004121 // TODO: Look into consolidating with getActivityDisplay()
4122 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004123 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4124 if (activityDisplay != null) {
4125 return activityDisplay;
4126 }
4127 if (mDisplayManager == null) {
4128 // The system isn't fully initialized yet.
4129 return null;
4130 }
4131 final Display display = mDisplayManager.getDisplay(displayId);
4132 if (display == null) {
4133 // The display is not registered in DisplayManager.
4134 return null;
4135 }
4136 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004137 activityDisplay = new ActivityDisplay(this, displayId);
4138 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004139 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4140 mWindowManager.onDisplayAdded(displayId);
4141 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004142 }
4143
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004144 @VisibleForTesting
4145 void attachDisplay(ActivityDisplay display) {
4146 mActivityDisplays.put(display.mDisplayId, display);
4147 }
4148
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004149 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004150 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004151 mService.mContext.getResources().getDimensionPixelSize(
4152 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004153 }
4154
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004155 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004156 if (displayId == DEFAULT_DISPLAY) {
4157 throw new IllegalArgumentException("Can't remove the primary display.");
4158 }
4159
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) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004163 final boolean destroyContentOnRemoval
4164 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004165 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4166 while (!stacks.isEmpty()) {
4167 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004168 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004169 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4170 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004171 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004172 } else {
4173 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4174 // a valid launch stack for all activities. This way the task history from
4175 // external display will be preserved on primary after move.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004176 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004177 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004178 }
David Stevens9440dc82017-03-16 19:00:20 -07004179
4180 releaseSleepTokens(activityDisplay);
4181
Craig Mautnere0a38842013-12-16 16:14:02 -08004182 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004183 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004184 }
4185 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 }
4187
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004188 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004189 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004190 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4191 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004192 // The window policy is responsible for stopping activities on the default display
4193 if (displayId != Display.DEFAULT_DISPLAY) {
4194 int displayState = activityDisplay.mDisplay.getState();
4195 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4196 activityDisplay.mOffToken =
4197 mService.acquireSleepToken("Display-off", displayId);
4198 } else if (displayState == Display.STATE_ON
4199 && activityDisplay.mOffToken != null) {
4200 activityDisplay.mOffToken.release();
4201 activityDisplay.mOffToken = null;
4202 }
4203 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004204 // TODO: Update the bounds.
4205 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004206 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004207 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004208 }
4209
David Stevens9440dc82017-03-16 19:00:20 -07004210 SleepToken createSleepTokenLocked(String tag, int displayId) {
4211 ActivityDisplay display = mActivityDisplays.get(displayId);
4212 if (display == null) {
4213 throw new IllegalArgumentException("Invalid display: " + displayId);
4214 }
4215
4216 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4217 mSleepTokens.add(token);
4218 display.mAllSleepTokens.add(token);
4219 return token;
4220 }
4221
4222 private void removeSleepTokenLocked(SleepTokenImpl token) {
4223 mSleepTokens.remove(token);
4224
4225 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4226 if (display != null) {
4227 display.mAllSleepTokens.remove(token);
4228 if (display.mAllSleepTokens.isEmpty()) {
4229 mService.updateSleepIfNeededLocked();
4230 }
4231 }
4232 }
4233
4234 private void releaseSleepTokens(ActivityDisplay display) {
4235 if (display.mAllSleepTokens.isEmpty()) {
4236 return;
4237 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004238 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004239 mSleepTokens.remove(token);
4240 }
4241 display.mAllSleepTokens.clear();
4242
4243 mService.updateSleepIfNeededLocked();
4244 }
4245
Wale Ogunwale68278562017-09-23 17:13:55 -07004246 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004247 final int displayId = stack.mDisplayId;
4248 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004249 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004250 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004251 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004252 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004253 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004254 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004255 // A stack might be not attached to a display.
Wale Ogunwale68278562017-09-23 17:13:55 -07004256 info.position = display != null ? display.mStacks.indexOf(stack) : 0;
4257 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004258
4259 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4260 final int numTasks = tasks.size();
4261 int[] taskIds = new int[numTasks];
4262 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004263 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004264 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004265 for (int i = 0; i < numTasks; ++i) {
4266 final TaskRecord task = tasks.get(i);
4267 taskIds[i] = task.taskId;
4268 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4269 : task.realActivity != null ? task.realActivity.flattenToString()
4270 : task.getTopActivity() != null ? task.getTopActivity().packageName
4271 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004272 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004273 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004274 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004275 }
4276 info.taskIds = taskIds;
4277 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004278 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004279 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004280
4281 final ActivityRecord top = stack.topRunningActivityLocked();
4282 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 return info;
4284 }
4285
Wale Ogunwale68278562017-09-23 17:13:55 -07004286 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004287 ActivityStack stack = getStack(stackId);
4288 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004289 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004290 }
4291 return null;
4292 }
4293
Wale Ogunwale68278562017-09-23 17:13:55 -07004294 StackInfo getStackInfo(int windowingMode, int activityType) {
4295 final ActivityStack stack = getStack(windowingMode, activityType);
4296 return (stack != null) ? getStackInfo(stack) : null;
4297 }
4298
Craig Mautner4a1cb222013-12-04 16:14:06 -08004299 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004300 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004301 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4302 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004303 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004304 list.add(getStackInfo(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004305 }
4306 }
4307 return list;
4308 }
4309
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004310 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004311 int preferredDisplayId, int actualStackId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004312 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
4313 actualStackId, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004314 }
4315
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004316 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004317 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4318 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004319 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004320 final ActivityStack actualStack = getStack(actualStackId);
4321 final boolean inSplitScreenMode = actualStack != null
4322 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004323 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004324 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004325 return;
4326 }
4327
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004328 // Handle incorrect launch/move to secondary display if needed.
4329 final boolean launchOnSecondaryDisplayFailed;
4330 if (isSecondaryDisplayPreferred) {
4331 final int actualDisplayId = task.getStack().mDisplayId;
4332 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4333 // The task landed on an inappropriate display somehow, move it to the default
4334 // display.
4335 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004336 mService.setTaskWindowingMode(task.taskId,
4337 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004338 launchOnSecondaryDisplayFailed = true;
4339 } else {
4340 // The task might have landed on a display different from requested.
4341 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4342 || (preferredDisplayId != INVALID_DISPLAY
4343 && preferredDisplayId != actualDisplayId);
4344 }
4345 } else {
4346 // The task wasn't requested to be on a secondary display.
4347 launchOnSecondaryDisplayFailed = false;
4348 }
4349
Jorim Jaggicd13d332016-04-27 15:40:20 -07004350 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004351 if (launchOnSecondaryDisplayFailed
4352 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004353 if (launchOnSecondaryDisplayFailed) {
4354 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4355 // display with config different from global config.
4356 mService.mTaskChangeNotificationController
4357 .notifyActivityLaunchOnSecondaryDisplayFailed();
4358 } else {
4359 // Display a warning toast that we tried to put a non-dockable task in the docked
4360 // stack.
4361 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4362 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004363
Andrii Kulianc27916642016-04-12 17:59:27 -07004364 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4365 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004366
4367 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004368 if (dockedStack != null) {
4369 moveTasksToFullscreenStackLocked(dockedStack,
4370 actualStackId == dockedStack.getStackId());
4371 }
Winson Chungd3395382016-12-13 11:49:09 -08004372 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004373 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004374 final String packageName = topActivity.appInfo.packageName;
4375 final int reason = isSecondaryDisplayPreferred
4376 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4377 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004378 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004379 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004380 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004381 }
4382
Jorim Jaggife89d122015-12-22 16:28:44 +01004383 void activityRelaunchedLocked(IBinder token) {
4384 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004385 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4386 if (r != null) {
4387 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004388 r.setSleeping(true, true);
4389 }
4390 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004391 }
4392
4393 void activityRelaunchingLocked(ActivityRecord r) {
4394 mWindowManager.notifyAppRelaunching(r.appToken);
4395 }
4396
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004397 void logStackState() {
4398 mActivityMetricsLogger.logWindowState();
4399 }
4400
Winson Chung5af42fc2017-03-24 17:11:33 -07004401 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004402 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4403 // end, then ensure that we defer all in between multi-window mode changes
4404 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4405 return;
4406 }
4407
Wale Ogunwale22e25262016-02-01 10:32:02 -08004408 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4409 final ActivityRecord r = task.mActivities.get(i);
4410 if (r.app != null && r.app.thread != null) {
4411 mMultiWindowModeChangedActivities.add(r);
4412 }
4413 }
4414
4415 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4416 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4417 }
4418 }
4419
Winson Chung5af42fc2017-03-24 17:11:33 -07004420 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004421 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004422 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004423 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004424 return;
4425 }
4426
Winson Chungab76bbc2017-08-14 13:33:51 -07004427 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004428 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004429
Winson Chungab76bbc2017-08-14 13:33:51 -07004430 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4431 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4432 final ActivityRecord r = task.mActivities.get(i);
4433 if (r.app != null && r.app.thread != null) {
4434 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004435 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004436 }
4437 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004438
Winson Chungab76bbc2017-08-14 13:33:51 -07004439 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4440 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4441 }
4442 }
4443
4444 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4445 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4446 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4447 final ActivityRecord r = task.mActivities.get(i);
4448 if (r.app != null && r.app.thread != null) {
4449 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004450 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004451 }
4452 }
4453
Tony Mak853304c2016-04-18 15:17:41 +01004454 void setDockedStackMinimized(boolean minimized) {
4455 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004456 }
4457
chaviw59b98852017-06-13 12:05:44 -07004458 void wakeUp(String reason) {
4459 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4460 }
4461
4462 /**
4463 * Begin deferring resume to avoid duplicate resumes in one pass.
4464 */
4465 private void beginDeferResume() {
4466 mDeferResumeCount++;
4467 }
4468
4469 /**
4470 * End deferring resume and determine if resume can be called.
4471 */
4472 private void endDeferResume() {
4473 mDeferResumeCount--;
4474 }
4475
4476 /**
4477 * @return True if resume can be called.
4478 */
4479 private boolean readyToResume() {
4480 return mDeferResumeCount == 0;
4481 }
4482
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004483 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004484
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004485 public ActivityStackSupervisorHandler(Looper looper) {
4486 super(looper);
4487 }
4488
Winson Chung4dabf232017-01-25 13:25:22 -08004489 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004490 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004491 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4492 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004493 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004494 }
4495
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004496 @Override
4497 public void handleMessage(Message msg) {
4498 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004499 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4500 synchronized (mService) {
4501 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4502 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004503 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004504 }
4505 }
4506 } break;
4507 case REPORT_PIP_MODE_CHANGED_MSG: {
4508 synchronized (mService) {
4509 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4510 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004511 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4512 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004513 }
4514 }
4515 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004516 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004517 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4518 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004519 // We don't at this point know if the activity is fullscreen,
4520 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004521 activityIdleInternal((ActivityRecord) msg.obj,
4522 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004523 } break;
4524 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004525 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004526 activityIdleInternal((ActivityRecord) msg.obj,
4527 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004528 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004529 case RESUME_TOP_ACTIVITY_MSG: {
4530 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004531 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004532 }
4533 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004534 case SLEEP_TIMEOUT_MSG: {
4535 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004536 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004537 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004538 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004539 }
4540 }
4541 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004542 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004543 synchronized (mService) {
4544 if (mLaunchingActivity.isHeld()) {
4545 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4546 if (VALIDATE_WAKE_LOCK_CALLER
4547 && Binder.getCallingUid() != Process.myUid()) {
4548 throw new IllegalStateException("Calling must be system uid");
4549 }
4550 mLaunchingActivity.release();
4551 }
4552 }
4553 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004554 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004555 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004556 } break;
4557 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004558 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004559 } break;
4560 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004561 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004562 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004563 case LAUNCH_TASK_BEHIND_COMPLETE: {
4564 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004565 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004566 if (r != null) {
4567 handleLaunchTaskBehindCompleteLocked(r);
4568 }
4569 }
4570 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004571
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004572 }
4573 }
4574 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004575
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004576 ActivityStack findStackBehind(ActivityStack stack) {
4577 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004578 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004579 if (display == null) {
4580 return null;
4581 }
4582 final ArrayList<ActivityStack> stacks = display.mStacks;
4583 for (int i = stacks.size() - 1; i >= 0; i--) {
4584 if (stacks.get(i) == stack && i > 0) {
4585 return stacks.get(i - 1);
4586 }
4587 }
4588 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4589 + " in=" + stacks);
4590 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004591
Jorim Jaggic69bd222016-03-15 14:38:37 +01004592 /**
4593 * Puts a task into resizing mode during the next app transition.
4594 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004595 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004596 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004597 private void setResizingDuringAnimation(TaskRecord task) {
4598 mResizingTasksDuringAnimation.add(task.taskId);
4599 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004600 }
4601
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004602 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4603 final TaskRecord task;
4604 final int callingUid;
4605 final String callingPackage;
4606 final Intent intent;
4607 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004608 int activityType = ACTIVITY_TYPE_UNDEFINED;
4609 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004610 final ActivityOptions activityOptions = (bOptions != null)
4611 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004612 if (activityOptions != null) {
4613 activityType = activityOptions.getLaunchActivityType();
4614 windowingMode = activityOptions.getLaunchWindowingMode();
4615 }
4616 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004617 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004618 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004619 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004620
Matthew Ng606dd802017-06-05 14:06:32 -07004621 mWindowManager.deferSurfaceLayout();
4622 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004623 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004624 mWindowManager.setDockedStackCreateState(
4625 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004626
Matthew Ng606dd802017-06-05 14:06:32 -07004627 // Defer updating the stack in which recents is until the app transition is done, to
4628 // not run into issues where we still need to draw the task in recents but the
4629 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004630 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004631 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004632 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004633
Matthew Ng606dd802017-06-05 14:06:32 -07004634 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004635 activityOptions);
Matthew Ng606dd802017-06-05 14:06:32 -07004636 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004637 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004638 mWindowManager.executeAppTransition();
4639 throw new IllegalArgumentException(
4640 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4641 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004642
Matthew Ng606dd802017-06-05 14:06:32 -07004643 // Since we don't have an actual source record here, we assume that the currently
4644 // focused activity was the source.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004645 final ActivityStack stack = getLaunchStack(null, activityOptions, task, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004646
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004647 if (stack != null && task.getStack() != stack) {
4648 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004649 "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004650 }
4651
4652 // If the user must confirm credentials (e.g. when first launching a work app and the
4653 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4654 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4655 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004656 final ActivityRecord targetActivity = task.getTopActivity();
4657
4658 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4659 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004660 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004661 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004662 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004663 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004664
4665 // If we are launching the task in the docked stack, put it into resizing mode so
4666 // the window renders full-screen with the background filling the void. Also only
4667 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004668 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004669 setResizingDuringAnimation(task);
4670 }
4671
4672 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004673 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004674 return ActivityManager.START_TASK_TO_FRONT;
4675 }
4676 callingUid = task.mCallingUid;
4677 callingPackage = task.mCallingPackage;
4678 intent = task.intent;
4679 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4680 userId = task.userId;
4681 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004682 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004683 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004684 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004685 }
Matthew Ng606dd802017-06-05 14:06:32 -07004686 return result;
4687 } finally {
4688 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004689 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004690 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004691
4692 /**
4693 * @return a list of activities which are the top ones in each visible stack. The first
4694 * entry will be the focused activity.
4695 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004696 List<IBinder> getTopVisibleActivities() {
4697 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4698 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004699 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004700 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4701 // Traverse all stacks on a display.
4702 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4703 final ActivityStack stack = display.mStacks.get(j);
4704 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004705 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004706 final ActivityRecord top = stack.topActivity();
4707 if (top != null) {
4708 if (stack == mFocusedStack) {
4709 topActivityTokens.add(0, top.appToken);
4710 } else {
4711 topActivityTokens.add(top.appToken);
4712 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004713 }
4714 }
4715 }
4716 }
4717 return topActivityTokens;
4718 }
Bryce Lee4a194382017-04-04 14:32:48 -07004719
4720 /**
4721 * Internal container to store a match qualifier alongside a WaitResult.
4722 */
4723 static class WaitInfo {
4724 private final ComponentName mTargetComponent;
4725 private final WaitResult mResult;
4726
4727 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4728 this.mTargetComponent = targetComponent;
4729 this.mResult = result;
4730 }
4731
Wale Ogunwale3270f172017-04-26 07:29:42 -07004732 public boolean matches(ComponentName targetComponent) {
4733 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004734 }
4735
4736 public WaitResult getResult() {
4737 return mResult;
4738 }
4739
4740 public ComponentName getComponent() {
4741 return mTargetComponent;
4742 }
4743
4744 public void dump(PrintWriter pw, String prefix) {
4745 pw.println(prefix + "WaitInfo:");
4746 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4747 pw.println(prefix + " mResult=");
4748 mResult.dump(pw, prefix);
4749 }
4750 }
David Stevens9440dc82017-03-16 19:00:20 -07004751
4752 private final class SleepTokenImpl extends SleepToken {
4753 private final String mTag;
4754 private final long mAcquireTime;
4755 private final int mDisplayId;
4756
4757 public SleepTokenImpl(String tag, int displayId) {
4758 mTag = tag;
4759 mDisplayId = displayId;
4760 mAcquireTime = SystemClock.uptimeMillis();
4761 }
4762
4763 @Override
4764 public void release() {
4765 synchronized (mService) {
4766 removeSleepTokenLocked(this);
4767 }
4768 }
4769
4770 @Override
4771 public String toString() {
4772 return "{\"" + mTag + "\", display " + mDisplayId
4773 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4774 }
4775 }
4776
Craig Mautner27084302013-03-25 08:05:25 -07004777}