blob: 736a766c7e2ae93a9b0514873b9bbff730b2a01a [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
Wale Ogunwaled046a012015-12-24 13:05:59 -0800634 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800635 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
636 if (!focusCandidate.isFocusable()) {
637 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800638 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800639 }
640
641 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800642 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800643 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800644
Wale Ogunwaled046a012015-12-24 13:05:59 -0800645 EventLogTags.writeAmFocusedStack(
646 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
647 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
648 }
649
650 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800651 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800652 if (r != null && r.idle) {
653 checkFinishBootingLocked();
654 }
655 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 }
657
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700658 void moveHomeStackToFront(String reason) {
659 mHomeStack.moveToFront(reason);
660 }
661
Matthew Ng330757d2017-02-28 14:19:17 -0800662 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700663 final ActivityStack recentsStack = getDefaultDisplay().getStack(
664 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800665 if (recentsStack != null) {
666 recentsStack.moveToFront(reason);
667 }
668 }
669
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700670 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800671 boolean moveHomeStackTaskToTop(String reason) {
672 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700673
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700674 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700675 if (top == null) {
676 return false;
677 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700678 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700680 }
681
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800682 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700683 if (!mService.mBooting && !mService.mBooted) {
684 // Not ready yet!
685 return false;
686 }
687
Craig Mautner84984fa2014-06-19 11:19:20 -0700688 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700689 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700690 }
691
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800692 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700693 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800694 final String myReason = reason + " resumeHomeStackTask";
695
Mark Lua56ea122015-10-08 13:31:01 +0800696 // Only resume home activity if isn't finishing.
697 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700698 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800699 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700700 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800701 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700702 }
703
Craig Mautner8d341ef2013-03-26 09:03:27 -0700704 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700705 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
706 }
707
708 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700709 return anyTaskForIdLocked(id, matchMode, null, !ON_TOP);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700710 }
711
712 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700713 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700714 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800715 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700716 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700717 * @param onTop If the stack for the task should be the topmost on the display.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700718 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700719 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700720 @Nullable ActivityOptions aOptions, boolean onTop) {
721 // If options are set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700722 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
723 throw new IllegalArgumentException("Should not specify activity options for non-restore"
724 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800725 }
726
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700729 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
730 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
731 final ActivityStack stack = display.getChildAt(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700732 final TaskRecord task = stack.taskForIdLocked(id);
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700733 if (task == null) {
734 continue;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800735 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700736 if (aOptions != null) {
737 // Resolve the stack the task should be placed in now based on options
738 // and reparent if needed.
739 final ActivityStack launchStack = getLaunchStack(null, aOptions, task, onTop);
740 if (launchStack != null && stack != launchStack) {
741 final int reparentMode = onTop
742 ? REPARENT_MOVE_STACK_TO_FRONT : REPARENT_LEAVE_STACK_IN_PLACE;
743 task.reparent(launchStack, onTop, reparentMode, ANIMATE, DEFER_RESUME,
744 "anyTaskForIdLocked");
745 }
746 }
747 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700748 }
749 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800750
Winson Chung7900bee2017-03-10 08:59:25 -0800751 // If we are matching stack tasks only, return now
752 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800753 return null;
754 }
755
Winson Chung7900bee2017-03-10 08:59:25 -0800756 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
757 // the task from recents
758 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Winson Chung1dbc8112017-09-28 18:05:31 -0700759 final TaskRecord task = mRecentTasks.getTask(id);
Bryce Lee92b7b652017-04-03 08:33:11 -0700760
761 if (task == null) {
762 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800763 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
764 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700765
766 return null;
767 }
768
769 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700770 return task;
771 }
772
Winson Chung7900bee2017-03-10 08:59:25 -0800773 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700774 if (!restoreRecentTaskLocked(task, aOptions, onTop)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700775 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
776 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800777 return null;
778 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700779 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800780 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700781 }
782
Craig Mautner6170f732013-04-02 13:05:23 -0700783 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800784 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800785 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700786 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
787 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = display.getChildAt(stackNdx);
789 final ActivityRecord r = stack.isInStackLocked(token);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 if (r != null) {
791 return r;
792 }
Craig Mautner6170f732013-04-02 13:05:23 -0700793 }
794 }
795 return null;
796 }
797
Tony Mak853304c2016-04-18 15:17:41 +0100798 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000799 * Detects whether we should show a lock screen in front of this task for a locked user.
800 * <p>
801 * We'll do this if either of the following holds:
802 * <ul>
803 * <li>The top activity explicitly belongs to {@param userId}.</li>
804 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
805 * </ul>
806 *
807 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100808 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000809 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
810 // To handle the case that work app is in the task but just is not the top one.
811 final ActivityRecord activityRecord = task.getTopActivity();
812 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
813
814 return (activityRecord != null && activityRecord.userId == userId)
815 || (resultTo != null && resultTo.userId == userId);
816 }
817
818 /**
819 * Find all visible task stacks containing {@param userId} and intercept them with an activity
820 * to block out the contents and possibly start a credential-confirming intent.
821 *
822 * @param userId user handle for the locked managed profile.
823 */
824 void lockAllProfileTasks(@UserIdInt int userId) {
825 mWindowManager.deferSurfaceLayout();
826 try {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
828 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
829 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
830 final ActivityStack stack = display.getChildAt(stackNdx);
831 final List<TaskRecord> tasks = stack.getAllTasks();
832 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
833 final TaskRecord task = tasks.get(taskNdx);
Robin Lee3fef1f22016-12-20 14:50:13 +0000834
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700835 // Check the task for a top activity belonging to userId, or returning a
836 // result to an activity belonging to userId. Example case: a document
837 // picker for personal files, opened by a work app, should still get locked.
838 if (taskTopActivityIsUser(task, userId)) {
839 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
840 task.taskId, userId);
841 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000842 }
Tony Mak853304c2016-04-18 15:17:41 +0100843 }
844 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000845 } finally {
846 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100847 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000848 }
849
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800850 void setNextTaskIdForUserLocked(int taskId, int userId) {
851 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
852 if (taskId > currentTaskId) {
853 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700854 }
855 }
856
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700857 static int nextTaskIdForUser(int taskId, int userId) {
858 int nextTaskId = taskId + 1;
859 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
860 // Wrap around as there will be smaller task ids that are available now.
861 nextTaskId -= MAX_TASK_IDS_PER_USER;
862 }
863 return nextTaskId;
864 }
865
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800866 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800867 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
868 // for a userId u, a taskId can only be in the range
869 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
870 // 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 -0700871 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Winson Chung1dbc8112017-09-28 18:05:31 -0700872 while (mRecentTasks.containsTaskId(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700873 || anyTaskForIdLocked(
874 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700875 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800876 if (candidateTaskId == currentTaskId) {
877 // Something wrong!
878 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
879 throw new IllegalStateException("Cannot get an available task id."
880 + " Reached limit of " + MAX_TASK_IDS_PER_USER
881 + " running tasks per user.");
882 }
883 }
884 mCurTaskIdForUser.put(userId, candidateTaskId);
885 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700886 }
887
Chong Zhang6cda19c2016-06-14 19:07:56 -0700888 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800889 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700890 if (stack == null) {
891 return null;
892 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700893 ActivityRecord resumedActivity = stack.mResumedActivity;
894 if (resumedActivity == null || resumedActivity.app == null) {
895 resumedActivity = stack.mPausingActivity;
896 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700897 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700898 }
899 }
900 return resumedActivity;
901 }
902
Dianne Hackbornff072722014-09-24 10:56:28 -0700903 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700904 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800905 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800906 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700907 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
908 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
909 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700910 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800911 continue;
912 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200913 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
914 final ActivityRecord top = stack.topRunningActivityLocked();
915 final int size = mTmpActivityList.size();
916 for (int i = 0; i < size; i++) {
917 final ActivityRecord activity = mTmpActivityList.get(i);
918 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
919 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200921 if (realStartActivityLocked(activity, app,
922 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800923 didSomething = true;
924 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700925 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800926 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200927 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800928 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700929 }
Craig Mautner20e72272013-04-01 13:45:53 -0700930 }
Craig Mautner20e72272013-04-01 13:45:53 -0700931 }
932 }
933 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700934 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700935 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700936 }
Craig Mautner20e72272013-04-01 13:45:53 -0700937 return didSomething;
938 }
939
940 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700942 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
943 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
944 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700945 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800946 continue;
947 }
948 final ActivityRecord resumedActivity = stack.mResumedActivity;
949 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700950 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800951 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800952 return false;
953 }
Craig Mautner20e72272013-04-01 13:45:53 -0700954 }
955 }
Wei Wang65c7a152016-06-02 18:51:22 -0700956 // Send launch end powerhint when idle
957 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700958 return true;
959 }
960
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800962 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700963 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
964 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
965 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700966 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800967 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700968 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800969 return false;
970 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 }
972 }
973 }
974 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700975 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
977 mLastFocusedStack + " to=" + mFocusedStack);
978 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700979 return true;
980 }
981
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700982 private boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800983 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800984 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700985 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
986 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
987 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800988 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700990 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800991 return false;
992 }
993 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800994 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700995 }
996 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800997 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700998 }
999
Craig Mautner2acc3892013-09-23 10:28:14 -07001000 /**
1001 * Pause all activities in either all of the stacks or just the back stacks.
1002 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001003 * @param resuming The resuming activity.
1004 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1005 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001006 * @return true if any activity was paused as a result of this call.
1007 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001008 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001009 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001010 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001011 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1012 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1013 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001014 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001015 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001016 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001017 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1018 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001019 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001020 }
1021 }
1022 return someActivityPaused;
1023 }
1024
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001026 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001027 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001028 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1029 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1030 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001031 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001032 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001033 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001034 Slog.d(TAG_STATES,
1035 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001036 pausing = false;
1037 } else {
1038 return false;
1039 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001040 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001041 }
1042 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001043 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 }
1045
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001046 void cancelInitializingActivities() {
1047 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001048 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1049 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1050 final ActivityStack stack = display.getChildAt(stackNdx);
1051 stack.cancelInitializingActivities();
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001052 }
1053 }
1054 }
1055
Bryce Lee4a194382017-04-04 14:32:48 -07001056 void waitActivityVisible(ComponentName name, WaitResult result) {
1057 final WaitInfo waitInfo = new WaitInfo(name, result);
1058 mWaitingForActivityVisible.add(waitInfo);
1059 }
1060
1061 void cleanupActivity(ActivityRecord r) {
1062 // Make sure this record is no longer in the pending finishes list.
1063 // This could happen, for example, if we are trimming activities
1064 // down to the max limit while they are still waiting to finish.
1065 mFinishingActivities.remove(r);
1066 mActivitiesWaitingForVisibleActivity.remove(r);
1067
1068 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001069 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001070 mWaitingForActivityVisible.remove(i);
1071 }
1072 }
1073 }
1074
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001075 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001077 }
1078
1079 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1080 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001081 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1082 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001083 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001084 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001085 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001086 result.timeout = false;
1087 result.who = w.getComponent();
1088 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1089 result.thisTime = result.totalTime;
1090 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001092 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001093 if (changed) {
1094 mService.notifyAll();
1095 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 }
1097
Chong Zhang5022da32016-06-21 16:31:37 -07001098 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1099 boolean changed = false;
1100 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1101 WaitResult w = mWaitingActivityLaunched.remove(i);
1102 if (w.who == null) {
1103 changed = true;
1104 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1105 // the starting activity ends up moving another activity to front, and it should
1106 // wait for this new activity to become visible instead.
1107 // Do not modify other fields.
1108 w.result = START_TASK_TO_FRONT;
1109 }
1110 }
1111 if (changed) {
1112 mService.notifyAll();
1113 }
1114 }
1115
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001116 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1117 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001118 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001120 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001121 if (w.who == null) {
1122 changed = true;
1123 w.timeout = timeout;
1124 if (r != null) {
1125 w.who = new ComponentName(r.info.packageName, r.info.name);
1126 }
1127 w.thisTime = thisTime;
1128 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001129 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001130 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001131 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001132 if (changed) {
1133 mService.notifyAll();
1134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001135 }
1136
Craig Mautner29219d92013-04-16 20:19:12 -07001137 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001138 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001139 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001140 if (r != null) {
1141 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001142 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001143
Andrii Kulian7318d632016-07-20 18:59:28 -07001144 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001145 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1146
1147 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1148 final int displayId = mTmpOrderedDisplayIds.get(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001149 final ActivityDisplay display = mActivityDisplays.get(displayId);
1150 for (int j = display.getChildCount() - 1; j >= 0; --j) {
1151 final ActivityStack stack = display.getChildAt(j);
1152 if (stack != focusedStack && stack.isTopStackOnDisplay() && stack.isFocusable()) {
Andrii Kulian7d95df42017-02-15 10:11:48 -08001153 r = stack.topRunningActivityLocked();
1154 if (r != null) {
1155 return r;
1156 }
Craig Mautner29219d92013-04-16 20:19:12 -07001157 }
1158 }
1159 }
1160 return null;
1161 }
1162
Dianne Hackborn09233282014-04-30 11:33:59 -07001163 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001164 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001165 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001166 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001167 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001168 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1169 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1170 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001171 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001172 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001173 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001174 }
1175 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001176
1177 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1178 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1179 while (maxNum > 0) {
1180 long mostRecentActiveTime = Long.MIN_VALUE;
1181 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001182 final int numTaskLists = runningTaskLists.size();
1183 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1184 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001185 if (!stackTaskList.isEmpty()) {
1186 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1187 if (lastActiveTime > mostRecentActiveTime) {
1188 mostRecentActiveTime = lastActiveTime;
1189 selectedStackList = stackTaskList;
1190 }
1191 }
1192 }
1193 if (selectedStackList != null) {
1194 list.add(selectedStackList.remove(0));
1195 --maxNum;
1196 } else {
1197 break;
1198 }
1199 }
Craig Mautner20e72272013-04-01 13:45:53 -07001200 }
1201
Todd Kennedy7440f172015-12-09 14:31:22 -08001202 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1203 ProfilerInfo profilerInfo) {
1204 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001205 if (aInfo != null) {
1206 // Store the found target back into the intent, because now that
1207 // we have it we never want to do this again. For example, if the
1208 // user navigates back to this point in the history, we should
1209 // always restart the exact same activity.
1210 intent.setComponent(new ComponentName(
1211 aInfo.applicationInfo.packageName, aInfo.name));
1212
1213 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001214 if (!aInfo.processName.equals("system")) {
1215 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001216 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001217 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001218
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001219 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1220 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1221 }
1222
Man Caocfa78b22015-06-11 20:14:34 -07001223 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1224 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1225 }
1226
1227 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001228 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001229 }
1230 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001231 final String intentLaunchToken = intent.getLaunchToken();
1232 if (aInfo.launchToken == null && intentLaunchToken != null) {
1233 aInfo.launchToken = intentLaunchToken;
1234 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001235 }
1236 return aInfo;
1237 }
1238
Todd Kennedy7440f172015-12-09 14:31:22 -08001239 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001240 return resolveIntent(intent, resolvedType, userId, 0);
1241 }
1242
1243 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001244 synchronized (mService) {
1245 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001246 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001247 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001248 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001249 }
1250
1251 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1252 ProfilerInfo profilerInfo, int userId) {
1253 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1254 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1255 }
1256
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001257 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1258 boolean andResume, boolean checkConfig) throws RemoteException {
1259
1260 if (!allPausedActivitiesComplete()) {
1261 // While there are activities pausing we skipping starting any new activities until
1262 // pauses are complete. NOTE: that we also do this for activities that are starting in
1263 // the paused state because they will first be resumed then paused on the client side.
1264 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1265 "realStartActivityLocked: Skipping start of r=" + r
1266 + " some activities pausing...");
1267 return false;
1268 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001269
Bryce Leeaf691c02017-03-20 14:20:22 -07001270 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001271 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001272
1273 beginDeferResume();
1274
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 try {
chaviw59b98852017-06-13 12:05:44 -07001276 r.startFreezingScreenLocked(app, 0);
1277
1278 // schedule launch ticks to collect information about slow apps.
1279 r.startLaunchTickingLocked();
1280
1281 r.app = app;
1282
Jorim Jaggi838c2452017-08-28 15:44:43 +02001283 if (mKeyguardController.isKeyguardLocked()) {
1284 r.notifyUnknownVisibilityLaunched();
1285 }
1286
chaviw59b98852017-06-13 12:05:44 -07001287 // Have the window manager re-evaluate the orientation of the screen based on the new
1288 // activity order. Note that as a result of this, it can call back into the activity
1289 // manager with a new orientation. We don't care about that, because the activity is
1290 // not currently running so we are just restarting it anyway.
1291 if (checkConfig) {
1292 final int displayId = r.getDisplayId();
1293 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1294 getDisplayOverrideConfiguration(displayId),
1295 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1296 // Deferring resume here because we're going to launch new activity shortly.
1297 // We don't want to perform a redundant launch of the same record while ensuring
1298 // configurations and trying to resume top activity of focused stack.
1299 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1300 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 }
chaviw59b98852017-06-13 12:05:44 -07001302
1303 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1304 true /* isTop */)) {
1305 // We only set the visibility to true if the activity is allowed to be visible
1306 // based on
1307 // keyguard state. This avoids setting this into motion in window manager that is
1308 // later cancelled due to later calls to ensure visible activities that set
1309 // visibility back to false.
1310 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001311 }
chaviw59b98852017-06-13 12:05:44 -07001312
chaviw59b98852017-06-13 12:05:44 -07001313 final int applicationInfoUid =
1314 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1315 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1316 Slog.wtf(TAG,
1317 "User ID for activity changing for " + r
1318 + " appInfo.uid=" + r.appInfo.uid
1319 + " info.ai.uid=" + applicationInfoUid
1320 + " old=" + r.app + " new=" + app);
1321 }
1322
1323 app.waitingToKill = null;
1324 r.launchCount++;
1325 r.lastLaunchTime = SystemClock.uptimeMillis();
1326
1327 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1328
1329 int idx = app.activities.indexOf(r);
1330 if (idx < 0) {
1331 app.activities.add(r);
1332 }
1333 mService.updateLruProcessLocked(app, true, null);
1334 mService.updateOomAdjLocked();
1335
1336 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1337 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001338 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001339 }
1340
1341 try {
1342 if (app.thread == null) {
1343 throw new RemoteException();
1344 }
1345 List<ResultInfo> results = null;
1346 List<ReferrerIntent> newIntents = null;
1347 if (andResume) {
1348 // We don't need to deliver new intents and/or set results if activity is going
1349 // to pause immediately after launch.
1350 results = r.results;
1351 newIntents = r.newIntents;
1352 }
1353 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1354 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1355 + " newIntents=" + newIntents + " andResume=" + andResume);
1356 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1357 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001358 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001359 // Home process is the root process of the task.
1360 mService.mHomeProcess = task.mActivities.get(0).app;
1361 }
1362 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1363 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1364 r.sleeping = false;
1365 r.forceNewConfig = false;
1366 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1367 mService.showAskCompatModeDialogLocked(r);
1368 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1369 ProfilerInfo profilerInfo = null;
1370 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1371 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1372 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001373 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1374 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1375 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001376 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001377 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001378 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001379 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001380 }
1381 }
chaviw59b98852017-06-13 12:05:44 -07001382
Andreas Gampe2b073a02017-06-22 17:28:57 -07001383 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 }
1386 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001387
chaviw59b98852017-06-13 12:05:44 -07001388 app.hasShownUi = true;
1389 app.pendingUiClean = true;
1390 app.forceProcessStateUpTo(mService.mTopProcessState);
1391 // Because we could be starting an Activity in the system process this may not go
1392 // across a Binder interface which would create a new Configuration. Consequently
1393 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001394
chaviw59b98852017-06-13 12:05:44 -07001395 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1396 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1397 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001398
chaviw59b98852017-06-13 12:05:44 -07001399 logIfTransactionTooLarge(r.intent, r.icicle);
1400 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1401 System.identityHashCode(r), r.info,
1402 // TODO: Have this take the merged configuration instead of separate global
1403 // and override configs.
1404 mergedConfiguration.getGlobalConfiguration(),
1405 mergedConfiguration.getOverrideConfiguration(), r.compat,
1406 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1407 r.persistentState, results, newIntents, !andResume,
1408 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001409
chaviw59b98852017-06-13 12:05:44 -07001410 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1411 // This may be a heavy-weight process! Note that the package
1412 // manager will ensure that only activity can run in the main
1413 // process of the .apk, which is the only thing that will be
1414 // considered heavy-weight.
1415 if (app.processName.equals(app.info.packageName)) {
1416 if (mService.mHeavyWeightProcess != null
1417 && mService.mHeavyWeightProcess != app) {
1418 Slog.w(TAG, "Starting new heavy weight process " + app
1419 + " when already running "
1420 + mService.mHeavyWeightProcess);
1421 }
1422 mService.mHeavyWeightProcess = app;
1423 Message msg = mService.mHandler.obtainMessage(
1424 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1425 msg.obj = r;
1426 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001427 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001428 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001429
chaviw59b98852017-06-13 12:05:44 -07001430 } catch (RemoteException e) {
1431 if (r.launchFailed) {
1432 // This is the second time we failed -- finish activity
1433 // and give up.
1434 Slog.e(TAG, "Second failure launching "
1435 + r.intent.getComponent().flattenToShortString()
1436 + ", giving up", e);
1437 mService.appDiedLocked(app);
1438 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1439 "2nd-crash", false);
1440 return false;
1441 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001442
chaviw59b98852017-06-13 12:05:44 -07001443 // This is the first time we failed -- restart process and
1444 // retry.
1445 r.launchFailed = true;
1446 app.activities.remove(r);
1447 throw e;
1448 }
1449 } finally {
1450 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001451 }
1452
1453 r.launchFailed = false;
1454 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001455 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001456 }
1457
chaviw59b98852017-06-13 12:05:44 -07001458 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001459 // As part of the process of launching, ActivityThread also performs
1460 // a resume.
1461 stack.minimalResumeActivityLocked(r);
1462 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001463 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001464 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001465 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001466 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001467 "Moving to PAUSED: " + r + " (starting in paused state)");
1468 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001469 }
1470
1471 // Launch the new version setup screen if needed. We do this -after-
1472 // launching the initial activity (that is, home), so that it can have
1473 // a chance to initialize itself while in the background, making the
1474 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001475 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001476 mService.startSetupActivityLocked();
1477 }
1478
Dianne Hackborn465fa392014-09-14 14:21:18 -07001479 // Update any services we are bound to that might care about whether
1480 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001481 if (r.app != null) {
1482 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1483 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001484
Craig Mautner2420ead2013-04-01 17:13:20 -07001485 return true;
1486 }
1487
Sudheer Shankafab200f2017-05-17 20:41:53 -07001488 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1489 int extrasSize = 0;
1490 if (intent != null) {
1491 final Bundle extras = intent.getExtras();
1492 if (extras != null) {
1493 extrasSize = extras.getSize();
1494 }
1495 }
1496 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1497 if (extrasSize + icicleSize > 200000) {
1498 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1499 + ", icicle size: " + icicleSize);
1500 }
1501 }
1502
Craig Mautnere79d42682013-04-01 19:01:53 -07001503 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001504 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001505 // Is this activity's application already running?
1506 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001507 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001508
Andrii Kulian02b7a832016-10-06 23:11:56 -07001509 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001510
1511 if (app != null && app.thread != null) {
1512 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001513 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1514 || !"android".equals(r.info.packageName)) {
1515 // Don't add this if it is a platform component that is marked
1516 // to run in multiple processes, because this is actually
1517 // part of the framework so doesn't make sense to track as a
1518 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001519 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1520 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001521 }
George Mount2c92c972014-03-20 09:38:23 -07001522 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001523 return;
1524 } catch (RemoteException e) {
1525 Slog.w(TAG, "Exception when starting activity "
1526 + r.intent.getComponent().flattenToShortString(), e);
1527 }
1528
1529 // If a dead object exception was thrown -- fall through to
1530 // restart the application.
1531 }
1532
1533 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001534 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001535 }
1536
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001537 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001538 String resultWho, int requestCode, int callingPid, int callingUid,
1539 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001540 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001541 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1542 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001543 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001544 return true;
1545 }
1546 final int componentRestriction = getComponentRestrictionForCallingPackage(
1547 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1548 final int actionRestriction = getActionRestrictionForCallingPackage(
1549 intent.getAction(), callingPackage, callingPid, callingUid);
1550 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1551 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1552 if (resultRecord != null) {
1553 resultStack.sendActivityResultLocked(-1,
1554 resultRecord, resultWho, requestCode,
1555 Activity.RESULT_CANCELED, null);
1556 }
1557 final String msg;
1558 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1559 msg = "Permission Denial: starting " + intent.toString()
1560 + " from " + callerApp + " (pid=" + callingPid
1561 + ", uid=" + callingUid + ")" + " with revoked permission "
1562 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1563 } else if (!aInfo.exported) {
1564 msg = "Permission Denial: starting " + intent.toString()
1565 + " from " + callerApp + " (pid=" + callingPid
1566 + ", uid=" + callingUid + ")"
1567 + " not exported from uid " + aInfo.applicationInfo.uid;
1568 } else {
1569 msg = "Permission Denial: starting " + intent.toString()
1570 + " from " + callerApp + " (pid=" + callingPid
1571 + ", uid=" + callingUid + ")"
1572 + " requires " + aInfo.permission;
1573 }
1574 Slog.w(TAG, msg);
1575 throw new SecurityException(msg);
1576 }
1577
1578 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1579 final String message = "Appop Denial: starting " + intent.toString()
1580 + " from " + callerApp + " (pid=" + callingPid
1581 + ", uid=" + callingUid + ")"
1582 + " requires " + AppOpsManager.permissionToOp(
1583 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1584 Slog.w(TAG, message);
1585 return false;
1586 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1587 final String message = "Appop Denial: starting " + intent.toString()
1588 + " from " + callerApp + " (pid=" + callingPid
1589 + ", uid=" + callingUid + ")"
1590 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1591 Slog.w(TAG, message);
1592 return false;
1593 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001594 if (options != null) {
1595 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1596 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1597 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001598 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001599 final String msg = "Permission Denial: starting " + intent.toString()
1600 + " from " + callerApp + " (pid=" + callingPid
1601 + ", uid=" + callingUid + ") with launchTaskId="
1602 + options.getLaunchTaskId();
1603 Slog.w(TAG, msg);
1604 throw new SecurityException(msg);
1605 }
1606 }
1607 // Check if someone tries to launch an activity on a private display with a different
1608 // owner.
1609 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001610 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1611 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001612 final String msg = "Permission Denial: starting " + intent.toString()
1613 + " from " + callerApp + " (pid=" + callingPid
1614 + ", uid=" + callingUid + ") with launchDisplayId="
1615 + launchDisplayId;
1616 Slog.w(TAG, msg);
1617 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001618 }
Charles He2bf28322017-10-12 22:24:49 +01001619 // Check if someone tries to launch an unwhitelisted activity into LockTask mode.
1620 final boolean lockTaskMode = options.getLockTaskMode();
1621 if (lockTaskMode && !mService.mLockTaskController.isPackageWhitelisted(
1622 UserHandle.getUserId(callingUid), aInfo.packageName)) {
1623 final String msg = "Permission Denial: starting " + intent.toString()
1624 + " from " + callerApp + " (pid=" + callingPid
1625 + ", uid=" + callingUid + ") with lockTaskMode=true";
1626 Slog.w(TAG, msg);
1627 throw new SecurityException(msg);
1628 }
Jorim Jaggi2adba072016-03-03 13:43:39 +01001629 }
1630
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001631 return true;
1632 }
1633
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001634 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001635 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1636 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001637 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1638 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1639
Andrii Kulian02689a72017-07-06 14:28:59 -07001640 if (callingPid == -1 && callingUid == -1) {
1641 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1642 return true;
1643 }
1644
Andrii Kulian62e6f252017-05-30 22:46:53 -07001645 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001646 if (activityDisplay == null) {
1647 Slog.w(TAG, "Launch on display check: display not found");
1648 return false;
1649 }
1650
Andrii Kulian02689a72017-07-06 14:28:59 -07001651 // Check if the caller has enough privileges to embed activities and launch to private
1652 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001653 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001654 callingUid);
1655 if (startAnyPerm == PERMISSION_GRANTED) {
1656 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1657 + " allow launch any on display");
1658 return true;
1659 }
1660
Andrii Kulian8f070292017-09-12 22:56:49 -07001661 // Check if caller is already present on display
1662 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1663
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001664 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1665 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1666 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001667 // Limit launching on virtual displays, because their contents can be read from Surface
1668 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001669 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1670 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1671 + " disallow launch on virtual display for not-embedded activity.");
1672 return false;
1673 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001674 // Check if the caller is allowed to embed activities from other apps.
1675 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001676 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001677 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1678 + " disallow activity embedding without permission.");
1679 return false;
1680 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001681 }
1682
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001683 if (!activityDisplay.isPrivate()) {
1684 // Anyone can launch on a public display.
1685 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1686 + " allow launch on public display");
1687 return true;
1688 }
1689
1690 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001691 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001692 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1693 + " allow launch for owner of the display");
1694 return true;
1695 }
1696
Andrii Kulian8f070292017-09-12 22:56:49 -07001697 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001698 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1699 + " allow launch for caller present on the display");
1700 return true;
1701 }
1702
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001703 Slog.w(TAG, "Launch on display check: denied");
1704 return false;
1705 }
1706
1707 /** Update lists of UIDs that are present on displays and have access to them. */
1708 void updateUIDsPresentOnDisplay() {
1709 mDisplayAccessUIDs.clear();
1710 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1711 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001712 // Only bother calculating the whitelist for private displays
1713 if (activityDisplay.isPrivate()) {
1714 mDisplayAccessUIDs.append(
1715 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1716 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001717 }
1718 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1719 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1720 }
1721
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001722 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001723 final long identity = Binder.clearCallingIdentity();
1724 try {
1725 return UserManager.get(mService.mContext).getUserInfo(userId);
1726 } finally {
1727 Binder.restoreCallingIdentity(identity);
1728 }
1729 }
1730
Svet Ganov99b60432015-06-27 13:15:22 -07001731 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001732 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001733 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1734 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001735 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001736 return ACTIVITY_RESTRICTION_PERMISSION;
1737 }
1738
Christopher Tateff7add02015-08-17 10:23:22 -07001739 if (activityInfo.permission == null) {
1740 return ACTIVITY_RESTRICTION_NONE;
1741 }
1742
Svet Ganov99b60432015-06-27 13:15:22 -07001743 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1744 if (opCode == AppOpsManager.OP_NONE) {
1745 return ACTIVITY_RESTRICTION_NONE;
1746 }
1747
1748 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1749 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001750 if (!ignoreTargetSecurity) {
1751 return ACTIVITY_RESTRICTION_APPOP;
1752 }
Svet Ganov99b60432015-06-27 13:15:22 -07001753 }
1754
1755 return ACTIVITY_RESTRICTION_NONE;
1756 }
1757
Svetoslav7008b512015-06-24 18:47:07 -07001758 private int getActionRestrictionForCallingPackage(String action,
1759 String callingPackage, int callingPid, int callingUid) {
1760 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
1764 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1765 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001766 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001767 }
1768
1769 final PackageInfo packageInfo;
1770 try {
1771 packageInfo = mService.mContext.getPackageManager()
1772 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1773 } catch (PackageManager.NameNotFoundException e) {
1774 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001775 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001776 }
1777
1778 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001779 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001780 }
1781
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001782 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001783 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001784 }
1785
1786 final int opCode = AppOpsManager.permissionToOpCode(permission);
1787 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001788 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001789 }
1790
1791 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1792 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001793 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001794 }
1795
Svet Ganov99b60432015-06-27 13:15:22 -07001796 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001797 }
1798
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001799 void setLaunchSource(int uid) {
1800 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1801 }
1802
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001803 void acquireLaunchWakelock() {
1804 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1805 throw new IllegalStateException("Calling must be system uid");
1806 }
1807 mLaunchingActivity.acquire();
1808 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1809 // To be safe, don't allow the wake lock to be held for too long.
1810 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1811 }
1812 }
1813
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001814 /**
1815 * Called when the frontmost task is idle.
1816 * @return the state of mService.mBooting before this was called.
1817 */
1818 private boolean checkFinishBootingLocked() {
1819 final boolean booting = mService.mBooting;
1820 boolean enableScreen = false;
1821 mService.mBooting = false;
1822 if (!mService.mBooted) {
1823 mService.mBooted = true;
1824 enableScreen = true;
1825 }
1826 if (booting || enableScreen) {
1827 mService.postFinishBooting(booting, enableScreen);
1828 }
1829 return booting;
1830 }
1831
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 // Checked.
1833 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001834 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001835 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001836
Craig Mautnerf3333272013-04-22 10:55:53 -07001837 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001838 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001839 int NS = 0;
1840 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001841 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001842 boolean activityRemoved = false;
1843
Wale Ogunwale7d701172015-03-11 15:36:30 -07001844 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001845 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001846 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1847 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001848 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1849 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001850 if (fromTimeout) {
1851 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001852 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001853
1854 // This is a hack to semi-deal with a race condition
1855 // in the client where it can be constructed with a
1856 // newer configuration from when we asked it to launch.
1857 // We'll update with whatever configuration it now says
1858 // it used to launch.
1859 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001860 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001861 }
1862
1863 // We are now idle. If someone is waiting for a thumbnail from
1864 // us, we can now deliver.
1865 r.idle = true;
1866
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001867 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001868 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001869 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001870 }
1871 }
1872
1873 if (allResumedActivitiesIdle()) {
1874 if (r != null) {
1875 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001876 }
1877
1878 if (mLaunchingActivity.isHeld()) {
1879 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1880 if (VALIDATE_WAKE_LOCK_CALLER &&
1881 Binder.getCallingUid() != Process.myUid()) {
1882 throw new IllegalStateException("Calling must be system uid");
1883 }
1884 mLaunchingActivity.release();
1885 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001886 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 }
1888
1889 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001890 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1891 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001892 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001893 if ((NF = mFinishingActivities.size()) > 0) {
1894 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 mFinishingActivities.clear();
1896 }
1897
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001899 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001900 mStartingUsers.clear();
1901 }
1902
Craig Mautnerf3333272013-04-22 10:55:53 -07001903 // Stop any activities that are scheduled to do so but have been
1904 // waiting for the next one to start.
1905 for (int i = 0; i < NS; i++) {
1906 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001907 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001908 if (stack != null) {
1909 if (r.finishing) {
1910 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1911 } else {
1912 stack.stopActivityLocked(r);
1913 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001914 }
1915 }
1916
1917 // Finish any activities that are scheduled to do so but have been
1918 // waiting for the next one to start.
1919 for (int i = 0; i < NF; i++) {
1920 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001921 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001922 if (stack != null) {
1923 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1924 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001925 }
1926
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001927 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001928 // Complete user switch
1929 if (startingUsers != null) {
1930 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001931 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001932 }
1933 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001934 }
1935
1936 mService.trimApplications();
1937 //dump();
1938 //mWindowManager.dump();
1939
Craig Mautnerf3333272013-04-22 10:55:53 -07001940 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001941 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001942 }
1943
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001944 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001945 }
1946
Craig Mautner8e569572013-10-11 17:36:59 -07001947 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001948 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001950 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1951 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1952 final ActivityStack stack = display.getChildAt(stackNdx);
1953 hasVisibleActivities |= stack.handleAppDiedLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001954 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001955 }
Craig Mautner19091252013-10-05 00:03:53 -07001956 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001957 }
1958
1959 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001960 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001961 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1962 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1963 final ActivityStack stack = display.getChildAt(stackNdx);
1964 stack.closeSystemDialogsLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001965 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001966 }
1967 }
1968
Craig Mautner93529a42013-10-04 15:03:13 -07001969 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001970 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001971 }
1972
Craig Mautner8d341ef2013-03-26 09:03:27 -07001973 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001974 * Update the last used stack id for non-current user (current user's last
1975 * used stack is the focused stack)
1976 */
1977 void updateUserStackLocked(int userId, ActivityStack stack) {
1978 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001979 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001980 }
1981 }
1982
1983 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001984 * @return true if some activity was finished (or would have finished if doit were true).
1985 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001986 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1987 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001988 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001989 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001990 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
1991 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1992 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001993 if (stack.finishDisabledPackageActivitiesLocked(
1994 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001995 didSomething = true;
1996 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001997 }
1998 }
1999 return didSomething;
2000 }
2001
Dianne Hackborna413dc02013-07-12 12:02:55 -07002002 void updatePreviousProcessLocked(ActivityRecord r) {
2003 // Now that this process has stopped, we may want to consider
2004 // it to be the previous app to try to keep around in case
2005 // the user wants to return to it.
2006
2007 // First, found out what is currently the foreground app, so that
2008 // we don't blow away the previous app if this activity is being
2009 // hosted by the process that is actually still the foreground.
2010 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002011 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002012 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2013 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2014 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002015 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002016 if (stack.mResumedActivity != null) {
2017 fgApp = stack.mResumedActivity.app;
2018 } else if (stack.mPausingActivity != null) {
2019 fgApp = stack.mPausingActivity.app;
2020 }
2021 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002022 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002023 }
2024 }
2025
2026 // Now set this one as the previous process, only if that really
2027 // makes sense to.
2028 if (r.app != null && fgApp != null && r.app != fgApp
2029 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002030 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002031 mService.mPreviousProcess = r.app;
2032 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2033 }
2034 }
2035
Wale Ogunwaled046a012015-12-24 13:05:59 -08002036 boolean resumeFocusedStackTopActivityLocked() {
2037 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002038 }
2039
Wale Ogunwaled046a012015-12-24 13:05:59 -08002040 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002041 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002042
2043 if (!readyToResume()) {
2044 return false;
2045 }
2046
Wale Ogunwaled046a012015-12-24 13:05:59 -08002047 if (targetStack != null && isFocusedStack(targetStack)) {
2048 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002049 }
chaviw59b98852017-06-13 12:05:44 -07002050
Wale Ogunwale06579d62016-04-30 15:29:06 -07002051 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2052 if (r == null || r.state != RESUMED) {
2053 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002054 } else if (r.state == RESUMED) {
2055 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2056 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002057 }
chaviw59b98852017-06-13 12:05:44 -07002058
Wale Ogunwaled046a012015-12-24 13:05:59 -08002059 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002060 }
2061
Todd Kennedy39bfee52016-02-24 10:28:21 -08002062 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2063 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002064 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2065 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2066 final ActivityStack stack = display.getChildAt(stackNdx);
2067 stack.updateActivityApplicationInfoLocked(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08002068 }
2069 }
2070 }
2071
Adrian Roos20d7df32016-01-12 18:59:43 +01002072 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2073 TaskRecord finishedTask = null;
2074 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002075 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002076 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2077 final int numStacks = display.getChildCount();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002078 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002079 final ActivityStack stack = display.getChildAt(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002080 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2081 if (stack == focusedStack || finishedTask == null) {
2082 finishedTask = t;
2083 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002084 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002085 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002086 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002087 }
2088
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002089 void finishVoiceTask(IVoiceInteractionSession session) {
2090 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002091 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
2092 final int numStacks = display.getChildCount();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002093 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002094 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002095 stack.finishVoiceTask(session);
2096 }
2097 }
2098 }
2099
Andrii Kulianc27916642016-04-12 17:59:27 -07002100 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2101 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002102 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2103 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002104 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002105 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2106 // Caller wants the home activity moved with it. To accomplish this,
2107 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002108 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002109 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002110 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002111 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002112 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2113 + task + " to front. Stack is null");
2114 return;
2115 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002116
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002117 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2118 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2119 task.updateOverrideConfiguration(bounds);
2120
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002121 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002122
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002123 if (stack != currentStack) {
2124 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2125 "findTaskToMoveToFrontLocked");
2126 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002127 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2128 // still need moveTaskToFrontLocked() below for any transition settings.
2129 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002130 if (stack.resizeStackWithLaunchBounds()) {
2131 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002132 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2133 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002134 } else {
2135 // WM resizeTask must be done after the task is moved to the correct stack,
2136 // because Task's setBounds() also updates dim layer's bounds, but that has
2137 // dependency on the stack.
2138 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002139 }
2140 }
2141
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002142 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002143 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002144 r == null ? null : r.appTimeTracker, reason);
2145
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002146 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002147 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002148
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002149 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002150 currentStack, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002151 }
2152
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002153 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002154 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002155 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002156 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002157 return false;
2158 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002159 return (mService.mSupportsPictureInPicture
2160 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002161 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002162 }
2163
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002164 LaunchingTaskPositioner getLaunchingTaskPositioner() {
2165 return mTaskPositioner;
2166 }
2167
Winson Chung55893332017-02-17 17:13:10 -08002168 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002169 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2170 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2171 if (stack != null) {
2172 return stack;
2173 }
2174 }
2175 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002176 }
2177
Wale Ogunwale68278562017-09-23 17:13:55 -07002178 /** @see ActivityDisplay#getStack(int, int) */
2179 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2180 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2181 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2182 if (stack != null) {
2183 return stack;
2184 }
2185 }
2186 return null;
2187 }
2188
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002189 /**
2190 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2191 * @param windowingMode The windowing mode we are checking support for.
2192 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2193 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2194 * @param supportsFreeform If we should consider support for freeform multi-window.
2195 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2196 * @param activityType The activity type under consideration.
2197 * @return true if the windowing mode is supported.
2198 */
2199 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2200 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2201 int activityType) {
2202
2203 if (windowingMode == WINDOWING_MODE_UNDEFINED
2204 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2205 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002206 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002207 if (!supportsMultiWindow) {
2208 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002209 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002210
2211 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2212 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2213 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2214 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002215 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002216
2217 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2218 return false;
2219 }
2220
2221 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2222 return false;
2223 }
2224 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002225 }
2226
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002227 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002228 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002229
2230 // First preference if the windowing mode in the activity options if set.
2231 int windowingMode = (options != null)
2232 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2233
2234 // If windowing mode is unset, then next preference is the candidate task, then the
2235 // activity record.
2236 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2237 if (task != null) {
2238 windowingMode = task.getWindowingMode();
2239 }
2240 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2241 windowingMode = r.getWindowingMode();
2242 }
2243 }
2244
2245 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002246 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2247 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2248 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2249 boolean supportsPip = mService.mSupportsPictureInPicture;
2250 if (supportsMultiWindow) {
2251 if (task != null) {
2252 supportsMultiWindow = task.isResizeable();
2253 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2254 // TODO: Do we need to check for freeform and Pip support here?
2255 } else if (r != null) {
2256 supportsMultiWindow = r.isResizeable();
2257 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2258 supportsFreeform = r.supportsFreeform();
2259 supportsPip = r.supportsPictureInPicture();
2260 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002261 }
2262
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002263 if (windowingMode != WINDOWING_MODE_UNDEFINED
2264 && isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002265 supportsFreeform, supportsPip, activityType)) {
2266 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002267 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002268 // Return root/systems windowing mode
2269 return getWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002270 }
2271
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002272 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002273 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002274 // Preference is given to the activity type for the activity then the task since the type
2275 // once set shouldn't change.
2276 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2277 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2278 activityType = task.getActivityType();
2279 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002280 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2281 return activityType;
2282 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002283 if (options != null) {
2284 activityType = options.getLaunchActivityType();
2285 }
2286 return activityType != ACTIVITY_TYPE_UNDEFINED ? activityType : ACTIVITY_TYPE_STANDARD;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002287 }
2288
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002289 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2290 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2291 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002292 }
2293
2294 /**
2295 * Returns the right stack to use for launching factoring in all the input parameters.
2296 *
2297 * @param r The activity we are trying to launch. Can be null.
2298 * @param options The activity options used to the launch. Can be null.
2299 * @param candidateTask The possible task the activity might be launched in. Can be null.
2300 *
2301 * @return The stack to use for the launch or INVALID_STACK_ID.
2302 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002303 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2304 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2305 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002306 int taskId = INVALID_TASK_ID;
2307 int displayId = INVALID_DISPLAY;
2308 //Rect bounds = null;
2309
2310 // We give preference to the launch preference in activity options.
2311 if (options != null) {
2312 taskId = options.getLaunchTaskId();
2313 displayId = options.getLaunchDisplayId();
2314 // TODO: Need to work this into the equation...
2315 //bounds = options.getLaunchBounds();
2316 }
2317
2318 // First preference for stack goes to the task Id set in the activity options. Use the stack
2319 // associated with that if possible.
2320 if (taskId != INVALID_TASK_ID) {
2321 // Temporarily set the task id to invalid in case in re-entry.
2322 options.setLaunchTaskId(INVALID_TASK_ID);
2323 final TaskRecord task = anyTaskForIdLocked(taskId,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002324 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002325 options.setLaunchTaskId(taskId);
2326 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002327 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002328 }
2329 }
2330
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002331 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002332 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2333 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002334
2335 // Next preference for stack goes to the display Id set in the activity options or the
2336 // candidate display.
2337 if (displayId == INVALID_DISPLAY) {
2338 displayId = candidateDisplayId;
2339 }
2340 if (displayId != INVALID_DISPLAY) {
2341 if (r != null) {
2342 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002343 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002344 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002345 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002346 }
2347 }
2348 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2349 if (display != null) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002350 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2351 if (stack != null) {
2352 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002353 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002354 }
2355 }
2356
2357 // Give preference to the stack and display of the input task and activity if they match the
2358 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002359 stack = null;
2360 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002361 if (candidateTask != null) {
2362 stack = candidateTask.getStack();
2363 }
2364 if (stack == null && r != null) {
2365 stack = r.getStack();
2366 }
2367 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002368 if (stack.isCompatible(windowingMode, activityType)) {
2369 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002370 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002371 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002372 }
2373
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002374 if (display == null
2375 // TODO: Can be removed once we figure-out how non-standard types should launch
2376 // outside the default display.
2377 || (activityType != ACTIVITY_TYPE_STANDARD
2378 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2379 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002380 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002381
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002382 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002383 }
2384
Andrii Kulian16802aa2016-11-02 12:21:33 -07002385 /**
2386 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2387 * If there is no such stack, new dynamic stack can be created.
2388 * @param displayId Target display.
2389 * @param r Activity that should be launched there.
2390 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2391 */
2392 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002393 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002394 if (activityDisplay == null) {
2395 throw new IllegalArgumentException(
2396 "Display with displayId=" + displayId + " not found.");
2397 }
2398
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002399 if (!r.canBeLaunchedOnDisplay(displayId)) {
2400 return null;
2401 }
2402
Andrii Kulian16802aa2016-11-02 12:21:33 -07002403 // Return the topmost valid stack on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002404 for (int i = activityDisplay.getChildCount() - 1; i >= 0; --i) {
2405 final ActivityStack stack = activityDisplay.getChildAt(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002406 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002407 return stack;
2408 }
2409 }
2410
2411 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002412 if (displayId != DEFAULT_DISPLAY) {
2413 return activityDisplay.createStack(
2414 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002415 }
2416
2417 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2418 return null;
2419 }
2420
Wale Ogunwale68278562017-09-23 17:13:55 -07002421 // TODO: Can probably be consolidated into getLaunchStack()...
2422 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2423 switch (stack.getActivityType()) {
2424 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2425 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2426 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002427 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002428 switch (stack.getWindowingMode()) {
2429 case WINDOWING_MODE_FULLSCREEN: return true;
2430 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2431 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2432 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2433 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2434 }
2435
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002436 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002437 return r.canBeLaunchedOnDisplay(displayId);
2438 }
2439 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2440 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002441 }
2442
Andrii Kulian7fc22812016-12-28 13:04:11 -08002443 /**
2444 * Get next focusable stack in the system. This will search across displays and stacks
2445 * in last-focused order for a focusable and visible stack, different from the target stack.
2446 *
2447 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2448 * searching for next candidate.
2449 * @return Next focusable {@link ActivityStack}, null if not found.
2450 */
2451 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2452 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2453
2454 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2455 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002456 // If a display is registered in WM, it must also be available in AM.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002457 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2458 for (int j = display.getChildCount() - 1; j >= 0; --j) {
2459 final ActivityStack stack = display.getChildAt(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002460 if (stack != currentFocus && stack.isFocusable()
2461 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002462 return stack;
2463 }
2464 }
2465 }
2466
2467 return null;
2468 }
2469
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002470 /**
2471 * Get next valid stack for launching provided activity in the system. This will search across
2472 * displays and stacks in last-focused order for a focusable and visible stack, except those
2473 * that are on a currently focused display.
2474 *
2475 * @param r The activity that is being launched.
2476 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2477 * searching for the next candidate.
2478 * @return Next valid {@link ActivityStack}, null if not found.
2479 */
2480 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2481 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2482 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2483 final int displayId = mTmpOrderedDisplayIds.get(i);
2484 if (displayId == currentFocus) {
2485 continue;
2486 }
2487 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2488 if (stack != null) {
2489 return stack;
2490 }
2491 }
2492 return null;
2493 }
2494
Craig Mautneree2e45a2014-06-27 12:10:03 -07002495 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002496 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002497 }
2498
2499 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002500 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2501 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2502 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002503 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002504 final ArrayList<ActivityRecord> activities = task.mActivities;
2505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2506 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002507 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002508 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002509 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002510 }
2511 }
2512 }
2513 }
2514 return null;
2515 }
2516
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002517 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2518 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2519 boolean deferResume) {
2520
2521 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002522 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002523 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002524 return;
2525 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002526
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002527 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002528 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002529 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002530 // If the docked stack exists, don't resize non-floating stacks independently of the
2531 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002532 return;
2533 }
2534
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002535 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002536 mWindowManager.deferSurfaceLayout();
2537 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002538 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002539 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2540 // null bounds = fullscreen windowing mode...at least for now.
2541 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2542 } else if (splitScreenActive) {
2543 // If we are in split-screen mode and this stack support split-screen, then
2544 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2545 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2546 }
2547 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002548 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002549 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002550 stack.ensureVisibleActivitiesConfigurationLocked(
2551 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002552 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002553 } finally {
2554 mWindowManager.continueSurfaceLayout();
2555 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002556 }
2557 }
2558
Wale Ogunwale68278562017-09-23 17:13:55 -07002559 private void deferUpdateBounds(int activityType) {
2560 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002561 if (stack != null) {
2562 stack.deferUpdateBounds();
2563 }
2564 }
2565
Wale Ogunwale68278562017-09-23 17:13:55 -07002566 private void continueUpdateBounds(int activityType) {
2567 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002568 if (stack != null) {
2569 stack.continueUpdateBounds();
2570 }
2571 }
2572
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002573 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002574 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002575 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2576 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002577 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002578 if (task != null) {
2579 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002580 }
2581 }
2582 mResizingTasksDuringAnimation.clear();
2583 }
2584
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002585 /**
2586 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002587 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002588 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002589 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002590 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002591
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002592 mWindowManager.deferSurfaceLayout();
2593 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002594 final int windowingMode = fromStack.getWindowingMode();
2595 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2596 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2597
2598 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002599 // We are moving all tasks from the docked stack to the fullscreen stack,
2600 // which is dismissing the docked stack, so resize all other stacks to
2601 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002602 for (int i = toDisplay.getChildCount() - 1; i >= 0; --i) {
2603 final ActivityStack otherStack = toDisplay.getChildAt(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002604 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2605 continue;
2606 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002607 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002608 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002609 }
2610
2611 // Also disable docked stack resizing since we have manually adjusted the
2612 // size of other stacks above and we don't want to trigger a docked stack
2613 // resize when we remove task from it below and it is detached from the
2614 // display because it no longer contains any tasks.
2615 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002616 } else if (inPinnedWindowingMode && onTop) {
2617 // Log if we are expanding the PiP to fullscreen
2618 MetricsLogger.action(mService.mContext,
2619 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002620 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002621
Winson Chung5af42fc2017-03-24 17:11:33 -07002622 // If we are moving from the pinned stack, then the animation takes care of updating
2623 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002624 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002625 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002626
Wale Ogunwale388945c2017-10-04 12:13:46 -07002627 if (!tasks.isEmpty()) {
2628 final int size = tasks.size();
2629 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2630 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2631
2632 if (onTop) {
2633 final int returnToType =
2634 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
2635 for (int i = 0; i < size; i++) {
2636 final TaskRecord task = tasks.get(i);
2637 final boolean isTopTask = i == (size - 1);
2638 if (inPinnedWindowingMode) {
2639 // Update the return-to to reflect where the pinned stack task was moved
2640 // from so that we retain the stack that was previously visible if the
2641 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2642 task.setTaskToReturnTo(returnToType);
2643 }
2644 // Defer resume until all the tasks have been moved to the fullscreen stack
2645 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2646 isTopTask /* animate */, DEFER_RESUME,
2647 schedulePictureInPictureModeChange,
2648 "moveTasksToFullscreenStack - onTop");
Winson Chung0c1ca092016-11-10 17:40:34 -08002649 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07002650 } else {
2651 for (int i = 0; i < size; i++) {
2652 final TaskRecord task = tasks.get(i);
2653 // Position the tasks in the fullscreen stack in order at the bottom of the
2654 // stack. Also defer resume until all the tasks have been moved to the
2655 // fullscreen stack.
2656 task.reparent(fullscreenStack, i /* position */,
2657 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2658 schedulePictureInPictureModeChange,
2659 "moveTasksToFullscreenStack - NOT_onTop");
2660 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002661 }
2662 }
Winson Chung74666102017-02-22 17:49:24 -08002663
2664 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2665 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002666 } finally {
2667 mAllowDockedStackResize = true;
2668 mWindowManager.continueSurfaceLayout();
2669 }
2670 }
2671
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002672 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002673 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2674 }
2675
2676 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2677 mWindowManager.inSurfaceTransaction(() ->
2678 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002679 }
2680
Jorim Jaggidc249c42015-12-15 14:57:31 -08002681 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002682 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2683 boolean preserveWindows) {
2684 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2685 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2686 false /* deferResume */);
2687 }
2688
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002689 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002690 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2691 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002692
2693 if (!mAllowDockedStackResize) {
2694 // Docked stack resize currently disabled.
2695 return;
2696 }
2697
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002698 final ActivityStack stack = getDefaultDisplay().getSplitScreenPrimaryStack();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002699 if (stack == null) {
2700 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2701 return;
2702 }
2703
2704 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2705 mWindowManager.deferSurfaceLayout();
2706 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002707 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2708 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002709 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002710 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002711
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002712 // TODO: Checking for isAttached might not be needed as if the user passes in null
2713 // dockedBounds then they want the docked stack to be dismissed.
2714 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2715 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002716 // In this case we make all other static stacks fullscreen and move all
2717 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002718 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002719
2720 // stack shouldn't contain anymore activities, so nothing to resume.
2721 r = null;
2722 } else {
2723 // Docked stacks occupy a dedicated region on screen so the size of all other
2724 // static stacks need to be adjusted so they don't overlap with the docked stack.
2725 // We get the bounds to use from window manager which has been adjusted for any
2726 // screen controls and is also the same for all stacks.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002727 final ActivityDisplay display = getDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002728 final Rect otherTaskRect = new Rect();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002729 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2730 final ActivityStack current = display.getChildAt(i);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002731 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002732 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002733 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002734 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002735 continue;
2736 }
2737 // Need to set windowing mode here before we try to get the dock bounds.
2738 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2739 current.getStackDockedModeBounds(
2740 tempOtherTaskBounds /* currentTempTaskBounds */,
2741 tempRect /* outStackBounds */,
2742 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2743
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002744 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002745 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2746 tempOtherTaskInsetBounds, preserveWindows,
2747 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002748 }
2749 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002750 if (!deferResume) {
2751 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2752 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002753 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002754 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002755 mWindowManager.continueSurfaceLayout();
2756 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2757 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002758 }
2759
Robert Carr0d00c2e2016-02-29 17:45:02 -08002760 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002761 // TODO(multi-display): Pinned stack display should be passed in.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002762 final PinnedActivityStack stack = getDefaultDisplay().getPinnedStack();
Robert Carr0d00c2e2016-02-29 17:45:02 -08002763 if (stack == null) {
2764 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2765 return;
2766 }
Winson Chung19953ca2017-04-11 11:19:23 -07002767
2768 // It is possible for the bounds animation from the WM to call this but be delayed by
2769 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2770 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2771 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2772 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002773 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002774 return;
2775 }
2776
Robert Carr0d00c2e2016-02-29 17:45:02 -08002777 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2778 mWindowManager.deferSurfaceLayout();
2779 try {
2780 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002781 Rect insetBounds = null;
2782 if (tempPinnedTaskBounds != null) {
2783 // We always use 0,0 as the position for the inset rect because
2784 // if we are getting insets at all in the pinned stack it must mean
2785 // we are headed for fullscreen.
2786 insetBounds = tempRect;
2787 insetBounds.top = 0;
2788 insetBounds.left = 0;
2789 insetBounds.right = tempPinnedTaskBounds.width();
2790 insetBounds.bottom = tempPinnedTaskBounds.height();
2791 }
2792 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002793 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002794 } finally {
2795 mWindowManager.continueSurfaceLayout();
2796 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2797 }
2798 }
2799
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002800 private void removeStackInSurfaceTransaction(ActivityStack stack) {
Winson Chung010927a2016-12-15 16:12:35 -08002801 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002802 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002803 /**
2804 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2805 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2806 * that the client receives onStop() before it is reparented. We do this by detaching
2807 * the stack from the display so that it will be considered invisible when
2808 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2809 * invisible as well and added to the stopping list. After which we process the
2810 * stopping list by handling the idle.
2811 */
2812 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2813 pinnedStack.mForceHidden = true;
2814 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2815 pinnedStack.mForceHidden = false;
2816 activityIdleInternalLocked(null, false /* fromTimeout */,
2817 true /* processPausingActivites */, null /* configuration */);
2818
2819 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002820 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002821 } else {
2822 for (int i = tasks.size() - 1; i >= 0; i--) {
2823 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2824 REMOVE_FROM_RECENTS);
2825 }
2826 }
2827 }
2828
2829 /**
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002830 * Removes the stack associated with the given {@param stack}. If the {@param stack} is the
Robert Carr6914f082017-03-20 19:04:30 -07002831 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2832 * instead moved back onto the fullscreen stack.
2833 */
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07002834 void removeStack(ActivityStack stack) {
2835 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stack));
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002836 }
2837
Wale Ogunwale68278562017-09-23 17:13:55 -07002838 /**
2839 * Removes stacks in the input windowing modes from the system if they are of activity type
2840 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2841 */
2842 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002843 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002844 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2845 }
2846 }
2847
2848 void removeStacksWithActivityTypes(int... activityTypes) {
2849 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2850 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002851 }
Robert Carr6914f082017-03-20 19:04:30 -07002852 }
2853
2854 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002855 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2856 */
2857 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2858 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2859 }
2860
2861 /**
Winson Chung010927a2016-12-15 16:12:35 -08002862 * Removes the task with the specified task id.
2863 *
2864 * @param taskId Identifier of the task to be removed.
2865 * @param killProcess Kill any process associated with the task if possible.
2866 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002867 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2868 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002869 * @return Returns true if the given task was found and removed.
2870 */
Winson Chung6954fc92017-03-24 16:22:12 -07002871 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2872 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002873 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002874 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002875 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002876 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2877 if (tr.isPersistable) {
2878 mService.notifyTaskPersisterLocked(null, true);
2879 }
2880 return true;
2881 }
2882 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2883 return false;
2884 }
2885
2886 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2887 if (removeFromRecents) {
Winson Chung1dbc8112017-09-28 18:05:31 -07002888 mRecentTasks.remove(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002889 }
2890 ComponentName component = tr.getBaseIntent().getComponent();
2891 if (component == null) {
2892 Slog.w(TAG, "No component for base intent of task: " + tr);
2893 return;
2894 }
2895
2896 // Find any running services associated with this app and stop if needed.
2897 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2898
2899 if (!killProcess) {
2900 return;
2901 }
2902
2903 // Determine if the process(es) for this task should be killed.
2904 final String pkg = component.getPackageName();
2905 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2906 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2907 for (int i = 0; i < pmap.size(); i++) {
2908
2909 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2910 for (int j = 0; j < uids.size(); j++) {
2911 ProcessRecord proc = uids.valueAt(j);
2912 if (proc.userId != tr.userId) {
2913 // Don't kill process for a different user.
2914 continue;
2915 }
2916 if (proc == mService.mHomeProcess) {
2917 // Don't kill the home process along with tasks from the same package.
2918 continue;
2919 }
2920 if (!proc.pkgList.containsKey(pkg)) {
2921 // Don't kill process that is not associated with this task.
2922 continue;
2923 }
2924
2925 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002926 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002927 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2928 // Don't kill process(es) that has an activity in a different task that is
2929 // also in recents.
2930 return;
2931 }
2932 }
2933
2934 if (proc.foregroundServices) {
2935 // Don't kill process(es) with foreground service.
2936 return;
2937 }
2938
2939 // Add process to kill list.
2940 procsToKill.add(proc);
2941 }
2942 }
2943
2944 // Kill the running processes.
2945 for (int i = 0; i < procsToKill.size(); i++) {
2946 ProcessRecord pr = procsToKill.get(i);
2947 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2948 && pr.curReceivers.isEmpty()) {
2949 pr.kill("remove task", true);
2950 } else {
2951 // We delay killing processes that are not in the background or running a receiver.
2952 pr.waitingToKill = "remove task";
2953 }
2954 }
2955 }
2956
Craig Mautner4a1cb222013-12-04 16:14:06 -08002957 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002958 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002959 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002960 break;
2961 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002962 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002963 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002964 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002965 }
2966
Chong Zhang5dcb2752015-08-18 13:50:26 -07002967 /**
Winson Chung1dbc8112017-09-28 18:05:31 -07002968 * Called to restore the state of the task into the stack that it's supposed to go into.
2969 *
Chong Zhang5dcb2752015-08-18 13:50:26 -07002970 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002971 * @param aOptions The activity options to use for restoration.
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002972 * @param onTop If the stack for the task should be the topmost on the display.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002973 * @return true if the task has been restored successfully.
2974 */
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002975 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions, boolean onTop) {
2976 final ActivityStack stack = getLaunchStack(null, aOptions, task, onTop);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002977 final ActivityStack currentStack = task.getStack();
2978 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002979 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002980 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002981 // Nothing else to do since it is already restored in the right stack.
2982 return true;
2983 }
2984 // Remove current stack association, so we can re-associate the task with the
2985 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002986 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002987 }
2988
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002989 stack.addTask(task, onTop, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002990 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwaleab5de372017-10-18 06:46:31 -07002991 task.createWindowContainer(onTop, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002992 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2993 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002994 final ArrayList<ActivityRecord> activities = task.mActivities;
2995 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002996 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002997 }
2998 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002999 }
3000
Winson Chung1dbc8112017-09-28 18:05:31 -07003001 @Override
3002 public void onRecentTaskAdded(TaskRecord task) {
3003 task.touchActiveTime();
3004 }
3005
3006 @Override
Winson Chungd6aa3db2017-10-05 17:18:43 -07003007 public void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed) {
3008 if (wasTrimmed) {
3009 // Task was trimmed from the recent tasks list -- remove the active task record as well
3010 // since the user won't really be able to go back to it
3011 removeTaskByIdLocked(task.taskId, false /* killProcess */,
3012 false /* removeFromRecents */);
3013 }
Winson Chung1dbc8112017-09-28 18:05:31 -07003014 task.removedFromRecents();
3015 }
3016
Wale Ogunwale040b4702015-08-06 18:10:50 -07003017 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003018 * Move stack with all its existing content to specified display.
3019 * @param stackId Id of stack to move.
3020 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003021 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003022 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003023 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003024 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003025 if (activityDisplay == null) {
3026 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3027 + displayId);
3028 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003029 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003030 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003031 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3032 + stackId);
3033 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003034
3035 final ActivityDisplay currentDisplay = stack.getDisplay();
3036 if (currentDisplay == null) {
3037 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3038 + " is not attached to any display.");
3039 }
3040
3041 if (currentDisplay.mDisplayId == displayId) {
3042 throw new IllegalArgumentException("Trying to move stack=" + stack
3043 + " to its current displayId=" + displayId);
3044 }
3045
3046 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003047 // TODO(multi-display): resize stacks properly if moved from split-screen.
3048 }
3049
3050 /**
Winson Chung74666102017-02-22 17:49:24 -08003051 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3052 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003053 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003054 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003055 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003056 final int stackId = stack.mStackId;
3057 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003058
Winson Chung74666102017-02-22 17:49:24 -08003059 // Check that we aren't reparenting to the same stack that the task is already in
3060 if (prevStack != null && prevStack.mStackId == stackId) {
3061 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3062 + " already in stackId=" + stackId);
3063 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003064 }
3065
Winson Chung74666102017-02-22 17:49:24 -08003066 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3067 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003068 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003069 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003070 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003071 }
3072
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003073 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3074 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003075 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003076 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3077 + " reparent task=" + task + " to stackId=" + stackId);
3078 }
3079
Winson Chung74666102017-02-22 17:49:24 -08003080 // Ensure that we aren't trying to move into a freeform stack without freeform
3081 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003082 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3083 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003084 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3085 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003086 }
3087
Winson Chung74666102017-02-22 17:49:24 -08003088 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3089 // used for split-screen mode and will cause things like the docked divider to show up. We
3090 // instead leave the task in its current stack or move it to the fullscreen stack if it
3091 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003092 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003093 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3094 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003095 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3096 // for example, a docked stack is created which will lead to the stack we are moving
3097 // from being resized and and its resizeable tasks being resized.
3098 try {
3099 task.mTemporarilyUnresizable = true;
3100 stack = stack.getDisplay().createStack(
3101 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3102 } finally {
3103 task.mTemporarilyUnresizable = false;
3104 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003105 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003106 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003107 }
3108
Winson Chung08f81892017-03-02 15:40:51 -08003109 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003110 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003111 if (stack == null) {
3112 throw new IllegalArgumentException(
3113 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3114 }
3115
3116 final ActivityRecord r = stack.topRunningActivityLocked();
3117 if (r == null) {
3118 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3119 + " in stack=" + stack);
3120 return false;
3121 }
3122
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003123 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003124 Slog.w(TAG,
3125 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003126 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003127 return false;
3128 }
3129
Winson Chung3a682872017-04-10 14:38:05 -07003130 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003131 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003132 return true;
3133 }
3134
Winson Chung8bca9e42017-04-16 15:59:43 -07003135 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003136 boolean moveHomeStackToFront, String reason) {
3137
Wale Ogunwale480dca02016-02-06 13:58:29 -08003138 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003139
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003140 final ActivityDisplay display = r.getStack().getDisplay();
3141 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003142
Bryce Lee04ab3462017-04-10 15:06:33 -07003143 // This will clear the pinned stack by moving an existing task to the full screen stack,
3144 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003145 if (stack != null) {
3146 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3147 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003148
Wale Ogunwale1666e312016-12-16 11:27:18 -08003149 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003150 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003151
Wale Ogunwale480dca02016-02-06 13:58:29 -08003152 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003153 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003154 // Resize the pinned stack to match the current size of the task the activity we are
3155 // going to be moving is currently contained in. We do this to have the right starting
3156 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003157 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003158 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003159 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003160
3161 if (task.mActivities.size() == 1) {
3162 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003163 // the stack without re-parenting the activity in a different task. We don't
3164 // move the home stack forward if we are currently entering picture-in-picture
3165 // while pausing because that changes the focused stack and may prevent the new
3166 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003167 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003168 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003169 // Move the home stack forward if the task we just moved to the pinned stack
3170 // was launched from home so home should be visible behind it.
3171 moveHomeStackToFront(reason);
3172 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003173 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003174 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3175 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003176 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003177 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003178 // reveal/leave the other activities in their original task.
3179
3180 // Currently, we don't support reparenting activities across tasks in two different
3181 // stacks, so instead, just create a new task in the same stack, reparent the
3182 // activity into that task, and then reparent the whole task to the new stack. This
3183 // ensures that all the necessary work to migrate states in the old and new stacks
3184 // is also done.
3185 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003186 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003187 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3188
Winson Chung5af42fc2017-03-24 17:11:33 -07003189 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003190 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003191 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003192 }
Winson Chungc2baac02017-01-11 13:34:47 -08003193
3194 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3195 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003196 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003197 } finally {
3198 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003199 }
3200
Winson Chunge7ba6862017-05-24 12:13:33 -07003201 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3202 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3203 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003204 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3205
Winson Chunge7ba6862017-05-24 12:13:33 -07003206 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3207 true /* fromFullscreen */);
3208
3209 // Update the visibility of all activities after the they have been reparented to the new
3210 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3211 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3212 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003213 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003214 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003215
Wale Ogunwale89be5762017-10-04 13:27:49 -07003216 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003217 }
3218
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003219 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003220 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3221 if (r == null || !r.isFocusable()) {
3222 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3223 "moveActivityStackToFront: unfocusable r=" + r);
3224 return false;
3225 }
3226
Bryce Leeaf691c02017-03-20 14:20:22 -07003227 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003228 final ActivityStack stack = r.getStack();
3229 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003230 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3231 + r + " task=" + task);
3232 return false;
3233 }
3234
Chong Zhang6cda19c2016-06-14 19:07:56 -07003235 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3236 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3237 "moveActivityStackToFront: already on top, r=" + r);
3238 return false;
3239 }
3240
3241 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3242 "moveActivityStackToFront: r=" + r);
3243
3244 stack.moveToFront(reason, task);
3245 return true;
3246 }
3247
David Stevensc6b91c62017-02-08 14:23:58 -08003248 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003249 mTmpFindTaskResult.r = null;
3250 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003251 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003252 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003254 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3255 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3256 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003257 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003258 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3259 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003260 continue;
3261 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003262 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003263 // It is possible to have tasks in multiple stacks with the same root affinity, so
3264 // we should keep looking after finding an affinity match to see if there is a
3265 // better match in another stack. Also, task affinity isn't a good enough reason
3266 // to target a display which isn't the source of the intent, so skip any affinity
3267 // matches not on the specified display.
3268 if (mTmpFindTaskResult.r != null) {
3269 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3270 return mTmpFindTaskResult.r;
3271 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003272 // Note: since the traversing through the stacks is top down, the floating
3273 // tasks should always have lower priority than any affinity-matching tasks
3274 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003275 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003276 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3277 Slog.d(TAG_TASKS, "Skipping match on different display "
3278 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003279 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003280 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003281 }
3282 }
Winson Chung5b895b72017-05-01 13:46:25 -07003283
David Stevensc6b91c62017-02-08 14:23:58 -08003284 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3285 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003286 }
3287
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003288 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003289 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003290 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003291 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3292 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3293 final ActivityStack stack = display.getChildAt(stackNdx);
3294 final ActivityRecord ar = stack.findActivityLocked(
3295 intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003296 if (ar != null) {
3297 return ar;
3298 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003299 }
3300 }
3301 return null;
3302 }
3303
David Stevens9440dc82017-03-16 19:00:20 -07003304 boolean hasAwakeDisplay() {
3305 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3306 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3307 if (!display.shouldSleep()) {
3308 return true;
3309 }
3310 }
3311 return false;
3312 }
3313
Craig Mautner8d341ef2013-03-26 09:03:27 -07003314 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003315 scheduleSleepTimeout();
3316 if (!mGoingToSleep.isHeld()) {
3317 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003318 if (mLaunchingActivity.isHeld()) {
3319 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3320 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003321 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003322 mLaunchingActivity.release();
3323 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003324 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003325 }
David Stevens9440dc82017-03-16 19:00:20 -07003326
3327 applySleepTokensLocked(false /* applyToStacks */);
3328
3329 checkReadyForSleepLocked(true /* allowDelay */);
3330 }
3331
3332 void prepareForShutdownLocked() {
3333 for (int i = 0; i < mActivityDisplays.size(); i++) {
3334 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3335 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336 }
3337
3338 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003339 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003340
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003341 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003342 final long endTime = System.currentTimeMillis() + timeout;
3343 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003344 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003345 long timeRemaining = endTime - System.currentTimeMillis();
3346 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003347 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003348 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003349 } catch (InterruptedException e) {
3350 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003351 } else {
3352 Slog.w(TAG, "Activity manager shutdown timed out");
3353 timedout = true;
3354 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003355 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003356 } else {
3357 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003358 }
3359 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003360
3361 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003362 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003363
Craig Mautner8d341ef2013-03-26 09:03:27 -07003364 return timedout;
3365 }
3366
3367 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003368 removeSleepTimeouts();
3369 if (mGoingToSleep.isHeld()) {
3370 mGoingToSleep.release();
3371 }
David Stevens9440dc82017-03-16 19:00:20 -07003372 }
3373
3374 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003375 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003376 // Set the sleeping state of the display.
3377 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3378 final boolean displayShouldSleep = display.shouldSleep();
3379 if (displayShouldSleep == display.isSleeping()) {
3380 continue;
3381 }
3382 display.setIsSleeping(displayShouldSleep);
3383
3384 if (!applyToStacks) {
3385 continue;
3386 }
3387
3388 // Set the sleeping state of the stacks on the display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003389 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3390 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003391 if (displayShouldSleep) {
3392 stack.goToSleepIfPossible(false /* shuttingDown */);
3393 } else {
3394 stack.awakeFromSleepingLocked();
3395 if (isFocusedStack(stack)) {
3396 resumeFocusedStackTopActivityLocked();
3397 }
3398 }
3399 }
3400
3401 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3402 continue;
3403 }
3404 // The display is awake now, so clean up the going to sleep list.
3405 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3406 final ActivityRecord r = it.next();
3407 if (r.getDisplayId() == display.mDisplayId) {
3408 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 }
Craig Mautner5314a402013-09-26 12:40:16 -07003410 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003411 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003412 }
3413
3414 void activitySleptLocked(ActivityRecord r) {
3415 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003416 final ActivityStack s = r.getStack();
3417 if (s != null) {
3418 s.checkReadyForSleep();
3419 } else {
3420 checkReadyForSleepLocked(true);
3421 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003422 }
3423
David Stevens9440dc82017-03-16 19:00:20 -07003424 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003425 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426 // Do not care.
3427 return;
3428 }
3429
David Stevens18abd0e2017-08-17 14:55:47 -07003430 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3431 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003432 }
3433
Wei Wang65c7a152016-06-02 18:51:22 -07003434 // Send launch end powerhint before going sleep
3435 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3436
Craig Mautner0eea92c2013-05-16 13:35:39 -07003437 removeSleepTimeouts();
3438
3439 if (mGoingToSleep.isHeld()) {
3440 mGoingToSleep.release();
3441 }
3442 if (mService.mShuttingDown) {
3443 mService.notifyAll();
3444 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003445 }
3446
David Stevens18abd0e2017-08-17 14:55:47 -07003447 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3448 // successfully put to sleep.
3449 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3450 boolean allSleep = true;
3451 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003452 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3453 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3454 final ActivityStack stack = display.getChildAt(stackNdx);
David Stevens18abd0e2017-08-17 14:55:47 -07003455 if (allowDelay) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003456 allSleep &= stack.goToSleepIfPossible(shuttingDown);
David Stevens18abd0e2017-08-17 14:55:47 -07003457 } else {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003458 stack.goToSleep();
David Stevens18abd0e2017-08-17 14:55:47 -07003459 }
3460 }
3461 }
3462 return allSleep;
3463 }
3464
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003465 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003466 // A resumed activity cannot be stopping. remove from list
3467 mStoppingActivities.remove(r);
3468
Andrii Kulian02b7a832016-10-06 23:11:56 -07003469 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003470 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003471 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003472 }
3473 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003474 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003475 mWindowManager.executeAppTransition();
3476 return true;
3477 }
3478 return false;
3479 }
3480
Craig Mautner8d341ef2013-03-26 09:03:27 -07003481 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003482 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003483 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3484 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3485 final ActivityStack stack = display.getChildAt(stackNdx);
3486 stack.handleAppCrashLocked(app);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003487 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003488 }
3489 }
3490
Craig Mautnerbb742462014-07-07 15:28:55 -07003491 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003492 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003493 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003494 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003495
3496 r.mLaunchTaskBehind = false;
Winson Chung1dbc8112017-09-28 18:05:31 -07003497 mRecentTasks.add(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003498 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003499 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003500
3501 // When launching tasks behind, update the last active time of the top task after the new
3502 // task has been shown briefly
3503 final ActivityRecord top = stack.topActivity();
3504 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003505 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003506 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003507 }
3508
3509 void scheduleLaunchTaskBehindComplete(IBinder token) {
3510 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3511 }
3512
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003513 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3514 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003515 mKeyguardController.beginActivityVisibilityUpdate();
3516 try {
3517 // First the front stacks. In case any are not fullscreen and are in front of home.
3518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003519 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3520 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3521 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggife762342016-10-13 14:33:27 +02003522 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3523 }
Craig Mautner580ea812013-04-25 12:58:38 -07003524 }
Jorim Jaggife762342016-10-13 14:33:27 +02003525 } finally {
3526 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003527 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003528 }
3529
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003530 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3531 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003532 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3533 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3534 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003535 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3536 }
3537 }
3538 }
3539
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003540 void invalidateTaskLayers() {
3541 mTaskLayersChanged = true;
3542 }
3543
3544 void rankTaskLayersIfNeeded() {
3545 if (!mTaskLayersChanged) {
3546 return;
3547 }
3548 mTaskLayersChanged = false;
3549 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003550 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003551 int baseLayer = 0;
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003552 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3553 final ActivityStack stack = display.getChildAt(stackNdx);
3554 baseLayer += stack.rankTaskLayers(baseLayer);
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003555 }
3556 }
3557 }
3558
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003559 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003561 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3562 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3563 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003564 stack.clearOtherAppTimeTrackers(except);
3565 }
3566 }
3567 }
3568
Craig Mautner8d341ef2013-03-26 09:03:27 -07003569 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003570 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003571 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3572 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3573 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003574 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003576 }
3577 }
3578
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003579 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3580 // Examine all activities currently running in the process.
3581 TaskRecord firstTask = null;
3582 // Tasks is non-null only if two or more tasks are found.
3583 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3585 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003586 ActivityRecord r = app.activities.get(i);
3587 // First, if we find an activity that is in the process of being destroyed,
3588 // then we just aren't going to do anything for now; we want things to settle
3589 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003590 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003592 return;
3593 }
3594 // Don't consider any activies that are currently not in a state where they
3595 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003596 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3597 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003599 continue;
3600 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003601
3602 final TaskRecord task = r.getTask();
3603 if (task != null) {
3604 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003605 + " from " + r);
3606 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003607 firstTask = task;
3608 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003609 if (tasks == null) {
3610 tasks = new ArraySet<>();
3611 tasks.add(firstTask);
3612 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003613 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003614 }
3615 }
3616 }
3617 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003618 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003619 return;
3620 }
3621 // If we have activities in multiple tasks that are in a position to be destroyed,
3622 // let's iterate through the tasks and release the oldest one.
3623 final int numDisplays = mActivityDisplays.size();
3624 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003625 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3626 final int stackCount = display.getChildCount();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003627 // Step through all stacks starting from behind, to hit the oldest things first.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003628 for (int stackNdx = 0; stackNdx < stackCount; stackNdx++) {
3629 final ActivityStack stack = display.getChildAt(stackNdx);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003630 // Try to release activities in this stack; if we manage to, we are done.
3631 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3632 return;
3633 }
3634 }
3635 }
3636 }
3637
Amith Yamasani37a40c22015-06-17 13:25:42 -07003638 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003639 final int focusStackId = mFocusedStack.getStackId();
3640 // We dismiss the docked stack whenever we switch users.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003641 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003642 if (dockedStack != null) {
3643 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3644 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003645 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3646 // also cause all tasks to be moved to the fullscreen stack at a position that is
3647 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003648 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003649
3650 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003651 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003652 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003653
Craig Mautner858d8a62013-04-23 17:08:34 -07003654 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003656 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3657 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3658 final ActivityStack stack = display.getChildAt(stackNdx);
Craig Mautnere0a38842013-12-16 16:14:02 -08003659 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003660 TaskRecord task = stack.topTask();
3661 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003662 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003663 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003664 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003665 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003666
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003667 ActivityStack stack = getStack(restoreStackId);
3668 if (stack == null) {
3669 stack = mHomeStack;
3670 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003671 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003672 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003673 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003674 } else {
3675 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003676 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003677 }
Craig Mautner93529a42013-10-04 15:03:13 -07003678 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003679 }
3680
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003681 /** Checks whether the userid is a profile of the current user. */
3682 boolean isCurrentProfileLocked(int userId) {
3683 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003684 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003685 }
3686
Bryce Leeb7c9b802017-05-02 14:20:24 -07003687 /**
3688 * Returns whether a stopping activity is present that should be stopped after visible, rather
3689 * than idle.
3690 * @return {@code true} if such activity is present. {@code false} otherwise.
3691 */
3692 boolean isStoppingNoHistoryActivity() {
3693 // Activities that are marked as nohistory should be stopped immediately after the resumed
3694 // activity has become visible.
3695 for (ActivityRecord record : mStoppingActivities) {
3696 if (record.isNoHistory()) {
3697 return true;
3698 }
3699 }
3700
3701 return false;
3702 }
3703
Winson Chung4dabf232017-01-25 13:25:22 -08003704 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3705 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003706 ArrayList<ActivityRecord> stops = null;
3707
3708 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003709 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3710 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003711 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003712 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003713 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3714 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003715 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003716 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003717 if (s.finishing) {
3718 // If this activity is finishing, it is sitting on top of
3719 // everyone else but we now know it is no longer needed...
3720 // so get rid of it. Otherwise, we need to go through the
3721 // normal flow and hide it once we determine that it is
3722 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003723 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003724 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003725 }
3726 }
David Stevens9440dc82017-03-16 19:00:20 -07003727 if (remove) {
3728 final ActivityStack stack = s.getStack();
3729 final boolean shouldSleepOrShutDown = stack != null
3730 ? stack.shouldSleepOrShutDownActivities()
3731 : mService.isSleepingOrShuttingDownLocked();
3732 if (!waitingVisible || shouldSleepOrShutDown) {
3733 if (!processPausingActivities && s.state == PAUSING) {
3734 // Defer processing pausing activities in this iteration and reschedule
3735 // a delayed idle to reprocess it again
3736 removeTimeoutsForActivityLocked(idleActivity);
3737 scheduleIdleTimeoutLocked(idleActivity);
3738 continue;
3739 }
Winson Chung4dabf232017-01-25 13:25:22 -08003740
David Stevens9440dc82017-03-16 19:00:20 -07003741 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3742 if (stops == null) {
3743 stops = new ArrayList<>();
3744 }
3745 stops.add(s);
3746 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003747 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003748 }
3749 }
3750
3751 return stops;
3752 }
3753
Craig Mautnercf910b02013-04-23 11:23:27 -07003754 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003755 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003756 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3757 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3758 final ActivityStack stack = display.getChildAt(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003759 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003760 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003761 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003762 if (r == null) Slog.e(TAG,
3763 "validateTop...: null top activity, stack=" + stack);
3764 else {
3765 final ActivityRecord pausing = stack.mPausingActivity;
3766 if (pausing != null && pausing == r) Slog.e(TAG,
3767 "validateTop...: top stack has pausing activity r=" + r
3768 + " state=" + state);
3769 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3770 "validateTop...: activity in front not resumed r=" + r
3771 + " state=" + state);
3772 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003773 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003774 final ActivityRecord resumed = stack.mResumedActivity;
3775 if (resumed != null && resumed == r) Slog.e(TAG,
3776 "validateTop...: back stack has resumed activity r=" + r
3777 + " state=" + state);
3778 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3779 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003780 }
3781 }
3782 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003783 }
3784
Craig Mautner27084302013-03-25 08:05:25 -07003785 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003786 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003787 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003788 pw.print(prefix);
3789 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003790 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003791 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3792 final ActivityDisplay display = mActivityDisplays.valueAt(i);
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003793 display.dump(pw, prefix);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003794 }
Bryce Lee4a194382017-04-04 14:32:48 -07003795 if (!mWaitingForActivityVisible.isEmpty()) {
3796 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3797 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3798 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3799 }
3800 }
3801
Jorim Jaggi8d786932016-10-26 19:08:36 -07003802 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003803 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003804 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003805
Yi Jin129fc6c2017-09-28 15:48:38 -07003806 public void writeToProto(ProtoOutputStream proto) {
Steven Timotius4346f0a2017-09-12 11:07:21 -07003807 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3808 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3809 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3810 activityDisplay.writeToProto(proto, DISPLAYS);
3811 }
3812 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3813 if (mFocusedStack != null) {
3814 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3815 ActivityRecord focusedActivity = getResumedActivityLocked();
3816 if (focusedActivity != null) {
3817 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3818 }
3819 } else {
3820 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3821 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003822 }
3823
Winson43d1f262016-06-14 16:05:55 -07003824 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003825 * Dump all connected displays' configurations.
3826 * @param prefix Prefix to apply to each line of the dump.
3827 */
3828 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3829 pw.print(prefix); pw.println("Display override configurations:");
3830 final int displayCount = mActivityDisplays.size();
3831 for (int i = 0; i < displayCount; i++) {
3832 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3833 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3834 pw.println(activityDisplay.getOverrideConfiguration());
3835 }
3836 }
3837
3838 /**
Winson43d1f262016-06-14 16:05:55 -07003839 * Dumps the activities matching the given {@param name} in the either the focused stack
3840 * or all visible stacks if {@param dumpVisibleStacks} is true.
3841 */
Winson Chung6998bc42017-02-28 17:07:05 -08003842 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3843 boolean dumpFocusedStackOnly) {
3844 if (dumpFocusedStackOnly) {
3845 return mFocusedStack.getDumpActivitiesLocked(name);
3846 } else {
Winson43d1f262016-06-14 16:05:55 -07003847 ArrayList<ActivityRecord> activities = new ArrayList<>();
3848 int numDisplays = mActivityDisplays.size();
3849 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003850 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3851 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3852 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003853 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003854 activities.addAll(stack.getDumpActivitiesLocked(name));
3855 }
3856 }
3857 }
3858 return activities;
Winson43d1f262016-06-14 16:05:55 -07003859 }
Craig Mautner20e72272013-04-01 13:45:53 -07003860 }
3861
Dianne Hackborn390517b2013-05-30 15:03:32 -07003862 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3863 boolean needSep, String prefix) {
3864 if (activity != null) {
3865 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3866 if (needSep) {
3867 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003868 }
3869 pw.print(prefix);
3870 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003871 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003872 }
3873 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003874 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003875 }
3876
Craig Mautner8d341ef2013-03-26 09:03:27 -07003877 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3878 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003879 boolean printed = false;
3880 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003881 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3882 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003883 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003884 pw.println(" (activities from top to bottom):");
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07003885 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3886 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
3887 final ActivityStack stack = display.getChildAt(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003888 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003889 pw.println(" Stack #" + stack.mStackId
Wale Ogunwale61911492017-10-11 08:50:50 -07003890 + ": type=" + activityTypeToString(stack.getActivityType())
3891 + " mode=" + windowingModeToString(stack.getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003892 pw.println(" mFullscreen=" + stack.mFullscreen);
3893 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3894 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003895
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003896 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3897 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003898
Craig Mautner4a1cb222013-12-04 16:14:06 -08003899 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3900 !dumpAll, false, dumpPackage, true,
3901 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003902
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 needSep = printed;
3904 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3905 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003906 if (pr) {
3907 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003908 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003909 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003910 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3911 " mResumedActivity: ");
3912 if (pr) {
3913 printed = true;
3914 needSep = false;
3915 }
3916 if (dumpAll) {
3917 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3918 " mLastPausedActivity: ");
3919 if (pr) {
3920 printed = true;
3921 needSep = true;
3922 }
3923 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3924 needSep, " mLastNoHistoryActivity: ");
3925 }
3926 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003927 }
3928 }
3929
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003930 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3931 false, dumpPackage, true, " Activities waiting to finish:", null);
3932 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3933 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003934 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3935 false, !dumpAll, false, dumpPackage, true,
3936 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003937 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3938 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003939
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003940 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003941 }
3942
Dianne Hackborn390517b2013-05-30 15:03:32 -07003943 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003944 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003945 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003946 String innerPrefix = null;
3947 String[] args = null;
3948 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003949 for (int i=list.size()-1; i>=0; i--) {
3950 final ActivityRecord r = list.get(i);
3951 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3952 continue;
3953 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003954 if (innerPrefix == null) {
3955 innerPrefix = prefix + " ";
3956 args = new String[0];
3957 }
3958 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003959 final boolean full = !brief && (complete || !r.isInHistory());
3960 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003961 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003962 needNL = false;
3963 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003964 if (header != null) {
3965 pw.println(header);
3966 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003967 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003968 if (lastTask != r.getTask()) {
3969 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 pw.print(prefix);
3971 pw.print(full ? "* " : " ");
3972 pw.println(lastTask);
3973 if (full) {
3974 lastTask.dump(pw, prefix + " ");
3975 } else if (complete) {
3976 // Complete + brief == give a summary. Isn't that obvious?!?
3977 if (lastTask.intent != null) {
3978 pw.print(prefix); pw.print(" ");
3979 pw.println(lastTask.intent.toInsecureStringWithClip());
3980 }
3981 }
3982 }
3983 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3984 pw.print(" #"); pw.print(i); pw.print(": ");
3985 pw.println(r);
3986 if (full) {
3987 r.dump(pw, innerPrefix);
3988 } else if (complete) {
3989 // Complete + brief == give a summary. Isn't that obvious?!?
3990 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3991 if (r.app != null) {
3992 pw.print(innerPrefix); pw.println(r.app);
3993 }
3994 }
3995 if (client && r.app != null && r.app.thread != null) {
3996 // flush anything that is already in the PrintWriter since the thread is going
3997 // to write to the file descriptor directly
3998 pw.flush();
3999 try {
4000 TransferPipe tp = new TransferPipe();
4001 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004002 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004003 // Short timeout, since blocking here can
4004 // deadlock with the application.
4005 tp.go(fd, 2000);
4006 } finally {
4007 tp.kill();
4008 }
4009 } catch (IOException e) {
4010 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4011 } catch (RemoteException e) {
4012 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4013 }
4014 needNL = true;
4015 }
4016 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004017 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004018 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004019
Craig Mautnerf3333272013-04-22 10:55:53 -07004020 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004021 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4022 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004023 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4024 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004025 }
4026
4027 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004028 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004029 }
4030
4031 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004032 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4033 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004034 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4035 }
4036
Craig Mautner05d29032013-05-03 13:40:13 -07004037 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004038 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4039 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4040 }
Craig Mautner05d29032013-05-03 13:40:13 -07004041 }
4042
Craig Mautner0eea92c2013-05-16 13:35:39 -07004043 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004044 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4045 }
4046
4047 final void scheduleSleepTimeout() {
4048 removeSleepTimeouts();
4049 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4050 }
4051
Craig Mautner4a1cb222013-12-04 16:14:06 -08004052 @Override
4053 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004054 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004055 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4056 }
4057
4058 @Override
4059 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004060 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004061 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4062 }
4063
4064 @Override
4065 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004066 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004067 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4068 }
4069
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004070 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004072 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004074 }
4075
Andrii Kulianca007a62016-11-22 16:33:28 -08004076 /** Check if display with specified id is added to the list. */
4077 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004078 return getActivityDisplayOrCreateLocked(displayId) != null;
4079 }
4080
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004081 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004082 ActivityDisplay getActivityDisplay(int displayId) {
4083 return mActivityDisplays.get(displayId);
4084 }
4085
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004086 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4087 ActivityDisplay getDefaultDisplay() {
4088 return mActivityDisplays.get(DEFAULT_DISPLAY);
4089 }
4090
Andrii Kulian62e6f252017-05-30 22:46:53 -07004091 /**
4092 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4093 * corresponding record in display manager.
4094 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004095 // TODO: Look into consolidating with getActivityDisplay()
4096 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004097 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4098 if (activityDisplay != null) {
4099 return activityDisplay;
4100 }
4101 if (mDisplayManager == null) {
4102 // The system isn't fully initialized yet.
4103 return null;
4104 }
4105 final Display display = mDisplayManager.getDisplay(displayId);
4106 if (display == null) {
4107 // The display is not registered in DisplayManager.
4108 return null;
4109 }
4110 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004111 activityDisplay = new ActivityDisplay(this, displayId);
4112 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004113 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4114 mWindowManager.onDisplayAdded(displayId);
4115 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004116 }
4117
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004118 @VisibleForTesting
4119 void attachDisplay(ActivityDisplay display) {
4120 mActivityDisplays.put(display.mDisplayId, display);
4121 }
4122
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004123 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004124 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004125 mService.mContext.getResources().getDimensionPixelSize(
4126 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004127 }
4128
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004129 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004130 if (displayId == DEFAULT_DISPLAY) {
4131 throw new IllegalArgumentException("Can't remove the primary display.");
4132 }
4133
Craig Mautner4a1cb222013-12-04 16:14:06 -08004134 synchronized (mService) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004135 final ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4136 if (activityDisplay == null) {
4137 return;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004139 final boolean destroyContentOnRemoval
4140 = activityDisplay.shouldDestroyContentOnRemove();
4141 while (activityDisplay.getChildCount() > 0) {
4142 final ActivityStack stack = activityDisplay.getChildAt(0);
4143 if (destroyContentOnRemoval) {
4144 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, false /* onTop */);
4145 stack.finishAllActivitiesLocked(true /* immediately */);
4146 } else {
4147 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4148 // a valid launch stack for all activities. This way the task history from
4149 // external display will be preserved on primary after move.
4150 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
4151 }
4152 }
4153
4154 releaseSleepTokens(activityDisplay);
4155
4156 mActivityDisplays.remove(displayId);
4157 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004158 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 }
4160
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004161 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004163 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4164 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004165 // The window policy is responsible for stopping activities on the default display
4166 if (displayId != Display.DEFAULT_DISPLAY) {
4167 int displayState = activityDisplay.mDisplay.getState();
4168 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4169 activityDisplay.mOffToken =
4170 mService.acquireSleepToken("Display-off", displayId);
4171 } else if (displayState == Display.STATE_ON
4172 && activityDisplay.mOffToken != null) {
4173 activityDisplay.mOffToken.release();
4174 activityDisplay.mOffToken = null;
4175 }
4176 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004177 // TODO: Update the bounds.
4178 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004179 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 }
4182
David Stevens9440dc82017-03-16 19:00:20 -07004183 SleepToken createSleepTokenLocked(String tag, int displayId) {
4184 ActivityDisplay display = mActivityDisplays.get(displayId);
4185 if (display == null) {
4186 throw new IllegalArgumentException("Invalid display: " + displayId);
4187 }
4188
4189 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4190 mSleepTokens.add(token);
4191 display.mAllSleepTokens.add(token);
4192 return token;
4193 }
4194
4195 private void removeSleepTokenLocked(SleepTokenImpl token) {
4196 mSleepTokens.remove(token);
4197
4198 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4199 if (display != null) {
4200 display.mAllSleepTokens.remove(token);
4201 if (display.mAllSleepTokens.isEmpty()) {
4202 mService.updateSleepIfNeededLocked();
4203 }
4204 }
4205 }
4206
4207 private void releaseSleepTokens(ActivityDisplay display) {
4208 if (display.mAllSleepTokens.isEmpty()) {
4209 return;
4210 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004211 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004212 mSleepTokens.remove(token);
4213 }
4214 display.mAllSleepTokens.clear();
4215
4216 mService.updateSleepIfNeededLocked();
4217 }
4218
Wale Ogunwale68278562017-09-23 17:13:55 -07004219 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004220 final int displayId = stack.mDisplayId;
4221 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004222 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004223 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004224 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004225 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004226 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004227 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004228 // A stack might be not attached to a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004229 info.position = display != null ? display.getIndexOf(stack) : 0;
Wale Ogunwale68278562017-09-23 17:13:55 -07004230 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004231
4232 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4233 final int numTasks = tasks.size();
4234 int[] taskIds = new int[numTasks];
4235 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004236 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004237 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004238 for (int i = 0; i < numTasks; ++i) {
4239 final TaskRecord task = tasks.get(i);
4240 taskIds[i] = task.taskId;
4241 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4242 : task.realActivity != null ? task.realActivity.flattenToString()
4243 : task.getTopActivity() != null ? task.getTopActivity().packageName
4244 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004245 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004246 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004247 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004248 }
4249 info.taskIds = taskIds;
4250 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004251 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004252 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004253
4254 final ActivityRecord top = stack.topRunningActivityLocked();
4255 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 return info;
4257 }
4258
Wale Ogunwale68278562017-09-23 17:13:55 -07004259 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004260 ActivityStack stack = getStack(stackId);
4261 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004262 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004263 }
4264 return null;
4265 }
4266
Wale Ogunwale68278562017-09-23 17:13:55 -07004267 StackInfo getStackInfo(int windowingMode, int activityType) {
4268 final ActivityStack stack = getStack(windowingMode, activityType);
4269 return (stack != null) ? getStackInfo(stack) : null;
4270 }
4271
Craig Mautner4a1cb222013-12-04 16:14:06 -08004272 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004273 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004274 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004275 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
4276 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
4277 final ActivityStack stack = display.getChildAt(stackNdx);
4278 list.add(getStackInfo(stack));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004279 }
4280 }
4281 return list;
4282 }
4283
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004284 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004285 int preferredDisplayId, ActivityStack actualStack) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004286 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004287 actualStack, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004288 }
4289
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004290 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004291 int preferredDisplayId, ActivityStack actualStack, boolean forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004292 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004293 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004294 final boolean inSplitScreenMode = actualStack != null
4295 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004296 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004297 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004298 return;
4299 }
4300
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004301 // Handle incorrect launch/move to secondary display if needed.
4302 final boolean launchOnSecondaryDisplayFailed;
4303 if (isSecondaryDisplayPreferred) {
4304 final int actualDisplayId = task.getStack().mDisplayId;
4305 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4306 // The task landed on an inappropriate display somehow, move it to the default
4307 // display.
4308 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004309 mService.setTaskWindowingMode(task.taskId,
4310 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004311 launchOnSecondaryDisplayFailed = true;
4312 } else {
4313 // The task might have landed on a display different from requested.
4314 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4315 || (preferredDisplayId != INVALID_DISPLAY
4316 && preferredDisplayId != actualDisplayId);
4317 }
4318 } else {
4319 // The task wasn't requested to be on a secondary display.
4320 launchOnSecondaryDisplayFailed = false;
4321 }
4322
Jorim Jaggicd13d332016-04-27 15:40:20 -07004323 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004324 if (launchOnSecondaryDisplayFailed
4325 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004326 if (launchOnSecondaryDisplayFailed) {
4327 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4328 // display with config different from global config.
4329 mService.mTaskChangeNotificationController
4330 .notifyActivityLaunchOnSecondaryDisplayFailed();
4331 } else {
4332 // Display a warning toast that we tried to put a non-dockable task in the docked
4333 // stack.
4334 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4335 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004336
Andrii Kulianc27916642016-04-12 17:59:27 -07004337 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4338 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004339
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004340 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004341 if (dockedStack != null) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004342 moveTasksToFullscreenStackLocked(dockedStack, actualStack == dockedStack);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004343 }
Winson Chungd3395382016-12-13 11:49:09 -08004344 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004345 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004346 final String packageName = topActivity.appInfo.packageName;
4347 final int reason = isSecondaryDisplayPreferred
4348 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4349 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004350 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004351 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004352 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004353 }
4354
Jorim Jaggife89d122015-12-22 16:28:44 +01004355 void activityRelaunchedLocked(IBinder token) {
4356 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004357 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4358 if (r != null) {
4359 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004360 r.setSleeping(true, true);
4361 }
4362 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004363 }
4364
4365 void activityRelaunchingLocked(ActivityRecord r) {
4366 mWindowManager.notifyAppRelaunching(r.appToken);
4367 }
4368
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004369 void logStackState() {
4370 mActivityMetricsLogger.logWindowState();
4371 }
4372
Winson Chung5af42fc2017-03-24 17:11:33 -07004373 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004374 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4375 // end, then ensure that we defer all in between multi-window mode changes
4376 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4377 return;
4378 }
4379
Wale Ogunwale22e25262016-02-01 10:32:02 -08004380 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4381 final ActivityRecord r = task.mActivities.get(i);
4382 if (r.app != null && r.app.thread != null) {
4383 mMultiWindowModeChangedActivities.add(r);
4384 }
4385 }
4386
4387 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4388 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4389 }
4390 }
4391
Winson Chung5af42fc2017-03-24 17:11:33 -07004392 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004393 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004394 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004395 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004396 return;
4397 }
4398
Winson Chungab76bbc2017-08-14 13:33:51 -07004399 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004400 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004401
Winson Chungab76bbc2017-08-14 13:33:51 -07004402 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4403 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4404 final ActivityRecord r = task.mActivities.get(i);
4405 if (r.app != null && r.app.thread != null) {
4406 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004407 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004408 }
4409 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004410
Winson Chungab76bbc2017-08-14 13:33:51 -07004411 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4412 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4413 }
4414 }
4415
4416 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4417 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4418 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4419 final ActivityRecord r = task.mActivities.get(i);
4420 if (r.app != null && r.app.thread != null) {
4421 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004422 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004423 }
4424 }
4425
Tony Mak853304c2016-04-18 15:17:41 +01004426 void setDockedStackMinimized(boolean minimized) {
4427 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004428 }
4429
chaviw59b98852017-06-13 12:05:44 -07004430 void wakeUp(String reason) {
4431 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4432 }
4433
4434 /**
4435 * Begin deferring resume to avoid duplicate resumes in one pass.
4436 */
4437 private void beginDeferResume() {
4438 mDeferResumeCount++;
4439 }
4440
4441 /**
4442 * End deferring resume and determine if resume can be called.
4443 */
4444 private void endDeferResume() {
4445 mDeferResumeCount--;
4446 }
4447
4448 /**
4449 * @return True if resume can be called.
4450 */
4451 private boolean readyToResume() {
4452 return mDeferResumeCount == 0;
4453 }
4454
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004455 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004456
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004457 public ActivityStackSupervisorHandler(Looper looper) {
4458 super(looper);
4459 }
4460
Winson Chung4dabf232017-01-25 13:25:22 -08004461 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004462 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004463 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4464 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004465 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004466 }
4467
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004468 @Override
4469 public void handleMessage(Message msg) {
4470 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004471 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4472 synchronized (mService) {
4473 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4474 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004475 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004476 }
4477 }
4478 } break;
4479 case REPORT_PIP_MODE_CHANGED_MSG: {
4480 synchronized (mService) {
4481 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4482 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004483 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4484 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004485 }
4486 }
4487 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004488 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004489 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4490 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004491 // We don't at this point know if the activity is fullscreen,
4492 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004493 activityIdleInternal((ActivityRecord) msg.obj,
4494 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004495 } break;
4496 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004497 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004498 activityIdleInternal((ActivityRecord) msg.obj,
4499 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004500 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004501 case RESUME_TOP_ACTIVITY_MSG: {
4502 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004503 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004504 }
4505 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004506 case SLEEP_TIMEOUT_MSG: {
4507 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004508 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004509 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004510 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004511 }
4512 }
4513 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004514 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004515 synchronized (mService) {
4516 if (mLaunchingActivity.isHeld()) {
4517 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4518 if (VALIDATE_WAKE_LOCK_CALLER
4519 && Binder.getCallingUid() != Process.myUid()) {
4520 throw new IllegalStateException("Calling must be system uid");
4521 }
4522 mLaunchingActivity.release();
4523 }
4524 }
4525 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004526 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004527 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004528 } break;
4529 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004530 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004531 } break;
4532 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004533 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004534 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004535 case LAUNCH_TASK_BEHIND_COMPLETE: {
4536 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004537 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004538 if (r != null) {
4539 handleLaunchTaskBehindCompleteLocked(r);
4540 }
4541 }
4542 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004543
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004544 }
4545 }
4546 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004547
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004548 ActivityStack findStackBehind(ActivityStack stack) {
4549 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004550 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004551 if (display == null) {
4552 return null;
4553 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004554 for (int i = display.getChildCount() - 1; i >= 0; i--) {
4555 if (display.getChildAt(i) == stack && i > 0) {
4556 return display.getChildAt(i - 1);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004557 }
4558 }
4559 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004560 + " in=" + display);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004561 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004562
Jorim Jaggic69bd222016-03-15 14:38:37 +01004563 /**
4564 * Puts a task into resizing mode during the next app transition.
4565 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004566 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004567 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004568 private void setResizingDuringAnimation(TaskRecord task) {
4569 mResizingTasksDuringAnimation.add(task.taskId);
4570 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004571 }
4572
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004573 int startActivityFromRecents(int taskId, Bundle bOptions) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004574 final TaskRecord task;
4575 final int callingUid;
4576 final String callingPackage;
4577 final Intent intent;
4578 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004579 int activityType = ACTIVITY_TYPE_UNDEFINED;
4580 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004581 final ActivityOptions activityOptions = (bOptions != null)
4582 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004583 if (activityOptions != null) {
4584 activityType = activityOptions.getLaunchActivityType();
4585 windowingMode = activityOptions.getLaunchWindowingMode();
4586 }
4587 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004588 throw new IllegalArgumentException("startActivityFromRecents: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004589 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004590 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004591
Matthew Ng606dd802017-06-05 14:06:32 -07004592 mWindowManager.deferSurfaceLayout();
4593 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004594 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004595 mWindowManager.setDockedStackCreateState(
4596 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004597
Matthew Ng606dd802017-06-05 14:06:32 -07004598 // Defer updating the stack in which recents is until the app transition is done, to
4599 // not run into issues where we still need to draw the task in recents but the
4600 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004601 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004602 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004603 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004604
Matthew Ng606dd802017-06-05 14:06:32 -07004605 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004606 activityOptions, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004607 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004608 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004609 mWindowManager.executeAppTransition();
4610 throw new IllegalArgumentException(
Wale Ogunwaleab5de372017-10-18 06:46:31 -07004611 "startActivityFromRecents: Task " + taskId + " not found.");
Matthew Ng606dd802017-06-05 14:06:32 -07004612 }
4613
4614 // If the user must confirm credentials (e.g. when first launching a work app and the
4615 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4616 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4617 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004618 final ActivityRecord targetActivity = task.getTopActivity();
4619
4620 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4621 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004622 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004623 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004624 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004625 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004626
4627 // If we are launching the task in the docked stack, put it into resizing mode so
4628 // the window renders full-screen with the background filling the void. Also only
4629 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004630 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004631 setResizingDuringAnimation(task);
4632 }
4633
4634 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004635 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004636 return ActivityManager.START_TASK_TO_FRONT;
4637 }
4638 callingUid = task.mCallingUid;
4639 callingPackage = task.mCallingPackage;
4640 intent = task.intent;
4641 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4642 userId = task.userId;
4643 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004644 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004645 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004646 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004647 }
Matthew Ng606dd802017-06-05 14:06:32 -07004648 return result;
4649 } finally {
4650 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004651 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004652 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004653
4654 /**
4655 * @return a list of activities which are the top ones in each visible stack. The first
4656 * entry will be the focused activity.
4657 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004658 List<IBinder> getTopVisibleActivities() {
4659 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4660 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004661 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004662 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4663 // Traverse all stacks on a display.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004664 for (int j = display.getChildCount() - 1; j >= 0; --j) {
4665 final ActivityStack stack = display.getChildAt(j);
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004666 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004667 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004668 final ActivityRecord top = stack.topActivity();
4669 if (top != null) {
4670 if (stack == mFocusedStack) {
4671 topActivityTokens.add(0, top.appToken);
4672 } else {
4673 topActivityTokens.add(top.appToken);
4674 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004675 }
4676 }
4677 }
4678 }
4679 return topActivityTokens;
4680 }
Bryce Lee4a194382017-04-04 14:32:48 -07004681
4682 /**
4683 * Internal container to store a match qualifier alongside a WaitResult.
4684 */
4685 static class WaitInfo {
4686 private final ComponentName mTargetComponent;
4687 private final WaitResult mResult;
4688
4689 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4690 this.mTargetComponent = targetComponent;
4691 this.mResult = result;
4692 }
4693
Wale Ogunwale3270f172017-04-26 07:29:42 -07004694 public boolean matches(ComponentName targetComponent) {
4695 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004696 }
4697
4698 public WaitResult getResult() {
4699 return mResult;
4700 }
4701
4702 public ComponentName getComponent() {
4703 return mTargetComponent;
4704 }
4705
4706 public void dump(PrintWriter pw, String prefix) {
4707 pw.println(prefix + "WaitInfo:");
4708 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4709 pw.println(prefix + " mResult=");
4710 mResult.dump(pw, prefix);
4711 }
4712 }
David Stevens9440dc82017-03-16 19:00:20 -07004713
4714 private final class SleepTokenImpl extends SleepToken {
4715 private final String mTag;
4716 private final long mAcquireTime;
4717 private final int mDisplayId;
4718
4719 public SleepTokenImpl(String tag, int displayId) {
4720 mTag = tag;
4721 mDisplayId = displayId;
4722 mAcquireTime = SystemClock.uptimeMillis();
4723 }
4724
4725 @Override
4726 public void release() {
4727 synchronized (mService) {
4728 removeSleepTokenLocked(this);
4729 }
4730 }
4731
4732 @Override
4733 public String toString() {
4734 return "{\"" + mTag + "\", display " + mDisplayId
4735 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4736 }
4737 }
4738
Craig Mautner27084302013-03-25 08:05:25 -07004739}