blob: 45b928ca507f3654a686f63b2f5c3c3e716a9c85 [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;
27import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
28import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
29import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
30import static android.app.ActivityManager.StackId.HOME_STACK_ID;
31import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
32import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
33import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ng330757d2017-02-28 14:19:17 -080034import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070035import static android.app.ActivityManager.StackId.getStackIdForActivityType;
36import static android.app.ActivityManager.StackId.getStackIdForWindowingMode;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070037import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY;
38import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070039import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070040import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070041import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
42import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070043import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale926aade2017-08-29 11:24:37 -070044import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070045import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
46import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070047import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070048import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Andrii Kulian3c9ad072017-08-01 11:45:22 -070049import static android.content.pm.PackageManager.PERMISSION_DENIED;
Jorim Jaggife762342016-10-13 14:33:27 +020050import static android.content.pm.PackageManager.PERMISSION_GRANTED;
chaviw59b98852017-06-13 12:05:44 -070051import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
Benjamin Franza83859f2017-07-03 16:34:14 +010052import static android.os.Process.SYSTEM_UID;
Jorim Jaggife762342016-10-13 14:33:27 +020053import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
54import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian16802aa2016-11-02 12:21:33 -070055import static android.view.Display.FLAG_PRIVATE;
56import static android.view.Display.INVALID_DISPLAY;
Andrii Kulian250d6532017-02-08 23:30:45 -080057import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
Andrii Kulian1cba31c2017-06-28 09:42:48 -070058import static android.view.Display.TYPE_VIRTUAL;
Winson Chung47900652017-04-06 18:44:25 -070059import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN;
Jorim Jaggife762342016-10-13 14:33:27 +020060import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
Jorim Jaggife762342016-10-13 14:33:27 +020061import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020063import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
64import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
65import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
66import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
67import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
68import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
69import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020070import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_FOCUS;
71import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_IDLE;
Jorim Jaggife762342016-10-13 14:33:27 +020072import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
73import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
74import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
75import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
76import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
77import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
78import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
Jorim Jaggife762342016-10-13 14:33:27 +020079import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
80import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
81import static com.android.server.am.ActivityManagerService.ANIMATE;
82import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Jorim Jaggife762342016-10-13 14:33:27 +020083import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
84import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
85import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
86import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
87import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
88import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
89import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
90import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
91import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
92import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
93import static com.android.server.am.ActivityStack.STACK_VISIBLE;
Wale Ogunwale0568aed2017-09-08 13:29:37 -070094import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
96import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Winson Chung74666102017-02-22 17:49:24 -080097import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
98import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
99import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
Jorim Jaggife762342016-10-13 14:33:27 +0200100import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800101import static java.lang.Integer.MAX_VALUE;
Jorim Jaggife762342016-10-13 14:33:27 +0200102
Svetoslav7008b512015-06-24 18:47:07 -0700103import android.Manifest;
Winson Chung7900bee2017-03-10 08:59:25 -0800104import android.annotation.IntDef;
Andrii Kulian16802aa2016-11-02 12:21:33 -0700105import android.annotation.NonNull;
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700106import android.annotation.Nullable;
Tony Mak853304c2016-04-18 15:17:41 +0100107import android.annotation.UserIdInt;
Craig Mautner2420ead2013-04-01 17:13:20 -0700108import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import android.app.ActivityManager;
Filip Gruszczynski80e29f12015-12-14 14:58:30 -0800110import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700111import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -0800112import android.app.ActivityManager.StackInfo;
David Stevens9440dc82017-03-16 19:00:20 -0700113import android.app.ActivityManagerInternal.SleepToken;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700114import android.app.ActivityOptions;
Svetoslav7008b512015-06-24 18:47:07 -0700115import android.app.AppOpsManager;
Jeff Hao1b012d32014-08-20 10:35:34 -0700116import android.app.ProfilerInfo;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import android.app.ResultInfo;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700118import android.app.WaitResult;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700120import android.content.Context;
121import android.content.Intent;
122import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -0700124import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import android.content.pm.PackageManager;
126import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100127import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700128import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800129import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130import android.hardware.display.DisplayManager;
131import android.hardware.display.DisplayManager.DisplayListener;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800132import android.hardware.display.DisplayManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800133import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134import android.os.Binder;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100135import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700137import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700138import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -0700140import android.os.Message;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700141import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700142import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700143import android.os.RemoteException;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700144import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700145import android.os.Trace;
Craig Mautner6170f732013-04-02 13:05:23 -0700146import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100147import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700148import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700149import android.provider.MediaStore;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700150import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700151import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700152import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700153import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800154import android.util.IntArray;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700155import android.util.MergedConfiguration;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700156import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800157import android.util.SparseArray;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800158import android.util.SparseIntArray;
David Stevens9440dc82017-03-16 19:00:20 -0700159import android.util.TimeUtils;
Craig Mautnered6649f2013-12-02 14:08:25 -0800160import android.view.Display;
Chong Zhangb15758a2015-11-17 12:12:03 -0800161
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700162import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800163import com.android.internal.content.ReferrerIntent;
Winson Chung14fbe142016-12-19 16:18:24 -0800164import com.android.internal.logging.MetricsLogger;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700165import com.android.internal.os.TransferPipe;
Svetoslav7008b512015-06-24 18:47:07 -0700166import com.android.internal.util.ArrayUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800167import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700168import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700169import com.android.server.wm.ConfigurationContainer;
Winson Chung19953ca2017-04-11 11:19:23 -0700170import com.android.server.wm.PinnedStackWindowController;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700171import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700172
Craig Mautner8d341ef2013-03-26 09:03:27 -0700173import java.io.FileDescriptor;
174import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700175import java.io.PrintWriter;
Winson Chung7900bee2017-03-10 08:59:25 -0800176import java.lang.annotation.Retention;
177import java.lang.annotation.RetentionPolicy;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700178import java.util.ArrayList;
David Stevens9440dc82017-03-16 19:00:20 -0700179import java.util.Iterator;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700180import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700181import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700182
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800183public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800184 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Chong Zhang6cda19c2016-06-14 19:07:56 -0700185 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700188 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700191 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700192 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800193 static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800194
Craig Mautnerf3333272013-04-22 10:55:53 -0700195 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700196 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700197
Craig Mautner0eea92c2013-05-16 13:35:39 -0700198 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700199 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700200
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700201 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700202 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700203
Craig Mautner05d29032013-05-03 13:40:13 -0700204 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
205 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
206 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700208 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800209 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
210 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
211 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700212 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Wale Ogunwale22e25262016-02-01 10:32:02 -0800213 static final int REPORT_MULTI_WINDOW_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 14;
214 static final int REPORT_PIP_MODE_CHANGED_MSG = FIRST_SUPERVISOR_STACK_MSG + 15;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800215
Wale Ogunwale040b4702015-08-06 18:10:50 -0700216 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700217
Wale Ogunwale040b4702015-08-06 18:10:50 -0700218 // Used to indicate if an object (e.g. stack) that we are trying to get
219 // should be created if it doesn't exist already.
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800220 static final boolean CREATE_IF_NEEDED = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700221
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700222 // Used to indicate that windows of activities should be preserved during the resize.
223 static final boolean PRESERVE_WINDOWS = true;
224
Wale Ogunwale040b4702015-08-06 18:10:50 -0700225 // Used to indicate if an object (e.g. task) should be moved/created
226 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700227 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700228
229 // Used to indicate that an objects (e.g. task) removal from its container
230 // (e.g. stack) is due to it moving to another container.
231 static final boolean MOVING = true;
232
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700233 // Force the focus to change to the stack we are moving a task to..
234 static final boolean FORCE_FOCUS = true;
235
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700236 // Don't execute any calls to resume.
237 static final boolean DEFER_RESUME = true;
238
Winson Chung010927a2016-12-15 16:12:35 -0800239 // Used to indicate that a task is removed it should also be removed from recents.
240 static final boolean REMOVE_FROM_RECENTS = true;
241
Winson Chung6954fc92017-03-24 16:22:12 -0700242 // Used to indicate that pausing an activity should occur immediately without waiting for
243 // the activity callback indicating that it has completed pausing
244 static final boolean PAUSE_IMMEDIATELY = true;
245
Winson Chung7900bee2017-03-10 08:59:25 -0800246 /**
247 * The modes which affect which tasks are returned when calling
248 * {@link ActivityStackSupervisor#anyTaskForIdLocked(int)}.
249 */
250 @Retention(RetentionPolicy.SOURCE)
251 @IntDef({
252 MATCH_TASK_IN_STACKS_ONLY,
253 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
254 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
255 })
256 public @interface AnyTaskForIdMatchTaskMode {}
257 // Match only tasks in the current stacks
258 static final int MATCH_TASK_IN_STACKS_ONLY = 0;
259 // Match either tasks in the current stacks, or in the recent tasks if not found in the stacks
260 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS = 1;
261 // Match either tasks in the current stacks, or in the recent tasks, restoring it to the
262 // provided stack id
263 static final int MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE = 2;
264
Svetoslav7008b512015-06-24 18:47:07 -0700265 // Activity actions an app cannot start if it uses a permission which is not granted.
266 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
267 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700268
Svetoslav7008b512015-06-24 18:47:07 -0700269 static {
270 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
271 Manifest.permission.CAMERA);
272 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
273 Manifest.permission.CAMERA);
274 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
275 Manifest.permission.CALL_PHONE);
276 }
277
Svet Ganov99b60432015-06-27 13:15:22 -0700278 /** Action restriction: launching the activity is not restricted. */
279 private static final int ACTIVITY_RESTRICTION_NONE = 0;
280 /** Action restriction: launching the activity is restricted by a permission. */
281 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
282 /** Action restriction: launching the activity is restricted by an app op. */
283 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700284
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700285 // For debugging to make sure the caller when acquiring/releasing our
286 // wake lock is the system process.
287 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800288 /** The number of distinct task ids that can be assigned to the tasks of a single user */
289 private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;
Craig Mautnerf3333272013-04-22 10:55:53 -0700290
Craig Mautner27084302013-03-25 08:05:25 -0700291 final ActivityManagerService mService;
292
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800293 private RecentTasks mRecentTasks;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800294
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700295 final ActivityStackSupervisorHandler mHandler;
296
297 /** Short cut */
298 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800299 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700300
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700301 /** Counter for next free stack ID to use for dynamic activity stacks. */
302 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700303
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800304 /**
305 * Maps the task identifier that activities are currently being started in to the userId of the
306 * task. Each time a new task is created, the entry for the userId of the task is incremented
307 */
308 private final SparseIntArray mCurTaskIdForUser = new SparseIntArray(20);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700309
Craig Mautner2420ead2013-04-01 17:13:20 -0700310 /** The current user */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800311 int mCurrentUser;
Craig Mautner2420ead2013-04-01 17:13:20 -0700312
Craig Mautnere0a38842013-12-16 16:14:02 -0800313 /** The stack containing the launcher app. Assumed to always be attached to
314 * Display.DEFAULT_DISPLAY. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800315 ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700316
Craig Mautnere0a38842013-12-16 16:14:02 -0800317 /** The stack currently receiving input or launching the next activity. */
Filip Gruszczynski07a0e492015-12-17 14:16:38 -0800318 ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700319
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
321 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800322 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324
325 /** List of activities that are waiting for a new activity to become visible before completing
326 * whatever operation they are supposed to do. */
Bryce Lee4a194382017-04-04 14:32:48 -0700327 // TODO: Remove mActivitiesWaitingForVisibleActivity list and just remove activity from
328 // mStoppingActivities when something else comes up.
329 final ArrayList<ActivityRecord> mActivitiesWaitingForVisibleActivity = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700330
Bryce Lee4a194382017-04-04 14:32:48 -0700331 /** List of processes waiting to find out when a specific activity becomes visible. */
332 private final ArrayList<WaitInfo> mWaitingForActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333
334 /** List of processes waiting to find out about the next launched activity. */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700335 final ArrayList<WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700336
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337 /** List of activities that are ready to be stopped, but waiting for the next activity to
338 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800339 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700340
Craig Mautnerf3333272013-04-22 10:55:53 -0700341 /** List of activities that are ready to be finished, but waiting for the previous activity to
342 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800343 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700344
Craig Mautner0eea92c2013-05-16 13:35:39 -0700345 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800346 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700347
Wale Ogunwale22e25262016-02-01 10:32:02 -0800348 /** List of activities whose multi-window mode changed that we need to report to the
349 * application */
350 final ArrayList<ActivityRecord> mMultiWindowModeChangedActivities = new ArrayList<>();
351
352 /** List of activities whose picture-in-picture mode changed that we need to report to the
353 * application */
354 final ArrayList<ActivityRecord> mPipModeChangedActivities = new ArrayList<>();
355
Winson Chung5af42fc2017-03-24 17:11:33 -0700356 /** The target stack bounds for the picture-in-picture mode changed that we need to report to
357 * the application */
358 Rect mPipModeChangedTargetStackBounds;
359
Craig Mautnerf3333272013-04-22 10:55:53 -0700360 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700361 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700362
Craig Mautnerde4ef022013-04-07 19:01:33 -0700363 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
364 * is being brought in front of us. */
365 boolean mUserLeaving = false;
366
Craig Mautner0eea92c2013-05-16 13:35:39 -0700367 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700368 * We don't want to allow the device to go to sleep while in the process
369 * of launching an activity. This is primarily to allow alarm intent
370 * receivers to launch an activity and get that to run before the device
371 * goes back to sleep.
372 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700373 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700374
375 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700376 * Set when the system is going to sleep, until we have
377 * successfully paused the current activity and released our wake lock.
378 * At that point the system is allowed to actually sleep.
379 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700380 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700381
David Stevens9440dc82017-03-16 19:00:20 -0700382 /**
383 * A list of tokens that cause the top activity to be put to sleep.
384 * They are used by components that may hide and block interaction with underlying
385 * activities.
386 */
387 final ArrayList<SleepToken> mSleepTokens = new ArrayList<SleepToken>();
388
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700389 /** Stack id of the front stack when user switched, indexed by userId. */
390 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700391
Craig Mautner4504de52013-12-20 09:06:56 -0800392 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800393 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700394 SparseArray<ActivityStack> mStacks = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800395
Bryce Leeaf3a4002017-03-20 10:37:12 -0700396 // TODO: There should be an ActivityDisplayController coordinating am/wm interaction.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700398 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800399
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800400 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
401
402 private DisplayManagerInternal mDisplayManagerInternal;
403 private InputManagerInternal mInputManagerInternal;
Jeff Brownca9bc702014-02-11 14:32:56 -0800404
Wale Ogunwaled046a012015-12-24 13:05:59 -0800405 /** Used to keep resumeTopActivityUncheckedLocked() from being entered recursively */
Craig Mautner42d04db2014-11-06 12:13:23 -0800406 boolean inResumeTopActivity;
407
Andrii Kulian1e32e022016-09-16 15:29:34 -0700408 /**
409 * Temporary rect used during docked stack resize calculation so we don't need to create a new
410 * object each time.
411 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700412 private final Rect tempRect = new Rect();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700413
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700414 // The default minimal size that will be used if the activity doesn't specify its minimal size.
415 // It will be calculated when the default display gets added.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700416 int mDefaultMinSizeOfResizeableTask = -1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700417
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700418 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
419 private boolean mTaskLayersChanged = true;
420
Jorim Jaggi275561a2016-02-23 10:11:02 -0500421 final ActivityMetricsLogger mActivityMetricsLogger;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800422
Jorim Jaggiea039a82017-08-02 14:37:49 +0200423 private final ArrayList<ActivityRecord> mTmpActivityList = new ArrayList<>();
424
Andrii Kulian1779e612016-10-12 21:58:25 -0700425 @Override
426 protected int getChildCount() {
427 return mActivityDisplays.size();
428 }
429
430 @Override
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700431 protected ActivityDisplay getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700432 return mActivityDisplays.valueAt(index);
433 }
434
435 @Override
436 protected ConfigurationContainer getParent() {
437 return null;
438 }
439
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700440 Configuration getDisplayOverrideConfiguration(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700441 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700442 if (activityDisplay == null) {
443 throw new IllegalArgumentException("No display found with id: " + displayId);
444 }
445
446 return activityDisplay.getOverrideConfiguration();
447 }
448
449 void setDisplayOverrideConfiguration(Configuration overrideConfiguration, int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -0700450 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700451 if (activityDisplay == null) {
452 throw new IllegalArgumentException("No display found with id: " + displayId);
453 }
454
455 activityDisplay.onOverrideConfigurationChanged(overrideConfiguration);
456 }
457
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700458 /** Check if placing task or activity on specified display is allowed. */
Andrii Kulian02689a72017-07-06 14:28:59 -0700459 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
460 int callingUid, ActivityInfo activityInfo) {
461 if (displayId == DEFAULT_DISPLAY) {
462 // No restrictions for the default display.
463 return true;
464 }
465 if (!mService.mSupportsMultiDisplay) {
466 // Can't launch on secondary displays if feature is not supported.
467 return false;
468 }
469 if (!resizeable && !displayConfigMatchesGlobal(displayId)) {
470 // Can't apply wrong configuration to non-resizeable activities.
471 return false;
472 }
473 if (!isCallerAllowedToLaunchOnDisplay(callingPid, callingUid, displayId, activityInfo)) {
474 // Can't place activities to a display that has restricted launch rules.
475 // In this case the request should be made by explicitly adding target display id and
476 // by caller with corresponding permissions. See #isCallerAllowedToLaunchOnDisplay().
477 return false;
478 }
479 return true;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700480 }
481
482 /**
483 * Check if configuration of specified display matches current global config.
484 * Used to check if we can put a non-resizeable activity on a secondary display and it will get
485 * the same config as on the default display.
486 * @param displayId Id of the display to check.
487 * @return {@code true} if configuration matches.
488 */
489 private boolean displayConfigMatchesGlobal(int displayId) {
490 if (displayId == DEFAULT_DISPLAY) {
491 return true;
492 }
493 if (displayId == INVALID_DISPLAY) {
494 return false;
495 }
Andrii Kulian62e6f252017-05-30 22:46:53 -0700496 final ActivityDisplay targetDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700497 if (targetDisplay == null) {
498 throw new IllegalArgumentException("No display found with id: " + displayId);
499 }
500 return getConfiguration().equals(targetDisplay.getConfiguration());
501 }
502
Wale Ogunwale39381972015-12-17 17:15:29 -0800503 static class FindTaskResult {
504 ActivityRecord r;
505 boolean matchedByRootAffinity;
506 }
507 private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
508
Craig Mautneree36c772014-07-16 14:56:05 -0700509 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800510 * Temp storage for display ids sorted in focus order.
511 * Maps position to id. Using {@link SparseIntArray} instead of {@link ArrayList} because
512 * it's more efficient, as the number of displays is usually small.
513 */
514 private SparseIntArray mTmpOrderedDisplayIds = new SparseIntArray();
515
516 /**
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100517 * Used to keep track whether app visibilities got changed since the last pause. Useful to
518 * determine whether to invoke the task stack change listener after pausing.
519 */
520 boolean mAppVisibilitiesChangedSinceLastPause;
521
522 /**
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100523 * Set of tasks that are in resizing mode during an app transition to fill the "void".
524 */
525 private final ArraySet<Integer> mResizingTasksDuringAnimation = new ArraySet<>();
526
Wale Ogunwalec8da41e2016-04-16 13:27:23 -0700527
528 /**
529 * If set to {@code false} all calls to resize the docked stack {@link #resizeDockedStackLocked}
530 * will be ignored. Useful for the case where the caller is handling resizing of other stack and
531 * moving tasks around and doesn't want dock stack to be resized due to an automatic trigger
532 * like the docked stack going empty.
533 */
534 private boolean mAllowDockedStackResize = true;
535
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100536 /**
Tony Mak853304c2016-04-18 15:17:41 +0100537 * Is dock currently minimized.
538 */
539 boolean mIsDockMinimized;
540
Jorim Jaggife762342016-10-13 14:33:27 +0200541 final KeyguardController mKeyguardController;
542
chaviw59b98852017-06-13 12:05:44 -0700543 private PowerManager mPowerManager;
544 private int mDeferResumeCount;
545
Tony Mak853304c2016-04-18 15:17:41 +0100546 /**
Craig Mautneree36c772014-07-16 14:56:05 -0700547 * Description of a request to start a new activity, which has been held
548 * due to app switches being disabled.
549 */
550 static class PendingActivityLaunch {
551 final ActivityRecord r;
552 final ActivityRecord sourceRecord;
553 final int startFlags;
554 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700555 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700556
557 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700558 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700559 r = _r;
560 sourceRecord = _sourceRecord;
561 startFlags = _startFlags;
562 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700563 callerApp = _callerApp;
564 }
565
566 void sendErrorResult(String message) {
567 try {
568 if (callerApp.thread != null) {
569 callerApp.thread.scheduleCrash(message);
570 }
571 } catch (RemoteException e) {
572 Slog.e(TAG, "Exception scheduling crash of failed "
573 + "activity launcher sourceRecord=" + sourceRecord, e);
574 }
Craig Mautneree36c772014-07-16 14:56:05 -0700575 }
576 }
577
Bryce Leeaf691c02017-03-20 14:20:22 -0700578 public ActivityStackSupervisor(ActivityManagerService service, Looper looper) {
Craig Mautner27084302013-03-25 08:05:25 -0700579 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700580 mHandler = new ActivityStackSupervisorHandler(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800581 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jorim Jaggife762342016-10-13 14:33:27 +0200582 mKeyguardController = new KeyguardController(service, this);
Jeff Brown2c43c332014-06-12 22:38:59 -0700583 }
584
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800585 void setRecentTasks(RecentTasks recentTasks) {
586 mRecentTasks = recentTasks;
587 }
588
Jeff Brown2c43c332014-06-12 22:38:59 -0700589 /**
590 * At the time when the constructor runs, the power manager has not yet been
591 * initialized. So we initialize our wakelocks afterwards.
592 */
593 void initPowerManagement() {
chaviw59b98852017-06-13 12:05:44 -0700594 mPowerManager = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
595 mGoingToSleep = mPowerManager
596 .newWakeLock(PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
597 mLaunchingActivity = mPowerManager.newWakeLock(PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700598 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700599 }
600
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700601 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 synchronized (mService) {
603 mWindowManager = wm;
Jorim Jaggife762342016-10-13 14:33:27 +0200604 mKeyguardController.setWindowManager(wm);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800605
606 mDisplayManager =
607 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
608 mDisplayManager.registerDisplayListener(this, null);
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800609 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610
611 Display[] displays = mDisplayManager.getDisplays();
612 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
613 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800614 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700615 if (activityDisplay.mDisplay == null) {
616 throw new IllegalStateException("Default Display does not exist");
617 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800618 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700619 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 }
621
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800622 mHomeStack = mFocusedStack = mLastFocusedStack =
623 getStack(HOME_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Jeff Brownca9bc702014-02-11 14:32:56 -0800624
625 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 }
Craig Mautner27084302013-03-25 08:05:25 -0700627 }
628
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700629 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800630 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700631 }
632
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700635 }
636
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700637 boolean isFocusedStack(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700638 return stack != null && stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700639 }
640
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800641 /** The top most stack on its display. */
642 boolean isFrontStackOnDisplay(ActivityStack stack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700643 return isFrontOfStackList(stack, stack.getDisplay().mStacks);
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800644 }
645
646 private boolean isFrontOfStackList(ActivityStack stack, List<ActivityStack> stackList) {
Andrii Kuliane8d928a2016-11-14 18:38:14 -0800647 return stack == stackList.get((stackList.size() - 1));
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700648 }
649
Wale Ogunwaled046a012015-12-24 13:05:59 -0800650 /** NOTE: Should only be called from {@link ActivityStack#moveToFront} */
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800651 void setFocusStackUnchecked(String reason, ActivityStack focusCandidate) {
652 if (!focusCandidate.isFocusable()) {
653 // The focus candidate isn't focusable. Move focus to the top stack that is focusable.
Andrii Kulian7fc22812016-12-28 13:04:11 -0800654 focusCandidate = getNextFocusableStackLocked(focusCandidate);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800655 }
656
657 if (focusCandidate != mFocusedStack) {
Wale Ogunwaled046a012015-12-24 13:05:59 -0800658 mLastFocusedStack = mFocusedStack;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800659 mFocusedStack = focusCandidate;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800660
Wale Ogunwaled046a012015-12-24 13:05:59 -0800661 EventLogTags.writeAmFocusedStack(
662 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
663 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
664 }
665
666 final ActivityRecord r = topRunningActivityLocked();
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800667 if (mService.mBooting || !mService.mBooted) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800668 if (r != null && r.idle) {
669 checkFinishBootingLocked();
670 }
671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700672 }
673
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700674 void moveHomeStackToFront(String reason) {
675 mHomeStack.moveToFront(reason);
676 }
677
Matthew Ng330757d2017-02-28 14:19:17 -0800678 void moveRecentsStackToFront(String reason) {
679 final ActivityStack recentsStack = getStack(RECENTS_STACK_ID);
680 if (recentsStack != null) {
681 recentsStack.moveToFront(reason);
682 }
683 }
684
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700685 /** Returns true if the focus activity was adjusted to the home stack top activity. */
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800686 boolean moveHomeStackTaskToTop(String reason) {
687 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700688
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700689 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700690 if (top == null) {
691 return false;
692 }
Chong Zhang6cda19c2016-06-14 19:07:56 -0700693 moveFocusableActivityStackToFrontLocked(top, reason);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700694 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700695 }
696
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800697 boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700698 if (!mService.mBooting && !mService.mBooted) {
699 // Not ready yet!
700 return false;
701 }
702
Craig Mautner84984fa2014-06-19 11:19:20 -0700703 if (prev != null) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700704 prev.getTask().setTaskToReturnTo(ACTIVITY_TYPE_STANDARD);
Craig Mautner84984fa2014-06-19 11:19:20 -0700705 }
706
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800707 mHomeStack.moveHomeStackTaskToTop();
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700708 ActivityRecord r = getHomeActivity();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800709 final String myReason = reason + " resumeHomeStackTask";
710
Mark Lua56ea122015-10-08 13:31:01 +0800711 // Only resume home activity if isn't finishing.
712 if (r != null && !r.finishing) {
Chong Zhang6cda19c2016-06-14 19:07:56 -0700713 moveFocusableActivityStackToFrontLocked(r, myReason);
Wale Ogunwaled046a012015-12-24 13:05:59 -0800714 return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700715 }
Wale Ogunwaled046a012015-12-24 13:05:59 -0800716 return mService.startHomeActivityLocked(mCurrentUser, myReason);
Craig Mautner69ada552013-04-18 13:51:51 -0700717 }
718
Craig Mautner8d341ef2013-03-26 09:03:27 -0700719 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700720 return anyTaskForIdLocked(id, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE);
721 }
722
723 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode) {
724 return anyTaskForIdLocked(id, matchMode, null);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700725 }
726
727 /**
Bryce Lee92b7b652017-04-03 08:33:11 -0700728 * Returns a {@link TaskRecord} for the input id if available. {@code null} otherwise.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700729 * @param id Id of the task we would like returned.
Winson Chung7900bee2017-03-10 08:59:25 -0800730 * @param matchMode The mode to match the given task id in.
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700731 * @param aOptions The activity options to use for restoration. Can be null.
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700732 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700733 TaskRecord anyTaskForIdLocked(int id, @AnyTaskForIdMatchTaskMode int matchMode,
734 @Nullable ActivityOptions aOptions) {
Winson Chung7900bee2017-03-10 08:59:25 -0800735 // If there is a stack id set, ensure that we are attempting to actually restore a task
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700736 // TODO: Don't really know if this is needed...
737 if (matchMode != MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE && aOptions != null) {
738 throw new IllegalArgumentException("Should not specify activity options for non-restore"
739 + " lookup");
Winson Chung7900bee2017-03-10 08:59:25 -0800740 }
741
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
746 ActivityStack stack = stacks.get(stackNdx);
Bryce Lee92b7b652017-04-03 08:33:11 -0700747 final TaskRecord task = stack.taskForIdLocked(id);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800748 if (task != null) {
749 return task;
750 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700751 }
752 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800753
Winson Chung7900bee2017-03-10 08:59:25 -0800754 // If we are matching stack tasks only, return now
755 if (matchMode == MATCH_TASK_IN_STACKS_ONLY) {
Wale Ogunwale7de05352014-12-12 15:21:33 -0800756 return null;
757 }
758
Winson Chung7900bee2017-03-10 08:59:25 -0800759 // Otherwise, check the recent tasks and return if we find it there and we are not restoring
760 // the task from recents
761 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Bryce Lee92b7b652017-04-03 08:33:11 -0700762 final TaskRecord task = mRecentTasks.taskForIdLocked(id);
763
764 if (task == null) {
765 if (DEBUG_RECENTS) {
Winson Chung7900bee2017-03-10 08:59:25 -0800766 Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
767 }
Bryce Lee92b7b652017-04-03 08:33:11 -0700768
769 return null;
770 }
771
772 if (matchMode == MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700773 return task;
774 }
775
Winson Chung7900bee2017-03-10 08:59:25 -0800776 // Implicitly, this case is MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700777 if (!restoreRecentTaskLocked(task, aOptions)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700778 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
779 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800780 return null;
781 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700782 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800783 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700784 }
785
Craig Mautner6170f732013-04-02 13:05:23 -0700786 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800787 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800789 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
791 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
792 if (r != null) {
793 return r;
794 }
Craig Mautner6170f732013-04-02 13:05:23 -0700795 }
796 }
797 return null;
798 }
799
Tony Mak853304c2016-04-18 15:17:41 +0100800 /**
Robin Lee3fef1f22016-12-20 14:50:13 +0000801 * Detects whether we should show a lock screen in front of this task for a locked user.
802 * <p>
803 * We'll do this if either of the following holds:
804 * <ul>
805 * <li>The top activity explicitly belongs to {@param userId}.</li>
806 * <li>The top activity returns a result to an activity belonging to {@param userId}.</li>
807 * </ul>
808 *
809 * @return {@code true} if the top activity looks like it belongs to {@param userId}.
Tony Mak853304c2016-04-18 15:17:41 +0100810 */
Robin Lee3fef1f22016-12-20 14:50:13 +0000811 private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) {
812 // To handle the case that work app is in the task but just is not the top one.
813 final ActivityRecord activityRecord = task.getTopActivity();
814 final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null);
815
816 return (activityRecord != null && activityRecord.userId == userId)
817 || (resultTo != null && resultTo.userId == userId);
818 }
819
820 /**
821 * Find all visible task stacks containing {@param userId} and intercept them with an activity
822 * to block out the contents and possibly start a credential-confirming intent.
823 *
824 * @param userId user handle for the locked managed profile.
825 */
826 void lockAllProfileTasks(@UserIdInt int userId) {
827 mWindowManager.deferSurfaceLayout();
828 try {
829 final List<ActivityStack> stacks = getStacks();
830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; stackNdx--) {
831 final List<TaskRecord> tasks = stacks.get(stackNdx).getAllTasks();
832 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) {
833 final TaskRecord task = tasks.get(taskNdx);
834
835 // Check the task for a top activity belonging to userId, or returning a result
836 // to an activity belonging to userId. Example case: a document picker for
837 // personal files, opened by a work app, should still get locked.
838 if (taskTopActivityIsUser(task, userId)) {
Robin Leec41f6ec2017-01-10 17:02:34 +0000839 mService.mTaskChangeNotificationController.notifyTaskProfileLocked(
840 task.taskId, userId);
Robin Lee3fef1f22016-12-20 14:50:13 +0000841 }
Tony Mak853304c2016-04-18 15:17:41 +0100842 }
843 }
Robin Lee3fef1f22016-12-20 14:50:13 +0000844 } finally {
845 mWindowManager.continueSurfaceLayout();
Tony Mak853304c2016-04-18 15:17:41 +0100846 }
Clara Bayarrif0649ce2016-01-14 12:30:42 +0000847 }
848
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800849 void setNextTaskIdForUserLocked(int taskId, int userId) {
850 final int currentTaskId = mCurTaskIdForUser.get(userId, -1);
851 if (taskId > currentTaskId) {
852 mCurTaskIdForUser.put(userId, taskId);
Craig Mautneref73ee12014-04-23 11:45:37 -0700853 }
854 }
855
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700856 static int nextTaskIdForUser(int taskId, int userId) {
857 int nextTaskId = taskId + 1;
858 if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
859 // Wrap around as there will be smaller task ids that are available now.
860 nextTaskId -= MAX_TASK_IDS_PER_USER;
861 }
862 return nextTaskId;
863 }
864
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800865 int getNextTaskIdForUserLocked(int userId) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800866 final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
867 // for a userId u, a taskId can only be in the range
868 // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
869 // 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 -0700870 int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800871 while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
Wale Ogunwale0568aed2017-09-08 13:29:37 -0700872 || anyTaskForIdLocked(
873 candidateTaskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS) != null) {
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700874 candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800875 if (candidateTaskId == currentTaskId) {
876 // Something wrong!
877 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
878 throw new IllegalStateException("Cannot get an available task id."
879 + " Reached limit of " + MAX_TASK_IDS_PER_USER
880 + " running tasks per user.");
881 }
882 }
883 mCurTaskIdForUser.put(userId, candidateTaskId);
884 return candidateTaskId;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700885 }
886
Chong Zhang6cda19c2016-06-14 19:07:56 -0700887 ActivityRecord getResumedActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800888 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700889 if (stack == null) {
890 return null;
891 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700892 ActivityRecord resumedActivity = stack.mResumedActivity;
893 if (resumedActivity == null || resumedActivity.app == null) {
894 resumedActivity = stack.mPausingActivity;
895 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700896 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 }
898 }
899 return resumedActivity;
900 }
901
Dianne Hackbornff072722014-09-24 10:56:28 -0700902 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700903 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800904 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
906 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800907 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
908 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700909 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800910 continue;
911 }
Jorim Jaggiea039a82017-08-02 14:37:49 +0200912 stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList);
913 final ActivityRecord top = stack.topRunningActivityLocked();
914 final int size = mTmpActivityList.size();
915 for (int i = 0; i < size; i++) {
916 final ActivityRecord activity = mTmpActivityList.get(i);
917 if (activity.app == null && app.uid == activity.info.applicationInfo.uid
918 && processName.equals(activity.processName)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800919 try {
Jorim Jaggiea039a82017-08-02 14:37:49 +0200920 if (realStartActivityLocked(activity, app,
921 top == activity /* andResume */, true /* checkConfig */)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800922 didSomething = true;
923 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700924 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800925 Slog.w(TAG, "Exception in new application when starting activity "
Jorim Jaggiea039a82017-08-02 14:37:49 +0200926 + top.intent.getComponent().flattenToShortString(), e);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800927 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700928 }
Craig Mautner20e72272013-04-01 13:45:53 -0700929 }
Craig Mautner20e72272013-04-01 13:45:53 -0700930 }
931 }
932 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700933 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700934 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700935 }
Craig Mautner20e72272013-04-01 13:45:53 -0700936 return didSomething;
937 }
938
939 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800940 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
941 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
943 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700944 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 continue;
946 }
947 final ActivityRecord resumedActivity = stack.mResumedActivity;
948 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700949 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800950 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800951 return false;
952 }
Craig Mautner20e72272013-04-01 13:45:53 -0700953 }
954 }
Wei Wang65c7a152016-06-02 18:51:22 -0700955 // Send launch end powerhint when idle
956 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
Craig Mautner20e72272013-04-01 13:45:53 -0700957 return true;
958 }
959
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800961 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
962 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800963 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
964 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700965 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800966 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700967 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800968 return false;
969 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700970 }
971 }
972 }
973 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700974 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800975 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
976 mLastFocusedStack + " to=" + mFocusedStack);
977 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 return true;
979 }
980
981 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800982 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800983 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
984 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800985 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
986 final ActivityStack stack = stacks.get(stackNdx);
987 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800988 if (r != null) {
Bryce Lee4a194382017-04-04 14:32:48 -0700989 if (!r.nowVisible || mActivitiesWaitingForVisibleActivity.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800990 return false;
991 }
992 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800993 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700994 }
995 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800996 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700997 }
998
Craig Mautner2acc3892013-09-23 10:28:14 -0700999 /**
1000 * Pause all activities in either all of the stacks or just the back stacks.
1001 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Wale Ogunwale950faff2016-08-08 09:51:04 -07001002 * @param resuming The resuming activity.
1003 * @param dontWait The resuming activity isn't going to wait for all activities to be paused
1004 * before resuming.
Craig Mautner2acc3892013-09-23 10:28:14 -07001005 * @return true if any activity was paused as a result of this call.
1006 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001007 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001008 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001009 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1010 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001011 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1012 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001013 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001014 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -08001015 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001016 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
1017 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001018 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001019 }
1020 }
1021 return someActivityPaused;
1022 }
1023
Craig Mautnerde4ef022013-04-07 19:01:33 -07001024 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001025 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -08001026 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1027 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001028 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1029 final ActivityStack stack = stacks.get(stackNdx);
1030 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001031 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001032 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001033 Slog.d(TAG_STATES,
1034 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001035 pausing = false;
1036 } else {
1037 return false;
1038 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001039 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 }
1041 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001042 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 }
1044
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001045 void cancelInitializingActivities() {
1046 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1047 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
1048 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1049 stacks.get(stackNdx).cancelInitializingActivities();
1050 }
1051 }
1052 }
1053
Bryce Lee4a194382017-04-04 14:32:48 -07001054 void waitActivityVisible(ComponentName name, WaitResult result) {
1055 final WaitInfo waitInfo = new WaitInfo(name, result);
1056 mWaitingForActivityVisible.add(waitInfo);
1057 }
1058
1059 void cleanupActivity(ActivityRecord r) {
1060 // Make sure this record is no longer in the pending finishes list.
1061 // This could happen, for example, if we are trimming activities
1062 // down to the max limit while they are still waiting to finish.
1063 mFinishingActivities.remove(r);
1064 mActivitiesWaitingForVisibleActivity.remove(r);
1065
1066 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
Wale Ogunwale3270f172017-04-26 07:29:42 -07001067 if (mWaitingForActivityVisible.get(i).matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001068 mWaitingForActivityVisible.remove(i);
1069 }
1070 }
1071 }
1072
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001073 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001074 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001075 }
1076
1077 void sendWaitingVisibleReportLocked(ActivityRecord r) {
1078 boolean changed = false;
Bryce Lee4a194382017-04-04 14:32:48 -07001079 for (int i = mWaitingForActivityVisible.size() - 1; i >= 0; --i) {
1080 final WaitInfo w = mWaitingForActivityVisible.get(i);
Wale Ogunwale3270f172017-04-26 07:29:42 -07001081 if (w.matches(r.realActivity)) {
Bryce Lee4a194382017-04-04 14:32:48 -07001082 final WaitResult result = w.getResult();
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001083 changed = true;
Bryce Lee4a194382017-04-04 14:32:48 -07001084 result.timeout = false;
1085 result.who = w.getComponent();
1086 result.totalTime = SystemClock.uptimeMillis() - result.thisTime;
1087 result.thisTime = result.totalTime;
1088 mWaitingForActivityVisible.remove(w);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001089 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001091 if (changed) {
1092 mService.notifyAll();
1093 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 }
1095
Chong Zhang5022da32016-06-21 16:31:37 -07001096 void reportTaskToFrontNoLaunch(ActivityRecord r) {
1097 boolean changed = false;
1098 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
1099 WaitResult w = mWaitingActivityLaunched.remove(i);
1100 if (w.who == null) {
1101 changed = true;
1102 // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
1103 // the starting activity ends up moving another activity to front, and it should
1104 // wait for this new activity to become visible instead.
1105 // Do not modify other fields.
1106 w.result = START_TASK_TO_FRONT;
1107 }
1108 }
1109 if (changed) {
1110 mService.notifyAll();
1111 }
1112 }
1113
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
1115 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001116 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001117 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -07001118 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001119 if (w.who == null) {
1120 changed = true;
1121 w.timeout = timeout;
1122 if (r != null) {
1123 w.who = new ComponentName(r.info.packageName, r.info.name);
1124 }
1125 w.thisTime = thisTime;
1126 w.totalTime = totalTime;
Chong Zhang5022da32016-06-21 16:31:37 -07001127 // Do not modify w.result.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001128 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001129 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -07001130 if (changed) {
1131 mService.notifyAll();
1132 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001133 }
1134
Craig Mautner29219d92013-04-16 20:19:12 -07001135 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001136 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001137 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -07001138 if (r != null) {
1139 return r;
Craig Mautner29219d92013-04-16 20:19:12 -07001140 }
Craig Mautner1602ec22013-05-12 10:24:27 -07001141
Andrii Kulian7318d632016-07-20 18:59:28 -07001142 // Look in other non-focused and non-home stacks.
Andrii Kulian7d95df42017-02-15 10:11:48 -08001143 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
1144
1145 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
1146 final int displayId = mTmpOrderedDisplayIds.get(i);
1147 final List<ActivityStack> stacks = mActivityDisplays.get(displayId).mStacks;
1148 if (stacks == null) {
1149 continue;
1150 }
1151 for (int j = stacks.size() - 1; j >= 0; --j) {
1152 final ActivityStack stack = stacks.get(j);
1153 if (stack != focusedStack && isFrontStackOnDisplay(stack) && stack.isFocusable()) {
1154 r = stack.topRunningActivityLocked();
1155 if (r != null) {
1156 return r;
1157 }
Craig Mautner29219d92013-04-16 20:19:12 -07001158 }
1159 }
1160 }
1161 return null;
1162 }
1163
Dianne Hackborn09233282014-04-30 11:33:59 -07001164 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001165 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001166 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
1167 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -08001168 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001169 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08001170 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001171 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1172 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -07001173 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001174 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -07001175 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -07001176 }
1177 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001178
1179 // The lists are already sorted from most recent to oldest. Just pull the most recent off
1180 // each list and add it to list. Stop when all lists are empty or maxNum reached.
1181 while (maxNum > 0) {
1182 long mostRecentActiveTime = Long.MIN_VALUE;
1183 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001184 final int numTaskLists = runningTaskLists.size();
1185 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
1186 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001187 if (!stackTaskList.isEmpty()) {
1188 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
1189 if (lastActiveTime > mostRecentActiveTime) {
1190 mostRecentActiveTime = lastActiveTime;
1191 selectedStackList = stackTaskList;
1192 }
1193 }
1194 }
1195 if (selectedStackList != null) {
1196 list.add(selectedStackList.remove(0));
1197 --maxNum;
1198 } else {
1199 break;
1200 }
1201 }
Craig Mautner20e72272013-04-01 13:45:53 -07001202 }
1203
Todd Kennedy7440f172015-12-09 14:31:22 -08001204 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
1205 ProfilerInfo profilerInfo) {
1206 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001207 if (aInfo != null) {
1208 // Store the found target back into the intent, because now that
1209 // we have it we never want to do this again. For example, if the
1210 // user navigates back to this point in the history, we should
1211 // always restart the exact same activity.
1212 intent.setComponent(new ComponentName(
1213 aInfo.applicationInfo.packageName, aInfo.name));
1214
1215 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -07001216 if (!aInfo.processName.equals("system")) {
1217 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Chong Zhangd25944e2016-06-09 16:15:27 -07001218 mService.setDebugApp(aInfo.processName, true, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001219 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001220
Tamas Berghammerdf6cb282016-01-29 12:07:00 +00001221 if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
1222 mService.setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
1223 }
1224
Man Caocfa78b22015-06-11 20:14:34 -07001225 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
1226 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
1227 }
1228
1229 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -07001230 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001231 }
1232 }
Todd Kennedyb3b431302017-03-20 16:05:48 -07001233 final String intentLaunchToken = intent.getLaunchToken();
1234 if (aInfo.launchToken == null && intentLaunchToken != null) {
1235 aInfo.launchToken = intentLaunchToken;
1236 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07001237 }
1238 return aInfo;
1239 }
1240
Todd Kennedy7440f172015-12-09 14:31:22 -08001241 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
Kenny Guyb1b30262016-02-09 16:02:35 +00001242 return resolveIntent(intent, resolvedType, userId, 0);
1243 }
1244
1245 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags) {
Todd Kennedy4d1de7d2017-02-23 10:32:18 -08001246 synchronized (mService) {
1247 return mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType,
Todd Kennedybe0b8892017-02-15 14:13:52 -08001248 PackageManager.MATCH_INSTANT | PackageManager.MATCH_DEFAULT_ONLY | flags
Kenny Guyb1b30262016-02-09 16:02:35 +00001249 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001250 }
Todd Kennedy7440f172015-12-09 14:31:22 -08001251 }
1252
1253 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
1254 ProfilerInfo profilerInfo, int userId) {
1255 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
1256 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
1257 }
1258
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001259 final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
1260 boolean andResume, boolean checkConfig) throws RemoteException {
1261
1262 if (!allPausedActivitiesComplete()) {
1263 // While there are activities pausing we skipping starting any new activities until
1264 // pauses are complete. NOTE: that we also do this for activities that are starting in
1265 // the paused state because they will first be resumed then paused on the client side.
1266 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
1267 "realStartActivityLocked: Skipping start of r=" + r
1268 + " some activities pausing...");
1269 return false;
1270 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001271
Bryce Leeaf691c02017-03-20 14:20:22 -07001272 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001273 final ActivityStack stack = task.getStack();
chaviw59b98852017-06-13 12:05:44 -07001274
1275 beginDeferResume();
1276
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 try {
chaviw59b98852017-06-13 12:05:44 -07001278 r.startFreezingScreenLocked(app, 0);
1279
1280 // schedule launch ticks to collect information about slow apps.
1281 r.startLaunchTickingLocked();
1282
1283 r.app = app;
1284
Jorim Jaggi838c2452017-08-28 15:44:43 +02001285 if (mKeyguardController.isKeyguardLocked()) {
1286 r.notifyUnknownVisibilityLaunched();
1287 }
1288
chaviw59b98852017-06-13 12:05:44 -07001289 // Have the window manager re-evaluate the orientation of the screen based on the new
1290 // activity order. Note that as a result of this, it can call back into the activity
1291 // manager with a new orientation. We don't care about that, because the activity is
1292 // not currently running so we are just restarting it anyway.
1293 if (checkConfig) {
1294 final int displayId = r.getDisplayId();
1295 final Configuration config = mWindowManager.updateOrientationFromAppTokens(
1296 getDisplayOverrideConfiguration(displayId),
1297 r.mayFreezeScreenLocked(app) ? r.appToken : null, displayId);
1298 // Deferring resume here because we're going to launch new activity shortly.
1299 // We don't want to perform a redundant launch of the same record while ensuring
1300 // configurations and trying to resume top activity of focused stack.
1301 mService.updateDisplayOverrideConfigurationLocked(config, r, true /* deferResume */,
1302 displayId);
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 }
chaviw59b98852017-06-13 12:05:44 -07001304
1305 if (r.getStack().checkKeyguardVisibility(r, true /* shouldBeVisible */,
1306 true /* isTop */)) {
1307 // We only set the visibility to true if the activity is allowed to be visible
1308 // based on
1309 // keyguard state. This avoids setting this into motion in window manager that is
1310 // later cancelled due to later calls to ensure visible activities that set
1311 // visibility back to false.
1312 r.setVisibility(true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 }
chaviw59b98852017-06-13 12:05:44 -07001314
chaviw59b98852017-06-13 12:05:44 -07001315 final int applicationInfoUid =
1316 (r.info.applicationInfo != null) ? r.info.applicationInfo.uid : -1;
1317 if ((r.userId != app.userId) || (r.appInfo.uid != applicationInfoUid)) {
1318 Slog.wtf(TAG,
1319 "User ID for activity changing for " + r
1320 + " appInfo.uid=" + r.appInfo.uid
1321 + " info.ai.uid=" + applicationInfoUid
1322 + " old=" + r.app + " new=" + app);
1323 }
1324
1325 app.waitingToKill = null;
1326 r.launchCount++;
1327 r.lastLaunchTime = SystemClock.uptimeMillis();
1328
1329 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
1330
1331 int idx = app.activities.indexOf(r);
1332 if (idx < 0) {
1333 app.activities.add(r);
1334 }
1335 mService.updateLruProcessLocked(app, true, null);
1336 mService.updateOomAdjLocked();
1337
1338 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1339 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Benjamin Franza83859f2017-07-03 16:34:14 +01001340 mService.mLockTaskController.startLockTaskMode(task, false, 0 /* blank UID */);
chaviw59b98852017-06-13 12:05:44 -07001341 }
1342
1343 try {
1344 if (app.thread == null) {
1345 throw new RemoteException();
1346 }
1347 List<ResultInfo> results = null;
1348 List<ReferrerIntent> newIntents = null;
1349 if (andResume) {
1350 // We don't need to deliver new intents and/or set results if activity is going
1351 // to pause immediately after launch.
1352 results = r.results;
1353 newIntents = r.newIntents;
1354 }
1355 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1356 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1357 + " newIntents=" + newIntents + " andResume=" + andResume);
1358 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, r.userId,
1359 System.identityHashCode(r), task.taskId, r.shortComponentName);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001360 if (r.isActivityTypeHome()) {
chaviw59b98852017-06-13 12:05:44 -07001361 // Home process is the root process of the task.
1362 mService.mHomeProcess = task.mActivities.get(0).app;
1363 }
1364 mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
1365 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY);
1366 r.sleeping = false;
1367 r.forceNewConfig = false;
1368 mService.showUnsupportedZoomDialogIfNeededLocked(r);
1369 mService.showAskCompatModeDialogLocked(r);
1370 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1371 ProfilerInfo profilerInfo = null;
1372 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1373 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1374 mService.mProfileProc = app;
Andreas Gampe2b073a02017-06-22 17:28:57 -07001375 ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
1376 if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
1377 if (profilerInfoSvc.profileFd != null) {
chaviw59b98852017-06-13 12:05:44 -07001378 try {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001379 profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
chaviw59b98852017-06-13 12:05:44 -07001380 } catch (IOException e) {
Andreas Gampe2b073a02017-06-22 17:28:57 -07001381 profilerInfoSvc.closeFd();
Craig Mautner2568c3a2015-03-26 14:22:34 -07001382 }
1383 }
chaviw59b98852017-06-13 12:05:44 -07001384
Andreas Gampe2b073a02017-06-22 17:28:57 -07001385 profilerInfo = new ProfilerInfo(profilerInfoSvc);
Craig Mautner2420ead2013-04-01 17:13:20 -07001386 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001387 }
1388 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001389
chaviw59b98852017-06-13 12:05:44 -07001390 app.hasShownUi = true;
1391 app.pendingUiClean = true;
1392 app.forceProcessStateUpTo(mService.mTopProcessState);
1393 // Because we could be starting an Activity in the system process this may not go
1394 // across a Binder interface which would create a new Configuration. Consequently
1395 // we have to always create a new Configuration here.
Bryce Leea163b762017-01-24 11:05:01 -08001396
chaviw59b98852017-06-13 12:05:44 -07001397 final MergedConfiguration mergedConfiguration = new MergedConfiguration(
1398 mService.getGlobalConfiguration(), r.getMergedOverrideConfiguration());
1399 r.setLastReportedConfiguration(mergedConfiguration);
Bryce Leea163b762017-01-24 11:05:01 -08001400
chaviw59b98852017-06-13 12:05:44 -07001401 logIfTransactionTooLarge(r.intent, r.icicle);
1402 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1403 System.identityHashCode(r), r.info,
1404 // TODO: Have this take the merged configuration instead of separate global
1405 // and override configs.
1406 mergedConfiguration.getGlobalConfiguration(),
1407 mergedConfiguration.getOverrideConfiguration(), r.compat,
1408 r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
1409 r.persistentState, results, newIntents, !andResume,
1410 mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001411
chaviw59b98852017-06-13 12:05:44 -07001412 if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
1413 // This may be a heavy-weight process! Note that the package
1414 // manager will ensure that only activity can run in the main
1415 // process of the .apk, which is the only thing that will be
1416 // considered heavy-weight.
1417 if (app.processName.equals(app.info.packageName)) {
1418 if (mService.mHeavyWeightProcess != null
1419 && mService.mHeavyWeightProcess != app) {
1420 Slog.w(TAG, "Starting new heavy weight process " + app
1421 + " when already running "
1422 + mService.mHeavyWeightProcess);
1423 }
1424 mService.mHeavyWeightProcess = app;
1425 Message msg = mService.mHandler.obtainMessage(
1426 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1427 msg.obj = r;
1428 mService.mHandler.sendMessage(msg);
Craig Mautner2420ead2013-04-01 17:13:20 -07001429 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001430 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001431
chaviw59b98852017-06-13 12:05:44 -07001432 } catch (RemoteException e) {
1433 if (r.launchFailed) {
1434 // This is the second time we failed -- finish activity
1435 // and give up.
1436 Slog.e(TAG, "Second failure launching "
1437 + r.intent.getComponent().flattenToShortString()
1438 + ", giving up", e);
1439 mService.appDiedLocked(app);
1440 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1441 "2nd-crash", false);
1442 return false;
1443 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001444
chaviw59b98852017-06-13 12:05:44 -07001445 // This is the first time we failed -- restart process and
1446 // retry.
1447 r.launchFailed = true;
1448 app.activities.remove(r);
1449 throw e;
1450 }
1451 } finally {
1452 endDeferResume();
Craig Mautner2420ead2013-04-01 17:13:20 -07001453 }
1454
1455 r.launchFailed = false;
1456 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001457 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001458 }
1459
chaviw59b98852017-06-13 12:05:44 -07001460 if (andResume && readyToResume()) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001461 // As part of the process of launching, ActivityThread also performs
1462 // a resume.
1463 stack.minimalResumeActivityLocked(r);
1464 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001465 // This activity is not starting in the resumed state... which should look like we asked
Wale Ogunwale919a05d2017-04-13 00:36:34 +00001466 // it to pause+stop (but remain visible), and it has done so and reported back the
Wale Ogunwaled046a012015-12-24 13:05:59 -08001467 // current icicle and other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001468 if (DEBUG_STATES) Slog.v(TAG_STATES,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001469 "Moving to PAUSED: " + r + " (starting in paused state)");
1470 r.state = PAUSED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001471 }
1472
1473 // Launch the new version setup screen if needed. We do this -after-
1474 // launching the initial activity (that is, home), so that it can have
1475 // a chance to initialize itself while in the background, making the
1476 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001477 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001478 mService.startSetupActivityLocked();
1479 }
1480
Dianne Hackborn465fa392014-09-14 14:21:18 -07001481 // Update any services we are bound to that might care about whether
1482 // their client may have activities.
Wale Ogunwaled6ac7622016-05-26 09:02:25 -07001483 if (r.app != null) {
1484 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1485 }
Dianne Hackborn465fa392014-09-14 14:21:18 -07001486
Craig Mautner2420ead2013-04-01 17:13:20 -07001487 return true;
1488 }
1489
Sudheer Shankafab200f2017-05-17 20:41:53 -07001490 private void logIfTransactionTooLarge(Intent intent, Bundle icicle) {
1491 int extrasSize = 0;
1492 if (intent != null) {
1493 final Bundle extras = intent.getExtras();
1494 if (extras != null) {
1495 extrasSize = extras.getSize();
1496 }
1497 }
1498 int icicleSize = (icicle == null ? 0 : icicle.getSize());
1499 if (extrasSize + icicleSize > 200000) {
1500 Slog.e(TAG, "Transaction too large, intent: " + intent + ", extras size: " + extrasSize
1501 + ", icicle size: " + icicleSize);
1502 }
1503 }
1504
Craig Mautnere79d42682013-04-01 19:01:53 -07001505 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001506 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001507 // Is this activity's application already running?
1508 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001509 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001510
Andrii Kulian02b7a832016-10-06 23:11:56 -07001511 r.getStack().setLaunchTime(r);
Craig Mautnere79d42682013-04-01 19:01:53 -07001512
1513 if (app != null && app.thread != null) {
1514 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001515 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1516 || !"android".equals(r.info.packageName)) {
1517 // Don't add this if it is a platform component that is marked
1518 // to run in multiple processes, because this is actually
1519 // part of the framework so doesn't make sense to track as a
1520 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001521 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1522 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001523 }
George Mount2c92c972014-03-20 09:38:23 -07001524 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001525 return;
1526 } catch (RemoteException e) {
1527 Slog.w(TAG, "Exception when starting activity "
1528 + r.intent.getComponent().flattenToShortString(), e);
1529 }
1530
1531 // If a dead object exception was thrown -- fall through to
1532 // restart the application.
1533 }
1534
1535 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001536 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001537 }
1538
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001539 boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001540 String resultWho, int requestCode, int callingPid, int callingUid,
1541 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
Jorim Jaggi2adba072016-03-03 13:43:39 +01001542 ActivityRecord resultRecord, ActivityStack resultStack, ActivityOptions options) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001543 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1544 callingUid);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001545 if (startAnyPerm == PERMISSION_GRANTED) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001546 return true;
1547 }
1548 final int componentRestriction = getComponentRestrictionForCallingPackage(
1549 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1550 final int actionRestriction = getActionRestrictionForCallingPackage(
1551 intent.getAction(), callingPackage, callingPid, callingUid);
1552 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1553 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1554 if (resultRecord != null) {
1555 resultStack.sendActivityResultLocked(-1,
1556 resultRecord, resultWho, requestCode,
1557 Activity.RESULT_CANCELED, null);
1558 }
1559 final String msg;
1560 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1561 msg = "Permission Denial: starting " + intent.toString()
1562 + " from " + callerApp + " (pid=" + callingPid
1563 + ", uid=" + callingUid + ")" + " with revoked permission "
1564 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1565 } else if (!aInfo.exported) {
1566 msg = "Permission Denial: starting " + intent.toString()
1567 + " from " + callerApp + " (pid=" + callingPid
1568 + ", uid=" + callingUid + ")"
1569 + " not exported from uid " + aInfo.applicationInfo.uid;
1570 } else {
1571 msg = "Permission Denial: starting " + intent.toString()
1572 + " from " + callerApp + " (pid=" + callingPid
1573 + ", uid=" + callingUid + ")"
1574 + " requires " + aInfo.permission;
1575 }
1576 Slog.w(TAG, msg);
1577 throw new SecurityException(msg);
1578 }
1579
1580 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1581 final String message = "Appop Denial: starting " + intent.toString()
1582 + " from " + callerApp + " (pid=" + callingPid
1583 + ", uid=" + callingUid + ")"
1584 + " requires " + AppOpsManager.permissionToOp(
1585 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1586 Slog.w(TAG, message);
1587 return false;
1588 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1589 final String message = "Appop Denial: starting " + intent.toString()
1590 + " from " + callerApp + " (pid=" + callingPid
1591 + ", uid=" + callingUid + ")"
1592 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1593 Slog.w(TAG, message);
1594 return false;
1595 }
Andrii Kulian16802aa2016-11-02 12:21:33 -07001596 if (options != null) {
1597 if (options.getLaunchTaskId() != INVALID_STACK_ID) {
1598 final int startInTaskPerm = mService.checkPermission(START_TASKS_FROM_RECENTS,
1599 callingPid, callingUid);
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001600 if (startInTaskPerm == PERMISSION_DENIED) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07001601 final String msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ") with launchTaskId="
1604 + options.getLaunchTaskId();
1605 Slog.w(TAG, msg);
1606 throw new SecurityException(msg);
1607 }
1608 }
1609 // Check if someone tries to launch an activity on a private display with a different
1610 // owner.
1611 final int launchDisplayId = options.getLaunchDisplayId();
Andrii Kulian02689a72017-07-06 14:28:59 -07001612 if (launchDisplayId != INVALID_DISPLAY && !isCallerAllowedToLaunchOnDisplay(callingPid,
1613 callingUid, launchDisplayId, aInfo)) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001614 final String msg = "Permission Denial: starting " + intent.toString()
1615 + " from " + callerApp + " (pid=" + callingPid
1616 + ", uid=" + callingUid + ") with launchDisplayId="
1617 + launchDisplayId;
1618 Slog.w(TAG, msg);
1619 throw new SecurityException(msg);
Jorim Jaggi2adba072016-03-03 13:43:39 +01001620 }
1621 }
1622
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001623 return true;
1624 }
1625
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001626 /** Check if caller is allowed to launch activities on specified display. */
Andrii Kulian02689a72017-07-06 14:28:59 -07001627 boolean isCallerAllowedToLaunchOnDisplay(int callingPid, int callingUid, int launchDisplayId,
1628 ActivityInfo aInfo) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001629 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: displayId=" + launchDisplayId
1630 + " callingPid=" + callingPid + " callingUid=" + callingUid);
1631
Andrii Kulian02689a72017-07-06 14:28:59 -07001632 if (callingPid == -1 && callingUid == -1) {
1633 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check: no caller info, skip check");
1634 return true;
1635 }
1636
Andrii Kulian62e6f252017-05-30 22:46:53 -07001637 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001638 if (activityDisplay == null) {
1639 Slog.w(TAG, "Launch on display check: display not found");
1640 return false;
1641 }
1642
Andrii Kulian02689a72017-07-06 14:28:59 -07001643 // Check if the caller has enough privileges to embed activities and launch to private
1644 // displays.
Andrii Kulian3a95edc2017-06-28 16:21:07 -07001645 final int startAnyPerm = mService.checkPermission(INTERNAL_SYSTEM_WINDOW, callingPid,
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001646 callingUid);
1647 if (startAnyPerm == PERMISSION_GRANTED) {
1648 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1649 + " allow launch any on display");
1650 return true;
1651 }
1652
Andrii Kulian8f070292017-09-12 22:56:49 -07001653 // Check if caller is already present on display
1654 final boolean uidPresentOnDisplay = activityDisplay.isUidPresent(callingUid);
1655
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001656 final int displayOwnerUid = activityDisplay.mDisplay.getOwnerUid();
1657 if (activityDisplay.mDisplay.getType() == TYPE_VIRTUAL && displayOwnerUid != SYSTEM_UID
1658 && displayOwnerUid != aInfo.applicationInfo.uid) {
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001659 // Limit launching on virtual displays, because their contents can be read from Surface
1660 // by apps that created them.
Andrii Kulian02689a72017-07-06 14:28:59 -07001661 if ((aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
1662 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1663 + " disallow launch on virtual display for not-embedded activity.");
1664 return false;
1665 }
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001666 // Check if the caller is allowed to embed activities from other apps.
1667 if (mService.checkPermission(ACTIVITY_EMBEDDING, callingPid, callingUid)
Andrii Kulian8f070292017-09-12 22:56:49 -07001668 == PERMISSION_DENIED && !uidPresentOnDisplay) {
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001669 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1670 + " disallow activity embedding without permission.");
1671 return false;
1672 }
Andrii Kulian1cba31c2017-06-28 09:42:48 -07001673 }
1674
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001675 if (!activityDisplay.isPrivate()) {
1676 // Anyone can launch on a public display.
1677 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1678 + " allow launch on public display");
1679 return true;
1680 }
1681
1682 // Check if the caller is the owner of the display.
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001683 if (displayOwnerUid == callingUid) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001684 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1685 + " allow launch for owner of the display");
1686 return true;
1687 }
1688
Andrii Kulian8f070292017-09-12 22:56:49 -07001689 if (uidPresentOnDisplay) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001690 if (DEBUG_TASKS) Slog.d(TAG, "Launch on display check:"
1691 + " allow launch for caller present on the display");
1692 return true;
1693 }
1694
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001695 Slog.w(TAG, "Launch on display check: denied");
1696 return false;
1697 }
1698
1699 /** Update lists of UIDs that are present on displays and have access to them. */
1700 void updateUIDsPresentOnDisplay() {
1701 mDisplayAccessUIDs.clear();
1702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1703 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
David Stevens82ea6cb2017-03-03 16:18:50 -08001704 // Only bother calculating the whitelist for private displays
1705 if (activityDisplay.isPrivate()) {
1706 mDisplayAccessUIDs.append(
1707 activityDisplay.mDisplayId, activityDisplay.getPresentUIDs());
1708 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001709 }
1710 // Store updated lists in DisplayManager. Callers from outside of AM should get them there.
1711 mDisplayManagerInternal.setDisplayAccessUIDs(mDisplayAccessUIDs);
1712 }
1713
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08001714 UserInfo getUserInfo(int userId) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001715 final long identity = Binder.clearCallingIdentity();
1716 try {
1717 return UserManager.get(mService.mContext).getUserInfo(userId);
1718 } finally {
1719 Binder.restoreCallingIdentity(identity);
1720 }
1721 }
1722
Svet Ganov99b60432015-06-27 13:15:22 -07001723 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001724 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001725 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1726 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001727 == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001728 return ACTIVITY_RESTRICTION_PERMISSION;
1729 }
1730
Christopher Tateff7add02015-08-17 10:23:22 -07001731 if (activityInfo.permission == null) {
1732 return ACTIVITY_RESTRICTION_NONE;
1733 }
1734
Svet Ganov99b60432015-06-27 13:15:22 -07001735 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1736 if (opCode == AppOpsManager.OP_NONE) {
1737 return ACTIVITY_RESTRICTION_NONE;
1738 }
1739
1740 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1741 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001742 if (!ignoreTargetSecurity) {
1743 return ACTIVITY_RESTRICTION_APPOP;
1744 }
Svet Ganov99b60432015-06-27 13:15:22 -07001745 }
1746
1747 return ACTIVITY_RESTRICTION_NONE;
1748 }
1749
Svetoslav7008b512015-06-24 18:47:07 -07001750 private int getActionRestrictionForCallingPackage(String action,
1751 String callingPackage, int callingPid, int callingUid) {
1752 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
1756 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1757 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001758 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001759 }
1760
1761 final PackageInfo packageInfo;
1762 try {
1763 packageInfo = mService.mContext.getPackageManager()
1764 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1765 } catch (PackageManager.NameNotFoundException e) {
1766 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001767 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001768 }
1769
1770 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001771 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001772 }
1773
Andrii Kulian3c9ad072017-08-01 11:45:22 -07001774 if (mService.checkPermission(permission, callingPid, callingUid) == PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001775 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001776 }
1777
1778 final int opCode = AppOpsManager.permissionToOpCode(permission);
1779 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001780 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001781 }
1782
1783 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1784 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001785 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001786 }
1787
Svet Ganov99b60432015-06-27 13:15:22 -07001788 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001789 }
1790
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001791 void setLaunchSource(int uid) {
1792 mLaunchingActivity.setWorkSource(new WorkSource(uid));
1793 }
1794
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001795 void acquireLaunchWakelock() {
1796 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1797 throw new IllegalStateException("Calling must be system uid");
1798 }
1799 mLaunchingActivity.acquire();
1800 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1801 // To be safe, don't allow the wake lock to be held for too long.
1802 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1803 }
1804 }
1805
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001806 /**
1807 * Called when the frontmost task is idle.
1808 * @return the state of mService.mBooting before this was called.
1809 */
1810 private boolean checkFinishBootingLocked() {
1811 final boolean booting = mService.mBooting;
1812 boolean enableScreen = false;
1813 mService.mBooting = false;
1814 if (!mService.mBooted) {
1815 mService.mBooted = true;
1816 enableScreen = true;
1817 }
1818 if (booting || enableScreen) {
1819 mService.postFinishBooting(booting, enableScreen);
1820 }
1821 return booting;
1822 }
1823
Craig Mautnerf3333272013-04-22 10:55:53 -07001824 // Checked.
1825 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
Winson Chung4dabf232017-01-25 13:25:22 -08001826 boolean processPausingActivities, Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001827 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001828
Craig Mautnerf3333272013-04-22 10:55:53 -07001829 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07001830 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07001831 int NS = 0;
1832 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001833 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07001834 boolean activityRemoved = false;
1835
Wale Ogunwale7d701172015-03-11 15:36:30 -07001836 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07001837 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001838 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
1839 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001840 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1841 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001842 if (fromTimeout) {
1843 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07001844 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001845
1846 // This is a hack to semi-deal with a race condition
1847 // in the client where it can be constructed with a
1848 // newer configuration from when we asked it to launch.
1849 // We'll update with whatever configuration it now says
1850 // it used to launch.
1851 if (config != null) {
Bryce Leea163b762017-01-24 11:05:01 -08001852 r.setLastReportedGlobalConfiguration(config);
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001853 }
1854
1855 // We are now idle. If someone is waiting for a thumbnail from
1856 // us, we can now deliver.
1857 r.idle = true;
1858
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001859 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001860 if (isFocusedStack(r.getStack()) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08001861 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001862 }
1863 }
1864
1865 if (allResumedActivitiesIdle()) {
1866 if (r != null) {
1867 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001868 }
1869
1870 if (mLaunchingActivity.isHeld()) {
1871 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
1872 if (VALIDATE_WAKE_LOCK_CALLER &&
1873 Binder.getCallingUid() != Process.myUid()) {
1874 throw new IllegalStateException("Calling must be system uid");
1875 }
1876 mLaunchingActivity.release();
1877 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001878 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07001879 }
1880
1881 // Atomically retrieve all of the other things to do.
Winson Chung4dabf232017-01-25 13:25:22 -08001882 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r,
1883 true /* remove */, processPausingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001884 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001885 if ((NF = mFinishingActivities.size()) > 0) {
1886 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07001887 mFinishingActivities.clear();
1888 }
1889
Craig Mautnerf3333272013-04-22 10:55:53 -07001890 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001891 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07001892 mStartingUsers.clear();
1893 }
1894
Craig Mautnerf3333272013-04-22 10:55:53 -07001895 // Stop any activities that are scheduled to do so but have been
1896 // waiting for the next one to start.
1897 for (int i = 0; i < NS; i++) {
1898 r = stops.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001899 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001900 if (stack != null) {
1901 if (r.finishing) {
1902 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
1903 } else {
1904 stack.stopActivityLocked(r);
1905 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001906 }
1907 }
1908
1909 // Finish any activities that are scheduled to do so but have been
1910 // waiting for the next one to start.
1911 for (int i = 0; i < NF; i++) {
1912 r = finishes.get(i);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001913 final ActivityStack stack = r.getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001914 if (stack != null) {
1915 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
1916 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001917 }
1918
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001919 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001920 // Complete user switch
1921 if (startingUsers != null) {
1922 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001923 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07001924 }
1925 }
Craig Mautnerf3333272013-04-22 10:55:53 -07001926 }
1927
1928 mService.trimApplications();
1929 //dump();
1930 //mWindowManager.dump();
1931
Craig Mautnerf3333272013-04-22 10:55:53 -07001932 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001933 resumeFocusedStackTopActivityLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07001934 }
1935
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001936 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07001937 }
1938
Craig Mautner8e569572013-10-11 17:36:59 -07001939 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07001940 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001941 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1942 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001943 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1944 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
1945 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001946 }
Craig Mautner19091252013-10-05 00:03:53 -07001947 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07001948 }
1949
1950 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08001951 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1952 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001953 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
1954 stacks.get(stackNdx).closeSystemDialogsLocked();
1955 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001956 }
1957 }
1958
Craig Mautner93529a42013-10-04 15:03:13 -07001959 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001960 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07001961 }
1962
Craig Mautner8d341ef2013-03-26 09:03:27 -07001963 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07001964 * Update the last used stack id for non-current user (current user's last
1965 * used stack is the focused stack)
1966 */
1967 void updateUserStackLocked(int userId, ActivityStack stack) {
1968 if (userId != mCurrentUser) {
1969 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
1970 }
1971 }
1972
1973 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07001974 * @return true if some activity was finished (or would have finished if doit were true).
1975 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07001976 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
1977 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07001978 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08001979 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
1980 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08001981 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001982 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07001983 if (stack.finishDisabledPackageActivitiesLocked(
1984 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001985 didSomething = true;
1986 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001987 }
1988 }
1989 return didSomething;
1990 }
1991
Dianne Hackborna413dc02013-07-12 12:02:55 -07001992 void updatePreviousProcessLocked(ActivityRecord r) {
1993 // Now that this process has stopped, we may want to consider
1994 // it to be the previous app to try to keep around in case
1995 // the user wants to return to it.
1996
1997 // First, found out what is currently the foreground app, so that
1998 // we don't blow away the previous app if this activity is being
1999 // hosted by the process that is actually still the foreground.
2000 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2002 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002003 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2004 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002005 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002006 if (stack.mResumedActivity != null) {
2007 fgApp = stack.mResumedActivity.app;
2008 } else if (stack.mPausingActivity != null) {
2009 fgApp = stack.mPausingActivity.app;
2010 }
2011 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002012 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002013 }
2014 }
2015
2016 // Now set this one as the previous process, only if that really
2017 // makes sense to.
2018 if (r.app != null && fgApp != null && r.app != fgApp
2019 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002020 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002021 mService.mPreviousProcess = r.app;
2022 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2023 }
2024 }
2025
Wale Ogunwaled046a012015-12-24 13:05:59 -08002026 boolean resumeFocusedStackTopActivityLocked() {
2027 return resumeFocusedStackTopActivityLocked(null, null, null);
Craig Mautner05d29032013-05-03 13:40:13 -07002028 }
2029
Wale Ogunwaled046a012015-12-24 13:05:59 -08002030 boolean resumeFocusedStackTopActivityLocked(
Chong Zhang280d3322015-11-03 17:27:26 -08002031 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
chaviw59b98852017-06-13 12:05:44 -07002032
2033 if (!readyToResume()) {
2034 return false;
2035 }
2036
Wale Ogunwaled046a012015-12-24 13:05:59 -08002037 if (targetStack != null && isFocusedStack(targetStack)) {
2038 return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
Craig Mautner05d29032013-05-03 13:40:13 -07002039 }
chaviw59b98852017-06-13 12:05:44 -07002040
Wale Ogunwale06579d62016-04-30 15:29:06 -07002041 final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
2042 if (r == null || r.state != RESUMED) {
2043 mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002044 } else if (r.state == RESUMED) {
2045 // Kick off any lingering app transitions form the MoveTaskToFront operation.
2046 mFocusedStack.executeAppTransition(targetOptions);
Wale Ogunwale06579d62016-04-30 15:29:06 -07002047 }
chaviw59b98852017-06-13 12:05:44 -07002048
Wale Ogunwaled046a012015-12-24 13:05:59 -08002049 return false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002050 }
2051
Todd Kennedy39bfee52016-02-24 10:28:21 -08002052 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
2053 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2054 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2055 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2056 stacks.get(stackNdx).updateActivityApplicationInfoLocked(aInfo);
2057 }
2058 }
2059 }
2060
Adrian Roos20d7df32016-01-12 18:59:43 +01002061 TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
2062 TaskRecord finishedTask = null;
2063 ActivityStack focusedStack = getFocusedStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002064 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2065 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002066 final int numStacks = stacks.size();
2067 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2068 final ActivityStack stack = stacks.get(stackNdx);
Adrian Roos20d7df32016-01-12 18:59:43 +01002069 TaskRecord t = stack.finishTopRunningActivityLocked(app, reason);
2070 if (stack == focusedStack || finishedTask == null) {
2071 finishedTask = t;
2072 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002073 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002074 }
Adrian Roos20d7df32016-01-12 18:59:43 +01002075 return finishedTask;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002076 }
2077
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002078 void finishVoiceTask(IVoiceInteractionSession session) {
2079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2080 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2081 final int numStacks = stacks.size();
2082 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2083 final ActivityStack stack = stacks.get(stackNdx);
2084 stack.finishVoiceTask(session);
2085 }
2086 }
2087 }
2088
Andrii Kulianc27916642016-04-12 17:59:27 -07002089 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, ActivityOptions options,
2090 String reason, boolean forceNonResizeable) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002091 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2092 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002093 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002094 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2095 // Caller wants the home activity moved with it. To accomplish this,
2096 // we'll just indicate that this task returns to the home task.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002097 task.setTaskToReturnTo(ACTIVITY_TYPE_HOME);
Craig Mautneraea74a52014-03-08 14:23:10 -08002098 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002099 final ActivityStack currentStack = task.getStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002100 if (currentStack == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002101 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2102 + task + " to front. Stack is null");
2103 return;
2104 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002105
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002106 if (task.isResizeable() && canUseActivityOptionsLaunchBounds(options)) {
2107 final Rect bounds = TaskRecord.validateBounds(options.getLaunchBounds());
2108 task.updateOverrideConfiguration(bounds);
2109
2110 int stackId = getLaunchStackId(null, options, task);
2111
2112 if (stackId != currentStack.mStackId) {
2113 task.reparent(stackId, ON_TOP, REPARENT_KEEP_STACK_AT_FRONT, !ANIMATE,
2114 DEFER_RESUME, "findTaskToMoveToFrontLocked");
2115 stackId = currentStack.mStackId;
2116 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2117 // still need moveTaskToFrontLocked() below for any transition settings.
2118 }
2119 if (StackId.resizeStackWithLaunchBounds(stackId)) {
2120 resizeStackLocked(stackId, bounds,
2121 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
2122 !PRESERVE_WINDOWS, true /* allowResizeInDockedMode */, !DEFER_RESUME);
2123 } else {
2124 // WM resizeTask must be done after the task is moved to the correct stack,
2125 // because Task's setBounds() also updates dim layer's bounds, but that has
2126 // dependency on the stack.
2127 task.resizeWindowContainer();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002128 }
2129 }
2130
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002131 final ActivityRecord r = task.getTopActivity();
Andrii Kulian02b7a832016-10-06 23:11:56 -07002132 currentStack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002133 r == null ? null : r.appTimeTracker, reason);
2134
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002135 if (DEBUG_STACK) Slog.d(TAG_STACK,
Andrii Kulian02b7a832016-10-06 23:11:56 -07002136 "findTaskToMoveToFront: moved to front of stack=" + currentStack);
Jorim Jaggi2adba072016-03-03 13:43:39 +01002137
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002138 handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, DEFAULT_DISPLAY,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07002139 currentStack.mStackId, forceNonResizeable);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002140 }
2141
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002142 boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002143 // We use the launch bounds in the activity options is the device supports freeform
Wale Ogunwale854809c2015-12-27 16:18:19 -08002144 // window management or is launching into the pinned stack.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002145 if (options == null || options.getLaunchBounds() == null) {
Wale Ogunwale854809c2015-12-27 16:18:19 -08002146 return false;
2147 }
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002148 return (mService.mSupportsPictureInPicture
2149 && options.getLaunchWindowingMode() == WINDOWING_MODE_PINNED)
Wale Ogunwale854809c2015-12-27 16:18:19 -08002150 || mService.mSupportsFreeformWindowManagement;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002151 }
2152
Winson Chung55893332017-02-17 17:13:10 -08002153 protected <T extends ActivityStack> T getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002154 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002155 }
2156
Winson Chung55893332017-02-17 17:13:10 -08002157 protected <T extends ActivityStack> T getStack(int stackId, boolean createStaticStackIfNeeded,
2158 boolean createOnTop) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002159 final ActivityStack stack = mStacks.get(stackId);
2160 if (stack != null) {
2161 return (T) stack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002162 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002163 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002164 return null;
2165 }
Matthew Ng330757d2017-02-28 14:19:17 -08002166 if (stackId == DOCKED_STACK_ID) {
2167 // Make sure recents stack exist when creating a dock stack as it normally need to be on
2168 // the other side of the docked stack and we make visibility decisions based on that.
2169 getStack(RECENTS_STACK_ID, CREATE_IF_NEEDED, createOnTop);
2170 }
Winson Chung55893332017-02-17 17:13:10 -08002171 return (T) createStackOnDisplay(stackId, DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002172 }
2173
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002174 private int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
2175 @Nullable TaskRecord task) {
2176
2177 // First preference if the windowing mode in the activity options if set.
2178 int windowingMode = (options != null)
2179 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
2180
2181 // If windowing mode is unset, then next preference is the candidate task, then the
2182 // activity record.
2183 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2184 if (task != null) {
2185 windowingMode = task.getWindowingMode();
2186 }
2187 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
2188 windowingMode = r.getWindowingMode();
2189 }
2190 }
2191
2192 // Make sure the windowing mode we are trying to use makes sense for what is supported.
2193 if (!mService.mSupportsMultiWindow && windowingMode != WINDOWING_MODE_FULLSCREEN) {
2194 windowingMode = WINDOWING_MODE_FULLSCREEN;
2195 }
2196
2197 if (!mService.mSupportsSplitScreenMultiWindow
2198 && (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2199 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)) {
2200 windowingMode = WINDOWING_MODE_FULLSCREEN;
2201 }
2202
2203 if (windowingMode == WINDOWING_MODE_FREEFORM
2204 && !mService.mSupportsFreeformWindowManagement) {
2205 windowingMode = WINDOWING_MODE_FULLSCREEN;
2206 }
2207
2208 return windowingMode;
2209 }
2210
2211 private int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
2212 @Nullable TaskRecord task) {
2213 // First preference if the activity type in the activity options if set.
2214 int activityType = (options != null)
2215 ? options.getLaunchActivityType() : ACTIVITY_TYPE_UNDEFINED;
2216
2217 if (activityType != ACTIVITY_TYPE_UNDEFINED) {
2218 return activityType;
2219 }
2220
2221 // If activity type is unset, then next preference is the task, then the activity record.
2222 if (task != null) {
2223 activityType = task.getActivityType();
2224 }
2225 if (activityType == ACTIVITY_TYPE_UNDEFINED && r != null) {
2226 activityType = r.getActivityType();
2227 }
2228 return activityType;
2229 }
2230
2231 int getLaunchStackId(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
2232 @Nullable TaskRecord candidateTask) {
2233 return getLaunchStackId(r, options, candidateTask, INVALID_DISPLAY);
2234 }
2235
2236 /**
2237 * Returns the right stack to use for launching factoring in all the input parameters.
2238 *
2239 * @param r The activity we are trying to launch. Can be null.
2240 * @param options The activity options used to the launch. Can be null.
2241 * @param candidateTask The possible task the activity might be launched in. Can be null.
2242 *
2243 * @return The stack to use for the launch or INVALID_STACK_ID.
2244 */
2245 int getLaunchStackId(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
2246 @Nullable TaskRecord candidateTask, int candidateDisplayId) {
2247 int taskId = INVALID_TASK_ID;
2248 int displayId = INVALID_DISPLAY;
2249 //Rect bounds = null;
2250
2251 // We give preference to the launch preference in activity options.
2252 if (options != null) {
2253 taskId = options.getLaunchTaskId();
2254 displayId = options.getLaunchDisplayId();
2255 // TODO: Need to work this into the equation...
2256 //bounds = options.getLaunchBounds();
2257 }
2258
2259 // First preference for stack goes to the task Id set in the activity options. Use the stack
2260 // associated with that if possible.
2261 if (taskId != INVALID_TASK_ID) {
2262 // Temporarily set the task id to invalid in case in re-entry.
2263 options.setLaunchTaskId(INVALID_TASK_ID);
2264 final TaskRecord task = anyTaskForIdLocked(taskId,
2265 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE, options);
2266 options.setLaunchTaskId(taskId);
2267 if (task != null) {
2268 return task.getStack().mStackId;
2269 }
2270 }
2271
2272 final int windowingMode = resolveWindowingMode(r, options, candidateTask);
2273 final int activityType = resolveActivityType(r, options, candidateTask);
2274 ActivityStack stack = null;
2275
2276 // Next preference for stack goes to the display Id set in the activity options or the
2277 // candidate display.
2278 if (displayId == INVALID_DISPLAY) {
2279 displayId = candidateDisplayId;
2280 }
2281 if (displayId != INVALID_DISPLAY) {
2282 if (r != null) {
2283 // TODO: This should also take in the windowing mode and activity type into account.
2284 stack = getValidLaunchStackOnDisplay(displayId, r);
2285 if (stack != null) {
2286 return stack.mStackId;
2287 }
2288 }
2289 final ActivityDisplay display = getActivityDisplayOrCreateLocked(displayId);
2290 if (display != null) {
2291 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
2292 stack = display.mStacks.get(i);
2293 if (stack.getWindowingMode() == windowingMode
2294 && stack.getActivityType() == activityType) {
2295 return stack.mStackId;
2296 }
2297 }
2298 // TODO: We should create the stack we want on the display at this point.
2299 }
2300 }
2301
2302 // Give preference to the stack and display of the input task and activity if they match the
2303 // mode we want to launch into.
2304 if (candidateTask != null) {
2305 stack = candidateTask.getStack();
2306 }
2307 if (stack == null && r != null) {
2308 stack = r.getStack();
2309 }
2310 if (stack != null) {
2311 if (stack.getWindowingMode() == windowingMode
2312 && stack.getActivityType() == activityType) {
2313 return stack.mStackId;
2314 }
2315 ActivityDisplay display = stack.getDisplay();
2316
2317 if (display != null) {
2318 for (int i = display.mStacks.size() - 1; i >= 0; --i) {
2319 stack = display.mStacks.get(i);
2320 if (stack.getWindowingMode() == windowingMode
2321 && stack.getActivityType() == activityType) {
2322 return stack.mStackId;
2323 }
2324 }
2325 }
2326 }
2327
2328 // Give preference to the type of activity we are trying to launch followed by the windowing
2329 // mode.
2330 int stackId = getStackIdForActivityType(activityType);
2331 if (stackId != INVALID_STACK_ID) {
2332 return stackId;
2333 }
2334 stackId = getStackIdForWindowingMode(windowingMode);
2335 if (stackId != INVALID_STACK_ID) {
2336 return stackId;
2337 }
2338
2339 // Whatever...return some default for now.
2340 if (candidateTask != null && candidateTask.mBounds != null
2341 && mService.mSupportsFreeformWindowManagement) {
2342 return FREEFORM_WORKSPACE_STACK_ID;
2343 }
2344 return FULLSCREEN_WORKSPACE_STACK_ID;
2345 }
2346
Andrii Kulian16802aa2016-11-02 12:21:33 -07002347 /**
2348 * Get a topmost stack on the display, that is a valid launch stack for specified activity.
2349 * If there is no such stack, new dynamic stack can be created.
2350 * @param displayId Target display.
2351 * @param r Activity that should be launched there.
2352 * @return Existing stack if there is a valid one, new dynamic stack if it is valid or null.
2353 */
2354 ActivityStack getValidLaunchStackOnDisplay(int displayId, @NonNull ActivityRecord r) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002355 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian16802aa2016-11-02 12:21:33 -07002356 if (activityDisplay == null) {
2357 throw new IllegalArgumentException(
2358 "Display with displayId=" + displayId + " not found.");
2359 }
2360
2361 // Return the topmost valid stack on the display.
2362 for (int i = activityDisplay.mStacks.size() - 1; i >= 0; --i) {
2363 final ActivityStack stack = activityDisplay.mStacks.get(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002364 if (isValidLaunchStackId(stack.mStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002365 return stack;
2366 }
2367 }
2368
2369 // If there is no valid stack on the external display - check if new dynamic stack will do.
2370 if (displayId != Display.DEFAULT_DISPLAY) {
2371 final int newDynamicStackId = getNextStackId();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002372 if (isValidLaunchStackId(newDynamicStackId, displayId, r)) {
Andrii Kulian16802aa2016-11-02 12:21:33 -07002373 return createStackOnDisplay(newDynamicStackId, displayId, true /*onTop*/);
2374 }
2375 }
2376
2377 Slog.w(TAG, "getValidLaunchStackOnDisplay: can't launch on displayId " + displayId);
2378 return null;
2379 }
2380
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002381 boolean isValidLaunchStackId(int stackId, int displayId, ActivityRecord r) {
2382 switch (stackId) {
2383 case INVALID_STACK_ID:
2384 case HOME_STACK_ID:
2385 return false;
2386 case FULLSCREEN_WORKSPACE_STACK_ID:
2387 return true;
2388 case FREEFORM_WORKSPACE_STACK_ID:
2389 return r.supportsFreeform();
2390 case DOCKED_STACK_ID:
2391 return r.supportsSplitScreen();
2392 case PINNED_STACK_ID:
2393 return r.supportsPictureInPicture();
2394 case RECENTS_STACK_ID:
2395 return r.isActivityTypeRecents();
2396 case ASSISTANT_STACK_ID:
2397 return r.isActivityTypeAssistant();
2398 default:
2399 if (StackId.isDynamicStack(stackId)) {
2400 return r.canBeLaunchedOnDisplay(displayId);
2401 }
2402 Slog.e(TAG, "isValidLaunchStackId: Unexpected stackId=" + stackId);
2403 return false;
2404 }
2405 }
2406
Craig Mautner967212c2013-04-13 21:10:58 -07002407 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002408 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002409 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2410 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002411 }
2412 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002413 }
2414
Jorim Jaggife762342016-10-13 14:33:27 +02002415 ArrayList<ActivityStack> getStacksOnDefaultDisplay() {
2416 return mActivityDisplays.valueAt(DEFAULT_DISPLAY).mStacks;
2417 }
2418
Andrii Kulian7fc22812016-12-28 13:04:11 -08002419 /**
2420 * Get next focusable stack in the system. This will search across displays and stacks
2421 * in last-focused order for a focusable and visible stack, different from the target stack.
2422 *
2423 * @param currentFocus The stack that previously had focus and thus needs to be ignored when
2424 * searching for next candidate.
2425 * @return Next focusable {@link ActivityStack}, null if not found.
2426 */
2427 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
2428 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2429
2430 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2431 final int displayId = mTmpOrderedDisplayIds.get(i);
Andrii Kulian62e6f252017-05-30 22:46:53 -07002432 // If a display is registered in WM, it must also be available in AM.
2433 @SuppressWarnings("ConstantConditions")
2434 final List<ActivityStack> stacks = getActivityDisplayOrCreateLocked(displayId).mStacks;
Andrii Kulian7fc22812016-12-28 13:04:11 -08002435 for (int j = stacks.size() - 1; j >= 0; --j) {
2436 final ActivityStack stack = stacks.get(j);
2437 if (stack != currentFocus && stack.isFocusable()
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002438 && stack.shouldBeVisible(null) != STACK_INVISIBLE) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08002439 return stack;
2440 }
2441 }
2442 }
2443
2444 return null;
2445 }
2446
Andrii Kuliana8fe3df2017-06-16 15:29:26 -07002447 /**
2448 * Get next valid stack for launching provided activity in the system. This will search across
2449 * displays and stacks in last-focused order for a focusable and visible stack, except those
2450 * that are on a currently focused display.
2451 *
2452 * @param r The activity that is being launched.
2453 * @param currentFocus The display that previously had focus and thus needs to be ignored when
2454 * searching for the next candidate.
2455 * @return Next valid {@link ActivityStack}, null if not found.
2456 */
2457 ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
2458 mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
2459 for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
2460 final int displayId = mTmpOrderedDisplayIds.get(i);
2461 if (displayId == currentFocus) {
2462 continue;
2463 }
2464 final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
2465 if (stack != null) {
2466 return stack;
2467 }
2468 }
2469 return null;
2470 }
2471
Craig Mautneree2e45a2014-06-27 12:10:03 -07002472 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002473 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002474 }
2475
2476 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002477 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2478 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2479 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002480 if (task.isActivityTypeHome()) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002481 final ArrayList<ActivityRecord> activities = task.mActivities;
2482 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2483 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002484 if (r.isActivityTypeHome()
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002485 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002486 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002487 }
2488 }
2489 }
2490 }
2491 return null;
2492 }
2493
Jorim Jaggidc249c42015-12-15 14:57:31 -08002494 void resizeStackLocked(int stackId, Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002495 boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
Jorim Jaggidc249c42015-12-15 14:57:31 -08002496 if (stackId == DOCKED_STACK_ID) {
2497 resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002498 preserveWindows, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002499 return;
2500 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08002501 final ActivityStack stack = getStack(stackId);
2502 if (stack == null) {
2503 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2504 return;
2505 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002506
Wale Ogunwale926aade2017-08-29 11:24:37 -07002507 final boolean splitScreenActive = getStack(DOCKED_STACK_ID) != null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002508 if (!allowResizeInDockedMode
Wale Ogunwale926aade2017-08-29 11:24:37 -07002509 && !stack.getWindowConfiguration().tasksAreFloating() && splitScreenActive) {
Winson Chunga2249ac2017-03-30 18:15:30 -07002510 // If the docked stack exists, don't resize non-floating stacks independently of the
2511 // size computed from the docked stack size (otherwise they will be out of sync)
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002512 return;
2513 }
2514
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002515 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002516 mWindowManager.deferSurfaceLayout();
2517 try {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002518 if (stack.supportSplitScreenWindowingMode()) {
2519 if (bounds == null && stack.inSplitScreenWindowingMode()) {
2520 // null bounds = fullscreen windowing mode...at least for now.
2521 stack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
2522 } else if (splitScreenActive) {
2523 // If we are in split-screen mode and this stack support split-screen, then
2524 // it should be split-screen secondary mode. i.e. adjacent to the docked stack.
2525 stack.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2526 }
2527 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002528 stack.resize(bounds, tempTaskBounds, tempTaskInsetBounds);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002529 if (!deferResume) {
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002530 stack.ensureVisibleActivitiesConfigurationLocked(
2531 stack.topRunningActivityLocked(), preserveWindows);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002532 }
Jorim Jaggic4025202015-10-22 16:43:34 +02002533 } finally {
2534 mWindowManager.continueSurfaceLayout();
2535 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002536 }
2537 }
2538
Wale Ogunwale926aade2017-08-29 11:24:37 -07002539 private void deferUpdateBounds(int stackId) {
Jorim Jaggi192086e2016-03-11 17:17:03 +01002540 final ActivityStack stack = getStack(stackId);
2541 if (stack != null) {
2542 stack.deferUpdateBounds();
2543 }
2544 }
2545
Wale Ogunwale926aade2017-08-29 11:24:37 -07002546 private void continueUpdateBounds(int stackId) {
Jorim Jaggi192086e2016-03-11 17:17:03 +01002547 final ActivityStack stack = getStack(stackId);
2548 if (stack != null) {
2549 stack.continueUpdateBounds();
2550 }
2551 }
2552
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002553 void notifyAppTransitionDone() {
Matthew Ng606dd802017-06-05 14:06:32 -07002554 continueUpdateBounds(RECENTS_STACK_ID);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002555 for (int i = mResizingTasksDuringAnimation.size() - 1; i >= 0; i--) {
2556 final int taskId = mResizingTasksDuringAnimation.valueAt(i);
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002557 final TaskRecord task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_ONLY);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002558 if (task != null) {
2559 task.setTaskDockedResizing(false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01002560 }
2561 }
2562 mResizingTasksDuringAnimation.clear();
2563 }
2564
Robert Carre7cc44d2017-03-20 19:04:30 -07002565 private void moveTasksToFullscreenStackInSurfaceTransaction(int fromStackId,
2566 boolean onTop) {
Robert Carr6914f082017-03-20 19:04:30 -07002567
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002568 final ActivityStack stack = getStack(fromStackId);
2569 if (stack == null) {
2570 return;
2571 }
2572
2573 mWindowManager.deferSurfaceLayout();
2574 try {
2575 if (fromStackId == DOCKED_STACK_ID) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002576 // We are moving all tasks from the docked stack to the fullscreen stack,
2577 // which is dismissing the docked stack, so resize all other stacks to
2578 // fullscreen here already so we don't end up with resize trashing.
2579 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002580 final ActivityStack otherStack = getStack(i);
2581 if (otherStack == null) {
2582 continue;
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002583 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002584 if (!otherStack.inSplitScreenSecondaryWindowingMode()) {
2585 continue;
2586 }
2587 resizeStackLocked(i, null, null, null, PRESERVE_WINDOWS,
2588 true /* allowResizeInDockedMode */, DEFER_RESUME);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002589 }
2590
2591 // Also disable docked stack resizing since we have manually adjusted the
2592 // size of other stacks above and we don't want to trigger a docked stack
2593 // resize when we remove task from it below and it is detached from the
2594 // display because it no longer contains any tasks.
2595 mAllowDockedStackResize = false;
Winson Chung47900652017-04-06 18:44:25 -07002596 } else if (fromStackId == PINNED_STACK_ID) {
2597 if (onTop) {
2598 // Log if we are expanding the PiP to fullscreen
2599 MetricsLogger.action(mService.mContext,
2600 ACTION_PICTURE_IN_PICTURE_EXPANDED_TO_FULLSCREEN);
2601 }
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002602 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002603 ActivityStack fullscreenStack = getStack(FULLSCREEN_WORKSPACE_STACK_ID);
Winson Chungc85d9ce2017-01-03 11:59:52 -08002604 final boolean isFullscreenStackVisible = fullscreenStack != null &&
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07002605 fullscreenStack.shouldBeVisible(null) == STACK_VISIBLE;
Winson Chung5af42fc2017-03-24 17:11:33 -07002606 // If we are moving from the pinned stack, then the animation takes care of updating
2607 // the picture-in-picture mode.
Winson Chung47900652017-04-06 18:44:25 -07002608 final boolean schedulePictureInPictureModeChange = (fromStackId == PINNED_STACK_ID);
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002609 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2610 final int size = tasks.size();
2611 if (onTop) {
2612 for (int i = 0; i < size; i++) {
Winson Chung0c1ca092016-11-10 17:40:34 -08002613 final TaskRecord task = tasks.get(i);
Winson Chung74666102017-02-22 17:49:24 -08002614 final boolean isTopTask = i == (size - 1);
Winson Chung0c1ca092016-11-10 17:40:34 -08002615 if (fromStackId == PINNED_STACK_ID) {
2616 // Update the return-to to reflect where the pinned stack task was moved
2617 // from so that we retain the stack that was previously visible if the
2618 // pinned stack is recreated. See moveActivityToPinnedStackLocked().
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002619 task.setTaskToReturnTo(isFullscreenStackVisible ?
2620 ACTIVITY_TYPE_STANDARD : ACTIVITY_TYPE_HOME);
Winson Chung0c1ca092016-11-10 17:40:34 -08002621 }
Winson Chung74666102017-02-22 17:49:24 -08002622 // Defer resume until all the tasks have been moved to the fullscreen stack
2623 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP,
2624 REPARENT_MOVE_STACK_TO_FRONT, isTopTask /* animate */, DEFER_RESUME,
Winson Chung5af42fc2017-03-24 17:11:33 -07002625 schedulePictureInPictureModeChange,
Winson Chung74666102017-02-22 17:49:24 -08002626 "moveTasksToFullscreenStack - onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002627 }
2628 } else {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002629 for (int i = 0; i < size; i++) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08002630 final TaskRecord task = tasks.get(i);
Winson Chung3a649982017-04-19 10:16:17 -07002631 // Position the tasks in the fullscreen stack in order at the bottom of the
2632 // stack. Also defer resume until all the tasks have been moved to the
2633 // fullscreen stack.
2634 task.reparent(FULLSCREEN_WORKSPACE_STACK_ID, i /* position */,
Winson Chung5af42fc2017-03-24 17:11:33 -07002635 REPARENT_LEAVE_STACK_IN_PLACE, !ANIMATE, DEFER_RESUME,
2636 schedulePictureInPictureModeChange,
2637 "moveTasksToFullscreenStack - NOT_onTop");
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002638 }
2639 }
Winson Chung74666102017-02-22 17:49:24 -08002640
2641 ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2642 resumeFocusedStackTopActivityLocked();
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002643 } finally {
2644 mAllowDockedStackResize = true;
2645 mWindowManager.continueSurfaceLayout();
2646 }
2647 }
2648
Robert Carr6914f082017-03-20 19:04:30 -07002649 void moveTasksToFullscreenStackLocked(int fromStackId, boolean onTop) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002650 mWindowManager.inSurfaceTransaction(
2651 () -> moveTasksToFullscreenStackInSurfaceTransaction(fromStackId, onTop));
Robert Carr6914f082017-03-20 19:04:30 -07002652 }
2653
Jorim Jaggidc249c42015-12-15 14:57:31 -08002654 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002655 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2656 boolean preserveWindows) {
2657 resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
2658 tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
2659 false /* deferResume */);
2660 }
2661
2662 void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
2663 Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
2664 boolean preserveWindows, boolean deferResume) {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002665
2666 if (!mAllowDockedStackResize) {
2667 // Docked stack resize currently disabled.
2668 return;
2669 }
2670
Jorim Jaggidc249c42015-12-15 14:57:31 -08002671 final ActivityStack stack = getStack(DOCKED_STACK_ID);
2672 if (stack == null) {
2673 Slog.w(TAG, "resizeDockedStackLocked: docked stack not found");
2674 return;
2675 }
2676
2677 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeDockedStack");
2678 mWindowManager.deferSurfaceLayout();
2679 try {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002680 // Don't allow re-entry while resizing. E.g. due to docked stack detaching.
2681 mAllowDockedStackResize = false;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002682 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002683 stack.resize(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002684
Andrii Kulian69fb5e42016-04-05 16:47:29 -07002685 // TODO: Checking for isAttached might not be needed as if the user passes in null
2686 // dockedBounds then they want the docked stack to be dismissed.
2687 if (stack.mFullscreen || (dockedBounds == null && !stack.isAttached())) {
2688 // The dock stack either was dismissed or went fullscreen, which is kinda the same.
Jorim Jaggidc249c42015-12-15 14:57:31 -08002689 // In this case we make all other static stacks fullscreen and move all
2690 // docked stack tasks to the fullscreen stack.
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002691 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, ON_TOP);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002692
2693 // stack shouldn't contain anymore activities, so nothing to resume.
2694 r = null;
2695 } else {
2696 // Docked stacks occupy a dedicated region on screen so the size of all other
2697 // static stacks need to be adjusted so they don't overlap with the docked stack.
2698 // We get the bounds to use from window manager which has been adjusted for any
2699 // screen controls and is also the same for all stacks.
Matthew Ngaa2b6202017-02-10 14:48:21 -08002700 final Rect otherTaskRect = new Rect();
Jorim Jaggidc249c42015-12-15 14:57:31 -08002701 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale926aade2017-08-29 11:24:37 -07002702 if (i == DOCKED_STACK_ID) {
2703 continue;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002704 }
Wale Ogunwale926aade2017-08-29 11:24:37 -07002705 final ActivityStack current = getStack(i);
2706 if (current == null || !current.supportSplitScreenWindowingMode()) {
2707 continue;
2708 }
2709 // Need to set windowing mode here before we try to get the dock bounds.
2710 current.setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
2711 current.getStackDockedModeBounds(
2712 tempOtherTaskBounds /* currentTempTaskBounds */,
2713 tempRect /* outStackBounds */,
2714 otherTaskRect /* outTempTaskBounds */, true /* ignoreVisibility */);
2715
2716 resizeStackLocked(i, !tempRect.isEmpty() ? tempRect : null,
2717 !otherTaskRect.isEmpty() ? otherTaskRect : tempOtherTaskBounds,
2718 tempOtherTaskInsetBounds, preserveWindows,
2719 true /* allowResizeInDockedMode */, deferResume);
Jorim Jaggidc249c42015-12-15 14:57:31 -08002720 }
2721 }
Andrii Kuliandb3e4ec2016-07-12 17:11:35 -07002722 if (!deferResume) {
2723 stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
2724 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002725 } finally {
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07002726 mAllowDockedStackResize = true;
Jorim Jaggidc249c42015-12-15 14:57:31 -08002727 mWindowManager.continueSurfaceLayout();
2728 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2729 }
Jorim Jaggidc249c42015-12-15 14:57:31 -08002730 }
2731
Robert Carr0d00c2e2016-02-29 17:45:02 -08002732 void resizePinnedStackLocked(Rect pinnedBounds, Rect tempPinnedTaskBounds) {
Winson Chung19953ca2017-04-11 11:19:23 -07002733 final PinnedActivityStack stack = getStack(PINNED_STACK_ID);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002734 if (stack == null) {
2735 Slog.w(TAG, "resizePinnedStackLocked: pinned stack not found");
2736 return;
2737 }
Winson Chung19953ca2017-04-11 11:19:23 -07002738
2739 // It is possible for the bounds animation from the WM to call this but be delayed by
2740 // another AM call that is holding the AMS lock. In such a case, the pinnedBounds may be
2741 // incorrect if AMS.resizeStackWithBoundsFromWindowManager() is already called while waiting
2742 // for the AMS lock to be freed. So check and make sure these bounds are still good.
2743 final PinnedStackWindowController stackController = stack.getWindowContainerController();
Winson Chung8bca9e42017-04-16 15:59:43 -07002744 if (stackController.pinnedStackResizeDisallowed()) {
Winson Chung19953ca2017-04-11 11:19:23 -07002745 return;
2746 }
2747
Robert Carr0d00c2e2016-02-29 17:45:02 -08002748 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizePinnedStack");
2749 mWindowManager.deferSurfaceLayout();
2750 try {
2751 ActivityRecord r = stack.topRunningActivityLocked();
Robert Carr7e4c90e2017-02-15 19:52:38 -08002752 Rect insetBounds = null;
2753 if (tempPinnedTaskBounds != null) {
2754 // We always use 0,0 as the position for the inset rect because
2755 // if we are getting insets at all in the pinned stack it must mean
2756 // we are headed for fullscreen.
2757 insetBounds = tempRect;
2758 insetBounds.top = 0;
2759 insetBounds.left = 0;
2760 insetBounds.right = tempPinnedTaskBounds.width();
2761 insetBounds.bottom = tempPinnedTaskBounds.height();
2762 }
2763 stack.resize(pinnedBounds, tempPinnedTaskBounds, insetBounds);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07002764 stack.ensureVisibleActivitiesConfigurationLocked(r, false);
Robert Carr0d00c2e2016-02-29 17:45:02 -08002765 } finally {
2766 mWindowManager.continueSurfaceLayout();
2767 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
2768 }
2769 }
2770
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002771 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002772 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002774 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002775 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002776 return createStack(stackId, activityDisplay, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002777
Craig Mautner4a1cb222013-12-04 16:14:06 -08002778 }
2779
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002780 ActivityStack createStack(int stackId, ActivityDisplay display, boolean onTop) {
2781 switch (stackId) {
2782 case PINNED_STACK_ID:
2783 return new PinnedActivityStack(display, stackId, this, mRecentTasks, onTop);
2784 default:
2785 return new ActivityStack(display, stackId, this, mRecentTasks, onTop);
2786 }
2787 }
Robert Carr6914f082017-03-20 19:04:30 -07002788
Robert Carre7cc44d2017-03-20 19:04:30 -07002789 void removeStackInSurfaceTransaction(int stackId) {
Winson Chung010927a2016-12-15 16:12:35 -08002790 final ActivityStack stack = getStack(stackId);
2791 if (stack == null) {
2792 return;
2793 }
2794
2795 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
2796 if (stack.getStackId() == PINNED_STACK_ID) {
Winson Chung47900652017-04-06 18:44:25 -07002797 /**
2798 * Workaround: Force-stop all the activities in the pinned stack before we reparent them
2799 * to the fullscreen stack. This is to guarantee that when we are removing a stack,
2800 * that the client receives onStop() before it is reparented. We do this by detaching
2801 * the stack from the display so that it will be considered invisible when
2802 * ensureActivitiesVisibleLocked() is called, and all of its activitys will be marked
2803 * invisible as well and added to the stopping list. After which we process the
2804 * stopping list by handling the idle.
2805 */
2806 final PinnedActivityStack pinnedStack = (PinnedActivityStack) stack;
2807 pinnedStack.mForceHidden = true;
2808 pinnedStack.ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
2809 pinnedStack.mForceHidden = false;
2810 activityIdleInternalLocked(null, false /* fromTimeout */,
2811 true /* processPausingActivites */, null /* configuration */);
2812
2813 // Move all the tasks to the bottom of the fullscreen stack
2814 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
Winson Chung010927a2016-12-15 16:12:35 -08002815 } else {
2816 for (int i = tasks.size() - 1; i >= 0; i--) {
2817 removeTaskByIdLocked(tasks.get(i).taskId, true /* killProcess */,
2818 REMOVE_FROM_RECENTS);
2819 }
2820 }
2821 }
2822
2823 /**
Robert Carr6914f082017-03-20 19:04:30 -07002824 * Removes the stack associated with the given {@param stackId}. If the {@param stackId} is the
2825 * pinned stack, then its tasks are not explicitly removed when the stack is destroyed, but
2826 * instead moved back onto the fullscreen stack.
2827 */
2828 void removeStackLocked(int stackId) {
Robert Carre7cc44d2017-03-20 19:04:30 -07002829 mWindowManager.inSurfaceTransaction(
2830 () -> removeStackInSurfaceTransaction(stackId));
Robert Carr6914f082017-03-20 19:04:30 -07002831 }
2832
2833 /**
Winson Chung6954fc92017-03-24 16:22:12 -07002834 * See {@link #removeTaskByIdLocked(int, boolean, boolean, boolean)}
2835 */
2836 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents) {
2837 return removeTaskByIdLocked(taskId, killProcess, removeFromRecents, !PAUSE_IMMEDIATELY);
2838 }
2839
2840 /**
Winson Chung010927a2016-12-15 16:12:35 -08002841 * Removes the task with the specified task id.
2842 *
2843 * @param taskId Identifier of the task to be removed.
2844 * @param killProcess Kill any process associated with the task if possible.
2845 * @param removeFromRecents Whether to also remove the task from recents.
Winson Chung6954fc92017-03-24 16:22:12 -07002846 * @param pauseImmediately Pauses all task activities immediately without waiting for the
2847 * pause-complete callback from the activity.
Winson Chung010927a2016-12-15 16:12:35 -08002848 * @return Returns true if the given task was found and removed.
2849 */
Winson Chung6954fc92017-03-24 16:22:12 -07002850 boolean removeTaskByIdLocked(int taskId, boolean killProcess, boolean removeFromRecents,
2851 boolean pauseImmediately) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002852 final TaskRecord tr = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Winson Chung010927a2016-12-15 16:12:35 -08002853 if (tr != null) {
Winson Chung6954fc92017-03-24 16:22:12 -07002854 tr.removeTaskActivitiesLocked(pauseImmediately);
Winson Chung010927a2016-12-15 16:12:35 -08002855 cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents);
2856 if (tr.isPersistable) {
2857 mService.notifyTaskPersisterLocked(null, true);
2858 }
2859 return true;
2860 }
2861 Slog.w(TAG, "Request to remove task ignored for non-existent task " + taskId);
2862 return false;
2863 }
2864
2865 void cleanUpRemovedTaskLocked(TaskRecord tr, boolean killProcess, boolean removeFromRecents) {
2866 if (removeFromRecents) {
2867 mRecentTasks.remove(tr);
2868 tr.removedFromRecents();
2869 }
2870 ComponentName component = tr.getBaseIntent().getComponent();
2871 if (component == null) {
2872 Slog.w(TAG, "No component for base intent of task: " + tr);
2873 return;
2874 }
2875
2876 // Find any running services associated with this app and stop if needed.
2877 mService.mServices.cleanUpRemovedTaskLocked(tr, component, new Intent(tr.getBaseIntent()));
2878
2879 if (!killProcess) {
2880 return;
2881 }
2882
2883 // Determine if the process(es) for this task should be killed.
2884 final String pkg = component.getPackageName();
2885 ArrayList<ProcessRecord> procsToKill = new ArrayList<>();
2886 ArrayMap<String, SparseArray<ProcessRecord>> pmap = mService.mProcessNames.getMap();
2887 for (int i = 0; i < pmap.size(); i++) {
2888
2889 SparseArray<ProcessRecord> uids = pmap.valueAt(i);
2890 for (int j = 0; j < uids.size(); j++) {
2891 ProcessRecord proc = uids.valueAt(j);
2892 if (proc.userId != tr.userId) {
2893 // Don't kill process for a different user.
2894 continue;
2895 }
2896 if (proc == mService.mHomeProcess) {
2897 // Don't kill the home process along with tasks from the same package.
2898 continue;
2899 }
2900 if (!proc.pkgList.containsKey(pkg)) {
2901 // Don't kill process that is not associated with this task.
2902 continue;
2903 }
2904
2905 for (int k = 0; k < proc.activities.size(); k++) {
Bryce Leeaf691c02017-03-20 14:20:22 -07002906 TaskRecord otherTask = proc.activities.get(k).getTask();
Winson Chung010927a2016-12-15 16:12:35 -08002907 if (tr.taskId != otherTask.taskId && otherTask.inRecents) {
2908 // Don't kill process(es) that has an activity in a different task that is
2909 // also in recents.
2910 return;
2911 }
2912 }
2913
2914 if (proc.foregroundServices) {
2915 // Don't kill process(es) with foreground service.
2916 return;
2917 }
2918
2919 // Add process to kill list.
2920 procsToKill.add(proc);
2921 }
2922 }
2923
2924 // Kill the running processes.
2925 for (int i = 0; i < procsToKill.size(); i++) {
2926 ProcessRecord pr = procsToKill.get(i);
2927 if (pr.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND
2928 && pr.curReceivers.isEmpty()) {
2929 pr.kill("remove task", true);
2930 } else {
2931 // We delay killing processes that are not in the background or running a receiver.
2932 pr.waitingToKill = "remove task";
2933 }
2934 }
2935 }
2936
Craig Mautner4a1cb222013-12-04 16:14:06 -08002937 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002938 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002939 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2940 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002941 break;
2942 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002943 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002944 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002945 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002946 }
2947
Chong Zhang5dcb2752015-08-18 13:50:26 -07002948 /**
2949 * Restores a recent task to a stack
2950 * @param task The recent task to be restored.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002951 * @param aOptions The activity options to use for restoration.
Chong Zhang5dcb2752015-08-18 13:50:26 -07002952 * @return true if the task has been restored successfully.
2953 */
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002954 boolean restoreRecentTaskLocked(TaskRecord task, ActivityOptions aOptions) {
2955 final int stackId = getLaunchStackId(null, aOptions, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002956 final ActivityStack currentStack = task.getStack();
2957 if (currentStack != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002958 // Task has already been restored once. See if we need to do anything more
Andrii Kulian02b7a832016-10-06 23:11:56 -07002959 if (currentStack.mStackId == stackId) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002960 // Nothing else to do since it is already restored in the right stack.
2961 return true;
2962 }
2963 // Remove current stack association, so we can re-associate the task with the
2964 // right stack below.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002965 currentStack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002966 }
2967
Wale Ogunwale0568aed2017-09-08 13:29:37 -07002968 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002969
Wale Ogunwale72919d22016-12-08 18:58:50 -08002970 stack.addTask(task, false /* toTop */, "restoreRecentTask");
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08002971 // TODO: move call for creation here and other place into Stack.addTask()
2972 task.createWindowContainer(false /* toTop */, true /* showForAllUsers */);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002973 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
2974 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002975 final ArrayList<ActivityRecord> activities = task.mActivities;
2976 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002977 activities.get(activityNdx).createWindowContainer();
Wale Ogunwale7de05352014-12-12 15:21:33 -08002978 }
2979 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07002980 }
2981
Wale Ogunwale040b4702015-08-06 18:10:50 -07002982 /**
Andrii Kulian839def92016-11-02 10:58:58 -07002983 * Move stack with all its existing content to specified display.
2984 * @param stackId Id of stack to move.
2985 * @param displayId Id of display to move stack to.
Andrii Kulian250d6532017-02-08 23:30:45 -08002986 * @param onTop Indicates whether container should be place on top or on bottom.
Andrii Kulian839def92016-11-02 10:58:58 -07002987 */
Andrii Kulian250d6532017-02-08 23:30:45 -08002988 void moveStackToDisplayLocked(int stackId, int displayId, boolean onTop) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07002989 final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(displayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002990 if (activityDisplay == null) {
2991 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown displayId="
2992 + displayId);
2993 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002994 final ActivityStack stack = mStacks.get(stackId);
2995 if (stack == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002996 throw new IllegalArgumentException("moveStackToDisplayLocked: Unknown stackId="
2997 + stackId);
2998 }
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002999
3000 final ActivityDisplay currentDisplay = stack.getDisplay();
3001 if (currentDisplay == null) {
3002 throw new IllegalStateException("moveStackToDisplayLocked: Stack with stack=" + stack
3003 + " is not attached to any display.");
3004 }
3005
3006 if (currentDisplay.mDisplayId == displayId) {
3007 throw new IllegalArgumentException("Trying to move stack=" + stack
3008 + " to its current displayId=" + displayId);
3009 }
3010
3011 stack.reparent(activityDisplay, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07003012 // TODO(multi-display): resize stacks properly if moved from split-screen.
3013 }
3014
3015 /**
Winson Chung74666102017-02-22 17:49:24 -08003016 * Returns the reparent target stack, creating the stack if necessary. This call also enforces
3017 * the various checks on tasks that are going to be reparented from one stack to another.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003018 */
Winson Chung74666102017-02-22 17:49:24 -08003019 ActivityStack getReparentTargetStack(TaskRecord task, int stackId, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07003020 final ActivityStack prevStack = task.getStack();
Chong Zhang02898352015-08-21 17:27:14 -07003021
Winson Chung74666102017-02-22 17:49:24 -08003022 // Check that we aren't reparenting to the same stack that the task is already in
3023 if (prevStack != null && prevStack.mStackId == stackId) {
3024 Slog.w(TAG, "Can not reparent to same stack, task=" + task
3025 + " already in stackId=" + stackId);
3026 return prevStack;
Wale Ogunwale513346d2016-01-27 10:55:01 -08003027 }
3028
Winson Chung74666102017-02-22 17:49:24 -08003029 // Ensure that we aren't trying to move into a multi-window stack without multi-window
3030 // support
3031 if (StackId.isMultiWindowStack(stackId) && !mService.mSupportsMultiWindow) {
3032 throw new IllegalArgumentException("Device doesn't support multi-window, can not"
3033 + " reparent task=" + task + " to stackId=" + stackId);
Winson Chungc2baac02017-01-11 13:34:47 -08003034 }
3035
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003036 // Ensure that we're not moving a task to a dynamic stack if device doesn't support
3037 // multi-display.
3038 // TODO(multi-display): Support non-dynamic stacks on secondary displays.
Andrii Kulianeafd9db2017-04-05 22:01:35 -07003039 if (StackId.isDynamicStack(stackId) && !mService.mSupportsMultiDisplay) {
3040 throw new IllegalArgumentException("Device doesn't support multi-display, can not"
3041 + " reparent task=" + task + " to stackId=" + stackId);
3042 }
3043
Winson Chung74666102017-02-22 17:49:24 -08003044 // Ensure that we aren't trying to move into a freeform stack without freeform
3045 // support
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003046 if (stackId == FREEFORM_WORKSPACE_STACK_ID && !mService.mSupportsFreeformWindowManagement) {
Winson Chung74666102017-02-22 17:49:24 -08003047 throw new IllegalArgumentException("Device doesn't support freeform, can not reparent"
3048 + " task=" + task);
Wale Ogunwale08559dc2016-02-23 12:20:08 -08003049 }
3050
Winson Chung74666102017-02-22 17:49:24 -08003051 // We don't allow moving a unresizeable task to the docked stack since the docked stack is
3052 // used for split-screen mode and will cause things like the docked divider to show up. We
3053 // instead leave the task in its current stack or move it to the fullscreen stack if it
3054 // isn't currently in a stack.
3055 if (stackId == DOCKED_STACK_ID && !task.isResizeable()) {
3056 stackId = (prevStack != null) ? prevStack.mStackId : FULLSCREEN_WORKSPACE_STACK_ID;
3057 Slog.w(TAG, "Can not move unresizeable task=" + task + " to docked stack."
3058 + " Moving to stackId=" + stackId + " instead.");
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003059 }
Wale Ogunwale961f4852016-02-01 20:25:54 -08003060
Winson Chung74666102017-02-22 17:49:24 -08003061 // Temporarily disable resizeablility of the task as we don't want it to be resized if, for
3062 // example, a docked stack is created which will lead to the stack we are moving from being
3063 // resized and and its resizeable tasks being resized.
Wale Ogunwale961f4852016-02-01 20:25:54 -08003064 try {
Winson Chung74666102017-02-22 17:49:24 -08003065 task.mTemporarilyUnresizable = true;
3066 return getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale961f4852016-02-01 20:25:54 -08003067 } finally {
Winson Chung74666102017-02-22 17:49:24 -08003068 task.mTemporarilyUnresizable = false;
Chong Zhangf596cd52016-01-05 13:42:44 -08003069 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003070 }
3071
Winson Chung08f81892017-03-02 15:40:51 -08003072 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect destBounds) {
Wale Ogunwale079a0042015-10-24 11:44:07 -07003073 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3074 if (stack == null) {
3075 throw new IllegalArgumentException(
3076 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3077 }
3078
3079 final ActivityRecord r = stack.topRunningActivityLocked();
3080 if (r == null) {
3081 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3082 + " in stack=" + stack);
3083 return false;
3084 }
3085
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003086 if (!mService.mForceResizableActivities && !r.supportsPictureInPicture()) {
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003087 Slog.w(TAG,
3088 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003089 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003090 return false;
3091 }
3092
Winson Chung3a682872017-04-10 14:38:05 -07003093 moveActivityToPinnedStackLocked(r, null /* sourceBounds */, 0f /* aspectRatio */,
Winson Chung08f81892017-03-02 15:40:51 -08003094 true /* moveHomeStackToFront */, "moveTopActivityToPinnedStack");
Wale Ogunwale9c604c72015-12-06 18:42:57 -08003095 return true;
3096 }
3097
Winson Chung8bca9e42017-04-16 15:59:43 -07003098 void moveActivityToPinnedStackLocked(ActivityRecord r, Rect sourceHintBounds, float aspectRatio,
Winson Chung08f81892017-03-02 15:40:51 -08003099 boolean moveHomeStackToFront, String reason) {
3100
Wale Ogunwale480dca02016-02-06 13:58:29 -08003101 mWindowManager.deferSurfaceLayout();
Winson Chung74666102017-02-22 17:49:24 -08003102
Bryce Lee04ab3462017-04-10 15:06:33 -07003103 // This will clear the pinned stack by moving an existing task to the full screen stack,
3104 // ensuring only one task is present.
3105 moveTasksToFullscreenStackLocked(PINNED_STACK_ID, !ON_TOP);
3106
Wale Ogunwale1666e312016-12-16 11:27:18 -08003107 // Need to make sure the pinned stack exist so we can resize it below...
Winson Chung55893332017-02-17 17:13:10 -08003108 final PinnedActivityStack stack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale1666e312016-12-16 11:27:18 -08003109
Wale Ogunwale480dca02016-02-06 13:58:29 -08003110 try {
Bryce Leeaf691c02017-03-20 14:20:22 -07003111 final TaskRecord task = r.getTask();
Wale Ogunwale480dca02016-02-06 13:58:29 -08003112 // Resize the pinned stack to match the current size of the task the activity we are
3113 // going to be moving is currently contained in. We do this to have the right starting
3114 // animation bounds for the pinned stack to the desired bounds the caller wants.
3115 resizeStackLocked(PINNED_STACK_ID, task.mBounds, null /* tempTaskBounds */,
3116 null /* tempTaskInsetBounds */, !PRESERVE_WINDOWS,
Wale Ogunwalec8da41e2016-04-16 13:27:23 -07003117 true /* allowResizeInDockedMode */, !DEFER_RESUME);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003118
3119 if (task.mActivities.size() == 1) {
3120 // There is only one activity in the task. So, we can just move the task over to
Winson Chungc2baac02017-01-11 13:34:47 -08003121 // the stack without re-parenting the activity in a different task. We don't
3122 // move the home stack forward if we are currently entering picture-in-picture
3123 // while pausing because that changes the focused stack and may prevent the new
3124 // starting activity from resuming.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003125 if (moveHomeStackToFront && task.returnsToHomeTask()
Winson Chungf7e03e12017-08-22 11:32:16 -07003126 && (r.state == RESUMED || !r.supportsEnterPipOnTaskSwitch)) {
Wale Ogunwaleda4ba962016-03-11 09:33:17 -08003127 // Move the home stack forward if the task we just moved to the pinned stack
3128 // was launched from home so home should be visible behind it.
3129 moveHomeStackToFront(reason);
3130 }
Winson Chung5af42fc2017-03-24 17:11:33 -07003131 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003132 task.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003133 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003134 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003135 // There are multiple activities in the task and moving the top activity should
Winson Chung74666102017-02-22 17:49:24 -08003136 // reveal/leave the other activities in their original task.
3137
3138 // Currently, we don't support reparenting activities across tasks in two different
3139 // stacks, so instead, just create a new task in the same stack, reparent the
3140 // activity into that task, and then reparent the whole task to the new stack. This
3141 // ensures that all the necessary work to migrate states in the old and new stacks
3142 // is also done.
3143 final TaskRecord newTask = task.getStack().createTaskRecord(
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003144 getNextTaskIdForUserLocked(r.userId), r.info, r.intent, null, null, true);
Winson Chung74666102017-02-22 17:49:24 -08003145 r.reparent(newTask, MAX_VALUE, "moveActivityToStack");
3146
Winson Chung5af42fc2017-03-24 17:11:33 -07003147 // Defer resume until below, and do not schedule PiP changes until we animate below
Winson Chung74666102017-02-22 17:49:24 -08003148 newTask.reparent(PINNED_STACK_ID, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, !ANIMATE,
Winson Chung5af42fc2017-03-24 17:11:33 -07003149 DEFER_RESUME, false /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwale480dca02016-02-06 13:58:29 -08003150 }
Winson Chungc2baac02017-01-11 13:34:47 -08003151
3152 // Reset the state that indicates it can enter PiP while pausing after we've moved it
3153 // to the pinned stack
Winson Chungf7e03e12017-08-22 11:32:16 -07003154 r.supportsEnterPipOnTaskSwitch = false;
Wale Ogunwale480dca02016-02-06 13:58:29 -08003155 } finally {
3156 mWindowManager.continueSurfaceLayout();
Wale Ogunwale079a0042015-10-24 11:44:07 -07003157 }
3158
Winson Chunge7ba6862017-05-24 12:13:33 -07003159 // Calculate the default bounds (don't use existing stack bounds as we may have just created
3160 // the stack, and schedule the start of the animation into PiP (the bounds animator that
3161 // is triggered by this is posted on another thread)
Winson Chunga71febe2017-05-22 11:14:22 -07003162 final Rect destBounds = stack.getDefaultPictureInPictureBounds(aspectRatio);
3163
Winson Chunge7ba6862017-05-24 12:13:33 -07003164 stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
3165 true /* fromFullscreen */);
3166
3167 // Update the visibility of all activities after the they have been reparented to the new
3168 // stack. This MUST run after the animation above is scheduled to ensure that the windows
3169 // drawn signal is scheduled after the bounds animation start call on the bounds animator
3170 // thread.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003171 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled046a012015-12-24 13:05:59 -08003172 resumeFocusedStackTopActivityLocked();
Wale Ogunwale03ce8632015-12-29 16:15:22 -08003173
Winson Chungb5026902017-05-03 12:45:13 -07003174 mService.mTaskChangeNotificationController.notifyActivityPinned(r.packageName,
3175 r.getTask().taskId);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003176 }
3177
Andrii Kulian0864bbb2017-02-16 15:45:58 -08003178 /** Move activity with its stack to front and make the stack focused. */
Chong Zhang6cda19c2016-06-14 19:07:56 -07003179 boolean moveFocusableActivityStackToFrontLocked(ActivityRecord r, String reason) {
3180 if (r == null || !r.isFocusable()) {
3181 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3182 "moveActivityStackToFront: unfocusable r=" + r);
3183 return false;
3184 }
3185
Bryce Leeaf691c02017-03-20 14:20:22 -07003186 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003187 final ActivityStack stack = r.getStack();
3188 if (stack == null) {
Chong Zhang6cda19c2016-06-14 19:07:56 -07003189 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: r="
3190 + r + " task=" + task);
3191 return false;
3192 }
3193
Chong Zhang6cda19c2016-06-14 19:07:56 -07003194 if (stack == mFocusedStack && stack.topRunningActivityLocked() == r) {
3195 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3196 "moveActivityStackToFront: already on top, r=" + r);
3197 return false;
3198 }
3199
3200 if (DEBUG_FOCUS) Slog.d(TAG_FOCUS,
3201 "moveActivityStackToFront: r=" + r);
3202
3203 stack.moveToFront(reason, task);
3204 return true;
3205 }
3206
David Stevensc6b91c62017-02-08 14:23:58 -08003207 ActivityRecord findTaskLocked(ActivityRecord r, int displayId) {
Wale Ogunwale39381972015-12-17 17:15:29 -08003208 mTmpFindTaskResult.r = null;
3209 mTmpFindTaskResult.matchedByRootAffinity = false;
David Stevensc6b91c62017-02-08 14:23:58 -08003210 ActivityRecord affinityMatch = null;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003211 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003212 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3213 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003214 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3215 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003216 if (!r.hasCompatibleActivityType(stack)) {
Winson Chung91e5c882017-04-21 14:44:38 -07003217 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) "
3218 + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003219 continue;
3220 }
Wale Ogunwale39381972015-12-17 17:15:29 -08003221 stack.findTaskLocked(r, mTmpFindTaskResult);
David Stevensc6b91c62017-02-08 14:23:58 -08003222 // It is possible to have tasks in multiple stacks with the same root affinity, so
3223 // we should keep looking after finding an affinity match to see if there is a
3224 // better match in another stack. Also, task affinity isn't a good enough reason
3225 // to target a display which isn't the source of the intent, so skip any affinity
3226 // matches not on the specified display.
3227 if (mTmpFindTaskResult.r != null) {
3228 if (!mTmpFindTaskResult.matchedByRootAffinity) {
3229 return mTmpFindTaskResult.r;
3230 } else if (mTmpFindTaskResult.r.getDisplayId() == displayId) {
Winson Chung5b895b72017-05-01 13:46:25 -07003231 // Note: since the traversing through the stacks is top down, the floating
3232 // tasks should always have lower priority than any affinity-matching tasks
3233 // in the fullscreen stacks
David Stevensc6b91c62017-02-08 14:23:58 -08003234 affinityMatch = mTmpFindTaskResult.r;
David Stevense5a7b642017-05-22 13:18:23 -07003235 } else if (DEBUG_TASKS && mTmpFindTaskResult.matchedByRootAffinity) {
3236 Slog.d(TAG_TASKS, "Skipping match on different display "
3237 + mTmpFindTaskResult.r.getDisplayId() + " " + displayId);
David Stevensc6b91c62017-02-08 14:23:58 -08003238 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003240 }
3241 }
Winson Chung5b895b72017-05-01 13:46:25 -07003242
David Stevensc6b91c62017-02-08 14:23:58 -08003243 if (DEBUG_TASKS && affinityMatch == null) Slog.d(TAG_TASKS, "No task found");
3244 return affinityMatch;
Craig Mautner8849a5e2013-04-02 16:41:03 -07003245 }
3246
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003247 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
3248 boolean compareIntentFilters) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003249 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3250 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003251 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07003252 final ActivityRecord ar = stacks.get(stackNdx)
3253 .findActivityLocked(intent, info, compareIntentFilters);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003254 if (ar != null) {
3255 return ar;
3256 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003257 }
3258 }
3259 return null;
3260 }
3261
David Stevens9440dc82017-03-16 19:00:20 -07003262 boolean hasAwakeDisplay() {
3263 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3264 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3265 if (!display.shouldSleep()) {
3266 return true;
3267 }
3268 }
3269 return false;
3270 }
3271
Craig Mautner8d341ef2013-03-26 09:03:27 -07003272 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003273 scheduleSleepTimeout();
3274 if (!mGoingToSleep.isHeld()) {
3275 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003276 if (mLaunchingActivity.isHeld()) {
3277 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3278 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003279 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003280 mLaunchingActivity.release();
3281 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003282 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003283 }
David Stevens9440dc82017-03-16 19:00:20 -07003284
3285 applySleepTokensLocked(false /* applyToStacks */);
3286
3287 checkReadyForSleepLocked(true /* allowDelay */);
3288 }
3289
3290 void prepareForShutdownLocked() {
3291 for (int i = 0; i < mActivityDisplays.size(); i++) {
3292 createSleepTokenLocked("shutdown", mActivityDisplays.keyAt(i));
3293 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003294 }
3295
3296 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003297 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003298
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003299 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003300 final long endTime = System.currentTimeMillis() + timeout;
3301 while (true) {
David Stevens18abd0e2017-08-17 14:55:47 -07003302 if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003303 long timeRemaining = endTime - System.currentTimeMillis();
3304 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003305 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003306 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003307 } catch (InterruptedException e) {
3308 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003309 } else {
3310 Slog.w(TAG, "Activity manager shutdown timed out");
3311 timedout = true;
3312 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003313 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003314 } else {
3315 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003316 }
3317 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003318
3319 // Force checkReadyForSleep to complete.
David Stevens9440dc82017-03-16 19:00:20 -07003320 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003321
Craig Mautner8d341ef2013-03-26 09:03:27 -07003322 return timedout;
3323 }
3324
3325 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003326 removeSleepTimeouts();
3327 if (mGoingToSleep.isHeld()) {
3328 mGoingToSleep.release();
3329 }
David Stevens9440dc82017-03-16 19:00:20 -07003330 }
3331
3332 void applySleepTokensLocked(boolean applyToStacks) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003333 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
David Stevens9440dc82017-03-16 19:00:20 -07003334 // Set the sleeping state of the display.
3335 final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
3336 final boolean displayShouldSleep = display.shouldSleep();
3337 if (displayShouldSleep == display.isSleeping()) {
3338 continue;
3339 }
3340 display.setIsSleeping(displayShouldSleep);
3341
3342 if (!applyToStacks) {
3343 continue;
3344 }
3345
3346 // Set the sleeping state of the stacks on the display.
3347 final ArrayList<ActivityStack> stacks = display.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003348 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3349 final ActivityStack stack = stacks.get(stackNdx);
David Stevens9440dc82017-03-16 19:00:20 -07003350 if (displayShouldSleep) {
3351 stack.goToSleepIfPossible(false /* shuttingDown */);
3352 } else {
3353 stack.awakeFromSleepingLocked();
3354 if (isFocusedStack(stack)) {
3355 resumeFocusedStackTopActivityLocked();
3356 }
3357 }
3358 }
3359
3360 if (displayShouldSleep || mGoingToSleepActivities.isEmpty()) {
3361 continue;
3362 }
3363 // The display is awake now, so clean up the going to sleep list.
3364 for (Iterator<ActivityRecord> it = mGoingToSleepActivities.iterator(); it.hasNext(); ) {
3365 final ActivityRecord r = it.next();
3366 if (r.getDisplayId() == display.mDisplayId) {
3367 it.remove();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003368 }
Craig Mautner5314a402013-09-26 12:40:16 -07003369 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003370 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003371 }
3372
3373 void activitySleptLocked(ActivityRecord r) {
3374 mGoingToSleepActivities.remove(r);
David Stevens9440dc82017-03-16 19:00:20 -07003375 final ActivityStack s = r.getStack();
3376 if (s != null) {
3377 s.checkReadyForSleep();
3378 } else {
3379 checkReadyForSleepLocked(true);
3380 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003381 }
3382
David Stevens9440dc82017-03-16 19:00:20 -07003383 void checkReadyForSleepLocked(boolean allowDelay) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003384 if (!mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003385 // Do not care.
3386 return;
3387 }
3388
David Stevens18abd0e2017-08-17 14:55:47 -07003389 if (!putStacksToSleepLocked(allowDelay, false /* shuttingDown */)) {
3390 return;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003391 }
3392
Wei Wang65c7a152016-06-02 18:51:22 -07003393 // Send launch end powerhint before going sleep
3394 mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
3395
Craig Mautner0eea92c2013-05-16 13:35:39 -07003396 removeSleepTimeouts();
3397
3398 if (mGoingToSleep.isHeld()) {
3399 mGoingToSleep.release();
3400 }
3401 if (mService.mShuttingDown) {
3402 mService.notifyAll();
3403 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003404 }
3405
David Stevens18abd0e2017-08-17 14:55:47 -07003406 // Tries to put all activity stacks to sleep. Returns true if all stacks were
3407 // successfully put to sleep.
3408 private boolean putStacksToSleepLocked(boolean allowDelay, boolean shuttingDown) {
3409 boolean allSleep = true;
3410 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3411 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3412 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3413 if (allowDelay) {
3414 allSleep &= stacks.get(stackNdx).goToSleepIfPossible(shuttingDown);
3415 } else {
3416 stacks.get(stackNdx).goToSleep();
3417 }
3418 }
3419 }
3420 return allSleep;
3421 }
3422
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003423 boolean reportResumedActivityLocked(ActivityRecord r) {
Bryce Lee29a649d2017-08-18 13:52:31 -07003424 // A resumed activity cannot be stopping. remove from list
3425 mStoppingActivities.remove(r);
3426
Andrii Kulian02b7a832016-10-06 23:11:56 -07003427 final ActivityStack stack = r.getStack();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003428 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003429 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003430 }
3431 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003432 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003433 mWindowManager.executeAppTransition();
3434 return true;
3435 }
3436 return false;
3437 }
3438
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003440 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003442 int stackNdx = stacks.size() - 1;
3443 while (stackNdx >= 0) {
3444 stacks.get(stackNdx).handleAppCrashLocked(app);
3445 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003446 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003447 }
3448 }
3449
Craig Mautnerbb742462014-07-07 15:28:55 -07003450 // Called when WindowManager has finished animating the launchingBehind activity to the back.
Andrii Kulian21713ac2016-10-12 22:05:05 -07003451 private void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003452 final TaskRecord task = r.getTask();
Andrii Kulian02b7a832016-10-06 23:11:56 -07003453 final ActivityStack stack = task.getStack();
Winson730bf062016-03-31 18:04:56 -07003454
3455 r.mLaunchTaskBehind = false;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003456 mRecentTasks.addLocked(task);
Yorke Leebd54c2a2016-10-25 13:49:23 -07003457 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003458 r.setVisibility(false);
Winson730bf062016-03-31 18:04:56 -07003459
3460 // When launching tasks behind, update the last active time of the top task after the new
3461 // task has been shown briefly
3462 final ActivityRecord top = stack.topActivity();
3463 if (top != null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003464 top.getTask().touchActiveTime();
Winson730bf062016-03-31 18:04:56 -07003465 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003466 }
3467
3468 void scheduleLaunchTaskBehindComplete(IBinder token) {
3469 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3470 }
3471
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003472 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3473 boolean preserveWindows) {
Jorim Jaggife762342016-10-13 14:33:27 +02003474 mKeyguardController.beginActivityVisibilityUpdate();
3475 try {
3476 // First the front stacks. In case any are not fullscreen and are in front of home.
3477 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3478 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3479 final int topStackNdx = stacks.size() - 1;
3480 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3481 final ActivityStack stack = stacks.get(stackNdx);
3482 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
3483 }
Craig Mautner580ea812013-04-25 12:58:38 -07003484 }
Jorim Jaggife762342016-10-13 14:33:27 +02003485 } finally {
3486 mKeyguardController.endActivityVisibilityUpdate();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003487 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003488 }
3489
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01003490 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
3491 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3492 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3493 final int topStackNdx = stacks.size() - 1;
3494 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3495 final ActivityStack stack = stacks.get(stackNdx);
3496 stack.addStartingWindowsForVisibleActivities(taskSwitch);
3497 }
3498 }
3499 }
3500
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003501 void invalidateTaskLayers() {
3502 mTaskLayersChanged = true;
3503 }
3504
3505 void rankTaskLayersIfNeeded() {
3506 if (!mTaskLayersChanged) {
3507 return;
3508 }
3509 mTaskLayersChanged = false;
3510 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3511 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3512 int baseLayer = 0;
3513 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3514 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3515 }
3516 }
3517 }
3518
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003519 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3520 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3521 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3522 final int topStackNdx = stacks.size() - 1;
3523 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3524 final ActivityStack stack = stacks.get(stackNdx);
3525 stack.clearOtherAppTimeTrackers(except);
3526 }
3527 }
3528 }
3529
Craig Mautner8d341ef2013-03-26 09:03:27 -07003530 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3532 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003533 final int numStacks = stacks.size();
3534 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3535 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003536 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003538 }
3539 }
3540
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003541 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3542 // Examine all activities currently running in the process.
3543 TaskRecord firstTask = null;
3544 // Tasks is non-null only if two or more tasks are found.
3545 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003546 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3547 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003548 ActivityRecord r = app.activities.get(i);
3549 // First, if we find an activity that is in the process of being destroyed,
3550 // then we just aren't going to do anything for now; we want things to settle
3551 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003552 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003553 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003554 return;
3555 }
3556 // Don't consider any activies that are currently not in a state where they
3557 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003558 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3559 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003561 continue;
3562 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003563
3564 final TaskRecord task = r.getTask();
3565 if (task != null) {
3566 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003567 + " from " + r);
3568 if (firstTask == null) {
Bryce Leeaf691c02017-03-20 14:20:22 -07003569 firstTask = task;
3570 } else if (firstTask != task) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003571 if (tasks == null) {
3572 tasks = new ArraySet<>();
3573 tasks.add(firstTask);
3574 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003575 tasks.add(task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003576 }
3577 }
3578 }
3579 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003580 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003581 return;
3582 }
3583 // If we have activities in multiple tasks that are in a position to be destroyed,
3584 // let's iterate through the tasks and release the oldest one.
3585 final int numDisplays = mActivityDisplays.size();
3586 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3587 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3588 // Step through all stacks starting from behind, to hit the oldest things first.
3589 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3590 final ActivityStack stack = stacks.get(stackNdx);
3591 // Try to release activities in this stack; if we manage to, we are done.
3592 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3593 return;
3594 }
3595 }
3596 }
3597 }
3598
Amith Yamasani37a40c22015-06-17 13:25:42 -07003599 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003600 final int focusStackId = mFocusedStack.getStackId();
3601 // We dismiss the docked stack whenever we switch users.
3602 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, focusStackId == DOCKED_STACK_ID);
Winson Chungc2b23a52017-01-09 15:44:55 -08003603 // Also dismiss the pinned stack whenever we switch users. Removing the pinned stack will
3604 // also cause all tasks to be moved to the fullscreen stack at a position that is
3605 // appropriate.
3606 removeStackLocked(PINNED_STACK_ID);
Wale Ogunwale9a98c522016-04-27 09:23:55 -07003607
3608 mUserStackInFront.put(mCurrentUser, focusStackId);
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003609 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003610 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003611
Craig Mautner858d8a62013-04-23 17:08:34 -07003612 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003613 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3614 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003615 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003616 final ActivityStack stack = stacks.get(stackNdx);
3617 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003618 TaskRecord task = stack.topTask();
3619 if (task != null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003620 stack.positionChildWindowContainerAtTop(task);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003621 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003622 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003623 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003624
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003625 ActivityStack stack = getStack(restoreStackId);
3626 if (stack == null) {
3627 stack = mHomeStack;
3628 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003629 final boolean homeInFront = stack.isActivityTypeHome();
Craig Mautnere0a38842013-12-16 16:14:02 -08003630 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003631 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003632 } else {
3633 // Stack was moved to another display while user was swapped out.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003634 resumeHomeStackTask(null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003635 }
Craig Mautner93529a42013-10-04 15:03:13 -07003636 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003637 }
3638
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003639 /** Checks whether the userid is a profile of the current user. */
3640 boolean isCurrentProfileLocked(int userId) {
3641 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003642 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003643 }
3644
Bryce Leeb7c9b802017-05-02 14:20:24 -07003645 /**
3646 * Returns whether a stopping activity is present that should be stopped after visible, rather
3647 * than idle.
3648 * @return {@code true} if such activity is present. {@code false} otherwise.
3649 */
3650 boolean isStoppingNoHistoryActivity() {
3651 // Activities that are marked as nohistory should be stopped immediately after the resumed
3652 // activity has become visible.
3653 for (ActivityRecord record : mStoppingActivities) {
3654 if (record.isNoHistory()) {
3655 return true;
3656 }
3657 }
3658
3659 return false;
3660 }
3661
Winson Chung4dabf232017-01-25 13:25:22 -08003662 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(ActivityRecord idleActivity,
3663 boolean remove, boolean processPausingActivities) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003664 ArrayList<ActivityRecord> stops = null;
3665
3666 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003667 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3668 ActivityRecord s = mStoppingActivities.get(activityNdx);
Bryce Lee4a194382017-04-04 14:32:48 -07003669 boolean waitingVisible = mActivitiesWaitingForVisibleActivity.contains(s);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003670 if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003671 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3672 if (waitingVisible && nowVisible) {
Bryce Lee4a194382017-04-04 14:32:48 -07003673 mActivitiesWaitingForVisibleActivity.remove(s);
Andrii Kulianee056812016-09-21 15:34:45 -07003674 waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003675 if (s.finishing) {
3676 // If this activity is finishing, it is sitting on top of
3677 // everyone else but we now know it is no longer needed...
3678 // so get rid of it. Otherwise, we need to go through the
3679 // normal flow and hide it once we determine that it is
3680 // hidden by the activities in front of it.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003681 if (DEBUG_STATES) Slog.v(TAG, "Before stopping, can hide: " + s);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003682 s.setVisibility(false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003683 }
3684 }
David Stevens9440dc82017-03-16 19:00:20 -07003685 if (remove) {
3686 final ActivityStack stack = s.getStack();
3687 final boolean shouldSleepOrShutDown = stack != null
3688 ? stack.shouldSleepOrShutDownActivities()
3689 : mService.isSleepingOrShuttingDownLocked();
3690 if (!waitingVisible || shouldSleepOrShutDown) {
3691 if (!processPausingActivities && s.state == PAUSING) {
3692 // Defer processing pausing activities in this iteration and reschedule
3693 // a delayed idle to reprocess it again
3694 removeTimeoutsForActivityLocked(idleActivity);
3695 scheduleIdleTimeoutLocked(idleActivity);
3696 continue;
3697 }
Winson Chung4dabf232017-01-25 13:25:22 -08003698
David Stevens9440dc82017-03-16 19:00:20 -07003699 if (DEBUG_STATES) Slog.v(TAG, "Ready to stop: " + s);
3700 if (stops == null) {
3701 stops = new ArrayList<>();
3702 }
3703 stops.add(s);
3704 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003705 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003706 }
3707 }
3708
3709 return stops;
3710 }
3711
Craig Mautnercf910b02013-04-23 11:23:27 -07003712 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3714 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3716 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003717 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003718 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003719 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003720 if (r == null) Slog.e(TAG,
3721 "validateTop...: null top activity, stack=" + stack);
3722 else {
3723 final ActivityRecord pausing = stack.mPausingActivity;
3724 if (pausing != null && pausing == r) Slog.e(TAG,
3725 "validateTop...: top stack has pausing activity r=" + r
3726 + " state=" + state);
3727 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3728 "validateTop...: activity in front not resumed r=" + r
3729 + " state=" + state);
3730 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003731 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003732 final ActivityRecord resumed = stack.mResumedActivity;
3733 if (resumed != null && resumed == r) Slog.e(TAG,
3734 "validateTop...: back stack has resumed activity r=" + r
3735 + " state=" + state);
3736 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3737 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003738 }
3739 }
3740 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003741 }
3742
Craig Mautner27084302013-03-25 08:05:25 -07003743 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003744 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003746 pw.print(prefix);
3747 pw.println("mCurTaskIdForUser=" + mCurTaskIdForUser);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003748 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07003749 pw.print(prefix); pw.println("mStacks=" + mStacks);
Bryce Lee4a194382017-04-04 14:32:48 -07003750 if (!mWaitingForActivityVisible.isEmpty()) {
3751 pw.print(prefix); pw.println("mWaitingForActivityVisible=");
3752 for (int i = 0; i < mWaitingForActivityVisible.size(); ++i) {
3753 pw.print(prefix); pw.print(prefix); mWaitingForActivityVisible.get(i).dump(pw, prefix);
3754 }
3755 }
3756
Jorim Jaggi8d786932016-10-26 19:08:36 -07003757 mKeyguardController.dump(pw, prefix);
Benjamin Franza83859f2017-07-03 16:34:14 +01003758 mService.mLockTaskController.dump(pw, prefix);
Craig Mautner27084302013-03-25 08:05:25 -07003759 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003760
Winson43d1f262016-06-14 16:05:55 -07003761 /**
Andrii Kulian5406e7a2016-10-21 11:55:23 -07003762 * Dump all connected displays' configurations.
3763 * @param prefix Prefix to apply to each line of the dump.
3764 */
3765 void dumpDisplayConfigs(PrintWriter pw, String prefix) {
3766 pw.print(prefix); pw.println("Display override configurations:");
3767 final int displayCount = mActivityDisplays.size();
3768 for (int i = 0; i < displayCount; i++) {
3769 final ActivityDisplay activityDisplay = mActivityDisplays.valueAt(i);
3770 pw.print(prefix); pw.print(" "); pw.print(activityDisplay.mDisplayId); pw.print(": ");
3771 pw.println(activityDisplay.getOverrideConfiguration());
3772 }
3773 }
3774
3775 /**
Winson43d1f262016-06-14 16:05:55 -07003776 * Dumps the activities matching the given {@param name} in the either the focused stack
3777 * or all visible stacks if {@param dumpVisibleStacks} is true.
3778 */
Winson Chung6998bc42017-02-28 17:07:05 -08003779 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name, boolean dumpVisibleStacksOnly,
3780 boolean dumpFocusedStackOnly) {
3781 if (dumpFocusedStackOnly) {
3782 return mFocusedStack.getDumpActivitiesLocked(name);
3783 } else {
Winson43d1f262016-06-14 16:05:55 -07003784 ArrayList<ActivityRecord> activities = new ArrayList<>();
3785 int numDisplays = mActivityDisplays.size();
3786 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3787 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3789 ActivityStack stack = stacks.get(stackNdx);
Winson Chung6998bc42017-02-28 17:07:05 -08003790 if (!dumpVisibleStacksOnly ||
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07003791 stack.shouldBeVisible(null) == STACK_VISIBLE) {
Winson43d1f262016-06-14 16:05:55 -07003792 activities.addAll(stack.getDumpActivitiesLocked(name));
3793 }
3794 }
3795 }
3796 return activities;
Winson43d1f262016-06-14 16:05:55 -07003797 }
Craig Mautner20e72272013-04-01 13:45:53 -07003798 }
3799
Dianne Hackborn390517b2013-05-30 15:03:32 -07003800 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3801 boolean needSep, String prefix) {
3802 if (activity != null) {
3803 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3804 if (needSep) {
3805 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003806 }
3807 pw.print(prefix);
3808 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003809 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003810 }
3811 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003812 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003813 }
3814
Craig Mautner8d341ef2013-03-26 09:03:27 -07003815 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3816 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003817 boolean printed = false;
3818 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003819 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3820 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003821 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003822 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003823 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003824 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003826 pw.println();
3827 pw.println(" Stack #" + stack.mStackId + ":");
3828 pw.println(" mFullscreen=" + stack.mFullscreen);
3829 pw.println(" isSleeping=" + stack.shouldSleepActivities());
3830 pw.println(" mBounds=" + stack.mBounds);
Winson Chungabb433b2017-03-24 09:35:42 -07003831
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003832 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3833 needSep);
Winson Chungabb433b2017-03-24 09:35:42 -07003834
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3836 !dumpAll, false, dumpPackage, true,
3837 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003838
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 needSep = printed;
3840 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3841 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003842 if (pr) {
3843 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003844 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003845 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3847 " mResumedActivity: ");
3848 if (pr) {
3849 printed = true;
3850 needSep = false;
3851 }
3852 if (dumpAll) {
3853 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3854 " mLastPausedActivity: ");
3855 if (pr) {
3856 printed = true;
3857 needSep = true;
3858 }
3859 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3860 needSep, " mLastNoHistoryActivity: ");
3861 }
3862 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003863 }
3864 }
3865
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003866 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3867 false, dumpPackage, true, " Activities waiting to finish:", null);
3868 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3869 false, dumpPackage, true, " Activities waiting to stop:", null);
Bryce Lee4a194382017-04-04 14:32:48 -07003870 printed |= dumpHistoryList(fd, pw, mActivitiesWaitingForVisibleActivity, " ", "Wait",
3871 false, !dumpAll, false, dumpPackage, true,
3872 " Activities waiting for another to become visible:", null);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003873 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3874 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003875
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003876 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003877 }
3878
Dianne Hackborn390517b2013-05-30 15:03:32 -07003879 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003880 String prefix, String label, boolean complete, boolean brief, boolean client,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003881 String dumpPackage, boolean needNL, String header, TaskRecord lastTask) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003882 String innerPrefix = null;
3883 String[] args = null;
3884 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003885 for (int i=list.size()-1; i>=0; i--) {
3886 final ActivityRecord r = list.get(i);
3887 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3888 continue;
3889 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003890 if (innerPrefix == null) {
3891 innerPrefix = prefix + " ";
3892 args = new String[0];
3893 }
3894 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003895 final boolean full = !brief && (complete || !r.isInHistory());
3896 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003897 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003898 needNL = false;
3899 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07003900 if (header != null) {
3901 pw.println(header);
3902 header = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003903 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003904 if (lastTask != r.getTask()) {
3905 lastTask = r.getTask();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003906 pw.print(prefix);
3907 pw.print(full ? "* " : " ");
3908 pw.println(lastTask);
3909 if (full) {
3910 lastTask.dump(pw, prefix + " ");
3911 } else if (complete) {
3912 // Complete + brief == give a summary. Isn't that obvious?!?
3913 if (lastTask.intent != null) {
3914 pw.print(prefix); pw.print(" ");
3915 pw.println(lastTask.intent.toInsecureStringWithClip());
3916 }
3917 }
3918 }
3919 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3920 pw.print(" #"); pw.print(i); pw.print(": ");
3921 pw.println(r);
3922 if (full) {
3923 r.dump(pw, innerPrefix);
3924 } else if (complete) {
3925 // Complete + brief == give a summary. Isn't that obvious?!?
3926 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3927 if (r.app != null) {
3928 pw.print(innerPrefix); pw.println(r.app);
3929 }
3930 }
3931 if (client && r.app != null && r.app.thread != null) {
3932 // flush anything that is already in the PrintWriter since the thread is going
3933 // to write to the file descriptor directly
3934 pw.flush();
3935 try {
3936 TransferPipe tp = new TransferPipe();
3937 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003938 r.app.thread.dumpActivity(tp.getWriteFd(), r.appToken, innerPrefix, args);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003939 // Short timeout, since blocking here can
3940 // deadlock with the application.
3941 tp.go(fd, 2000);
3942 } finally {
3943 tp.kill();
3944 }
3945 } catch (IOException e) {
3946 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3947 } catch (RemoteException e) {
3948 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3949 }
3950 needNL = true;
3951 }
3952 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003953 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003954 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003955
Craig Mautnerf3333272013-04-22 10:55:53 -07003956 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003957 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3958 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003959 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3960 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003961 }
3962
3963 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003964 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003965 }
3966
3967 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003968 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3969 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003970 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3971 }
3972
Craig Mautner05d29032013-05-03 13:40:13 -07003973 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003974 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3975 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3976 }
Craig Mautner05d29032013-05-03 13:40:13 -07003977 }
3978
Craig Mautner0eea92c2013-05-16 13:35:39 -07003979 void removeSleepTimeouts() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003980 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3981 }
3982
3983 final void scheduleSleepTimeout() {
3984 removeSleepTimeouts();
3985 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3986 }
3987
Craig Mautner4a1cb222013-12-04 16:14:06 -08003988 @Override
3989 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003990 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003991 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3992 }
3993
3994 @Override
3995 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003996 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003997 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3998 }
3999
4000 @Override
4001 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004002 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004003 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4004 }
4005
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004006 private void handleDisplayAdded(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 synchronized (mService) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004008 getActivityDisplayOrCreateLocked(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004010 }
4011
Andrii Kulianca007a62016-11-22 16:33:28 -08004012 /** Check if display with specified id is added to the list. */
4013 boolean isDisplayAdded(int displayId) {
Andrii Kulian62e6f252017-05-30 22:46:53 -07004014 return getActivityDisplayOrCreateLocked(displayId) != null;
4015 }
4016
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004017 ActivityDisplay getActivityDisplay(int displayId) {
4018 return mActivityDisplays.get(displayId);
4019 }
4020
Andrii Kulian62e6f252017-05-30 22:46:53 -07004021 /**
4022 * Get an existing instance of {@link ActivityDisplay} or create new if there is a
4023 * corresponding record in display manager.
4024 */
4025 private ActivityDisplay getActivityDisplayOrCreateLocked(int displayId) {
4026 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4027 if (activityDisplay != null) {
4028 return activityDisplay;
4029 }
4030 if (mDisplayManager == null) {
4031 // The system isn't fully initialized yet.
4032 return null;
4033 }
4034 final Display display = mDisplayManager.getDisplay(displayId);
4035 if (display == null) {
4036 // The display is not registered in DisplayManager.
4037 return null;
4038 }
4039 // The display hasn't been added to ActivityManager yet, create a new record now.
4040 activityDisplay = new ActivityDisplay(displayId);
4041 if (activityDisplay.mDisplay == null) {
4042 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4043 return null;
4044 }
4045 mActivityDisplays.put(displayId, activityDisplay);
4046 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
4047 mWindowManager.onDisplayAdded(displayId);
4048 return activityDisplay;
Andrii Kulianca007a62016-11-22 16:33:28 -08004049 }
4050
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004051 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004052 mDefaultMinSizeOfResizeableTask =
Jorim Jaggi19cf2972016-04-07 23:26:10 -07004053 mService.mContext.getResources().getDimensionPixelSize(
4054 com.android.internal.R.dimen.default_minimal_size_resizable_task);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004055 }
4056
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004057 private void handleDisplayRemoved(int displayId) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004058 if (displayId == DEFAULT_DISPLAY) {
4059 throw new IllegalArgumentException("Can't remove the primary display.");
4060 }
4061
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004063 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4064 if (activityDisplay != null) {
Andrii Kulian250d6532017-02-08 23:30:45 -08004065 final boolean destroyContentOnRemoval
4066 = activityDisplay.shouldDestroyContentOnRemove();
Andrii Kuliana33818c2017-02-16 16:11:30 -08004067 final ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
4068 while (!stacks.isEmpty()) {
4069 final ActivityStack stack = stacks.get(0);
Andrii Kulian250d6532017-02-08 23:30:45 -08004070 if (destroyContentOnRemoval) {
Andrii Kuliana33818c2017-02-16 16:11:30 -08004071 moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
4072 false /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004073 stack.finishAllActivitiesLocked(true /* immediately */);
Andrii Kuliana33818c2017-02-16 16:11:30 -08004074 } else {
4075 // Moving all tasks to fullscreen stack, because it's guaranteed to be
4076 // a valid launch stack for all activities. This way the task history from
4077 // external display will be preserved on primary after move.
4078 moveTasksToFullscreenStackLocked(stack.getStackId(), true /* onTop */);
Andrii Kulian250d6532017-02-08 23:30:45 -08004079 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 }
David Stevens9440dc82017-03-16 19:00:20 -07004081
4082 releaseSleepTokens(activityDisplay);
4083
Craig Mautnere0a38842013-12-16 16:14:02 -08004084 mActivityDisplays.remove(displayId);
Bryce Leeaf3a4002017-03-20 10:37:12 -07004085 mWindowManager.onDisplayRemoved(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004086 }
4087 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 }
4089
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004090 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004092 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4093 if (activityDisplay != null) {
David Stevens9440dc82017-03-16 19:00:20 -07004094 // The window policy is responsible for stopping activities on the default display
4095 if (displayId != Display.DEFAULT_DISPLAY) {
4096 int displayState = activityDisplay.mDisplay.getState();
4097 if (displayState == Display.STATE_OFF && activityDisplay.mOffToken == null) {
4098 activityDisplay.mOffToken =
4099 mService.acquireSleepToken("Display-off", displayId);
4100 } else if (displayState == Display.STATE_ON
4101 && activityDisplay.mOffToken != null) {
4102 activityDisplay.mOffToken.release();
4103 activityDisplay.mOffToken = null;
4104 }
4105 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004106 // TODO: Update the bounds.
4107 }
Bryce Leeaf3a4002017-03-20 10:37:12 -07004108 mWindowManager.onDisplayChanged(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 }
4111
David Stevens9440dc82017-03-16 19:00:20 -07004112 SleepToken createSleepTokenLocked(String tag, int displayId) {
4113 ActivityDisplay display = mActivityDisplays.get(displayId);
4114 if (display == null) {
4115 throw new IllegalArgumentException("Invalid display: " + displayId);
4116 }
4117
4118 final SleepTokenImpl token = new SleepTokenImpl(tag, displayId);
4119 mSleepTokens.add(token);
4120 display.mAllSleepTokens.add(token);
4121 return token;
4122 }
4123
4124 private void removeSleepTokenLocked(SleepTokenImpl token) {
4125 mSleepTokens.remove(token);
4126
4127 ActivityDisplay display = mActivityDisplays.get(token.mDisplayId);
4128 if (display != null) {
4129 display.mAllSleepTokens.remove(token);
4130 if (display.mAllSleepTokens.isEmpty()) {
4131 mService.updateSleepIfNeededLocked();
4132 }
4133 }
4134 }
4135
4136 private void releaseSleepTokens(ActivityDisplay display) {
4137 if (display.mAllSleepTokens.isEmpty()) {
4138 return;
4139 }
4140 for (SleepTokenImpl token : display.mAllSleepTokens) {
4141 mSleepTokens.remove(token);
4142 }
4143 display.mAllSleepTokens.clear();
4144
4145 mService.updateSleepIfNeededLocked();
4146 }
4147
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004148 private StackInfo getStackInfoLocked(ActivityStack stack) {
Andrii Kulian7e215d72017-04-26 18:33:28 -07004149 final int displayId = stack.mDisplayId;
4150 final ActivityDisplay display = mActivityDisplays.get(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 StackInfo info = new StackInfo();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004152 stack.getWindowContainerBounds(info.bounds);
Andrii Kulian7e215d72017-04-26 18:33:28 -07004153 info.displayId = displayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004154 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004155 info.userId = stack.mCurrentUser;
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004156 info.visible = stack.shouldBeVisible(null) == STACK_VISIBLE;
Andrii Kulian62e6f252017-05-30 22:46:53 -07004157 // A stack might be not attached to a display.
Winson529c8e42016-05-17 11:08:40 -07004158 info.position = display != null
4159 ? display.mStacks.indexOf(stack)
4160 : 0;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004161
4162 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4163 final int numTasks = tasks.size();
4164 int[] taskIds = new int[numTasks];
4165 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004166 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004167 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 for (int i = 0; i < numTasks; ++i) {
4169 final TaskRecord task = tasks.get(i);
4170 taskIds[i] = task.taskId;
4171 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4172 : task.realActivity != null ? task.realActivity.flattenToString()
4173 : task.getTopActivity() != null ? task.getTopActivity().packageName
4174 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004175 taskBounds[i] = new Rect();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004176 task.getWindowContainerBounds(taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004177 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004178 }
4179 info.taskIds = taskIds;
4180 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004181 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004182 info.taskUserIds = taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07004183
4184 final ActivityRecord top = stack.topRunningActivityLocked();
4185 info.topActivity = top != null ? top.intent.getComponent() : null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 return info;
4187 }
4188
4189 StackInfo getStackInfoLocked(int stackId) {
4190 ActivityStack stack = getStack(stackId);
4191 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004192 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004193 }
4194 return null;
4195 }
4196
4197 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004198 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004199 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4200 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004201 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004202 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004203 }
4204 }
4205 return list;
4206 }
4207
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004208 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004209 int preferredDisplayId, int actualStackId) {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004210 handleNonResizableTaskIfNeeded(task, preferredWindowingMode, preferredDisplayId,
4211 actualStackId, false /* forceNonResizable */);
Andrii Kulianc27916642016-04-12 17:59:27 -07004212 }
4213
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004214 void handleNonResizableTaskIfNeeded(TaskRecord task, int preferredWindowingMode,
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004215 int preferredDisplayId, int actualStackId, boolean forceNonResizable) {
4216 final boolean isSecondaryDisplayPreferred =
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004217 (preferredDisplayId != DEFAULT_DISPLAY && preferredDisplayId != INVALID_DISPLAY);
Wale Ogunwale926aade2017-08-29 11:24:37 -07004218 final ActivityStack actualStack = getStack(actualStackId);
4219 final boolean inSplitScreenMode = actualStack != null
4220 && actualStack.inSplitScreenWindowingMode();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004221 if (((!inSplitScreenMode && preferredWindowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004222 && !isSecondaryDisplayPreferred) || task.isActivityTypeHome()) {
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004223 return;
4224 }
4225
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004226 // Handle incorrect launch/move to secondary display if needed.
4227 final boolean launchOnSecondaryDisplayFailed;
4228 if (isSecondaryDisplayPreferred) {
4229 final int actualDisplayId = task.getStack().mDisplayId;
4230 if (!task.canBeLaunchedOnDisplay(actualDisplayId)) {
4231 // The task landed on an inappropriate display somehow, move it to the default
4232 // display.
4233 // TODO(multi-display): Find proper stack for the task on the default display.
4234 mService.moveTaskToStack(task.taskId, FULLSCREEN_WORKSPACE_STACK_ID,
4235 true /* toTop */);
4236 launchOnSecondaryDisplayFailed = true;
4237 } else {
4238 // The task might have landed on a display different from requested.
4239 launchOnSecondaryDisplayFailed = actualDisplayId == DEFAULT_DISPLAY
4240 || (preferredDisplayId != INVALID_DISPLAY
4241 && preferredDisplayId != actualDisplayId);
4242 }
4243 } else {
4244 // The task wasn't requested to be on a secondary display.
4245 launchOnSecondaryDisplayFailed = false;
4246 }
4247
Jorim Jaggicd13d332016-04-27 15:40:20 -07004248 final ActivityRecord topActivity = task.getTopActivity();
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004249 if (launchOnSecondaryDisplayFailed || !task.supportsSplitScreen() || forceNonResizable) {
4250 if (launchOnSecondaryDisplayFailed) {
4251 // Display a warning toast that we tried to put a non-resizeable task on a secondary
4252 // display with config different from global config.
4253 mService.mTaskChangeNotificationController
4254 .notifyActivityLaunchOnSecondaryDisplayFailed();
4255 } else {
4256 // Display a warning toast that we tried to put a non-dockable task in the docked
4257 // stack.
4258 mService.mTaskChangeNotificationController.notifyActivityDismissingDockedStack();
4259 }
Jorim Jaggid53f0922016-04-06 22:16:23 -07004260
Andrii Kulianc27916642016-04-12 17:59:27 -07004261 // Dismiss docked stack. If task appeared to be in docked stack but is not resizable -
4262 // we need to move it to top of fullscreen stack, otherwise it will be covered.
Wale Ogunwale9a98c522016-04-27 09:23:55 -07004263 moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, actualStackId == DOCKED_STACK_ID);
Winson Chungd3395382016-12-13 11:49:09 -08004264 } else if (topActivity != null && topActivity.isNonResizableOrForcedResizable()
Jorim Jaggicd13d332016-04-27 15:40:20 -07004265 && !topActivity.noDisplay) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004266 final String packageName = topActivity.appInfo.packageName;
4267 final int reason = isSecondaryDisplayPreferred
4268 ? FORCED_RESIZEABLE_REASON_SECONDARY_DISPLAY
4269 : FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Yorke Leebd54c2a2016-10-25 13:49:23 -07004270 mService.mTaskChangeNotificationController.notifyActivityForcedResizable(
Andrii Kulian036e3ad2017-04-19 10:55:10 -07004271 task.taskId, reason, packageName);
Wale Ogunwaleb9b16a72016-01-27 12:24:44 -08004272 }
Chong Zhangb15758a2015-11-17 12:12:03 -08004273 }
4274
Jorim Jaggife89d122015-12-22 16:28:44 +01004275 void activityRelaunchedLocked(IBinder token) {
4276 mWindowManager.notifyAppRelaunchingFinished(token);
David Stevens9440dc82017-03-16 19:00:20 -07004277 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
4278 if (r != null) {
4279 if (r.getStack().shouldSleepOrShutDownActivities()) {
Wale Ogunwale3e997362016-09-06 10:37:56 -07004280 r.setSleeping(true, true);
4281 }
4282 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004283 }
4284
4285 void activityRelaunchingLocked(ActivityRecord r) {
4286 mWindowManager.notifyAppRelaunching(r.appToken);
4287 }
4288
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004289 void logStackState() {
4290 mActivityMetricsLogger.logWindowState();
4291 }
4292
Winson Chung5af42fc2017-03-24 17:11:33 -07004293 void scheduleUpdateMultiWindowMode(TaskRecord task) {
Winson Chung8bca9e42017-04-16 15:59:43 -07004294 // If the stack is animating in a way where we will be forcing a multi-mode change at the
4295 // end, then ensure that we defer all in between multi-window mode changes
4296 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
4297 return;
4298 }
4299
Wale Ogunwale22e25262016-02-01 10:32:02 -08004300 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4301 final ActivityRecord r = task.mActivities.get(i);
4302 if (r.app != null && r.app.thread != null) {
4303 mMultiWindowModeChangedActivities.add(r);
4304 }
4305 }
4306
4307 if (!mHandler.hasMessages(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG)) {
4308 mHandler.sendEmptyMessage(REPORT_MULTI_WINDOW_MODE_CHANGED_MSG);
4309 }
4310 }
4311
Winson Chung5af42fc2017-03-24 17:11:33 -07004312 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, ActivityStack prevStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07004313 final ActivityStack stack = task.getStack();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004314 if (prevStack == null || prevStack == stack
4315 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
4316 return;
4317 }
4318
Winson Chungab76bbc2017-08-14 13:33:51 -07004319 scheduleUpdatePictureInPictureModeIfNeeded(task, stack.mBounds);
Winson Chung5af42fc2017-03-24 17:11:33 -07004320 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004321
Winson Chungab76bbc2017-08-14 13:33:51 -07004322 void scheduleUpdatePictureInPictureModeIfNeeded(TaskRecord task, Rect targetStackBounds) {
4323 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4324 final ActivityRecord r = task.mActivities.get(i);
4325 if (r.app != null && r.app.thread != null) {
4326 mPipModeChangedActivities.add(r);
Winson Chung5af42fc2017-03-24 17:11:33 -07004327 }
Winson Chungab76bbc2017-08-14 13:33:51 -07004328 }
4329 mPipModeChangedTargetStackBounds = targetStackBounds;
Winson Chung5af42fc2017-03-24 17:11:33 -07004330
Winson Chungab76bbc2017-08-14 13:33:51 -07004331 if (!mHandler.hasMessages(REPORT_PIP_MODE_CHANGED_MSG)) {
4332 mHandler.sendEmptyMessage(REPORT_PIP_MODE_CHANGED_MSG);
4333 }
4334 }
4335
4336 void updatePictureInPictureMode(TaskRecord task, Rect targetStackBounds, boolean forceUpdate) {
4337 mHandler.removeMessages(REPORT_PIP_MODE_CHANGED_MSG);
4338 for (int i = task.mActivities.size() - 1; i >= 0; i--) {
4339 final ActivityRecord r = task.mActivities.get(i);
4340 if (r.app != null && r.app.thread != null) {
4341 r.updatePictureInPictureMode(targetStackBounds, forceUpdate);
Winson Chung5af42fc2017-03-24 17:11:33 -07004342 }
Wale Ogunwale22e25262016-02-01 10:32:02 -08004343 }
4344 }
4345
Tony Mak853304c2016-04-18 15:17:41 +01004346 void setDockedStackMinimized(boolean minimized) {
4347 mIsDockMinimized = minimized;
Tony Mak853304c2016-04-18 15:17:41 +01004348 }
4349
chaviw59b98852017-06-13 12:05:44 -07004350 void wakeUp(String reason) {
4351 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.server.am:TURN_ON:" + reason);
4352 }
4353
4354 /**
4355 * Begin deferring resume to avoid duplicate resumes in one pass.
4356 */
4357 private void beginDeferResume() {
4358 mDeferResumeCount++;
4359 }
4360
4361 /**
4362 * End deferring resume and determine if resume can be called.
4363 */
4364 private void endDeferResume() {
4365 mDeferResumeCount--;
4366 }
4367
4368 /**
4369 * @return True if resume can be called.
4370 */
4371 private boolean readyToResume() {
4372 return mDeferResumeCount == 0;
4373 }
4374
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004375 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004376
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004377 public ActivityStackSupervisorHandler(Looper looper) {
4378 super(looper);
4379 }
4380
Winson Chung4dabf232017-01-25 13:25:22 -08004381 void activityIdleInternal(ActivityRecord r, boolean processPausingActivities) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004382 synchronized (mService) {
Winson Chung4dabf232017-01-25 13:25:22 -08004383 activityIdleInternalLocked(r != null ? r.appToken : null, true /* fromTimeout */,
4384 processPausingActivities, null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004385 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004386 }
4387
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004388 @Override
4389 public void handleMessage(Message msg) {
4390 switch (msg.what) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004391 case REPORT_MULTI_WINDOW_MODE_CHANGED_MSG: {
4392 synchronized (mService) {
4393 for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) {
4394 final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i);
Winson Chung5af42fc2017-03-24 17:11:33 -07004395 r.updateMultiWindowMode();
Wale Ogunwale22e25262016-02-01 10:32:02 -08004396 }
4397 }
4398 } break;
4399 case REPORT_PIP_MODE_CHANGED_MSG: {
4400 synchronized (mService) {
4401 for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) {
4402 final ActivityRecord r = mPipModeChangedActivities.remove(i);
Winson Chungab76bbc2017-08-14 13:33:51 -07004403 r.updatePictureInPictureMode(mPipModeChangedTargetStackBounds,
4404 false /* forceUpdate */);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004405 }
4406 }
4407 } break;
Craig Mautnerf3333272013-04-22 10:55:53 -07004408 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004409 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4410 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004411 // We don't at this point know if the activity is fullscreen,
4412 // so we need to be conservative and assume it isn't.
Winson Chung4dabf232017-01-25 13:25:22 -08004413 activityIdleInternal((ActivityRecord) msg.obj,
4414 true /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004415 } break;
4416 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004417 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Winson Chung4dabf232017-01-25 13:25:22 -08004418 activityIdleInternal((ActivityRecord) msg.obj,
4419 false /* processPausingActivities */);
Craig Mautnerf3333272013-04-22 10:55:53 -07004420 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004421 case RESUME_TOP_ACTIVITY_MSG: {
4422 synchronized (mService) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004423 resumeFocusedStackTopActivityLocked();
Craig Mautner05d29032013-05-03 13:40:13 -07004424 }
4425 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004426 case SLEEP_TIMEOUT_MSG: {
4427 synchronized (mService) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07004428 if (mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07004429 Slog.w(TAG, "Sleep timeout! Sleeping now.");
David Stevens9440dc82017-03-16 19:00:20 -07004430 checkReadyForSleepLocked(false /* allowDelay */);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004431 }
4432 }
4433 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004434 case LAUNCH_TIMEOUT_MSG: {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004435 synchronized (mService) {
4436 if (mLaunchingActivity.isHeld()) {
4437 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4438 if (VALIDATE_WAKE_LOCK_CALLER
4439 && Binder.getCallingUid() != Process.myUid()) {
4440 throw new IllegalStateException("Calling must be system uid");
4441 }
4442 mLaunchingActivity.release();
4443 }
4444 }
4445 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004446 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004447 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 } break;
4449 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004450 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004451 } break;
4452 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004453 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004455 case LAUNCH_TASK_BEHIND_COMPLETE: {
4456 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004457 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004458 if (r != null) {
4459 handleLaunchTaskBehindCompleteLocked(r);
4460 }
4461 }
4462 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004463
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004464 }
4465 }
4466 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004467
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004468 // TODO: Move to its own file.
Craig Mautner4a1cb222013-12-04 16:14:06 -08004469 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4470 * attached {@link ActivityStack}s */
Andrii Kulian1779e612016-10-12 21:58:25 -07004471 class ActivityDisplay extends ConfigurationContainer {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004472 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004473 int mDisplayId;
4474 Display mDisplay;
Craig Mautnered6649f2013-12-02 14:08:25 -08004475
Craig Mautner4a1cb222013-12-04 16:14:06 -08004476 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4477 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Wale Ogunwale1f544be2015-12-17 10:27:23 -08004478 final ArrayList<ActivityStack> mStacks = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004479
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004480 /** Array of all UIDs that are present on the display. */
4481 private IntArray mDisplayAccessUIDs = new IntArray();
4482
David Stevens9440dc82017-03-16 19:00:20 -07004483 /** All tokens used to put activities on this stack to sleep (including mOffToken) */
4484 final ArrayList<SleepTokenImpl> mAllSleepTokens = new ArrayList<>();
4485 /** The token acquired by ActivityStackSupervisor to put stacks on the display to sleep */
4486 SleepToken mOffToken;
4487
4488 private boolean mSleeping;
4489
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004490 @VisibleForTesting
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004491 ActivityDisplay() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004492 mActivityDisplays.put(mDisplayId, this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004493 }
Craig Mautner4504de52013-12-20 09:06:56 -08004494
Craig Mautner1a70a162014-09-13 12:09:31 -07004495 // After instantiation, check that mDisplay is not null before using this. The alternative
4496 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004497 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004498 final Display display = mDisplayManager.getDisplay(displayId);
4499 if (display == null) {
4500 return;
4501 }
4502 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004503 }
4504
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004505 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004506 mDisplay = display;
4507 mDisplayId = display.getDisplayId();
Craig Mautnered6649f2013-12-02 14:08:25 -08004508 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004509
Winson Chung7c3ede32017-03-14 14:18:34 -07004510 void attachStack(ActivityStack stack, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08004511 if (DEBUG_STACK) Slog.v(TAG_STACK, "attachStack: attaching " + stack
Winson Chung7c3ede32017-03-14 14:18:34 -07004512 + " to displayId=" + mDisplayId + " position=" + position);
4513 mStacks.add(position, stack);
David Stevens9440dc82017-03-16 19:00:20 -07004514 mService.updateSleepIfNeededLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 }
4516
Wale Ogunwale1666e312016-12-16 11:27:18 -08004517 void detachStack(ActivityStack stack) {
4518 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachStack: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004519 + " from displayId=" + mDisplayId);
4520 mStacks.remove(stack);
David Stevens9440dc82017-03-16 19:00:20 -07004521 mService.updateSleepIfNeededLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004522 }
4523
Craig Mautner34b73df2014-01-12 21:11:08 -08004524 @Override
4525 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004526 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4527 }
Andrii Kulian1779e612016-10-12 21:58:25 -07004528
4529 @Override
4530 protected int getChildCount() {
4531 return mStacks.size();
4532 }
4533
4534 @Override
4535 protected ConfigurationContainer getChildAt(int index) {
4536 return mStacks.get(index);
4537 }
4538
4539 @Override
4540 protected ConfigurationContainer getParent() {
4541 return ActivityStackSupervisor.this;
4542 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08004543
4544 boolean isPrivate() {
4545 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
4546 }
4547
4548 boolean isUidPresent(int uid) {
4549 for (ActivityStack stack : mStacks) {
4550 if (stack.isUidPresent(uid)) {
4551 return true;
4552 }
4553 }
4554 return false;
4555 }
4556
4557 /** Update and get all UIDs that are present on the display and have access to it. */
4558 private IntArray getPresentUIDs() {
4559 mDisplayAccessUIDs.clear();
4560 for (ActivityStack stack : mStacks) {
4561 stack.getPresentUIDs(mDisplayAccessUIDs);
4562 }
4563 return mDisplayAccessUIDs;
4564 }
Andrii Kulian250d6532017-02-08 23:30:45 -08004565
4566 boolean shouldDestroyContentOnRemove() {
4567 return mDisplay.getRemoveMode() == REMOVE_MODE_DESTROY_CONTENT;
4568 }
David Stevens9440dc82017-03-16 19:00:20 -07004569
4570 boolean shouldSleep() {
4571 return (mStacks.isEmpty() || !mAllSleepTokens.isEmpty())
4572 && (mService.mRunningVoice == null);
4573 }
4574
4575 boolean isSleeping() {
4576 return mSleeping;
4577 }
4578
4579 void setIsSleeping(boolean asleep) {
4580 mSleeping = asleep;
4581 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004582 }
4583
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004584 ActivityStack findStackBehind(ActivityStack stack) {
4585 // TODO(multi-display): We are only looking for stacks on the default display.
Jorim Jaggife762342016-10-13 14:33:27 +02004586 final ActivityDisplay display = mActivityDisplays.get(DEFAULT_DISPLAY);
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08004587 if (display == null) {
4588 return null;
4589 }
4590 final ArrayList<ActivityStack> stacks = display.mStacks;
4591 for (int i = stacks.size() - 1; i >= 0; i--) {
4592 if (stacks.get(i) == stack && i > 0) {
4593 return stacks.get(i - 1);
4594 }
4595 }
4596 throw new IllegalStateException("Failed to find a stack behind stack=" + stack
4597 + " in=" + stacks);
4598 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004599
Jorim Jaggic69bd222016-03-15 14:38:37 +01004600 /**
4601 * Puts a task into resizing mode during the next app transition.
4602 *
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004603 * @param task The task to put into resizing mode
Jorim Jaggic69bd222016-03-15 14:38:37 +01004604 */
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004605 private void setResizingDuringAnimation(TaskRecord task) {
4606 mResizingTasksDuringAnimation.add(task.taskId);
4607 task.setTaskDockedResizing(true);
Jorim Jaggic69bd222016-03-15 14:38:37 +01004608 }
4609
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004610 final int startActivityFromRecentsInner(int taskId, Bundle bOptions) {
4611 final TaskRecord task;
4612 final int callingUid;
4613 final String callingPackage;
4614 final Intent intent;
4615 final int userId;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004616 int activityType = ACTIVITY_TYPE_UNDEFINED;
4617 int windowingMode = WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004618 final ActivityOptions activityOptions = (bOptions != null)
4619 ? new ActivityOptions(bOptions) : null;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004620 if (activityOptions != null) {
4621 activityType = activityOptions.getLaunchActivityType();
4622 windowingMode = activityOptions.getLaunchWindowingMode();
4623 }
4624 if (activityType == ACTIVITY_TYPE_HOME || activityType == ACTIVITY_TYPE_RECENTS) {
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004625 throw new IllegalArgumentException("startActivityFromRecentsInner: Task "
Matthew Ngae1ff4f2016-11-10 15:49:14 -08004626 + taskId + " can't be launch in the home/recents stack.");
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004627 }
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004628
Matthew Ng606dd802017-06-05 14:06:32 -07004629 mWindowManager.deferSurfaceLayout();
4630 try {
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004631 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004632 mWindowManager.setDockedStackCreateState(
4633 activityOptions.getDockCreateMode(), null /* initialBounds */);
Jorim Jaggi3c800a42016-04-15 19:44:50 -07004634
Matthew Ng606dd802017-06-05 14:06:32 -07004635 // Defer updating the stack in which recents is until the app transition is done, to
4636 // not run into issues where we still need to draw the task in recents but the
4637 // docked stack is already created.
4638 deferUpdateBounds(RECENTS_STACK_ID);
4639 mWindowManager.prepareAppTransition(TRANSIT_DOCK_TASK_FROM_RECENTS, false);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004640 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004641
Matthew Ng606dd802017-06-05 14:06:32 -07004642 task = anyTaskForIdLocked(taskId, MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE,
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004643 activityOptions);
Matthew Ng606dd802017-06-05 14:06:32 -07004644 if (task == null) {
4645 continueUpdateBounds(RECENTS_STACK_ID);
4646 mWindowManager.executeAppTransition();
4647 throw new IllegalArgumentException(
4648 "startActivityFromRecentsInner: Task " + taskId + " not found.");
4649 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004650
Matthew Ng606dd802017-06-05 14:06:32 -07004651 // Since we don't have an actual source record here, we assume that the currently
4652 // focused activity was the source.
4653 final ActivityStack focusedStack = getFocusedStack();
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004654 final ActivityRecord sourceRecord = focusedStack != null
4655 ? focusedStack.topActivity() : null;
4656 final int stackId = getLaunchStackId(null, activityOptions, task);
Matthew Ng606dd802017-06-05 14:06:32 -07004657
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004658 if (stackId != INVALID_STACK_ID && task.getStackId() != stackId) {
4659 task.reparent(stackId, ON_TOP, REPARENT_MOVE_STACK_TO_FRONT, ANIMATE, DEFER_RESUME,
4660 "startActivityFromRecents");
Matthew Ng606dd802017-06-05 14:06:32 -07004661 }
4662
4663 // If the user must confirm credentials (e.g. when first launching a work app and the
4664 // Work Challenge is present) let startActivityInPackage handle the intercepting.
4665 if (!mService.mUserController.shouldConfirmCredentials(task.userId)
4666 && task.getRootActivity() != null) {
Bryce Lee28d80422017-07-21 13:25:13 -07004667 final ActivityRecord targetActivity = task.getTopActivity();
4668
4669 mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */,
4670 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004671 mActivityMetricsLogger.notifyActivityLaunching();
Jorim Jaggi42befc62017-06-13 11:54:04 -07004672 mService.moveTaskToFrontLocked(task.taskId, 0, bOptions, true /* fromRecents */);
Matthew Ng606dd802017-06-05 14:06:32 -07004673 mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
Bryce Lee28d80422017-07-21 13:25:13 -07004674 targetActivity);
Matthew Ng606dd802017-06-05 14:06:32 -07004675
4676 // If we are launching the task in the docked stack, put it into resizing mode so
4677 // the window renders full-screen with the background filling the void. Also only
4678 // call this at the end to make sure that tasks exists on the window manager side.
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004679 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng606dd802017-06-05 14:06:32 -07004680 setResizingDuringAnimation(task);
4681 }
4682
4683 mService.mActivityStarter.postStartActivityProcessing(task.getTopActivity(),
4684 ActivityManager.START_TASK_TO_FRONT,
4685 sourceRecord != null
4686 ? sourceRecord.getTask().getStackId() : INVALID_STACK_ID,
4687 sourceRecord, task.getStack());
4688 return ActivityManager.START_TASK_TO_FRONT;
4689 }
4690 callingUid = task.mCallingUid;
4691 callingPackage = task.mCallingPackage;
4692 intent = task.intent;
4693 intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
4694 userId = task.userId;
4695 int result = mService.startActivityInPackage(callingUid, callingPackage, intent, null,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004696 null, null, 0, 0, bOptions, userId, task, "startActivityFromRecents");
Wale Ogunwale0568aed2017-09-08 13:29:37 -07004697 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08004698 setResizingDuringAnimation(task);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004699 }
Matthew Ng606dd802017-06-05 14:06:32 -07004700 return result;
4701 } finally {
4702 mWindowManager.continueSurfaceLayout();
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004703 }
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +01004704 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004705
4706 /**
4707 * @return a list of activities which are the top ones in each visible stack. The first
4708 * entry will be the focused activity.
4709 */
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004710 List<IBinder> getTopVisibleActivities() {
4711 final ArrayList<IBinder> topActivityTokens = new ArrayList<>();
4712 // Traverse all displays.
Robert Carr22374c02017-02-21 13:01:47 -08004713 for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004714 final ActivityDisplay display = mActivityDisplays.valueAt(i);
4715 // Traverse all stacks on a display.
4716 for (int j = display.mStacks.size() - 1; j >= 0; j--) {
4717 final ActivityStack stack = display.mStacks.get(j);
4718 // Get top activity from a visible stack and add it to the list.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07004719 if (stack.shouldBeVisible(null /* starting */)
Andrii Kulian0864bbb2017-02-16 15:45:58 -08004720 == ActivityStack.STACK_VISIBLE) {
4721 final ActivityRecord top = stack.topActivity();
4722 if (top != null) {
4723 if (stack == mFocusedStack) {
4724 topActivityTokens.add(0, top.appToken);
4725 } else {
4726 topActivityTokens.add(top.appToken);
4727 }
Amith Yamasanie8222e52016-04-08 15:28:47 -07004728 }
4729 }
4730 }
4731 }
4732 return topActivityTokens;
4733 }
Bryce Lee4a194382017-04-04 14:32:48 -07004734
4735 /**
4736 * Internal container to store a match qualifier alongside a WaitResult.
4737 */
4738 static class WaitInfo {
4739 private final ComponentName mTargetComponent;
4740 private final WaitResult mResult;
4741
4742 public WaitInfo(ComponentName targetComponent, WaitResult result) {
4743 this.mTargetComponent = targetComponent;
4744 this.mResult = result;
4745 }
4746
Wale Ogunwale3270f172017-04-26 07:29:42 -07004747 public boolean matches(ComponentName targetComponent) {
4748 return mTargetComponent == null || mTargetComponent.equals(targetComponent);
Bryce Lee4a194382017-04-04 14:32:48 -07004749 }
4750
4751 public WaitResult getResult() {
4752 return mResult;
4753 }
4754
4755 public ComponentName getComponent() {
4756 return mTargetComponent;
4757 }
4758
4759 public void dump(PrintWriter pw, String prefix) {
4760 pw.println(prefix + "WaitInfo:");
4761 pw.println(prefix + " mTargetComponent=" + mTargetComponent);
4762 pw.println(prefix + " mResult=");
4763 mResult.dump(pw, prefix);
4764 }
4765 }
David Stevens9440dc82017-03-16 19:00:20 -07004766
4767 private final class SleepTokenImpl extends SleepToken {
4768 private final String mTag;
4769 private final long mAcquireTime;
4770 private final int mDisplayId;
4771
4772 public SleepTokenImpl(String tag, int displayId) {
4773 mTag = tag;
4774 mDisplayId = displayId;
4775 mAcquireTime = SystemClock.uptimeMillis();
4776 }
4777
4778 @Override
4779 public void release() {
4780 synchronized (mService) {
4781 removeSleepTokenLocked(this);
4782 }
4783 }
4784
4785 @Override
4786 public String toString() {
4787 return "{\"" + mTag + "\", display " + mDisplayId
4788 + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
4789 }
4790 }
4791
Craig Mautner27084302013-03-25 08:05:25 -07004792}