blob: 424caa00d0566d20521ec0ad28447914ad7d0ffb [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;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070024import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
28import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.HOME_STACK_ID;
30import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020031import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080032import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070033import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
34import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070035import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070036import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070037import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
38import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070039import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070040import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070041import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
42import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070043import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070044import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070045import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020046import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070047import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010048import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020049import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
50import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070051import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070052import static android.view.Display.TYPE_VIRTUAL;
Winson Chung47900652017-04-06 18:44:25 -070053import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020057import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
61import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
63import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020064import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020066import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
68import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
69import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
70import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
72import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
74import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
75import static com.android.server.am.ActivityManagerService.ANIMATE;
76import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020077import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
78import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
79import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
80import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
81import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
82import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
83import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
84import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
85import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070086import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020087import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
88import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Winson Chung74666102017-02-22 17:49:24 -080089import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
90import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
91import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Steven Timotius4346f0a2017-09-12 11:07:21 -070092import static com.android.server.am.proto.ActivityStackSupervisorProto.DISPLAYS;
93import static com.android.server.am.proto.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
94import static com.android.server.am.proto.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
95import static com.android.server.am.proto.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
96import static com.android.server.am.proto.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
Jorim Jaggife762342016-10-13 14:33:27 +020097import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080098import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +020099
Svetoslav7008b512015-06-24 18:47:07 -0700100import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800101import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700102import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700103import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100104import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700105import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800107import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700108import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800109import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700110import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700111import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700112import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700113import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700115import android.app.WaitResult;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700116import android.app.WindowConfiguration;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118import android.content.Context;
119import android.content.Intent;
120import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700121import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700122import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.PackageManager;
124import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100125import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700126import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800127import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128import android.hardware.display.DisplayManager;
129import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800130import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800131import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700132import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100133import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700134import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700135import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700136import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700137import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700138import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700139import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700140import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700142import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700143import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700144import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100145import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700146import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700147import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700148import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700150import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700151import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800152import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700153import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700154import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800155import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800156import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700157import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700158import android.util.proto.ProtoOutputStream;
Craig Mautnered6649f2013-12-02 14:08:25 -0800159import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800160
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700161import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800162import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800163import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700164import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700165import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800166import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700167import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700168import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700169import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172import java.io.FileDescriptor;
173import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700174import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800175import java.lang.annotation.Retention;
176import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700177import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700178import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700179import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700180import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700181
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800182public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800183 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700184 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700185 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700186 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700190 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700191 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800192 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193
Craig Mautnerf3333272013-04-22 10:55:53 -0700194 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700195 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700196
Craig Mautner0eea92c2013-05-16 13:35:39 -0700197 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700198 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700199
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700200 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700201 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700202
Craig Mautner05d29032013-05-03 13:40:13 -0700203 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
204 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
205 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700206 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800208 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
209 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
210 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700211 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800212 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
213 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800214
Wale Ogunwale040b4702015-08-06 18:10:50 -0700215 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700216
Wale Ogunwale040b4702015-08-06 18:10:50 -0700217 // Used to indicate if an object (e.g. stack) that we are trying to get
218 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800219 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700220
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700221 // Used to indicate that windows of activities should be preserved during the resize.
222 static final boolean PRESERVE_WINDOWS = true;
223
Wale Ogunwale040b4702015-08-06 18:10:50 -0700224 // Used to indicate if an object (e.g. task) should be moved/created
225 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700226 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700227
228 // Used to indicate that an objects (e.g. task) removal from its container
229 // (e.g. stack) is due to it moving to another container.
230 static final boolean MOVING = true;
231
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700232 // Force the focus to change to the stack we are moving a task to..
233 static final boolean FORCE_FOCUS = true;
234
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700235 // Don't execute any calls to resume.
236 static final boolean DEFER_RESUME = true;
237
Winson Chung010927a2016-12-15 16:12:35 -0800238 // Used to indicate that a task is removed it should also be removed from recents.
239 static final boolean REMOVE_FROM_RECENTS = true;
240
Winson Chung6954fc92017-03-24 16:22:12 -0700241 // Used to indicate that pausing an activity should occur immediately without waiting for
242 // the activity callback indicating that it has completed pausing
243 static final boolean PAUSE_IMMEDIATELY = true;
244
Winson Chung7900bee2017-03-10 08:59:25 -0800245 /**
246 * The modes which affect which tasks are returned when calling
247 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
248 */
249 @Retention(RetentionPolicy.SOURCE)
250 @IntDef({
251 MATCH_TASK_IN_STACKS_ONLY,
252 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
253 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
254 })
255 public @interface AnyTaskForIdMatchTaskMode {}
256 // Match only tasks in the current stacks
257 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
258 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
259 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
260 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
261 // provided stack id
262 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
263
Svetoslav7008b512015-06-24 18:47:07 -0700264 // Activity actions an app cannot start if it uses a permission which is not granted.
265 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
266 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700267
Svetoslav7008b512015-06-24 18:47:07 -0700268 static {
269 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
270 Manifest.permission.CAMERA);
271 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
272 Manifest.permission.CAMERA);
273 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
274 Manifest.permission.CALL_PHONE);
275 }
276
Svet Ganov99b60432015-06-27 13:15:22 -0700277 /** Action restriction: launching the activity is not restricted. */
278 private static final int ACTIVITY_RESTRICTION_NONE = 0;
279 /** Action restriction: launching the activity is restricted by a permission. */
280 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
281 /** Action restriction: launching the activity is restricted by an app op. */
282 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700283
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700284 // For debugging to make sure the caller when acquiring/releasing our
285 // wake lock is the system process.
286 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800287 /** The number of distinct task ids that can be assigned to the tasks of a single user */
288 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700289
Craig Mautner27084302013-03-25 08:05:25 -0700290 final ActivityManagerService mService;
291
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800292 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800293
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700294 final ActivityStackSupervisorHandler mHandler;
295
296 /** Short cut */
297 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800298 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700299
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700300 /** Counter for next free stack ID to use for dynamic activity stacks. */
301 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700302
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800303 /**
304 * Maps the task identifier that activities are currently being started in to the userId of the
305 * task. Each time a new task is created, the entry for the userId of the task is incremented
306 */
307 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700308
Craig Mautner2420ead2013-04-01 17:13:20 -0700309 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800310 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700311
Craig Mautnere0a38842013-12-16 16:14:02 -0800312 /** The stack containing the launcher app. Assumed to always be attached to
313 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800314 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700315
Craig Mautnere0a38842013-12-16 16:14:02 -0800316 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800317 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700318
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
320 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800321 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323
324 /** List of activities that are waiting for a new activity to become visible before completing
325 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700326 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
327 // mStoppingActivities when something else comes up.
328 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Bryce Lee4a194382017-04-04 14:32:48 -0700330 /** List of processes waiting to find out when a specific activity becomes visible. */
331 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700332
333 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700334 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700335
Craig Mautnerde4ef022013-04-07 19:01:33 -0700336 /** List of activities that are ready to be stopped, but waiting for the next activity to
337 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800338 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700339
Craig Mautnerf3333272013-04-22 10:55:53 -0700340 /** List of activities that are ready to be finished, but waiting for the previous activity to
341 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800342 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700343
Craig Mautner0eea92c2013-05-16 13:35:39 -0700344 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800345 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700346
Wale Ogunwale22e25262016-02-01 10:32:02 -0800347 /** List of activities whose multi-window mode changed that we need to report to the
348 * application */
349 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
350
351 /** List of activities whose picture-in-picture mode changed that we need to report to the
352 * application */
353 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
354
Winson Chung5af42fc2017-03-24 17:11:33 -0700355 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
356 * the application */
357 Rect mPipModeChangedTargetStackBounds;
358
Craig Mautnerf3333272013-04-22 10:55:53 -0700359 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700360 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700361
Craig Mautnerde4ef022013-04-07 19:01:33 -0700362 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
363 * is being brought in front of us. */
364 boolean mUserLeaving = false;
365
Craig Mautner0eea92c2013-05-16 13:35:39 -0700366 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700367 * We don't want to allow the device to go to sleep while in the process
368 * of launching an activity. This is primarily to allow alarm intent
369 * receivers to launch an activity and get that to run before the device
370 * goes back to sleep.
371 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700372 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700373
374 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700375 * Set when the system is going to sleep, until we have
376 * successfully paused the current activity and released our wake lock.
377 * At that point the system is allowed to actually sleep.
378 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700379 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700380
David Stevens9440dc82017-03-16 19:00:20 -0700381 /**
382 * A list of tokens that cause the top activity to be put to sleep.
383 * They are used by components that may hide and block interaction with underlying
384 * activities.
385 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700386 final ArrayList<SleepToken> mSleepTokens = new ArrayList<>();
David Stevens9440dc82017-03-16 19:00:20 -0700387
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700388 /** Stack id of the front stack when user switched, indexed by userId. */
389 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700390
Bryce Leeaf3a4002017-03-20 10:37:12 -0700391 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700393 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800394
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800395 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
396
397 private DisplayManagerInternal mDisplayManagerInternal;
398 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800399
Wale Ogunwaled046a012015-12-24 13:05:59 -0800400 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800401 boolean inResumeTopActivity;
402
Andrii Kulian1e32e022016-09-16 15:29:34 -0700403 /**
404 * Temporary rect used during docked stack resize calculation so we don't need to create a new
405 * object each time.
406 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700407 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700408
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700409 // The default minimal size that will be used if the activity doesn't specify its minimal size.
410 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700411 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700412
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700413 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
414 private boolean mTaskLayersChanged = true;
415
Jorim Jaggi275561a2016-02-23 10:11:02 -0500416 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800417
Jorim Jaggiea039a82017-08-02 14:37:49 +0200418 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
419
Andrii Kulian1779e612016-10-12 21:58:25 -0700420 @Override
421 protected int getChildCount() {
422 return mActivityDisplays.size();
423 }
424
425 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700426 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700427 return mActivityDisplays.valueAt(index);
428 }
429
430 @Override
431 protected ConfigurationContainer getParent() {
432 return null;
433 }
434
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700435 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700436 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700437 if (activityDisplay == null) {
438 throw new IllegalArgumentException("No display found with id: " + displayId);
439 }
440
441 return activityDisplay.getOverrideConfiguration();
442 }
443
444 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700445 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700446 if (activityDisplay == null) {
447 throw new IllegalArgumentException("No display found with id: " + displayId);
448 }
449
450 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
451 }
452
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700453 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700454 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
455 int callingUid, ActivityInfo activityInfo) {
456 if (displayId == DEFAULT_DISPLAY) {
457 // No restrictions for the default display.
458 return true;
459 }
460 if (!mService.mSupportsMultiDisplay) {
461 // Can't launch on secondary displays if feature is not supported.
462 return false;
463 }
464 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
465 // Can't apply wrong configuration to non-resizeable activities.
466 return false;
467 }
468 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
469 // Can't place activities to a display that has restricted launch rules.
470 // In this case the request should be made by explicitly adding target display id and
471 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
472 return false;
473 }
474 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700475 }
476
477 /**
478 * Check if configuration of specified display matches current global config.
479 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
480 * the same config as on the default display.
481 * @param displayId Id of the display to check.
482 * @return {@code true} if configuration matches.
483 */
484 private boolean displayConfigMatchesGlobal(int displayId) {
485 if (displayId == DEFAULT_DISPLAY) {
486 return true;
487 }
488 if (displayId == INVALID_DISPLAY) {
489 return false;
490 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700491 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700492 if (targetDisplay == null) {
493 throw new IllegalArgumentException("No display found with id: " + displayId);
494 }
495 return getConfiguration().equals(targetDisplay.getConfiguration());
496 }
497
Wale Ogunwale39381972015-12-17 17:15:29 -0800498 static class FindTaskResult {
499 ActivityRecord r;
500 boolean matchedByRootAffinity;
501 }
502 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
503
Craig Mautneree36c772014-07-16 14:56:05 -0700504 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800505 * Temp storage for display ids sorted in focus order.
506 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
507 * it's more efficient, as the number of displays is usually small.
508 */
509 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
510
511 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100512 * Used to keep track whether app visibilities got changed since the last pause. Useful to
513 * determine whether to invoke the task stack change listener after pausing.
514 */
515 boolean mAppVisibilitiesChangedSinceLastPause;
516
517 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100518 * Set of tasks that are in resizing mode during an app transition to fill the "void".
519 */
520 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
521
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700522
523 /**
524 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
525 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
526 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
527 * like the docked stack going empty.
528 */
529 private boolean mAllowDockedStackResize = true;
530
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100531 /**
Tony Mak853304c2016-04-18 15:17:41 +0100532 * Is dock currently minimized.
533 */
534 boolean mIsDockMinimized;
535
Jorim Jaggife762342016-10-13 14:33:27 +0200536 final KeyguardController mKeyguardController;
537
chaviw59b98852017-06-13 12:05:44 -0700538 private PowerManager mPowerManager;
539 private int mDeferResumeCount;
540
Tony Mak853304c2016-04-18 15:17:41 +0100541 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700542 * Description of a request to start a new activity, which has been held
543 * due to app switches being disabled.
544 */
545 static class PendingActivityLaunch {
546 final ActivityRecord r;
547 final ActivityRecord sourceRecord;
548 final int startFlags;
549 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700550 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700551
552 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700553 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700554 r = _r;
555 sourceRecord = _sourceRecord;
556 startFlags = _startFlags;
557 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700558 callerApp = _callerApp;
559 }
560
561 void sendErrorResult(String message) {
562 try {
563 if (callerApp.thread != null) {
564 callerApp.thread.scheduleCrash(message);
565 }
566 } catch (RemoteException e) {
567 Slog.e(TAG, "Exception scheduling crash of failed "
568 + "activity launcher sourceRecord=" + sourceRecord, e);
569 }
Craig Mautneree36c772014-07-16 14:56:05 -0700570 }
571 }
572
Bryce Leeaf691c02017-03-20 14:20:22 -0700573 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700574 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700575 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800576 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200577 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700578 }
579
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800580 void setRecentTasks(RecentTasks recentTasks) {
581 mRecentTasks = recentTasks;
582 }
583
Jeff Brown2c43c332014-06-12 22:38:59 -0700584 /**
585 * At the time when the constructor runs, the power manager has not yet been
586 * initialized. So we initialize our wakelocks afterwards.
587 */
588 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700589 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
590 mGoingToSleep = mPowerManager
591 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
592 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700593 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700594 }
595
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700596 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 synchronized (mService) {
598 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200599 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600
601 mDisplayManager =
602 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
603 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800604 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605
606 Display[] displays = mDisplayManager.getDisplays();
607 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
608 final int displayId = displays[displayNdx].getDisplayId();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700609 ActivityDisplay activityDisplay = new ActivityDisplay(this, displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -0800610 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700611 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800612 }
613
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700614 mHomeStack = mFocusedStack = mLastFocusedStack = getDefaultDisplay().getOrCreateStack(
615 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800616
617 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800618 }
Craig Mautner27084302013-03-25 08:05:25 -0700619 }
620
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700621 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800622 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700623 }
624
Craig Mautnerde4ef022013-04-07 19:01:33 -0700625 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700627 }
628
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700629 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700630 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700631 }
632
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800633 /** The top most stack on its display. */
634 boolean isFrontStackOnDisplay(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700635 return isFrontOfStackList(stack, stack.getDisplay().mStacks);
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800636 }
637
638 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800639 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700640 }
641
Wale Ogunwaled046a012015-12-24 13:05:59 -0800642 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800643 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
644 if (!focusCandidate.isFocusable()) {
645 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800646 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800647 }
648
649 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800650 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800652
Wale Ogunwaled046a012015-12-24 13:05:59 -0800653 EventLogTags.writeAmFocusedStack(
654 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
655 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
656 }
657
658 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800659 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800660 if (r != null && r.idle) {
661 checkFinishBootingLocked();
662 }
663 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700664 }
665
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700666 void moveHomeStackToFront(String reason) {
667 mHomeStack.moveToFront(reason);
668 }
669
Matthew Ng330757d2017-02-28 14:19:17 -0800670 void moveRecentsStackToFront(String reason) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700671 final ActivityStack recentsStack = getDefaultDisplay().getStack(
672 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_RECENTS);
Matthew Ng330757d2017-02-28 14:19:17 -0800673 if (recentsStack != null) {
674 recentsStack.moveToFront(reason);
675 }
676 }
677
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700678 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800679 boolean moveHomeStackTaskToTop(String reason) {
680 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700681
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700682 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700683 if (top == null) {
684 return false;
685 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700686 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700687 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700688 }
689
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800690 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700691 if (!mService.mBooting && !mService.mBooted) {
692 // Not ready yet!
693 return false;
694 }
695
Craig Mautner84984fa2014-06-19 11:19:20 -0700696 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700697 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700698 }
699
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800700 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700701 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800702 final String myReason = reason + " resumeHomeStackTask";
703
Mark Lua56ea122015-10-08 13:31:01 +0800704 // Only resume home activity if isn't finishing.
705 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700706 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800707 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700708 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800709 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700710 }
711
Craig Mautner8d341ef2013-03-26 09:03:27 -0700712 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700713 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
714 }
715
716 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
717 return anyTaskForIdLocked(id, matchMode, null);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700718 }
719
720 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700721 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700722 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800723 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700724 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700725 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700726 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
727 @Nullable ActivityOptions aOptions) {
Winson Chung7900bee2017-03-10 08:59:25 -0800728 // If there is a stack id set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700729 // TODO: Don't really know if this is needed...
730 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
731 throw new IllegalArgumentException("Should not specify activity options for non-restore"
732 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800733 }
734
Craig Mautnere0a38842013-12-16 16:14:02 -0800735 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800737 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
739 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700740 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800741 if (task != null) {
742 return task;
743 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700744 }
745 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800746
Winson Chung7900bee2017-03-10 08:59:25 -0800747 // If we are matching stack tasks only, return now
748 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800749 return null;
750 }
751
Winson Chung7900bee2017-03-10 08:59:25 -0800752 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
753 // the task from recents
754 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700755 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
756
757 if (task == null) {
758 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800759 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
760 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700761
762 return null;
763 }
764
765 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700766 return task;
767 }
768
Winson Chung7900bee2017-03-10 08:59:25 -0800769 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700770 if (!restoreRecentTaskLocked(task, aOptions)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700771 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
772 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800773 return null;
774 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700775 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800776 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700777 }
778
Craig Mautner6170f732013-04-02 13:05:23 -0700779 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800780 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800781 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800782 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
784 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
785 if (r != null) {
786 return r;
787 }
Craig Mautner6170f732013-04-02 13:05:23 -0700788 }
789 }
790 return null;
791 }
792
Tony Mak853304c2016-04-18 15:17:41 +0100793 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000794 * Detects whether we should show a lock screen in front of this task for a locked user.
795 * <p>
796 * We'll do this if either of the following holds:
797 * <ul>
798 * <li>The top activity explicitly belongs to {@param userId}.</li>
799 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
800 * </ul>
801 *
802 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100803 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000804 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
805 // To handle the case that work app is in the task but just is not the top one.
806 final ActivityRecord activityRecord = task.getTopActivity();
807 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
808
809 return (activityRecord != null && activityRecord.userId == userId)
810 || (resultTo != null && resultTo.userId == userId);
811 }
812
813 /**
814 * Find all visible task stacks containing {@param userId} and intercept them with an activity
815 * to block out the contents and possibly start a credential-confirming intent.
816 *
817 * @param userId user handle for the locked managed profile.
818 */
819 void lockAllProfileTasks(@UserIdInt int userId) {
820 mWindowManager.deferSurfaceLayout();
821 try {
822 final List<ActivityStack> stacks = getStacks();
823 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
824 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
825 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
826 final TaskRecord task = tasks.get(taskNdx);
827
828 // Check the task for a top activity belonging to userId, or returning a result
829 // to an activity belonging to userId. Example case: a document picker for
830 // personal files, opened by a work app, should still get locked.
831 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000832 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
833 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000834 }
Tony Mak853304c2016-04-18 15:17:41 +0100835 }
836 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000837 } finally {
838 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100839 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000840 }
841
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800842 void setNextTaskIdForUserLocked(int taskId, int userId) {
843 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
844 if (taskId > currentTaskId) {
845 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700846 }
847 }
848
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700849 static int nextTaskIdForUser(int taskId, int userId) {
850 int nextTaskId = taskId + 1;
851 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
852 // Wrap around as there will be smaller task ids that are available now.
853 nextTaskId -= MAX_TASK_IDS_PER_USER;
854 }
855 return nextTaskId;
856 }
857
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800858 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800859 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
860 // for a userId u, a taskId can only be in the range
861 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
862 // 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 -0700863 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800864 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700865 || anyTaskForIdLocked(
866 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700867 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800868 if (candidateTaskId == currentTaskId) {
869 // Something wrong!
870 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
871 throw new IllegalStateException("Cannot get an available task id."
872 + " Reached limit of " + MAX_TASK_IDS_PER_USER
873 + " running tasks per user.");
874 }
875 }
876 mCurTaskIdForUser.put(userId, candidateTaskId);
877 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700878 }
879
Chong Zhang6cda19c2016-06-14 19:07:56 -0700880 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800881 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700882 if (stack == null) {
883 return null;
884 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700885 ActivityRecord resumedActivity = stack.mResumedActivity;
886 if (resumedActivity == null || resumedActivity.app == null) {
887 resumedActivity = stack.mPausingActivity;
888 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700889 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700890 }
891 }
892 return resumedActivity;
893 }
894
Dianne Hackbornff072722014-09-24 10:56:28 -0700895 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700896 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800897 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800898 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
899 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800900 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
901 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700902 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 continue;
904 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200905 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
906 final ActivityRecord top = stack.topRunningActivityLocked();
907 final int size = mTmpActivityList.size();
908 for (int i = 0; i < size; i++) {
909 final ActivityRecord activity = mTmpActivityList.get(i);
910 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
911 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800912 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200913 if (realStartActivityLocked(activity, app,
914 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800915 didSomething = true;
916 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700917 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800918 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200919 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800920 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700921 }
Craig Mautner20e72272013-04-01 13:45:53 -0700922 }
Craig Mautner20e72272013-04-01 13:45:53 -0700923 }
924 }
925 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700926 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700927 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700928 }
Craig Mautner20e72272013-04-01 13:45:53 -0700929 return didSomething;
930 }
931
932 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800933 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
934 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800935 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
936 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700937 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800938 continue;
939 }
940 final ActivityRecord resumedActivity = stack.mResumedActivity;
941 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700942 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800943 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 return false;
945 }
Craig Mautner20e72272013-04-01 13:45:53 -0700946 }
947 }
Wei Wang65c7a152016-06-02 18:51:22 -0700948 // Send launch end powerhint when idle
949 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700950 return true;
951 }
952
Craig Mautnerde4ef022013-04-07 19:01:33 -0700953 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800954 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
955 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800956 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
957 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700958 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800959 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700960 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800961 return false;
962 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700963 }
964 }
965 }
966 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700967 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
969 mLastFocusedStack + " to=" + mFocusedStack);
970 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700971 return true;
972 }
973
974 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800975 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800976 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
977 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800978 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
979 final ActivityStack stack = stacks.get(stackNdx);
980 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800981 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700982 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800983 return false;
984 }
985 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800986 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700987 }
988 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800989 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700990 }
991
Craig Mautner2acc3892013-09-23 10:28:14 -0700992 /**
993 * Pause all activities in either all of the stacks or just the back stacks.
994 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -0700995 * @param resuming The resuming activity.
996 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
997 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -0700998 * @return true if any activity was paused as a result of this call.
999 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001000 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001001 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1003 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001004 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1005 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001006 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001007 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001008 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1010 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001012 }
1013 }
1014 return someActivityPaused;
1015 }
1016
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001018 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001019 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1020 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001021 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1022 final ActivityStack stack = stacks.get(stackNdx);
1023 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001025 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001026 Slog.d(TAG_STATES,
1027 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001028 pausing = false;
1029 } else {
1030 return false;
1031 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001032 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 }
1034 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001035 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001036 }
1037
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001038 void cancelInitializingActivities() {
1039 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1040 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1041 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1042 stacks.get(stackNdx).cancelInitializingActivities();
1043 }
1044 }
1045 }
1046
Bryce Lee4a194382017-04-04 14:32:48 -07001047 void waitActivityVisible(ComponentName name, WaitResult result) {
1048 final WaitInfo waitInfo = new WaitInfo(name, result);
1049 mWaitingForActivityVisible.add(waitInfo);
1050 }
1051
1052 void cleanupActivity(ActivityRecord r) {
1053 // Make sure this record is no longer in the pending finishes list.
1054 // This could happen, for example, if we are trimming activities
1055 // down to the max limit while they are still waiting to finish.
1056 mFinishingActivities.remove(r);
1057 mActivitiesWaitingForVisibleActivity.remove(r);
1058
1059 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001060 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001061 mWaitingForActivityVisible.remove(i);
1062 }
1063 }
1064 }
1065
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001067 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001068 }
1069
1070 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1071 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001072 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1073 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001074 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001075 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001076 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001077 result.timeout = false;
1078 result.who = w.getComponent();
1079 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1080 result.thisTime = result.totalTime;
1081 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001082 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001083 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001084 if (changed) {
1085 mService.notifyAll();
1086 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001087 }
1088
Chong Zhang5022da32016-06-21 16:31:37 -07001089 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1090 boolean changed = false;
1091 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1092 WaitResult w = mWaitingActivityLaunched.remove(i);
1093 if (w.who == null) {
1094 changed = true;
1095 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1096 // the starting activity ends up moving another activity to front, and it should
1097 // wait for this new activity to become visible instead.
1098 // Do not modify other fields.
1099 w.result = START_TASK_TO_FRONT;
1100 }
1101 }
1102 if (changed) {
1103 mService.notifyAll();
1104 }
1105 }
1106
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1108 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001109 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001110 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001111 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001112 if (w.who == null) {
1113 changed = true;
1114 w.timeout = timeout;
1115 if (r != null) {
1116 w.who = new ComponentName(r.info.packageName, r.info.name);
1117 }
1118 w.thisTime = thisTime;
1119 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001120 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001123 if (changed) {
1124 mService.notifyAll();
1125 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001126 }
1127
Craig Mautner29219d92013-04-16 20:19:12 -07001128 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001129 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001130 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001131 if (r != null) {
1132 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001133 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001134
Andrii Kulian7318d632016-07-20 18:59:28 -07001135 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001136 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1137
1138 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1139 final int displayId = mTmpOrderedDisplayIds.get(i);
1140 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1141 if (stacks == null) {
1142 continue;
1143 }
1144 for (int j = stacks.size() - 1; j >= 0; --j) {
1145 final ActivityStack stack = stacks.get(j);
1146 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1147 r = stack.topRunningActivityLocked();
1148 if (r != null) {
1149 return r;
1150 }
Craig Mautner29219d92013-04-16 20:19:12 -07001151 }
1152 }
1153 }
1154 return null;
1155 }
1156
Dianne Hackborn09233282014-04-30 11:33:59 -07001157 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001158 // Gather all of the running tasks for each stack into runningTaskLists.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001159 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001160 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001161 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001162 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001163 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1164 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001165 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001166 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001167 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001168 }
1169 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001170
1171 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1172 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1173 while (maxNum > 0) {
1174 long mostRecentActiveTime = Long.MIN_VALUE;
1175 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001176 final int numTaskLists = runningTaskLists.size();
1177 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1178 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001179 if (!stackTaskList.isEmpty()) {
1180 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1181 if (lastActiveTime > mostRecentActiveTime) {
1182 mostRecentActiveTime = lastActiveTime;
1183 selectedStackList = stackTaskList;
1184 }
1185 }
1186 }
1187 if (selectedStackList != null) {
1188 list.add(selectedStackList.remove(0));
1189 --maxNum;
1190 } else {
1191 break;
1192 }
1193 }
Craig Mautner20e72272013-04-01 13:45:53 -07001194 }
1195
Todd Kennedy7440f172015-12-09 14:31:22 -08001196 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1197 ProfilerInfo profilerInfo) {
1198 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001199 if (aInfo != null) {
1200 // Store the found target back into the intent, because now that
1201 // we have it we never want to do this again. For example, if the
1202 // user navigates back to this point in the history, we should
1203 // always restart the exact same activity.
1204 intent.setComponent(new ComponentName(
1205 aInfo.applicationInfo.packageName, aInfo.name));
1206
1207 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001208 if (!aInfo.processName.equals("system")) {
1209 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001210 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001211 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001212
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001213 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1214 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1215 }
1216
Man Caocfa78b22015-06-11 20:14:34 -07001217 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1218 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1219 }
1220
1221 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001222 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001223 }
1224 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001225 final String intentLaunchToken = intent.getLaunchToken();
1226 if (aInfo.launchToken == null && intentLaunchToken != null) {
1227 aInfo.launchToken = intentLaunchToken;
1228 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001229 }
1230 return aInfo;
1231 }
1232
Todd Kennedy7440f172015-12-09 14:31:22 -08001233 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001234 return resolveIntent(intent, resolvedType, userId, 0);
1235 }
1236
1237 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001238 synchronized (mService) {
1239 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001240 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001241 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001242 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001243 }
1244
1245 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1246 ProfilerInfo profilerInfo, int userId) {
1247 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1248 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1249 }
1250
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001251 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1252 boolean andResume, boolean checkConfig) throws RemoteException {
1253
1254 if (!allPausedActivitiesComplete()) {
1255 // While there are activities pausing we skipping starting any new activities until
1256 // pauses are complete. NOTE: that we also do this for activities that are starting in
1257 // the paused state because they will first be resumed then paused on the client side.
1258 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1259 "realStartActivityLocked: Skipping start of r=" + r
1260 + " some activities pausing...");
1261 return false;
1262 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001263
Bryce Leeaf691c02017-03-20 14:20:22 -07001264 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001265 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001266
1267 beginDeferResume();
1268
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 try {
chaviw59b98852017-06-13 12:05:44 -07001270 r.startFreezingScreenLocked(app, 0);
1271
1272 // schedule launch ticks to collect information about slow apps.
1273 r.startLaunchTickingLocked();
1274
1275 r.app = app;
1276
Jorim Jaggi838c2452017-08-28 15:44:43 +02001277 if (mKeyguardController.isKeyguardLocked()) {
1278 r.notifyUnknownVisibilityLaunched();
1279 }
1280
chaviw59b98852017-06-13 12:05:44 -07001281 // Have the window manager re-evaluate the orientation of the screen based on the new
1282 // activity order. Note that as a result of this, it can call back into the activity
1283 // manager with a new orientation. We don't care about that, because the activity is
1284 // not currently running so we are just restarting it anyway.
1285 if (checkConfig) {
1286 final int displayId = r.getDisplayId();
1287 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1288 getDisplayOverrideConfiguration(displayId),
1289 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1290 // Deferring resume here because we're going to launch new activity shortly.
1291 // We don't want to perform a redundant launch of the same record while ensuring
1292 // configurations and trying to resume top activity of focused stack.
1293 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1294 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
chaviw59b98852017-06-13 12:05:44 -07001296
1297 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1298 true /* isTop */)) {
1299 // We only set the visibility to true if the activity is allowed to be visible
1300 // based on
1301 // keyguard state. This avoids setting this into motion in window manager that is
1302 // later cancelled due to later calls to ensure visible activities that set
1303 // visibility back to false.
1304 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001305 }
chaviw59b98852017-06-13 12:05:44 -07001306
chaviw59b98852017-06-13 12:05:44 -07001307 final int applicationInfoUid =
1308 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1309 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1310 Slog.wtf(TAG,
1311 "User ID for activity changing for " + r
1312 + " appInfo.uid=" + r.appInfo.uid
1313 + " info.ai.uid=" + applicationInfoUid
1314 + " old=" + r.app + " new=" + app);
1315 }
1316
1317 app.waitingToKill = null;
1318 r.launchCount++;
1319 r.lastLaunchTime = SystemClock.uptimeMillis();
1320
1321 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1322
1323 int idx = app.activities.indexOf(r);
1324 if (idx < 0) {
1325 app.activities.add(r);
1326 }
1327 mService.updateLruProcessLocked(app, true, null);
1328 mService.updateOomAdjLocked();
1329
1330 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1331 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001332 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001333 }
1334
1335 try {
1336 if (app.thread == null) {
1337 throw new RemoteException();
1338 }
1339 List<ResultInfo> results = null;
1340 List<ReferrerIntent> newIntents = null;
1341 if (andResume) {
1342 // We don't need to deliver new intents and/or set results if activity is going
1343 // to pause immediately after launch.
1344 results = r.results;
1345 newIntents = r.newIntents;
1346 }
1347 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1348 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1349 + " newIntents=" + newIntents + " andResume=" + andResume);
1350 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1351 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001352 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001353 // Home process is the root process of the task.
1354 mService.mHomeProcess = task.mActivities.get(0).app;
1355 }
1356 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1357 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1358 r.sleeping = false;
1359 r.forceNewConfig = false;
1360 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1361 mService.showAskCompatModeDialogLocked(r);
1362 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1363 ProfilerInfo profilerInfo = null;
1364 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1365 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1366 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001367 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1368 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1369 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001370 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001371 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001372 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001373 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001374 }
1375 }
chaviw59b98852017-06-13 12:05:44 -07001376
Andreas Gampe2b073a02017-06-22 17:28:57 -07001377 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 }
1380 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001381
chaviw59b98852017-06-13 12:05:44 -07001382 app.hasShownUi = true;
1383 app.pendingUiClean = true;
1384 app.forceProcessStateUpTo(mService.mTopProcessState);
1385 // Because we could be starting an Activity in the system process this may not go
1386 // across a Binder interface which would create a new Configuration. Consequently
1387 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001388
chaviw59b98852017-06-13 12:05:44 -07001389 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1390 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1391 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001392
chaviw59b98852017-06-13 12:05:44 -07001393 logIfTransactionTooLarge(r.intent, r.icicle);
1394 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1395 System.identityHashCode(r), r.info,
1396 // TODO: Have this take the merged configuration instead of separate global
1397 // and override configs.
1398 mergedConfiguration.getGlobalConfiguration(),
1399 mergedConfiguration.getOverrideConfiguration(), r.compat,
1400 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1401 r.persistentState, results, newIntents, !andResume,
1402 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001403
chaviw59b98852017-06-13 12:05:44 -07001404 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1405 // This may be a heavy-weight process! Note that the package
1406 // manager will ensure that only activity can run in the main
1407 // process of the .apk, which is the only thing that will be
1408 // considered heavy-weight.
1409 if (app.processName.equals(app.info.packageName)) {
1410 if (mService.mHeavyWeightProcess != null
1411 && mService.mHeavyWeightProcess != app) {
1412 Slog.w(TAG, "Starting new heavy weight process " + app
1413 + " when already running "
1414 + mService.mHeavyWeightProcess);
1415 }
1416 mService.mHeavyWeightProcess = app;
1417 Message msg = mService.mHandler.obtainMessage(
1418 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1419 msg.obj = r;
1420 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001421 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001422 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001423
chaviw59b98852017-06-13 12:05:44 -07001424 } catch (RemoteException e) {
1425 if (r.launchFailed) {
1426 // This is the second time we failed -- finish activity
1427 // and give up.
1428 Slog.e(TAG, "Second failure launching "
1429 + r.intent.getComponent().flattenToShortString()
1430 + ", giving up", e);
1431 mService.appDiedLocked(app);
1432 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1433 "2nd-crash", false);
1434 return false;
1435 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001436
chaviw59b98852017-06-13 12:05:44 -07001437 // This is the first time we failed -- restart process and
1438 // retry.
1439 r.launchFailed = true;
1440 app.activities.remove(r);
1441 throw e;
1442 }
1443 } finally {
1444 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001445 }
1446
1447 r.launchFailed = false;
1448 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001449 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001450 }
1451
chaviw59b98852017-06-13 12:05:44 -07001452 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 // As part of the process of launching, ActivityThread also performs
1454 // a resume.
1455 stack.minimalResumeActivityLocked(r);
1456 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001457 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001458 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001459 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001460 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001461 "Moving to PAUSED: " + r + " (starting in paused state)");
1462 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001463 }
1464
1465 // Launch the new version setup screen if needed. We do this -after-
1466 // launching the initial activity (that is, home), so that it can have
1467 // a chance to initialize itself while in the background, making the
1468 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001469 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001470 mService.startSetupActivityLocked();
1471 }
1472
Dianne Hackborn465fa392014-09-14 14:21:18 -07001473 // Update any services we are bound to that might care about whether
1474 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001475 if (r.app != null) {
1476 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1477 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001478
Craig Mautner2420ead2013-04-01 17:13:20 -07001479 return true;
1480 }
1481
Sudheer Shankafab200f2017-05-17 20:41:53 -07001482 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1483 int extrasSize = 0;
1484 if (intent != null) {
1485 final Bundle extras = intent.getExtras();
1486 if (extras != null) {
1487 extrasSize = extras.getSize();
1488 }
1489 }
1490 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1491 if (extrasSize + icicleSize > 200000) {
1492 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1493 + ", icicle size: " + icicleSize);
1494 }
1495 }
1496
Craig Mautnere79d42682013-04-01 19:01:53 -07001497 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001498 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001499 // Is this activity's application already running?
1500 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001501 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001502
Andrii Kulian02b7a832016-10-06 23:11:56 -07001503 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001504
1505 if (app != null && app.thread != null) {
1506 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001507 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1508 || !"android".equals(r.info.packageName)) {
1509 // Don't add this if it is a platform component that is marked
1510 // to run in multiple processes, because this is actually
1511 // part of the framework so doesn't make sense to track as a
1512 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001513 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1514 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001515 }
George Mount2c92c972014-03-20 09:38:23 -07001516 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001517 return;
1518 } catch (RemoteException e) {
1519 Slog.w(TAG, "Exception when starting activity "
1520 + r.intent.getComponent().flattenToShortString(), e);
1521 }
1522
1523 // If a dead object exception was thrown -- fall through to
1524 // restart the application.
1525 }
1526
1527 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001528 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001529 }
1530
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001531 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001532 String resultWho, int requestCode, int callingPid, int callingUid,
1533 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001534 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001535 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1536 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001537 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001538 return true;
1539 }
1540 final int componentRestriction = getComponentRestrictionForCallingPackage(
1541 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1542 final int actionRestriction = getActionRestrictionForCallingPackage(
1543 intent.getAction(), callingPackage, callingPid, callingUid);
1544 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1545 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1546 if (resultRecord != null) {
1547 resultStack.sendActivityResultLocked(-1,
1548 resultRecord, resultWho, requestCode,
1549 Activity.RESULT_CANCELED, null);
1550 }
1551 final String msg;
1552 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1553 msg = "Permission Denial: starting " + intent.toString()
1554 + " from " + callerApp + " (pid=" + callingPid
1555 + ", uid=" + callingUid + ")" + " with revoked permission "
1556 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1557 } else if (!aInfo.exported) {
1558 msg = "Permission Denial: starting " + intent.toString()
1559 + " from " + callerApp + " (pid=" + callingPid
1560 + ", uid=" + callingUid + ")"
1561 + " not exported from uid " + aInfo.applicationInfo.uid;
1562 } else {
1563 msg = "Permission Denial: starting " + intent.toString()
1564 + " from " + callerApp + " (pid=" + callingPid
1565 + ", uid=" + callingUid + ")"
1566 + " requires " + aInfo.permission;
1567 }
1568 Slog.w(TAG, msg);
1569 throw new SecurityException(msg);
1570 }
1571
1572 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1573 final String message = "Appop Denial: starting " + intent.toString()
1574 + " from " + callerApp + " (pid=" + callingPid
1575 + ", uid=" + callingUid + ")"
1576 + " requires " + AppOpsManager.permissionToOp(
1577 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1578 Slog.w(TAG, message);
1579 return false;
1580 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1581 final String message = "Appop Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ")"
1584 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1585 Slog.w(TAG, message);
1586 return false;
1587 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001588 if (options != null) {
1589 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1590 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1591 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001592 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001593 final String msg = "Permission Denial: starting " + intent.toString()
1594 + " from " + callerApp + " (pid=" + callingPid
1595 + ", uid=" + callingUid + ") with launchTaskId="
1596 + options.getLaunchTaskId();
1597 Slog.w(TAG, msg);
1598 throw new SecurityException(msg);
1599 }
1600 }
1601 // Check if someone tries to launch an activity on a private display with a different
1602 // owner.
1603 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001604 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1605 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001606 final String msg = "Permission Denial: starting " + intent.toString()
1607 + " from " + callerApp + " (pid=" + callingPid
1608 + ", uid=" + callingUid + ") with launchDisplayId="
1609 + launchDisplayId;
1610 Slog.w(TAG, msg);
1611 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001612 }
1613 }
1614
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001615 return true;
1616 }
1617
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001618 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001619 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1620 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001621 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1622 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1623
Andrii Kulian02689a72017-07-06 14:28:59 -07001624 if (callingPid == -1 && callingUid == -1) {
1625 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1626 return true;
1627 }
1628
Andrii Kulian62e6f252017-05-30 22:46:53 -07001629 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001630 if (activityDisplay == null) {
1631 Slog.w(TAG, "Launch on display check: display not found");
1632 return false;
1633 }
1634
Andrii Kulian02689a72017-07-06 14:28:59 -07001635 // Check if the caller has enough privileges to embed activities and launch to private
1636 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001637 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001638 callingUid);
1639 if (startAnyPerm == PERMISSION_GRANTED) {
1640 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1641 + " allow launch any on display");
1642 return true;
1643 }
1644
Andrii Kulian8f070292017-09-12 22:56:49 -07001645 // Check if caller is already present on display
1646 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1647
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001648 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1649 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1650 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001651 // Limit launching on virtual displays, because their contents can be read from Surface
1652 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001653 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1654 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1655 + " disallow launch on virtual display for not-embedded activity.");
1656 return false;
1657 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001658 // Check if the caller is allowed to embed activities from other apps.
1659 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001660 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001661 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1662 + " disallow activity embedding without permission.");
1663 return false;
1664 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001665 }
1666
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001667 if (!activityDisplay.isPrivate()) {
1668 // Anyone can launch on a public display.
1669 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1670 + " allow launch on public display");
1671 return true;
1672 }
1673
1674 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001675 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001676 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1677 + " allow launch for owner of the display");
1678 return true;
1679 }
1680
Andrii Kulian8f070292017-09-12 22:56:49 -07001681 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001682 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1683 + " allow launch for caller present on the display");
1684 return true;
1685 }
1686
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001687 Slog.w(TAG, "Launch on display check: denied");
1688 return false;
1689 }
1690
1691 /** Update lists of UIDs that are present on displays and have access to them. */
1692 void updateUIDsPresentOnDisplay() {
1693 mDisplayAccessUIDs.clear();
1694 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1695 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001696 // Only bother calculating the whitelist for private displays
1697 if (activityDisplay.isPrivate()) {
1698 mDisplayAccessUIDs.append(
1699 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1700 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001701 }
1702 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1703 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1704 }
1705
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001706 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001707 final long identity = Binder.clearCallingIdentity();
1708 try {
1709 return UserManager.get(mService.mContext).getUserInfo(userId);
1710 } finally {
1711 Binder.restoreCallingIdentity(identity);
1712 }
1713 }
1714
Svet Ganov99b60432015-06-27 13:15:22 -07001715 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001716 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001717 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1718 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001719 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_PERMISSION;
1721 }
1722
Christopher Tateff7add02015-08-17 10:23:22 -07001723 if (activityInfo.permission == null) {
1724 return ACTIVITY_RESTRICTION_NONE;
1725 }
1726
Svet Ganov99b60432015-06-27 13:15:22 -07001727 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1728 if (opCode == AppOpsManager.OP_NONE) {
1729 return ACTIVITY_RESTRICTION_NONE;
1730 }
1731
1732 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1733 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001734 if (!ignoreTargetSecurity) {
1735 return ACTIVITY_RESTRICTION_APPOP;
1736 }
Svet Ganov99b60432015-06-27 13:15:22 -07001737 }
1738
1739 return ACTIVITY_RESTRICTION_NONE;
1740 }
1741
Svetoslav7008b512015-06-24 18:47:07 -07001742 private int getActionRestrictionForCallingPackage(String action,
1743 String callingPackage, int callingPid, int callingUid) {
1744 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001745 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001746 }
1747
1748 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1749 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 final PackageInfo packageInfo;
1754 try {
1755 packageInfo = mService.mContext.getPackageManager()
1756 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1757 } catch (PackageManager.NameNotFoundException e) {
1758 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001766 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001767 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001768 }
1769
1770 final int opCode = AppOpsManager.permissionToOpCode(permission);
1771 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001772 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001773 }
1774
1775 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1776 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001777 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001778 }
1779
Svet Ganov99b60432015-06-27 13:15:22 -07001780 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001781 }
1782
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001783 void setLaunchSource(int uid) {
1784 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1785 }
1786
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001787 void acquireLaunchWakelock() {
1788 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1789 throw new IllegalStateException("Calling must be system uid");
1790 }
1791 mLaunchingActivity.acquire();
1792 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1793 // To be safe, don't allow the wake lock to be held for too long.
1794 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1795 }
1796 }
1797
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001798 /**
1799 * Called when the frontmost task is idle.
1800 * @return the state of mService.mBooting before this was called.
1801 */
1802 private boolean checkFinishBootingLocked() {
1803 final boolean booting = mService.mBooting;
1804 boolean enableScreen = false;
1805 mService.mBooting = false;
1806 if (!mService.mBooted) {
1807 mService.mBooted = true;
1808 enableScreen = true;
1809 }
1810 if (booting || enableScreen) {
1811 mService.postFinishBooting(booting, enableScreen);
1812 }
1813 return booting;
1814 }
1815
Craig Mautnerf3333272013-04-22 10:55:53 -07001816 // Checked.
1817 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001818 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001819 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001820
Craig Mautnerf3333272013-04-22 10:55:53 -07001821 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001822 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001823 int NS = 0;
1824 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001825 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001826 boolean activityRemoved = false;
1827
Wale Ogunwale7d701172015-03-11 15:36:30 -07001828 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001830 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1831 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001832 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1833 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001834 if (fromTimeout) {
1835 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001836 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001837
1838 // This is a hack to semi-deal with a race condition
1839 // in the client where it can be constructed with a
1840 // newer configuration from when we asked it to launch.
1841 // We'll update with whatever configuration it now says
1842 // it used to launch.
1843 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001844 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001845 }
1846
1847 // We are now idle. If someone is waiting for a thumbnail from
1848 // us, we can now deliver.
1849 r.idle = true;
1850
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001851 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001852 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001853 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001854 }
1855 }
1856
1857 if (allResumedActivitiesIdle()) {
1858 if (r != null) {
1859 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001860 }
1861
1862 if (mLaunchingActivity.isHeld()) {
1863 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1864 if (VALIDATE_WAKE_LOCK_CALLER &&
1865 Binder.getCallingUid() != Process.myUid()) {
1866 throw new IllegalStateException("Calling must be system uid");
1867 }
1868 mLaunchingActivity.release();
1869 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001870 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001871 }
1872
1873 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001874 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1875 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001876 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001877 if ((NF = mFinishingActivities.size()) > 0) {
1878 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 mFinishingActivities.clear();
1880 }
1881
Craig Mautnerf3333272013-04-22 10:55:53 -07001882 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001883 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 mStartingUsers.clear();
1885 }
1886
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 // Stop any activities that are scheduled to do so but have been
1888 // waiting for the next one to start.
1889 for (int i = 0; i < NS; i++) {
1890 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001891 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001892 if (stack != null) {
1893 if (r.finishing) {
1894 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1895 } else {
1896 stack.stopActivityLocked(r);
1897 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001898 }
1899 }
1900
1901 // Finish any activities that are scheduled to do so but have been
1902 // waiting for the next one to start.
1903 for (int i = 0; i < NF; i++) {
1904 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001905 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001906 if (stack != null) {
1907 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1908 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001909 }
1910
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001911 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001912 // Complete user switch
1913 if (startingUsers != null) {
1914 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001915 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001916 }
1917 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001918 }
1919
1920 mService.trimApplications();
1921 //dump();
1922 //mWindowManager.dump();
1923
Craig Mautnerf3333272013-04-22 10:55:53 -07001924 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001925 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001926 }
1927
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001928 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001929 }
1930
Craig Mautner8e569572013-10-11 17:36:59 -07001931 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001932 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001933 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1934 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001935 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1936 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1937 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001938 }
Craig Mautner19091252013-10-05 00:03:53 -07001939 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001940 }
1941
1942 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001943 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1944 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001945 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1946 stacks.get(stackNdx).closeSystemDialogsLocked();
1947 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001948 }
1949 }
1950
Craig Mautner93529a42013-10-04 15:03:13 -07001951 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001952 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001953 }
1954
Craig Mautner8d341ef2013-03-26 09:03:27 -07001955 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001956 * Update the last used stack id for non-current user (current user's last
1957 * used stack is the focused stack)
1958 */
1959 void updateUserStackLocked(int userId, ActivityStack stack) {
1960 if (userId != mCurrentUser) {
1961 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1962 }
1963 }
1964
1965 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001966 * @return true if some activity was finished (or would have finished if doit were true).
1967 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001968 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1969 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001970 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001971 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1972 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001973 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001974 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001975 if (stack.finishDisabledPackageActivitiesLocked(
1976 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001977 didSomething = true;
1978 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001979 }
1980 }
1981 return didSomething;
1982 }
1983
Dianne Hackborna413dc02013-07-12 12:02:55 -07001984 void updatePreviousProcessLocked(ActivityRecord r) {
1985 // Now that this process has stopped, we may want to consider
1986 // it to be the previous app to try to keep around in case
1987 // the user wants to return to it.
1988
1989 // First, found out what is currently the foreground app, so that
1990 // we don't blow away the previous app if this activity is being
1991 // hosted by the process that is actually still the foreground.
1992 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08001993 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1994 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001995 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1996 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001997 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001998 if (stack.mResumedActivity != null) {
1999 fgApp = stack.mResumedActivity.app;
2000 } else if (stack.mPausingActivity != null) {
2001 fgApp = stack.mPausingActivity.app;
2002 }
2003 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002004 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002005 }
2006 }
2007
2008 // Now set this one as the previous process, only if that really
2009 // makes sense to.
2010 if (r.app != null && fgApp != null && r.app != fgApp
2011 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002012 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002013 mService.mPreviousProcess = r.app;
2014 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2015 }
2016 }
2017
Wale Ogunwaled046a012015-12-24 13:05:59 -08002018 boolean resumeFocusedStackTopActivityLocked() {
2019 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002020 }
2021
Wale Ogunwaled046a012015-12-24 13:05:59 -08002022 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002023 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002024
2025 if (!readyToResume()) {
2026 return false;
2027 }
2028
Wale Ogunwaled046a012015-12-24 13:05:59 -08002029 if (targetStack != null && isFocusedStack(targetStack)) {
2030 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002031 }
chaviw59b98852017-06-13 12:05:44 -07002032
Wale Ogunwale06579d62016-04-30 15:29:06 -07002033 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2034 if (r == null || r.state != RESUMED) {
2035 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002036 } else if (r.state == RESUMED) {
2037 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2038 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002039 }
chaviw59b98852017-06-13 12:05:44 -07002040
Wale Ogunwaled046a012015-12-24 13:05:59 -08002041 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002042 }
2043
Todd Kennedy39bfee52016-02-24 10:28:21 -08002044 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2045 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2046 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2047 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2048 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2049 }
2050 }
2051 }
2052
Adrian Roos20d7df32016-01-12 18:59:43 +01002053 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2054 TaskRecord finishedTask = null;
2055 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002056 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2057 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002058 final int numStacks = stacks.size();
2059 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2060 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002061 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2062 if (stack == focusedStack || finishedTask == null) {
2063 finishedTask = t;
2064 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002065 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002066 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002067 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002068 }
2069
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002070 void finishVoiceTask(IVoiceInteractionSession session) {
2071 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2072 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2073 final int numStacks = stacks.size();
2074 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2075 final ActivityStack stack = stacks.get(stackNdx);
2076 stack.finishVoiceTask(session);
2077 }
2078 }
2079 }
2080
Andrii Kulianc27916642016-04-12 17:59:27 -07002081 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2082 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002083 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2084 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002085 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002086 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2087 // Caller wants the home activity moved with it. To accomplish this,
2088 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002089 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002090 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002091 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002092 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002093 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2094 + task + " to front. Stack is null");
2095 return;
2096 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002097
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002098 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2099 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2100 task.updateOverrideConfiguration(bounds);
2101
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002102 ActivityStack stack = getLaunchStack(null, options, task, ON_TOP);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002103
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002104 if (stack != currentStack) {
2105 task.reparent(stack, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE, DEFER_RESUME,
2106 "findTaskToMoveToFrontLocked");
2107 stack = currentStack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002108 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2109 // still need moveTaskToFrontLocked() below for any transition settings.
2110 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002111 if (StackId.resizeStackWithLaunchBounds(stack.mStackId)) {
2112 resizeStackLocked(stack.mStackId, bounds, null /* tempTaskBounds */,
2113 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
2114 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002115 } else {
2116 // WM resizeTask must be done after the task is moved to the correct stack,
2117 // because Task's setBounds() also updates dim layer's bounds, but that has
2118 // dependency on the stack.
2119 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002120 }
2121 }
2122
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002123 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002124 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002125 r == null ? null : r.appTimeTracker, reason);
2126
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002127 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002128 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002129
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002130 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002131 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002132 }
2133
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002134 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002135 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002136 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002137 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002138 return false;
2139 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002140 return (mService.mSupportsPictureInPicture
2141 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002142 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002143 }
2144
Winson Chung55893332017-02-17 17:13:10 -08002145 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002146 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2147 final T stack = mActivityDisplays.valueAt(i).getStack(stackId);
2148 if (stack != null) {
2149 return stack;
2150 }
2151 }
2152 return null;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002153 }
2154
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002155 /**
2156 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
2157 * @param windowingMode The windowing mode we are checking support for.
2158 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
2159 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
2160 * @param supportsFreeform If we should consider support for freeform multi-window.
2161 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
2162 * @param activityType The activity type under consideration.
2163 * @return true if the windowing mode is supported.
2164 */
2165 boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
2166 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
2167 int activityType) {
2168
2169 if (windowingMode == WINDOWING_MODE_UNDEFINED
2170 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
2171 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002172 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002173 if (!supportsMultiWindow) {
2174 return false;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002175 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002176
2177 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2178 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
2179 return supportsSplitScreen && WindowConfiguration.supportSplitScreenWindowingMode(
2180 windowingMode, activityType);
Matthew Ng330757d2017-02-28 14:19:17 -08002181 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002182
2183 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
2184 return false;
2185 }
2186
2187 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
2188 return false;
2189 }
2190 return true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002191 }
2192
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002193 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002194 @Nullable TaskRecord task, int activityType) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002195
2196 // First preference if the windowing mode in the activity options if set.
2197 int windowingMode = (options != null)
2198 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2199
2200 // If windowing mode is unset, then next preference is the candidate task, then the
2201 // activity record.
2202 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2203 if (task != null) {
2204 windowingMode = task.getWindowingMode();
2205 }
2206 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2207 windowingMode = r.getWindowingMode();
2208 }
2209 }
2210
2211 // Make sure the windowing mode we are trying to use makes sense for what is supported.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002212 boolean supportsMultiWindow = mService.mSupportsMultiWindow;
2213 boolean supportsSplitScreen = mService.mSupportsSplitScreenMultiWindow;
2214 boolean supportsFreeform = mService.mSupportsFreeformWindowManagement;
2215 boolean supportsPip = mService.mSupportsPictureInPicture;
2216 if (supportsMultiWindow) {
2217 if (task != null) {
2218 supportsMultiWindow = task.isResizeable();
2219 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
2220 // TODO: Do we need to check for freeform and Pip support here?
2221 } else if (r != null) {
2222 supportsMultiWindow = r.isResizeable();
2223 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
2224 supportsFreeform = r.supportsFreeform();
2225 supportsPip = r.supportsPictureInPicture();
2226 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002227 }
2228
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002229 if (isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
2230 supportsFreeform, supportsPip, activityType)) {
2231 return windowingMode;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002232 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002233 return WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002234 }
2235
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002236 int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002237 @Nullable TaskRecord task) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002238 // Preference is given to the activity type for the activity then the task since the type
2239 // once set shouldn't change.
2240 int activityType = r != null ? r.getActivityType() : ACTIVITY_TYPE_UNDEFINED;
2241 if (activityType == ACTIVITY_TYPE_UNDEFINED && task != null) {
2242 activityType = task.getActivityType();
2243 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002244 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2245 return activityType;
2246 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002247 return options != null ? options.getLaunchActivityType() : ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002248 }
2249
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002250 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2251 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop) {
2252 return getLaunchStack(r, options, candidateTask, onTop, INVALID_DISPLAY);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002253 }
2254
2255 /**
2256 * Returns the right stack to use for launching factoring in all the input parameters.
2257 *
2258 * @param r The activity we are trying to launch. Can be null.
2259 * @param options The activity options used to the launch. Can be null.
2260 * @param candidateTask The possible task the activity might be launched in. Can be null.
2261 *
2262 * @return The stack to use for the launch or INVALID_STACK_ID.
2263 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002264 <T extends ActivityStack> T getLaunchStack(@Nullable ActivityRecord r,
2265 @Nullable ActivityOptions options, @Nullable TaskRecord candidateTask, boolean onTop,
2266 int candidateDisplayId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002267 int taskId = INVALID_TASK_ID;
2268 int displayId = INVALID_DISPLAY;
2269 //Rect bounds = null;
2270
2271 // We give preference to the launch preference in activity options.
2272 if (options != null) {
2273 taskId = options.getLaunchTaskId();
2274 displayId = options.getLaunchDisplayId();
2275 // TODO: Need to work this into the equation...
2276 //bounds = options.getLaunchBounds();
2277 }
2278
2279 // First preference for stack goes to the task Id set in the activity options. Use the stack
2280 // associated with that if possible.
2281 if (taskId != INVALID_TASK_ID) {
2282 // Temporarily set the task id to invalid in case in re-entry.
2283 options.setLaunchTaskId(INVALID_TASK_ID);
2284 final TaskRecord task = anyTaskForIdLocked(taskId,
2285 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options);
2286 options.setLaunchTaskId(taskId);
2287 if (task != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002288 return task.getStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002289 }
2290 }
2291
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002292 final int activityType = resolveActivityType(r, options, candidateTask);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002293 int windowingMode = resolveWindowingMode(r, options, candidateTask, activityType);
2294 T stack = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002295
2296 // Next preference for stack goes to the display Id set in the activity options or the
2297 // candidate display.
2298 if (displayId == INVALID_DISPLAY) {
2299 displayId = candidateDisplayId;
2300 }
2301 if (displayId != INVALID_DISPLAY) {
2302 if (r != null) {
2303 // TODO: This should also take in the windowing mode and activity type into account.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002304 stack = (T) getValidLaunchStackOnDisplay(displayId, r);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002305 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002306 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002307 }
2308 }
2309 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2310 if (display != null) {
2311 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002312 stack = (T) display.mStacks.get(i);
2313 if (stack.isCompatible(windowingMode, activityType)) {
2314 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002315 }
2316 }
2317 // TODO: We should create the stack we want on the display at this point.
2318 }
2319 }
2320
2321 // Give preference to the stack and display of the input task and activity if they match the
2322 // mode we want to launch into.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002323 stack = null;
2324 ActivityDisplay display = null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002325 if (candidateTask != null) {
2326 stack = candidateTask.getStack();
2327 }
2328 if (stack == null && r != null) {
2329 stack = r.getStack();
2330 }
2331 if (stack != null) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002332 if (stack.isCompatible(windowingMode, activityType)) {
2333 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002334 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002335 display = stack.getDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002336 }
2337
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002338 if (display == null
2339 // TODO: Can be removed once we figure-out how non-standard types should launch
2340 // outside the default display.
2341 || (activityType != ACTIVITY_TYPE_STANDARD
2342 && activityType != ACTIVITY_TYPE_UNDEFINED)) {
2343 display = getDefaultDisplay();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002344 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002345
2346 stack = display.getOrCreateStack(windowingMode, activityType, onTop);
2347 if (stack != null) {
2348 return stack;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002349 }
2350
2351 // Whatever...return some default for now.
2352 if (candidateTask != null && candidateTask.mBounds != null
2353 && mService.mSupportsFreeformWindowManagement) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002354 windowingMode = WINDOWING_MODE_FREEFORM;
2355 } else {
2356 windowingMode = WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002357 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002358 return display.getOrCreateStack(windowingMode, activityType, onTop);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002359 }
2360
Andrii Kulian16802aa2016-11-02 12:21:33 -07002361 /**
2362 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2363 * If there is no such stack, new dynamic stack can be created.
2364 * @param displayId Target display.
2365 * @param r Activity that should be launched there.
2366 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2367 */
2368 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002369 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002370 if (activityDisplay == null) {
2371 throw new IllegalArgumentException(
2372 "Display with displayId=" + displayId + " not found.");
2373 }
2374
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002375 if (!r.canBeLaunchedOnDisplay(displayId)) {
2376 return null;
2377 }
2378
Andrii Kulian16802aa2016-11-02 12:21:33 -07002379 // Return the topmost valid stack on the display.
2380 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2381 final ActivityStack stack = activityDisplay.mStacks.get(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002382 if (isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002383 return stack;
2384 }
2385 }
2386
2387 // If there is no valid stack on the external display - check if new dynamic stack will do.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002388 if (displayId != DEFAULT_DISPLAY) {
2389 return activityDisplay.createStack(
2390 r.getWindowingMode(), r.getActivityType(), true /*onTop*/);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002391 }
2392
2393 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2394 return null;
2395 }
2396
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002397 boolean isValidLaunchStackId(int stackId, int displayId, ActivityRecord r) {
2398 switch (stackId) {
2399 case INVALID_STACK_ID:
2400 case HOME_STACK_ID:
2401 return false;
2402 case FULLSCREEN_WORKSPACE_STACK_ID:
2403 return true;
2404 case FREEFORM_WORKSPACE_STACK_ID:
2405 return r.supportsFreeform();
2406 case DOCKED_STACK_ID:
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002407 return r.supportsSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002408 case PINNED_STACK_ID:
2409 return r.supportsPictureInPicture();
2410 case RECENTS_STACK_ID:
2411 return r.isActivityTypeRecents();
2412 case ASSISTANT_STACK_ID:
2413 return r.isActivityTypeAssistant();
2414 default:
2415 if (StackId.isDynamicStack(stackId)) {
2416 return r.canBeLaunchedOnDisplay(displayId);
2417 }
2418 Slog.e(TAG, "isValidLaunchStackId: Unexpected stackId=" + stackId);
2419 return false;
2420 }
2421 }
2422
Craig Mautner967212c2013-04-13 21:10:58 -07002423 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002424 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002425 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2426 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002427 }
2428 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002429 }
2430
Jorim Jaggife762342016-10-13 14:33:27 +02002431 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2432 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2433 }
2434
Andrii Kulian7fc22812016-12-28 13:04:11 -08002435 /**
2436 * Get next focusable stack in the system. This will search across displays and stacks
2437 * in last-focused order for a focusable and visible stack, different from the target stack.
2438 *
2439 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2440 * searching for next candidate.
2441 * @return Next focusable {@link ActivityStack}, null if not found.
2442 */
2443 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2444 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2445
2446 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2447 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002448 // If a display is registered in WM, it must also be available in AM.
2449 @SuppressWarnings("ConstantConditions")
2450 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002451 for (int j = stacks.size() - 1; j >= 0; --j) {
2452 final ActivityStack stack = stacks.get(j);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002453 if (stack != currentFocus && stack.isFocusable()
2454 && stack.shouldBeVisible(null)) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002455 return stack;
2456 }
2457 }
2458 }
2459
2460 return null;
2461 }
2462
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002463 /**
2464 * Get next valid stack for launching provided activity in the system. This will search across
2465 * displays and stacks in last-focused order for a focusable and visible stack, except those
2466 * that are on a currently focused display.
2467 *
2468 * @param r The activity that is being launched.
2469 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2470 * searching for the next candidate.
2471 * @return Next valid {@link ActivityStack}, null if not found.
2472 */
2473 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2474 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2475 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2476 final int displayId = mTmpOrderedDisplayIds.get(i);
2477 if (displayId == currentFocus) {
2478 continue;
2479 }
2480 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2481 if (stack != null) {
2482 return stack;
2483 }
2484 }
2485 return null;
2486 }
2487
Craig Mautneree2e45a2014-06-27 12:10:03 -07002488 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002489 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002490 }
2491
2492 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2494 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2495 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002496 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002497 final ArrayList<ActivityRecord> activities = task.mActivities;
2498 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2499 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002500 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002501 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002502 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002503 }
2504 }
2505 }
2506 }
2507 return null;
2508 }
2509
Jorim Jaggidc249c42015-12-15 14:57:31 -08002510 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002511 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002512 if (stackId == DOCKED_STACK_ID) {
2513 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002514 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002515 return;
2516 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002517 final ActivityStack stack = getStack(stackId);
2518 if (stack == null) {
2519 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2520 return;
2521 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002522
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002523 final boolean splitScreenActive = getDefaultDisplay().hasSplitScreenStack();
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002524 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002525 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002526 // If the docked stack exists, don't resize non-floating stacks independently of the
2527 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002528 return;
2529 }
2530
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002531 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002532 mWindowManager.deferSurfaceLayout();
2533 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002534 if (stack.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002535 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2536 // null bounds = fullscreen windowing mode...at least for now.
2537 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2538 } else if (splitScreenActive) {
2539 // If we are in split-screen mode and this stack support split-screen, then
2540 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2541 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2542 }
2543 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002544 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002545 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002546 stack.ensureVisibleActivitiesConfigurationLocked(
2547 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002548 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002549 } finally {
2550 mWindowManager.continueSurfaceLayout();
2551 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002552 }
2553 }
2554
Wale Ogunwale926aade2017-08-29 11:24:37 -07002555 private void deferUpdateBounds(int stackId) {
Jorim Jaggi192086e2016-03-11 17:17:03 +01002556 final ActivityStack stack = getStack(stackId);
2557 if (stack != null) {
2558 stack.deferUpdateBounds();
2559 }
2560 }
2561
Wale Ogunwale926aade2017-08-29 11:24:37 -07002562 private void continueUpdateBounds(int stackId) {
Jorim Jaggi192086e2016-03-11 17:17:03 +01002563 final ActivityStack stack = getStack(stackId);
2564 if (stack != null) {
2565 stack.continueUpdateBounds();
2566 }
2567 }
2568
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002569 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002570 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002571 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2572 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002573 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002574 if (task != null) {
2575 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002576 }
2577 }
2578 mResizingTasksDuringAnimation.clear();
2579 }
2580
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002581 /**
2582 * TODO: This should just change the windowing mode and resize vs. actually moving task around.
2583 * Can do that once we are no longer using static stack ids. Specially when
2584 * {@link ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} is removed.
2585 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002586 private void moveTasksToFullscreenStackInSurfaceTransaction(ActivityStack fromStack,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002587 int toDisplayId, boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002588
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002589 mWindowManager.deferSurfaceLayout();
2590 try {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002591 final int windowingMode = fromStack.getWindowingMode();
2592 final boolean inPinnedWindowingMode = windowingMode == WINDOWING_MODE_PINNED;
2593 final ActivityDisplay toDisplay = getActivityDisplay(toDisplayId);
2594
2595 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002596 // We are moving all tasks from the docked stack to the fullscreen stack,
2597 // which is dismissing the docked stack, so resize all other stacks to
2598 // fullscreen here already so we don't end up with resize trashing.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002599 final ArrayList<ActivityStack> displayStacks = toDisplay.mStacks;
2600 for (int i = displayStacks.size() - 1; i >= 0; --i) {
2601 final ActivityStack otherStack = displayStacks.get(i);
Wale Ogunwale926aade2017-08-29 11:24:37 -07002602 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2603 continue;
2604 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002605 resizeStackLocked(otherStack.mStackId, null, null, null, PRESERVE_WINDOWS,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002606 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002607 }
2608
2609 // Also disable docked stack resizing since we have manually adjusted the
2610 // size of other stacks above and we don't want to trigger a docked stack
2611 // resize when we remove task from it below and it is detached from the
2612 // display because it no longer contains any tasks.
2613 mAllowDockedStackResize = false;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002614 } else if (inPinnedWindowingMode && onTop) {
2615 // Log if we are expanding the PiP to fullscreen
2616 MetricsLogger.action(mService.mContext,
2617 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002618 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002619
Winson Chung5af42fc2017-03-24 17:11:33 -07002620 // If we are moving from the pinned stack, then the animation takes care of updating
2621 // the picture-in-picture mode.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002622 final boolean schedulePictureInPictureModeChange = inPinnedWindowingMode;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002623 final ArrayList<TaskRecord> tasks = fromStack.getAllTasks();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002624 final int size = tasks.size();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002625 final ActivityStack fullscreenStack = toDisplay.getOrCreateStack(
2626 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, onTop);
2627
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002628 if (onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002629 final int returnToType =
2630 toDisplay.getTopVisibleStackActivityType(WINDOWING_MODE_PINNED);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002631 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002632 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002633 final boolean isTopTask = i == (size - 1);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002634 if (inPinnedWindowingMode) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002635 // Update the return-to to reflect where the pinned stack task was moved
2636 // from so that we retain the stack that was previously visible if the
2637 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002638 task.setTaskToReturnTo(returnToType);
Winson Chung0c1ca092016-11-10 17:40:34 -08002639 }
Winson Chung74666102017-02-22 17:49:24 -08002640 // Defer resume until all the tasks have been moved to the fullscreen stack
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002641 task.reparent(fullscreenStack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT,
2642 isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002643 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002644 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002645 }
2646 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002647 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002648 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002649 // Position the tasks in the fullscreen stack in order at the bottom of the
2650 // stack. Also defer resume until all the tasks have been moved to the
2651 // fullscreen stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002652 task.reparent(fullscreenStack, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002653 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2654 schedulePictureInPictureModeChange,
2655 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002656 }
2657 }
Winson Chung74666102017-02-22 17:49:24 -08002658
2659 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2660 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002661 } finally {
2662 mAllowDockedStackResize = true;
2663 mWindowManager.continueSurfaceLayout();
2664 }
2665 }
2666
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002667 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, boolean onTop) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002668 moveTasksToFullscreenStackLocked(fromStack, DEFAULT_DISPLAY, onTop);
2669 }
2670
2671 void moveTasksToFullscreenStackLocked(ActivityStack fromStack, int toDisplayId, boolean onTop) {
2672 mWindowManager.inSurfaceTransaction(() ->
2673 moveTasksToFullscreenStackInSurfaceTransaction(fromStack, toDisplayId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002674 }
2675
Jorim Jaggidc249c42015-12-15 14:57:31 -08002676 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002677 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2678 boolean preserveWindows) {
2679 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2680 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2681 false /* deferResume */);
2682 }
2683
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002684 private void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002685 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2686 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002687
2688 if (!mAllowDockedStackResize) {
2689 // Docked stack resize currently disabled.
2690 return;
2691 }
2692
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002693 final ActivityStack stack = getDefaultDisplay().getStack(
2694 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002695 if (stack == null) {
2696 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2697 return;
2698 }
2699
2700 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2701 mWindowManager.deferSurfaceLayout();
2702 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002703 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2704 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002705 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002706 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002707
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002708 // TODO: Checking for isAttached might not be needed as if the user passes in null
2709 // dockedBounds then they want the docked stack to be dismissed.
2710 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2711 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002712 // In this case we make all other static stacks fullscreen and move all
2713 // docked stack tasks to the fullscreen stack.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002714 moveTasksToFullscreenStackLocked(stack, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002715
2716 // stack shouldn't contain anymore activities, so nothing to resume.
2717 r = null;
2718 } else {
2719 // Docked stacks occupy a dedicated region on screen so the size of all other
2720 // static stacks need to be adjusted so they don't overlap with the docked stack.
2721 // We get the bounds to use from window manager which has been adjusted for any
2722 // screen controls and is also the same for all stacks.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002723 final ArrayList<ActivityStack> stacks = getStacksOnDefaultDisplay();
Matthew Ngaa2b6202017-02-10 14:48:21 -08002724 final Rect otherTaskRect = new Rect();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002725 for (int i = stacks.size() - 1; i >= 0; --i) {
2726 final ActivityStack current = stacks.get(i);
2727 if (current.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002728 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002729 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002730 if (!current.supportsSplitScreenWindowingMode()) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002731 continue;
2732 }
2733 // Need to set windowing mode here before we try to get the dock bounds.
2734 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2735 current.getStackDockedModeBounds(
2736 tempOtherTaskBounds /* currentTempTaskBounds */,
2737 tempRect /* outStackBounds */,
2738 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2739
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002740 resizeStackLocked(current.mStackId, !tempRect.isEmpty() ? tempRect : null,
Wale Ogunwale926aade2017-08-29 11:24:37 -07002741 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2742 tempOtherTaskInsetBounds, preserveWindows,
2743 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002744 }
2745 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002746 if (!deferResume) {
2747 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2748 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002749 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002750 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002751 mWindowManager.continueSurfaceLayout();
2752 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2753 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002754 }
2755
Robert Carr0d00c2e2016-02-29 17:45:02 -08002756 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002757 // TODO(multi-display): Pinned stack display should be passed in.
2758 final PinnedActivityStack stack = getDefaultDisplay().getStack(
2759 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002760 if (stack == null) {
2761 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2762 return;
2763 }
Winson Chung19953ca2017-04-11 11:19:23 -07002764
2765 // It is possible for the bounds animation from the WM to call this but be delayed by
2766 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2767 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2768 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2769 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002770 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002771 return;
2772 }
2773
Robert Carr0d00c2e2016-02-29 17:45:02 -08002774 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2775 mWindowManager.deferSurfaceLayout();
2776 try {
2777 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002778 Rect insetBounds = null;
2779 if (tempPinnedTaskBounds != null) {
2780 // We always use 0,0 as the position for the inset rect because
2781 // if we are getting insets at all in the pinned stack it must mean
2782 // we are headed for fullscreen.
2783 insetBounds = tempRect;
2784 insetBounds.top = 0;
2785 insetBounds.left = 0;
2786 insetBounds.right = tempPinnedTaskBounds.width();
2787 insetBounds.bottom = tempPinnedTaskBounds.height();
2788 }
2789 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002790 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002791 } finally {
2792 mWindowManager.continueSurfaceLayout();
2793 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2794 }
2795 }
2796
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002797 private void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002798 final ActivityStack stack = getStack(stackId);
2799 if (stack == null) {
2800 return;
2801 }
2802
2803 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002804 if (stack.getWindowingMode() == WINDOWING_MODE_PINNED) {
Winson Chung47900652017-04-06 18:44:25 -07002805 /**
2806 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2807 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2808 * that the client receives onStop() before it is reparented. We do this by detaching
2809 * the stack from the display so that it will be considered invisible when
2810 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2811 * invisible as well and added to the stopping list. After which we process the
2812 * stopping list by handling the idle.
2813 */
2814 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2815 pinnedStack.mForceHidden = true;
2816 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2817 pinnedStack.mForceHidden = false;
2818 activityIdleInternalLocked(null, false /* fromTimeout */,
2819 true /* processPausingActivites */, null /* configuration */);
2820
2821 // Move all the tasks to the bottom of the fullscreen stack
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002822 moveTasksToFullscreenStackLocked(pinnedStack, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002823 } else {
2824 for (int i = tasks.size() - 1; i >= 0; i--) {
2825 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2826 REMOVE_FROM_RECENTS);
2827 }
2828 }
2829 }
2830
2831 /**
Robert Carr6914f082017-03-20 19:04:30 -07002832 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2833 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2834 * instead moved back onto the fullscreen stack.
2835 */
2836 void removeStackLocked(int stackId) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002837 mWindowManager.inSurfaceTransaction(() -> removeStackInSurfaceTransaction(stackId));
2838 }
2839
2840 /** Removes all stacks in the input windowing mode from the system */
2841 void removeStacksInWindowingMode(int windowingMode) {
2842 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
2843 mActivityDisplays.valueAt(i).removeStacksInWindowingMode(windowingMode);
2844 }
Robert Carr6914f082017-03-20 19:04:30 -07002845 }
2846
2847 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002848 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2849 */
2850 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2851 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2852 }
2853
2854 /**
Winson Chung010927a2016-12-15 16:12:35 -08002855 * Removes the task with the specified task id.
2856 *
2857 * @param taskId Identifier of the task to be removed.
2858 * @param killProcess Kill any process associated with the task if possible.
2859 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002860 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2861 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002862 * @return Returns true if the given task was found and removed.
2863 */
Winson Chung6954fc92017-03-24 16:22:12 -07002864 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2865 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002866 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002867 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002868 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002869 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2870 if (tr.isPersistable) {
2871 mService.notifyTaskPersisterLocked(null, true);
2872 }
2873 return true;
2874 }
2875 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2876 return false;
2877 }
2878
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002879 void addRecentActivity(ActivityRecord r) {
2880 if (r == null) {
2881 return;
2882 }
2883 final TaskRecord task = r.getTask();
2884 mRecentTasks.addLocked(task);
2885 task.touchActiveTime();
2886 }
2887
2888 void removeTaskFromRecents(TaskRecord task) {
2889 mRecentTasks.remove(task);
2890 task.removedFromRecents();
2891 }
2892
Winson Chung010927a2016-12-15 16:12:35 -08002893 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2894 if (removeFromRecents) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002895 removeTaskFromRecents(tr);
Winson Chung010927a2016-12-15 16:12:35 -08002896 }
2897 ComponentName component = tr.getBaseIntent().getComponent();
2898 if (component == null) {
2899 Slog.w(TAG, "No component for base intent of task: " + tr);
2900 return;
2901 }
2902
2903 // Find any running services associated with this app and stop if needed.
2904 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2905
2906 if (!killProcess) {
2907 return;
2908 }
2909
2910 // Determine if the process(es) for this task should be killed.
2911 final String pkg = component.getPackageName();
2912 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2913 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2914 for (int i = 0; i < pmap.size(); i++) {
2915
2916 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2917 for (int j = 0; j < uids.size(); j++) {
2918 ProcessRecord proc = uids.valueAt(j);
2919 if (proc.userId != tr.userId) {
2920 // Don't kill process for a different user.
2921 continue;
2922 }
2923 if (proc == mService.mHomeProcess) {
2924 // Don't kill the home process along with tasks from the same package.
2925 continue;
2926 }
2927 if (!proc.pkgList.containsKey(pkg)) {
2928 // Don't kill process that is not associated with this task.
2929 continue;
2930 }
2931
2932 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002933 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002934 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2935 // Don't kill process(es) that has an activity in a different task that is
2936 // also in recents.
2937 return;
2938 }
2939 }
2940
2941 if (proc.foregroundServices) {
2942 // Don't kill process(es) with foreground service.
2943 return;
2944 }
2945
2946 // Add process to kill list.
2947 procsToKill.add(proc);
2948 }
2949 }
2950
2951 // Kill the running processes.
2952 for (int i = 0; i < procsToKill.size(); i++) {
2953 ProcessRecord pr = procsToKill.get(i);
2954 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2955 && pr.curReceivers.isEmpty()) {
2956 pr.kill("remove task", true);
2957 } else {
2958 // We delay killing processes that are not in the background or running a receiver.
2959 pr.waitingToKill = "remove task";
2960 }
2961 }
2962 }
2963
Craig Mautner4a1cb222013-12-04 16:14:06 -08002964 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002965 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002966 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2967 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002968 break;
2969 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002970 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002972 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002973 }
2974
Chong Zhang5dcb2752015-08-18 13:50:26 -07002975 /**
2976 * Restores a recent task to a stack
2977 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002978 * @param aOptions The activity options to use for restoration.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002979 * @return true if the task has been restored successfully.
2980 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002981 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002982 final ActivityStack stack = getLaunchStack(null, aOptions, task, !ON_TOP);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002983 final ActivityStack currentStack = task.getStack();
2984 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002985 // Task has already been restored once. See if we need to do anything more
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002986 if (currentStack == stack) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002987 // Nothing else to do since it is already restored in the right stack.
2988 return true;
2989 }
2990 // Remove current stack association, so we can re-associate the task with the
2991 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002992 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002993 }
2994
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002995 stack.addTask(task, !ON_TOP, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002996 // TODO: move call for creation here and other place into Stack.addTask()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002997 task.createWindowContainer(!ON_TOP, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002998 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2999 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003000 final ArrayList<ActivityRecord> activities = task.mActivities;
3001 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003002 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08003003 }
3004 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003005 }
3006
Wale Ogunwale040b4702015-08-06 18:10:50 -07003007 /**
Andrii Kulian839def92016-11-02 10:58:58 -07003008 * Move stack with all its existing content to specified display.
3009 * @param stackId Id of stack to move.
3010 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08003011 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07003012 */
Andrii Kulian250d6532017-02-08 23:30:45 -08003013 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07003014 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07003015 if (activityDisplay == null) {
3016 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
3017 + displayId);
3018 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003019 final ActivityStack stack = getStack(stackId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003020 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07003021 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
3022 + stackId);
3023 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003024
3025 final ActivityDisplay currentDisplay = stack.getDisplay();
3026 if (currentDisplay == null) {
3027 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3028 + " is not attached to any display.");
3029 }
3030
3031 if (currentDisplay.mDisplayId == displayId) {
3032 throw new IllegalArgumentException("Trying to move stack=" + stack
3033 + " to its current displayId=" + displayId);
3034 }
3035
3036 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003037 // TODO(multi-display): resize stacks properly if moved from split-screen.
3038 }
3039
3040 /**
Winson Chung74666102017-02-22 17:49:24 -08003041 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3042 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003043 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003044 ActivityStack getReparentTargetStack(TaskRecord task, ActivityStack stack, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003045 final ActivityStack prevStack = task.getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003046 final int stackId = stack.mStackId;
3047 final boolean inMultiWindowMode = stack.inMultiWindowMode();
Chong Zhang02898352015-08-21 17:27:14 -07003048
Winson Chung74666102017-02-22 17:49:24 -08003049 // Check that we aren't reparenting to the same stack that the task is already in
3050 if (prevStack != null && prevStack.mStackId == stackId) {
3051 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3052 + " already in stackId=" + stackId);
3053 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003054 }
3055
Winson Chung74666102017-02-22 17:49:24 -08003056 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3057 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003058 if (inMultiWindowMode && !mService.mSupportsMultiWindow) {
Winson Chung74666102017-02-22 17:49:24 -08003059 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003060 + " reparent task=" + task + " to stack=" + stack);
Winson Chungc2baac02017-01-11 13:34:47 -08003061 }
3062
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003063 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3064 // multi-display.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003065 if (stack.mDisplayId != DEFAULT_DISPLAY && !mService.mSupportsMultiDisplay) {
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003066 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3067 + " reparent task=" + task + " to stackId=" + stackId);
3068 }
3069
Winson Chung74666102017-02-22 17:49:24 -08003070 // Ensure that we aren't trying to move into a freeform stack without freeform
3071 // support
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003072 if (stack.getWindowingMode() == WINDOWING_MODE_FREEFORM
3073 && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003074 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3075 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003076 }
3077
Winson Chung74666102017-02-22 17:49:24 -08003078 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3079 // used for split-screen mode and will cause things like the docked divider to show up. We
3080 // instead leave the task in its current stack or move it to the fullscreen stack if it
3081 // isn't currently in a stack.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003082 if (inMultiWindowMode && !task.isResizeable()) {
Winson Chung74666102017-02-22 17:49:24 -08003083 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3084 + " Moving to stackId=" + stackId + " instead.");
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003085 // Temporarily disable resizeablility of the task as we don't want it to be resized if,
3086 // for example, a docked stack is created which will lead to the stack we are moving
3087 // from being resized and and its resizeable tasks being resized.
3088 try {
3089 task.mTemporarilyUnresizable = true;
3090 stack = stack.getDisplay().createStack(
3091 WINDOWING_MODE_FULLSCREEN, stack.getActivityType(), toTop);
3092 } finally {
3093 task.mTemporarilyUnresizable = false;
3094 }
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003095 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003096 return stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003097 }
3098
Winson Chung08f81892017-03-02 15:40:51 -08003099 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003100 final ActivityStack stack = getStack(stackId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003101 if (stack == null) {
3102 throw new IllegalArgumentException(
3103 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3104 }
3105
3106 final ActivityRecord r = stack.topRunningActivityLocked();
3107 if (r == null) {
3108 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3109 + " in stack=" + stack);
3110 return false;
3111 }
3112
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003113 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003114 Slog.w(TAG,
3115 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003116 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003117 return false;
3118 }
3119
Winson Chung3a682872017-04-10 14:38:05 -07003120 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003121 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003122 return true;
3123 }
3124
Winson Chung8bca9e42017-04-16 15:59:43 -07003125 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003126 boolean moveHomeStackToFront, String reason) {
3127
Wale Ogunwale480dca02016-02-06 13:58:29 -08003128 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003129
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003130 final ActivityDisplay display = r.getStack().getDisplay();
3131 PinnedActivityStack stack = display.getPinnedStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003132
Bryce Lee04ab3462017-04-10 15:06:33 -07003133 // This will clear the pinned stack by moving an existing task to the full screen stack,
3134 // ensuring only one task is present.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003135 if (stack != null) {
3136 moveTasksToFullscreenStackLocked(stack, !ON_TOP);
3137 }
Bryce Lee04ab3462017-04-10 15:06:33 -07003138
Wale Ogunwale1666e312016-12-16 11:27:18 -08003139 // Need to make sure the pinned stack exist so we can resize it below...
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003140 stack = display.getOrCreateStack(WINDOWING_MODE_PINNED, r.getActivityType(), ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003141
Wale Ogunwale480dca02016-02-06 13:58:29 -08003142 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003143 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003144 // Resize the pinned stack to match the current size of the task the activity we are
3145 // going to be moving is currently contained in. We do this to have the right starting
3146 // animation bounds for the pinned stack to the desired bounds the caller wants.
3147 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3148 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003149 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003150
3151 if (task.mActivities.size() == 1) {
3152 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003153 // the stack without re-parenting the activity in a different task. We don't
3154 // move the home stack forward if we are currently entering picture-in-picture
3155 // while pausing because that changes the focused stack and may prevent the new
3156 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003157 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003158 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003159 // Move the home stack forward if the task we just moved to the pinned stack
3160 // was launched from home so home should be visible behind it.
3161 moveHomeStackToFront(reason);
3162 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003163 // Defer resume until below, and do not schedule PiP changes until we animate below
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003164 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE, DEFER_RESUME,
3165 false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003166 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003167 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003168 // reveal/leave the other activities in their original task.
3169
3170 // Currently, we don't support reparenting activities across tasks in two different
3171 // stacks, so instead, just create a new task in the same stack, reparent the
3172 // activity into that task, and then reparent the whole task to the new stack. This
3173 // ensures that all the necessary work to migrate states in the old and new stacks
3174 // is also done.
3175 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003176 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003177 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
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 newTask.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003181 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003182 }
Winson Chungc2baac02017-01-11 13:34:47 -08003183
3184 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3185 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003186 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003187 } finally {
3188 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003189 }
3190
Winson Chunge7ba6862017-05-24 12:13:33 -07003191 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3192 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3193 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003194 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3195
Winson Chunge7ba6862017-05-24 12:13:33 -07003196 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3197 true /* fromFullscreen */);
3198
3199 // Update the visibility of all activities after the they have been reparented to the new
3200 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3201 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3202 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003203 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003204 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003205
Winson Chung85d3c8a2017-09-15 15:41:00 -07003206 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName, r.userId,
Winson Chungb5026902017-05-03 12:45:13 -07003207 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003208 }
3209
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003210 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003211 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3212 if (r == null || !r.isFocusable()) {
3213 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3214 "moveActivityStackToFront: unfocusable r=" + r);
3215 return false;
3216 }
3217
Bryce Leeaf691c02017-03-20 14:20:22 -07003218 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003219 final ActivityStack stack = r.getStack();
3220 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003221 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3222 + r + " task=" + task);
3223 return false;
3224 }
3225
Chong Zhang6cda19c2016-06-14 19:07:56 -07003226 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3227 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3228 "moveActivityStackToFront: already on top, r=" + r);
3229 return false;
3230 }
3231
3232 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3233 "moveActivityStackToFront: r=" + r);
3234
3235 stack.moveToFront(reason, task);
3236 return true;
3237 }
3238
David Stevensc6b91c62017-02-08 14:23:58 -08003239 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003240 mTmpFindTaskResult.r = null;
3241 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003242 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003243 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003244 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3245 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003246 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3247 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003248 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003249 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3250 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003251 continue;
3252 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003253 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003254 // It is possible to have tasks in multiple stacks with the same root affinity, so
3255 // we should keep looking after finding an affinity match to see if there is a
3256 // better match in another stack. Also, task affinity isn't a good enough reason
3257 // to target a display which isn't the source of the intent, so skip any affinity
3258 // matches not on the specified display.
3259 if (mTmpFindTaskResult.r != null) {
3260 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3261 return mTmpFindTaskResult.r;
3262 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003263 // Note: since the traversing through the stacks is top down, the floating
3264 // tasks should always have lower priority than any affinity-matching tasks
3265 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003266 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003267 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3268 Slog.d(TAG_TASKS, "Skipping match on different display "
3269 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003270 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003272 }
3273 }
Winson Chung5b895b72017-05-01 13:46:25 -07003274
David Stevensc6b91c62017-02-08 14:23:58 -08003275 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3276 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003277 }
3278
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003279 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3280 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003281 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3282 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003284 final ActivityRecord ar = stacks.get(stackNdx)
3285 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003286 if (ar != null) {
3287 return ar;
3288 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003289 }
3290 }
3291 return null;
3292 }
3293
David Stevens9440dc82017-03-16 19:00:20 -07003294 boolean hasAwakeDisplay() {
3295 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3296 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3297 if (!display.shouldSleep()) {
3298 return true;
3299 }
3300 }
3301 return false;
3302 }
3303
Craig Mautner8d341ef2013-03-26 09:03:27 -07003304 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003305 scheduleSleepTimeout();
3306 if (!mGoingToSleep.isHeld()) {
3307 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003308 if (mLaunchingActivity.isHeld()) {
3309 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3310 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003311 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003312 mLaunchingActivity.release();
3313 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003314 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003315 }
David Stevens9440dc82017-03-16 19:00:20 -07003316
3317 applySleepTokensLocked(false /* applyToStacks */);
3318
3319 checkReadyForSleepLocked(true /* allowDelay */);
3320 }
3321
3322 void prepareForShutdownLocked() {
3323 for (int i = 0; i < mActivityDisplays.size(); i++) {
3324 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3325 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003326 }
3327
3328 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003329 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003330
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003331 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003332 final long endTime = System.currentTimeMillis() + timeout;
3333 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003334 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003335 long timeRemaining = endTime - System.currentTimeMillis();
3336 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003337 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003338 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003339 } catch (InterruptedException e) {
3340 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341 } else {
3342 Slog.w(TAG, "Activity manager shutdown timed out");
3343 timedout = true;
3344 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003345 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003346 } else {
3347 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003348 }
3349 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003350
3351 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003352 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003353
Craig Mautner8d341ef2013-03-26 09:03:27 -07003354 return timedout;
3355 }
3356
3357 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003358 removeSleepTimeouts();
3359 if (mGoingToSleep.isHeld()) {
3360 mGoingToSleep.release();
3361 }
David Stevens9440dc82017-03-16 19:00:20 -07003362 }
3363
3364 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003365 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003366 // Set the sleeping state of the display.
3367 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3368 final boolean displayShouldSleep = display.shouldSleep();
3369 if (displayShouldSleep == display.isSleeping()) {
3370 continue;
3371 }
3372 display.setIsSleeping(displayShouldSleep);
3373
3374 if (!applyToStacks) {
3375 continue;
3376 }
3377
3378 // Set the sleeping state of the stacks on the display.
3379 final ArrayList<ActivityStack> stacks = display.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003380 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3381 final ActivityStack stack = stacks.get(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003382 if (displayShouldSleep) {
3383 stack.goToSleepIfPossible(false /* shuttingDown */);
3384 } else {
3385 stack.awakeFromSleepingLocked();
3386 if (isFocusedStack(stack)) {
3387 resumeFocusedStackTopActivityLocked();
3388 }
3389 }
3390 }
3391
3392 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3393 continue;
3394 }
3395 // The display is awake now, so clean up the going to sleep list.
3396 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3397 final ActivityRecord r = it.next();
3398 if (r.getDisplayId() == display.mDisplayId) {
3399 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003400 }
Craig Mautner5314a402013-09-26 12:40:16 -07003401 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003402 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003403 }
3404
3405 void activitySleptLocked(ActivityRecord r) {
3406 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003407 final ActivityStack s = r.getStack();
3408 if (s != null) {
3409 s.checkReadyForSleep();
3410 } else {
3411 checkReadyForSleepLocked(true);
3412 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003413 }
3414
David Stevens9440dc82017-03-16 19:00:20 -07003415 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003416 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003417 // Do not care.
3418 return;
3419 }
3420
David Stevens18abd0e2017-08-17 14:55:47 -07003421 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3422 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423 }
3424
Wei Wang65c7a152016-06-02 18:51:22 -07003425 // Send launch end powerhint before going sleep
3426 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3427
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 removeSleepTimeouts();
3429
3430 if (mGoingToSleep.isHeld()) {
3431 mGoingToSleep.release();
3432 }
3433 if (mService.mShuttingDown) {
3434 mService.notifyAll();
3435 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 }
3437
David Stevens18abd0e2017-08-17 14:55:47 -07003438 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3439 // successfully put to sleep.
3440 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3441 boolean allSleep = true;
3442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3444 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3445 if (allowDelay) {
3446 allSleep &= stacks.get(stackNdx).goToSleepIfPossible(shuttingDown);
3447 } else {
3448 stacks.get(stackNdx).goToSleep();
3449 }
3450 }
3451 }
3452 return allSleep;
3453 }
3454
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003455 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003456 // A resumed activity cannot be stopping. remove from list
3457 mStoppingActivities.remove(r);
3458
Andrii Kulian02b7a832016-10-06 23:11:56 -07003459 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003460 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003461 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003462 }
3463 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003464 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003465 mWindowManager.executeAppTransition();
3466 return true;
3467 }
3468 return false;
3469 }
3470
Craig Mautner8d341ef2013-03-26 09:03:27 -07003471 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003472 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3473 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003474 int stackNdx = stacks.size() - 1;
3475 while (stackNdx >= 0) {
3476 stacks.get(stackNdx).handleAppCrashLocked(app);
3477 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003478 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003479 }
3480 }
3481
Craig Mautnerbb742462014-07-07 15:28:55 -07003482 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003483 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003484 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003485 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003486
3487 r.mLaunchTaskBehind = false;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003488 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003489 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003490 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003491
3492 // When launching tasks behind, update the last active time of the top task after the new
3493 // task has been shown briefly
3494 final ActivityRecord top = stack.topActivity();
3495 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003496 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003497 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003498 }
3499
3500 void scheduleLaunchTaskBehindComplete(IBinder token) {
3501 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3502 }
3503
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003504 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3505 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003506 mKeyguardController.beginActivityVisibilityUpdate();
3507 try {
3508 // First the front stacks. In case any are not fullscreen and are in front of home.
3509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3510 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3511 final int topStackNdx = stacks.size() - 1;
3512 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3513 final ActivityStack stack = stacks.get(stackNdx);
3514 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3515 }
Craig Mautner580ea812013-04-25 12:58:38 -07003516 }
Jorim Jaggife762342016-10-13 14:33:27 +02003517 } finally {
3518 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003519 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003520 }
3521
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003522 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3523 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3524 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3525 final int topStackNdx = stacks.size() - 1;
3526 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3527 final ActivityStack stack = stacks.get(stackNdx);
3528 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3529 }
3530 }
3531 }
3532
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003533 void invalidateTaskLayers() {
3534 mTaskLayersChanged = true;
3535 }
3536
3537 void rankTaskLayersIfNeeded() {
3538 if (!mTaskLayersChanged) {
3539 return;
3540 }
3541 mTaskLayersChanged = false;
3542 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3543 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3544 int baseLayer = 0;
3545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3546 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3547 }
3548 }
3549 }
3550
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003551 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3552 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3553 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3554 final int topStackNdx = stacks.size() - 1;
3555 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3556 final ActivityStack stack = stacks.get(stackNdx);
3557 stack.clearOtherAppTimeTrackers(except);
3558 }
3559 }
3560 }
3561
Craig Mautner8d341ef2013-03-26 09:03:27 -07003562 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003563 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3564 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 final int numStacks = stacks.size();
3566 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3567 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003568 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003570 }
3571 }
3572
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003573 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3574 // Examine all activities currently running in the process.
3575 TaskRecord firstTask = null;
3576 // Tasks is non-null only if two or more tasks are found.
3577 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003578 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3579 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003580 ActivityRecord r = app.activities.get(i);
3581 // First, if we find an activity that is in the process of being destroyed,
3582 // then we just aren't going to do anything for now; we want things to settle
3583 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003584 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003585 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003586 return;
3587 }
3588 // Don't consider any activies that are currently not in a state where they
3589 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003590 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3591 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003593 continue;
3594 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003595
3596 final TaskRecord task = r.getTask();
3597 if (task != null) {
3598 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003599 + " from " + r);
3600 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003601 firstTask = task;
3602 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003603 if (tasks == null) {
3604 tasks = new ArraySet<>();
3605 tasks.add(firstTask);
3606 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003607 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003608 }
3609 }
3610 }
3611 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003612 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003613 return;
3614 }
3615 // If we have activities in multiple tasks that are in a position to be destroyed,
3616 // let's iterate through the tasks and release the oldest one.
3617 final int numDisplays = mActivityDisplays.size();
3618 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3619 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3620 // Step through all stacks starting from behind, to hit the oldest things first.
3621 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3622 final ActivityStack stack = stacks.get(stackNdx);
3623 // Try to release activities in this stack; if we manage to, we are done.
3624 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3625 return;
3626 }
3627 }
3628 }
3629 }
3630
Amith Yamasani37a40c22015-06-17 13:25:42 -07003631 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003632 final int focusStackId = mFocusedStack.getStackId();
3633 // We dismiss the docked stack whenever we switch users.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07003634 final ActivityStack dockedStack = getDefaultDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003635 if (dockedStack != null) {
3636 moveTasksToFullscreenStackLocked(dockedStack, mFocusedStack == dockedStack);
3637 }
Winson Chungc2b23a52017-01-09 15:44:55 -08003638 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3639 // also cause all tasks to be moved to the fullscreen stack at a position that is
3640 // appropriate.
3641 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003642
3643 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003644 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003645 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003646
Craig Mautner858d8a62013-04-23 17:08:34 -07003647 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003648 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3649 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003651 final ActivityStack stack = stacks.get(stackNdx);
3652 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003653 TaskRecord task = stack.topTask();
3654 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003655 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003656 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003657 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003658 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003659
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003660 ActivityStack stack = getStack(restoreStackId);
3661 if (stack == null) {
3662 stack = mHomeStack;
3663 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003664 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003665 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003666 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003667 } else {
3668 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003669 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003670 }
Craig Mautner93529a42013-10-04 15:03:13 -07003671 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003672 }
3673
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003674 /** Checks whether the userid is a profile of the current user. */
3675 boolean isCurrentProfileLocked(int userId) {
3676 if (userId == mCurrentUser) return true;
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07003677 return mService.mUserController.isCurrentProfile(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003678 }
3679
Bryce Leeb7c9b802017-05-02 14:20:24 -07003680 /**
3681 * Returns whether a stopping activity is present that should be stopped after visible, rather
3682 * than idle.
3683 * @return {@code true} if such activity is present. {@code false} otherwise.
3684 */
3685 boolean isStoppingNoHistoryActivity() {
3686 // Activities that are marked as nohistory should be stopped immediately after the resumed
3687 // activity has become visible.
3688 for (ActivityRecord record : mStoppingActivities) {
3689 if (record.isNoHistory()) {
3690 return true;
3691 }
3692 }
3693
3694 return false;
3695 }
3696
Winson Chung4dabf232017-01-25 13:25:22 -08003697 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3698 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003699 ArrayList<ActivityRecord> stops = null;
3700
3701 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003702 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3703 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003704 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003705 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003706 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3707 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003708 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003709 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003710 if (s.finishing) {
3711 // If this activity is finishing, it is sitting on top of
3712 // everyone else but we now know it is no longer needed...
3713 // so get rid of it. Otherwise, we need to go through the
3714 // normal flow and hide it once we determine that it is
3715 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003716 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003717 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003718 }
3719 }
David Stevens9440dc82017-03-16 19:00:20 -07003720 if (remove) {
3721 final ActivityStack stack = s.getStack();
3722 final boolean shouldSleepOrShutDown = stack != null
3723 ? stack.shouldSleepOrShutDownActivities()
3724 : mService.isSleepingOrShuttingDownLocked();
3725 if (!waitingVisible || shouldSleepOrShutDown) {
3726 if (!processPausingActivities && s.state == PAUSING) {
3727 // Defer processing pausing activities in this iteration and reschedule
3728 // a delayed idle to reprocess it again
3729 removeTimeoutsForActivityLocked(idleActivity);
3730 scheduleIdleTimeoutLocked(idleActivity);
3731 continue;
3732 }
Winson Chung4dabf232017-01-25 13:25:22 -08003733
David Stevens9440dc82017-03-16 19:00:20 -07003734 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3735 if (stops == null) {
3736 stops = new ArrayList<>();
3737 }
3738 stops.add(s);
3739 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003740 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003741 }
3742 }
3743
3744 return stops;
3745 }
3746
Craig Mautnercf910b02013-04-23 11:23:27 -07003747 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3750 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3751 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003752 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003753 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003754 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003755 if (r == null) Slog.e(TAG,
3756 "validateTop...: null top activity, stack=" + stack);
3757 else {
3758 final ActivityRecord pausing = stack.mPausingActivity;
3759 if (pausing != null && pausing == r) Slog.e(TAG,
3760 "validateTop...: top stack has pausing activity r=" + r
3761 + " state=" + state);
3762 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3763 "validateTop...: activity in front not resumed r=" + r
3764 + " state=" + state);
3765 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003766 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003767 final ActivityRecord resumed = stack.mResumedActivity;
3768 if (resumed != null && resumed == r) Slog.e(TAG,
3769 "validateTop...: back stack has resumed activity r=" + r
3770 + " state=" + state);
3771 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3772 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003773 }
3774 }
3775 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003776 }
3777
Craig Mautner27084302013-03-25 08:05:25 -07003778 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003779 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003781 pw.print(prefix);
3782 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003783 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003784 for (int i = mActivityDisplays.size() - 1; i >= 0; --i) {
3785 final ActivityDisplay display = mActivityDisplays.valueAt(i);
3786 pw.println(prefix + "displayId=" + display.mDisplayId + " mStacks=" + display.mStacks);
3787 }
Bryce Lee4a194382017-04-04 14:32:48 -07003788 if (!mWaitingForActivityVisible.isEmpty()) {
3789 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3790 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3791 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3792 }
3793 }
3794
Jorim Jaggi8d786932016-10-26 19:08:36 -07003795 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003796 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003797 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003798
Steven Timotius4346f0a2017-09-12 11:07:21 -07003799 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3800 final long token = proto.start(fieldId);
3801 super.writeToProto(proto, CONFIGURATION_CONTAINER);
3802 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3803 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
3804 activityDisplay.writeToProto(proto, DISPLAYS);
3805 }
3806 mKeyguardController.writeToProto(proto, KEYGUARD_CONTROLLER);
3807 if (mFocusedStack != null) {
3808 proto.write(FOCUSED_STACK_ID, mFocusedStack.mStackId);
3809 ActivityRecord focusedActivity = getResumedActivityLocked();
3810 if (focusedActivity != null) {
3811 focusedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
3812 }
3813 } else {
3814 proto.write(FOCUSED_STACK_ID, INVALID_STACK_ID);
3815 }
3816 proto.end(token);
3817 }
3818
Winson43d1f262016-06-14 16:05:55 -07003819 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003820 * Dump all connected displays' configurations.
3821 * @param prefix Prefix to apply to each line of the dump.
3822 */
3823 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3824 pw.print(prefix); pw.println("Display override configurations:");
3825 final int displayCount = mActivityDisplays.size();
3826 for (int i = 0; i < displayCount; i++) {
3827 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3828 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3829 pw.println(activityDisplay.getOverrideConfiguration());
3830 }
3831 }
3832
3833 /**
Winson43d1f262016-06-14 16:05:55 -07003834 * Dumps the activities matching the given {@param name} in the either the focused stack
3835 * or all visible stacks if {@param dumpVisibleStacks} is true.
3836 */
Winson Chung6998bc42017-02-28 17:07:05 -08003837 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3838 boolean dumpFocusedStackOnly) {
3839 if (dumpFocusedStackOnly) {
3840 return mFocusedStack.getDumpActivitiesLocked(name);
3841 } else {
Winson43d1f262016-06-14 16:05:55 -07003842 ArrayList<ActivityRecord> activities = new ArrayList<>();
3843 int numDisplays = mActivityDisplays.size();
3844 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3845 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3846 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3847 ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07003848 if (!dumpVisibleStacksOnly || stack.shouldBeVisible(null)) {
Winson43d1f262016-06-14 16:05:55 -07003849 activities.addAll(stack.getDumpActivitiesLocked(name));
3850 }
3851 }
3852 }
3853 return activities;
Winson43d1f262016-06-14 16:05:55 -07003854 }
Craig Mautner20e72272013-04-01 13:45:53 -07003855 }
3856
Dianne Hackborn390517b2013-05-30 15:03:32 -07003857 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3858 boolean needSep, String prefix) {
3859 if (activity != null) {
3860 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3861 if (needSep) {
3862 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003863 }
3864 pw.print(prefix);
3865 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003866 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003867 }
3868 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003869 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003870 }
3871
Craig Mautner8d341ef2013-03-26 09:03:27 -07003872 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3873 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003874 boolean printed = false;
3875 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003876 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3877 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003878 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003879 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003880 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003881 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003882 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003883 pw.println();
3884 pw.println(" Stack #" + stack.mStackId + ":");
3885 pw.println(" mFullscreen=" + stack.mFullscreen);
3886 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3887 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003888
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003889 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3890 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003891
Craig Mautner4a1cb222013-12-04 16:14:06 -08003892 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3893 !dumpAll, false, dumpPackage, true,
3894 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003895
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 needSep = printed;
3897 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3898 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003899 if (pr) {
3900 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003901 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003902 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003903 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3904 " mResumedActivity: ");
3905 if (pr) {
3906 printed = true;
3907 needSep = false;
3908 }
3909 if (dumpAll) {
3910 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3911 " mLastPausedActivity: ");
3912 if (pr) {
3913 printed = true;
3914 needSep = true;
3915 }
3916 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3917 needSep, " mLastNoHistoryActivity: ");
3918 }
3919 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003920 }
3921 }
3922
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003923 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3924 false, dumpPackage, true, " Activities waiting to finish:", null);
3925 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3926 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003927 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3928 false, !dumpAll, false, dumpPackage, true,
3929 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003930 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3931 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003932
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003933 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003934 }
3935
Dianne Hackborn390517b2013-05-30 15:03:32 -07003936 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003937 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003938 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003939 String innerPrefix = null;
3940 String[] args = null;
3941 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003942 for (int i=list.size()-1; i>=0; i--) {
3943 final ActivityRecord r = list.get(i);
3944 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3945 continue;
3946 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003947 if (innerPrefix == null) {
3948 innerPrefix = prefix + " ";
3949 args = new String[0];
3950 }
3951 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003952 final boolean full = !brief && (complete || !r.isInHistory());
3953 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003954 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003955 needNL = false;
3956 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003957 if (header != null) {
3958 pw.println(header);
3959 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003960 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003961 if (lastTask != r.getTask()) {
3962 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003963 pw.print(prefix);
3964 pw.print(full ? "* " : " ");
3965 pw.println(lastTask);
3966 if (full) {
3967 lastTask.dump(pw, prefix + " ");
3968 } else if (complete) {
3969 // Complete + brief == give a summary. Isn't that obvious?!?
3970 if (lastTask.intent != null) {
3971 pw.print(prefix); pw.print(" ");
3972 pw.println(lastTask.intent.toInsecureStringWithClip());
3973 }
3974 }
3975 }
3976 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3977 pw.print(" #"); pw.print(i); pw.print(": ");
3978 pw.println(r);
3979 if (full) {
3980 r.dump(pw, innerPrefix);
3981 } else if (complete) {
3982 // Complete + brief == give a summary. Isn't that obvious?!?
3983 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3984 if (r.app != null) {
3985 pw.print(innerPrefix); pw.println(r.app);
3986 }
3987 }
3988 if (client && r.app != null && r.app.thread != null) {
3989 // flush anything that is already in the PrintWriter since the thread is going
3990 // to write to the file descriptor directly
3991 pw.flush();
3992 try {
3993 TransferPipe tp = new TransferPipe();
3994 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003995 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003996 // Short timeout, since blocking here can
3997 // deadlock with the application.
3998 tp.go(fd, 2000);
3999 } finally {
4000 tp.kill();
4001 }
4002 } catch (IOException e) {
4003 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4004 } catch (RemoteException e) {
4005 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4006 }
4007 needNL = true;
4008 }
4009 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004010 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004011 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004012
Craig Mautnerf3333272013-04-22 10:55:53 -07004013 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004014 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4015 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004016 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4017 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004018 }
4019
4020 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004021 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004022 }
4023
4024 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004025 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4026 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004027 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4028 }
4029
Craig Mautner05d29032013-05-03 13:40:13 -07004030 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004031 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4032 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4033 }
Craig Mautner05d29032013-05-03 13:40:13 -07004034 }
4035
Craig Mautner0eea92c2013-05-16 13:35:39 -07004036 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004037 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4038 }
4039
4040 final void scheduleSleepTimeout() {
4041 removeSleepTimeouts();
4042 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4043 }
4044
Craig Mautner4a1cb222013-12-04 16:14:06 -08004045 @Override
4046 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004047 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004048 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4049 }
4050
4051 @Override
4052 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004053 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004054 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4055 }
4056
4057 @Override
4058 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004059 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4061 }
4062
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004063 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004064 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004065 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004066 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004067 }
4068
Andrii Kulianca007a62016-11-22 16:33:28 -08004069 /** Check if display with specified id is added to the list. */
4070 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004071 return getActivityDisplayOrCreateLocked(displayId) != null;
4072 }
4073
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004074 // TODO: Look into consolidating with getActivityDisplayOrCreateLocked()
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004075 ActivityDisplay getActivityDisplay(int displayId) {
4076 return mActivityDisplays.get(displayId);
4077 }
4078
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004079 // TODO(multi-display): Look at all callpoints to make sure they make sense in multi-display.
4080 ActivityDisplay getDefaultDisplay() {
4081 return mActivityDisplays.get(DEFAULT_DISPLAY);
4082 }
4083
Andrii Kulian62e6f252017-05-30 22:46:53 -07004084 /**
4085 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4086 * corresponding record in display manager.
4087 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004088 // TODO: Look into consolidating with getActivityDisplay()
4089 ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004090 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4091 if (activityDisplay != null) {
4092 return activityDisplay;
4093 }
4094 if (mDisplayManager == null) {
4095 // The system isn't fully initialized yet.
4096 return null;
4097 }
4098 final Display display = mDisplayManager.getDisplay(displayId);
4099 if (display == null) {
4100 // The display is not registered in DisplayManager.
4101 return null;
4102 }
4103 // The display hasn't been added to ActivityManager yet, create a new record now.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004104 activityDisplay = new ActivityDisplay(this, displayId);
4105 attachDisplay(activityDisplay);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004106 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4107 mWindowManager.onDisplayAdded(displayId);
4108 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004109 }
4110
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004111 @VisibleForTesting
4112 void attachDisplay(ActivityDisplay display) {
4113 mActivityDisplays.put(display.mDisplayId, display);
4114 }
4115
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004116 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004117 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004118 mService.mContext.getResources().getDimensionPixelSize(
4119 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004120 }
4121
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004122 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004123 if (displayId == DEFAULT_DISPLAY) {
4124 throw new IllegalArgumentException("Can't remove the primary display.");
4125 }
4126
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004128 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4129 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004130 final boolean destroyContentOnRemoval
4131 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004132 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4133 while (!stacks.isEmpty()) {
4134 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004135 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004136 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4137 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004138 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004139 } else {
4140 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4141 // a valid launch stack for all activities. This way the task history from
4142 // external display will be preserved on primary after move.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004143 moveTasksToFullscreenStackLocked(stack, true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004144 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004145 }
David Stevens9440dc82017-03-16 19:00:20 -07004146
4147 releaseSleepTokens(activityDisplay);
4148
Craig Mautnere0a38842013-12-16 16:14:02 -08004149 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004150 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 }
4152 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 }
4154
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004155 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004156 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004157 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4158 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004159 // The window policy is responsible for stopping activities on the default display
4160 if (displayId != Display.DEFAULT_DISPLAY) {
4161 int displayState = activityDisplay.mDisplay.getState();
4162 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4163 activityDisplay.mOffToken =
4164 mService.acquireSleepToken("Display-off", displayId);
4165 } else if (displayState == Display.STATE_ON
4166 && activityDisplay.mOffToken != null) {
4167 activityDisplay.mOffToken.release();
4168 activityDisplay.mOffToken = null;
4169 }
4170 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004171 // TODO: Update the bounds.
4172 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004173 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004174 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 }
4176
David Stevens9440dc82017-03-16 19:00:20 -07004177 SleepToken createSleepTokenLocked(String tag, int displayId) {
4178 ActivityDisplay display = mActivityDisplays.get(displayId);
4179 if (display == null) {
4180 throw new IllegalArgumentException("Invalid display: " + displayId);
4181 }
4182
4183 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4184 mSleepTokens.add(token);
4185 display.mAllSleepTokens.add(token);
4186 return token;
4187 }
4188
4189 private void removeSleepTokenLocked(SleepTokenImpl token) {
4190 mSleepTokens.remove(token);
4191
4192 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4193 if (display != null) {
4194 display.mAllSleepTokens.remove(token);
4195 if (display.mAllSleepTokens.isEmpty()) {
4196 mService.updateSleepIfNeededLocked();
4197 }
4198 }
4199 }
4200
4201 private void releaseSleepTokens(ActivityDisplay display) {
4202 if (display.mAllSleepTokens.isEmpty()) {
4203 return;
4204 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004205 for (SleepToken token : display.mAllSleepTokens) {
David Stevens9440dc82017-03-16 19:00:20 -07004206 mSleepTokens.remove(token);
4207 }
4208 display.mAllSleepTokens.clear();
4209
4210 mService.updateSleepIfNeededLocked();
4211 }
4212
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004213 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004214 final int displayId = stack.mDisplayId;
4215 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004217 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004218 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004219 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004220 info.userId = stack.mCurrentUser;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004221 info.visible = stack.shouldBeVisible(null);
Andrii Kulian62e6f252017-05-30 22:46:53 -07004222 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004223 info.position = display != null
4224 ? display.mStacks.indexOf(stack)
4225 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226
4227 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4228 final int numTasks = tasks.size();
4229 int[] taskIds = new int[numTasks];
4230 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004231 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004232 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004233 for (int i = 0; i < numTasks; ++i) {
4234 final TaskRecord task = tasks.get(i);
4235 taskIds[i] = task.taskId;
4236 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4237 : task.realActivity != null ? task.realActivity.flattenToString()
4238 : task.getTopActivity() != null ? task.getTopActivity().packageName
4239 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004240 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004241 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004242 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004243 }
4244 info.taskIds = taskIds;
4245 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004246 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004247 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004248
4249 final ActivityRecord top = stack.topRunningActivityLocked();
4250 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004251 return info;
4252 }
4253
4254 StackInfo getStackInfoLocked(int stackId) {
4255 ActivityStack stack = getStack(stackId);
4256 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004257 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004258 }
4259 return null;
4260 }
4261
4262 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004263 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004264 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4265 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004266 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004267 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004268 }
4269 }
4270 return list;
4271 }
4272
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004273 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004274 int preferredDisplayId, int actualStackId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004275 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
4276 actualStackId, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004277 }
4278
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004279 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004280 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4281 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004282 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004283 final ActivityStack actualStack = getStack(actualStackId);
4284 final boolean inSplitScreenMode = actualStack != null
4285 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004286 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004287 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004288 return;
4289 }
4290
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004291 // Handle incorrect launch/move to secondary display if needed.
4292 final boolean launchOnSecondaryDisplayFailed;
4293 if (isSecondaryDisplayPreferred) {
4294 final int actualDisplayId = task.getStack().mDisplayId;
4295 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4296 // The task landed on an inappropriate display somehow, move it to the default
4297 // display.
4298 // TODO(multi-display): Find proper stack for the task on the default display.
4299 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4300 true /* toTop */);
4301 launchOnSecondaryDisplayFailed = true;
4302 } else {
4303 // The task might have landed on a display different from requested.
4304 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4305 || (preferredDisplayId != INVALID_DISPLAY
4306 && preferredDisplayId != actualDisplayId);
4307 }
4308 } else {
4309 // The task wasn't requested to be on a secondary display.
4310 launchOnSecondaryDisplayFailed = false;
4311 }
4312
Jorim Jaggicd13d332016-04-27 15:40:20 -07004313 final ActivityRecord topActivity = task.getTopActivity();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004314 if (launchOnSecondaryDisplayFailed
4315 || !task.supportsSplitScreenWindowingMode() || forceNonResizable) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004316 if (launchOnSecondaryDisplayFailed) {
4317 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4318 // display with config different from global config.
4319 mService.mTaskChangeNotificationController
4320 .notifyActivityLaunchOnSecondaryDisplayFailed();
4321 } else {
4322 // Display a warning toast that we tried to put a non-dockable task in the docked
4323 // stack.
4324 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4325 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004326
Andrii Kulianc27916642016-04-12 17:59:27 -07004327 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4328 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004329
4330 final ActivityStack dockedStack = task.getStack().getDisplay().getSplitScreenStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004331 if (dockedStack != null) {
4332 moveTasksToFullscreenStackLocked(dockedStack,
4333 actualStackId == dockedStack.getStackId());
4334 }
Winson Chungd3395382016-12-13 11:49:09 -08004335 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004336 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004337 final String packageName = topActivity.appInfo.packageName;
4338 final int reason = isSecondaryDisplayPreferred
4339 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4340 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004341 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004342 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004343 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004344 }
4345
Jorim Jaggife89d122015-12-22 16:28:44 +01004346 void activityRelaunchedLocked(IBinder token) {
4347 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004348 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4349 if (r != null) {
4350 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004351 r.setSleeping(true, true);
4352 }
4353 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004354 }
4355
4356 void activityRelaunchingLocked(ActivityRecord r) {
4357 mWindowManager.notifyAppRelaunching(r.appToken);
4358 }
4359
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004360 void logStackState() {
4361 mActivityMetricsLogger.logWindowState();
4362 }
4363
Winson Chung5af42fc2017-03-24 17:11:33 -07004364 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004365 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4366 // end, then ensure that we defer all in between multi-window mode changes
4367 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4368 return;
4369 }
4370
Wale Ogunwale22e25262016-02-01 10:32:02 -08004371 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4372 final ActivityRecord r = task.mActivities.get(i);
4373 if (r.app != null && r.app.thread != null) {
4374 mMultiWindowModeChangedActivities.add(r);
4375 }
4376 }
4377
4378 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4379 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4380 }
4381 }
4382
Winson Chung5af42fc2017-03-24 17:11:33 -07004383 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004384 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004385 if (prevStack == null || prevStack == stack
4386 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4387 return;
4388 }
4389
Winson Chungab76bbc2017-08-14 13:33:51 -07004390 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004391 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004392
Winson Chungab76bbc2017-08-14 13:33:51 -07004393 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4394 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4395 final ActivityRecord r = task.mActivities.get(i);
4396 if (r.app != null && r.app.thread != null) {
4397 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004398 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004399 }
4400 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004401
Winson Chungab76bbc2017-08-14 13:33:51 -07004402 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4403 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4404 }
4405 }
4406
4407 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4408 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4409 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4410 final ActivityRecord r = task.mActivities.get(i);
4411 if (r.app != null && r.app.thread != null) {
4412 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004413 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004414 }
4415 }
4416
Tony Mak853304c2016-04-18 15:17:41 +01004417 void setDockedStackMinimized(boolean minimized) {
4418 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004419 }
4420
chaviw59b98852017-06-13 12:05:44 -07004421 void wakeUp(String reason) {
4422 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4423 }
4424
4425 /**
4426 * Begin deferring resume to avoid duplicate resumes in one pass.
4427 */
4428 private void beginDeferResume() {
4429 mDeferResumeCount++;
4430 }
4431
4432 /**
4433 * End deferring resume and determine if resume can be called.
4434 */
4435 private void endDeferResume() {
4436 mDeferResumeCount--;
4437 }
4438
4439 /**
4440 * @return True if resume can be called.
4441 */
4442 private boolean readyToResume() {
4443 return mDeferResumeCount == 0;
4444 }
4445
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004446 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004447
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004448 public ActivityStackSupervisorHandler(Looper looper) {
4449 super(looper);
4450 }
4451
Winson Chung4dabf232017-01-25 13:25:22 -08004452 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004453 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004454 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4455 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004456 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004457 }
4458
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004459 @Override
4460 public void handleMessage(Message msg) {
4461 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004462 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4463 synchronized (mService) {
4464 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4465 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004466 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004467 }
4468 }
4469 } break;
4470 case REPORT_PIP_MODE_CHANGED_MSG: {
4471 synchronized (mService) {
4472 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4473 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004474 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4475 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004476 }
4477 }
4478 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004479 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004480 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4481 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004482 // We don't at this point know if the activity is fullscreen,
4483 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004484 activityIdleInternal((ActivityRecord) msg.obj,
4485 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004486 } break;
4487 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004488 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004489 activityIdleInternal((ActivityRecord) msg.obj,
4490 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004491 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004492 case RESUME_TOP_ACTIVITY_MSG: {
4493 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004494 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004495 }
4496 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004497 case SLEEP_TIMEOUT_MSG: {
4498 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004499 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004500 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004501 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004502 }
4503 }
4504 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004505 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004506 synchronized (mService) {
4507 if (mLaunchingActivity.isHeld()) {
4508 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4509 if (VALIDATE_WAKE_LOCK_CALLER
4510 && Binder.getCallingUid() != Process.myUid()) {
4511 throw new IllegalStateException("Calling must be system uid");
4512 }
4513 mLaunchingActivity.release();
4514 }
4515 }
4516 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004518 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004519 } break;
4520 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004521 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004522 } break;
4523 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004524 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004525 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004526 case LAUNCH_TASK_BEHIND_COMPLETE: {
4527 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004528 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004529 if (r != null) {
4530 handleLaunchTaskBehindCompleteLocked(r);
4531 }
4532 }
4533 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004534
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004535 }
4536 }
4537 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004538
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004539 ActivityStack findStackBehind(ActivityStack stack) {
4540 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004541 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004542 if (display == null) {
4543 return null;
4544 }
4545 final ArrayList<ActivityStack> stacks = display.mStacks;
4546 for (int i = stacks.size() - 1; i >= 0; i--) {
4547 if (stacks.get(i) == stack && i > 0) {
4548 return stacks.get(i - 1);
4549 }
4550 }
4551 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4552 + " in=" + stacks);
4553 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004554
Jorim Jaggic69bd222016-03-15 14:38:37 +01004555 /**
4556 * Puts a task into resizing mode during the next app transition.
4557 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004558 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004559 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004560 private void setResizingDuringAnimation(TaskRecord task) {
4561 mResizingTasksDuringAnimation.add(task.taskId);
4562 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004563 }
4564
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004565 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4566 final TaskRecord task;
4567 final int callingUid;
4568 final String callingPackage;
4569 final Intent intent;
4570 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004571 int activityType = ACTIVITY_TYPE_UNDEFINED;
4572 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004573 final ActivityOptions activityOptions = (bOptions != null)
4574 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004575 if (activityOptions != null) {
4576 activityType = activityOptions.getLaunchActivityType();
4577 windowingMode = activityOptions.getLaunchWindowingMode();
4578 }
4579 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004580 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004581 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004582 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004583
Matthew Ng606dd802017-06-05 14:06:32 -07004584 mWindowManager.deferSurfaceLayout();
4585 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004586 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004587 mWindowManager.setDockedStackCreateState(
4588 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004589
Matthew Ng606dd802017-06-05 14:06:32 -07004590 // Defer updating the stack in which recents is until the app transition is done, to
4591 // not run into issues where we still need to draw the task in recents but the
4592 // docked stack is already created.
4593 deferUpdateBounds(RECENTS_STACK_ID);
4594 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004595 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004596
Matthew Ng606dd802017-06-05 14:06:32 -07004597 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004598 activityOptions);
Matthew Ng606dd802017-06-05 14:06:32 -07004599 if (task == null) {
4600 continueUpdateBounds(RECENTS_STACK_ID);
4601 mWindowManager.executeAppTransition();
4602 throw new IllegalArgumentException(
4603 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4604 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004605
Matthew Ng606dd802017-06-05 14:06:32 -07004606 // Since we don't have an actual source record here, we assume that the currently
4607 // focused activity was the source.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004608 final ActivityStack stack = getLaunchStack(null, activityOptions, task, ON_TOP);
Matthew Ng606dd802017-06-05 14:06:32 -07004609
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004610 if (stack != null && task.getStack() != stack) {
4611 task.reparent(stack, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004612 "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004613 }
4614
4615 // If the user must confirm credentials (e.g. when first launching a work app and the
4616 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4617 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4618 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004619 final ActivityRecord targetActivity = task.getTopActivity();
4620
4621 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4622 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004623 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004624 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004625 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004626 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004627
4628 // If we are launching the task in the docked stack, put it into resizing mode so
4629 // the window renders full-screen with the background filling the void. Also only
4630 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004631 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004632 setResizingDuringAnimation(task);
4633 }
4634
4635 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07004636 ActivityManager.START_TASK_TO_FRONT, task.getStack());
Matthew Ng606dd802017-06-05 14:06:32 -07004637 return ActivityManager.START_TASK_TO_FRONT;
4638 }
4639 callingUid = task.mCallingUid;
4640 callingPackage = task.mCallingPackage;
4641 intent = task.intent;
4642 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4643 userId = task.userId;
4644 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004645 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004646 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004647 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004648 }
Matthew Ng606dd802017-06-05 14:06:32 -07004649 return result;
4650 } finally {
4651 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004652 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004653 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004654
4655 /**
4656 * @return a list of activities which are the top ones in each visible stack. The first
4657 * entry will be the focused activity.
4658 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004659 List<IBinder> getTopVisibleActivities() {
4660 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4661 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004662 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004663 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4664 // Traverse all stacks on a display.
4665 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4666 final ActivityStack stack = display.mStacks.get(j);
4667 // Get top activity from a visible stack and add it to the list.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07004668 if (stack.shouldBeVisible(null /* starting */)) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004669 final ActivityRecord top = stack.topActivity();
4670 if (top != null) {
4671 if (stack == mFocusedStack) {
4672 topActivityTokens.add(0, top.appToken);
4673 } else {
4674 topActivityTokens.add(top.appToken);
4675 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004676 }
4677 }
4678 }
4679 }
4680 return topActivityTokens;
4681 }
Bryce Lee4a194382017-04-04 14:32:48 -07004682
4683 /**
4684 * Internal container to store a match qualifier alongside a WaitResult.
4685 */
4686 static class WaitInfo {
4687 private final ComponentName mTargetComponent;
4688 private final WaitResult mResult;
4689
4690 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4691 this.mTargetComponent = targetComponent;
4692 this.mResult = result;
4693 }
4694
Wale Ogunwale3270f172017-04-26 07:29:42 -07004695 public boolean matches(ComponentName targetComponent) {
4696 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004697 }
4698
4699 public WaitResult getResult() {
4700 return mResult;
4701 }
4702
4703 public ComponentName getComponent() {
4704 return mTargetComponent;
4705 }
4706
4707 public void dump(PrintWriter pw, String prefix) {
4708 pw.println(prefix + "WaitInfo:");
4709 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4710 pw.println(prefix + " mResult=");
4711 mResult.dump(pw, prefix);
4712 }
4713 }
David Stevens9440dc82017-03-16 19:00:20 -07004714
4715 private final class SleepTokenImpl extends SleepToken {
4716 private final String mTag;
4717 private final long mAcquireTime;
4718 private final int mDisplayId;
4719
4720 public SleepTokenImpl(String tag, int displayId) {
4721 mTag = tag;
4722 mDisplayId = displayId;
4723 mAcquireTime = SystemClock.uptimeMillis();
4724 }
4725
4726 @Override
4727 public void release() {
4728 synchronized (mService) {
4729 removeSleepTokenLocked(this);
4730 }
4731 }
4732
4733 @Override
4734 public String toString() {
4735 return "{\"" + mTag + "\", display " + mDisplayId
4736 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4737 }
4738 }
4739
Craig Mautner27084302013-03-25 08:05:25 -07004740}