blob: c5cb5bbcdd735c15e21852a72e2de93ecec56f1d [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 Chung47900652017-04-06 18:44:25 -070049import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020050import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020053import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020060import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020062import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
71import static com.android.server.am.ActivityManagerService.ANIMATE;
72import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
74import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
75import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
76import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
77import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
78import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
79import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
80import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
81import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070082import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020083import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
84import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Winson Chung74666102017-02-22 17:49:24 -080085import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
86import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
87import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -070088import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
89import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
90import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
91import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
92import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Jorim Jaggife762342016-10-13 14:33:27 +020093import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080094import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020095
Svetoslav7008b512015-06-24 18:47:07 -070096import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -080097import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -070098import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070099import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100100import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700102import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800103import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700104import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700106import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700107import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700109import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700110import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700111import android.app.WaitResult;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700112import android.app.WindowConfiguration;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700113import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700114import android.content.Context;
115import android.content.Intent;
116import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700118import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.pm.PackageManager;
120import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100121import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700122import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800123import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800124import android.hardware.display.DisplayManager;
125import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800126import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800127import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100129import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700130import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700131import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700133import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700134import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700135import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700136import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700137import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700139import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700140import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100141import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700142import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700143import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700144import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700145import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700146import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700147import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800148import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700149import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700150import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800151import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800152import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700153import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700154import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800155import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800156
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700157import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800158import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800159import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700160import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700161import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800162import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700163import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700164import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700165import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700167
Craig Mautner8d341ef2013-03-26 09:03:27 -0700168import java.io.FileDescriptor;
169import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700170import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800171import java.lang.annotation.Retention;
172import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700173import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700174import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700176import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700177
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800178public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800179 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700180 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700181 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700182 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800188 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800189
Craig Mautnerf3333272013-04-22 10:55:53 -0700190 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700192
Craig Mautner0eea92c2013-05-16 13:35:39 -0700193 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700194 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700195
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700196 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700197 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700198
Craig Mautner05d29032013-05-03 13:40:13 -0700199 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
200 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
201 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700202 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800204 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
205 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
206 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700207 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800208 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
209 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800210
Wale Ogunwale040b4702015-08-06 18:10:50 -0700211 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212
Wale Ogunwale040b4702015-08-06 18:10:50 -0700213 // Used to indicate if an object (e.g. stack) that we are trying to get
214 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800215 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700217 // Used to indicate that windows of activities should be preserved during the resize.
218 static final boolean PRESERVE_WINDOWS = true;
219
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220 // Used to indicate if an object (e.g. task) should be moved/created
221 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700222 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700223
224 // Used to indicate that an objects (e.g. task) removal from its container
225 // (e.g. stack) is due to it moving to another container.
226 static final boolean MOVING = true;
227
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700228 // Force the focus to change to the stack we are moving a task to..
229 static final boolean FORCE_FOCUS = true;
230
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700231 // Don't execute any calls to resume.
232 static final boolean DEFER_RESUME = true;
233
Winson Chung010927a2016-12-15 16:12:35 -0800234 // Used to indicate that a task is removed it should also be removed from recents.
235 static final boolean REMOVE_FROM_RECENTS = true;
236
Winson Chung6954fc92017-03-24 16:22:12 -0700237 // Used to indicate that pausing an activity should occur immediately without waiting for
238 // the activity callback indicating that it has completed pausing
239 static final boolean PAUSE_IMMEDIATELY = true;
240
Winson Chung7900bee2017-03-10 08:59:25 -0800241 /**
242 * The modes which affect which tasks are returned when calling
243 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
244 */
245 @Retention(RetentionPolicy.SOURCE)
246 @IntDef({
247 MATCH_TASK_IN_STACKS_ONLY,
248 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
249 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
250 })
251 public @interface AnyTaskForIdMatchTaskMode {}
252 // Match only tasks in the current stacks
253 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
254 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
255 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
256 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
257 // provided stack id
258 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
259
Svetoslav7008b512015-06-24 18:47:07 -0700260 // Activity actions an app cannot start if it uses a permission which is not granted.
261 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
262 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700263
Svetoslav7008b512015-06-24 18:47:07 -0700264 static {
265 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
266 Manifest.permission.CAMERA);
267 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
268 Manifest.permission.CAMERA);
269 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
270 Manifest.permission.CALL_PHONE);
271 }
272
Svet Ganov99b60432015-06-27 13:15:22 -0700273 /** Action restriction: launching the activity is not restricted. */
274 private static final int ACTIVITY_RESTRICTION_NONE = 0;
275 /** Action restriction: launching the activity is restricted by a permission. */
276 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
277 /** Action restriction: launching the activity is restricted by an app op. */
278 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700279
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700280 // For debugging to make sure the caller when acquiring/releasing our
281 // wake lock is the system process.
282 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800283 /** The number of distinct task ids that can be assigned to the tasks of a single user */
284 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Craig Mautner27084302013-03-25 08:05:25 -0700286 final ActivityManagerService mService;
287
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800289
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700290 final ActivityStackSupervisorHandler mHandler;
291
292 /** Short cut */
293 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800294 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295
Bryce Lee9ad3eb32017-10-10 10:10:31 -0700296 LaunchingTaskPositioner mTaskPositioner = new LaunchingTaskPositioner();
297
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700298 /** Counter for next free stack ID to use for dynamic activity stacks. */
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700299 private int mNextFreeStackId = 0;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700300
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800301 /**
302 * Maps the task identifier that activities are currently being started in to the userId of the
303 * task. Each time a new task is created, the entry for the userId of the task is incremented
304 */
305 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700306
Craig Mautner2420ead2013-04-01 17:13:20 -0700307 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800308 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700309
Craig Mautnere0a38842013-12-16 16:14:02 -0800310 /** The stack containing the launcher app. Assumed to always be attached to
311 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800312 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700313
Craig Mautnere0a38842013-12-16 16:14:02 -0800314 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700316
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
318 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800319 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
322 /** List of activities that are waiting for a new activity to become visible before completing
323 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700324 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
325 // mStoppingActivities when something else comes up.
326 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700327
Bryce Lee4a194382017-04-04 14:32:48 -0700328 /** List of processes waiting to find out when a specific activity becomes visible. */
329 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330
331 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700332 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333
Craig Mautnerde4ef022013-04-07 19:01:33 -0700334 /** List of activities that are ready to be stopped, but waiting for the next activity to
335 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800336 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337
Craig Mautnerf3333272013-04-22 10:55:53 -0700338 /** List of activities that are ready to be finished, but waiting for the previous activity to
339 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800340 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700341
Craig Mautner0eea92c2013-05-16 13:35:39 -0700342 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800343 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344
Wale Ogunwale22e25262016-02-01 10:32:02 -0800345 /** List of activities whose multi-window mode changed that we need to report to the
346 * application */
347 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
348
349 /** List of activities whose picture-in-picture mode changed that we need to report to the
350 * application */
351 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
352
Winson Chung5af42fc2017-03-24 17:11:33 -0700353 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
354 * the application */
355 Rect mPipModeChangedTargetStackBounds;
356
Craig Mautnerf3333272013-04-22 10:55:53 -0700357 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700358 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700359
Craig Mautnerde4ef022013-04-07 19:01:33 -0700360 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
361 * is being brought in front of us. */
362 boolean mUserLeaving = false;
363
Craig Mautner0eea92c2013-05-16 13:35:39 -0700364 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700365 * We don't want to allow the device to go to sleep while in the process
366 * of launching an activity. This is primarily to allow alarm intent
367 * receivers to launch an activity and get that to run before the device
368 * goes back to sleep.
369 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700370 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700371
372 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700373 * Set when the system is going to sleep, until we have
374 * successfully paused the current activity and released our wake lock.
375 * At that point the system is allowed to actually sleep.
376 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700377 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700378
David Stevens9440dc82017-03-16 19:00:20 -0700379 /**
380 * A list of tokens that cause the top activity to be put to sleep.
381 * They are used by components that may hide and block interaction with underlying
382 * activities.
383 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700384 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700385
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700386 /** Stack id of the front stack when user switched, indexed by userId. */
387 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700388
Bryce Leeaf3a4002017-03-20 10:37:12 -0700389 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700391 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800393 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
394
395 private DisplayManagerInternal mDisplayManagerInternal;
396 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800397
Wale Ogunwaled046a012015-12-24 13:05:59 -0800398 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800399 boolean inResumeTopActivity;
400
Andrii Kulian1e32e022016-09-16 15:29:34 -0700401 /**
402 * Temporary rect used during docked stack resize calculation so we don't need to create a new
403 * object each time.
404 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700405 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700406
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700407 // The default minimal size that will be used if the activity doesn't specify its minimal size.
408 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700409 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700410
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700411 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
412 private boolean mTaskLayersChanged = true;
413
Jorim Jaggi275561a2016-02-23 10:11:02 -0500414 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800415
Jorim Jaggiea039a82017-08-02 14:37:49 +0200416 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
417
Andrii Kulian1779e612016-10-12 21:58:25 -0700418 @Override
419 protected int getChildCount() {
420 return mActivityDisplays.size();
421 }
422
423 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700424 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700425 return mActivityDisplays.valueAt(index);
426 }
427
428 @Override
429 protected ConfigurationContainer getParent() {
430 return null;
431 }
432
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700433 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700434 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700435 if (activityDisplay == null) {
436 throw new IllegalArgumentException("No display found with id: " + displayId);
437 }
438
439 return activityDisplay.getOverrideConfiguration();
440 }
441
442 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700443 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700444 if (activityDisplay == null) {
445 throw new IllegalArgumentException("No display found with id: " + displayId);
446 }
447
448 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
449 }
450
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700451 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700452 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
453 int callingUid, ActivityInfo activityInfo) {
454 if (displayId == DEFAULT_DISPLAY) {
455 // No restrictions for the default display.
456 return true;
457 }
458 if (!mService.mSupportsMultiDisplay) {
459 // Can't launch on secondary displays if feature is not supported.
460 return false;
461 }
462 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
463 // Can't apply wrong configuration to non-resizeable activities.
464 return false;
465 }
466 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
467 // Can't place activities to a display that has restricted launch rules.
468 // In this case the request should be made by explicitly adding target display id and
469 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
470 return false;
471 }
472 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700473 }
474
475 /**
476 * Check if configuration of specified display matches current global config.
477 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
478 * the same config as on the default display.
479 * @param displayId Id of the display to check.
480 * @return {@code true} if configuration matches.
481 */
482 private boolean displayConfigMatchesGlobal(int displayId) {
483 if (displayId == DEFAULT_DISPLAY) {
484 return true;
485 }
486 if (displayId == INVALID_DISPLAY) {
487 return false;
488 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700489 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700490 if (targetDisplay == null) {
491 throw new IllegalArgumentException("No display found with id: " + displayId);
492 }
493 return getConfiguration().equals(targetDisplay.getConfiguration());
494 }
495
Wale Ogunwale39381972015-12-17 17:15:29 -0800496 static class FindTaskResult {
497 ActivityRecord r;
498 boolean matchedByRootAffinity;
499 }
500 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
501
Craig Mautneree36c772014-07-16 14:56:05 -0700502 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800503 * Temp storage for display ids sorted in focus order.
504 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
505 * it's more efficient, as the number of displays is usually small.
506 */
507 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
508
509 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100510 * Used to keep track whether app visibilities got changed since the last pause. Useful to
511 * determine whether to invoke the task stack change listener after pausing.
512 */
513 boolean mAppVisibilitiesChangedSinceLastPause;
514
515 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100516 * Set of tasks that are in resizing mode during an app transition to fill the "void".
517 */
518 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
519
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700520
521 /**
522 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
523 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
524 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
525 * like the docked stack going empty.
526 */
527 private boolean mAllowDockedStackResize = true;
528
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100529 /**
Tony Mak853304c2016-04-18 15:17:41 +0100530 * Is dock currently minimized.
531 */
532 boolean mIsDockMinimized;
533
Jorim Jaggife762342016-10-13 14:33:27 +0200534 final KeyguardController mKeyguardController;
535
chaviw59b98852017-06-13 12:05:44 -0700536 private PowerManager mPowerManager;
537 private int mDeferResumeCount;
538
Tony Mak853304c2016-04-18 15:17:41 +0100539 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700540 * Description of a request to start a new activity, which has been held
541 * due to app switches being disabled.
542 */
543 static class PendingActivityLaunch {
544 final ActivityRecord r;
545 final ActivityRecord sourceRecord;
546 final int startFlags;
547 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700548 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700549
550 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700551 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700552 r = _r;
553 sourceRecord = _sourceRecord;
554 startFlags = _startFlags;
555 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700556 callerApp = _callerApp;
557 }
558
559 void sendErrorResult(String message) {
560 try {
561 if (callerApp.thread != null) {
562 callerApp.thread.scheduleCrash(message);
563 }
564 } catch (RemoteException e) {
565 Slog.e(TAG, "Exception scheduling crash of failed "
566 + "activity launcher sourceRecord=" + sourceRecord, e);
567 }
Craig Mautneree36c772014-07-16 14:56:05 -0700568 }
569 }
570
Bryce Leeaf691c02017-03-20 14:20:22 -0700571 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700572 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700573 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800574 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200575 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700576 }
577
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800578 void setRecentTasks(RecentTasks recentTasks) {
579 mRecentTasks = recentTasks;
580 }
581
Jeff Brown2c43c332014-06-12 22:38:59 -0700582 /**
583 * At the time when the constructor runs, the power manager has not yet been
584 * initialized. So we initialize our wakelocks afterwards.
585 */
586 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700587 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
588 mGoingToSleep = mPowerManager
589 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
590 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700591 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700592 }
593
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700594 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 synchronized (mService) {
596 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200597 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598
599 mDisplayManager =
600 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
601 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800602 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800603
604 Display[] displays = mDisplayManager.getDisplays();
605 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
606 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700607 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800608 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700609 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 }
611
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700612 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
613 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800614
615 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 }
Craig Mautner27084302013-03-25 08:05:25 -0700617 }
618
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700619 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700621 }
622
Craig Mautnerde4ef022013-04-07 19:01:33 -0700623 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700625 }
626
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700627 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700628 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700629 }
630
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800631 /** The top most stack on its display. */
632 boolean isFrontStackOnDisplay(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700633 return isFrontOfStackList(stack, stack.getDisplay().mStacks);
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800634 }
635
636 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800637 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700638 }
639
Wale Ogunwaled046a012015-12-24 13:05:59 -0800640 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800641 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
642 if (!focusCandidate.isFocusable()) {
643 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800644 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800645 }
646
647 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800648 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800649 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800650
Wale Ogunwaled046a012015-12-24 13:05:59 -0800651 EventLogTags.writeAmFocusedStack(
652 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
653 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
654 }
655
656 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800657 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800658 if (r != null && r.idle) {
659 checkFinishBootingLocked();
660 }
661 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700662 }
663
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700664 void moveHomeStackToFront(String reason) {
665 mHomeStack.moveToFront(reason);
666 }
667
Matthew Ng330757d2017-02-28 14:19:17 -0800668 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700669 final ActivityStack recentsStack = getDefaultDisplay().getStack(
670 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800671 if (recentsStack != null) {
672 recentsStack.moveToFront(reason);
673 }
674 }
675
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700676 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800677 boolean moveHomeStackTaskToTop(String reason) {
678 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700679
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700680 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681 if (top == null) {
682 return false;
683 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700684 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700685 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700686 }
687
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800688 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700689 if (!mService.mBooting && !mService.mBooted) {
690 // Not ready yet!
691 return false;
692 }
693
Craig Mautner84984fa2014-06-19 11:19:20 -0700694 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700695 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700696 }
697
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800698 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700699 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800700 final String myReason = reason + " resumeHomeStackTask";
701
Mark Lua56ea122015-10-08 13:31:01 +0800702 // Only resume home activity if isn't finishing.
703 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700704 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800705 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700706 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800707 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700708 }
709
Craig Mautner8d341ef2013-03-26 09:03:27 -0700710 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700711 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
712 }
713
714 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
715 return anyTaskForIdLocked(id, matchMode, null);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700716 }
717
718 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700719 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700720 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800721 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700722 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700723 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700724 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
725 @Nullable ActivityOptions aOptions) {
Winson Chung7900bee2017-03-10 08:59:25 -0800726 // If there is a stack id set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700727 // TODO: Don't really know if this is needed...
728 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
729 throw new IllegalArgumentException("Should not specify activity options for non-restore"
730 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800731 }
732
Craig Mautnere0a38842013-12-16 16:14:02 -0800733 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800735 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
737 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700738 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 if (task != null) {
740 return task;
741 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700742 }
743 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800744
Winson Chung7900bee2017-03-10 08:59:25 -0800745 // If we are matching stack tasks only, return now
746 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800747 return null;
748 }
749
Winson Chung7900bee2017-03-10 08:59:25 -0800750 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
751 // the task from recents
752 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700753 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
754
755 if (task == null) {
756 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800757 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
758 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700759
760 return null;
761 }
762
763 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700764 return task;
765 }
766
Winson Chung7900bee2017-03-10 08:59:25 -0800767 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700768 if (!restoreRecentTaskLocked(task, aOptions)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700769 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
770 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800771 return null;
772 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700773 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800774 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700775 }
776
Craig Mautner6170f732013-04-02 13:05:23 -0700777 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800778 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800779 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800780 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800781 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
782 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
783 if (r != null) {
784 return r;
785 }
Craig Mautner6170f732013-04-02 13:05:23 -0700786 }
787 }
788 return null;
789 }
790
Tony Mak853304c2016-04-18 15:17:41 +0100791 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000792 * Detects whether we should show a lock screen in front of this task for a locked user.
793 * <p>
794 * We'll do this if either of the following holds:
795 * <ul>
796 * <li>The top activity explicitly belongs to {@param userId}.</li>
797 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
798 * </ul>
799 *
800 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100801 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000802 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
803 // To handle the case that work app is in the task but just is not the top one.
804 final ActivityRecord activityRecord = task.getTopActivity();
805 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
806
807 return (activityRecord != null && activityRecord.userId == userId)
808 || (resultTo != null && resultTo.userId == userId);
809 }
810
811 /**
812 * Find all visible task stacks containing {@param userId} and intercept them with an activity
813 * to block out the contents and possibly start a credential-confirming intent.
814 *
815 * @param userId user handle for the locked managed profile.
816 */
817 void lockAllProfileTasks(@UserIdInt int userId) {
818 mWindowManager.deferSurfaceLayout();
819 try {
820 final List<ActivityStack> stacks = getStacks();
821 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
822 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
823 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
824 final TaskRecord task = tasks.get(taskNdx);
825
826 // Check the task for a top activity belonging to userId, or returning a result
827 // to an activity belonging to userId. Example case: a document picker for
828 // personal files, opened by a work app, should still get locked.
829 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000830 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
831 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000832 }
Tony Mak853304c2016-04-18 15:17:41 +0100833 }
834 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000835 } finally {
836 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100837 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000838 }
839
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800840 void setNextTaskIdForUserLocked(int taskId, int userId) {
841 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
842 if (taskId > currentTaskId) {
843 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700844 }
845 }
846
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700847 static int nextTaskIdForUser(int taskId, int userId) {
848 int nextTaskId = taskId + 1;
849 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
850 // Wrap around as there will be smaller task ids that are available now.
851 nextTaskId -= MAX_TASK_IDS_PER_USER;
852 }
853 return nextTaskId;
854 }
855
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800856 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800857 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
858 // for a userId u, a taskId can only be in the range
859 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
860 // 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 -0700861 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800862 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700863 || anyTaskForIdLocked(
864 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700865 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800866 if (candidateTaskId == currentTaskId) {
867 // Something wrong!
868 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
869 throw new IllegalStateException("Cannot get an available task id."
870 + " Reached limit of " + MAX_TASK_IDS_PER_USER
871 + " running tasks per user.");
872 }
873 }
874 mCurTaskIdForUser.put(userId, candidateTaskId);
875 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700876 }
877
Chong Zhang6cda19c2016-06-14 19:07:56 -0700878 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800879 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700880 if (stack == null) {
881 return null;
882 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700883 ActivityRecord resumedActivity = stack.mResumedActivity;
884 if (resumedActivity == null || resumedActivity.app == null) {
885 resumedActivity = stack.mPausingActivity;
886 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700887 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700888 }
889 }
890 return resumedActivity;
891 }
892
Dianne Hackbornff072722014-09-24 10:56:28 -0700893 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700894 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800895 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800896 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
897 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800898 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
899 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700900 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800901 continue;
902 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200903 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
904 final ActivityRecord top = stack.topRunningActivityLocked();
905 final int size = mTmpActivityList.size();
906 for (int i = 0; i < size; i++) {
907 final ActivityRecord activity = mTmpActivityList.get(i);
908 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
909 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200911 if (realStartActivityLocked(activity, app,
912 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800913 didSomething = true;
914 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700915 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800916 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200917 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700919 }
Craig Mautner20e72272013-04-01 13:45:53 -0700920 }
Craig Mautner20e72272013-04-01 13:45:53 -0700921 }
922 }
923 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700924 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700925 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700926 }
Craig Mautner20e72272013-04-01 13:45:53 -0700927 return didSomething;
928 }
929
930 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800931 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
932 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800933 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
934 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700935 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 continue;
937 }
938 final ActivityRecord resumedActivity = stack.mResumedActivity;
939 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700940 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800941 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 return false;
943 }
Craig Mautner20e72272013-04-01 13:45:53 -0700944 }
945 }
Wei Wang65c7a152016-06-02 18:51:22 -0700946 // Send launch end powerhint when idle
947 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700948 return true;
949 }
950
Craig Mautnerde4ef022013-04-07 19:01:33 -0700951 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800952 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
953 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800954 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
955 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700956 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800957 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700958 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 return false;
960 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 }
962 }
963 }
964 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700965 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
967 mLastFocusedStack + " to=" + mFocusedStack);
968 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700969 return true;
970 }
971
972 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800973 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800974 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
975 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800976 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
977 final ActivityStack stack = stacks.get(stackNdx);
978 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800979 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700980 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800981 return false;
982 }
983 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800984 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 }
986 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800987 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700988 }
989
Craig Mautner2acc3892013-09-23 10:28:14 -0700990 /**
991 * Pause all activities in either all of the stacks or just the back stacks.
992 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700993 * @param resuming The resuming activity.
994 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
995 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700996 * @return true if any activity was paused as a result of this call.
997 */
Wale Ogunwale950faff2016-08-08 09:51:04 -0700998 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700999 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1001 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001002 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1003 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001004 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001005 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001006 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001007 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1008 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001009 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001010 }
1011 }
1012 return someActivityPaused;
1013 }
1014
Craig Mautnerde4ef022013-04-07 19:01:33 -07001015 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001016 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001017 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1018 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001019 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1020 final ActivityStack stack = stacks.get(stackNdx);
1021 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001022 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001023 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001024 Slog.d(TAG_STATES,
1025 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001026 pausing = false;
1027 } else {
1028 return false;
1029 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001030 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001031 }
1032 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001033 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001034 }
1035
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001036 void cancelInitializingActivities() {
1037 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1038 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1039 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1040 stacks.get(stackNdx).cancelInitializingActivities();
1041 }
1042 }
1043 }
1044
Bryce Lee4a194382017-04-04 14:32:48 -07001045 void waitActivityVisible(ComponentName name, WaitResult result) {
1046 final WaitInfo waitInfo = new WaitInfo(name, result);
1047 mWaitingForActivityVisible.add(waitInfo);
1048 }
1049
1050 void cleanupActivity(ActivityRecord r) {
1051 // Make sure this record is no longer in the pending finishes list.
1052 // This could happen, for example, if we are trimming activities
1053 // down to the max limit while they are still waiting to finish.
1054 mFinishingActivities.remove(r);
1055 mActivitiesWaitingForVisibleActivity.remove(r);
1056
1057 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001058 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001059 mWaitingForActivityVisible.remove(i);
1060 }
1061 }
1062 }
1063
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001064 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001065 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001066 }
1067
1068 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1069 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001070 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1071 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001072 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001073 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001074 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001075 result.timeout = false;
1076 result.who = w.getComponent();
1077 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1078 result.thisTime = result.totalTime;
1079 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001080 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001081 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001082 if (changed) {
1083 mService.notifyAll();
1084 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 }
1086
Chong Zhang5022da32016-06-21 16:31:37 -07001087 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1088 boolean changed = false;
1089 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1090 WaitResult w = mWaitingActivityLaunched.remove(i);
1091 if (w.who == null) {
1092 changed = true;
1093 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1094 // the starting activity ends up moving another activity to front, and it should
1095 // wait for this new activity to become visible instead.
1096 // Do not modify other fields.
1097 w.result = START_TASK_TO_FRONT;
1098 }
1099 }
1100 if (changed) {
1101 mService.notifyAll();
1102 }
1103 }
1104
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001105 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1106 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001107 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001108 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001109 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001110 if (w.who == null) {
1111 changed = true;
1112 w.timeout = timeout;
1113 if (r != null) {
1114 w.who = new ComponentName(r.info.packageName, r.info.name);
1115 }
1116 w.thisTime = thisTime;
1117 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001118 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001120 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001121 if (changed) {
1122 mService.notifyAll();
1123 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001124 }
1125
Craig Mautner29219d92013-04-16 20:19:12 -07001126 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001127 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001128 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001129 if (r != null) {
1130 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001131 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001132
Andrii Kulian7318d632016-07-20 18:59:28 -07001133 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001134 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1135
1136 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1137 final int displayId = mTmpOrderedDisplayIds.get(i);
1138 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1139 if (stacks == null) {
1140 continue;
1141 }
1142 for (int j = stacks.size() - 1; j >= 0; --j) {
1143 final ActivityStack stack = stacks.get(j);
1144 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1145 r = stack.topRunningActivityLocked();
1146 if (r != null) {
1147 return r;
1148 }
Craig Mautner29219d92013-04-16 20:19:12 -07001149 }
1150 }
1151 }
1152 return null;
1153 }
1154
Dianne Hackborn09233282014-04-30 11:33:59 -07001155 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001156 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001157 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001158 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001159 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001160 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001161 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1162 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001163 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001164 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001165 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001166 }
1167 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001168
1169 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1170 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1171 while (maxNum > 0) {
1172 long mostRecentActiveTime = Long.MIN_VALUE;
1173 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001174 final int numTaskLists = runningTaskLists.size();
1175 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1176 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001177 if (!stackTaskList.isEmpty()) {
1178 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1179 if (lastActiveTime > mostRecentActiveTime) {
1180 mostRecentActiveTime = lastActiveTime;
1181 selectedStackList = stackTaskList;
1182 }
1183 }
1184 }
1185 if (selectedStackList != null) {
1186 list.add(selectedStackList.remove(0));
1187 --maxNum;
1188 } else {
1189 break;
1190 }
1191 }
Craig Mautner20e72272013-04-01 13:45:53 -07001192 }
1193
Todd Kennedy7440f172015-12-09 14:31:22 -08001194 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1195 ProfilerInfo profilerInfo) {
1196 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001197 if (aInfo != null) {
1198 // Store the found target back into the intent, because now that
1199 // we have it we never want to do this again. For example, if the
1200 // user navigates back to this point in the history, we should
1201 // always restart the exact same activity.
1202 intent.setComponent(new ComponentName(
1203 aInfo.applicationInfo.packageName, aInfo.name));
1204
1205 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001206 if (!aInfo.processName.equals("system")) {
1207 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001208 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001209 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001210
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001211 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1212 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1213 }
1214
Man Caocfa78b22015-06-11 20:14:34 -07001215 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1216 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1217 }
1218
1219 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001220 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001221 }
1222 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001223 final String intentLaunchToken = intent.getLaunchToken();
1224 if (aInfo.launchToken == null && intentLaunchToken != null) {
1225 aInfo.launchToken = intentLaunchToken;
1226 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001227 }
1228 return aInfo;
1229 }
1230
Todd Kennedy7440f172015-12-09 14:31:22 -08001231 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001232 return resolveIntent(intent, resolvedType, userId, 0);
1233 }
1234
1235 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001236 synchronized (mService) {
1237 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001238 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Todd Kennedy82b08422017-09-28 13:32:05 -07001239 | ActivityManagerService.STOCK_PM_FLAGS, userId, true);
Todd Kennedy7440f172015-12-09 14:31:22 -08001240 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001241 }
1242
1243 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1244 ProfilerInfo profilerInfo, int userId) {
1245 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1246 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1247 }
1248
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001249 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1250 boolean andResume, boolean checkConfig) throws RemoteException {
1251
1252 if (!allPausedActivitiesComplete()) {
1253 // While there are activities pausing we skipping starting any new activities until
1254 // pauses are complete. NOTE: that we also do this for activities that are starting in
1255 // the paused state because they will first be resumed then paused on the client side.
1256 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1257 "realStartActivityLocked: Skipping start of r=" + r
1258 + " some activities pausing...");
1259 return false;
1260 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001261
Bryce Leeaf691c02017-03-20 14:20:22 -07001262 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001263 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001264
1265 beginDeferResume();
1266
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 try {
chaviw59b98852017-06-13 12:05:44 -07001268 r.startFreezingScreenLocked(app, 0);
1269
1270 // schedule launch ticks to collect information about slow apps.
1271 r.startLaunchTickingLocked();
1272
1273 r.app = app;
1274
Jorim Jaggi838c2452017-08-28 15:44:43 +02001275 if (mKeyguardController.isKeyguardLocked()) {
1276 r.notifyUnknownVisibilityLaunched();
1277 }
1278
chaviw59b98852017-06-13 12:05:44 -07001279 // Have the window manager re-evaluate the orientation of the screen based on the new
1280 // activity order. Note that as a result of this, it can call back into the activity
1281 // manager with a new orientation. We don't care about that, because the activity is
1282 // not currently running so we are just restarting it anyway.
1283 if (checkConfig) {
1284 final int displayId = r.getDisplayId();
1285 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1286 getDisplayOverrideConfiguration(displayId),
1287 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1288 // Deferring resume here because we're going to launch new activity shortly.
1289 // We don't want to perform a redundant launch of the same record while ensuring
1290 // configurations and trying to resume top activity of focused stack.
1291 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1292 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 }
chaviw59b98852017-06-13 12:05:44 -07001294
1295 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1296 true /* isTop */)) {
1297 // We only set the visibility to true if the activity is allowed to be visible
1298 // based on
1299 // keyguard state. This avoids setting this into motion in window manager that is
1300 // later cancelled due to later calls to ensure visible activities that set
1301 // visibility back to false.
1302 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 }
chaviw59b98852017-06-13 12:05:44 -07001304
chaviw59b98852017-06-13 12:05:44 -07001305 final int applicationInfoUid =
1306 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1307 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1308 Slog.wtf(TAG,
1309 "User ID for activity changing for " + r
1310 + " appInfo.uid=" + r.appInfo.uid
1311 + " info.ai.uid=" + applicationInfoUid
1312 + " old=" + r.app + " new=" + app);
1313 }
1314
1315 app.waitingToKill = null;
1316 r.launchCount++;
1317 r.lastLaunchTime = SystemClock.uptimeMillis();
1318
1319 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1320
1321 int idx = app.activities.indexOf(r);
1322 if (idx < 0) {
1323 app.activities.add(r);
1324 }
1325 mService.updateLruProcessLocked(app, true, null);
1326 mService.updateOomAdjLocked();
1327
1328 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1329 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001330 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001331 }
1332
1333 try {
1334 if (app.thread == null) {
1335 throw new RemoteException();
1336 }
1337 List<ResultInfo> results = null;
1338 List<ReferrerIntent> newIntents = null;
1339 if (andResume) {
1340 // We don't need to deliver new intents and/or set results if activity is going
1341 // to pause immediately after launch.
1342 results = r.results;
1343 newIntents = r.newIntents;
1344 }
1345 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1346 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1347 + " newIntents=" + newIntents + " andResume=" + andResume);
1348 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1349 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001350 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001351 // Home process is the root process of the task.
1352 mService.mHomeProcess = task.mActivities.get(0).app;
1353 }
1354 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1355 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1356 r.sleeping = false;
1357 r.forceNewConfig = false;
1358 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1359 mService.showAskCompatModeDialogLocked(r);
1360 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1361 ProfilerInfo profilerInfo = null;
1362 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1363 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1364 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001365 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1366 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1367 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001368 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001369 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001370 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001371 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001372 }
1373 }
chaviw59b98852017-06-13 12:05:44 -07001374
Andreas Gampe2b073a02017-06-22 17:28:57 -07001375 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001377 }
1378 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001379
chaviw59b98852017-06-13 12:05:44 -07001380 app.hasShownUi = true;
1381 app.pendingUiClean = true;
1382 app.forceProcessStateUpTo(mService.mTopProcessState);
1383 // Because we could be starting an Activity in the system process this may not go
1384 // across a Binder interface which would create a new Configuration. Consequently
1385 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001386
chaviw59b98852017-06-13 12:05:44 -07001387 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1388 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1389 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001390
chaviw59b98852017-06-13 12:05:44 -07001391 logIfTransactionTooLarge(r.intent, r.icicle);
1392 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1393 System.identityHashCode(r), r.info,
1394 // TODO: Have this take the merged configuration instead of separate global
1395 // and override configs.
1396 mergedConfiguration.getGlobalConfiguration(),
1397 mergedConfiguration.getOverrideConfiguration(), r.compat,
1398 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1399 r.persistentState, results, newIntents, !andResume,
1400 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001401
chaviw59b98852017-06-13 12:05:44 -07001402 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1403 // This may be a heavy-weight process! Note that the package
1404 // manager will ensure that only activity can run in the main
1405 // process of the .apk, which is the only thing that will be
1406 // considered heavy-weight.
1407 if (app.processName.equals(app.info.packageName)) {
1408 if (mService.mHeavyWeightProcess != null
1409 && mService.mHeavyWeightProcess != app) {
1410 Slog.w(TAG, "Starting new heavy weight process " + app
1411 + " when already running "
1412 + mService.mHeavyWeightProcess);
1413 }
1414 mService.mHeavyWeightProcess = app;
1415 Message msg = mService.mHandler.obtainMessage(
1416 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1417 msg.obj = r;
1418 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001419 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001420 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001421
chaviw59b98852017-06-13 12:05:44 -07001422 } catch (RemoteException e) {
1423 if (r.launchFailed) {
1424 // This is the second time we failed -- finish activity
1425 // and give up.
1426 Slog.e(TAG, "Second failure launching "
1427 + r.intent.getComponent().flattenToShortString()
1428 + ", giving up", e);
1429 mService.appDiedLocked(app);
1430 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1431 "2nd-crash", false);
1432 return false;
1433 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001434
chaviw59b98852017-06-13 12:05:44 -07001435 // This is the first time we failed -- restart process and
1436 // retry.
1437 r.launchFailed = true;
1438 app.activities.remove(r);
1439 throw e;
1440 }
1441 } finally {
1442 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001443 }
1444
1445 r.launchFailed = false;
1446 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001447 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001448 }
1449
chaviw59b98852017-06-13 12:05:44 -07001450 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001451 // As part of the process of launching, ActivityThread also performs
1452 // a resume.
1453 stack.minimalResumeActivityLocked(r);
1454 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001455 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001456 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001457 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001458 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001459 "Moving to PAUSED: " + r + " (starting in paused state)");
1460 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001461 }
1462
1463 // Launch the new version setup screen if needed. We do this -after-
1464 // launching the initial activity (that is, home), so that it can have
1465 // a chance to initialize itself while in the background, making the
1466 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001467 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001468 mService.startSetupActivityLocked();
1469 }
1470
Dianne Hackborn465fa392014-09-14 14:21:18 -07001471 // Update any services we are bound to that might care about whether
1472 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001473 if (r.app != null) {
1474 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1475 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001476
Craig Mautner2420ead2013-04-01 17:13:20 -07001477 return true;
1478 }
1479
Sudheer Shankafab200f2017-05-17 20:41:53 -07001480 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1481 int extrasSize = 0;
1482 if (intent != null) {
1483 final Bundle extras = intent.getExtras();
1484 if (extras != null) {
1485 extrasSize = extras.getSize();
1486 }
1487 }
1488 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1489 if (extrasSize + icicleSize > 200000) {
1490 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1491 + ", icicle size: " + icicleSize);
1492 }
1493 }
1494
Craig Mautnere79d42682013-04-01 19:01:53 -07001495 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001496 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001497 // Is this activity's application already running?
1498 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001499 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001500
Andrii Kulian02b7a832016-10-06 23:11:56 -07001501 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001502
1503 if (app != null && app.thread != null) {
1504 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001505 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1506 || !"android".equals(r.info.packageName)) {
1507 // Don't add this if it is a platform component that is marked
1508 // to run in multiple processes, because this is actually
1509 // part of the framework so doesn't make sense to track as a
1510 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001511 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1512 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001513 }
George Mount2c92c972014-03-20 09:38:23 -07001514 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001515 return;
1516 } catch (RemoteException e) {
1517 Slog.w(TAG, "Exception when starting activity "
1518 + r.intent.getComponent().flattenToShortString(), e);
1519 }
1520
1521 // If a dead object exception was thrown -- fall through to
1522 // restart the application.
1523 }
1524
1525 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001526 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001527 }
1528
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001529 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001530 String resultWho, int requestCode, int callingPid, int callingUid,
1531 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001532 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001533 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1534 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001535 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001536 return true;
1537 }
1538 final int componentRestriction = getComponentRestrictionForCallingPackage(
1539 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1540 final int actionRestriction = getActionRestrictionForCallingPackage(
1541 intent.getAction(), callingPackage, callingPid, callingUid);
1542 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1543 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1544 if (resultRecord != null) {
1545 resultStack.sendActivityResultLocked(-1,
1546 resultRecord, resultWho, requestCode,
1547 Activity.RESULT_CANCELED, null);
1548 }
1549 final String msg;
1550 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1551 msg = "Permission Denial: starting " + intent.toString()
1552 + " from " + callerApp + " (pid=" + callingPid
1553 + ", uid=" + callingUid + ")" + " with revoked permission "
1554 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1555 } else if (!aInfo.exported) {
1556 msg = "Permission Denial: starting " + intent.toString()
1557 + " from " + callerApp + " (pid=" + callingPid
1558 + ", uid=" + callingUid + ")"
1559 + " not exported from uid " + aInfo.applicationInfo.uid;
1560 } else {
1561 msg = "Permission Denial: starting " + intent.toString()
1562 + " from " + callerApp + " (pid=" + callingPid
1563 + ", uid=" + callingUid + ")"
1564 + " requires " + aInfo.permission;
1565 }
1566 Slog.w(TAG, msg);
1567 throw new SecurityException(msg);
1568 }
1569
1570 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1571 final String message = "Appop Denial: starting " + intent.toString()
1572 + " from " + callerApp + " (pid=" + callingPid
1573 + ", uid=" + callingUid + ")"
1574 + " requires " + AppOpsManager.permissionToOp(
1575 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1576 Slog.w(TAG, message);
1577 return false;
1578 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1579 final String message = "Appop Denial: starting " + intent.toString()
1580 + " from " + callerApp + " (pid=" + callingPid
1581 + ", uid=" + callingUid + ")"
1582 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1583 Slog.w(TAG, message);
1584 return false;
1585 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001586 if (options != null) {
1587 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1588 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1589 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001590 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001591 final String msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ") with launchTaskId="
1594 + options.getLaunchTaskId();
1595 Slog.w(TAG, msg);
1596 throw new SecurityException(msg);
1597 }
1598 }
1599 // Check if someone tries to launch an activity on a private display with a different
1600 // owner.
1601 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001602 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1603 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001604 final String msg = "Permission Denial: starting " + intent.toString()
1605 + " from " + callerApp + " (pid=" + callingPid
1606 + ", uid=" + callingUid + ") with launchDisplayId="
1607 + launchDisplayId;
1608 Slog.w(TAG, msg);
1609 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001610 }
1611 }
1612
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001613 return true;
1614 }
1615
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001616 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001617 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1618 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001619 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1620 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1621
Andrii Kulian02689a72017-07-06 14:28:59 -07001622 if (callingPid == -1 && callingUid == -1) {
1623 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1624 return true;
1625 }
1626
Andrii Kulian62e6f252017-05-30 22:46:53 -07001627 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001628 if (activityDisplay == null) {
1629 Slog.w(TAG, "Launch on display check: display not found");
1630 return false;
1631 }
1632
Andrii Kulian02689a72017-07-06 14:28:59 -07001633 // Check if the caller has enough privileges to embed activities and launch to private
1634 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001635 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001636 callingUid);
1637 if (startAnyPerm == PERMISSION_GRANTED) {
1638 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1639 + " allow launch any on display");
1640 return true;
1641 }
1642
Andrii Kulian8f070292017-09-12 22:56:49 -07001643 // Check if caller is already present on display
1644 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1645
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001646 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1647 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1648 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001649 // Limit launching on virtual displays, because their contents can be read from Surface
1650 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001651 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1652 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1653 + " disallow launch on virtual display for not-embedded activity.");
1654 return false;
1655 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001656 // Check if the caller is allowed to embed activities from other apps.
1657 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001658 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001659 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1660 + " disallow activity embedding without permission.");
1661 return false;
1662 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001663 }
1664
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001665 if (!activityDisplay.isPrivate()) {
1666 // Anyone can launch on a public display.
1667 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1668 + " allow launch on public display");
1669 return true;
1670 }
1671
1672 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001673 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001674 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1675 + " allow launch for owner of the display");
1676 return true;
1677 }
1678
Andrii Kulian8f070292017-09-12 22:56:49 -07001679 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001680 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1681 + " allow launch for caller present on the display");
1682 return true;
1683 }
1684
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001685 Slog.w(TAG, "Launch on display check: denied");
1686 return false;
1687 }
1688
1689 /** Update lists of UIDs that are present on displays and have access to them. */
1690 void updateUIDsPresentOnDisplay() {
1691 mDisplayAccessUIDs.clear();
1692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1693 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001694 // Only bother calculating the whitelist for private displays
1695 if (activityDisplay.isPrivate()) {
1696 mDisplayAccessUIDs.append(
1697 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1698 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001699 }
1700 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1701 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1702 }
1703
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001704 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001705 final long identity = Binder.clearCallingIdentity();
1706 try {
1707 return UserManager.get(mService.mContext).getUserInfo(userId);
1708 } finally {
1709 Binder.restoreCallingIdentity(identity);
1710 }
1711 }
1712
Svet Ganov99b60432015-06-27 13:15:22 -07001713 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001715 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1716 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001717 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001718 return ACTIVITY_RESTRICTION_PERMISSION;
1719 }
1720
Christopher Tateff7add02015-08-17 10:23:22 -07001721 if (activityInfo.permission == null) {
1722 return ACTIVITY_RESTRICTION_NONE;
1723 }
1724
Svet Ganov99b60432015-06-27 13:15:22 -07001725 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1726 if (opCode == AppOpsManager.OP_NONE) {
1727 return ACTIVITY_RESTRICTION_NONE;
1728 }
1729
1730 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1731 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001732 if (!ignoreTargetSecurity) {
1733 return ACTIVITY_RESTRICTION_APPOP;
1734 }
Svet Ganov99b60432015-06-27 13:15:22 -07001735 }
1736
1737 return ACTIVITY_RESTRICTION_NONE;
1738 }
1739
Svetoslav7008b512015-06-24 18:47:07 -07001740 private int getActionRestrictionForCallingPackage(String action,
1741 String callingPackage, int callingPid, int callingUid) {
1742 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1747 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 final PackageInfo packageInfo;
1752 try {
1753 packageInfo = mService.mContext.getPackageManager()
1754 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1755 } catch (PackageManager.NameNotFoundException e) {
1756 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
1760 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001764 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001765 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001766 }
1767
1768 final int opCode = AppOpsManager.permissionToOpCode(permission);
1769 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001770 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001771 }
1772
1773 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1774 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001775 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001776 }
1777
Svet Ganov99b60432015-06-27 13:15:22 -07001778 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001779 }
1780
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001781 void setLaunchSource(int uid) {
1782 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1783 }
1784
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001785 void acquireLaunchWakelock() {
1786 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1787 throw new IllegalStateException("Calling must be system uid");
1788 }
1789 mLaunchingActivity.acquire();
1790 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1791 // To be safe, don't allow the wake lock to be held for too long.
1792 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1793 }
1794 }
1795
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001796 /**
1797 * Called when the frontmost task is idle.
1798 * @return the state of mService.mBooting before this was called.
1799 */
1800 private boolean checkFinishBootingLocked() {
1801 final boolean booting = mService.mBooting;
1802 boolean enableScreen = false;
1803 mService.mBooting = false;
1804 if (!mService.mBooted) {
1805 mService.mBooted = true;
1806 enableScreen = true;
1807 }
1808 if (booting || enableScreen) {
1809 mService.postFinishBooting(booting, enableScreen);
1810 }
1811 return booting;
1812 }
1813
Craig Mautnerf3333272013-04-22 10:55:53 -07001814 // Checked.
1815 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001816 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001817 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001818
Craig Mautnerf3333272013-04-22 10:55:53 -07001819 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001820 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001821 int NS = 0;
1822 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 boolean activityRemoved = false;
1825
Wale Ogunwale7d701172015-03-11 15:36:30 -07001826 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001827 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001828 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1829 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001830 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1831 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001832 if (fromTimeout) {
1833 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001835
1836 // This is a hack to semi-deal with a race condition
1837 // in the client where it can be constructed with a
1838 // newer configuration from when we asked it to launch.
1839 // We'll update with whatever configuration it now says
1840 // it used to launch.
1841 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001842 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001843 }
1844
1845 // We are now idle. If someone is waiting for a thumbnail from
1846 // us, we can now deliver.
1847 r.idle = true;
1848
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001849 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001850 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001851 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001852 }
1853 }
1854
1855 if (allResumedActivitiesIdle()) {
1856 if (r != null) {
1857 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001858 }
1859
1860 if (mLaunchingActivity.isHeld()) {
1861 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1862 if (VALIDATE_WAKE_LOCK_CALLER &&
1863 Binder.getCallingUid() != Process.myUid()) {
1864 throw new IllegalStateException("Calling must be system uid");
1865 }
1866 mLaunchingActivity.release();
1867 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001868 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001869 }
1870
1871 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001872 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1873 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001874 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001875 if ((NF = mFinishingActivities.size()) > 0) {
1876 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001877 mFinishingActivities.clear();
1878 }
1879
Craig Mautnerf3333272013-04-22 10:55:53 -07001880 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001881 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 mStartingUsers.clear();
1883 }
1884
Craig Mautnerf3333272013-04-22 10:55:53 -07001885 // Stop any activities that are scheduled to do so but have been
1886 // waiting for the next one to start.
1887 for (int i = 0; i < NS; i++) {
1888 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001889 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001890 if (stack != null) {
1891 if (r.finishing) {
1892 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1893 } else {
1894 stack.stopActivityLocked(r);
1895 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001896 }
1897 }
1898
1899 // Finish any activities that are scheduled to do so but have been
1900 // waiting for the next one to start.
1901 for (int i = 0; i < NF; i++) {
1902 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001903 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001904 if (stack != null) {
1905 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1906 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001907 }
1908
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001909 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001910 // Complete user switch
1911 if (startingUsers != null) {
1912 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001913 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001914 }
1915 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001916 }
1917
1918 mService.trimApplications();
1919 //dump();
1920 //mWindowManager.dump();
1921
Craig Mautnerf3333272013-04-22 10:55:53 -07001922 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001923 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001924 }
1925
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001926 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001927 }
1928
Craig Mautner8e569572013-10-11 17:36:59 -07001929 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001930 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001931 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1932 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001933 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1934 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1935 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001936 }
Craig Mautner19091252013-10-05 00:03:53 -07001937 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001938 }
1939
1940 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1944 stacks.get(stackNdx).closeSystemDialogsLocked();
1945 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001946 }
1947 }
1948
Craig Mautner93529a42013-10-04 15:03:13 -07001949 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001950 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001951 }
1952
Craig Mautner8d341ef2013-03-26 09:03:27 -07001953 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001954 * Update the last used stack id for non-current user (current user's last
1955 * used stack is the focused stack)
1956 */
1957 void updateUserStackLocked(int userId, ActivityStack stack) {
1958 if (userId != mCurrentUser) {
Wale Ogunwale68278562017-09-23 17:13:55 -07001959 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : mHomeStack.mStackId);
Chong Zhang45c25ce2015-08-10 22:18:26 -07001960 }
1961 }
1962
1963 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001964 * @return true if some activity was finished (or would have finished if doit were true).
1965 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001966 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1967 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001968 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001969 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1970 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001971 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001972 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001973 if (stack.finishDisabledPackageActivitiesLocked(
1974 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001975 didSomething = true;
1976 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001977 }
1978 }
1979 return didSomething;
1980 }
1981
Dianne Hackborna413dc02013-07-12 12:02:55 -07001982 void updatePreviousProcessLocked(ActivityRecord r) {
1983 // Now that this process has stopped, we may want to consider
1984 // it to be the previous app to try to keep around in case
1985 // the user wants to return to it.
1986
1987 // First, found out what is currently the foreground app, so that
1988 // we don't blow away the previous app if this activity is being
1989 // hosted by the process that is actually still the foreground.
1990 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001991 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1992 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001993 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1994 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001995 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001996 if (stack.mResumedActivity != null) {
1997 fgApp = stack.mResumedActivity.app;
1998 } else if (stack.mPausingActivity != null) {
1999 fgApp = stack.mPausingActivity.app;
2000 }
2001 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002002 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002003 }
2004 }
2005
2006 // Now set this one as the previous process, only if that really
2007 // makes sense to.
2008 if (r.app != null && fgApp != null && r.app != fgApp
2009 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002010 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002011 mService.mPreviousProcess = r.app;
2012 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2013 }
2014 }
2015
Wale Ogunwaled046a012015-12-24 13:05:59 -08002016 boolean resumeFocusedStackTopActivityLocked() {
2017 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002018 }
2019
Wale Ogunwaled046a012015-12-24 13:05:59 -08002020 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002021 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002022
2023 if (!readyToResume()) {
2024 return false;
2025 }
2026
Wale Ogunwaled046a012015-12-24 13:05:59 -08002027 if (targetStack != null && isFocusedStack(targetStack)) {
2028 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002029 }
chaviw59b98852017-06-13 12:05:44 -07002030
Wale Ogunwale06579d62016-04-30 15:29:06 -07002031 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2032 if (r == null || r.state != RESUMED) {
2033 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002034 } else if (r.state == RESUMED) {
2035 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2036 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002037 }
chaviw59b98852017-06-13 12:05:44 -07002038
Wale Ogunwaled046a012015-12-24 13:05:59 -08002039 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002040 }
2041
Todd Kennedy39bfee52016-02-24 10:28:21 -08002042 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2043 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2044 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2045 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2046 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2047 }
2048 }
2049 }
2050
Adrian Roos20d7df32016-01-12 18:59:43 +01002051 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2052 TaskRecord finishedTask = null;
2053 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002054 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2055 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002056 final int numStacks = stacks.size();
2057 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2058 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002059 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2060 if (stack == focusedStack || finishedTask == null) {
2061 finishedTask = t;
2062 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002063 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002064 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002065 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002066 }
2067
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002068 void finishVoiceTask(IVoiceInteractionSession session) {
2069 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2070 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2071 final int numStacks = stacks.size();
2072 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2073 final ActivityStack stack = stacks.get(stackNdx);
2074 stack.finishVoiceTask(session);
2075 }
2076 }
2077 }
2078
Andrii Kulianc27916642016-04-12 17:59:27 -07002079 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2080 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002081 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2082 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002083 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002084 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2085 // Caller wants the home activity moved with it. To accomplish this,
2086 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002087 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002088 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002089 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002090 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002091 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2092 + task + " to front. Stack is null");
2093 return;
2094 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002095
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002096 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2097 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2098 task.updateOverrideConfiguration(bounds);
2099
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002100 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002101
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002102 if (stack != currentStack) {
2103 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2104 "findTaskToMoveToFrontLocked");
2105 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002106 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2107 // still need moveTaskToFrontLocked() below for any transition settings.
2108 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002109 if (stack.resizeStackWithLaunchBounds()) {
2110 resizeStackLocked(stack, bounds, null /* tempTaskBounds */,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002111 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2112 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002113 } else {
2114 // WM resizeTask must be done after the task is moved to the correct stack,
2115 // because Task's setBounds() also updates dim layer's bounds, but that has
2116 // dependency on the stack.
2117 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002118 }
2119 }
2120
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002121 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002122 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002123 r == null ? null : r.appTimeTracker, reason);
2124
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002125 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002126 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002127
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002128 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002129 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002130 }
2131
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002132 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002133 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002134 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002135 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002136 return false;
2137 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002138 return (mService.mSupportsPictureInPicture
2139 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002140 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002141 }
2142
Bryce Lee9ad3eb32017-10-10 10:10:31 -07002143 LaunchingTaskPositioner getLaunchingTaskPositioner() {
2144 return mTaskPositioner;
2145 }
2146
Winson Chung55893332017-02-17 17:13:10 -08002147 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002148 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2149 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2150 if (stack != null) {
2151 return stack;
2152 }
2153 }
2154 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002155 }
2156
Wale Ogunwale68278562017-09-23 17:13:55 -07002157 /** @see ActivityDisplay#getStack(int, int) */
2158 private <T extends ActivityStack> T getStack(int windowingMode, int activityType) {
2159 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2160 final T stack = mActivityDisplays.valueAt(i).getStack(windowingMode, activityType);
2161 if (stack != null) {
2162 return stack;
2163 }
2164 }
2165 return null;
2166 }
2167
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002168 /**
2169 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2170 * @param windowingMode The windowing mode we are checking support for.
2171 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2172 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2173 * @param supportsFreeform If we should consider support for freeform multi-window.
2174 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2175 * @param activityType The activity type under consideration.
2176 * @return true if the windowing mode is supported.
2177 */
2178 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2179 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2180 int activityType) {
2181
2182 if (windowingMode == WINDOWING_MODE_UNDEFINED
2183 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2184 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002185 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002186 if (!supportsMultiWindow) {
2187 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002188 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002189
2190 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2191 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2192 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2193 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002194 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002195
2196 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2197 return false;
2198 }
2199
2200 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2201 return false;
2202 }
2203 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002204 }
2205
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002206 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002207 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002208
2209 // First preference if the windowing mode in the activity options if set.
2210 int windowingMode = (options != null)
2211 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2212
2213 // If windowing mode is unset, then next preference is the candidate task, then the
2214 // activity record.
2215 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2216 if (task != null) {
2217 windowingMode = task.getWindowingMode();
2218 }
2219 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2220 windowingMode = r.getWindowingMode();
2221 }
2222 }
2223
2224 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002225 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2226 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2227 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2228 boolean supportsPip = mService.mSupportsPictureInPicture;
2229 if (supportsMultiWindow) {
2230 if (task != null) {
2231 supportsMultiWindow = task.isResizeable();
2232 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2233 // TODO: Do we need to check for freeform and Pip support here?
2234 } else if (r != null) {
2235 supportsMultiWindow = r.isResizeable();
2236 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2237 supportsFreeform = r.supportsFreeform();
2238 supportsPip = r.supportsPictureInPicture();
2239 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002240 }
2241
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002242 if (isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
2243 supportsFreeform, supportsPip, activityType)) {
2244 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002245 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002246 return WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002247 }
2248
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002249 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002250 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002251 // Preference is given to the activity type for the activity then the task since the type
2252 // once set shouldn't change.
2253 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2254 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2255 activityType = task.getActivityType();
2256 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002257 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2258 return activityType;
2259 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002260 return options != null ? options.getLaunchActivityType() : ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002261 }
2262
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002263 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2264 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2265 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002266 }
2267
2268 /**
2269 * Returns the right stack to use for launching factoring in all the input parameters.
2270 *
2271 * @param r The activity we are trying to launch. Can be null.
2272 * @param options The activity options used to the launch. Can be null.
2273 * @param candidateTask The possible task the activity might be launched in. Can be null.
2274 *
2275 * @return The stack to use for the launch or INVALID_STACK_ID.
2276 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002277 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2278 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2279 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002280 int taskId = INVALID_TASK_ID;
2281 int displayId = INVALID_DISPLAY;
2282 //Rect bounds = null;
2283
2284 // We give preference to the launch preference in activity options.
2285 if (options != null) {
2286 taskId = options.getLaunchTaskId();
2287 displayId = options.getLaunchDisplayId();
2288 // TODO: Need to work this into the equation...
2289 //bounds = options.getLaunchBounds();
2290 }
2291
2292 // First preference for stack goes to the task Id set in the activity options. Use the stack
2293 // associated with that if possible.
2294 if (taskId != INVALID_TASK_ID) {
2295 // Temporarily set the task id to invalid in case in re-entry.
2296 options.setLaunchTaskId(INVALID_TASK_ID);
2297 final TaskRecord task = anyTaskForIdLocked(taskId,
2298 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options);
2299 options.setLaunchTaskId(taskId);
2300 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002301 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002302 }
2303 }
2304
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002305 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002306 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2307 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002308
2309 // Next preference for stack goes to the display Id set in the activity options or the
2310 // candidate display.
2311 if (displayId == INVALID_DISPLAY) {
2312 displayId = candidateDisplayId;
2313 }
2314 if (displayId != INVALID_DISPLAY) {
2315 if (r != null) {
2316 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002317 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002318 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002319 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002320 }
2321 }
2322 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2323 if (display != null) {
2324 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002325 stack = (T) display.mStacks.get(i);
2326 if (stack.isCompatible(windowingMode, activityType)) {
2327 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002328 }
2329 }
2330 // TODO: We should create the stack we want on the display at this point.
2331 }
2332 }
2333
2334 // Give preference to the stack and display of the input task and activity if they match the
2335 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002336 stack = null;
2337 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002338 if (candidateTask != null) {
2339 stack = candidateTask.getStack();
2340 }
2341 if (stack == null && r != null) {
2342 stack = r.getStack();
2343 }
2344 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002345 if (stack.isCompatible(windowingMode, activityType)) {
2346 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002347 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002348 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002349 }
2350
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002351 if (display == null
2352 // TODO: Can be removed once we figure-out how non-standard types should launch
2353 // outside the default display.
2354 || (activityType != ACTIVITY_TYPE_STANDARD
2355 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2356 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002357 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002358
2359 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2360 if (stack != null) {
2361 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002362 }
2363
2364 // Whatever...return some default for now.
2365 if (candidateTask != null && candidateTask.mBounds != null
2366 && mService.mSupportsFreeformWindowManagement) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002367 windowingMode = WINDOWING_MODE_FREEFORM;
2368 } else {
2369 windowingMode = WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002370 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002371 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002372 }
2373
Andrii Kulian16802aa2016-11-02 12:21:33 -07002374 /**
2375 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2376 * If there is no such stack, new dynamic stack can be created.
2377 * @param displayId Target display.
2378 * @param r Activity that should be launched there.
2379 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2380 */
2381 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002382 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002383 if (activityDisplay == null) {
2384 throw new IllegalArgumentException(
2385 "Display with displayId=" + displayId + " not found.");
2386 }
2387
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002388 if (!r.canBeLaunchedOnDisplay(displayId)) {
2389 return null;
2390 }
2391
Andrii Kulian16802aa2016-11-02 12:21:33 -07002392 // Return the topmost valid stack on the display.
2393 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2394 final ActivityStack stack = activityDisplay.mStacks.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07002395 if (isValidLaunchStack(stack, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002396 return stack;
2397 }
2398 }
2399
2400 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002401 if (displayId != DEFAULT_DISPLAY) {
2402 return activityDisplay.createStack(
2403 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002404 }
2405
2406 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2407 return null;
2408 }
2409
Wale Ogunwale68278562017-09-23 17:13:55 -07002410 // TODO: Can probably be consolidated into getLaunchStack()...
2411 private boolean isValidLaunchStack(ActivityStack stack, int displayId, ActivityRecord r) {
2412 switch (stack.getActivityType()) {
2413 case ACTIVITY_TYPE_HOME: return r.isActivityTypeHome();
2414 case ACTIVITY_TYPE_RECENTS: return r.isActivityTypeRecents();
2415 case ACTIVITY_TYPE_ASSISTANT: return r.isActivityTypeAssistant();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002416 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002417 switch (stack.getWindowingMode()) {
2418 case WINDOWING_MODE_FULLSCREEN: return true;
2419 case WINDOWING_MODE_FREEFORM: return r.supportsFreeform();
2420 case WINDOWING_MODE_PINNED: return r.supportsPictureInPicture();
2421 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: return r.supportsSplitScreenWindowingMode();
2422 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: return r.supportsSplitScreenWindowingMode();
2423 }
2424
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002425 if (!stack.isOnHomeDisplay()) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002426 return r.canBeLaunchedOnDisplay(displayId);
2427 }
2428 Slog.e(TAG, "isValidLaunchStack: Unexpected stack=" + stack);
2429 return false;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002430 }
2431
Craig Mautner967212c2013-04-13 21:10:58 -07002432 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002433 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002434 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2435 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002436 }
2437 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002438 }
2439
Jorim Jaggife762342016-10-13 14:33:27 +02002440 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2441 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2442 }
2443
Andrii Kulian7fc22812016-12-28 13:04:11 -08002444 /**
2445 * Get next focusable stack in the system. This will search across displays and stacks
2446 * in last-focused order for a focusable and visible stack, different from the target stack.
2447 *
2448 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2449 * searching for next candidate.
2450 * @return Next focusable {@link ActivityStack}, null if not found.
2451 */
2452 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2453 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2454
2455 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2456 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002457 // If a display is registered in WM, it must also be available in AM.
2458 @SuppressWarnings("ConstantConditions")
2459 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002460 for (int j = stacks.size() - 1; j >= 0; --j) {
2461 final ActivityStack stack = stacks.get(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002462 if (stack != currentFocus && stack.isFocusable()
2463 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002464 return stack;
2465 }
2466 }
2467 }
2468
2469 return null;
2470 }
2471
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002472 /**
2473 * Get next valid stack for launching provided activity in the system. This will search across
2474 * displays and stacks in last-focused order for a focusable and visible stack, except those
2475 * that are on a currently focused display.
2476 *
2477 * @param r The activity that is being launched.
2478 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2479 * searching for the next candidate.
2480 * @return Next valid {@link ActivityStack}, null if not found.
2481 */
2482 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2483 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2484 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2485 final int displayId = mTmpOrderedDisplayIds.get(i);
2486 if (displayId == currentFocus) {
2487 continue;
2488 }
2489 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2490 if (stack != null) {
2491 return stack;
2492 }
2493 }
2494 return null;
2495 }
2496
Craig Mautneree2e45a2014-06-27 12:10:03 -07002497 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002498 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002499 }
2500
2501 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002502 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2503 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2504 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002505 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002506 final ArrayList<ActivityRecord> activities = task.mActivities;
2507 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2508 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002509 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002510 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002511 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002512 }
2513 }
2514 }
2515 }
2516 return null;
2517 }
2518
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002519 void resizeStackLocked(ActivityStack stack, Rect bounds, Rect tempTaskBounds,
2520 Rect tempTaskInsetBounds, boolean preserveWindows, boolean allowResizeInDockedMode,
2521 boolean deferResume) {
2522
2523 if (stack.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002524 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002525 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002526 return;
2527 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002528
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002529 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002530 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002531 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002532 // If the docked stack exists, don't resize non-floating stacks independently of the
2533 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002534 return;
2535 }
2536
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002537 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stack.mStackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002538 mWindowManager.deferSurfaceLayout();
2539 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002540 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002541 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2542 // null bounds = fullscreen windowing mode...at least for now.
2543 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2544 } else if (splitScreenActive) {
2545 // If we are in split-screen mode and this stack support split-screen, then
2546 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2547 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2548 }
2549 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002550 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002551 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002552 stack.ensureVisibleActivitiesConfigurationLocked(
2553 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002554 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002555 } finally {
2556 mWindowManager.continueSurfaceLayout();
2557 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002558 }
2559 }
2560
Wale Ogunwale68278562017-09-23 17:13:55 -07002561 private void deferUpdateBounds(int activityType) {
2562 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002563 if (stack != null) {
2564 stack.deferUpdateBounds();
2565 }
2566 }
2567
Wale Ogunwale68278562017-09-23 17:13:55 -07002568 private void continueUpdateBounds(int activityType) {
2569 final ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
Jorim Jaggi192086e2016-03-11 17:17:03 +01002570 if (stack != null) {
2571 stack.continueUpdateBounds();
2572 }
2573 }
2574
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002575 void notifyAppTransitionDone() {
Wale Ogunwale68278562017-09-23 17:13:55 -07002576 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002577 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2578 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002579 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002580 if (task != null) {
2581 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002582 }
2583 }
2584 mResizingTasksDuringAnimation.clear();
2585 }
2586
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002587 /**
2588 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002589 * Can do that once we are no longer using static stack ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002590 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002591 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002592 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002593
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002594 mWindowManager.deferSurfaceLayout();
2595 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002596 final int windowingMode = fromStack.getWindowingMode();
2597 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2598 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2599
2600 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002601 // We are moving all tasks from the docked stack to the fullscreen stack,
2602 // which is dismissing the docked stack, so resize all other stacks to
2603 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002604 final ArrayList<ActivityStack> displayStacks = toDisplay.mStacks;
2605 for (int i = displayStacks.size() - 1; i >= 0; --i) {
2606 final ActivityStack otherStack = displayStacks.get(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002607 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2608 continue;
2609 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002610 resizeStackLocked(otherStack, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002611 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002612 }
2613
2614 // Also disable docked stack resizing since we have manually adjusted the
2615 // size of other stacks above and we don't want to trigger a docked stack
2616 // resize when we remove task from it below and it is detached from the
2617 // display because it no longer contains any tasks.
2618 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002619 } else if (inPinnedWindowingMode && onTop) {
2620 // Log if we are expanding the PiP to fullscreen
2621 MetricsLogger.action(mService.mContext,
2622 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002623 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002624
Winson Chung5af42fc2017-03-24 17:11:33 -07002625 // If we are moving from the pinned stack, then the animation takes care of updating
2626 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002627 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002628 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002629
Wale Ogunwale388945c2017-10-04 12:13:46 -07002630 if (!tasks.isEmpty()) {
2631 final int size = tasks.size();
2632 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2633 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2634
2635 if (onTop) {
2636 final int returnToType =
2637 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
2638 for (int i = 0; i < size; i++) {
2639 final TaskRecord task = tasks.get(i);
2640 final boolean isTopTask = i == (size - 1);
2641 if (inPinnedWindowingMode) {
2642 // Update the return-to to reflect where the pinned stack task was moved
2643 // from so that we retain the stack that was previously visible if the
2644 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
2645 task.setTaskToReturnTo(returnToType);
2646 }
2647 // Defer resume until all the tasks have been moved to the fullscreen stack
2648 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2649 isTopTask /* animate */, DEFER_RESUME,
2650 schedulePictureInPictureModeChange,
2651 "moveTasksToFullscreenStack - onTop");
Winson Chung0c1ca092016-11-10 17:40:34 -08002652 }
Wale Ogunwale388945c2017-10-04 12:13:46 -07002653 } else {
2654 for (int i = 0; i < size; i++) {
2655 final TaskRecord task = tasks.get(i);
2656 // Position the tasks in the fullscreen stack in order at the bottom of the
2657 // stack. Also defer resume until all the tasks have been moved to the
2658 // fullscreen stack.
2659 task.reparent(fullscreenStack, i /* position */,
2660 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2661 schedulePictureInPictureModeChange,
2662 "moveTasksToFullscreenStack - NOT_onTop");
2663 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002664 }
2665 }
Winson Chung74666102017-02-22 17:49:24 -08002666
2667 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2668 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002669 } finally {
2670 mAllowDockedStackResize = true;
2671 mWindowManager.continueSurfaceLayout();
2672 }
2673 }
2674
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002675 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002676 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2677 }
2678
2679 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2680 mWindowManager.inSurfaceTransaction(() ->
2681 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002682 }
2683
Jorim Jaggidc249c42015-12-15 14:57:31 -08002684 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002685 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2686 boolean preserveWindows) {
2687 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2688 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2689 false /* deferResume */);
2690 }
2691
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002692 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002693 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2694 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002695
2696 if (!mAllowDockedStackResize) {
2697 // Docked stack resize currently disabled.
2698 return;
2699 }
2700
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002701 final ActivityStack stack = getDefaultDisplay().getStack(
2702 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002703 if (stack == null) {
2704 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2705 return;
2706 }
2707
2708 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2709 mWindowManager.deferSurfaceLayout();
2710 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002711 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2712 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002713 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002714 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002715
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002716 // TODO: Checking for isAttached might not be needed as if the user passes in null
2717 // dockedBounds then they want the docked stack to be dismissed.
2718 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2719 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002720 // In this case we make all other static stacks fullscreen and move all
2721 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002722 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002723
2724 // stack shouldn't contain anymore activities, so nothing to resume.
2725 r = null;
2726 } else {
2727 // Docked stacks occupy a dedicated region on screen so the size of all other
2728 // static stacks need to be adjusted so they don't overlap with the docked stack.
2729 // We get the bounds to use from window manager which has been adjusted for any
2730 // screen controls and is also the same for all stacks.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002731 final ArrayList<ActivityStack> stacks = getStacksOnDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002732 final Rect otherTaskRect = new Rect();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002733 for (int i = stacks.size() - 1; i >= 0; --i) {
2734 final ActivityStack current = stacks.get(i);
2735 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002736 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002737 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002738 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002739 continue;
2740 }
2741 // Need to set windowing mode here before we try to get the dock bounds.
2742 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2743 current.getStackDockedModeBounds(
2744 tempOtherTaskBounds /* currentTempTaskBounds */,
2745 tempRect /* outStackBounds */,
2746 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2747
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002748 resizeStackLocked(current, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002749 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2750 tempOtherTaskInsetBounds, preserveWindows,
2751 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002752 }
2753 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002754 if (!deferResume) {
2755 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2756 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002757 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002758 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002759 mWindowManager.continueSurfaceLayout();
2760 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2761 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002762 }
2763
Robert Carr0d00c2e2016-02-29 17:45:02 -08002764 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002765 // TODO(multi-display): Pinned stack display should be passed in.
2766 final PinnedActivityStack stack = getDefaultDisplay().getStack(
2767 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002768 if (stack == null) {
2769 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2770 return;
2771 }
Winson Chung19953ca2017-04-11 11:19:23 -07002772
2773 // It is possible for the bounds animation from the WM to call this but be delayed by
2774 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2775 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2776 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2777 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002778 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002779 return;
2780 }
2781
Robert Carr0d00c2e2016-02-29 17:45:02 -08002782 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2783 mWindowManager.deferSurfaceLayout();
2784 try {
2785 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002786 Rect insetBounds = null;
2787 if (tempPinnedTaskBounds != null) {
2788 // We always use 0,0 as the position for the inset rect because
2789 // if we are getting insets at all in the pinned stack it must mean
2790 // we are headed for fullscreen.
2791 insetBounds = tempRect;
2792 insetBounds.top = 0;
2793 insetBounds.left = 0;
2794 insetBounds.right = tempPinnedTaskBounds.width();
2795 insetBounds.bottom = tempPinnedTaskBounds.height();
2796 }
2797 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002798 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002799 } finally {
2800 mWindowManager.continueSurfaceLayout();
2801 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2802 }
2803 }
2804
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002805 private void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002806 final ActivityStack stack = getStack(stackId);
2807 if (stack == null) {
2808 return;
2809 }
2810
2811 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002812 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002813 /**
2814 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2815 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2816 * that the client receives onStop() before it is reparented. We do this by detaching
2817 * the stack from the display so that it will be considered invisible when
2818 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2819 * invisible as well and added to the stopping list. After which we process the
2820 * stopping list by handling the idle.
2821 */
2822 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2823 pinnedStack.mForceHidden = true;
2824 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2825 pinnedStack.mForceHidden = false;
2826 activityIdleInternalLocked(null, false /* fromTimeout */,
2827 true /* processPausingActivites */, null /* configuration */);
2828
2829 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002830 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002831 } else {
2832 for (int i = tasks.size() - 1; i >= 0; i--) {
2833 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2834 REMOVE_FROM_RECENTS);
2835 }
2836 }
2837 }
2838
2839 /**
Robert Carr6914f082017-03-20 19:04:30 -07002840 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2841 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2842 * instead moved back onto the fullscreen stack.
2843 */
2844 void removeStackLocked(int stackId) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002845 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stackId));
2846 }
2847
Wale Ogunwale68278562017-09-23 17:13:55 -07002848 /**
2849 * Removes stacks in the input windowing modes from the system if they are of activity type
2850 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
2851 */
2852 void removeStacksInWindowingModes(int... windowingModes) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002853 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
Wale Ogunwale68278562017-09-23 17:13:55 -07002854 mActivityDisplays.valueAt(i).removeStacksInWindowingModes(windowingModes);
2855 }
2856 }
2857
2858 void removeStacksWithActivityTypes(int... activityTypes) {
2859 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2860 mActivityDisplays.valueAt(i).removeStacksWithActivityTypes(activityTypes);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002861 }
Robert Carr6914f082017-03-20 19:04:30 -07002862 }
2863
2864 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002865 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2866 */
2867 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2868 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2869 }
2870
2871 /**
Winson Chung010927a2016-12-15 16:12:35 -08002872 * Removes the task with the specified task id.
2873 *
2874 * @param taskId Identifier of the task to be removed.
2875 * @param killProcess Kill any process associated with the task if possible.
2876 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002877 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2878 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002879 * @return Returns true if the given task was found and removed.
2880 */
Winson Chung6954fc92017-03-24 16:22:12 -07002881 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2882 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002883 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002884 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002885 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002886 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2887 if (tr.isPersistable) {
2888 mService.notifyTaskPersisterLocked(null, true);
2889 }
2890 return true;
2891 }
2892 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2893 return false;
2894 }
2895
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002896 void addRecentActivity(ActivityRecord r) {
2897 if (r == null) {
2898 return;
2899 }
2900 final TaskRecord task = r.getTask();
2901 mRecentTasks.addLocked(task);
2902 task.touchActiveTime();
2903 }
2904
2905 void removeTaskFromRecents(TaskRecord task) {
2906 mRecentTasks.remove(task);
2907 task.removedFromRecents();
2908 }
2909
Winson Chung010927a2016-12-15 16:12:35 -08002910 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2911 if (removeFromRecents) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002912 removeTaskFromRecents(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002913 }
2914 ComponentName component = tr.getBaseIntent().getComponent();
2915 if (component == null) {
2916 Slog.w(TAG, "No component for base intent of task: " + tr);
2917 return;
2918 }
2919
2920 // Find any running services associated with this app and stop if needed.
2921 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2922
2923 if (!killProcess) {
2924 return;
2925 }
2926
2927 // Determine if the process(es) for this task should be killed.
2928 final String pkg = component.getPackageName();
2929 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2930 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2931 for (int i = 0; i < pmap.size(); i++) {
2932
2933 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2934 for (int j = 0; j < uids.size(); j++) {
2935 ProcessRecord proc = uids.valueAt(j);
2936 if (proc.userId != tr.userId) {
2937 // Don't kill process for a different user.
2938 continue;
2939 }
2940 if (proc == mService.mHomeProcess) {
2941 // Don't kill the home process along with tasks from the same package.
2942 continue;
2943 }
2944 if (!proc.pkgList.containsKey(pkg)) {
2945 // Don't kill process that is not associated with this task.
2946 continue;
2947 }
2948
2949 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002950 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002951 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2952 // Don't kill process(es) that has an activity in a different task that is
2953 // also in recents.
2954 return;
2955 }
2956 }
2957
2958 if (proc.foregroundServices) {
2959 // Don't kill process(es) with foreground service.
2960 return;
2961 }
2962
2963 // Add process to kill list.
2964 procsToKill.add(proc);
2965 }
2966 }
2967
2968 // Kill the running processes.
2969 for (int i = 0; i < procsToKill.size(); i++) {
2970 ProcessRecord pr = procsToKill.get(i);
2971 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2972 && pr.curReceivers.isEmpty()) {
2973 pr.kill("remove task", true);
2974 } else {
2975 // We delay killing processes that are not in the background or running a receiver.
2976 pr.waitingToKill = "remove task";
2977 }
2978 }
2979 }
2980
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002982 while (true) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002983 if (getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002984 break;
2985 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002986 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002987 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002988 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002989 }
2990
Chong Zhang5dcb2752015-08-18 13:50:26 -07002991 /**
2992 * Restores a recent task to a stack
2993 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002994 * @param aOptions The activity options to use for restoration.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002995 * @return true if the task has been restored successfully.
2996 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002997 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002998 final ActivityStack stack = getLaunchStack(null, aOptions, task, !ON_TOP);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002999 final ActivityStack currentStack = task.getStack();
3000 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003001 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003002 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003003 // Nothing else to do since it is already restored in the right stack.
3004 return true;
3005 }
3006 // Remove current stack association, so we can re-associate the task with the
3007 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07003008 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003009 }
3010
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003011 stack.addTask(task, !ON_TOP, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003012 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003013 task.createWindowContainer(!ON_TOP, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3015 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003016 final ArrayList<ActivityRecord> activities = task.mActivities;
3017 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003018 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003019 }
3020 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003021 }
3022
Wale Ogunwale040b4702015-08-06 18:10:50 -07003023 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003024 * Move stack with all its existing content to specified display.
3025 * @param stackId Id of stack to move.
3026 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003027 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003028 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003029 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003030 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003031 if (activityDisplay == null) {
3032 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3033 + displayId);
3034 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003035 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003036 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003037 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3038 + stackId);
3039 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003040
3041 final ActivityDisplay currentDisplay = stack.getDisplay();
3042 if (currentDisplay == null) {
3043 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3044 + " is not attached to any display.");
3045 }
3046
3047 if (currentDisplay.mDisplayId == displayId) {
3048 throw new IllegalArgumentException("Trying to move stack=" + stack
3049 + " to its current displayId=" + displayId);
3050 }
3051
3052 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003053 // TODO(multi-display): resize stacks properly if moved from split-screen.
3054 }
3055
3056 /**
Winson Chung74666102017-02-22 17:49:24 -08003057 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3058 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003059 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003060 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003061 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003062 final int stackId = stack.mStackId;
3063 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003064
Winson Chung74666102017-02-22 17:49:24 -08003065 // Check that we aren't reparenting to the same stack that the task is already in
3066 if (prevStack != null && prevStack.mStackId == stackId) {
3067 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3068 + " already in stackId=" + stackId);
3069 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003070 }
3071
Winson Chung74666102017-02-22 17:49:24 -08003072 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3073 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003074 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003075 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003076 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003077 }
3078
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003079 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3080 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003081 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003082 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3083 + " reparent task=" + task + " to stackId=" + stackId);
3084 }
3085
Winson Chung74666102017-02-22 17:49:24 -08003086 // Ensure that we aren't trying to move into a freeform stack without freeform
3087 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003088 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3089 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003090 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3091 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003092 }
3093
Winson Chung74666102017-02-22 17:49:24 -08003094 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3095 // used for split-screen mode and will cause things like the docked divider to show up. We
3096 // instead leave the task in its current stack or move it to the fullscreen stack if it
3097 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003098 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003099 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3100 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003101 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3102 // for example, a docked stack is created which will lead to the stack we are moving
3103 // from being resized and and its resizeable tasks being resized.
3104 try {
3105 task.mTemporarilyUnresizable = true;
3106 stack = stack.getDisplay().createStack(
3107 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3108 } finally {
3109 task.mTemporarilyUnresizable = false;
3110 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003111 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003112 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113 }
3114
Winson Chung08f81892017-03-02 15:40:51 -08003115 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003116 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003117 if (stack == null) {
3118 throw new IllegalArgumentException(
3119 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3120 }
3121
3122 final ActivityRecord r = stack.topRunningActivityLocked();
3123 if (r == null) {
3124 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3125 + " in stack=" + stack);
3126 return false;
3127 }
3128
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003129 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003130 Slog.w(TAG,
3131 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003132 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003133 return false;
3134 }
3135
Winson Chung3a682872017-04-10 14:38:05 -07003136 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003137 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003138 return true;
3139 }
3140
Winson Chung8bca9e42017-04-16 15:59:43 -07003141 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003142 boolean moveHomeStackToFront, String reason) {
3143
Wale Ogunwale480dca02016-02-06 13:58:29 -08003144 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003145
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003146 final ActivityDisplay display = r.getStack().getDisplay();
3147 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003148
Bryce Lee04ab3462017-04-10 15:06:33 -07003149 // This will clear the pinned stack by moving an existing task to the full screen stack,
3150 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003151 if (stack != null) {
3152 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3153 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003154
Wale Ogunwale1666e312016-12-16 11:27:18 -08003155 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003156 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003157
Wale Ogunwale480dca02016-02-06 13:58:29 -08003158 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003159 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003160 // Resize the pinned stack to match the current size of the task the activity we are
3161 // going to be moving is currently contained in. We do this to have the right starting
3162 // animation bounds for the pinned stack to the desired bounds the caller wants.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003163 resizeStackLocked(stack, task.mBounds, null /* tempTaskBounds */,
Wale Ogunwale480dca02016-02-06 13:58:29 -08003164 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003165 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003166
3167 if (task.mActivities.size() == 1) {
3168 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003169 // the stack without re-parenting the activity in a different task. We don't
3170 // move the home stack forward if we are currently entering picture-in-picture
3171 // while pausing because that changes the focused stack and may prevent the new
3172 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003173 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003174 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003175 // Move the home stack forward if the task we just moved to the pinned stack
3176 // was launched from home so home should be visible behind it.
3177 moveHomeStackToFront(reason);
3178 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003179 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003180 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3181 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003182 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003183 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003184 // reveal/leave the other activities in their original task.
3185
3186 // Currently, we don't support reparenting activities across tasks in two different
3187 // stacks, so instead, just create a new task in the same stack, reparent the
3188 // activity into that task, and then reparent the whole task to the new stack. This
3189 // ensures that all the necessary work to migrate states in the old and new stacks
3190 // is also done.
3191 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003192 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003193 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3194
Winson Chung5af42fc2017-03-24 17:11:33 -07003195 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003196 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003197 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003198 }
Winson Chungc2baac02017-01-11 13:34:47 -08003199
3200 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3201 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003202 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003203 } finally {
3204 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003205 }
3206
Winson Chunge7ba6862017-05-24 12:13:33 -07003207 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3208 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3209 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003210 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3211
Winson Chunge7ba6862017-05-24 12:13:33 -07003212 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3213 true /* fromFullscreen */);
3214
3215 // Update the visibility of all activities after the they have been reparented to the new
3216 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3217 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3218 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003219 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003220 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003221
Wale Ogunwale89be5762017-10-04 13:27:49 -07003222 mService.mTaskChangeNotificationController.notifyActivityPinned(r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003223 }
3224
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003225 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003226 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3227 if (r == null || !r.isFocusable()) {
3228 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3229 "moveActivityStackToFront: unfocusable r=" + r);
3230 return false;
3231 }
3232
Bryce Leeaf691c02017-03-20 14:20:22 -07003233 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003234 final ActivityStack stack = r.getStack();
3235 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003236 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3237 + r + " task=" + task);
3238 return false;
3239 }
3240
Chong Zhang6cda19c2016-06-14 19:07:56 -07003241 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3242 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3243 "moveActivityStackToFront: already on top, r=" + r);
3244 return false;
3245 }
3246
3247 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3248 "moveActivityStackToFront: r=" + r);
3249
3250 stack.moveToFront(reason, task);
3251 return true;
3252 }
3253
David Stevensc6b91c62017-02-08 14:23:58 -08003254 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003255 mTmpFindTaskResult.r = null;
3256 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003257 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003258 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003259 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3260 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003261 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3262 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003263 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003264 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3265 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003266 continue;
3267 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003268 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003269 // It is possible to have tasks in multiple stacks with the same root affinity, so
3270 // we should keep looking after finding an affinity match to see if there is a
3271 // better match in another stack. Also, task affinity isn't a good enough reason
3272 // to target a display which isn't the source of the intent, so skip any affinity
3273 // matches not on the specified display.
3274 if (mTmpFindTaskResult.r != null) {
3275 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3276 return mTmpFindTaskResult.r;
3277 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003278 // Note: since the traversing through the stacks is top down, the floating
3279 // tasks should always have lower priority than any affinity-matching tasks
3280 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003281 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003282 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3283 Slog.d(TAG_TASKS, "Skipping match on different display "
3284 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003285 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003287 }
3288 }
Winson Chung5b895b72017-05-01 13:46:25 -07003289
David Stevensc6b91c62017-02-08 14:23:58 -08003290 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3291 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003292 }
3293
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003294 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3295 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003296 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3297 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003299 final ActivityRecord ar = stacks.get(stackNdx)
3300 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 if (ar != null) {
3302 return ar;
3303 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003304 }
3305 }
3306 return null;
3307 }
3308
David Stevens9440dc82017-03-16 19:00:20 -07003309 boolean hasAwakeDisplay() {
3310 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3311 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3312 if (!display.shouldSleep()) {
3313 return true;
3314 }
3315 }
3316 return false;
3317 }
3318
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003320 scheduleSleepTimeout();
3321 if (!mGoingToSleep.isHeld()) {
3322 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003323 if (mLaunchingActivity.isHeld()) {
3324 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3325 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003326 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003327 mLaunchingActivity.release();
3328 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003329 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003330 }
David Stevens9440dc82017-03-16 19:00:20 -07003331
3332 applySleepTokensLocked(false /* applyToStacks */);
3333
3334 checkReadyForSleepLocked(true /* allowDelay */);
3335 }
3336
3337 void prepareForShutdownLocked() {
3338 for (int i = 0; i < mActivityDisplays.size(); i++) {
3339 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3340 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 }
3342
3343 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003344 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003345
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003346 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003347 final long endTime = System.currentTimeMillis() + timeout;
3348 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003349 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003350 long timeRemaining = endTime - System.currentTimeMillis();
3351 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003352 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003353 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 } catch (InterruptedException e) {
3355 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003356 } else {
3357 Slog.w(TAG, "Activity manager shutdown timed out");
3358 timedout = true;
3359 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003360 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003361 } else {
3362 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003363 }
3364 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003365
3366 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003367 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003368
Craig Mautner8d341ef2013-03-26 09:03:27 -07003369 return timedout;
3370 }
3371
3372 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003373 removeSleepTimeouts();
3374 if (mGoingToSleep.isHeld()) {
3375 mGoingToSleep.release();
3376 }
David Stevens9440dc82017-03-16 19:00:20 -07003377 }
3378
3379 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003380 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003381 // Set the sleeping state of the display.
3382 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3383 final boolean displayShouldSleep = display.shouldSleep();
3384 if (displayShouldSleep == display.isSleeping()) {
3385 continue;
3386 }
3387 display.setIsSleeping(displayShouldSleep);
3388
3389 if (!applyToStacks) {
3390 continue;
3391 }
3392
3393 // Set the sleeping state of the stacks on the display.
3394 final ArrayList<ActivityStack> stacks = display.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3396 final ActivityStack stack = stacks.get(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003397 if (displayShouldSleep) {
3398 stack.goToSleepIfPossible(false /* shuttingDown */);
3399 } else {
3400 stack.awakeFromSleepingLocked();
3401 if (isFocusedStack(stack)) {
3402 resumeFocusedStackTopActivityLocked();
3403 }
3404 }
3405 }
3406
3407 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3408 continue;
3409 }
3410 // The display is awake now, so clean up the going to sleep list.
3411 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3412 final ActivityRecord r = it.next();
3413 if (r.getDisplayId() == display.mDisplayId) {
3414 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 }
Craig Mautner5314a402013-09-26 12:40:16 -07003416 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003418 }
3419
3420 void activitySleptLocked(ActivityRecord r) {
3421 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003422 final ActivityStack s = r.getStack();
3423 if (s != null) {
3424 s.checkReadyForSleep();
3425 } else {
3426 checkReadyForSleepLocked(true);
3427 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 }
3429
David Stevens9440dc82017-03-16 19:00:20 -07003430 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003431 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003432 // Do not care.
3433 return;
3434 }
3435
David Stevens18abd0e2017-08-17 14:55:47 -07003436 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3437 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 }
3439
Wei Wang65c7a152016-06-02 18:51:22 -07003440 // Send launch end powerhint before going sleep
3441 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3442
Craig Mautner0eea92c2013-05-16 13:35:39 -07003443 removeSleepTimeouts();
3444
3445 if (mGoingToSleep.isHeld()) {
3446 mGoingToSleep.release();
3447 }
3448 if (mService.mShuttingDown) {
3449 mService.notifyAll();
3450 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 }
3452
David Stevens18abd0e2017-08-17 14:55:47 -07003453 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3454 // successfully put to sleep.
3455 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3456 boolean allSleep = true;
3457 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3458 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3459 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3460 if (allowDelay) {
3461 allSleep &= stacks.get(stackNdx).goToSleepIfPossible(shuttingDown);
3462 } else {
3463 stacks.get(stackNdx).goToSleep();
3464 }
3465 }
3466 }
3467 return allSleep;
3468 }
3469
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003470 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003471 // A resumed activity cannot be stopping. remove from list
3472 mStoppingActivities.remove(r);
3473
Andrii Kulian02b7a832016-10-06 23:11:56 -07003474 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003475 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003476 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003477 }
3478 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003479 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003480 mWindowManager.executeAppTransition();
3481 return true;
3482 }
3483 return false;
3484 }
3485
Craig Mautner8d341ef2013-03-26 09:03:27 -07003486 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3488 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003489 int stackNdx = stacks.size() - 1;
3490 while (stackNdx >= 0) {
3491 stacks.get(stackNdx).handleAppCrashLocked(app);
3492 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003493 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003494 }
3495 }
3496
Craig Mautnerbb742462014-07-07 15:28:55 -07003497 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003498 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003499 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003500 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003501
3502 r.mLaunchTaskBehind = false;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003503 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003504 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003505 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003506
3507 // When launching tasks behind, update the last active time of the top task after the new
3508 // task has been shown briefly
3509 final ActivityRecord top = stack.topActivity();
3510 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003511 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003512 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003513 }
3514
3515 void scheduleLaunchTaskBehindComplete(IBinder token) {
3516 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3517 }
3518
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003519 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3520 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003521 mKeyguardController.beginActivityVisibilityUpdate();
3522 try {
3523 // First the front stacks. In case any are not fullscreen and are in front of home.
3524 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3525 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3526 final int topStackNdx = stacks.size() - 1;
3527 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3528 final ActivityStack stack = stacks.get(stackNdx);
3529 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3530 }
Craig Mautner580ea812013-04-25 12:58:38 -07003531 }
Jorim Jaggife762342016-10-13 14:33:27 +02003532 } finally {
3533 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003534 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003535 }
3536
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003537 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3538 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3539 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3540 final int topStackNdx = stacks.size() - 1;
3541 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3542 final ActivityStack stack = stacks.get(stackNdx);
3543 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3544 }
3545 }
3546 }
3547
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003548 void invalidateTaskLayers() {
3549 mTaskLayersChanged = true;
3550 }
3551
3552 void rankTaskLayersIfNeeded() {
3553 if (!mTaskLayersChanged) {
3554 return;
3555 }
3556 mTaskLayersChanged = false;
3557 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3558 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3559 int baseLayer = 0;
3560 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3561 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3562 }
3563 }
3564 }
3565
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003566 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3568 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3569 final int topStackNdx = stacks.size() - 1;
3570 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3571 final ActivityStack stack = stacks.get(stackNdx);
3572 stack.clearOtherAppTimeTrackers(except);
3573 }
3574 }
3575 }
3576
Craig Mautner8d341ef2013-03-26 09:03:27 -07003577 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003578 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3579 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003580 final int numStacks = stacks.size();
3581 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3582 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003583 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003584 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003585 }
3586 }
3587
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003588 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3589 // Examine all activities currently running in the process.
3590 TaskRecord firstTask = null;
3591 // Tasks is non-null only if two or more tasks are found.
3592 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003593 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3594 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003595 ActivityRecord r = app.activities.get(i);
3596 // First, if we find an activity that is in the process of being destroyed,
3597 // then we just aren't going to do anything for now; we want things to settle
3598 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003599 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003600 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003601 return;
3602 }
3603 // Don't consider any activies that are currently not in a state where they
3604 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003605 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3606 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003607 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003608 continue;
3609 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003610
3611 final TaskRecord task = r.getTask();
3612 if (task != null) {
3613 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003614 + " from " + r);
3615 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003616 firstTask = task;
3617 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003618 if (tasks == null) {
3619 tasks = new ArraySet<>();
3620 tasks.add(firstTask);
3621 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003622 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003623 }
3624 }
3625 }
3626 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003627 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003628 return;
3629 }
3630 // If we have activities in multiple tasks that are in a position to be destroyed,
3631 // let's iterate through the tasks and release the oldest one.
3632 final int numDisplays = mActivityDisplays.size();
3633 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3634 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3635 // Step through all stacks starting from behind, to hit the oldest things first.
3636 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3637 final ActivityStack stack = stacks.get(stackNdx);
3638 // Try to release activities in this stack; if we manage to, we are done.
3639 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3640 return;
3641 }
3642 }
3643 }
3644 }
3645
Amith Yamasani37a40c22015-06-17 13:25:42 -07003646 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003647 final int focusStackId = mFocusedStack.getStackId();
3648 // We dismiss the docked stack whenever we switch users.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003649 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003650 if (dockedStack != null) {
3651 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3652 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003653 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3654 // also cause all tasks to be moved to the fullscreen stack at a position that is
3655 // appropriate.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003656 removeStacksInWindowingModes(WINDOWING_MODE_PINNED);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003657
3658 mUserStackInFront.put(mCurrentUser, focusStackId);
Wale Ogunwale68278562017-09-23 17:13:55 -07003659 final int restoreStackId = mUserStackInFront.get(userId, mHomeStack.mStackId);
Craig Mautner2420ead2013-04-01 17:13:20 -07003660 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003661
Craig Mautner858d8a62013-04-23 17:08:34 -07003662 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3664 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003665 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003666 final ActivityStack stack = stacks.get(stackNdx);
3667 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003668 TaskRecord task = stack.topTask();
3669 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003670 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003671 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003672 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003673 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003674
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003675 ActivityStack stack = getStack(restoreStackId);
3676 if (stack == null) {
3677 stack = mHomeStack;
3678 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003679 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003680 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003681 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003682 } else {
3683 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003684 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003685 }
Craig Mautner93529a42013-10-04 15:03:13 -07003686 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003687 }
3688
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003689 /** Checks whether the userid is a profile of the current user. */
3690 boolean isCurrentProfileLocked(int userId) {
3691 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003692 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003693 }
3694
Bryce Leeb7c9b802017-05-02 14:20:24 -07003695 /**
3696 * Returns whether a stopping activity is present that should be stopped after visible, rather
3697 * than idle.
3698 * @return {@code true} if such activity is present. {@code false} otherwise.
3699 */
3700 boolean isStoppingNoHistoryActivity() {
3701 // Activities that are marked as nohistory should be stopped immediately after the resumed
3702 // activity has become visible.
3703 for (ActivityRecord record : mStoppingActivities) {
3704 if (record.isNoHistory()) {
3705 return true;
3706 }
3707 }
3708
3709 return false;
3710 }
3711
Winson Chung4dabf232017-01-25 13:25:22 -08003712 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3713 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003714 ArrayList<ActivityRecord> stops = null;
3715
3716 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003717 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3718 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003719 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003720 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003721 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3722 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003723 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003724 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003725 if (s.finishing) {
3726 // If this activity is finishing, it is sitting on top of
3727 // everyone else but we now know it is no longer needed...
3728 // so get rid of it. Otherwise, we need to go through the
3729 // normal flow and hide it once we determine that it is
3730 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003731 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003732 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003733 }
3734 }
David Stevens9440dc82017-03-16 19:00:20 -07003735 if (remove) {
3736 final ActivityStack stack = s.getStack();
3737 final boolean shouldSleepOrShutDown = stack != null
3738 ? stack.shouldSleepOrShutDownActivities()
3739 : mService.isSleepingOrShuttingDownLocked();
3740 if (!waitingVisible || shouldSleepOrShutDown) {
3741 if (!processPausingActivities && s.state == PAUSING) {
3742 // Defer processing pausing activities in this iteration and reschedule
3743 // a delayed idle to reprocess it again
3744 removeTimeoutsForActivityLocked(idleActivity);
3745 scheduleIdleTimeoutLocked(idleActivity);
3746 continue;
3747 }
Winson Chung4dabf232017-01-25 13:25:22 -08003748
David Stevens9440dc82017-03-16 19:00:20 -07003749 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3750 if (stops == null) {
3751 stops = new ArrayList<>();
3752 }
3753 stops.add(s);
3754 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003755 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003756 }
3757 }
3758
3759 return stops;
3760 }
3761
Craig Mautnercf910b02013-04-23 11:23:27 -07003762 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003763 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3764 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3765 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3766 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003767 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003768 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003769 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003770 if (r == null) Slog.e(TAG,
3771 "validateTop...: null top activity, stack=" + stack);
3772 else {
3773 final ActivityRecord pausing = stack.mPausingActivity;
3774 if (pausing != null && pausing == r) Slog.e(TAG,
3775 "validateTop...: top stack has pausing activity r=" + r
3776 + " state=" + state);
3777 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3778 "validateTop...: activity in front not resumed r=" + r
3779 + " state=" + state);
3780 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003781 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003782 final ActivityRecord resumed = stack.mResumedActivity;
3783 if (resumed != null && resumed == r) Slog.e(TAG,
3784 "validateTop...: back stack has resumed activity r=" + r
3785 + " state=" + state);
3786 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3787 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003788 }
3789 }
3790 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003791 }
3792
Craig Mautner27084302013-03-25 08:05:25 -07003793 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003794 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003796 pw.print(prefix);
3797 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003798 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003799 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3800 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3801 pw.println(prefix + "displayId=" + display.mDisplayId + " mStacks=" + display.mStacks);
3802 }
Bryce Lee4a194382017-04-04 14:32:48 -07003803 if (!mWaitingForActivityVisible.isEmpty()) {
3804 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3805 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3806 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3807 }
3808 }
3809
Jorim Jaggi8d786932016-10-26 19:08:36 -07003810 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003811 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003812 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003813
Steven Timotius4346f0a2017-09-12 11:07:21 -07003814 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3815 final long token = proto.start(fieldId);
3816 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3817 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3818 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3819 activityDisplay.writeToProto(proto, DISPLAYS);
3820 }
3821 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3822 if (mFocusedStack != null) {
3823 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3824 ActivityRecord focusedActivity = getResumedActivityLocked();
3825 if (focusedActivity != null) {
3826 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3827 }
3828 } else {
3829 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3830 }
3831 proto.end(token);
3832 }
3833
Winson43d1f262016-06-14 16:05:55 -07003834 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003835 * Dump all connected displays' configurations.
3836 * @param prefix Prefix to apply to each line of the dump.
3837 */
3838 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3839 pw.print(prefix); pw.println("Display override configurations:");
3840 final int displayCount = mActivityDisplays.size();
3841 for (int i = 0; i < displayCount; i++) {
3842 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3843 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3844 pw.println(activityDisplay.getOverrideConfiguration());
3845 }
3846 }
3847
3848 /**
Winson43d1f262016-06-14 16:05:55 -07003849 * Dumps the activities matching the given {@param name} in the either the focused stack
3850 * or all visible stacks if {@param dumpVisibleStacks} is true.
3851 */
Winson Chung6998bc42017-02-28 17:07:05 -08003852 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3853 boolean dumpFocusedStackOnly) {
3854 if (dumpFocusedStackOnly) {
3855 return mFocusedStack.getDumpActivitiesLocked(name);
3856 } else {
Winson43d1f262016-06-14 16:05:55 -07003857 ArrayList<ActivityRecord> activities = new ArrayList<>();
3858 int numDisplays = mActivityDisplays.size();
3859 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3860 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3861 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3862 ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003863 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003864 activities.addAll(stack.getDumpActivitiesLocked(name));
3865 }
3866 }
3867 }
3868 return activities;
Winson43d1f262016-06-14 16:05:55 -07003869 }
Craig Mautner20e72272013-04-01 13:45:53 -07003870 }
3871
Dianne Hackborn390517b2013-05-30 15:03:32 -07003872 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3873 boolean needSep, String prefix) {
3874 if (activity != null) {
3875 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3876 if (needSep) {
3877 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003878 }
3879 pw.print(prefix);
3880 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003881 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003882 }
3883 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003884 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003885 }
3886
Craig Mautner8d341ef2013-03-26 09:03:27 -07003887 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3888 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003889 boolean printed = false;
3890 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003891 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3892 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003893 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003894 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003895 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003896 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003897 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003898 pw.println();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003899 pw.println(" Stack #" + stack.mStackId
3900 + ": type=" + activityTypeToString(getActivityType())
3901 + " mode=" + windowingModeToString(getWindowingMode()));
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003902 pw.println(" mFullscreen=" + stack.mFullscreen);
3903 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3904 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003905
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003906 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3907 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003908
Craig Mautner4a1cb222013-12-04 16:14:06 -08003909 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3910 !dumpAll, false, dumpPackage, true,
3911 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003912
Craig Mautner4a1cb222013-12-04 16:14:06 -08003913 needSep = printed;
3914 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3915 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003916 if (pr) {
3917 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003918 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003919 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003920 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3921 " mResumedActivity: ");
3922 if (pr) {
3923 printed = true;
3924 needSep = false;
3925 }
3926 if (dumpAll) {
3927 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3928 " mLastPausedActivity: ");
3929 if (pr) {
3930 printed = true;
3931 needSep = true;
3932 }
3933 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3934 needSep, " mLastNoHistoryActivity: ");
3935 }
3936 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003937 }
3938 }
3939
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003940 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3941 false, dumpPackage, true, " Activities waiting to finish:", null);
3942 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3943 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003944 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3945 false, !dumpAll, false, dumpPackage, true,
3946 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003947 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3948 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003949
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003950 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003951 }
3952
Dianne Hackborn390517b2013-05-30 15:03:32 -07003953 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003954 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003955 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003956 String innerPrefix = null;
3957 String[] args = null;
3958 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003959 for (int i=list.size()-1; i>=0; i--) {
3960 final ActivityRecord r = list.get(i);
3961 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3962 continue;
3963 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003964 if (innerPrefix == null) {
3965 innerPrefix = prefix + " ";
3966 args = new String[0];
3967 }
3968 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003969 final boolean full = !brief && (complete || !r.isInHistory());
3970 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003971 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003972 needNL = false;
3973 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003974 if (header != null) {
3975 pw.println(header);
3976 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003977 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003978 if (lastTask != r.getTask()) {
3979 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003980 pw.print(prefix);
3981 pw.print(full ? "* " : " ");
3982 pw.println(lastTask);
3983 if (full) {
3984 lastTask.dump(pw, prefix + " ");
3985 } else if (complete) {
3986 // Complete + brief == give a summary. Isn't that obvious?!?
3987 if (lastTask.intent != null) {
3988 pw.print(prefix); pw.print(" ");
3989 pw.println(lastTask.intent.toInsecureStringWithClip());
3990 }
3991 }
3992 }
3993 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3994 pw.print(" #"); pw.print(i); pw.print(": ");
3995 pw.println(r);
3996 if (full) {
3997 r.dump(pw, innerPrefix);
3998 } else if (complete) {
3999 // Complete + brief == give a summary. Isn't that obvious?!?
4000 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4001 if (r.app != null) {
4002 pw.print(innerPrefix); pw.println(r.app);
4003 }
4004 }
4005 if (client && r.app != null && r.app.thread != null) {
4006 // flush anything that is already in the PrintWriter since the thread is going
4007 // to write to the file descriptor directly
4008 pw.flush();
4009 try {
4010 TransferPipe tp = new TransferPipe();
4011 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07004012 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004013 // Short timeout, since blocking here can
4014 // deadlock with the application.
4015 tp.go(fd, 2000);
4016 } finally {
4017 tp.kill();
4018 }
4019 } catch (IOException e) {
4020 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4021 } catch (RemoteException e) {
4022 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4023 }
4024 needNL = true;
4025 }
4026 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004027 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004028 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004029
Craig Mautnerf3333272013-04-22 10:55:53 -07004030 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004031 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4032 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004033 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4034 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004035 }
4036
4037 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004038 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004039 }
4040
4041 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004042 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4043 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004044 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4045 }
4046
Craig Mautner05d29032013-05-03 13:40:13 -07004047 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004048 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4049 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4050 }
Craig Mautner05d29032013-05-03 13:40:13 -07004051 }
4052
Craig Mautner0eea92c2013-05-16 13:35:39 -07004053 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004054 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4055 }
4056
4057 final void scheduleSleepTimeout() {
4058 removeSleepTimeouts();
4059 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4060 }
4061
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 @Override
4063 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004064 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004065 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4066 }
4067
4068 @Override
4069 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004070 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004071 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4072 }
4073
4074 @Override
4075 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004076 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004077 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4078 }
4079
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004080 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004081 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004082 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004084 }
4085
Andrii Kulianca007a62016-11-22 16:33:28 -08004086 /** Check if display with specified id is added to the list. */
4087 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004088 return getActivityDisplayOrCreateLocked(displayId) != null;
4089 }
4090
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004091 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004092 ActivityDisplay getActivityDisplay(int displayId) {
4093 return mActivityDisplays.get(displayId);
4094 }
4095
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004096 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4097 ActivityDisplay getDefaultDisplay() {
4098 return mActivityDisplays.get(DEFAULT_DISPLAY);
4099 }
4100
Andrii Kulian62e6f252017-05-30 22:46:53 -07004101 /**
4102 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4103 * corresponding record in display manager.
4104 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004105 // TODO: Look into consolidating with getActivityDisplay()
4106 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004107 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4108 if (activityDisplay != null) {
4109 return activityDisplay;
4110 }
4111 if (mDisplayManager == null) {
4112 // The system isn't fully initialized yet.
4113 return null;
4114 }
4115 final Display display = mDisplayManager.getDisplay(displayId);
4116 if (display == null) {
4117 // The display is not registered in DisplayManager.
4118 return null;
4119 }
4120 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004121 activityDisplay = new ActivityDisplay(this, displayId);
4122 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004123 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4124 mWindowManager.onDisplayAdded(displayId);
4125 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004126 }
4127
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004128 @VisibleForTesting
4129 void attachDisplay(ActivityDisplay display) {
4130 mActivityDisplays.put(display.mDisplayId, display);
4131 }
4132
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004133 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004134 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004135 mService.mContext.getResources().getDimensionPixelSize(
4136 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004137 }
4138
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004139 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004140 if (displayId == DEFAULT_DISPLAY) {
4141 throw new IllegalArgumentException("Can't remove the primary display.");
4142 }
4143
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004145 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4146 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004147 final boolean destroyContentOnRemoval
4148 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004149 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4150 while (!stacks.isEmpty()) {
4151 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004152 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004153 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4154 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004155 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004156 } else {
4157 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4158 // a valid launch stack for all activities. This way the task history from
4159 // external display will be preserved on primary after move.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004160 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004161 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 }
David Stevens9440dc82017-03-16 19:00:20 -07004163
4164 releaseSleepTokens(activityDisplay);
4165
Craig Mautnere0a38842013-12-16 16:14:02 -08004166 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004167 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 }
4169 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004170 }
4171
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004172 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004173 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004174 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4175 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004176 // The window policy is responsible for stopping activities on the default display
4177 if (displayId != Display.DEFAULT_DISPLAY) {
4178 int displayState = activityDisplay.mDisplay.getState();
4179 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4180 activityDisplay.mOffToken =
4181 mService.acquireSleepToken("Display-off", displayId);
4182 } else if (displayState == Display.STATE_ON
4183 && activityDisplay.mOffToken != null) {
4184 activityDisplay.mOffToken.release();
4185 activityDisplay.mOffToken = null;
4186 }
4187 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 // TODO: Update the bounds.
4189 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004190 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004191 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 }
4193
David Stevens9440dc82017-03-16 19:00:20 -07004194 SleepToken createSleepTokenLocked(String tag, int displayId) {
4195 ActivityDisplay display = mActivityDisplays.get(displayId);
4196 if (display == null) {
4197 throw new IllegalArgumentException("Invalid display: " + displayId);
4198 }
4199
4200 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4201 mSleepTokens.add(token);
4202 display.mAllSleepTokens.add(token);
4203 return token;
4204 }
4205
4206 private void removeSleepTokenLocked(SleepTokenImpl token) {
4207 mSleepTokens.remove(token);
4208
4209 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4210 if (display != null) {
4211 display.mAllSleepTokens.remove(token);
4212 if (display.mAllSleepTokens.isEmpty()) {
4213 mService.updateSleepIfNeededLocked();
4214 }
4215 }
4216 }
4217
4218 private void releaseSleepTokens(ActivityDisplay display) {
4219 if (display.mAllSleepTokens.isEmpty()) {
4220 return;
4221 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004222 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004223 mSleepTokens.remove(token);
4224 }
4225 display.mAllSleepTokens.clear();
4226
4227 mService.updateSleepIfNeededLocked();
4228 }
4229
Wale Ogunwale68278562017-09-23 17:13:55 -07004230 private StackInfo getStackInfo(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004231 final int displayId = stack.mDisplayId;
4232 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004234 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004235 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004236 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004237 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004238 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004239 // A stack might be not attached to a display.
Wale Ogunwale68278562017-09-23 17:13:55 -07004240 info.position = display != null ? display.mStacks.indexOf(stack) : 0;
4241 info.configuration.setTo(stack.getConfiguration());
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242
4243 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4244 final int numTasks = tasks.size();
4245 int[] taskIds = new int[numTasks];
4246 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004247 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004248 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004249 for (int i = 0; i < numTasks; ++i) {
4250 final TaskRecord task = tasks.get(i);
4251 taskIds[i] = task.taskId;
4252 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4253 : task.realActivity != null ? task.realActivity.flattenToString()
4254 : task.getTopActivity() != null ? task.getTopActivity().packageName
4255 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004256 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004257 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004258 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 }
4260 info.taskIds = taskIds;
4261 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004262 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004263 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004264
4265 final ActivityRecord top = stack.topRunningActivityLocked();
4266 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 return info;
4268 }
4269
Wale Ogunwale68278562017-09-23 17:13:55 -07004270 StackInfo getStackInfo(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004271 ActivityStack stack = getStack(stackId);
4272 if (stack != null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004273 return getStackInfo(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004274 }
4275 return null;
4276 }
4277
Wale Ogunwale68278562017-09-23 17:13:55 -07004278 StackInfo getStackInfo(int windowingMode, int activityType) {
4279 final ActivityStack stack = getStack(windowingMode, activityType);
4280 return (stack != null) ? getStackInfo(stack) : null;
4281 }
4282
Craig Mautner4a1cb222013-12-04 16:14:06 -08004283 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004284 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004285 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4286 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004287 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004288 list.add(getStackInfo(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004289 }
4290 }
4291 return list;
4292 }
4293
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004294 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004295 int preferredDisplayId, int actualStackId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004296 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
4297 actualStackId, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004298 }
4299
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004300 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004301 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4302 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004303 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004304 final ActivityStack actualStack = getStack(actualStackId);
4305 final boolean inSplitScreenMode = actualStack != null
4306 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004307 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004308 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004309 return;
4310 }
4311
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004312 // Handle incorrect launch/move to secondary display if needed.
4313 final boolean launchOnSecondaryDisplayFailed;
4314 if (isSecondaryDisplayPreferred) {
4315 final int actualDisplayId = task.getStack().mDisplayId;
4316 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4317 // The task landed on an inappropriate display somehow, move it to the default
4318 // display.
4319 // TODO(multi-display): Find proper stack for the task on the default display.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004320 mService.setTaskWindowingMode(task.taskId,
4321 WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY, true /* toTop */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004322 launchOnSecondaryDisplayFailed = true;
4323 } else {
4324 // The task might have landed on a display different from requested.
4325 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4326 || (preferredDisplayId != INVALID_DISPLAY
4327 && preferredDisplayId != actualDisplayId);
4328 }
4329 } else {
4330 // The task wasn't requested to be on a secondary display.
4331 launchOnSecondaryDisplayFailed = false;
4332 }
4333
Jorim Jaggicd13d332016-04-27 15:40:20 -07004334 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004335 if (launchOnSecondaryDisplayFailed
4336 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004337 if (launchOnSecondaryDisplayFailed) {
4338 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4339 // display with config different from global config.
4340 mService.mTaskChangeNotificationController
4341 .notifyActivityLaunchOnSecondaryDisplayFailed();
4342 } else {
4343 // Display a warning toast that we tried to put a non-dockable task in the docked
4344 // stack.
4345 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4346 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004347
Andrii Kulianc27916642016-04-12 17:59:27 -07004348 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4349 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004350
4351 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004352 if (dockedStack != null) {
4353 moveTasksToFullscreenStackLocked(dockedStack,
4354 actualStackId == dockedStack.getStackId());
4355 }
Winson Chungd3395382016-12-13 11:49:09 -08004356 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004357 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004358 final String packageName = topActivity.appInfo.packageName;
4359 final int reason = isSecondaryDisplayPreferred
4360 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4361 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004362 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004363 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004364 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004365 }
4366
Jorim Jaggife89d122015-12-22 16:28:44 +01004367 void activityRelaunchedLocked(IBinder token) {
4368 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004369 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4370 if (r != null) {
4371 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004372 r.setSleeping(true, true);
4373 }
4374 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004375 }
4376
4377 void activityRelaunchingLocked(ActivityRecord r) {
4378 mWindowManager.notifyAppRelaunching(r.appToken);
4379 }
4380
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004381 void logStackState() {
4382 mActivityMetricsLogger.logWindowState();
4383 }
4384
Winson Chung5af42fc2017-03-24 17:11:33 -07004385 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004386 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4387 // end, then ensure that we defer all in between multi-window mode changes
4388 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4389 return;
4390 }
4391
Wale Ogunwale22e25262016-02-01 10:32:02 -08004392 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4393 final ActivityRecord r = task.mActivities.get(i);
4394 if (r.app != null && r.app.thread != null) {
4395 mMultiWindowModeChangedActivities.add(r);
4396 }
4397 }
4398
4399 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4400 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4401 }
4402 }
4403
Winson Chung5af42fc2017-03-24 17:11:33 -07004404 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004405 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004406 if (prevStack == null || prevStack == stack
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004407 || (!prevStack.inPinnedWindowingMode() && !stack.inPinnedWindowingMode())) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004408 return;
4409 }
4410
Winson Chungab76bbc2017-08-14 13:33:51 -07004411 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004412 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004413
Winson Chungab76bbc2017-08-14 13:33:51 -07004414 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4415 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4416 final ActivityRecord r = task.mActivities.get(i);
4417 if (r.app != null && r.app.thread != null) {
4418 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004419 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004420 }
4421 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004422
Winson Chungab76bbc2017-08-14 13:33:51 -07004423 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4424 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4425 }
4426 }
4427
4428 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4429 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4430 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4431 final ActivityRecord r = task.mActivities.get(i);
4432 if (r.app != null && r.app.thread != null) {
4433 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004434 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004435 }
4436 }
4437
Tony Mak853304c2016-04-18 15:17:41 +01004438 void setDockedStackMinimized(boolean minimized) {
4439 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004440 }
4441
chaviw59b98852017-06-13 12:05:44 -07004442 void wakeUp(String reason) {
4443 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4444 }
4445
4446 /**
4447 * Begin deferring resume to avoid duplicate resumes in one pass.
4448 */
4449 private void beginDeferResume() {
4450 mDeferResumeCount++;
4451 }
4452
4453 /**
4454 * End deferring resume and determine if resume can be called.
4455 */
4456 private void endDeferResume() {
4457 mDeferResumeCount--;
4458 }
4459
4460 /**
4461 * @return True if resume can be called.
4462 */
4463 private boolean readyToResume() {
4464 return mDeferResumeCount == 0;
4465 }
4466
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004467 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004468
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004469 public ActivityStackSupervisorHandler(Looper looper) {
4470 super(looper);
4471 }
4472
Winson Chung4dabf232017-01-25 13:25:22 -08004473 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004474 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004475 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4476 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004477 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004478 }
4479
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004480 @Override
4481 public void handleMessage(Message msg) {
4482 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004483 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4484 synchronized (mService) {
4485 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4486 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004487 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004488 }
4489 }
4490 } break;
4491 case REPORT_PIP_MODE_CHANGED_MSG: {
4492 synchronized (mService) {
4493 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4494 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004495 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4496 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004497 }
4498 }
4499 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004500 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004501 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4502 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004503 // We don't at this point know if the activity is fullscreen,
4504 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004505 activityIdleInternal((ActivityRecord) msg.obj,
4506 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004507 } break;
4508 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004509 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004510 activityIdleInternal((ActivityRecord) msg.obj,
4511 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004512 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004513 case RESUME_TOP_ACTIVITY_MSG: {
4514 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004515 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004516 }
4517 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004518 case SLEEP_TIMEOUT_MSG: {
4519 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004520 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004521 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004522 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004523 }
4524 }
4525 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004526 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004527 synchronized (mService) {
4528 if (mLaunchingActivity.isHeld()) {
4529 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4530 if (VALIDATE_WAKE_LOCK_CALLER
4531 && Binder.getCallingUid() != Process.myUid()) {
4532 throw new IllegalStateException("Calling must be system uid");
4533 }
4534 mLaunchingActivity.release();
4535 }
4536 }
4537 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004538 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004539 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004540 } break;
4541 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004542 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004543 } break;
4544 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004545 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004547 case LAUNCH_TASK_BEHIND_COMPLETE: {
4548 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004549 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004550 if (r != null) {
4551 handleLaunchTaskBehindCompleteLocked(r);
4552 }
4553 }
4554 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004555
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004556 }
4557 }
4558 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004559
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004560 ActivityStack findStackBehind(ActivityStack stack) {
4561 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004562 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004563 if (display == null) {
4564 return null;
4565 }
4566 final ArrayList<ActivityStack> stacks = display.mStacks;
4567 for (int i = stacks.size() - 1; i >= 0; i--) {
4568 if (stacks.get(i) == stack && i > 0) {
4569 return stacks.get(i - 1);
4570 }
4571 }
4572 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4573 + " in=" + stacks);
4574 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004575
Jorim Jaggic69bd222016-03-15 14:38:37 +01004576 /**
4577 * Puts a task into resizing mode during the next app transition.
4578 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004579 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004580 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004581 private void setResizingDuringAnimation(TaskRecord task) {
4582 mResizingTasksDuringAnimation.add(task.taskId);
4583 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004584 }
4585
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004586 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4587 final TaskRecord task;
4588 final int callingUid;
4589 final String callingPackage;
4590 final Intent intent;
4591 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004592 int activityType = ACTIVITY_TYPE_UNDEFINED;
4593 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004594 final ActivityOptions activityOptions = (bOptions != null)
4595 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004596 if (activityOptions != null) {
4597 activityType = activityOptions.getLaunchActivityType();
4598 windowingMode = activityOptions.getLaunchWindowingMode();
4599 }
4600 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004601 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004602 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004603 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004604
Matthew Ng606dd802017-06-05 14:06:32 -07004605 mWindowManager.deferSurfaceLayout();
4606 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004607 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004608 mWindowManager.setDockedStackCreateState(
4609 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004610
Matthew Ng606dd802017-06-05 14:06:32 -07004611 // Defer updating the stack in which recents is until the app transition is done, to
4612 // not run into issues where we still need to draw the task in recents but the
4613 // docked stack is already created.
Wale Ogunwale68278562017-09-23 17:13:55 -07004614 deferUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004615 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004616 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004617
Matthew Ng606dd802017-06-05 14:06:32 -07004618 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004619 activityOptions);
Matthew Ng606dd802017-06-05 14:06:32 -07004620 if (task == null) {
Wale Ogunwale68278562017-09-23 17:13:55 -07004621 continueUpdateBounds(ACTIVITY_TYPE_RECENTS);
Matthew Ng606dd802017-06-05 14:06:32 -07004622 mWindowManager.executeAppTransition();
4623 throw new IllegalArgumentException(
4624 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4625 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004626
Matthew Ng606dd802017-06-05 14:06:32 -07004627 // Since we don't have an actual source record here, we assume that the currently
4628 // focused activity was the source.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004629 final ActivityStack stack = getLaunchStack(null, activityOptions, task, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004630
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004631 if (stack != null && task.getStack() != stack) {
4632 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004633 "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004634 }
4635
4636 // If the user must confirm credentials (e.g. when first launching a work app and the
4637 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4638 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4639 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004640 final ActivityRecord targetActivity = task.getTopActivity();
4641
4642 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4643 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004644 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004645 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004646 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004647 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004648
4649 // If we are launching the task in the docked stack, put it into resizing mode so
4650 // the window renders full-screen with the background filling the void. Also only
4651 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004652 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004653 setResizingDuringAnimation(task);
4654 }
4655
4656 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004657 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004658 return ActivityManager.START_TASK_TO_FRONT;
4659 }
4660 callingUid = task.mCallingUid;
4661 callingPackage = task.mCallingPackage;
4662 intent = task.intent;
4663 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4664 userId = task.userId;
4665 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004666 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004667 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004668 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004669 }
Matthew Ng606dd802017-06-05 14:06:32 -07004670 return result;
4671 } finally {
4672 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004673 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004674 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004675
4676 /**
4677 * @return a list of activities which are the top ones in each visible stack. The first
4678 * entry will be the focused activity.
4679 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004680 List<IBinder> getTopVisibleActivities() {
4681 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4682 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004683 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004684 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4685 // Traverse all stacks on a display.
4686 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4687 final ActivityStack stack = display.mStacks.get(j);
4688 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004689 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004690 final ActivityRecord top = stack.topActivity();
4691 if (top != null) {
4692 if (stack == mFocusedStack) {
4693 topActivityTokens.add(0, top.appToken);
4694 } else {
4695 topActivityTokens.add(top.appToken);
4696 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004697 }
4698 }
4699 }
4700 }
4701 return topActivityTokens;
4702 }
Bryce Lee4a194382017-04-04 14:32:48 -07004703
4704 /**
4705 * Internal container to store a match qualifier alongside a WaitResult.
4706 */
4707 static class WaitInfo {
4708 private final ComponentName mTargetComponent;
4709 private final WaitResult mResult;
4710
4711 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4712 this.mTargetComponent = targetComponent;
4713 this.mResult = result;
4714 }
4715
Wale Ogunwale3270f172017-04-26 07:29:42 -07004716 public boolean matches(ComponentName targetComponent) {
4717 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004718 }
4719
4720 public WaitResult getResult() {
4721 return mResult;
4722 }
4723
4724 public ComponentName getComponent() {
4725 return mTargetComponent;
4726 }
4727
4728 public void dump(PrintWriter pw, String prefix) {
4729 pw.println(prefix + "WaitInfo:");
4730 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4731 pw.println(prefix + " mResult=");
4732 mResult.dump(pw, prefix);
4733 }
4734 }
David Stevens9440dc82017-03-16 19:00:20 -07004735
4736 private final class SleepTokenImpl extends SleepToken {
4737 private final String mTag;
4738 private final long mAcquireTime;
4739 private final int mDisplayId;
4740
4741 public SleepTokenImpl(String tag, int displayId) {
4742 mTag = tag;
4743 mDisplayId = displayId;
4744 mAcquireTime = SystemClock.uptimeMillis();
4745 }
4746
4747 @Override
4748 public void release() {
4749 synchronized (mService) {
4750 removeSleepTokenLocked(this);
4751 }
4752 }
4753
4754 @Override
4755 public String toString() {
4756 return "{\"" + mTag + "\", display " + mDisplayId
4757 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4758 }
4759 }
4760
Craig Mautner27084302013-03-25 08:05:25 -07004761}